blob: 86f084d539d534a851c22e4e5ac70bb1799ddc74 [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øgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400121 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400123 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500124
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500125 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500126
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500127 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500128 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400129 window_data_handler_t data_handler;
130 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500131 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400132
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500133 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500134 struct window *menu;
135
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500136 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400137 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138};
139
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500140struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500141 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500142 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400143 struct wl_list link;
144 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500145 widget_resize_handler_t resize_handler;
146 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500147 widget_enter_handler_t enter_handler;
148 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500149 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500150 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 void *user_data;
152};
153
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400154struct input {
155 struct display *display;
156 struct wl_input_device *input_device;
157 struct window *pointer_focus;
158 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400159 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160 uint32_t modifiers;
161 int32_t x, y, sx, sy;
162 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400163
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500164 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500165 struct widget *grab;
166 uint32_t grab_button;
167
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168 struct wl_data_device *data_device;
169 struct data_offer *drag_offer;
170 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171};
172
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500173struct output {
174 struct display *display;
175 struct wl_output *output;
176 struct rectangle allocation;
177 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200178
179 display_output_handler_t destroy_handler;
180 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500181};
182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500183struct frame {
184 struct widget *widget;
185 struct widget *child;
186 int margin;
187};
188
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500189struct menu {
190 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500191 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500192 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500193 const char **entries;
194 uint32_t time;
195 int current;
196 int count;
197 menu_func_t func;
198};
199
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400200enum {
201 POINTER_DEFAULT = 100,
202 POINTER_UNSET
203};
204
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500205enum window_location {
206 WINDOW_INTERIOR = 0,
207 WINDOW_RESIZING_TOP = 1,
208 WINDOW_RESIZING_BOTTOM = 2,
209 WINDOW_RESIZING_LEFT = 4,
210 WINDOW_RESIZING_TOP_LEFT = 5,
211 WINDOW_RESIZING_BOTTOM_LEFT = 6,
212 WINDOW_RESIZING_RIGHT = 8,
213 WINDOW_RESIZING_TOP_RIGHT = 9,
214 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
215 WINDOW_RESIZING_MASK = 15,
216 WINDOW_EXTERIOR = 16,
217 WINDOW_TITLEBAR = 17,
218 WINDOW_CLIENT_AREA = 18,
219};
220
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400221const char *option_xkb_layout = "us";
222const char *option_xkb_variant = "";
223const char *option_xkb_options = "";
224
225static const GOptionEntry xkb_option_entries[] = {
226 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_layout, "XKB Layout" },
228 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
229 &option_xkb_variant, "XKB Variant" },
230 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_options, "XKB Options" },
232 { NULL }
233};
234
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400235static const cairo_user_data_key_t surface_data_key;
236struct surface_data {
237 struct wl_buffer *buffer;
238};
239
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500240#define MULT(_d,c,a,t) \
241 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
242
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500243#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245struct egl_window_surface_data {
246 struct display *display;
247 struct wl_surface *surface;
248 struct wl_egl_window *window;
249 EGLSurface surf;
250};
251
252static void
253egl_window_surface_data_destroy(void *p)
254{
255 struct egl_window_surface_data *data = p;
256 struct display *d = data->display;
257
258 eglDestroySurface(d->dpy, data->surf);
259 wl_egl_window_destroy(data->window);
260 data->surface = NULL;
261
262 free(p);
263}
264
265static cairo_surface_t *
266display_create_egl_window_surface(struct display *display,
267 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269 struct rectangle *rectangle)
270{
271 cairo_surface_t *cairo_surface;
272 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 EGLConfig config;
274 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200275 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 static const EGLint premul_attribs[] = {
278 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
279 EGL_NONE
280 };
281
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100282 data = malloc(sizeof *data);
283 if (data == NULL)
284 return NULL;
285
286 data->display = display;
287 data->surface = surface;
288
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 if (flags & SURFACE_OPAQUE) {
290 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200292 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 } else {
294 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200295 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 attribs = premul_attribs;
297 }
298
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400299 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400301 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 data->surf = eglCreateWindowSurface(display->dpy, config,
304 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100305
Benjamin Franzke0c991632011-09-27 21:57:31 +0200306 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100307 data->surf,
308 rectangle->width,
309 rectangle->height);
310
311 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
312 data, egl_window_surface_data_destroy);
313
314 return cairo_surface;
315}
316
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500317struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200319 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 EGLImageKHR image;
321 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324};
325
326static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500327egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400328{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331
Benjamin Franzke0c991632011-09-27 21:57:31 +0200332 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200334 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800335
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500336 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500338 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340}
341
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500343display_get_image_for_egl_image_surface(struct display *display,
344 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500345{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500347
348 data = cairo_surface_get_user_data (surface, &surface_data_key);
349
350 return data->image;
351}
352
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500353static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400355 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500356 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500358 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359 EGLDisplay dpy = display->dpy;
360 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200361 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400362
363 data = malloc(sizeof *data);
364 if (data == NULL)
365 return NULL;
366
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800367 data->display = display;
368
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400369 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400370 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500371 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000372 free(data);
373 return NULL;
374 }
375
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 if (flags & SURFACE_OPAQUE) {
377 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200378 content = CAIRO_CONTENT_COLOR;
379 } else {
380 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200381 content = CAIRO_CONTENT_COLOR_ALPHA;
382 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400383
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500384 data->image = display->create_image(dpy, NULL,
385 EGL_NATIVE_PIXMAP_KHR,
386 (EGLClientBuffer) data->pixmap,
387 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500389 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390 free(data);
391 return NULL;
392 }
393
394 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400395 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500400 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200401 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
Benjamin Franzke0c991632011-09-27 21:57:31 +0200403 surface = cairo_gl_surface_create_for_texture(data->device,
404 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405 data->texture,
406 rectangle->width,
407 rectangle->height);
408
409 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
412 return surface;
413}
414
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500415static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500416display_create_egl_image_surface_from_file(struct display *display,
417 const char *filename,
418 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419{
420 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421 GdkPixbuf *pixbuf;
422 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400423 int stride, i;
424 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500425 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400426
427 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400429 FALSE, &error);
430 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
434 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500435 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400438
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439
440 stride = gdk_pixbuf_get_rowstride(pixbuf);
441 pixels = gdk_pixbuf_get_pixels(pixbuf);
442
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400444 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400446 while (p < end) {
447 unsigned int t;
448
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400449 MULT(p[0], p[0], p[3], t);
450 MULT(p[1], p[1], p[3], t);
451 MULT(p[2], p[2], p[3], t);
452 p += 4;
453
454 }
455 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400456
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200457 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000458 if (surface == NULL) {
459 g_object_unref(pixbuf);
460 return NULL;
461 }
462
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800463 data = cairo_surface_get_user_data(surface, &surface_data_key);
464
Benjamin Franzke0c991632011-09-27 21:57:31 +0200465 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800466 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800467 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
468 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200469 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400470
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500471 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400472
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 return surface;
474}
475
476#endif
477
478struct wl_buffer *
479display_get_buffer_for_surface(struct display *display,
480 cairo_surface_t *surface)
481{
482 struct surface_data *data;
483
484 data = cairo_surface_get_user_data (surface, &surface_data_key);
485
486 return data->buffer;
487}
488
489struct shm_surface_data {
490 struct surface_data data;
491 void *map;
492 size_t length;
493};
494
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500495static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496shm_surface_data_destroy(void *p)
497{
498 struct shm_surface_data *data = p;
499
500 wl_buffer_destroy(data->data.buffer);
501 munmap(data->map, data->length);
502}
503
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500504static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400506 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507{
508 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400510 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800511 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512 char filename[] = "/tmp/wayland-shm-XXXXXX";
513
514 data = malloc(sizeof *data);
515 if (data == NULL)
516 return NULL;
517
518 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
519 rectangle->width);
520 data->length = stride * rectangle->height;
521 fd = mkstemp(filename);
522 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000523 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 return NULL;
525 }
526 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000527 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528 close(fd);
529 return NULL;
530 }
531
532 data->map = mmap(NULL, data->length,
533 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
534 unlink(filename);
535
536 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000537 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538 close(fd);
539 return NULL;
540 }
541
542 surface = cairo_image_surface_create_for_data (data->map,
543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400552 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400554 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556 data->data.buffer = wl_shm_create_buffer(display->shm,
557 fd,
558 rectangle->width,
559 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400560 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
562 close(fd);
563
564 return surface;
565}
566
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500567static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568display_create_shm_surface_from_file(struct display *display,
569 const char *filename,
570 struct rectangle *rect)
571{
572 cairo_surface_t *surface;
573 GdkPixbuf *pixbuf;
574 GError *error = NULL;
575 int stride, i;
576 unsigned char *pixels, *p, *end, *dest_data;
577 int dest_stride;
578 uint32_t *d;
579
580 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
581 rect->width, rect->height,
582 FALSE, &error);
583 if (error != NULL)
584 return NULL;
585
586 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
587 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500588 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400589 return NULL;
590 }
591
592 stride = gdk_pixbuf_get_rowstride(pixbuf);
593 pixels = gdk_pixbuf_get_pixels(pixbuf);
594
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400595 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000596 if (surface == NULL) {
597 g_object_unref(pixbuf);
598 return NULL;
599 }
600
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601 dest_data = cairo_image_surface_get_data (surface);
602 dest_stride = cairo_image_surface_get_stride (surface);
603
604 for (i = 0; i < rect->height; i++) {
605 d = (uint32_t *) (dest_data + i * dest_stride);
606 p = pixels + i * stride;
607 end = p + rect->width * 4;
608 while (p < end) {
609 unsigned int t;
610 unsigned char a, r, g, b;
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612 a = p[3];
613 MULT(r, p[0], a, t);
614 MULT(g, p[1], a, t);
615 MULT(b, p[2], a, t);
616 p += 4;
617 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
618 }
619 }
620
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500621 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622
623 return surface;
624}
625
nobled7b87cb02011-02-01 18:51:47 +0000626static int
627check_size(struct rectangle *rect)
628{
629 if (rect->width && rect->height)
630 return 0;
631
632 fprintf(stderr, "tried to create surface of "
633 "width: %d, height: %d\n", rect->width, rect->height);
634 return -1;
635}
636
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637cairo_surface_t *
638display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400640 struct rectangle *rectangle,
641 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642{
nobled7b87cb02011-02-01 18:51:47 +0000643 if (check_size(rectangle) < 0)
644 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500645#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500646 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 if (surface)
648 return display_create_egl_window_surface(display,
649 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400650 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651 rectangle);
652 else
653 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400654 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500656 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400658 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400659}
660
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500661static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662display_create_surface_from_file(struct display *display,
663 const char *filename,
664 struct rectangle *rectangle)
665{
nobled7b87cb02011-02-01 18:51:47 +0000666 if (check_size(rectangle) < 0)
667 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500668#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500669 if (display->dpy) {
670 return display_create_egl_image_surface_from_file(display,
671 filename,
672 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500673 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500675 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400676}
Yuval Fledel45568f62010-12-06 09:18:12 -0500677 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678 const char *filename;
679 int hotspot_x, hotspot_y;
680} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500681 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
682 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
683 { DATADIR "/weston/bottom_side.png", 16, 20 },
684 { DATADIR "/weston/grabbing.png", 20, 17 },
685 { DATADIR "/weston/left_ptr.png", 10, 5 },
686 { DATADIR "/weston/left_side.png", 10, 20 },
687 { DATADIR "/weston/right_side.png", 30, 19 },
688 { DATADIR "/weston/top_left_corner.png", 8, 8 },
689 { DATADIR "/weston/top_right_corner.png", 26, 8 },
690 { DATADIR "/weston/top_side.png", 18, 8 },
691 { DATADIR "/weston/xterm.png", 15, 15 },
692 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693};
694
695static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697{
698 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces =
704 malloc(count * sizeof *display->pointer_surfaces);
705 rect.width = width;
706 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400707 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400709 display_create_surface_from_file(display,
710 pointer_images[i].filename,
711 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100712 if (!display->pointer_surfaces[i]) {
713 fprintf(stderr, "Error loading pointer image: %s\n",
714 pointer_images[i].filename);
715 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400716 }
717
718}
719
Pekka Paalanen325bb602011-12-19 10:31:45 +0200720static void
721destroy_pointer_surfaces(struct display *display)
722{
723 int i, count;
724
725 count = ARRAY_LENGTH(pointer_images);
726 for (i = 0; i < count; ++i) {
727 if (display->pointer_surfaces[i])
728 cairo_surface_destroy(display->pointer_surfaces[i]);
729 }
730 free(display->pointer_surfaces);
731}
732
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733cairo_surface_t *
734display_get_pointer_surface(struct display *display, int pointer,
735 int *width, int *height,
736 int *hotspot_x, int *hotspot_y)
737{
738 cairo_surface_t *surface;
739
740 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500741#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 *width = cairo_gl_surface_get_width(surface);
743 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000744#else
745 *width = cairo_image_surface_get_width(surface);
746 *height = cairo_image_surface_get_height(surface);
747#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748 *hotspot_x = pointer_images[pointer].hotspot_x;
749 *hotspot_y = pointer_images[pointer].hotspot_y;
750
751 return cairo_surface_reference(surface);
752}
753
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754static void
755window_attach_surface(struct window *window);
756
757static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400758free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759{
760 struct window *window = data;
761
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400763 cairo_surface_destroy(window->pending_surface);
764 window->pending_surface = NULL;
765 if (window->cairo_surface)
766 window_attach_surface(window);
767}
768
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400769static const struct wl_callback_listener free_surface_listener = {
770 free_surface
771};
772
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500773static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200774window_get_resize_dx_dy(struct window *window, int *x, int *y)
775{
776 if (window->resize_edges & WINDOW_RESIZING_LEFT)
777 *x = window->server_allocation.width - window->allocation.width;
778 else
779 *x = 0;
780
781 if (window->resize_edges & WINDOW_RESIZING_TOP)
782 *y = window->server_allocation.height -
783 window->allocation.height;
784 else
785 *y = 0;
786
787 window->resize_edges = 0;
788}
789
790static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791window_set_type(struct window *window)
792{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200793 if (!window->shell_surface)
794 return;
795
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400796 switch (window->type) {
797 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200798 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 break;
800 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200801 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 break;
803 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200804 wl_shell_surface_set_transient(window->shell_surface,
805 window->parent->shell_surface,
806 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400807 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500808 case TYPE_MENU:
809 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400810 case TYPE_CUSTOM:
811 break;
812 }
813}
814
815static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500816window_attach_surface(struct window *window)
817{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400818 struct display *display = window->display;
819 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400820 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000821#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500824 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400826 if (display->shell)
827 window_set_type(window);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
832 data = cairo_surface_get_user_data(window->cairo_surface,
833 &surface_data_key);
834
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 cairo_gl_surface_swapbuffers(window->cairo_surface);
836 wl_egl_window_get_attached_size(data->window,
837 &window->server_allocation.width,
838 &window->server_allocation.height);
839 break;
840 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000841#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200843 window_get_resize_dx_dy(window, &x, &y);
844
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 if (window->pending_surface != NULL)
846 return;
847
848 window->pending_surface = window->cairo_surface;
849 window->cairo_surface = NULL;
850
851 buffer =
852 display_get_buffer_for_surface(display,
853 window->pending_surface);
854
855 wl_surface_attach(window->surface, buffer, x, y);
856 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400857 cb = wl_display_sync(display->display);
858 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000860 default:
861 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500863
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500864 wl_surface_damage(window->surface, 0, 0,
865 window->allocation.width,
866 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500867}
868
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500869void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400870window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100872 if (window->cairo_surface) {
873 switch (window->buffer_type) {
874 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
875 case WINDOW_BUFFER_TYPE_SHM:
876 display_surface_damage(window->display,
877 window->cairo_surface,
878 0, 0,
879 window->allocation.width,
880 window->allocation.height);
881 break;
882 default:
883 break;
884 }
885 window_attach_surface(window);
886 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887}
888
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889void
890window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400891{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500892 cairo_surface_reference(surface);
893
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894 if (window->cairo_surface != NULL)
895 cairo_surface_destroy(window->cairo_surface);
896
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500897 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898}
899
Benjamin Franzke22d54812011-07-16 19:50:32 +0000900#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901static void
902window_resize_cairo_window_surface(struct window *window)
903{
904 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906
907 data = cairo_surface_get_user_data(window->cairo_surface,
908 &surface_data_key);
909
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200910 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 wl_egl_window_resize(data->window,
912 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200913 window->allocation.height,
914 x,y);
915
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916 cairo_gl_surface_set_size(window->cairo_surface,
917 window->allocation.width,
918 window->allocation.height);
919}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000920#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100921
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400922struct display *
923window_get_display(struct window *window)
924{
925 return window->display;
926}
927
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928void
929window_create_surface(struct window *window)
930{
931 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400932 uint32_t flags = 0;
933
934 if (!window->transparent)
935 flags = SURFACE_OPAQUE;
936
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500938#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
940 if (window->cairo_surface) {
941 window_resize_cairo_window_surface(window);
942 return;
943 }
944 surface = display_create_surface(window->display,
945 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400946 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100947 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500948 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
953#endif
954 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400956 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800958 default:
959 surface = NULL;
960 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400962
963 window_set_surface(window, surface);
964 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400965}
966
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400967void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500968window_destroy(struct window *window)
969{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200970 struct display *display = window->display;
971 struct input *input;
972
973 if (window->redraw_scheduled)
974 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500975 if (window->resize_scheduled)
976 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200977
978 wl_list_for_each(input, &display->input_list, link) {
979 if (input->pointer_focus == window)
980 input->pointer_focus = NULL;
Kristian Høgsberg62444802012-01-11 11:18:14 -0500981 if (input->focus_widget &&
982 input->focus_widget->window == window)
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500983 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200984 if (input->keyboard_focus == window)
985 input->keyboard_focus = NULL;
986 }
987
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200988 if (window->shell_surface)
989 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500990 wl_surface_destroy(window->surface);
991 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200992
993 if (window->cairo_surface != NULL)
994 cairo_surface_destroy(window->cairo_surface);
995 if (window->pending_surface != NULL)
996 cairo_surface_destroy(window->pending_surface);
997
998 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500999 free(window);
1000}
1001
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001003widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001007 wl_list_for_each(child, &widget->child_list, link) {
1008 target = widget_find_widget(child, x, y);
1009 if (target)
1010 return target;
1011 }
1012
1013 if (widget->allocation.x <= x &&
1014 x < widget->allocation.x + widget->allocation.width &&
1015 widget->allocation.y <= y &&
1016 y < widget->allocation.y + widget->allocation.height) {
1017 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018 }
1019
1020 return NULL;
1021}
1022
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001023static struct widget *
1024widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 widget = malloc(sizeof *widget);
1029 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001030 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001031 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001032 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001033 wl_list_init(&widget->child_list);
1034
1035 return widget;
1036}
1037
1038struct widget *
1039window_add_widget(struct window *window, void *data)
1040{
1041 window->widget = widget_create(window, data);
1042 wl_list_init(&window->widget->link);
1043
1044 return window->widget;
1045}
1046
1047struct widget *
1048widget_add_widget(struct widget *parent, void *data)
1049{
1050 struct widget *widget;
1051
1052 widget = widget_create(parent->window, data);
1053 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001055 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056}
1057
1058void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001059widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001061 wl_list_remove(&widget->link);
1062 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001063}
1064
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001066widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069}
1070
1071void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001072widget_set_size(struct widget *widget, int32_t width, int32_t height)
1073{
1074 struct window *window = widget->window;
1075
1076 widget->allocation.width = width;
1077 widget->allocation.height = height;
1078
1079 window->allocation.x = 0;
1080 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001081 window->allocation.width = width;
1082 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001083}
1084
1085void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086widget_set_allocation(struct widget *widget,
1087 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089 widget->allocation.x = x;
1090 widget->allocation.y = y;
1091 widget->allocation.width = width;
1092 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001101void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001102widget_set_resize_handler(struct widget *widget,
1103 widget_resize_handler_t handler)
1104{
1105 widget->resize_handler = handler;
1106}
1107
1108void
1109widget_set_redraw_handler(struct widget *widget,
1110 widget_redraw_handler_t handler)
1111{
1112 widget->redraw_handler = handler;
1113}
1114
1115void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001116widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001117{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118 widget->enter_handler = handler;
1119}
1120
1121void
1122widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1123{
1124 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001125}
1126
1127void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001128widget_set_motion_handler(struct widget *widget,
1129 widget_motion_handler_t handler)
1130{
1131 widget->motion_handler = handler;
1132}
1133
1134void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001135widget_set_button_handler(struct widget *widget,
1136 widget_button_handler_t handler)
1137{
1138 widget->button_handler = handler;
1139}
1140
1141void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001142widget_schedule_redraw(struct widget *widget)
1143{
1144 window_schedule_redraw(widget->window);
1145}
1146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001147cairo_surface_t *
1148window_get_surface(struct window *window)
1149{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001150 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151}
1152
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001153struct wl_surface *
1154window_get_wl_surface(struct window *window)
1155{
1156 return window->surface;
1157}
1158
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159struct wl_shell_surface *
1160window_get_wl_shell_surface(struct window *window)
1161{
1162 return window->shell_surface;
1163}
1164
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165static void
1166frame_resize_handler(struct widget *widget,
1167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 struct frame *frame = data;
1170 struct widget *child = frame->child;
1171 struct rectangle allocation;
1172 int decoration_width, decoration_height;
1173
1174 decoration_width = 20 + frame->margin * 2;
1175 decoration_height = 60 + frame->margin * 2;
1176
1177 allocation.x = 10 + frame->margin;
1178 allocation.y = 50 + frame->margin;
1179 allocation.width = width - decoration_width;
1180 allocation.height = height - decoration_height;
1181
1182 widget_set_allocation(child, allocation.x, allocation.y,
1183 allocation.width, allocation.height);
1184
1185 if (child->resize_handler)
1186 child->resize_handler(child,
1187 allocation.width,
1188 allocation.height,
1189 child->user_data);
1190
1191 widget_set_allocation(widget, 0, 0,
1192 child->allocation.width + decoration_width,
1193 child->allocation.height + decoration_height);
1194}
1195
1196static void
1197frame_redraw_handler(struct widget *widget, void *data)
1198{
1199 struct frame *frame = data;
1200 cairo_t *cr;
1201 cairo_text_extents_t extents;
1202 cairo_surface_t *source;
1203 int width, height, shadow_dx = 3, shadow_dy = 3;
1204 struct window *window = widget->window;
1205
1206 width = widget->allocation.width;
1207 height = widget->allocation.height;
1208
1209 cr = cairo_create(window->cairo_surface);
1210
1211 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1212 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1213 cairo_paint(cr);
1214
1215 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1216 tile_mask(cr, window->display->shadow,
1217 shadow_dx, shadow_dy, width, height,
1218 frame->margin + 10 - shadow_dx,
1219 frame->margin + 10 - shadow_dy);
1220
1221 if (window->keyboard_device)
1222 source = window->display->active_frame;
1223 else
1224 source = window->display->inactive_frame;
1225
1226 tile_source(cr, source, 0, 0, width, height,
1227 frame->margin + 10, frame->margin + 50);
1228
1229 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1230 cairo_set_font_size(cr, 14);
1231 cairo_text_extents(cr, window->title, &extents);
1232 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1233 if (window->keyboard_device)
1234 cairo_set_source_rgb(cr, 0, 0, 0);
1235 else
1236 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1237 cairo_show_text(cr, window->title);
1238
1239 cairo_destroy(cr);
1240}
1241
1242static int
1243frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1244{
1245 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247 const int grip_size = 8;
1248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 hlocation = WINDOW_RESIZING_RIGHT;
1257 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001258 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001262 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 vlocation = WINDOW_RESIZING_BOTTOM;
1268 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271 location = vlocation | hlocation;
1272 if (location & WINDOW_EXTERIOR)
1273 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275 location = WINDOW_TITLEBAR;
1276 else if (location == WINDOW_INTERIOR)
1277 location = WINDOW_CLIENT_AREA;
1278
1279 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280}
1281
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001283frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001285 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288 switch (location) {
1289 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001305 case WINDOW_EXTERIOR:
1306 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001308 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001310}
1311
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001312static void
1313frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001315 switch (index) {
1316 case 0: /* close */
1317 if (window->close_handler)
1318 window->close_handler(window->parent,
1319 window->user_data);
1320 else
1321 display_exit(window->display);
1322 break;
1323 case 1: /* fullscreen */
1324 /* we don't have a way to get out of fullscreen for now */
1325 window_set_fullscreen(window, 1);
1326 break;
1327 case 2: /* rotate */
1328 case 3: /* scale */
1329 break;
1330 }
1331}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001332
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001333static int
1334frame_enter_handler(struct widget *widget,
1335 struct input *input, uint32_t time,
1336 int32_t x, int32_t y, void *data)
1337{
1338 return frame_get_pointer_image_for_location(data, input);
1339}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001340
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001341static int
1342frame_motion_handler(struct widget *widget,
1343 struct input *input, uint32_t time,
1344 int32_t x, int32_t y, void *data)
1345{
1346 return frame_get_pointer_image_for_location(data, input);
1347}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001348
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001349static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001350frame_button_handler(struct widget *widget,
1351 struct input *input, uint32_t time,
1352 int button, int state, void *data)
1353
1354{
1355 struct frame *frame = data;
1356 struct window *window = widget->window;
1357 int location;
1358 int32_t x, y;
1359 static const char *entries[] = {
1360 "Close", "Fullscreen", "Rotate", "Scale"
1361 };
1362
1363 location = frame_get_pointer_location(frame, input->sx, input->sy);
1364
1365 if (window->display->shell && button == BTN_LEFT && state == 1) {
1366 switch (location) {
1367 case WINDOW_TITLEBAR:
1368 if (!window->shell_surface)
1369 break;
1370 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001371 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001372 wl_shell_surface_move(window->shell_surface,
1373 input_get_input_device(input),
1374 time);
1375 break;
1376 case WINDOW_RESIZING_TOP:
1377 case WINDOW_RESIZING_BOTTOM:
1378 case WINDOW_RESIZING_LEFT:
1379 case WINDOW_RESIZING_RIGHT:
1380 case WINDOW_RESIZING_TOP_LEFT:
1381 case WINDOW_RESIZING_TOP_RIGHT:
1382 case WINDOW_RESIZING_BOTTOM_LEFT:
1383 case WINDOW_RESIZING_BOTTOM_RIGHT:
1384 if (!window->shell_surface)
1385 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001386 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387 wl_shell_surface_resize(window->shell_surface,
1388 input_get_input_device(input),
1389 time, location);
1390 break;
1391 }
1392 } else if (button == BTN_RIGHT && state == 1) {
1393 input_get_position(input, &x, &y);
1394 window->menu = window_create_menu(window->display,
1395 input, time,
1396 window,
1397 x - 10, y - 10,
1398 frame_menu_func,
1399 entries, 4);
1400 window_schedule_redraw(window->menu);
1401 }
1402}
1403
1404struct widget *
1405frame_create(struct window *window, void *data)
1406{
1407 struct frame *frame;
1408
1409 frame = malloc(sizeof *frame);
1410 memset(frame, 0, sizeof *frame);
1411
1412 frame->widget = window_add_widget(window, frame);
1413 frame->child = widget_add_widget(frame->widget, data);
1414 frame->margin = 16;
1415
1416 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1417 widget_set_resize_handler(frame->widget, frame_resize_handler);
1418 widget_set_enter_handler(frame->widget, frame_enter_handler);
1419 widget_set_motion_handler(frame->widget, frame_motion_handler);
1420 widget_set_button_handler(frame->widget, frame_button_handler);
1421
1422 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001423}
1424
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001425static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001426input_set_focus_widget(struct input *input, struct widget *focus,
1427 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001428{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001429 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001430 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001432 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433 return;
1434
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001435 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001436 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001437 widget = old;
1438 if (input->grab)
1439 widget = input->grab;
1440 if (widget->leave_handler)
1441 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001442 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001443 }
1444
1445 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001446 widget = focus;
1447 if (input->grab)
1448 widget = input->grab;
1449 if (widget->enter_handler)
1450 pointer = widget->enter_handler(focus, input, time,
1451 x, y,
1452 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001453 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001454
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001455 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001456 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457}
1458
1459static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001460input_handle_motion(void *data, struct wl_input_device *input_device,
1461 uint32_t time,
1462 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001463{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001464 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001465 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001466 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001467 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001468
1469 input->x = x;
1470 input->y = y;
1471 input->sx = sx;
1472 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001473
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001474 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001475 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001476 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001477 }
1478
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001479 if (input->grab)
1480 widget = input->grab;
1481 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001482 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001483 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001484 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001485 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001486 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001487
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001488 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001489}
1490
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001491void
1492input_grab(struct input *input, struct widget *widget, uint32_t button)
1493{
1494 input->grab = widget;
1495 input->grab_button = button;
1496}
1497
1498void
1499input_ungrab(struct input *input, uint32_t time)
1500{
1501 struct widget *widget;
1502
1503 input->grab = NULL;
1504 if (input->pointer_focus) {
1505 widget = widget_find_widget(input->pointer_focus->widget,
1506 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001507 input_set_focus_widget(input, widget,
1508 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001509 }
1510}
1511
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001512static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001513input_handle_button(void *data,
1514 struct wl_input_device *input_device,
1515 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001516{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001517 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001518 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001519
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001520 if (input->focus_widget && input->grab == NULL && state)
1521 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001522
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001523 widget = input->focus_widget;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001524 if (widget && widget->button_handler)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001525 (*input->grab->button_handler)(widget,
1526 input, time,
1527 button, state,
1528 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001529
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001530 if (input->grab && input->grab_button == button && !state)
1531 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001532}
1533
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001534static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001535input_handle_key(void *data, struct wl_input_device *input_device,
1536 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001537{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001538 struct input *input = data;
1539 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001540 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001541 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001542
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001543 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001544 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001545 return;
1546
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001547 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001548 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001549 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1550 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001551
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001552 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1553
1554 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001555 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001556 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001557 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001558
1559 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001560 (*window->key_handler)(window, input, time, key, sym, state,
1561 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001562}
1563
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001564static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001565input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001566{
1567 struct window *window = input->pointer_focus;
1568
1569 if (!window)
1570 return;
1571
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001572 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001573
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574 input->pointer_focus = NULL;
1575 input->current_pointer_image = POINTER_UNSET;
1576}
1577
1578static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001579input_handle_pointer_focus(void *data,
1580 struct wl_input_device *input_device,
1581 uint32_t time, struct wl_surface *surface,
1582 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001583{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001584 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001585 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001586 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001587
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001588 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001589 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001590 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001591
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001592 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001593 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001594 window = input->pointer_focus;
1595
Kristian Høgsberg59826582011-01-20 11:56:57 -05001596 input->x = x;
1597 input->y = y;
1598 input->sx = sx;
1599 input->sy = sy;
1600
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001601 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001602 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001603 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001604}
1605
1606static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001607input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001608{
1609 struct window *window = input->keyboard_focus;
1610
1611 if (!window)
1612 return;
1613
1614 window->keyboard_device = NULL;
1615 if (window->keyboard_focus_handler)
1616 (*window->keyboard_focus_handler)(window, NULL,
1617 window->user_data);
1618
1619 input->keyboard_focus = NULL;
1620}
1621
1622static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001623input_handle_keyboard_focus(void *data,
1624 struct wl_input_device *input_device,
1625 uint32_t time,
1626 struct wl_surface *surface,
1627 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001628{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001629 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001630 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001631 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001632 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001633
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001634 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001635
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001636 if (surface)
1637 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001638
1639 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001640 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001641 for (k = keys->data; k < end; k++)
1642 input->modifiers |= d->xkb->map->modmap[*k];
1643
1644 window = input->keyboard_focus;
1645 if (window) {
1646 window->keyboard_device = input;
1647 if (window->keyboard_focus_handler)
1648 (*window->keyboard_focus_handler)(window,
1649 window->keyboard_device,
1650 window->user_data);
1651 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001652}
1653
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001654static void
1655input_handle_touch_down(void *data,
1656 struct wl_input_device *wl_input_device,
1657 uint32_t time, struct wl_surface *surface,
1658 int32_t id, int32_t x, int32_t y)
1659{
1660}
1661
1662static void
1663input_handle_touch_up(void *data,
1664 struct wl_input_device *wl_input_device,
1665 uint32_t time, int32_t id)
1666{
1667}
1668
1669static void
1670input_handle_touch_motion(void *data,
1671 struct wl_input_device *wl_input_device,
1672 uint32_t time, int32_t id, int32_t x, int32_t y)
1673{
1674}
1675
1676static void
1677input_handle_touch_frame(void *data,
1678 struct wl_input_device *wl_input_device)
1679{
1680}
1681
1682static void
1683input_handle_touch_cancel(void *data,
1684 struct wl_input_device *wl_input_device)
1685{
1686}
1687
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001688static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001689 input_handle_motion,
1690 input_handle_button,
1691 input_handle_key,
1692 input_handle_pointer_focus,
1693 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001694 input_handle_touch_down,
1695 input_handle_touch_up,
1696 input_handle_touch_motion,
1697 input_handle_touch_frame,
1698 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001699};
1700
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001701void
1702input_get_position(struct input *input, int32_t *x, int32_t *y)
1703{
1704 *x = input->sx;
1705 *y = input->sy;
1706}
1707
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001708struct wl_input_device *
1709input_get_input_device(struct input *input)
1710{
1711 return input->input_device;
1712}
1713
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001714uint32_t
1715input_get_modifiers(struct input *input)
1716{
1717 return input->modifiers;
1718}
1719
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001720struct widget *
1721input_get_focus_widget(struct input *input)
1722{
1723 return input->focus_widget;
1724}
1725
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001726struct data_offer {
1727 struct wl_data_offer *offer;
1728 struct input *input;
1729 struct wl_array types;
1730 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001731
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001732 struct task io_task;
1733 int fd;
1734 data_func_t func;
1735 int32_t x, y;
1736 void *user_data;
1737};
1738
1739static void
1740data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1741{
1742 struct data_offer *offer = data;
1743 char **p;
1744
1745 p = wl_array_add(&offer->types, sizeof *p);
1746 *p = strdup(type);
1747}
1748
1749static const struct wl_data_offer_listener data_offer_listener = {
1750 data_offer_offer,
1751};
1752
1753static void
1754data_offer_destroy(struct data_offer *offer)
1755{
1756 char **p;
1757
1758 offer->refcount--;
1759 if (offer->refcount == 0) {
1760 wl_data_offer_destroy(offer->offer);
1761 for (p = offer->types.data; *p; p++)
1762 free(*p);
1763 wl_array_release(&offer->types);
1764 free(offer);
1765 }
1766}
1767
1768static void
1769data_device_data_offer(void *data,
1770 struct wl_data_device *data_device, uint32_t id)
1771{
1772 struct data_offer *offer;
1773
1774 offer = malloc(sizeof *offer);
1775
1776 wl_array_init(&offer->types);
1777 offer->refcount = 1;
1778 offer->input = data;
1779
1780 /* FIXME: Generate typesafe wrappers for this */
1781 offer->offer = (struct wl_data_offer *)
1782 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1783 id, &wl_data_offer_interface);
1784
1785 wl_data_offer_add_listener(offer->offer,
1786 &data_offer_listener, offer);
1787}
1788
1789static void
1790data_device_enter(void *data, struct wl_data_device *data_device,
1791 uint32_t time, struct wl_surface *surface,
1792 int32_t x, int32_t y, struct wl_data_offer *offer)
1793{
1794 struct input *input = data;
1795 struct window *window;
1796 char **p;
1797
1798 input->drag_offer = wl_data_offer_get_user_data(offer);
1799 window = wl_surface_get_user_data(surface);
1800 input->pointer_focus = window;
1801
1802 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1803 *p = NULL;
1804
1805 window = input->pointer_focus;
1806 if (window->data_handler)
1807 window->data_handler(window, input, time, x, y,
1808 input->drag_offer->types.data,
1809 window->user_data);
1810}
1811
1812static void
1813data_device_leave(void *data, struct wl_data_device *data_device)
1814{
1815 struct input *input = data;
1816
1817 data_offer_destroy(input->drag_offer);
1818 input->drag_offer = NULL;
1819}
1820
1821static void
1822data_device_motion(void *data, struct wl_data_device *data_device,
1823 uint32_t time, int32_t x, int32_t y)
1824{
1825 struct input *input = data;
1826 struct window *window = input->pointer_focus;
1827
1828 input->sx = x;
1829 input->sy = y;
1830
1831 if (window->data_handler)
1832 window->data_handler(window, input, time, x, y,
1833 input->drag_offer->types.data,
1834 window->user_data);
1835}
1836
1837static void
1838data_device_drop(void *data, struct wl_data_device *data_device)
1839{
1840 struct input *input = data;
1841 struct window *window = input->pointer_focus;
1842
1843 if (window->drop_handler)
1844 window->drop_handler(window, input,
1845 input->sx, input->sy, window->user_data);
1846}
1847
1848static void
1849data_device_selection(void *data,
1850 struct wl_data_device *wl_data_device,
1851 struct wl_data_offer *offer)
1852{
1853 struct input *input = data;
1854 char **p;
1855
1856 if (input->selection_offer)
1857 data_offer_destroy(input->selection_offer);
1858
1859 input->selection_offer = wl_data_offer_get_user_data(offer);
1860 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1861 *p = NULL;
1862}
1863
1864static const struct wl_data_device_listener data_device_listener = {
1865 data_device_data_offer,
1866 data_device_enter,
1867 data_device_leave,
1868 data_device_motion,
1869 data_device_drop,
1870 data_device_selection
1871};
1872
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001873void
1874input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1875{
1876 struct display *display = input->display;
1877 struct wl_buffer *buffer;
1878 cairo_surface_t *surface;
1879
1880 if (pointer == input->current_pointer_image)
1881 return;
1882
1883 input->current_pointer_image = pointer;
1884 surface = display->pointer_surfaces[pointer];
1885
1886 if (!surface)
1887 return;
1888
1889 buffer = display_get_buffer_for_surface(display, surface);
1890 wl_input_device_attach(input->input_device, time, buffer,
1891 pointer_images[pointer].hotspot_x,
1892 pointer_images[pointer].hotspot_y);
1893}
1894
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001895struct wl_data_device *
1896input_get_data_device(struct input *input)
1897{
1898 return input->data_device;
1899}
1900
1901void
1902input_set_selection(struct input *input,
1903 struct wl_data_source *source, uint32_t time)
1904{
1905 wl_data_device_set_selection(input->data_device, source, time);
1906}
1907
1908void
1909input_accept(struct input *input, uint32_t time, const char *type)
1910{
1911 wl_data_offer_accept(input->drag_offer->offer, time, type);
1912}
1913
1914static void
1915offer_io_func(struct task *task, uint32_t events)
1916{
1917 struct data_offer *offer =
1918 container_of(task, struct data_offer, io_task);
1919 unsigned int len;
1920 char buffer[4096];
1921
1922 len = read(offer->fd, buffer, sizeof buffer);
1923 offer->func(buffer, len,
1924 offer->x, offer->y, offer->user_data);
1925
1926 if (len == 0) {
1927 close(offer->fd);
1928 data_offer_destroy(offer);
1929 }
1930}
1931
1932static void
1933data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1934 data_func_t func, void *user_data)
1935{
1936 int p[2];
1937
1938 pipe2(p, O_CLOEXEC);
1939 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1940 close(p[1]);
1941
1942 offer->io_task.run = offer_io_func;
1943 offer->fd = p[0];
1944 offer->func = func;
1945 offer->refcount++;
1946 offer->user_data = user_data;
1947
1948 display_watch_fd(offer->input->display,
1949 offer->fd, EPOLLIN, &offer->io_task);
1950}
1951
1952void
1953input_receive_drag_data(struct input *input, const char *mime_type,
1954 data_func_t func, void *data)
1955{
1956 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1957 input->drag_offer->x = input->sx;
1958 input->drag_offer->y = input->sy;
1959}
1960
1961int
1962input_receive_selection_data(struct input *input, const char *mime_type,
1963 data_func_t func, void *data)
1964{
1965 char **p;
1966
1967 if (input->selection_offer == NULL)
1968 return -1;
1969
1970 for (p = input->selection_offer->types.data; *p; p++)
1971 if (strcmp(mime_type, *p) == 0)
1972 break;
1973
1974 if (*p == NULL)
1975 return -1;
1976
1977 data_offer_receive_data(input->selection_offer,
1978 mime_type, func, data);
1979 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001980}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001981
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001982int
1983input_receive_selection_data_to_fd(struct input *input,
1984 const char *mime_type, int fd)
1985{
1986 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1987
1988 return 0;
1989}
1990
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001991void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001992window_move(struct window *window, struct input *input, uint32_t time)
1993{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001994 if (!window->shell_surface)
1995 return;
1996
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001997 wl_shell_surface_move(window->shell_surface,
1998 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001999}
2000
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002001static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002002window_resize(struct window *window, int32_t width, int32_t height)
2003{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002004 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002005 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002006
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002007 allocation.x = 0;
2008 allocation.y = 0;
2009 allocation.width = width;
2010 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002011
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002012 widget = window->widget;
2013 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002014 allocation.width, allocation.height);
2015
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002016 if (widget->resize_handler)
2017 widget->resize_handler(widget,
2018 allocation.width,
2019 allocation.height,
2020 widget->user_data);
2021
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002022 window->allocation = widget->allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002023
2024 window_schedule_redraw(window);
2025}
2026
2027static void
2028idle_resize(struct task *task, uint32_t events)
2029{
2030 struct window *window =
2031 container_of(task, struct window, resize_task);
2032
2033 window_resize(window,
2034 window->allocation.width, window->allocation.height);
2035 window->resize_scheduled = 0;
2036}
2037
2038void
2039window_schedule_resize(struct window *window, int width, int height)
2040{
2041 if (!window->resize_scheduled) {
2042 window->allocation.width = width;
2043 window->allocation.height = height;
2044 window->resize_task.run = idle_resize;
2045 display_defer(window->display, &window->resize_task);
2046 window->resize_scheduled = 1;
2047 }
2048}
2049
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002050void
2051widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2052{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002053 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002054}
2055
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002056static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002057handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002058 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002059 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002060{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002061 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002062
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002063 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002064 return;
2065
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002066 window->resize_edges = edges;
2067
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002068 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002069}
2070
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071static void
2072handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2073{
2074 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002075 struct menu *menu = window->widget->user_data;
2076
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002078 * device. Or just use wl_callback. And this really needs to
2079 * be a window vfunc that the menu can set. And we need the
2080 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002082 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002083 input_ungrab(menu->input, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002084 window_destroy(window);
2085}
2086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002088 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002090};
2091
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002092void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002093window_get_allocation(struct window *window,
2094 struct rectangle *allocation)
2095{
2096 *allocation = window->allocation;
2097}
2098
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002099static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002100widget_redraw(struct widget *widget)
2101{
2102 struct widget *child;
2103
2104 if (widget->redraw_handler)
2105 widget->redraw_handler(widget, widget->user_data);
2106 wl_list_for_each(child, &widget->child_list, link)
2107 widget_redraw(child);
2108}
2109
2110static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002111idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002112{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002113 struct window *window =
2114 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002115
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002116 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002117 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002118 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002119 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120}
2121
2122void
2123window_schedule_redraw(struct window *window)
2124{
2125 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002126 window->redraw_task.run = idle_redraw;
2127 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002128 window->redraw_scheduled = 1;
2129 }
2130}
2131
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002132void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002133window_set_custom(struct window *window)
2134{
2135 window->type = TYPE_CUSTOM;
2136}
2137
2138void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002139window_set_fullscreen(struct window *window, int fullscreen)
2140{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002141 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002142 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002143
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002144 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002145 return;
2146
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002147 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002148 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002149 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002150 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002151 width = output->allocation.width;
2152 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002153 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002154 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155 width = window->saved_allocation.width;
2156 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002157 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002160}
2161
2162void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002163window_set_user_data(struct window *window, void *data)
2164{
2165 window->user_data = data;
2166}
2167
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002168void *
2169window_get_user_data(struct window *window)
2170{
2171 return window->user_data;
2172}
2173
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002174void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002175window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002176 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002177{
2178 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002179}
2180
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002181void
2182window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002183 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002184{
2185 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002186}
2187
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002188void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002189window_set_data_handler(struct window *window, window_data_handler_t handler)
2190{
2191 window->data_handler = handler;
2192}
2193
2194void
2195window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2196{
2197 window->drop_handler = handler;
2198}
2199
2200void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002201window_set_close_handler(struct window *window,
2202 window_close_handler_t handler)
2203{
2204 window->close_handler = handler;
2205}
2206
2207void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002208window_set_transparent(struct window *window, int transparent)
2209{
2210 window->transparent = transparent;
2211}
2212
2213void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002214window_set_title(struct window *window, const char *title)
2215{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002216 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002217 window->title = strdup(title);
2218}
2219
2220const char *
2221window_get_title(struct window *window)
2222{
2223 return window->title;
2224}
2225
2226void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002227display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2228 int32_t x, int32_t y, int32_t width, int32_t height)
2229{
2230 struct wl_buffer *buffer;
2231
2232 buffer = display_get_buffer_for_surface(display, cairo_surface);
2233
2234 wl_buffer_damage(buffer, x, y, width, height);
2235}
2236
2237void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002238window_damage(struct window *window, int32_t x, int32_t y,
2239 int32_t width, int32_t height)
2240{
2241 wl_surface_damage(window->surface, x, y, width, height);
2242}
2243
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002244static struct window *
2245window_create_internal(struct display *display, struct window *parent,
2246 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002247{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002248 struct window *window;
2249
2250 window = malloc(sizeof *window);
2251 if (window == NULL)
2252 return NULL;
2253
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002254 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002255 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002256 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002257 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002258 if (display->shell) {
2259 window->shell_surface =
2260 wl_shell_get_shell_surface(display->shell,
2261 window->surface);
2262 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002263 window->allocation.x = 0;
2264 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002265 window->allocation.width = width;
2266 window->allocation.height = height;
2267 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002268 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002269
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002270 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002271#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002272 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2273 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002274#else
2275 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2276#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002277 else
2278 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002279
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002280 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002281 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002282
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002283 if (window->shell_surface) {
2284 wl_shell_surface_set_user_data(window->shell_surface, window);
2285 wl_shell_surface_add_listener(window->shell_surface,
2286 &shell_surface_listener, window);
2287 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002288
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002289 return window;
2290}
2291
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002292struct window *
2293window_create(struct display *display, int32_t width, int32_t height)
2294{
2295 struct window *window;
2296
2297 window = window_create_internal(display, NULL, width, height);
2298 if (!window)
2299 return NULL;
2300
2301 return window;
2302}
2303
2304struct window *
2305window_create_transient(struct display *display, struct window *parent,
2306 int32_t x, int32_t y, int32_t width, int32_t height)
2307{
2308 struct window *window;
2309
2310 window = window_create_internal(parent->display,
2311 parent, width, height);
2312 if (!window)
2313 return NULL;
2314
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002315 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002316 window->x = x;
2317 window->y = y;
2318
2319 return window;
2320}
2321
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002322static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002323menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002324{
2325 int next;
2326
2327 next = (sy - 8) / 20;
2328 if (menu->current != next) {
2329 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002330 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332}
2333
2334static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002335menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002336 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002337 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002338{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002339 struct menu *menu = data;
2340
2341 if (widget == menu->widget)
2342 menu_set_item(data, y);
2343
2344 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002345}
2346
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002347static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002348menu_enter_handler(struct widget *widget,
2349 struct input *input, uint32_t time,
2350 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002352 struct menu *menu = data;
2353
2354 if (widget == menu->widget)
2355 menu_set_item(data, y);
2356
2357 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002358}
2359
2360static void
2361menu_leave_handler(struct widget *widget, struct input *input, void *data)
2362{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002363 struct menu *menu = data;
2364
2365 if (widget == menu->widget)
2366 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002367}
2368
2369static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002370menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371 struct input *input, uint32_t time,
2372 int button, int state, void *data)
2373
2374{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002375 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002376
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002377 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002378 /* Either relase after press-drag-release or
2379 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002380 menu->func(menu->window->parent,
2381 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002382 input_ungrab(input, time);
2383 window_destroy(menu->widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002384 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002385}
2386
2387static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002388menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002389{
2390 cairo_t *cr;
2391 const int32_t r = 3, margin = 3;
2392 struct menu *menu = data;
2393 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002394 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002395
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002396 cr = cairo_create(window->cairo_surface);
2397 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2398 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2399 cairo_paint(cr);
2400
2401 width = window->allocation.width;
2402 height = window->allocation.height;
2403 rounded_rect(cr, 0, 0, width, height, r);
2404 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2405 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2406 cairo_fill(cr);
2407
2408 for (i = 0; i < menu->count; i++) {
2409 if (i == menu->current) {
2410 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2411 cairo_rectangle(cr, margin, i * 20 + margin,
2412 width - 2 * margin, 20);
2413 cairo_fill(cr);
2414 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2415 cairo_move_to(cr, 10, i * 20 + 16);
2416 cairo_show_text(cr, menu->entries[i]);
2417 } else {
2418 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2419 cairo_move_to(cr, 10, i * 20 + 16);
2420 cairo_show_text(cr, menu->entries[i]);
2421 }
2422 }
2423
2424 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002425}
2426
2427struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002428window_create_menu(struct display *display,
2429 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002430 int32_t x, int32_t y,
2431 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002432{
2433 struct window *window;
2434 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002435 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436
2437 menu = malloc(sizeof *menu);
2438 if (!menu)
2439 return NULL;
2440
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002441 window = window_create_internal(parent->display, parent,
2442 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002443 if (!window)
2444 return NULL;
2445
2446 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002447 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448 menu->entries = entries;
2449 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002450 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002451 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002452 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002453 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002454 window->type = TYPE_MENU;
2455 window->x = x;
2456 window->y = y;
2457
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002458 wl_shell_surface_set_popup(window->shell_surface,
2459 input->input_device, time,
2460 window->parent->shell_surface,
2461 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002463 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002464 widget_set_enter_handler(menu->widget, menu_enter_handler);
2465 widget_set_leave_handler(menu->widget, menu_leave_handler);
2466 widget_set_motion_handler(menu->widget, menu_motion_handler);
2467 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002468
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002469 input_grab(input, menu->widget, 0);
2470
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002471 return window;
2472}
2473
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002474void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002475window_set_buffer_type(struct window *window, enum window_buffer_type type)
2476{
2477 window->buffer_type = type;
2478}
2479
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002480
2481static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002482display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002483 struct wl_output *wl_output,
2484 int x, int y,
2485 int physical_width,
2486 int physical_height,
2487 int subpixel,
2488 const char *make,
2489 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002490{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002491 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002492
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002493 output->allocation.x = x;
2494 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002495}
2496
2497static void
2498display_handle_mode(void *data,
2499 struct wl_output *wl_output,
2500 uint32_t flags,
2501 int width,
2502 int height,
2503 int refresh)
2504{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002505 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002506 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002507
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002508 if (flags & WL_OUTPUT_MODE_CURRENT) {
2509 output->allocation.width = width;
2510 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002511 if (display->output_configure_handler)
2512 (*display->output_configure_handler)(
2513 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002514 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002515}
2516
2517static const struct wl_output_listener output_listener = {
2518 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002519 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002520};
2521
2522static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002523display_add_output(struct display *d, uint32_t id)
2524{
2525 struct output *output;
2526
2527 output = malloc(sizeof *output);
2528 if (output == NULL)
2529 return;
2530
2531 memset(output, 0, sizeof *output);
2532 output->display = d;
2533 output->output =
2534 wl_display_bind(d->display, id, &wl_output_interface);
2535 wl_list_insert(d->output_list.prev, &output->link);
2536
2537 wl_output_add_listener(output->output, &output_listener, output);
2538}
2539
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002540static void
2541output_destroy(struct output *output)
2542{
2543 if (output->destroy_handler)
2544 (*output->destroy_handler)(output, output->user_data);
2545
2546 wl_output_destroy(output->output);
2547 wl_list_remove(&output->link);
2548 free(output);
2549}
2550
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002551void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002552display_set_output_configure_handler(struct display *display,
2553 display_output_handler_t handler)
2554{
2555 struct output *output;
2556
2557 display->output_configure_handler = handler;
2558 if (!handler)
2559 return;
2560
2561 wl_list_for_each(output, &display->output_list, link)
2562 (*display->output_configure_handler)(output,
2563 display->user_data);
2564}
2565
2566void
2567output_set_user_data(struct output *output, void *data)
2568{
2569 output->user_data = data;
2570}
2571
2572void *
2573output_get_user_data(struct output *output)
2574{
2575 return output->user_data;
2576}
2577
2578void
2579output_set_destroy_handler(struct output *output,
2580 display_output_handler_t handler)
2581{
2582 output->destroy_handler = handler;
2583 /* FIXME: implement this, once we have way to remove outputs */
2584}
2585
2586void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002587output_get_allocation(struct output *output, struct rectangle *allocation)
2588{
2589 *allocation = output->allocation;
2590}
2591
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002592struct wl_output *
2593output_get_wl_output(struct output *output)
2594{
2595 return output->output;
2596}
2597
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002598static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002599display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002600{
2601 struct input *input;
2602
2603 input = malloc(sizeof *input);
2604 if (input == NULL)
2605 return;
2606
2607 memset(input, 0, sizeof *input);
2608 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002609 input->input_device =
2610 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002611 input->pointer_focus = NULL;
2612 input->keyboard_focus = NULL;
2613 wl_list_insert(d->input_list.prev, &input->link);
2614
2615 wl_input_device_add_listener(input->input_device,
2616 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002617 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002618
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002619 input->data_device =
2620 wl_data_device_manager_get_data_device(d->data_device_manager,
2621 input->input_device);
2622 wl_data_device_add_listener(input->data_device,
2623 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002624}
2625
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002626static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002627input_destroy(struct input *input)
2628{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002629 input_remove_keyboard_focus(input);
2630 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002631
2632 if (input->drag_offer)
2633 data_offer_destroy(input->drag_offer);
2634
2635 if (input->selection_offer)
2636 data_offer_destroy(input->selection_offer);
2637
2638 wl_data_device_destroy(input->data_device);
2639 wl_list_remove(&input->link);
2640 wl_input_device_destroy(input->input_device);
2641 free(input);
2642}
2643
2644static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002645display_handle_global(struct wl_display *display, uint32_t id,
2646 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002647{
2648 struct display *d = data;
2649
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002650 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002651 d->compositor =
2652 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002653 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002654 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002655 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002656 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002657 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002658 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002659 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002660 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002661 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2662 d->data_device_manager =
2663 wl_display_bind(display, id,
2664 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002665 }
2666}
2667
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002668static void
2669display_render_frame(struct display *d)
2670{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002671 int radius = 8;
2672 cairo_t *cr;
2673
2674 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2675 cr = cairo_create(d->shadow);
2676 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2677 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2678 rounded_rect(cr, 16, 16, 112, 112, radius);
2679 cairo_fill(cr);
2680 cairo_destroy(cr);
2681 blur_surface(d->shadow, 64);
2682
2683 d->active_frame =
2684 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2685 cr = cairo_create(d->active_frame);
2686 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2687 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2688 rounded_rect(cr, 16, 16, 112, 112, radius);
2689 cairo_fill(cr);
2690 cairo_destroy(cr);
2691
2692 d->inactive_frame =
2693 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2694 cr = cairo_create(d->inactive_frame);
2695 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2696 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2697 rounded_rect(cr, 16, 16, 112, 112, radius);
2698 cairo_fill(cr);
2699 cairo_destroy(cr);
2700}
2701
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002702static void
2703init_xkb(struct display *d)
2704{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002705 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002706
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002707 names.rules = "evdev";
2708 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002709 names.layout = option_xkb_layout;
2710 names.variant = option_xkb_variant;
2711 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002712
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002713 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002714 if (!d->xkb) {
2715 fprintf(stderr, "Failed to compile keymap\n");
2716 exit(1);
2717 }
2718}
2719
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002720static void
2721fini_xkb(struct display *display)
2722{
2723 xkb_free_keymap(display->xkb);
2724}
2725
Yuval Fledel45568f62010-12-06 09:18:12 -05002726static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002727init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002728{
2729 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002730 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002731
2732 static const EGLint premul_argb_cfg_attribs[] = {
2733 EGL_SURFACE_TYPE,
2734 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2735 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002736 EGL_RED_SIZE, 1,
2737 EGL_GREEN_SIZE, 1,
2738 EGL_BLUE_SIZE, 1,
2739 EGL_ALPHA_SIZE, 1,
2740 EGL_DEPTH_SIZE, 1,
2741 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2742 EGL_NONE
2743 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002744
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002745 static const EGLint rgb_cfg_attribs[] = {
2746 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2747 EGL_RED_SIZE, 1,
2748 EGL_GREEN_SIZE, 1,
2749 EGL_BLUE_SIZE, 1,
2750 EGL_ALPHA_SIZE, 0,
2751 EGL_DEPTH_SIZE, 1,
2752 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2753 EGL_NONE
2754 };
2755
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002756 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002757 if (!eglInitialize(d->dpy, &major, &minor)) {
2758 fprintf(stderr, "failed to initialize display\n");
2759 return -1;
2760 }
2761
2762 if (!eglBindAPI(EGL_OPENGL_API)) {
2763 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2764 return -1;
2765 }
2766
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002767 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002768 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002769 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002770 return -1;
2771 }
2772
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002773 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2774 &d->rgb_config, 1, &n) || n != 1) {
2775 fprintf(stderr, "failed to choose rgb config\n");
2776 return -1;
2777 }
2778
Benjamin Franzke0c991632011-09-27 21:57:31 +02002779 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2780 if (d->rgb_ctx == NULL) {
2781 fprintf(stderr, "failed to create context\n");
2782 return -1;
2783 }
2784 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2785 EGL_NO_CONTEXT, NULL);
2786 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002787 fprintf(stderr, "failed to create context\n");
2788 return -1;
2789 }
2790
Benjamin Franzke0c991632011-09-27 21:57:31 +02002791 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002792 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002793 return -1;
2794 }
2795
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002796#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002797 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2798 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002799 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002800 return -1;
2801 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002802 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2803 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2804 fprintf(stderr, "failed to get cairo egl argb device\n");
2805 return -1;
2806 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002807#endif
2808
2809 return 0;
2810}
2811
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002812static void
2813fini_egl(struct display *display)
2814{
2815#ifdef HAVE_CAIRO_EGL
2816 cairo_device_destroy(display->argb_device);
2817 cairo_device_destroy(display->rgb_device);
2818#endif
2819
2820 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2821 EGL_NO_CONTEXT);
2822
2823 eglTerminate(display->dpy);
2824 eglReleaseThread();
2825}
2826
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002827static int
2828event_mask_update(uint32_t mask, void *data)
2829{
2830 struct display *d = data;
2831
2832 d->mask = mask;
2833
2834 return 0;
2835}
2836
2837static void
2838handle_display_data(struct task *task, uint32_t events)
2839{
2840 struct display *display =
2841 container_of(task, struct display, display_task);
2842
2843 wl_display_iterate(display->display, display->mask);
2844}
2845
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002846struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002847display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002848{
2849 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002850 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002851 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002852 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002853
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002854 g_type_init();
2855
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002856 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002857 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002858 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002859
2860 xkb_option_group = g_option_group_new("xkb",
2861 "Keyboard options",
2862 "Show all XKB options",
2863 NULL, NULL);
2864 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2865 g_option_context_add_group (context, xkb_option_group);
2866
2867 if (!g_option_context_parse(context, argc, argv, &error)) {
2868 fprintf(stderr, "option parsing failed: %s\n", error->message);
2869 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002870 }
2871
Tim Wiederhake748f6722011-01-23 23:25:25 +01002872 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002873
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002874 d = malloc(sizeof *d);
2875 if (d == NULL)
2876 return NULL;
2877
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002878 memset(d, 0, sizeof *d);
2879
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002880 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002881 if (d->display == NULL) {
2882 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002883 return NULL;
2884 }
2885
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002886 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2887 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2888 d->display_task.run = handle_display_data;
2889 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2890
2891 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002892 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002893 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002894
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002895 /* Set up listener so we'll catch all events. */
2896 wl_display_add_global_listener(d->display,
2897 display_handle_global, d);
2898
2899 /* Process connection events. */
2900 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002901 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002902 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002903
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002904 d->image_target_texture_2d =
2905 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2906 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2907 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2908
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002909 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002910
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002911 display_render_frame(d);
2912
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002913 wl_list_init(&d->window_list);
2914
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002915 init_xkb(d);
2916
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002917 return d;
2918}
2919
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002920static void
2921display_destroy_outputs(struct display *display)
2922{
2923 struct output *tmp;
2924 struct output *output;
2925
2926 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2927 output_destroy(output);
2928}
2929
Pekka Paalanene1207c72011-12-16 12:02:09 +02002930static void
2931display_destroy_inputs(struct display *display)
2932{
2933 struct input *tmp;
2934 struct input *input;
2935
2936 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2937 input_destroy(input);
2938}
2939
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002940void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002941display_destroy(struct display *display)
2942{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002943 if (!wl_list_empty(&display->window_list))
2944 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2945
2946 if (!wl_list_empty(&display->deferred_list))
2947 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2948
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002949 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002950 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002951
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002952 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002953
2954 cairo_surface_destroy(display->active_frame);
2955 cairo_surface_destroy(display->inactive_frame);
2956 cairo_surface_destroy(display->shadow);
2957 destroy_pointer_surfaces(display);
2958
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002959 fini_egl(display);
2960
Pekka Paalanenc2052982011-12-16 11:41:32 +02002961 if (display->shell)
2962 wl_shell_destroy(display->shell);
2963
2964 if (display->shm)
2965 wl_shm_destroy(display->shm);
2966
2967 if (display->data_device_manager)
2968 wl_data_device_manager_destroy(display->data_device_manager);
2969
2970 wl_compositor_destroy(display->compositor);
2971
2972 close(display->epoll_fd);
2973
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002974 wl_display_flush(display->display);
2975 wl_display_destroy(display->display);
2976 free(display);
2977}
2978
2979void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002980display_set_user_data(struct display *display, void *data)
2981{
2982 display->user_data = data;
2983}
2984
2985void *
2986display_get_user_data(struct display *display)
2987{
2988 return display->user_data;
2989}
2990
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002991struct wl_display *
2992display_get_display(struct display *display)
2993{
2994 return display->display;
2995}
2996
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002997struct output *
2998display_get_output(struct display *display)
2999{
3000 return container_of(display->output_list.next, struct output, link);
3001}
3002
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003003struct wl_compositor *
3004display_get_compositor(struct display *display)
3005{
3006 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003007}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003008
3009EGLDisplay
3010display_get_egl_display(struct display *d)
3011{
3012 return d->dpy;
3013}
3014
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003015struct wl_data_source *
3016display_create_data_source(struct display *display)
3017{
3018 return wl_data_device_manager_create_data_source(display->data_device_manager);
3019}
3020
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003021EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003022display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003023{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003024 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003025}
3026
Benjamin Franzke0c991632011-09-27 21:57:31 +02003027EGLConfig
3028display_get_argb_egl_config(struct display *d)
3029{
3030 return d->premultiplied_argb_config;
3031}
3032
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003033struct wl_shell *
3034display_get_shell(struct display *display)
3035{
3036 return display->shell;
3037}
3038
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003039int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003040display_acquire_window_surface(struct display *display,
3041 struct window *window,
3042 EGLContext ctx)
3043{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003044#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003045 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003046 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047
3048 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003049 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003050 device = cairo_surface_get_device(window->cairo_surface);
3051 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003052 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003053
Benjamin Franzke0c991632011-09-27 21:57:31 +02003054 if (!ctx) {
3055 if (device == display->rgb_device)
3056 ctx = display->rgb_ctx;
3057 else if (device == display->argb_device)
3058 ctx = display->argb_ctx;
3059 else
3060 assert(0);
3061 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003062
3063 data = cairo_surface_get_user_data(window->cairo_surface,
3064 &surface_data_key);
3065
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003066 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003067 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003068 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3069 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003070
3071 return 0;
3072#else
3073 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003074#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003075}
3076
3077void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003078display_release_window_surface(struct display *display,
3079 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003080{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003081#ifdef HAVE_CAIRO_EGL
3082 cairo_device_t *device;
3083
3084 device = cairo_surface_get_device(window->cairo_surface);
3085 if (!device)
3086 return;
3087
3088 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003090 cairo_device_release(device);
3091#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003092}
3093
3094void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003095display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003096{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003097 wl_list_insert(&display->deferred_list, &task->link);
3098}
3099
3100void
3101display_watch_fd(struct display *display,
3102 int fd, uint32_t events, struct task *task)
3103{
3104 struct epoll_event ep;
3105
3106 ep.events = events;
3107 ep.data.ptr = task;
3108 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3109}
3110
3111void
3112display_run(struct display *display)
3113{
3114 struct task *task;
3115 struct epoll_event ep[16];
3116 int i, count;
3117
Pekka Paalanen826d7952011-12-15 10:14:07 +02003118 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003119 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003120 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003121
3122 while (!wl_list_empty(&display->deferred_list)) {
3123 task = container_of(display->deferred_list.next,
3124 struct task, link);
3125 wl_list_remove(&task->link);
3126 task->run(task, 0);
3127 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003128
3129 if (!display->running)
3130 break;
3131
3132 wl_display_flush(display->display);
3133
3134 count = epoll_wait(display->epoll_fd,
3135 ep, ARRAY_LENGTH(ep), -1);
3136 for (i = 0; i < count; i++) {
3137 task = ep[i].data.ptr;
3138 task->run(task, ep[i].events);
3139 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003140 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003141}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003142
3143void
3144display_exit(struct display *display)
3145{
3146 display->running = 0;
3147}