blob: 4e109774d4583f8146312a636bd500d6041e474c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Daniel Stone9d4f0302012-02-15 16:33:21 +000052#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050053#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040054
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020056#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040057#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050060
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050062 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050063 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040064 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040065 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050069 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Pekka Paalanen826d7952011-12-15 10:14:07 +020082 int running;
83
Kristian Høgsberg478d9262010-06-08 20:34:11 -040084 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040085 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050086 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040087 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040088 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040089 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040090
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050091 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
92 PFNEGLCREATEIMAGEKHRPROC create_image;
93 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020094
95 display_output_handler_t output_configure_handler;
96
97 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050098};
99
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400101 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400102 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500103 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500105 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400106 TYPE_CUSTOM
107};
108
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500109struct window {
110 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500111 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500112 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500114 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500115 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500116 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500117 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400118 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400119 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400120 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500121 int resize_scheduled;
122 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400124 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400126 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500127
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500129
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500130 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500131 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400132 window_data_handler_t data_handler;
133 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500134 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400135
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200136 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500137 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500138
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500139 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400140 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500141};
142
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500143struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500144 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500145 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400146 struct wl_list link;
147 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500148 widget_resize_handler_t resize_handler;
149 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500150 widget_enter_handler_t enter_handler;
151 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500152 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500153 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400154 void *user_data;
155};
156
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400157struct input {
158 struct display *display;
159 struct wl_input_device *input_device;
160 struct window *pointer_focus;
161 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400162 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400163 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200164 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400166
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500167 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500168 struct widget *grab;
169 uint32_t grab_button;
170
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400171 struct wl_data_device *data_device;
172 struct data_offer *drag_offer;
173 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174};
175
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500176struct output {
177 struct display *display;
178 struct wl_output *output;
179 struct rectangle allocation;
180 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200181
182 display_output_handler_t destroy_handler;
183 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500184};
185
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500186struct frame {
187 struct widget *widget;
188 struct widget *child;
189 int margin;
190};
191
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500192struct menu {
193 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500194 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500195 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500196 const char **entries;
197 uint32_t time;
198 int current;
199 int count;
200 menu_func_t func;
201};
202
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400203enum {
204 POINTER_DEFAULT = 100,
205 POINTER_UNSET
206};
207
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500208enum window_location {
209 WINDOW_INTERIOR = 0,
210 WINDOW_RESIZING_TOP = 1,
211 WINDOW_RESIZING_BOTTOM = 2,
212 WINDOW_RESIZING_LEFT = 4,
213 WINDOW_RESIZING_TOP_LEFT = 5,
214 WINDOW_RESIZING_BOTTOM_LEFT = 6,
215 WINDOW_RESIZING_RIGHT = 8,
216 WINDOW_RESIZING_TOP_RIGHT = 9,
217 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
218 WINDOW_RESIZING_MASK = 15,
219 WINDOW_EXTERIOR = 16,
220 WINDOW_TITLEBAR = 17,
221 WINDOW_CLIENT_AREA = 18,
222};
223
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400224const char *option_xkb_layout = "us";
225const char *option_xkb_variant = "";
226const char *option_xkb_options = "";
227
228static const GOptionEntry xkb_option_entries[] = {
229 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
230 &option_xkb_layout, "XKB Layout" },
231 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
232 &option_xkb_variant, "XKB Variant" },
233 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
234 &option_xkb_options, "XKB Options" },
235 { NULL }
236};
237
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400238static const cairo_user_data_key_t surface_data_key;
239struct surface_data {
240 struct wl_buffer *buffer;
241};
242
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500243#define MULT(_d,c,a,t) \
244 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
245
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500246#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100248struct egl_window_surface_data {
249 struct display *display;
250 struct wl_surface *surface;
251 struct wl_egl_window *window;
252 EGLSurface surf;
253};
254
255static void
256egl_window_surface_data_destroy(void *p)
257{
258 struct egl_window_surface_data *data = p;
259 struct display *d = data->display;
260
261 eglDestroySurface(d->dpy, data->surf);
262 wl_egl_window_destroy(data->window);
263 data->surface = NULL;
264
265 free(p);
266}
267
268static cairo_surface_t *
269display_create_egl_window_surface(struct display *display,
270 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400271 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272 struct rectangle *rectangle)
273{
274 cairo_surface_t *cairo_surface;
275 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200277 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278
279 data = malloc(sizeof *data);
280 if (data == NULL)
281 return NULL;
282
283 data->display = display;
284 data->surface = surface;
285
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 if (flags & SURFACE_OPAQUE) {
287 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500290 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400292 }
293
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400294 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500299 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300
Benjamin Franzke0c991632011-09-27 21:57:31 +0200301 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 data->surf,
303 rectangle->width,
304 rectangle->height);
305
306 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
307 data, egl_window_surface_data_destroy);
308
309 return cairo_surface;
310}
311
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 EGLImageKHR image;
316 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319};
320
321static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500333 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335}
336
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_get_image_for_egl_image_surface(struct display *display,
339 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342
343 data = cairo_surface_get_user_data (surface, &surface_data_key);
344
345 return data->image;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 EGLDisplay dpy = display->dpy;
355 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
358 data = malloc(sizeof *data);
359 if (data == NULL)
360 return NULL;
361
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800362 data->display = display;
363
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400364 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000367 free(data);
368 return NULL;
369 }
370
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 if (flags & SURFACE_OPAQUE) {
372 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR;
374 } else {
375 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 content = CAIRO_CONTENT_COLOR_ALPHA;
377 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500379 data->image = display->create_image(dpy, NULL,
380 EGL_NATIVE_PIXMAP_KHR,
381 (EGLClientBuffer) data->pixmap,
382 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500384 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500385 free(data);
386 return NULL;
387 }
388
389 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400390 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500391
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500395 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397
Benjamin Franzke0c991632011-09-27 21:57:31 +0200398 surface = cairo_gl_surface_create_for_texture(data->device,
399 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 data->texture,
401 rectangle->width,
402 rectangle->height);
403
404 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500405 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406
407 return surface;
408}
409
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500410static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500411display_create_egl_image_surface_from_file(struct display *display,
412 const char *filename,
413 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414{
415 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400416 GdkPixbuf *pixbuf;
417 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400418 int stride, i;
419 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500420 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
422 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424 FALSE, &error);
425 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400427
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400428 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
429 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500430 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400433
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400434
435 stride = gdk_pixbuf_get_rowstride(pixbuf);
436 pixels = gdk_pixbuf_get_pixels(pixbuf);
437
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400440 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 while (p < end) {
442 unsigned int t;
443
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400444 MULT(p[0], p[0], p[3], t);
445 MULT(p[1], p[1], p[3], t);
446 MULT(p[2], p[2], p[3], t);
447 p += 4;
448
449 }
450 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400451
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200452 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000453 if (surface == NULL) {
454 g_object_unref(pixbuf);
455 return NULL;
456 }
457
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 data = cairo_surface_get_user_data(surface, &surface_data_key);
459
Benjamin Franzke0c991632011-09-27 21:57:31 +0200460 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800461 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800462 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
463 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200464 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500466 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400467
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468 return surface;
469}
470
471#endif
472
473struct wl_buffer *
474display_get_buffer_for_surface(struct display *display,
475 cairo_surface_t *surface)
476{
477 struct surface_data *data;
478
479 data = cairo_surface_get_user_data (surface, &surface_data_key);
480
481 return data->buffer;
482}
483
484struct shm_surface_data {
485 struct surface_data data;
486 void *map;
487 size_t length;
488};
489
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500490static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491shm_surface_data_destroy(void *p)
492{
493 struct shm_surface_data *data = p;
494
495 wl_buffer_destroy(data->data.buffer);
496 munmap(data->map, data->length);
497}
498
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500499static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400501 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400502{
503 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400504 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400505 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800506 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507 char filename[] = "/tmp/wayland-shm-XXXXXX";
508
509 data = malloc(sizeof *data);
510 if (data == NULL)
511 return NULL;
512
513 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
514 rectangle->width);
515 data->length = stride * rectangle->height;
516 fd = mkstemp(filename);
517 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000518 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519 return NULL;
520 }
521 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000522 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 close(fd);
524 return NULL;
525 }
526
527 data->map = mmap(NULL, data->length,
528 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
529 unlink(filename);
530
531 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000532 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533 close(fd);
534 return NULL;
535 }
536
537 surface = cairo_image_surface_create_for_data (data->map,
538 CAIRO_FORMAT_ARGB32,
539 rectangle->width,
540 rectangle->height,
541 stride);
542
543 cairo_surface_set_user_data (surface, &surface_data_key,
544 data, shm_surface_data_destroy);
545
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500547 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 data->data.buffer = wl_shm_create_buffer(display->shm,
552 fd,
553 rectangle->width,
554 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 close(fd);
558
559 return surface;
560}
561
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500562static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563display_create_shm_surface_from_file(struct display *display,
564 const char *filename,
565 struct rectangle *rect)
566{
567 cairo_surface_t *surface;
568 GdkPixbuf *pixbuf;
569 GError *error = NULL;
570 int stride, i;
571 unsigned char *pixels, *p, *end, *dest_data;
572 int dest_stride;
573 uint32_t *d;
574
575 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
576 rect->width, rect->height,
577 FALSE, &error);
578 if (error != NULL)
579 return NULL;
580
581 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
582 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500583 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 return NULL;
585 }
586
587 stride = gdk_pixbuf_get_rowstride(pixbuf);
588 pixels = gdk_pixbuf_get_pixels(pixbuf);
589
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400590 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000591 if (surface == NULL) {
592 g_object_unref(pixbuf);
593 return NULL;
594 }
595
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596 dest_data = cairo_image_surface_get_data (surface);
597 dest_stride = cairo_image_surface_get_stride (surface);
598
599 for (i = 0; i < rect->height; i++) {
600 d = (uint32_t *) (dest_data + i * dest_stride);
601 p = pixels + i * stride;
602 end = p + rect->width * 4;
603 while (p < end) {
604 unsigned int t;
605 unsigned char a, r, g, b;
606
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607 a = p[3];
608 MULT(r, p[0], a, t);
609 MULT(g, p[1], a, t);
610 MULT(b, p[2], a, t);
611 p += 4;
612 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
613 }
614 }
615
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500616 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617
618 return surface;
619}
620
nobled7b87cb02011-02-01 18:51:47 +0000621static int
622check_size(struct rectangle *rect)
623{
624 if (rect->width && rect->height)
625 return 0;
626
627 fprintf(stderr, "tried to create surface of "
628 "width: %d, height: %d\n", rect->width, rect->height);
629 return -1;
630}
631
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632cairo_surface_t *
633display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400635 struct rectangle *rectangle,
636 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637{
nobled7b87cb02011-02-01 18:51:47 +0000638 if (check_size(rectangle) < 0)
639 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500641 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 if (surface)
643 return display_create_egl_window_surface(display,
644 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 rectangle);
647 else
648 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400649 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500651 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400653 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654}
655
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500656static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657display_create_surface_from_file(struct display *display,
658 const char *filename,
659 struct rectangle *rectangle)
660{
nobled7b87cb02011-02-01 18:51:47 +0000661 if (check_size(rectangle) < 0)
662 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500663#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500664 if (display->dpy) {
665 return display_create_egl_image_surface_from_file(display,
666 filename,
667 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671}
Yuval Fledel45568f62010-12-06 09:18:12 -0500672 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673 const char *filename;
674 int hotspot_x, hotspot_y;
675} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500676 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
677 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
678 { DATADIR "/weston/bottom_side.png", 16, 20 },
679 { DATADIR "/weston/grabbing.png", 20, 17 },
680 { DATADIR "/weston/left_ptr.png", 10, 5 },
681 { DATADIR "/weston/left_side.png", 10, 20 },
682 { DATADIR "/weston/right_side.png", 30, 19 },
683 { DATADIR "/weston/top_left_corner.png", 8, 8 },
684 { DATADIR "/weston/top_right_corner.png", 26, 8 },
685 { DATADIR "/weston/top_side.png", 18, 8 },
686 { DATADIR "/weston/xterm.png", 15, 15 },
687 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688};
689
690static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692{
693 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400698 display->pointer_surfaces =
699 malloc(count * sizeof *display->pointer_surfaces);
700 rect.width = width;
701 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 display_create_surface_from_file(display,
705 pointer_images[i].filename,
706 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100707 if (!display->pointer_surfaces[i]) {
708 fprintf(stderr, "Error loading pointer image: %s\n",
709 pointer_images[i].filename);
710 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400711 }
712
713}
714
Pekka Paalanen325bb602011-12-19 10:31:45 +0200715static void
716destroy_pointer_surfaces(struct display *display)
717{
718 int i, count;
719
720 count = ARRAY_LENGTH(pointer_images);
721 for (i = 0; i < count; ++i) {
722 if (display->pointer_surfaces[i])
723 cairo_surface_destroy(display->pointer_surfaces[i]);
724 }
725 free(display->pointer_surfaces);
726}
727
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728cairo_surface_t *
729display_get_pointer_surface(struct display *display, int pointer,
730 int *width, int *height,
731 int *hotspot_x, int *hotspot_y)
732{
733 cairo_surface_t *surface;
734
735 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500736#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400737 *width = cairo_gl_surface_get_width(surface);
738 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000739#else
740 *width = cairo_image_surface_get_width(surface);
741 *height = cairo_image_surface_get_height(surface);
742#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400743 *hotspot_x = pointer_images[pointer].hotspot_x;
744 *hotspot_y = pointer_images[pointer].hotspot_y;
745
746 return cairo_surface_reference(surface);
747}
748
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400749static void
750window_attach_surface(struct window *window);
751
752static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754{
755 struct window *window = data;
756
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758 cairo_surface_destroy(window->pending_surface);
759 window->pending_surface = NULL;
760 if (window->cairo_surface)
761 window_attach_surface(window);
762}
763
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400764static const struct wl_callback_listener free_surface_listener = {
765 free_surface
766};
767
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500768static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200769window_get_resize_dx_dy(struct window *window, int *x, int *y)
770{
771 if (window->resize_edges & WINDOW_RESIZING_LEFT)
772 *x = window->server_allocation.width - window->allocation.width;
773 else
774 *x = 0;
775
776 if (window->resize_edges & WINDOW_RESIZING_TOP)
777 *y = window->server_allocation.height -
778 window->allocation.height;
779 else
780 *y = 0;
781
782 window->resize_edges = 0;
783}
784
785static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500786window_attach_surface(struct window *window)
787{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400788 struct display *display = window->display;
789 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400790 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000791#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500794 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795
796 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
799 data = cairo_surface_get_user_data(window->cairo_surface,
800 &surface_data_key);
801
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 cairo_gl_surface_swapbuffers(window->cairo_surface);
803 wl_egl_window_get_attached_size(data->window,
804 &window->server_allocation.width,
805 &window->server_allocation.height);
806 break;
807 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000808#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 case WINDOW_BUFFER_TYPE_SHM:
810 if (window->pending_surface != NULL)
811 return;
812
813 window->pending_surface = window->cairo_surface;
814 window->cairo_surface = NULL;
815
816 buffer =
817 display_get_buffer_for_surface(display,
818 window->pending_surface);
819
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200820 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 wl_surface_attach(window->surface, buffer, x, y);
822 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400823 cb = wl_display_sync(display->display);
824 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826 default:
827 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500829
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500830 wl_surface_damage(window->surface, 0, 0,
831 window->allocation.width,
832 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500833}
834
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500835void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400836window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500837{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100838 if (window->cairo_surface) {
839 switch (window->buffer_type) {
840 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
841 case WINDOW_BUFFER_TYPE_SHM:
842 display_surface_damage(window->display,
843 window->cairo_surface,
844 0, 0,
845 window->allocation.width,
846 window->allocation.height);
847 break;
848 default:
849 break;
850 }
851 window_attach_surface(window);
852 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500853}
854
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855void
856window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500858 cairo_surface_reference(surface);
859
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400860 if (window->cairo_surface != NULL)
861 cairo_surface_destroy(window->cairo_surface);
862
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500863 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400864}
865
Benjamin Franzke22d54812011-07-16 19:50:32 +0000866#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867static void
868window_resize_cairo_window_surface(struct window *window)
869{
870 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200871 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872
873 data = cairo_surface_get_user_data(window->cairo_surface,
874 &surface_data_key);
875
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200876 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877 wl_egl_window_resize(data->window,
878 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200879 window->allocation.height,
880 x,y);
881
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100882 cairo_gl_surface_set_size(window->cairo_surface,
883 window->allocation.width,
884 window->allocation.height);
885}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000886#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400888struct display *
889window_get_display(struct window *window)
890{
891 return window->display;
892}
893
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894void
895window_create_surface(struct window *window)
896{
897 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400898 uint32_t flags = 0;
899
900 if (!window->transparent)
901 flags = SURFACE_OPAQUE;
902
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500904#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
906 if (window->cairo_surface) {
907 window_resize_cairo_window_surface(window);
908 return;
909 }
910 surface = display_create_surface(window->display,
911 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500914 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400915 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400917 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400918 break;
919#endif
920 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800924 default:
925 surface = NULL;
926 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400927 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928
929 window_set_surface(window, surface);
930 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931}
932
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200933static void frame_destroy(struct frame *frame);
934
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400935void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500936window_destroy(struct window *window)
937{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200938 struct display *display = window->display;
939 struct input *input;
940
941 if (window->redraw_scheduled)
942 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500943 if (window->resize_scheduled)
944 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200945
946 wl_list_for_each(input, &display->input_list, link) {
947 if (input->pointer_focus == window)
948 input->pointer_focus = NULL;
949 if (input->keyboard_focus == window)
950 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500951 if (input->focus_widget &&
952 input->focus_widget->window == window)
953 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200954 }
955
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200956 if (window->frame)
957 frame_destroy(window->frame);
958
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200959 if (window->shell_surface)
960 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500961 wl_surface_destroy(window->surface);
962 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200963
964 if (window->cairo_surface != NULL)
965 cairo_surface_destroy(window->cairo_surface);
966 if (window->pending_surface != NULL)
967 cairo_surface_destroy(window->pending_surface);
968
969 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500970 free(window);
971}
972
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500973static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500974widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400975{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500976 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500978 wl_list_for_each(child, &widget->child_list, link) {
979 target = widget_find_widget(child, x, y);
980 if (target)
981 return target;
982 }
983
984 if (widget->allocation.x <= x &&
985 x < widget->allocation.x + widget->allocation.width &&
986 widget->allocation.y <= y &&
987 y < widget->allocation.y + widget->allocation.height) {
988 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989 }
990
991 return NULL;
992}
993
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500994static struct widget *
995widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 widget = malloc(sizeof *widget);
1000 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001001 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001003 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004 wl_list_init(&widget->child_list);
1005
1006 return widget;
1007}
1008
1009struct widget *
1010window_add_widget(struct window *window, void *data)
1011{
1012 window->widget = widget_create(window, data);
1013 wl_list_init(&window->widget->link);
1014
1015 return window->widget;
1016}
1017
1018struct widget *
1019widget_add_widget(struct widget *parent, void *data)
1020{
1021 struct widget *widget;
1022
1023 widget = widget_create(parent->window, data);
1024 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027}
1028
1029void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001030widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001031{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001032 struct display *display = widget->window->display;
1033 struct input *input;
1034
1035 wl_list_for_each(input, &display->input_list, link) {
1036 if (input->focus_widget == widget)
1037 input->focus_widget = NULL;
1038 }
1039
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001040 wl_list_remove(&widget->link);
1041 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042}
1043
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001046{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001047 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048}
1049
1050void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001051widget_set_size(struct widget *widget, int32_t width, int32_t height)
1052{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001053 widget->allocation.width = width;
1054 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001055}
1056
1057void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058widget_set_allocation(struct widget *widget,
1059 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061 widget->allocation.x = x;
1062 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001063 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064}
1065
1066void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001067widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070}
1071
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001072void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001073widget_set_resize_handler(struct widget *widget,
1074 widget_resize_handler_t handler)
1075{
1076 widget->resize_handler = handler;
1077}
1078
1079void
1080widget_set_redraw_handler(struct widget *widget,
1081 widget_redraw_handler_t handler)
1082{
1083 widget->redraw_handler = handler;
1084}
1085
1086void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001087widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001088{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001089 widget->enter_handler = handler;
1090}
1091
1092void
1093widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1094{
1095 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001096}
1097
1098void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001099widget_set_motion_handler(struct widget *widget,
1100 widget_motion_handler_t handler)
1101{
1102 widget->motion_handler = handler;
1103}
1104
1105void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001106widget_set_button_handler(struct widget *widget,
1107 widget_button_handler_t handler)
1108{
1109 widget->button_handler = handler;
1110}
1111
1112void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001113widget_schedule_redraw(struct widget *widget)
1114{
1115 window_schedule_redraw(widget->window);
1116}
1117
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001118cairo_surface_t *
1119window_get_surface(struct window *window)
1120{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001121 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001122}
1123
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001124struct wl_surface *
1125window_get_wl_surface(struct window *window)
1126{
1127 return window->surface;
1128}
1129
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001130struct wl_shell_surface *
1131window_get_wl_shell_surface(struct window *window)
1132{
1133 return window->shell_surface;
1134}
1135
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001136static void
1137frame_resize_handler(struct widget *widget,
1138 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001140 struct frame *frame = data;
1141 struct widget *child = frame->child;
1142 struct rectangle allocation;
1143 int decoration_width, decoration_height;
1144
1145 decoration_width = 20 + frame->margin * 2;
1146 decoration_height = 60 + frame->margin * 2;
1147
1148 allocation.x = 10 + frame->margin;
1149 allocation.y = 50 + frame->margin;
1150 allocation.width = width - decoration_width;
1151 allocation.height = height - decoration_height;
1152
1153 widget_set_allocation(child, allocation.x, allocation.y,
1154 allocation.width, allocation.height);
1155
1156 if (child->resize_handler)
1157 child->resize_handler(child,
1158 allocation.width,
1159 allocation.height,
1160 child->user_data);
1161
1162 widget_set_allocation(widget, 0, 0,
1163 child->allocation.width + decoration_width,
1164 child->allocation.height + decoration_height);
1165}
1166
1167static void
1168frame_redraw_handler(struct widget *widget, void *data)
1169{
1170 struct frame *frame = data;
1171 cairo_t *cr;
1172 cairo_text_extents_t extents;
1173 cairo_surface_t *source;
1174 int width, height, shadow_dx = 3, shadow_dy = 3;
1175 struct window *window = widget->window;
1176
1177 width = widget->allocation.width;
1178 height = widget->allocation.height;
1179
1180 cr = cairo_create(window->cairo_surface);
1181
1182 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1183 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1184 cairo_paint(cr);
1185
1186 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1187 tile_mask(cr, window->display->shadow,
1188 shadow_dx, shadow_dy, width, height,
1189 frame->margin + 10 - shadow_dx,
1190 frame->margin + 10 - shadow_dy);
1191
1192 if (window->keyboard_device)
1193 source = window->display->active_frame;
1194 else
1195 source = window->display->inactive_frame;
1196
1197 tile_source(cr, source, 0, 0, width, height,
1198 frame->margin + 10, frame->margin + 50);
1199
1200 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1201 cairo_set_font_size(cr, 14);
1202 cairo_text_extents(cr, window->title, &extents);
1203 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1204 if (window->keyboard_device)
1205 cairo_set_source_rgb(cr, 0, 0, 0);
1206 else
1207 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1208 cairo_show_text(cr, window->title);
1209
1210 cairo_destroy(cr);
1211}
1212
1213static int
1214frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1215{
1216 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001218 const int grip_size = 8;
1219
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001220 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001221 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001222 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001224 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001225 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 hlocation = WINDOW_RESIZING_RIGHT;
1228 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001229 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001230
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001232 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238 vlocation = WINDOW_RESIZING_BOTTOM;
1239 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001241
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 location = vlocation | hlocation;
1243 if (location & WINDOW_EXTERIOR)
1244 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 location = WINDOW_TITLEBAR;
1247 else if (location == WINDOW_INTERIOR)
1248 location = WINDOW_CLIENT_AREA;
1249
1250 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251}
1252
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001253static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001256 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 switch (location) {
1260 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001261 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001263 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001265 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001267 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001271 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001273 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001275 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 case WINDOW_EXTERIOR:
1277 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001279 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001281}
1282
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001283static void
1284frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001285{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286 switch (index) {
1287 case 0: /* close */
1288 if (window->close_handler)
1289 window->close_handler(window->parent,
1290 window->user_data);
1291 else
1292 display_exit(window->display);
1293 break;
1294 case 1: /* fullscreen */
1295 /* we don't have a way to get out of fullscreen for now */
1296 window_set_fullscreen(window, 1);
1297 break;
1298 case 2: /* rotate */
1299 case 3: /* scale */
1300 break;
1301 }
1302}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001304void
1305window_show_frame_menu(struct window *window,
1306 struct input *input, uint32_t time)
1307{
1308 int32_t x, y;
1309
1310 static const char *entries[] = {
1311 "Close", "Fullscreen", "Rotate", "Scale"
1312 };
1313
1314 input_get_position(input, &x, &y);
1315 window_show_menu(window->display, input, time, window,
1316 x - 10, y - 10, frame_menu_func, entries, 4);
1317}
1318
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001319static int
1320frame_enter_handler(struct widget *widget,
1321 struct input *input, uint32_t time,
1322 int32_t x, int32_t y, void *data)
1323{
1324 return frame_get_pointer_image_for_location(data, input);
1325}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001326
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001327static int
1328frame_motion_handler(struct widget *widget,
1329 struct input *input, uint32_t time,
1330 int32_t x, int32_t y, void *data)
1331{
1332 return frame_get_pointer_image_for_location(data, input);
1333}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001334
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001335static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001336frame_button_handler(struct widget *widget,
1337 struct input *input, uint32_t time,
1338 int button, int state, void *data)
1339
1340{
1341 struct frame *frame = data;
1342 struct window *window = widget->window;
1343 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001344 location = frame_get_pointer_location(frame, input->sx, input->sy);
1345
1346 if (window->display->shell && button == BTN_LEFT && state == 1) {
1347 switch (location) {
1348 case WINDOW_TITLEBAR:
1349 if (!window->shell_surface)
1350 break;
1351 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001352 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001353 wl_shell_surface_move(window->shell_surface,
1354 input_get_input_device(input),
1355 time);
1356 break;
1357 case WINDOW_RESIZING_TOP:
1358 case WINDOW_RESIZING_BOTTOM:
1359 case WINDOW_RESIZING_LEFT:
1360 case WINDOW_RESIZING_RIGHT:
1361 case WINDOW_RESIZING_TOP_LEFT:
1362 case WINDOW_RESIZING_TOP_RIGHT:
1363 case WINDOW_RESIZING_BOTTOM_LEFT:
1364 case WINDOW_RESIZING_BOTTOM_RIGHT:
1365 if (!window->shell_surface)
1366 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001367 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001368 wl_shell_surface_resize(window->shell_surface,
1369 input_get_input_device(input),
1370 time, location);
1371 break;
1372 }
1373 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001374 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375 }
1376}
1377
1378struct widget *
1379frame_create(struct window *window, void *data)
1380{
1381 struct frame *frame;
1382
1383 frame = malloc(sizeof *frame);
1384 memset(frame, 0, sizeof *frame);
1385
1386 frame->widget = window_add_widget(window, frame);
1387 frame->child = widget_add_widget(frame->widget, data);
1388 frame->margin = 16;
1389
1390 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1391 widget_set_resize_handler(frame->widget, frame_resize_handler);
1392 widget_set_enter_handler(frame->widget, frame_enter_handler);
1393 widget_set_motion_handler(frame->widget, frame_motion_handler);
1394 widget_set_button_handler(frame->widget, frame_button_handler);
1395
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001396 window->frame = frame;
1397
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001399}
1400
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001401static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001402frame_destroy(struct frame *frame)
1403{
1404 /* frame->child must be destroyed by the application */
1405 widget_destroy(frame->widget);
1406 free(frame);
1407}
1408
1409static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001410input_set_focus_widget(struct input *input, struct widget *focus,
1411 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001412{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001413 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001414 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001415
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001416 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001417 return;
1418
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001419 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001420 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001421 widget = old;
1422 if (input->grab)
1423 widget = input->grab;
1424 if (widget->leave_handler)
1425 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001426 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001427 }
1428
1429 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001430 widget = focus;
1431 if (input->grab)
1432 widget = input->grab;
1433 if (widget->enter_handler)
1434 pointer = widget->enter_handler(focus, input, time,
1435 x, y,
1436 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001437 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001438
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001439 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001440 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441}
1442
1443static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001444input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001445 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001446{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001447 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001449 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001450 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001451
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001452 input->sx = sx;
1453 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001454
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001455 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001456 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001457 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458 }
1459
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001460 if (input->grab)
1461 widget = input->grab;
1462 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001463 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001464 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001465 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001466 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001467 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001468
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001469 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001470}
1471
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001472void
1473input_grab(struct input *input, struct widget *widget, uint32_t button)
1474{
1475 input->grab = widget;
1476 input->grab_button = button;
1477}
1478
1479void
1480input_ungrab(struct input *input, uint32_t time)
1481{
1482 struct widget *widget;
1483
1484 input->grab = NULL;
1485 if (input->pointer_focus) {
1486 widget = widget_find_widget(input->pointer_focus->widget,
1487 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001488 input_set_focus_widget(input, widget,
1489 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001490 }
1491}
1492
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001493static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001494input_handle_button(void *data,
1495 struct wl_input_device *input_device,
1496 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001497{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001498 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001499 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001500
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001501 if (input->focus_widget && input->grab == NULL && state)
1502 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001503
Neil Roberts6b28aad2012-01-23 19:11:18 +00001504 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001506 (*widget->button_handler)(widget,
1507 input, time,
1508 button, state,
1509 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001510
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001511 if (input->grab && input->grab_button == button && !state)
1512 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001513}
1514
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001515static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001516input_handle_key(void *data, struct wl_input_device *input_device,
1517 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001518{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001519 struct input *input = data;
1520 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001521 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001522 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001523
Daniel Stone0d5a5092012-02-16 12:48:00 +00001524 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001525 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001526 return;
1527
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001528 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001529 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001530 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1531 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001532
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001533 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1534
1535 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001536 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001537 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001538 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001539
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001540 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1541 if (state)
1542 window_set_maximized(window,
1543 window->type != TYPE_MAXIMIZED);
1544 } else if (window->key_handler) {
1545 (*window->key_handler)(window, input, time, key,
1546 sym, state, window->user_data);
1547 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001548}
1549
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001550static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001551input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001552{
1553 struct window *window = input->pointer_focus;
1554
1555 if (!window)
1556 return;
1557
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001558 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001559
Pekka Paalanene1207c72011-12-16 12:02:09 +02001560 input->pointer_focus = NULL;
1561 input->current_pointer_image = POINTER_UNSET;
1562}
1563
1564static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001565input_handle_pointer_focus(void *data,
1566 struct wl_input_device *input_device,
1567 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001568 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001569{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001571 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001572 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001573
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001574 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001575 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001576 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001577
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001578 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001579 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001580 window = input->pointer_focus;
1581
Kristian Høgsberg59826582011-01-20 11:56:57 -05001582 input->sx = sx;
1583 input->sy = sy;
1584
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001586 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001587 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001588}
1589
1590static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001591input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001592{
1593 struct window *window = input->keyboard_focus;
1594
1595 if (!window)
1596 return;
1597
1598 window->keyboard_device = NULL;
1599 if (window->keyboard_focus_handler)
1600 (*window->keyboard_focus_handler)(window, NULL,
1601 window->user_data);
1602
1603 input->keyboard_focus = NULL;
1604}
1605
1606static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001607input_handle_keyboard_focus(void *data,
1608 struct wl_input_device *input_device,
1609 uint32_t time,
1610 struct wl_surface *surface,
1611 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001612{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001613 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001614 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001615 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001616 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001617
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001618 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001619
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001620 if (surface)
1621 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001622
1623 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001624 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001625 for (k = keys->data; k < end; k++)
1626 input->modifiers |= d->xkb->map->modmap[*k];
1627
1628 window = input->keyboard_focus;
1629 if (window) {
1630 window->keyboard_device = input;
1631 if (window->keyboard_focus_handler)
1632 (*window->keyboard_focus_handler)(window,
1633 window->keyboard_device,
1634 window->user_data);
1635 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001636}
1637
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001638static void
1639input_handle_touch_down(void *data,
1640 struct wl_input_device *wl_input_device,
1641 uint32_t time, struct wl_surface *surface,
1642 int32_t id, int32_t x, int32_t y)
1643{
1644}
1645
1646static void
1647input_handle_touch_up(void *data,
1648 struct wl_input_device *wl_input_device,
1649 uint32_t time, int32_t id)
1650{
1651}
1652
1653static void
1654input_handle_touch_motion(void *data,
1655 struct wl_input_device *wl_input_device,
1656 uint32_t time, int32_t id, int32_t x, int32_t y)
1657{
1658}
1659
1660static void
1661input_handle_touch_frame(void *data,
1662 struct wl_input_device *wl_input_device)
1663{
1664}
1665
1666static void
1667input_handle_touch_cancel(void *data,
1668 struct wl_input_device *wl_input_device)
1669{
1670}
1671
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001672static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001673 input_handle_motion,
1674 input_handle_button,
1675 input_handle_key,
1676 input_handle_pointer_focus,
1677 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001678 input_handle_touch_down,
1679 input_handle_touch_up,
1680 input_handle_touch_motion,
1681 input_handle_touch_frame,
1682 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001683};
1684
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001685void
1686input_get_position(struct input *input, int32_t *x, int32_t *y)
1687{
1688 *x = input->sx;
1689 *y = input->sy;
1690}
1691
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001692struct wl_input_device *
1693input_get_input_device(struct input *input)
1694{
1695 return input->input_device;
1696}
1697
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001698uint32_t
1699input_get_modifiers(struct input *input)
1700{
1701 return input->modifiers;
1702}
1703
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001704struct widget *
1705input_get_focus_widget(struct input *input)
1706{
1707 return input->focus_widget;
1708}
1709
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001710struct data_offer {
1711 struct wl_data_offer *offer;
1712 struct input *input;
1713 struct wl_array types;
1714 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001715
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001716 struct task io_task;
1717 int fd;
1718 data_func_t func;
1719 int32_t x, y;
1720 void *user_data;
1721};
1722
1723static void
1724data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1725{
1726 struct data_offer *offer = data;
1727 char **p;
1728
1729 p = wl_array_add(&offer->types, sizeof *p);
1730 *p = strdup(type);
1731}
1732
1733static const struct wl_data_offer_listener data_offer_listener = {
1734 data_offer_offer,
1735};
1736
1737static void
1738data_offer_destroy(struct data_offer *offer)
1739{
1740 char **p;
1741
1742 offer->refcount--;
1743 if (offer->refcount == 0) {
1744 wl_data_offer_destroy(offer->offer);
1745 for (p = offer->types.data; *p; p++)
1746 free(*p);
1747 wl_array_release(&offer->types);
1748 free(offer);
1749 }
1750}
1751
1752static void
1753data_device_data_offer(void *data,
1754 struct wl_data_device *data_device, uint32_t id)
1755{
1756 struct data_offer *offer;
1757
1758 offer = malloc(sizeof *offer);
1759
1760 wl_array_init(&offer->types);
1761 offer->refcount = 1;
1762 offer->input = data;
1763
1764 /* FIXME: Generate typesafe wrappers for this */
1765 offer->offer = (struct wl_data_offer *)
1766 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1767 id, &wl_data_offer_interface);
1768
1769 wl_data_offer_add_listener(offer->offer,
1770 &data_offer_listener, offer);
1771}
1772
1773static void
1774data_device_enter(void *data, struct wl_data_device *data_device,
1775 uint32_t time, struct wl_surface *surface,
1776 int32_t x, int32_t y, struct wl_data_offer *offer)
1777{
1778 struct input *input = data;
1779 struct window *window;
1780 char **p;
1781
1782 input->drag_offer = wl_data_offer_get_user_data(offer);
1783 window = wl_surface_get_user_data(surface);
1784 input->pointer_focus = window;
1785
1786 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1787 *p = NULL;
1788
1789 window = input->pointer_focus;
1790 if (window->data_handler)
1791 window->data_handler(window, input, time, x, y,
1792 input->drag_offer->types.data,
1793 window->user_data);
1794}
1795
1796static void
1797data_device_leave(void *data, struct wl_data_device *data_device)
1798{
1799 struct input *input = data;
1800
1801 data_offer_destroy(input->drag_offer);
1802 input->drag_offer = NULL;
1803}
1804
1805static void
1806data_device_motion(void *data, struct wl_data_device *data_device,
1807 uint32_t time, int32_t x, int32_t y)
1808{
1809 struct input *input = data;
1810 struct window *window = input->pointer_focus;
1811
1812 input->sx = x;
1813 input->sy = y;
1814
1815 if (window->data_handler)
1816 window->data_handler(window, input, time, x, y,
1817 input->drag_offer->types.data,
1818 window->user_data);
1819}
1820
1821static void
1822data_device_drop(void *data, struct wl_data_device *data_device)
1823{
1824 struct input *input = data;
1825 struct window *window = input->pointer_focus;
1826
1827 if (window->drop_handler)
1828 window->drop_handler(window, input,
1829 input->sx, input->sy, window->user_data);
1830}
1831
1832static void
1833data_device_selection(void *data,
1834 struct wl_data_device *wl_data_device,
1835 struct wl_data_offer *offer)
1836{
1837 struct input *input = data;
1838 char **p;
1839
1840 if (input->selection_offer)
1841 data_offer_destroy(input->selection_offer);
1842
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001843 if (offer) {
1844 input->selection_offer = wl_data_offer_get_user_data(offer);
1845 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1846 *p = NULL;
1847 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001848}
1849
1850static const struct wl_data_device_listener data_device_listener = {
1851 data_device_data_offer,
1852 data_device_enter,
1853 data_device_leave,
1854 data_device_motion,
1855 data_device_drop,
1856 data_device_selection
1857};
1858
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001859void
1860input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1861{
1862 struct display *display = input->display;
1863 struct wl_buffer *buffer;
1864 cairo_surface_t *surface;
1865
1866 if (pointer == input->current_pointer_image)
1867 return;
1868
1869 input->current_pointer_image = pointer;
1870 surface = display->pointer_surfaces[pointer];
1871
1872 if (!surface)
1873 return;
1874
1875 buffer = display_get_buffer_for_surface(display, surface);
1876 wl_input_device_attach(input->input_device, time, buffer,
1877 pointer_images[pointer].hotspot_x,
1878 pointer_images[pointer].hotspot_y);
1879}
1880
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001881struct wl_data_device *
1882input_get_data_device(struct input *input)
1883{
1884 return input->data_device;
1885}
1886
1887void
1888input_set_selection(struct input *input,
1889 struct wl_data_source *source, uint32_t time)
1890{
1891 wl_data_device_set_selection(input->data_device, source, time);
1892}
1893
1894void
1895input_accept(struct input *input, uint32_t time, const char *type)
1896{
1897 wl_data_offer_accept(input->drag_offer->offer, time, type);
1898}
1899
1900static void
1901offer_io_func(struct task *task, uint32_t events)
1902{
1903 struct data_offer *offer =
1904 container_of(task, struct data_offer, io_task);
1905 unsigned int len;
1906 char buffer[4096];
1907
1908 len = read(offer->fd, buffer, sizeof buffer);
1909 offer->func(buffer, len,
1910 offer->x, offer->y, offer->user_data);
1911
1912 if (len == 0) {
1913 close(offer->fd);
1914 data_offer_destroy(offer);
1915 }
1916}
1917
1918static void
1919data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1920 data_func_t func, void *user_data)
1921{
1922 int p[2];
1923
1924 pipe2(p, O_CLOEXEC);
1925 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1926 close(p[1]);
1927
1928 offer->io_task.run = offer_io_func;
1929 offer->fd = p[0];
1930 offer->func = func;
1931 offer->refcount++;
1932 offer->user_data = user_data;
1933
1934 display_watch_fd(offer->input->display,
1935 offer->fd, EPOLLIN, &offer->io_task);
1936}
1937
1938void
1939input_receive_drag_data(struct input *input, const char *mime_type,
1940 data_func_t func, void *data)
1941{
1942 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1943 input->drag_offer->x = input->sx;
1944 input->drag_offer->y = input->sy;
1945}
1946
1947int
1948input_receive_selection_data(struct input *input, const char *mime_type,
1949 data_func_t func, void *data)
1950{
1951 char **p;
1952
1953 if (input->selection_offer == NULL)
1954 return -1;
1955
1956 for (p = input->selection_offer->types.data; *p; p++)
1957 if (strcmp(mime_type, *p) == 0)
1958 break;
1959
1960 if (*p == NULL)
1961 return -1;
1962
1963 data_offer_receive_data(input->selection_offer,
1964 mime_type, func, data);
1965 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001966}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001967
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001968int
1969input_receive_selection_data_to_fd(struct input *input,
1970 const char *mime_type, int fd)
1971{
1972 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1973
1974 return 0;
1975}
1976
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001977void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001978window_move(struct window *window, struct input *input, uint32_t time)
1979{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001980 if (!window->shell_surface)
1981 return;
1982
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001983 wl_shell_surface_move(window->shell_surface,
1984 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001985}
1986
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001987static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001988idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001989{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001990 struct window *window =
1991 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001992 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001993
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001994 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001995 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001996 widget_set_allocation(widget,
1997 window->pending_allocation.x,
1998 window->pending_allocation.y,
1999 window->pending_allocation.width,
2000 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002001
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002002 if (widget->resize_handler)
2003 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002004 widget->allocation.width,
2005 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002006 widget->user_data);
2007
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002008 if (window->allocation.width != widget->allocation.width ||
2009 window->allocation.height != widget->allocation.height) {
2010 window->allocation = widget->allocation;
2011 window_schedule_redraw(window);
2012 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002013}
2014
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002015void
2016window_schedule_resize(struct window *window, int width, int height)
2017{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002018 window->pending_allocation.x = 0;
2019 window->pending_allocation.y = 0;
2020 window->pending_allocation.width = width;
2021 window->pending_allocation.height = height;
2022
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002023 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002024 window->resize_task.run = idle_resize;
2025 display_defer(window->display, &window->resize_task);
2026 window->resize_scheduled = 1;
2027 }
2028}
2029
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002030void
2031widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2032{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002033 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002034}
2035
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002036static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002037handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002038 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002039 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002040{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002041 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002042
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002043 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002044 return;
2045
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002046 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002047 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002048}
2049
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002050static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002051menu_destroy(struct menu *menu)
2052{
2053 widget_destroy(menu->widget);
2054 window_destroy(menu->window);
2055 free(menu);
2056}
2057
2058static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002059handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2060{
2061 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002062 struct menu *menu = window->widget->user_data;
2063
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002065 * device. Or just use wl_callback. And this really needs to
2066 * be a window vfunc that the menu can set. And we need the
2067 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002069 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002070 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002071 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072}
2073
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002074static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002075 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002076 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002077};
2078
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002079void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002080window_get_allocation(struct window *window,
2081 struct rectangle *allocation)
2082{
2083 *allocation = window->allocation;
2084}
2085
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002086static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002087widget_redraw(struct widget *widget)
2088{
2089 struct widget *child;
2090
2091 if (widget->redraw_handler)
2092 widget->redraw_handler(widget, widget->user_data);
2093 wl_list_for_each(child, &widget->child_list, link)
2094 widget_redraw(child);
2095}
2096
2097static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002098idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002099{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002100 struct window *window =
2101 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002102
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002103 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002104 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002105 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002106 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002107}
2108
2109void
2110window_schedule_redraw(struct window *window)
2111{
2112 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002113 window->redraw_task.run = idle_redraw;
2114 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002115 window->redraw_scheduled = 1;
2116 }
2117}
2118
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002119void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002120window_set_custom(struct window *window)
2121{
2122 window->type = TYPE_CUSTOM;
2123}
2124
2125void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002126window_set_fullscreen(struct window *window, int fullscreen)
2127{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002128 if (!window->display->shell)
2129 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002130
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002131 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002132 return;
2133
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002134 if (fullscreen) {
2135 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002136 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002137 wl_shell_surface_set_fullscreen(window->shell_surface,
2138 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2139 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002140 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002141 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002142 wl_shell_surface_set_toplevel(window->shell_surface);
2143 window_schedule_resize(window,
2144 window->saved_allocation.width,
2145 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002146 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002147}
2148
2149void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002150window_set_maximized(struct window *window, int maximized)
2151{
2152 if (!window->display->shell)
2153 return;
2154
2155 if ((window->type == TYPE_MAXIMIZED) == maximized)
2156 return;
2157
2158 if (window->type == TYPE_TOPLEVEL) {
2159 window->saved_allocation = window->allocation;
2160 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2161 window->type = TYPE_MAXIMIZED;
2162 } else {
2163 wl_shell_surface_set_toplevel(window->shell_surface);
2164 window->type = TYPE_TOPLEVEL;
2165 window_schedule_resize(window,
2166 window->saved_allocation.width,
2167 window->saved_allocation.height);
2168 }
2169}
2170
2171void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002172window_set_user_data(struct window *window, void *data)
2173{
2174 window->user_data = data;
2175}
2176
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002177void *
2178window_get_user_data(struct window *window)
2179{
2180 return window->user_data;
2181}
2182
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002183void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002184window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002185 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002186{
2187 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002188}
2189
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002190void
2191window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002192 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002193{
2194 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002195}
2196
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002197void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198window_set_data_handler(struct window *window, window_data_handler_t handler)
2199{
2200 window->data_handler = handler;
2201}
2202
2203void
2204window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2205{
2206 window->drop_handler = handler;
2207}
2208
2209void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002210window_set_close_handler(struct window *window,
2211 window_close_handler_t handler)
2212{
2213 window->close_handler = handler;
2214}
2215
2216void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002217window_set_transparent(struct window *window, int transparent)
2218{
2219 window->transparent = transparent;
2220}
2221
2222void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002223window_set_title(struct window *window, const char *title)
2224{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002225 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002226 window->title = strdup(title);
2227}
2228
2229const char *
2230window_get_title(struct window *window)
2231{
2232 return window->title;
2233}
2234
2235void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002236display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2237 int32_t x, int32_t y, int32_t width, int32_t height)
2238{
2239 struct wl_buffer *buffer;
2240
2241 buffer = display_get_buffer_for_surface(display, cairo_surface);
2242
2243 wl_buffer_damage(buffer, x, y, width, height);
2244}
2245
2246void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002247window_damage(struct window *window, int32_t x, int32_t y,
2248 int32_t width, int32_t height)
2249{
2250 wl_surface_damage(window->surface, x, y, width, height);
2251}
2252
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002253static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002254window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002255{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002256 struct window *window;
2257
2258 window = malloc(sizeof *window);
2259 if (window == NULL)
2260 return NULL;
2261
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002262 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002263 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002264 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002265 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002266 if (display->shell) {
2267 window->shell_surface =
2268 wl_shell_get_shell_surface(display->shell,
2269 window->surface);
2270 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002271 window->allocation.x = 0;
2272 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002273 window->allocation.width = 0;
2274 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002275 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002276 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002277 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002278
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002279 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002280#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002281 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2282 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002283#else
2284 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2285#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002286 else
2287 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002288
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002289 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002290 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002291
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002292 if (window->shell_surface) {
2293 wl_shell_surface_set_user_data(window->shell_surface, window);
2294 wl_shell_surface_add_listener(window->shell_surface,
2295 &shell_surface_listener, window);
2296 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002297
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002298 return window;
2299}
2300
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002301struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002302window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002303{
2304 struct window *window;
2305
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002306 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002307 if (!window)
2308 return NULL;
2309
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002310 window->type = TYPE_TOPLEVEL;
2311 if (display->shell)
2312 wl_shell_surface_set_toplevel(window->shell_surface);
2313
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002314 return window;
2315}
2316
2317struct window *
2318window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002319 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002320{
2321 struct window *window;
2322
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002323 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002324 if (!window)
2325 return NULL;
2326
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002327 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002328 window->x = x;
2329 window->y = y;
2330
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002331 if (display->shell)
2332 wl_shell_surface_set_transient(window->shell_surface,
2333 window->parent->shell_surface,
2334 window->x, window->y, 0);
2335
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002336 return window;
2337}
2338
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002339static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002340menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002341{
2342 int next;
2343
2344 next = (sy - 8) / 20;
2345 if (menu->current != next) {
2346 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002347 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002348 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002349}
2350
2351static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002352menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002353 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002354 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002355{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002356 struct menu *menu = data;
2357
2358 if (widget == menu->widget)
2359 menu_set_item(data, y);
2360
2361 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002362}
2363
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002364static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002365menu_enter_handler(struct widget *widget,
2366 struct input *input, uint32_t time,
2367 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002368{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002369 struct menu *menu = data;
2370
2371 if (widget == menu->widget)
2372 menu_set_item(data, y);
2373
2374 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002375}
2376
2377static void
2378menu_leave_handler(struct widget *widget, struct input *input, void *data)
2379{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002380 struct menu *menu = data;
2381
2382 if (widget == menu->widget)
2383 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002384}
2385
2386static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002387menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002388 struct input *input, uint32_t time,
2389 int button, int state, void *data)
2390
2391{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002392 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002393
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002394 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002395 /* Either relase after press-drag-release or
2396 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002397 menu->func(menu->window->parent,
2398 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002399 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002400 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002401 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002402}
2403
2404static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002405menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002406{
2407 cairo_t *cr;
2408 const int32_t r = 3, margin = 3;
2409 struct menu *menu = data;
2410 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002411 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002412
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002413 cr = cairo_create(window->cairo_surface);
2414 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2415 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2416 cairo_paint(cr);
2417
2418 width = window->allocation.width;
2419 height = window->allocation.height;
2420 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002421
2422 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2424 cairo_fill(cr);
2425
2426 for (i = 0; i < menu->count; i++) {
2427 if (i == menu->current) {
2428 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2429 cairo_rectangle(cr, margin, i * 20 + margin,
2430 width - 2 * margin, 20);
2431 cairo_fill(cr);
2432 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2433 cairo_move_to(cr, 10, i * 20 + 16);
2434 cairo_show_text(cr, menu->entries[i]);
2435 } else {
2436 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2437 cairo_move_to(cr, 10, i * 20 + 16);
2438 cairo_show_text(cr, menu->entries[i]);
2439 }
2440 }
2441
2442 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002443}
2444
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002445void
2446window_show_menu(struct display *display,
2447 struct input *input, uint32_t time, struct window *parent,
2448 int32_t x, int32_t y,
2449 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002450{
2451 struct window *window;
2452 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002453 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002454
2455 menu = malloc(sizeof *menu);
2456 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002457 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002459 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002460 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002461 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462
2463 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002464 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002465 menu->entries = entries;
2466 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002467 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002468 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002469 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002470 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002471 window->type = TYPE_MENU;
2472 window->x = x;
2473 window->y = y;
2474
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002475 wl_shell_surface_set_popup(window->shell_surface,
2476 input->input_device, time,
2477 window->parent->shell_surface,
2478 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002479
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002480 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002481 widget_set_enter_handler(menu->widget, menu_enter_handler);
2482 widget_set_leave_handler(menu->widget, menu_leave_handler);
2483 widget_set_motion_handler(menu->widget, menu_motion_handler);
2484 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002485
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002486 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002487 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002488}
2489
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002490void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002491window_set_buffer_type(struct window *window, enum window_buffer_type type)
2492{
2493 window->buffer_type = type;
2494}
2495
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002496
2497static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002498display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002499 struct wl_output *wl_output,
2500 int x, int y,
2501 int physical_width,
2502 int physical_height,
2503 int subpixel,
2504 const char *make,
2505 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002506{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002507 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002508
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002509 output->allocation.x = x;
2510 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002511}
2512
2513static void
2514display_handle_mode(void *data,
2515 struct wl_output *wl_output,
2516 uint32_t flags,
2517 int width,
2518 int height,
2519 int refresh)
2520{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002521 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002522 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002523
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002524 if (flags & WL_OUTPUT_MODE_CURRENT) {
2525 output->allocation.width = width;
2526 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002527 if (display->output_configure_handler)
2528 (*display->output_configure_handler)(
2529 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002530 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002531}
2532
2533static const struct wl_output_listener output_listener = {
2534 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002535 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002536};
2537
2538static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002539display_add_output(struct display *d, uint32_t id)
2540{
2541 struct output *output;
2542
2543 output = malloc(sizeof *output);
2544 if (output == NULL)
2545 return;
2546
2547 memset(output, 0, sizeof *output);
2548 output->display = d;
2549 output->output =
2550 wl_display_bind(d->display, id, &wl_output_interface);
2551 wl_list_insert(d->output_list.prev, &output->link);
2552
2553 wl_output_add_listener(output->output, &output_listener, output);
2554}
2555
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002556static void
2557output_destroy(struct output *output)
2558{
2559 if (output->destroy_handler)
2560 (*output->destroy_handler)(output, output->user_data);
2561
2562 wl_output_destroy(output->output);
2563 wl_list_remove(&output->link);
2564 free(output);
2565}
2566
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002567void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002568display_set_output_configure_handler(struct display *display,
2569 display_output_handler_t handler)
2570{
2571 struct output *output;
2572
2573 display->output_configure_handler = handler;
2574 if (!handler)
2575 return;
2576
2577 wl_list_for_each(output, &display->output_list, link)
2578 (*display->output_configure_handler)(output,
2579 display->user_data);
2580}
2581
2582void
2583output_set_user_data(struct output *output, void *data)
2584{
2585 output->user_data = data;
2586}
2587
2588void *
2589output_get_user_data(struct output *output)
2590{
2591 return output->user_data;
2592}
2593
2594void
2595output_set_destroy_handler(struct output *output,
2596 display_output_handler_t handler)
2597{
2598 output->destroy_handler = handler;
2599 /* FIXME: implement this, once we have way to remove outputs */
2600}
2601
2602void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002603output_get_allocation(struct output *output, struct rectangle *allocation)
2604{
2605 *allocation = output->allocation;
2606}
2607
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002608struct wl_output *
2609output_get_wl_output(struct output *output)
2610{
2611 return output->output;
2612}
2613
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002614static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002615display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002616{
2617 struct input *input;
2618
2619 input = malloc(sizeof *input);
2620 if (input == NULL)
2621 return;
2622
2623 memset(input, 0, sizeof *input);
2624 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002625 input->input_device =
2626 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002627 input->pointer_focus = NULL;
2628 input->keyboard_focus = NULL;
2629 wl_list_insert(d->input_list.prev, &input->link);
2630
2631 wl_input_device_add_listener(input->input_device,
2632 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002633 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002634
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002635 input->data_device =
2636 wl_data_device_manager_get_data_device(d->data_device_manager,
2637 input->input_device);
2638 wl_data_device_add_listener(input->data_device,
2639 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002640}
2641
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002642static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002643input_destroy(struct input *input)
2644{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002645 input_remove_keyboard_focus(input);
2646 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002647
2648 if (input->drag_offer)
2649 data_offer_destroy(input->drag_offer);
2650
2651 if (input->selection_offer)
2652 data_offer_destroy(input->selection_offer);
2653
2654 wl_data_device_destroy(input->data_device);
2655 wl_list_remove(&input->link);
2656 wl_input_device_destroy(input->input_device);
2657 free(input);
2658}
2659
2660static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002661display_handle_global(struct wl_display *display, uint32_t id,
2662 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002663{
2664 struct display *d = data;
2665
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002666 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002667 d->compositor =
2668 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002669 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002670 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002671 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002672 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002673 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002674 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002675 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002676 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002677 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2678 d->data_device_manager =
2679 wl_display_bind(display, id,
2680 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002681 }
2682}
2683
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002684static void
2685display_render_frame(struct display *d)
2686{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002687 int radius = 8;
2688 cairo_t *cr;
2689
2690 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2691 cr = cairo_create(d->shadow);
2692 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2693 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2694 rounded_rect(cr, 16, 16, 112, 112, radius);
2695 cairo_fill(cr);
2696 cairo_destroy(cr);
2697 blur_surface(d->shadow, 64);
2698
2699 d->active_frame =
2700 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2701 cr = cairo_create(d->active_frame);
2702 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2703 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2704 rounded_rect(cr, 16, 16, 112, 112, radius);
2705 cairo_fill(cr);
2706 cairo_destroy(cr);
2707
2708 d->inactive_frame =
2709 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2710 cr = cairo_create(d->inactive_frame);
2711 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2712 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2713 rounded_rect(cr, 16, 16, 112, 112, radius);
2714 cairo_fill(cr);
2715 cairo_destroy(cr);
2716}
2717
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002718static void
2719init_xkb(struct display *d)
2720{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002721 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002722
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002723 names.rules = "evdev";
2724 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002725 names.layout = option_xkb_layout;
2726 names.variant = option_xkb_variant;
2727 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002728
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002729 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002730 if (!d->xkb) {
2731 fprintf(stderr, "Failed to compile keymap\n");
2732 exit(1);
2733 }
2734}
2735
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002736static void
2737fini_xkb(struct display *display)
2738{
2739 xkb_free_keymap(display->xkb);
2740}
2741
Yuval Fledel45568f62010-12-06 09:18:12 -05002742static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002743init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002744{
2745 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002746 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002747
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002748 static const EGLint argb_cfg_attribs[] = {
2749 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002750 EGL_RED_SIZE, 1,
2751 EGL_GREEN_SIZE, 1,
2752 EGL_BLUE_SIZE, 1,
2753 EGL_ALPHA_SIZE, 1,
2754 EGL_DEPTH_SIZE, 1,
2755 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2756 EGL_NONE
2757 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002758
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002759 static const EGLint rgb_cfg_attribs[] = {
2760 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2761 EGL_RED_SIZE, 1,
2762 EGL_GREEN_SIZE, 1,
2763 EGL_BLUE_SIZE, 1,
2764 EGL_ALPHA_SIZE, 0,
2765 EGL_DEPTH_SIZE, 1,
2766 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2767 EGL_NONE
2768 };
2769
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002770#ifdef USE_CAIRO_GLESV2
2771 static const EGLint context_attribs[] = {
2772 EGL_CONTEXT_CLIENT_VERSION, 2,
2773 EGL_NONE
2774 };
2775 EGLint api = EGL_OPENGL_ES_API;
2776#else
2777 EGLint *context_attribs = NULL;
2778 EGLint api = EGL_OPENGL_API;
2779#endif
2780
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002781 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002782 if (!eglInitialize(d->dpy, &major, &minor)) {
2783 fprintf(stderr, "failed to initialize display\n");
2784 return -1;
2785 }
2786
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002787 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002788 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2789 return -1;
2790 }
2791
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002792 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2793 &d->argb_config, 1, &n) || n != 1) {
2794 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002795 return -1;
2796 }
2797
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002798 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2799 &d->rgb_config, 1, &n) || n != 1) {
2800 fprintf(stderr, "failed to choose rgb config\n");
2801 return -1;
2802 }
2803
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002804 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2805 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002806 if (d->rgb_ctx == NULL) {
2807 fprintf(stderr, "failed to create context\n");
2808 return -1;
2809 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002810 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002811 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002812 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002813 fprintf(stderr, "failed to create context\n");
2814 return -1;
2815 }
2816
Benjamin Franzke0c991632011-09-27 21:57:31 +02002817 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002818 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002819 return -1;
2820 }
2821
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002823 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2824 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002825 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002826 return -1;
2827 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002828 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2829 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2830 fprintf(stderr, "failed to get cairo egl argb device\n");
2831 return -1;
2832 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002833#endif
2834
2835 return 0;
2836}
2837
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002838static void
2839fini_egl(struct display *display)
2840{
2841#ifdef HAVE_CAIRO_EGL
2842 cairo_device_destroy(display->argb_device);
2843 cairo_device_destroy(display->rgb_device);
2844#endif
2845
2846 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2847 EGL_NO_CONTEXT);
2848
2849 eglTerminate(display->dpy);
2850 eglReleaseThread();
2851}
2852
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002853static int
2854event_mask_update(uint32_t mask, void *data)
2855{
2856 struct display *d = data;
2857
2858 d->mask = mask;
2859
2860 return 0;
2861}
2862
2863static void
2864handle_display_data(struct task *task, uint32_t events)
2865{
2866 struct display *display =
2867 container_of(task, struct display, display_task);
2868
2869 wl_display_iterate(display->display, display->mask);
2870}
2871
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002872struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002873display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002874{
2875 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002876 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002877 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002878 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002879
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002880 g_type_init();
2881
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002882 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002883 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002884 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002885
2886 xkb_option_group = g_option_group_new("xkb",
2887 "Keyboard options",
2888 "Show all XKB options",
2889 NULL, NULL);
2890 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2891 g_option_context_add_group (context, xkb_option_group);
2892
2893 if (!g_option_context_parse(context, argc, argv, &error)) {
2894 fprintf(stderr, "option parsing failed: %s\n", error->message);
2895 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002896 }
2897
Tim Wiederhake748f6722011-01-23 23:25:25 +01002898 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002899
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002900 d = malloc(sizeof *d);
2901 if (d == NULL)
2902 return NULL;
2903
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002904 memset(d, 0, sizeof *d);
2905
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002906 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002907 if (d->display == NULL) {
2908 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002909 return NULL;
2910 }
2911
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002912 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2913 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2914 d->display_task.run = handle_display_data;
2915 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2916
2917 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002918 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002919 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002920
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002921 /* Set up listener so we'll catch all events. */
2922 wl_display_add_global_listener(d->display,
2923 display_handle_global, d);
2924
2925 /* Process connection events. */
2926 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002927 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002928 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002929
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002930 d->image_target_texture_2d =
2931 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2932 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2933 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2934
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002935 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002936
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002937 display_render_frame(d);
2938
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002939 wl_list_init(&d->window_list);
2940
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002941 init_xkb(d);
2942
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002943 return d;
2944}
2945
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002946static void
2947display_destroy_outputs(struct display *display)
2948{
2949 struct output *tmp;
2950 struct output *output;
2951
2952 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2953 output_destroy(output);
2954}
2955
Pekka Paalanene1207c72011-12-16 12:02:09 +02002956static void
2957display_destroy_inputs(struct display *display)
2958{
2959 struct input *tmp;
2960 struct input *input;
2961
2962 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2963 input_destroy(input);
2964}
2965
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002966void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002967display_destroy(struct display *display)
2968{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002969 if (!wl_list_empty(&display->window_list))
2970 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2971
2972 if (!wl_list_empty(&display->deferred_list))
2973 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2974
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002975 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002976 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002977
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002978 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002979
2980 cairo_surface_destroy(display->active_frame);
2981 cairo_surface_destroy(display->inactive_frame);
2982 cairo_surface_destroy(display->shadow);
2983 destroy_pointer_surfaces(display);
2984
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002985 fini_egl(display);
2986
Pekka Paalanenc2052982011-12-16 11:41:32 +02002987 if (display->shell)
2988 wl_shell_destroy(display->shell);
2989
2990 if (display->shm)
2991 wl_shm_destroy(display->shm);
2992
2993 if (display->data_device_manager)
2994 wl_data_device_manager_destroy(display->data_device_manager);
2995
2996 wl_compositor_destroy(display->compositor);
2997
2998 close(display->epoll_fd);
2999
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003000 wl_display_flush(display->display);
3001 wl_display_destroy(display->display);
3002 free(display);
3003}
3004
3005void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003006display_set_user_data(struct display *display, void *data)
3007{
3008 display->user_data = data;
3009}
3010
3011void *
3012display_get_user_data(struct display *display)
3013{
3014 return display->user_data;
3015}
3016
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003017struct wl_display *
3018display_get_display(struct display *display)
3019{
3020 return display->display;
3021}
3022
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003023struct output *
3024display_get_output(struct display *display)
3025{
3026 return container_of(display->output_list.next, struct output, link);
3027}
3028
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003029struct wl_compositor *
3030display_get_compositor(struct display *display)
3031{
3032 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003033}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003034
3035EGLDisplay
3036display_get_egl_display(struct display *d)
3037{
3038 return d->dpy;
3039}
3040
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003041struct wl_data_source *
3042display_create_data_source(struct display *display)
3043{
3044 return wl_data_device_manager_create_data_source(display->data_device_manager);
3045}
3046
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003048display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003049{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003050 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003051}
3052
Benjamin Franzke0c991632011-09-27 21:57:31 +02003053EGLConfig
3054display_get_argb_egl_config(struct display *d)
3055{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003056 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003057}
3058
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003059struct wl_shell *
3060display_get_shell(struct display *display)
3061{
3062 return display->shell;
3063}
3064
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003065int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003066display_acquire_window_surface(struct display *display,
3067 struct window *window,
3068 EGLContext ctx)
3069{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003070#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003071 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003072 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003073
3074 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003075 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003076 device = cairo_surface_get_device(window->cairo_surface);
3077 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003078 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003079
Benjamin Franzke0c991632011-09-27 21:57:31 +02003080 if (!ctx) {
3081 if (device == display->rgb_device)
3082 ctx = display->rgb_ctx;
3083 else if (device == display->argb_device)
3084 ctx = display->argb_ctx;
3085 else
3086 assert(0);
3087 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003088
3089 data = cairo_surface_get_user_data(window->cairo_surface,
3090 &surface_data_key);
3091
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003092 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003093 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003094 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3095 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003096
3097 return 0;
3098#else
3099 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003100#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003101}
3102
3103void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003104display_release_window_surface(struct display *display,
3105 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003106{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003107#ifdef HAVE_CAIRO_EGL
3108 cairo_device_t *device;
3109
3110 device = cairo_surface_get_device(window->cairo_surface);
3111 if (!device)
3112 return;
3113
3114 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003115 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003116 cairo_device_release(device);
3117#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003118}
3119
3120void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003121display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003122{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003123 wl_list_insert(&display->deferred_list, &task->link);
3124}
3125
3126void
3127display_watch_fd(struct display *display,
3128 int fd, uint32_t events, struct task *task)
3129{
3130 struct epoll_event ep;
3131
3132 ep.events = events;
3133 ep.data.ptr = task;
3134 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3135}
3136
3137void
3138display_run(struct display *display)
3139{
3140 struct task *task;
3141 struct epoll_event ep[16];
3142 int i, count;
3143
Pekka Paalanen826d7952011-12-15 10:14:07 +02003144 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003145 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003146 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003147
3148 while (!wl_list_empty(&display->deferred_list)) {
3149 task = container_of(display->deferred_list.next,
3150 struct task, link);
3151 wl_list_remove(&task->link);
3152 task->run(task, 0);
3153 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003154
3155 if (!display->running)
3156 break;
3157
3158 wl_display_flush(display->display);
3159
3160 count = epoll_wait(display->epoll_fd,
3161 ep, ARRAY_LENGTH(ep), -1);
3162 for (i = 0; i < count; i++) {
3163 task = ep[i].data.ptr;
3164 task->run(task, ep[i].events);
3165 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003166 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003167}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003168
3169void
3170display_exit(struct display *display)
3171{
3172 display->running = 0;
3173}