blob: b736b81f9dea2c6d6cb31f256beee9f672645216 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118 int resize_scheduled;
119 struct task resize_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500121 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400123 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400124 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400127 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500128
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500129 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500130
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500132 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400133 window_data_handler_t data_handler;
134 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500135 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400136
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500137 struct wl_list widget_list;
138 struct widget *focus_widget;
139 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400140
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141 struct window *menu;
142
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500143 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400144 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500145};
146
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500147struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500148 struct window *window;
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;
158};
159
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160struct input {
161 struct display *display;
162 struct wl_input_device *input_device;
163 struct window *pointer_focus;
164 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400165 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400166 uint32_t modifiers;
167 int32_t x, y, sx, sy;
168 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400169
170 struct wl_data_device *data_device;
171 struct data_offer *drag_offer;
172 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173};
174
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500175struct output {
176 struct display *display;
177 struct wl_output *output;
178 struct rectangle allocation;
179 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200180
181 display_output_handler_t destroy_handler;
182 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183};
184
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500185struct menu {
186 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500187 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500188 const char **entries;
189 uint32_t time;
190 int current;
191 int count;
192 menu_func_t func;
193};
194
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400195enum {
196 POINTER_DEFAULT = 100,
197 POINTER_UNSET
198};
199
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500200enum window_location {
201 WINDOW_INTERIOR = 0,
202 WINDOW_RESIZING_TOP = 1,
203 WINDOW_RESIZING_BOTTOM = 2,
204 WINDOW_RESIZING_LEFT = 4,
205 WINDOW_RESIZING_TOP_LEFT = 5,
206 WINDOW_RESIZING_BOTTOM_LEFT = 6,
207 WINDOW_RESIZING_RIGHT = 8,
208 WINDOW_RESIZING_TOP_RIGHT = 9,
209 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
210 WINDOW_RESIZING_MASK = 15,
211 WINDOW_EXTERIOR = 16,
212 WINDOW_TITLEBAR = 17,
213 WINDOW_CLIENT_AREA = 18,
214};
215
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400216const char *option_xkb_layout = "us";
217const char *option_xkb_variant = "";
218const char *option_xkb_options = "";
219
220static const GOptionEntry xkb_option_entries[] = {
221 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
222 &option_xkb_layout, "XKB Layout" },
223 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
224 &option_xkb_variant, "XKB Variant" },
225 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
226 &option_xkb_options, "XKB Options" },
227 { NULL }
228};
229
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400230static const cairo_user_data_key_t surface_data_key;
231struct surface_data {
232 struct wl_buffer *buffer;
233};
234
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500235#define MULT(_d,c,a,t) \
236 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
237
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500238#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400239
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100240struct egl_window_surface_data {
241 struct display *display;
242 struct wl_surface *surface;
243 struct wl_egl_window *window;
244 EGLSurface surf;
245};
246
247static void
248egl_window_surface_data_destroy(void *p)
249{
250 struct egl_window_surface_data *data = p;
251 struct display *d = data->display;
252
253 eglDestroySurface(d->dpy, data->surf);
254 wl_egl_window_destroy(data->window);
255 data->surface = NULL;
256
257 free(p);
258}
259
260static cairo_surface_t *
261display_create_egl_window_surface(struct display *display,
262 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400263 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100264 struct rectangle *rectangle)
265{
266 cairo_surface_t *cairo_surface;
267 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 EGLConfig config;
269 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200270 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100271
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400272 static const EGLint premul_attribs[] = {
273 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
274 EGL_NONE
275 };
276
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100277 data = malloc(sizeof *data);
278 if (data == NULL)
279 return NULL;
280
281 data->display = display;
282 data->surface = surface;
283
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400284 if (flags & SURFACE_OPAQUE) {
285 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200287 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400288 } else {
289 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200290 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400291 attribs = premul_attribs;
292 }
293
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400294 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 data->surf = eglCreateWindowSurface(display->dpy, config,
299 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300
Benjamin Franzke0c991632011-09-27 21:57:31 +0200301 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 data->surf,
303 rectangle->width,
304 rectangle->height);
305
306 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
307 data, egl_window_surface_data_destroy);
308
309 return cairo_surface;
310}
311
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 EGLImageKHR image;
316 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319};
320
321static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500333 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335}
336
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_get_image_for_egl_image_surface(struct display *display,
339 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342
343 data = cairo_surface_get_user_data (surface, &surface_data_key);
344
345 return data->image;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 EGLDisplay dpy = display->dpy;
355 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
358 data = malloc(sizeof *data);
359 if (data == NULL)
360 return NULL;
361
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800362 data->display = display;
363
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400364 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000367 free(data);
368 return NULL;
369 }
370
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 if (flags & SURFACE_OPAQUE) {
372 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR;
374 } else {
375 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 content = CAIRO_CONTENT_COLOR_ALPHA;
377 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500379 data->image = display->create_image(dpy, NULL,
380 EGL_NATIVE_PIXMAP_KHR,
381 (EGLClientBuffer) data->pixmap,
382 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500384 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500385 free(data);
386 return NULL;
387 }
388
389 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400390 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500391
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500395 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397
Benjamin Franzke0c991632011-09-27 21:57:31 +0200398 surface = cairo_gl_surface_create_for_texture(data->device,
399 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 data->texture,
401 rectangle->width,
402 rectangle->height);
403
404 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500405 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406
407 return surface;
408}
409
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500410static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500411display_create_egl_image_surface_from_file(struct display *display,
412 const char *filename,
413 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414{
415 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400416 GdkPixbuf *pixbuf;
417 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400418 int stride, i;
419 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500420 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
422 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424 FALSE, &error);
425 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400427
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400428 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
429 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500430 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400433
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400434
435 stride = gdk_pixbuf_get_rowstride(pixbuf);
436 pixels = gdk_pixbuf_get_pixels(pixbuf);
437
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400440 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 while (p < end) {
442 unsigned int t;
443
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400444 MULT(p[0], p[0], p[3], t);
445 MULT(p[1], p[1], p[3], t);
446 MULT(p[2], p[2], p[3], t);
447 p += 4;
448
449 }
450 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400451
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200452 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000453 if (surface == NULL) {
454 g_object_unref(pixbuf);
455 return NULL;
456 }
457
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 data = cairo_surface_get_user_data(surface, &surface_data_key);
459
Benjamin Franzke0c991632011-09-27 21:57:31 +0200460 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800461 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800462 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
463 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200464 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500466 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400467
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468 return surface;
469}
470
471#endif
472
473struct wl_buffer *
474display_get_buffer_for_surface(struct display *display,
475 cairo_surface_t *surface)
476{
477 struct surface_data *data;
478
479 data = cairo_surface_get_user_data (surface, &surface_data_key);
480
481 return data->buffer;
482}
483
484struct shm_surface_data {
485 struct surface_data data;
486 void *map;
487 size_t length;
488};
489
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500490static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491shm_surface_data_destroy(void *p)
492{
493 struct shm_surface_data *data = p;
494
495 wl_buffer_destroy(data->data.buffer);
496 munmap(data->map, data->length);
497}
498
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500499static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400501 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400502{
503 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400504 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400505 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800506 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507 char filename[] = "/tmp/wayland-shm-XXXXXX";
508
509 data = malloc(sizeof *data);
510 if (data == NULL)
511 return NULL;
512
513 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
514 rectangle->width);
515 data->length = stride * rectangle->height;
516 fd = mkstemp(filename);
517 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000518 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519 return NULL;
520 }
521 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000522 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 close(fd);
524 return NULL;
525 }
526
527 data->map = mmap(NULL, data->length,
528 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
529 unlink(filename);
530
531 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000532 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533 close(fd);
534 return NULL;
535 }
536
537 surface = cairo_image_surface_create_for_data (data->map,
538 CAIRO_FORMAT_ARGB32,
539 rectangle->width,
540 rectangle->height,
541 stride);
542
543 cairo_surface_set_user_data (surface, &surface_data_key,
544 data, shm_surface_data_destroy);
545
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400547 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400549 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 data->data.buffer = wl_shm_create_buffer(display->shm,
552 fd,
553 rectangle->width,
554 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 close(fd);
558
559 return surface;
560}
561
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500562static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563display_create_shm_surface_from_file(struct display *display,
564 const char *filename,
565 struct rectangle *rect)
566{
567 cairo_surface_t *surface;
568 GdkPixbuf *pixbuf;
569 GError *error = NULL;
570 int stride, i;
571 unsigned char *pixels, *p, *end, *dest_data;
572 int dest_stride;
573 uint32_t *d;
574
575 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
576 rect->width, rect->height,
577 FALSE, &error);
578 if (error != NULL)
579 return NULL;
580
581 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
582 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500583 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 return NULL;
585 }
586
587 stride = gdk_pixbuf_get_rowstride(pixbuf);
588 pixels = gdk_pixbuf_get_pixels(pixbuf);
589
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400590 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000591 if (surface == NULL) {
592 g_object_unref(pixbuf);
593 return NULL;
594 }
595
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596 dest_data = cairo_image_surface_get_data (surface);
597 dest_stride = cairo_image_surface_get_stride (surface);
598
599 for (i = 0; i < rect->height; i++) {
600 d = (uint32_t *) (dest_data + i * dest_stride);
601 p = pixels + i * stride;
602 end = p + rect->width * 4;
603 while (p < end) {
604 unsigned int t;
605 unsigned char a, r, g, b;
606
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607 a = p[3];
608 MULT(r, p[0], a, t);
609 MULT(g, p[1], a, t);
610 MULT(b, p[2], a, t);
611 p += 4;
612 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
613 }
614 }
615
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500616 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617
618 return surface;
619}
620
nobled7b87cb02011-02-01 18:51:47 +0000621static int
622check_size(struct rectangle *rect)
623{
624 if (rect->width && rect->height)
625 return 0;
626
627 fprintf(stderr, "tried to create surface of "
628 "width: %d, height: %d\n", rect->width, rect->height);
629 return -1;
630}
631
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632cairo_surface_t *
633display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400635 struct rectangle *rectangle,
636 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637{
nobled7b87cb02011-02-01 18:51:47 +0000638 if (check_size(rectangle) < 0)
639 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500641 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 if (surface)
643 return display_create_egl_window_surface(display,
644 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 rectangle);
647 else
648 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400649 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500651 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400653 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654}
655
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500656static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657display_create_surface_from_file(struct display *display,
658 const char *filename,
659 struct rectangle *rectangle)
660{
nobled7b87cb02011-02-01 18:51:47 +0000661 if (check_size(rectangle) < 0)
662 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500663#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500664 if (display->dpy) {
665 return display_create_egl_image_surface_from_file(display,
666 filename,
667 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671}
Yuval Fledel45568f62010-12-06 09:18:12 -0500672 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673 const char *filename;
674 int hotspot_x, hotspot_y;
675} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500676 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
677 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
678 { DATADIR "/weston/bottom_side.png", 16, 20 },
679 { DATADIR "/weston/grabbing.png", 20, 17 },
680 { DATADIR "/weston/left_ptr.png", 10, 5 },
681 { DATADIR "/weston/left_side.png", 10, 20 },
682 { DATADIR "/weston/right_side.png", 30, 19 },
683 { DATADIR "/weston/top_left_corner.png", 8, 8 },
684 { DATADIR "/weston/top_right_corner.png", 26, 8 },
685 { DATADIR "/weston/top_side.png", 18, 8 },
686 { DATADIR "/weston/xterm.png", 15, 15 },
687 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688};
689
690static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692{
693 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400698 display->pointer_surfaces =
699 malloc(count * sizeof *display->pointer_surfaces);
700 rect.width = width;
701 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 display_create_surface_from_file(display,
705 pointer_images[i].filename,
706 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100707 if (!display->pointer_surfaces[i]) {
708 fprintf(stderr, "Error loading pointer image: %s\n",
709 pointer_images[i].filename);
710 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400711 }
712
713}
714
Pekka Paalanen325bb602011-12-19 10:31:45 +0200715static void
716destroy_pointer_surfaces(struct display *display)
717{
718 int i, count;
719
720 count = ARRAY_LENGTH(pointer_images);
721 for (i = 0; i < count; ++i) {
722 if (display->pointer_surfaces[i])
723 cairo_surface_destroy(display->pointer_surfaces[i]);
724 }
725 free(display->pointer_surfaces);
726}
727
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728cairo_surface_t *
729display_get_pointer_surface(struct display *display, int pointer,
730 int *width, int *height,
731 int *hotspot_x, int *hotspot_y)
732{
733 cairo_surface_t *surface;
734
735 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500736#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400737 *width = cairo_gl_surface_get_width(surface);
738 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000739#else
740 *width = cairo_image_surface_get_width(surface);
741 *height = cairo_image_surface_get_height(surface);
742#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400743 *hotspot_x = pointer_images[pointer].hotspot_x;
744 *hotspot_y = pointer_images[pointer].hotspot_y;
745
746 return cairo_surface_reference(surface);
747}
748
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400749static void
750window_attach_surface(struct window *window);
751
752static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754{
755 struct window *window = data;
756
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758 cairo_surface_destroy(window->pending_surface);
759 window->pending_surface = NULL;
760 if (window->cairo_surface)
761 window_attach_surface(window);
762}
763
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400764static const struct wl_callback_listener free_surface_listener = {
765 free_surface
766};
767
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500768static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200769window_get_resize_dx_dy(struct window *window, int *x, int *y)
770{
771 if (window->resize_edges & WINDOW_RESIZING_LEFT)
772 *x = window->server_allocation.width - window->allocation.width;
773 else
774 *x = 0;
775
776 if (window->resize_edges & WINDOW_RESIZING_TOP)
777 *y = window->server_allocation.height -
778 window->allocation.height;
779 else
780 *y = 0;
781
782 window->resize_edges = 0;
783}
784
785static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400786window_set_type(struct window *window)
787{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200788 if (!window->shell_surface)
789 return;
790
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 switch (window->type) {
792 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
795 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400797 break;
798 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200799 wl_shell_surface_set_transient(window->shell_surface,
800 window->parent->shell_surface,
801 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500803 case TYPE_MENU:
804 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400805 case TYPE_CUSTOM:
806 break;
807 }
808}
809
810static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500811window_attach_surface(struct window *window)
812{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400813 struct display *display = window->display;
814 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400815 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500819 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400821 if (display->shell)
822 window_set_type(window);
823
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
827 data = cairo_surface_get_user_data(window->cairo_surface,
828 &surface_data_key);
829
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 cairo_gl_surface_swapbuffers(window->cairo_surface);
831 wl_egl_window_get_attached_size(data->window,
832 &window->server_allocation.width,
833 &window->server_allocation.height);
834 break;
835 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000836#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200838 window_get_resize_dx_dy(window, &x, &y);
839
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840 if (window->pending_surface != NULL)
841 return;
842
843 window->pending_surface = window->cairo_surface;
844 window->cairo_surface = NULL;
845
846 buffer =
847 display_get_buffer_for_surface(display,
848 window->pending_surface);
849
850 wl_surface_attach(window->surface, buffer, x, y);
851 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400852 cb = wl_display_sync(display->display);
853 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000855 default:
856 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500858
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500859 wl_surface_damage(window->surface, 0, 0,
860 window->allocation.width,
861 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500862}
863
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500864void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400865window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500866{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100867 if (window->cairo_surface) {
868 switch (window->buffer_type) {
869 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
870 case WINDOW_BUFFER_TYPE_SHM:
871 display_surface_damage(window->display,
872 window->cairo_surface,
873 0, 0,
874 window->allocation.width,
875 window->allocation.height);
876 break;
877 default:
878 break;
879 }
880 window_attach_surface(window);
881 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500882}
883
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884void
885window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400886{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500887 cairo_surface_reference(surface);
888
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889 if (window->cairo_surface != NULL)
890 cairo_surface_destroy(window->cairo_surface);
891
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500892 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893}
894
Benjamin Franzke22d54812011-07-16 19:50:32 +0000895#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896static void
897window_resize_cairo_window_surface(struct window *window)
898{
899 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200900 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901
902 data = cairo_surface_get_user_data(window->cairo_surface,
903 &surface_data_key);
904
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 wl_egl_window_resize(data->window,
907 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 window->allocation.height,
909 x,y);
910
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 cairo_gl_surface_set_size(window->cairo_surface,
912 window->allocation.width,
913 window->allocation.height);
914}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000915#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400917struct display *
918window_get_display(struct window *window)
919{
920 return window->display;
921}
922
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400923void
924window_create_surface(struct window *window)
925{
926 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 uint32_t flags = 0;
928
929 if (!window->transparent)
930 flags = SURFACE_OPAQUE;
931
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500933#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100934 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
935 if (window->cairo_surface) {
936 window_resize_cairo_window_surface(window);
937 return;
938 }
939 surface = display_create_surface(window->display,
940 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400941 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500943 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100945 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400946 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947 break;
948#endif
949 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400950 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800953 default:
954 surface = NULL;
955 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400957
958 window_set_surface(window, surface);
959 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960}
961
962static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500963window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500964{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500965 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500966 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400967 cairo_surface_t *frame;
968 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500969
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400970 width = window->allocation.width;
971 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500972
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500973 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500974
Kristian Høgsberg09531622010-06-14 23:22:15 -0400975 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400976 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400977 cairo_paint(cr);
978
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400979 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400980 tile_mask(cr, window->display->shadow,
981 shadow_dx, shadow_dy, width, height,
982 window->margin + 10 - shadow_dx,
983 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500984
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400985 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400986 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400987 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400988 frame = window->display->inactive_frame;
989
990 tile_source(cr, frame, 0, 0, width, height,
991 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500992
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500993 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
994 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500995 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400996 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400997 if (window->keyboard_device)
998 cairo_set_source_rgb(cr, 0, 0, 0);
999 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001000 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001001 cairo_show_text(cr, window->title);
1002
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001003 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001004}
1005
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001006void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001007window_destroy(struct window *window)
1008{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001009 struct display *display = window->display;
1010 struct input *input;
1011
1012 if (window->redraw_scheduled)
1013 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001014 if (window->resize_scheduled)
1015 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001016
1017 wl_list_for_each(input, &display->input_list, link) {
1018 if (input->pointer_focus == window)
1019 input->pointer_focus = NULL;
1020 if (input->keyboard_focus == window)
1021 input->keyboard_focus = NULL;
1022 }
1023
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001024 if (window->shell_surface)
1025 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001026 wl_surface_destroy(window->surface);
1027 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001028
1029 if (window->cairo_surface != NULL)
1030 cairo_surface_destroy(window->cairo_surface);
1031 if (window->pending_surface != NULL)
1032 cairo_surface_destroy(window->pending_surface);
1033
1034 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001035 free(window);
1036}
1037
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038static struct widget *
1039window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001043 wl_list_for_each(widget, &window->widget_list, link) {
1044 if (widget->allocation.x <= x &&
1045 x < widget->allocation.x + widget->allocation.width &&
1046 widget->allocation.y <= y &&
1047 y < widget->allocation.y + widget->allocation.height) {
1048 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001049 }
1050 }
1051
1052 return NULL;
1053}
1054
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001055struct widget *
1056window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 widget = malloc(sizeof *widget);
1061 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001062 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001064 widget->allocation = window->allocation;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05001065 wl_list_insert(&window->widget_list, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001067 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068}
1069
1070void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001075 wl_list_for_each(widget, &window->widget_list, link)
1076 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077}
1078
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001079struct widget *
1080window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001082 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001083}
1084
1085void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001088 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
1091void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092widget_set_allocation(struct widget *widget,
1093 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001094{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001095 widget->allocation.x = x;
1096 widget->allocation.y = y;
1097 widget->allocation.width = width;
1098 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
1101void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001104 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001105}
1106
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001107void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001108widget_set_resize_handler(struct widget *widget,
1109 widget_resize_handler_t handler)
1110{
1111 widget->resize_handler = handler;
1112}
1113
1114void
1115widget_set_redraw_handler(struct widget *widget,
1116 widget_redraw_handler_t handler)
1117{
1118 widget->redraw_handler = handler;
1119}
1120
1121void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001122widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001123{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001124 widget->enter_handler = handler;
1125}
1126
1127void
1128widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1129{
1130 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001131}
1132
1133void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001134widget_set_motion_handler(struct widget *widget,
1135 widget_motion_handler_t handler)
1136{
1137 widget->motion_handler = handler;
1138}
1139
1140void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001141widget_set_button_handler(struct widget *widget,
1142 widget_button_handler_t handler)
1143{
1144 widget->button_handler = handler;
1145}
1146
1147void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001148widget_schedule_redraw(struct widget *widget)
1149{
1150 window_schedule_redraw(widget->window);
1151}
1152
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001153cairo_surface_t *
1154window_get_surface(struct window *window)
1155{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001156 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001157}
1158
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001159struct wl_surface *
1160window_get_wl_surface(struct window *window)
1161{
1162 return window->surface;
1163}
1164
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001165struct wl_shell_surface *
1166window_get_wl_shell_surface(struct window *window)
1167{
1168 return window->shell_surface;
1169}
1170
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171static int
1172get_pointer_location(struct window *window, int32_t x, int32_t y)
1173{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001174 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001175 const int grip_size = 8;
1176
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001177 if (!window->decoration)
1178 return WINDOW_CLIENT_AREA;
1179
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001180 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001182 else if (window->margin <= x && x < window->margin + grip_size)
1183 hlocation = WINDOW_RESIZING_LEFT;
1184 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001185 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001186 else if (x < window->allocation.width - window->margin)
1187 hlocation = WINDOW_RESIZING_RIGHT;
1188 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001189 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001190
1191 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001192 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 else if (window->margin <= y && y < window->margin + grip_size)
1194 vlocation = WINDOW_RESIZING_TOP;
1195 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001196 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197 else if (y < window->allocation.height - window->margin)
1198 vlocation = WINDOW_RESIZING_BOTTOM;
1199 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001200 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001201
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001202 location = vlocation | hlocation;
1203 if (location & WINDOW_EXTERIOR)
1204 location = WINDOW_EXTERIOR;
1205 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1206 location = WINDOW_TITLEBAR;
1207 else if (location == WINDOW_INTERIOR)
1208 location = WINDOW_CLIENT_AREA;
1209
1210 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001211}
1212
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001213static int
1214input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217
1218 location = get_pointer_location(input->pointer_focus,
1219 input->sx, input->sy);
1220 switch (location) {
1221 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001222 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001224 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001226 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001228 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001230 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001232 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001233 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001234 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001236 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001237 case WINDOW_EXTERIOR:
1238 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001239 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001241 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001243}
1244
1245void
1246input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1247{
1248 struct display *display = input->display;
1249 struct wl_buffer *buffer;
1250 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001252 if (pointer == input->current_pointer_image)
1253 return;
1254
1255 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001256 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001257
1258 if (!surface)
1259 return;
1260
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001262 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 pointer_images[pointer].hotspot_x,
1264 pointer_images[pointer].hotspot_y);
1265}
1266
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001267static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001268window_set_focus_widget(struct window *window, struct widget *focus,
1269 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001270{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001271 struct widget *old;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001272 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001273
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001274 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001275 return;
1276
Kristian Høgsbergee143232012-01-09 08:42:24 -05001277 old = window->focus_widget;
1278 if (old) {
1279 if (old->leave_handler)
1280 old->leave_handler(old, input, old->user_data);
1281 window->focus_widget = NULL;
1282 }
1283
1284 if (focus) {
1285 if (focus->enter_handler)
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001286 pointer = focus->enter_handler(focus, input, time,
1287 x, y, focus->user_data);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001288 window->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001289
1290 pointer = input_get_pointer_image_for_location(input, pointer);
1291 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001292 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293}
1294
1295static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001296input_handle_motion(void *data, struct wl_input_device *input_device,
1297 uint32_t time,
1298 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001299{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001300 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001302 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001303 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001304
1305 input->x = x;
1306 input->y = y;
1307 input->sx = sx;
1308 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001310 if (!window->focus_widget || !window->widget_grab_button) {
1311 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001312 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313 }
1314
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001315 widget = window->focus_widget;
1316 if (widget && widget->motion_handler)
1317 pointer = widget->motion_handler(widget, input, time, sx, sy,
1318 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001319
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001320 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001321 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001322}
1323
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001324static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001325window_menu_func(struct window *window, int index, void *data)
1326{
1327 switch (index) {
1328 case 0: /* close */
1329 if (window->close_handler)
1330 window->close_handler(window->parent,
1331 window->user_data);
1332 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001333 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001334 break;
1335 case 1: /* fullscreen */
1336 /* we don't have a way to get out of fullscreen for now */
1337 window_set_fullscreen(window, 1);
1338 break;
1339 case 2: /* rotate */
1340 case 3: /* scale */
1341 break;
1342 }
1343}
1344
1345
1346static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001347input_handle_button(void *data,
1348 struct wl_input_device *input_device,
1349 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001350{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001351 struct input *input = data;
1352 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001353 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001354 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001355 int32_t x, y;
1356 static const char *entries[] = {
1357 "Close", "Fullscreen", "Rotate", "Scale"
1358 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001359
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001360 if (window->focus_widget && window->widget_grab_button == 0 && state)
1361 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001362
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001363 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001364
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001365 widget = window->focus_widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001366 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001367 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001368 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001369 if (!window->shell_surface)
1370 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001371 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001372 wl_shell_surface_move(window->shell_surface,
1373 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001374 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001375 case WINDOW_RESIZING_TOP:
1376 case WINDOW_RESIZING_BOTTOM:
1377 case WINDOW_RESIZING_LEFT:
1378 case WINDOW_RESIZING_RIGHT:
1379 case WINDOW_RESIZING_TOP_LEFT:
1380 case WINDOW_RESIZING_TOP_RIGHT:
1381 case WINDOW_RESIZING_BOTTOM_LEFT:
1382 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001383 if (!window->shell_surface)
1384 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001385 wl_shell_surface_resize(window->shell_surface,
1386 input_device, time,
1387 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001388 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001389 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001390 if (widget && widget->button_handler)
1391 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001392 input, time,
1393 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001394 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001395 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001396 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001397 } else if (button == BTN_RIGHT && state == 1) {
1398 switch (location) {
1399 default:
1400 input_get_position(input, &x, &y);
1401 window->menu = window_create_menu(window->display,
1402 input, time,
1403 window,
1404 x - 10, y - 10,
1405 window_menu_func,
1406 entries, 4);
1407 window_schedule_redraw(window->menu);
1408 break;
1409 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001410 if (widget && widget->button_handler)
1411 (*widget->button_handler)(widget,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001412 input, time,
1413 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001414 widget->user_data);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001415 break;
1416 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001417 } else {
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001418 if (widget && widget->button_handler)
1419 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001420 input, time,
1421 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001422 widget->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001423 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001425 if (window->focus_widget &&
1426 window->widget_grab_button == button && !state) {
1427 window->widget_grab_button = 0;
1428 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001429 window_set_focus_widget(window, widget, input, time,
1430 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001432}
1433
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001434static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001435input_handle_key(void *data, struct wl_input_device *input_device,
1436 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001437{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001438 struct input *input = data;
1439 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001440 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001441 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001442
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001443 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001444 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001445 return;
1446
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001447 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001448 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001449 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1450 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001451
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001452 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1453
1454 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001455 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001456 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001457 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001458
1459 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001460 (*window->key_handler)(window, input, time, key, sym, state,
1461 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001462}
1463
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001464static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001465input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001466{
1467 struct window *window = input->pointer_focus;
1468
1469 if (!window)
1470 return;
1471
Kristian Høgsbergee143232012-01-09 08:42:24 -05001472 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001473
Pekka Paalanene1207c72011-12-16 12:02:09 +02001474 input->pointer_focus = NULL;
1475 input->current_pointer_image = POINTER_UNSET;
1476}
1477
1478static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001479input_handle_pointer_focus(void *data,
1480 struct wl_input_device *input_device,
1481 uint32_t time, struct wl_surface *surface,
1482 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001483{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001484 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001485 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001486 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001487 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001488
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001489 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001490 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001491 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001492
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001493 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001494 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001495 window = input->pointer_focus;
1496
Kristian Høgsberg59826582011-01-20 11:56:57 -05001497 input->x = x;
1498 input->y = y;
1499 input->sx = sx;
1500 input->sy = sy;
1501
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001502 pointer = POINTER_LEFT_PTR;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001503 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001504 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001505
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001506 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001507 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001509}
1510
1511static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001512input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001513{
1514 struct window *window = input->keyboard_focus;
1515
1516 if (!window)
1517 return;
1518
1519 window->keyboard_device = NULL;
1520 if (window->keyboard_focus_handler)
1521 (*window->keyboard_focus_handler)(window, NULL,
1522 window->user_data);
1523
1524 input->keyboard_focus = NULL;
1525}
1526
1527static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001528input_handle_keyboard_focus(void *data,
1529 struct wl_input_device *input_device,
1530 uint32_t time,
1531 struct wl_surface *surface,
1532 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001533{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001534 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001535 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001536 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001537 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001538
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001539 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001540
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001541 if (surface)
1542 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001543
1544 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001545 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001546 for (k = keys->data; k < end; k++)
1547 input->modifiers |= d->xkb->map->modmap[*k];
1548
1549 window = input->keyboard_focus;
1550 if (window) {
1551 window->keyboard_device = input;
1552 if (window->keyboard_focus_handler)
1553 (*window->keyboard_focus_handler)(window,
1554 window->keyboard_device,
1555 window->user_data);
1556 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001557}
1558
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001559static void
1560input_handle_touch_down(void *data,
1561 struct wl_input_device *wl_input_device,
1562 uint32_t time, struct wl_surface *surface,
1563 int32_t id, int32_t x, int32_t y)
1564{
1565}
1566
1567static void
1568input_handle_touch_up(void *data,
1569 struct wl_input_device *wl_input_device,
1570 uint32_t time, int32_t id)
1571{
1572}
1573
1574static void
1575input_handle_touch_motion(void *data,
1576 struct wl_input_device *wl_input_device,
1577 uint32_t time, int32_t id, int32_t x, int32_t y)
1578{
1579}
1580
1581static void
1582input_handle_touch_frame(void *data,
1583 struct wl_input_device *wl_input_device)
1584{
1585}
1586
1587static void
1588input_handle_touch_cancel(void *data,
1589 struct wl_input_device *wl_input_device)
1590{
1591}
1592
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001593static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001594 input_handle_motion,
1595 input_handle_button,
1596 input_handle_key,
1597 input_handle_pointer_focus,
1598 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001599 input_handle_touch_down,
1600 input_handle_touch_up,
1601 input_handle_touch_motion,
1602 input_handle_touch_frame,
1603 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001604};
1605
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001606void
1607input_get_position(struct input *input, int32_t *x, int32_t *y)
1608{
1609 *x = input->sx;
1610 *y = input->sy;
1611}
1612
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001613struct wl_input_device *
1614input_get_input_device(struct input *input)
1615{
1616 return input->input_device;
1617}
1618
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001619uint32_t
1620input_get_modifiers(struct input *input)
1621{
1622 return input->modifiers;
1623}
1624
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001625struct data_offer {
1626 struct wl_data_offer *offer;
1627 struct input *input;
1628 struct wl_array types;
1629 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001630
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001631 struct task io_task;
1632 int fd;
1633 data_func_t func;
1634 int32_t x, y;
1635 void *user_data;
1636};
1637
1638static void
1639data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1640{
1641 struct data_offer *offer = data;
1642 char **p;
1643
1644 p = wl_array_add(&offer->types, sizeof *p);
1645 *p = strdup(type);
1646}
1647
1648static const struct wl_data_offer_listener data_offer_listener = {
1649 data_offer_offer,
1650};
1651
1652static void
1653data_offer_destroy(struct data_offer *offer)
1654{
1655 char **p;
1656
1657 offer->refcount--;
1658 if (offer->refcount == 0) {
1659 wl_data_offer_destroy(offer->offer);
1660 for (p = offer->types.data; *p; p++)
1661 free(*p);
1662 wl_array_release(&offer->types);
1663 free(offer);
1664 }
1665}
1666
1667static void
1668data_device_data_offer(void *data,
1669 struct wl_data_device *data_device, uint32_t id)
1670{
1671 struct data_offer *offer;
1672
1673 offer = malloc(sizeof *offer);
1674
1675 wl_array_init(&offer->types);
1676 offer->refcount = 1;
1677 offer->input = data;
1678
1679 /* FIXME: Generate typesafe wrappers for this */
1680 offer->offer = (struct wl_data_offer *)
1681 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1682 id, &wl_data_offer_interface);
1683
1684 wl_data_offer_add_listener(offer->offer,
1685 &data_offer_listener, offer);
1686}
1687
1688static void
1689data_device_enter(void *data, struct wl_data_device *data_device,
1690 uint32_t time, struct wl_surface *surface,
1691 int32_t x, int32_t y, struct wl_data_offer *offer)
1692{
1693 struct input *input = data;
1694 struct window *window;
1695 char **p;
1696
1697 input->drag_offer = wl_data_offer_get_user_data(offer);
1698 window = wl_surface_get_user_data(surface);
1699 input->pointer_focus = window;
1700
1701 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1702 *p = NULL;
1703
1704 window = input->pointer_focus;
1705 if (window->data_handler)
1706 window->data_handler(window, input, time, x, y,
1707 input->drag_offer->types.data,
1708 window->user_data);
1709}
1710
1711static void
1712data_device_leave(void *data, struct wl_data_device *data_device)
1713{
1714 struct input *input = data;
1715
1716 data_offer_destroy(input->drag_offer);
1717 input->drag_offer = NULL;
1718}
1719
1720static void
1721data_device_motion(void *data, struct wl_data_device *data_device,
1722 uint32_t time, int32_t x, int32_t y)
1723{
1724 struct input *input = data;
1725 struct window *window = input->pointer_focus;
1726
1727 input->sx = x;
1728 input->sy = y;
1729
1730 if (window->data_handler)
1731 window->data_handler(window, input, time, x, y,
1732 input->drag_offer->types.data,
1733 window->user_data);
1734}
1735
1736static void
1737data_device_drop(void *data, struct wl_data_device *data_device)
1738{
1739 struct input *input = data;
1740 struct window *window = input->pointer_focus;
1741
1742 if (window->drop_handler)
1743 window->drop_handler(window, input,
1744 input->sx, input->sy, window->user_data);
1745}
1746
1747static void
1748data_device_selection(void *data,
1749 struct wl_data_device *wl_data_device,
1750 struct wl_data_offer *offer)
1751{
1752 struct input *input = data;
1753 char **p;
1754
1755 if (input->selection_offer)
1756 data_offer_destroy(input->selection_offer);
1757
1758 input->selection_offer = wl_data_offer_get_user_data(offer);
1759 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1760 *p = NULL;
1761}
1762
1763static const struct wl_data_device_listener data_device_listener = {
1764 data_device_data_offer,
1765 data_device_enter,
1766 data_device_leave,
1767 data_device_motion,
1768 data_device_drop,
1769 data_device_selection
1770};
1771
1772struct wl_data_device *
1773input_get_data_device(struct input *input)
1774{
1775 return input->data_device;
1776}
1777
1778void
1779input_set_selection(struct input *input,
1780 struct wl_data_source *source, uint32_t time)
1781{
1782 wl_data_device_set_selection(input->data_device, source, time);
1783}
1784
1785void
1786input_accept(struct input *input, uint32_t time, const char *type)
1787{
1788 wl_data_offer_accept(input->drag_offer->offer, time, type);
1789}
1790
1791static void
1792offer_io_func(struct task *task, uint32_t events)
1793{
1794 struct data_offer *offer =
1795 container_of(task, struct data_offer, io_task);
1796 unsigned int len;
1797 char buffer[4096];
1798
1799 len = read(offer->fd, buffer, sizeof buffer);
1800 offer->func(buffer, len,
1801 offer->x, offer->y, offer->user_data);
1802
1803 if (len == 0) {
1804 close(offer->fd);
1805 data_offer_destroy(offer);
1806 }
1807}
1808
1809static void
1810data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1811 data_func_t func, void *user_data)
1812{
1813 int p[2];
1814
1815 pipe2(p, O_CLOEXEC);
1816 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1817 close(p[1]);
1818
1819 offer->io_task.run = offer_io_func;
1820 offer->fd = p[0];
1821 offer->func = func;
1822 offer->refcount++;
1823 offer->user_data = user_data;
1824
1825 display_watch_fd(offer->input->display,
1826 offer->fd, EPOLLIN, &offer->io_task);
1827}
1828
1829void
1830input_receive_drag_data(struct input *input, const char *mime_type,
1831 data_func_t func, void *data)
1832{
1833 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1834 input->drag_offer->x = input->sx;
1835 input->drag_offer->y = input->sy;
1836}
1837
1838int
1839input_receive_selection_data(struct input *input, const char *mime_type,
1840 data_func_t func, void *data)
1841{
1842 char **p;
1843
1844 if (input->selection_offer == NULL)
1845 return -1;
1846
1847 for (p = input->selection_offer->types.data; *p; p++)
1848 if (strcmp(mime_type, *p) == 0)
1849 break;
1850
1851 if (*p == NULL)
1852 return -1;
1853
1854 data_offer_receive_data(input->selection_offer,
1855 mime_type, func, data);
1856 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001857}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001858
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001859int
1860input_receive_selection_data_to_fd(struct input *input,
1861 const char *mime_type, int fd)
1862{
1863 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1864
1865 return 0;
1866}
1867
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001868void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001869window_move(struct window *window, struct input *input, uint32_t time)
1870{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001871 if (!window->shell_surface)
1872 return;
1873
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001874 wl_shell_surface_move(window->shell_surface,
1875 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001876}
1877
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001878static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001879window_resize(struct window *window, int32_t width, int32_t height)
1880{
1881 struct widget *widget;
1882 struct rectangle allocation;
1883
1884 if (window->decoration) {
1885 allocation.x = 20;
1886 allocation.y = 60;
1887 allocation.width = width - 20 - window->margin * 2;
1888 allocation.height = height - 60 - window->margin * 2;
1889 } else {
1890 allocation.x = 0;
1891 allocation.y = 0;
1892 allocation.width = width;
1893 allocation.height = height;
1894 }
1895
1896 window->allocation.width = width;
1897 window->allocation.height = height;
1898
1899 wl_list_for_each(widget, &window->widget_list, link) {
1900 if (widget->resize_handler)
1901 widget->resize_handler(widget,
1902 allocation.width,
1903 allocation.height,
1904 widget->user_data);
1905 else
1906 widget->allocation = allocation;
1907 }
1908
1909 window_schedule_redraw(window);
1910}
1911
1912static void
1913idle_resize(struct task *task, uint32_t events)
1914{
1915 struct window *window =
1916 container_of(task, struct window, resize_task);
1917
1918 window_resize(window,
1919 window->allocation.width, window->allocation.height);
1920 window->resize_scheduled = 0;
1921}
1922
1923void
1924window_schedule_resize(struct window *window, int width, int height)
1925{
1926 if (!window->resize_scheduled) {
1927 window->allocation.width = width;
1928 window->allocation.height = height;
1929 window->resize_task.run = idle_resize;
1930 display_defer(window->display, &window->resize_task);
1931 window->resize_scheduled = 1;
1932 }
1933}
1934
1935static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001936handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001937 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001938 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001939{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001940 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001941
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001942 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001943 return;
1944
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001945 window->resize_edges = edges;
1946
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001947 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001948}
1949
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001950static void
1951handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1952{
1953 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001954 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955 /* FIXME: Need more context in this event, at least the input
1956 * device. Or just use wl_callback. */
1957
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001958 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001959 window_destroy(window);
1960}
1961
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001962static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001963 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001964 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001965};
1966
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001967void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001968window_get_allocation(struct window *window,
1969 struct rectangle *allocation)
1970{
1971 *allocation = window->allocation;
1972}
1973
1974void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001975window_get_child_allocation(struct window *window,
1976 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001977{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001978 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001979 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001980 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001981 allocation->x = window->margin + 10;
1982 allocation->y = window->margin + 50;
1983 allocation->width =
1984 window->allocation.width - 20 - window->margin * 2;
1985 allocation->height =
1986 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001987 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001988}
1989
1990void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001991window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001992{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001993 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001994 window->allocation.x = 20 + window->margin;
1995 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001996 window->allocation.width = width + 20 + window->margin * 2;
1997 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001998 } else {
1999 window->allocation.x = 0;
2000 window->allocation.y = 0;
2001 window->allocation.width = width;
2002 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002003 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05002004}
2005
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002006static void
2007idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002008{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002009 struct window *window =
2010 container_of(task, struct window, redraw_task);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002011 struct widget *widget;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002012
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002013 window_create_surface(window);
2014 if (window->decoration)
2015 window_draw_decorations(window);
2016
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002017 wl_list_for_each_reverse(widget, &window->widget_list, link)
2018 widget->redraw_handler(widget, widget->user_data);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002019
2020 window_flush(window);
2021
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002022 window->redraw_scheduled = 0;
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002023
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002024}
2025
2026void
2027window_schedule_redraw(struct window *window)
2028{
2029 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002030 window->redraw_task.run = idle_redraw;
2031 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002032 window->redraw_scheduled = 1;
2033 }
2034}
2035
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002036void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002037window_set_custom(struct window *window)
2038{
2039 window->type = TYPE_CUSTOM;
2040}
2041
2042void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002043window_set_fullscreen(struct window *window, int fullscreen)
2044{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002045 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002046 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002047
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002048 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002049 return;
2050
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002051 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002052 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002053 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002054 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002055 width = output->allocation.width;
2056 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002057 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002058 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002059 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002060 width = window->saved_allocation.width - 20 - window->margin * 2;
2061 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002062 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002063 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002064
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002065 window_resize(window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002066}
2067
2068void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002069window_set_decoration(struct window *window, int decoration)
2070{
2071 window->decoration = decoration;
2072}
2073
2074void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002075window_set_user_data(struct window *window, void *data)
2076{
2077 window->user_data = data;
2078}
2079
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002080void *
2081window_get_user_data(struct window *window)
2082{
2083 return window->user_data;
2084}
2085
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002086void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002087window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002088 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002089{
2090 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002091}
2092
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002093void
2094window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002095 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002096{
2097 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002098}
2099
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002100void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002101window_set_data_handler(struct window *window, window_data_handler_t handler)
2102{
2103 window->data_handler = handler;
2104}
2105
2106void
2107window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2108{
2109 window->drop_handler = handler;
2110}
2111
2112void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002113window_set_close_handler(struct window *window,
2114 window_close_handler_t handler)
2115{
2116 window->close_handler = handler;
2117}
2118
2119void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002120window_set_transparent(struct window *window, int transparent)
2121{
2122 window->transparent = transparent;
2123}
2124
2125void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002126window_set_title(struct window *window, const char *title)
2127{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002128 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002129 window->title = strdup(title);
2130}
2131
2132const char *
2133window_get_title(struct window *window)
2134{
2135 return window->title;
2136}
2137
2138void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002139display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2140 int32_t x, int32_t y, int32_t width, int32_t height)
2141{
2142 struct wl_buffer *buffer;
2143
2144 buffer = display_get_buffer_for_surface(display, cairo_surface);
2145
2146 wl_buffer_damage(buffer, x, y, width, height);
2147}
2148
2149void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002150window_damage(struct window *window, int32_t x, int32_t y,
2151 int32_t width, int32_t height)
2152{
2153 wl_surface_damage(window->surface, x, y, width, height);
2154}
2155
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002156static struct window *
2157window_create_internal(struct display *display, struct window *parent,
2158 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002159{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002160 struct window *window;
2161
2162 window = malloc(sizeof *window);
2163 if (window == NULL)
2164 return NULL;
2165
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002166 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002167 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002168 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002169 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002170 if (display->shell) {
2171 window->shell_surface =
2172 wl_shell_get_shell_surface(display->shell,
2173 window->surface);
2174 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002175 window->allocation.x = 0;
2176 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002177 window->allocation.width = width;
2178 window->allocation.height = height;
2179 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002180 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002181 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002182 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002183
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002184 wl_list_init(&window->widget_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002185
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002186 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002187#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002188 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2189 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002190#else
2191 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2192#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002193 else
2194 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002195
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002196 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002197 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002198
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002199 if (window->shell_surface) {
2200 wl_shell_surface_set_user_data(window->shell_surface, window);
2201 wl_shell_surface_add_listener(window->shell_surface,
2202 &shell_surface_listener, window);
2203 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002204
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002205 return window;
2206}
2207
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002208struct window *
2209window_create(struct display *display, int32_t width, int32_t height)
2210{
2211 struct window *window;
2212
2213 window = window_create_internal(display, NULL, width, height);
2214 if (!window)
2215 return NULL;
2216
2217 return window;
2218}
2219
2220struct window *
2221window_create_transient(struct display *display, struct window *parent,
2222 int32_t x, int32_t y, int32_t width, int32_t height)
2223{
2224 struct window *window;
2225
2226 window = window_create_internal(parent->display,
2227 parent, width, height);
2228 if (!window)
2229 return NULL;
2230
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002231 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002232 window->x = x;
2233 window->y = y;
2234
2235 return window;
2236}
2237
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002238static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002239menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002240{
2241 int next;
2242
2243 next = (sy - 8) / 20;
2244 if (menu->current != next) {
2245 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002246 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002247 }
2248
2249 return POINTER_LEFT_PTR;
2250}
2251
2252static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002253menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002254 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002255 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002256{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002257 return menu_set_item(data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002258}
2259
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002260static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002261menu_enter_handler(struct widget *widget,
2262 struct input *input, uint32_t time,
2263 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002264{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002265 return menu_set_item(data, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002266}
2267
2268static void
2269menu_leave_handler(struct widget *widget, struct input *input, void *data)
2270{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002271 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002272}
2273
2274static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002275menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002276 struct input *input, uint32_t time,
2277 int button, int state, void *data)
2278
2279{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002280 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002281
2282 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002283 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002284 menu->func(menu->window->parent,
2285 menu->current, menu->window->parent->user_data);
2286 window_destroy(widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002287 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002288}
2289
2290static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002291menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002292{
2293 cairo_t *cr;
2294 const int32_t r = 3, margin = 3;
2295 struct menu *menu = data;
2296 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002297 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002298
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002299 cr = cairo_create(window->cairo_surface);
2300 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2301 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2302 cairo_paint(cr);
2303
2304 width = window->allocation.width;
2305 height = window->allocation.height;
2306 rounded_rect(cr, 0, 0, width, height, r);
2307 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2308 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2309 cairo_fill(cr);
2310
2311 for (i = 0; i < menu->count; i++) {
2312 if (i == menu->current) {
2313 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2314 cairo_rectangle(cr, margin, i * 20 + margin,
2315 width - 2 * margin, 20);
2316 cairo_fill(cr);
2317 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2318 cairo_move_to(cr, 10, i * 20 + 16);
2319 cairo_show_text(cr, menu->entries[i]);
2320 } else {
2321 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2322 cairo_move_to(cr, 10, i * 20 + 16);
2323 cairo_show_text(cr, menu->entries[i]);
2324 }
2325 }
2326
2327 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002328}
2329
2330struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002331window_create_menu(struct display *display,
2332 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002333 int32_t x, int32_t y,
2334 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002335{
2336 struct window *window;
2337 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002338 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002339
2340 menu = malloc(sizeof *menu);
2341 if (!menu)
2342 return NULL;
2343
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002344 window = window_create_internal(parent->display, parent,
2345 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346 if (!window)
2347 return NULL;
2348
2349 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002350 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351 menu->entries = entries;
2352 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002353 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002354 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002355 window->decoration = 0;
2356 window->type = TYPE_MENU;
2357 window->x = x;
2358 window->y = y;
2359
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002360 wl_shell_surface_set_popup(window->shell_surface,
2361 input->input_device, time,
2362 window->parent->shell_surface,
2363 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002364
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002365 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002366 widget_set_enter_handler(menu->widget, menu_enter_handler);
2367 widget_set_leave_handler(menu->widget, menu_leave_handler);
2368 widget_set_motion_handler(menu->widget, menu_motion_handler);
2369 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002370
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371 return window;
2372}
2373
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002374void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002375window_set_buffer_type(struct window *window, enum window_buffer_type type)
2376{
2377 window->buffer_type = type;
2378}
2379
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002380
2381static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002382display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002383 struct wl_output *wl_output,
2384 int x, int y,
2385 int physical_width,
2386 int physical_height,
2387 int subpixel,
2388 const char *make,
2389 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002390{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002391 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002392
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002393 output->allocation.x = x;
2394 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002395}
2396
2397static void
2398display_handle_mode(void *data,
2399 struct wl_output *wl_output,
2400 uint32_t flags,
2401 int width,
2402 int height,
2403 int refresh)
2404{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002405 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002406 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002407
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002408 if (flags & WL_OUTPUT_MODE_CURRENT) {
2409 output->allocation.width = width;
2410 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002411 if (display->output_configure_handler)
2412 (*display->output_configure_handler)(
2413 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002414 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002415}
2416
2417static const struct wl_output_listener output_listener = {
2418 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002419 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002420};
2421
2422static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002423display_add_output(struct display *d, uint32_t id)
2424{
2425 struct output *output;
2426
2427 output = malloc(sizeof *output);
2428 if (output == NULL)
2429 return;
2430
2431 memset(output, 0, sizeof *output);
2432 output->display = d;
2433 output->output =
2434 wl_display_bind(d->display, id, &wl_output_interface);
2435 wl_list_insert(d->output_list.prev, &output->link);
2436
2437 wl_output_add_listener(output->output, &output_listener, output);
2438}
2439
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002440static void
2441output_destroy(struct output *output)
2442{
2443 if (output->destroy_handler)
2444 (*output->destroy_handler)(output, output->user_data);
2445
2446 wl_output_destroy(output->output);
2447 wl_list_remove(&output->link);
2448 free(output);
2449}
2450
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002451void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002452display_set_output_configure_handler(struct display *display,
2453 display_output_handler_t handler)
2454{
2455 struct output *output;
2456
2457 display->output_configure_handler = handler;
2458 if (!handler)
2459 return;
2460
2461 wl_list_for_each(output, &display->output_list, link)
2462 (*display->output_configure_handler)(output,
2463 display->user_data);
2464}
2465
2466void
2467output_set_user_data(struct output *output, void *data)
2468{
2469 output->user_data = data;
2470}
2471
2472void *
2473output_get_user_data(struct output *output)
2474{
2475 return output->user_data;
2476}
2477
2478void
2479output_set_destroy_handler(struct output *output,
2480 display_output_handler_t handler)
2481{
2482 output->destroy_handler = handler;
2483 /* FIXME: implement this, once we have way to remove outputs */
2484}
2485
2486void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002487output_get_allocation(struct output *output, struct rectangle *allocation)
2488{
2489 *allocation = output->allocation;
2490}
2491
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002492struct wl_output *
2493output_get_wl_output(struct output *output)
2494{
2495 return output->output;
2496}
2497
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002498static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002499display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002500{
2501 struct input *input;
2502
2503 input = malloc(sizeof *input);
2504 if (input == NULL)
2505 return;
2506
2507 memset(input, 0, sizeof *input);
2508 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002509 input->input_device =
2510 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002511 input->pointer_focus = NULL;
2512 input->keyboard_focus = NULL;
2513 wl_list_insert(d->input_list.prev, &input->link);
2514
2515 wl_input_device_add_listener(input->input_device,
2516 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002517 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002518
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002519 input->data_device =
2520 wl_data_device_manager_get_data_device(d->data_device_manager,
2521 input->input_device);
2522 wl_data_device_add_listener(input->data_device,
2523 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002524}
2525
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002526static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002527input_destroy(struct input *input)
2528{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002529 input_remove_keyboard_focus(input);
2530 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002531
2532 if (input->drag_offer)
2533 data_offer_destroy(input->drag_offer);
2534
2535 if (input->selection_offer)
2536 data_offer_destroy(input->selection_offer);
2537
2538 wl_data_device_destroy(input->data_device);
2539 wl_list_remove(&input->link);
2540 wl_input_device_destroy(input->input_device);
2541 free(input);
2542}
2543
2544static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002545display_handle_global(struct wl_display *display, uint32_t id,
2546 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002547{
2548 struct display *d = data;
2549
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002550 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002551 d->compositor =
2552 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002553 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002554 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002555 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002556 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002557 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002558 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002559 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002560 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002561 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2562 d->data_device_manager =
2563 wl_display_bind(display, id,
2564 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002565 }
2566}
2567
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002568static void
2569display_render_frame(struct display *d)
2570{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002571 int radius = 8;
2572 cairo_t *cr;
2573
2574 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2575 cr = cairo_create(d->shadow);
2576 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2577 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2578 rounded_rect(cr, 16, 16, 112, 112, radius);
2579 cairo_fill(cr);
2580 cairo_destroy(cr);
2581 blur_surface(d->shadow, 64);
2582
2583 d->active_frame =
2584 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2585 cr = cairo_create(d->active_frame);
2586 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2587 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2588 rounded_rect(cr, 16, 16, 112, 112, radius);
2589 cairo_fill(cr);
2590 cairo_destroy(cr);
2591
2592 d->inactive_frame =
2593 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2594 cr = cairo_create(d->inactive_frame);
2595 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2596 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2597 rounded_rect(cr, 16, 16, 112, 112, radius);
2598 cairo_fill(cr);
2599 cairo_destroy(cr);
2600}
2601
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002602static void
2603init_xkb(struct display *d)
2604{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002605 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002606
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002607 names.rules = "evdev";
2608 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002609 names.layout = option_xkb_layout;
2610 names.variant = option_xkb_variant;
2611 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002612
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002613 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002614 if (!d->xkb) {
2615 fprintf(stderr, "Failed to compile keymap\n");
2616 exit(1);
2617 }
2618}
2619
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002620static void
2621fini_xkb(struct display *display)
2622{
2623 xkb_free_keymap(display->xkb);
2624}
2625
Yuval Fledel45568f62010-12-06 09:18:12 -05002626static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002627init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002628{
2629 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002630 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002631
2632 static const EGLint premul_argb_cfg_attribs[] = {
2633 EGL_SURFACE_TYPE,
2634 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2635 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002636 EGL_RED_SIZE, 1,
2637 EGL_GREEN_SIZE, 1,
2638 EGL_BLUE_SIZE, 1,
2639 EGL_ALPHA_SIZE, 1,
2640 EGL_DEPTH_SIZE, 1,
2641 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2642 EGL_NONE
2643 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002644
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002645 static const EGLint rgb_cfg_attribs[] = {
2646 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2647 EGL_RED_SIZE, 1,
2648 EGL_GREEN_SIZE, 1,
2649 EGL_BLUE_SIZE, 1,
2650 EGL_ALPHA_SIZE, 0,
2651 EGL_DEPTH_SIZE, 1,
2652 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2653 EGL_NONE
2654 };
2655
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002656 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002657 if (!eglInitialize(d->dpy, &major, &minor)) {
2658 fprintf(stderr, "failed to initialize display\n");
2659 return -1;
2660 }
2661
2662 if (!eglBindAPI(EGL_OPENGL_API)) {
2663 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2664 return -1;
2665 }
2666
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002667 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002668 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002669 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002670 return -1;
2671 }
2672
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002673 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2674 &d->rgb_config, 1, &n) || n != 1) {
2675 fprintf(stderr, "failed to choose rgb config\n");
2676 return -1;
2677 }
2678
Benjamin Franzke0c991632011-09-27 21:57:31 +02002679 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2680 if (d->rgb_ctx == NULL) {
2681 fprintf(stderr, "failed to create context\n");
2682 return -1;
2683 }
2684 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2685 EGL_NO_CONTEXT, NULL);
2686 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002687 fprintf(stderr, "failed to create context\n");
2688 return -1;
2689 }
2690
Benjamin Franzke0c991632011-09-27 21:57:31 +02002691 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002692 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002693 return -1;
2694 }
2695
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002696#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002697 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2698 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002699 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002700 return -1;
2701 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002702 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2703 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2704 fprintf(stderr, "failed to get cairo egl argb device\n");
2705 return -1;
2706 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002707#endif
2708
2709 return 0;
2710}
2711
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002712static void
2713fini_egl(struct display *display)
2714{
2715#ifdef HAVE_CAIRO_EGL
2716 cairo_device_destroy(display->argb_device);
2717 cairo_device_destroy(display->rgb_device);
2718#endif
2719
2720 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2721 EGL_NO_CONTEXT);
2722
2723 eglTerminate(display->dpy);
2724 eglReleaseThread();
2725}
2726
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002727static int
2728event_mask_update(uint32_t mask, void *data)
2729{
2730 struct display *d = data;
2731
2732 d->mask = mask;
2733
2734 return 0;
2735}
2736
2737static void
2738handle_display_data(struct task *task, uint32_t events)
2739{
2740 struct display *display =
2741 container_of(task, struct display, display_task);
2742
2743 wl_display_iterate(display->display, display->mask);
2744}
2745
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002746struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002747display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002748{
2749 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002750 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002751 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002752 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002753
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002754 g_type_init();
2755
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002756 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002757 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002758 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002759
2760 xkb_option_group = g_option_group_new("xkb",
2761 "Keyboard options",
2762 "Show all XKB options",
2763 NULL, NULL);
2764 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2765 g_option_context_add_group (context, xkb_option_group);
2766
2767 if (!g_option_context_parse(context, argc, argv, &error)) {
2768 fprintf(stderr, "option parsing failed: %s\n", error->message);
2769 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002770 }
2771
Tim Wiederhake748f6722011-01-23 23:25:25 +01002772 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002773
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002774 d = malloc(sizeof *d);
2775 if (d == NULL)
2776 return NULL;
2777
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002778 memset(d, 0, sizeof *d);
2779
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002780 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002781 if (d->display == NULL) {
2782 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002783 return NULL;
2784 }
2785
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002786 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2787 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2788 d->display_task.run = handle_display_data;
2789 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2790
2791 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002792 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002793 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002794
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002795 /* Set up listener so we'll catch all events. */
2796 wl_display_add_global_listener(d->display,
2797 display_handle_global, d);
2798
2799 /* Process connection events. */
2800 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002801 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002802 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002803
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002804 d->image_target_texture_2d =
2805 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2806 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2807 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2808
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002809 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002810
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002811 display_render_frame(d);
2812
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002813 wl_list_init(&d->window_list);
2814
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002815 init_xkb(d);
2816
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002817 return d;
2818}
2819
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002820static void
2821display_destroy_outputs(struct display *display)
2822{
2823 struct output *tmp;
2824 struct output *output;
2825
2826 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2827 output_destroy(output);
2828}
2829
Pekka Paalanene1207c72011-12-16 12:02:09 +02002830static void
2831display_destroy_inputs(struct display *display)
2832{
2833 struct input *tmp;
2834 struct input *input;
2835
2836 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2837 input_destroy(input);
2838}
2839
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002840void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002841display_destroy(struct display *display)
2842{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002843 if (!wl_list_empty(&display->window_list))
2844 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2845
2846 if (!wl_list_empty(&display->deferred_list))
2847 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2848
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002849 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002850 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002851
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002852 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002853
2854 cairo_surface_destroy(display->active_frame);
2855 cairo_surface_destroy(display->inactive_frame);
2856 cairo_surface_destroy(display->shadow);
2857 destroy_pointer_surfaces(display);
2858
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002859 fini_egl(display);
2860
Pekka Paalanenc2052982011-12-16 11:41:32 +02002861 if (display->shell)
2862 wl_shell_destroy(display->shell);
2863
2864 if (display->shm)
2865 wl_shm_destroy(display->shm);
2866
2867 if (display->data_device_manager)
2868 wl_data_device_manager_destroy(display->data_device_manager);
2869
2870 wl_compositor_destroy(display->compositor);
2871
2872 close(display->epoll_fd);
2873
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002874 wl_display_flush(display->display);
2875 wl_display_destroy(display->display);
2876 free(display);
2877}
2878
2879void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002880display_set_user_data(struct display *display, void *data)
2881{
2882 display->user_data = data;
2883}
2884
2885void *
2886display_get_user_data(struct display *display)
2887{
2888 return display->user_data;
2889}
2890
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002891struct wl_display *
2892display_get_display(struct display *display)
2893{
2894 return display->display;
2895}
2896
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002897struct output *
2898display_get_output(struct display *display)
2899{
2900 return container_of(display->output_list.next, struct output, link);
2901}
2902
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002903struct wl_compositor *
2904display_get_compositor(struct display *display)
2905{
2906 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002907}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002908
2909EGLDisplay
2910display_get_egl_display(struct display *d)
2911{
2912 return d->dpy;
2913}
2914
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002915struct wl_data_source *
2916display_create_data_source(struct display *display)
2917{
2918 return wl_data_device_manager_create_data_source(display->data_device_manager);
2919}
2920
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002921EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002922display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002923{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002924 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002925}
2926
Benjamin Franzke0c991632011-09-27 21:57:31 +02002927EGLConfig
2928display_get_argb_egl_config(struct display *d)
2929{
2930 return d->premultiplied_argb_config;
2931}
2932
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002933struct wl_shell *
2934display_get_shell(struct display *display)
2935{
2936 return display->shell;
2937}
2938
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002939int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002940display_acquire_window_surface(struct display *display,
2941 struct window *window,
2942 EGLContext ctx)
2943{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002944#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002945 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002946 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002947
2948 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002949 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002950 device = cairo_surface_get_device(window->cairo_surface);
2951 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002952 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002953
Benjamin Franzke0c991632011-09-27 21:57:31 +02002954 if (!ctx) {
2955 if (device == display->rgb_device)
2956 ctx = display->rgb_ctx;
2957 else if (device == display->argb_device)
2958 ctx = display->argb_ctx;
2959 else
2960 assert(0);
2961 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002962
2963 data = cairo_surface_get_user_data(window->cairo_surface,
2964 &surface_data_key);
2965
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002966 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002967 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002968 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2969 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002970
2971 return 0;
2972#else
2973 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002974#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002975}
2976
2977void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002978display_release_window_surface(struct display *display,
2979 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002980{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002981#ifdef HAVE_CAIRO_EGL
2982 cairo_device_t *device;
2983
2984 device = cairo_surface_get_device(window->cairo_surface);
2985 if (!device)
2986 return;
2987
2988 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002989 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002990 cairo_device_release(device);
2991#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002992}
2993
2994void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002995display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002996{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002997 wl_list_insert(&display->deferred_list, &task->link);
2998}
2999
3000void
3001display_watch_fd(struct display *display,
3002 int fd, uint32_t events, struct task *task)
3003{
3004 struct epoll_event ep;
3005
3006 ep.events = events;
3007 ep.data.ptr = task;
3008 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3009}
3010
3011void
3012display_run(struct display *display)
3013{
3014 struct task *task;
3015 struct epoll_event ep[16];
3016 int i, count;
3017
Pekka Paalanen826d7952011-12-15 10:14:07 +02003018 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003019 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003020 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003021
3022 while (!wl_list_empty(&display->deferred_list)) {
3023 task = container_of(display->deferred_list.next,
3024 struct task, link);
3025 wl_list_remove(&task->link);
3026 task->run(task, 0);
3027 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003028
3029 if (!display->running)
3030 break;
3031
3032 wl_display_flush(display->display);
3033
3034 count = epoll_wait(display->epoll_fd,
3035 ep, ARRAY_LENGTH(ep), -1);
3036 for (i = 0; i < count; i++) {
3037 task = ep[i].data.ptr;
3038 task->run(task, ep[i].events);
3039 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003040 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003041}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003042
3043void
3044display_exit(struct display *display)
3045{
3046 display->running = 0;
3047}