blob: 9058e4e40a73901ddaceff6df91eef377e639a59 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400126 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500127 int resize_scheduled;
128 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500134 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500136 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500137 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400138 window_data_handler_t data_handler;
139 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500140 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200142 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500143 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400146 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147};
148
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500149struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500150 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500151 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152 struct wl_list link;
153 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500154 widget_resize_handler_t resize_handler;
155 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500156 widget_enter_handler_t enter_handler;
157 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500158 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500159 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400160 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500161 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400162};
163
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164struct input {
165 struct display *display;
166 struct wl_input_device *input_device;
167 struct window *pointer_focus;
168 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400169 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400170 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200171 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400173
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500174 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500175 struct widget *grab;
176 uint32_t grab_button;
177
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400178 struct wl_data_device *data_device;
179 struct data_offer *drag_offer;
180 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400181};
182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183struct output {
184 struct display *display;
185 struct wl_output *output;
186 struct rectangle allocation;
187 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200188
189 display_output_handler_t destroy_handler;
190 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500191};
192
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500193struct frame {
194 struct widget *widget;
195 struct widget *child;
196 int margin;
197};
198
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500199struct menu {
200 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500201 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500202 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500203 const char **entries;
204 uint32_t time;
205 int current;
206 int count;
207 menu_func_t func;
208};
209
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400210enum {
211 POINTER_DEFAULT = 100,
212 POINTER_UNSET
213};
214
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500215enum window_location {
216 WINDOW_INTERIOR = 0,
217 WINDOW_RESIZING_TOP = 1,
218 WINDOW_RESIZING_BOTTOM = 2,
219 WINDOW_RESIZING_LEFT = 4,
220 WINDOW_RESIZING_TOP_LEFT = 5,
221 WINDOW_RESIZING_BOTTOM_LEFT = 6,
222 WINDOW_RESIZING_RIGHT = 8,
223 WINDOW_RESIZING_TOP_RIGHT = 9,
224 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
225 WINDOW_RESIZING_MASK = 15,
226 WINDOW_EXTERIOR = 16,
227 WINDOW_TITLEBAR = 17,
228 WINDOW_CLIENT_AREA = 18,
229};
230
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400231const char *option_xkb_layout = "us";
232const char *option_xkb_variant = "";
233const char *option_xkb_options = "";
234
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400235static const struct weston_option xkb_options[] = {
236 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
237 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
238 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400239};
240
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400241static const cairo_user_data_key_t surface_data_key;
242struct surface_data {
243 struct wl_buffer *buffer;
244};
245
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500246#define MULT(_d,c,a,t) \
247 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
248
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500249#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100251struct egl_window_surface_data {
252 struct display *display;
253 struct wl_surface *surface;
254 struct wl_egl_window *window;
255 EGLSurface surf;
256};
257
258static void
259egl_window_surface_data_destroy(void *p)
260{
261 struct egl_window_surface_data *data = p;
262 struct display *d = data->display;
263
264 eglDestroySurface(d->dpy, data->surf);
265 wl_egl_window_destroy(data->window);
266 data->surface = NULL;
267
268 free(p);
269}
270
271static cairo_surface_t *
272display_create_egl_window_surface(struct display *display,
273 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400274 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275 struct rectangle *rectangle)
276{
277 cairo_surface_t *cairo_surface;
278 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400279 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200280 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100281
282 data = malloc(sizeof *data);
283 if (data == NULL)
284 return NULL;
285
286 data->display = display;
287 data->surface = surface;
288
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500289 config = display->argb_config;
290 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400291
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400292 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500297 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 data->surf,
301 rectangle->width,
302 rectangle->height);
303
304 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
305 data, egl_window_surface_data_destroy);
306
307 return cairo_surface;
308}
309
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200312 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 EGLImageKHR image;
314 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317};
318
319static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324
Benjamin Franzke0c991632011-09-27 21:57:31 +0200325 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800328
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500329 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500331 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500332 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333}
334
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500335EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336display_get_image_for_egl_image_surface(struct display *display,
337 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340
341 data = cairo_surface_get_user_data (surface, &surface_data_key);
342
343 return data->image;
344}
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400348 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 EGLDisplay dpy = display->dpy;
353 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355
356 data = malloc(sizeof *data);
357 if (data == NULL)
358 return NULL;
359
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800360 data->display = display;
361
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400362 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500364 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000365 free(data);
366 return NULL;
367 }
368
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500369 data->device = display->argb_device;
370 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500372 data->image = display->create_image(dpy, NULL,
373 EGL_NATIVE_PIXMAP_KHR,
374 (EGLClientBuffer) data->pixmap,
375 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500376 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500377 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500378 free(data);
379 return NULL;
380 }
381
382 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400383 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384
Benjamin Franzke0c991632011-09-27 21:57:31 +0200385 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400386 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400387 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500388 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200389 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 surface = cairo_gl_surface_create_for_texture(data->device,
392 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 data->texture,
394 rectangle->width,
395 rectangle->height);
396
397 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500398 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399
400 return surface;
401}
402
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500403static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500404display_create_egl_image_surface_from_file(struct display *display,
405 const char *filename,
406 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407{
408 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400409 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400411
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400412 image = load_image(filename);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200414 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000415 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400416 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000417 return NULL;
418 }
419
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800420 data = cairo_surface_get_user_data(surface, &surface_data_key);
421
Benjamin Franzke0c991632011-09-27 21:57:31 +0200422 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800423 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800424 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400425 GL_RGBA, GL_UNSIGNED_BYTE,
426 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200427 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400429 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return surface;
432}
433
434#endif
435
436struct wl_buffer *
437display_get_buffer_for_surface(struct display *display,
438 cairo_surface_t *surface)
439{
440 struct surface_data *data;
441
442 data = cairo_surface_get_user_data (surface, &surface_data_key);
443
444 return data->buffer;
445}
446
447struct shm_surface_data {
448 struct surface_data data;
449 void *map;
450 size_t length;
451};
452
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500453static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454shm_surface_data_destroy(void *p)
455{
456 struct shm_surface_data *data = p;
457
458 wl_buffer_destroy(data->data.buffer);
459 munmap(data->map, data->length);
460}
461
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500462static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400463display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400464 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465{
466 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400467 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400468 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800469 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400470 char filename[] = "/tmp/wayland-shm-XXXXXX";
471
472 data = malloc(sizeof *data);
473 if (data == NULL)
474 return NULL;
475
476 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
477 rectangle->width);
478 data->length = stride * rectangle->height;
479 fd = mkstemp(filename);
480 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000481 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400482 return NULL;
483 }
484 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000485 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400486 close(fd);
487 return NULL;
488 }
489
490 data->map = mmap(NULL, data->length,
491 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
492 unlink(filename);
493
494 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000495 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496 close(fd);
497 return NULL;
498 }
499
500 surface = cairo_image_surface_create_for_data (data->map,
501 CAIRO_FORMAT_ARGB32,
502 rectangle->width,
503 rectangle->height,
504 stride);
505
506 cairo_surface_set_user_data (surface, &surface_data_key,
507 data, shm_surface_data_destroy);
508
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400509 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500510 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400511 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500512 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400513
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400514 data->data.buffer = wl_shm_create_buffer(display->shm,
515 fd,
516 rectangle->width,
517 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400518 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519
520 close(fd);
521
522 return surface;
523}
524
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500525static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526display_create_shm_surface_from_file(struct display *display,
527 const char *filename,
528 struct rectangle *rect)
529{
530 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400531 pixman_image_t *image;
532 void *dest;
533 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400535 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400537 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000538 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400539 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000540 return NULL;
541 }
542
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400543 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
544 dest = cairo_image_surface_get_data(surface);
545 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400547 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
549 return surface;
550}
551
nobled7b87cb02011-02-01 18:51:47 +0000552static int
553check_size(struct rectangle *rect)
554{
555 if (rect->width && rect->height)
556 return 0;
557
558 fprintf(stderr, "tried to create surface of "
559 "width: %d, height: %d\n", rect->width, rect->height);
560 return -1;
561}
562
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563cairo_surface_t *
564display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100565 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400566 struct rectangle *rectangle,
567 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568{
nobled7b87cb02011-02-01 18:51:47 +0000569 if (check_size(rectangle) < 0)
570 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500571#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500572 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100573 if (surface)
574 return display_create_egl_window_surface(display,
575 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400576 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100577 rectangle);
578 else
579 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400580 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100581 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500582 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400584 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585}
586
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500587static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588display_create_surface_from_file(struct display *display,
589 const char *filename,
590 struct rectangle *rectangle)
591{
nobled7b87cb02011-02-01 18:51:47 +0000592 if (check_size(rectangle) < 0)
593 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500594#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500595 if (display->dpy) {
596 return display_create_egl_image_surface_from_file(display,
597 filename,
598 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500599 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500601 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400602}
Yuval Fledel45568f62010-12-06 09:18:12 -0500603 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400604 const char *filename;
605 int hotspot_x, hotspot_y;
606} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500607 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
608 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
609 { DATADIR "/weston/bottom_side.png", 16, 20 },
610 { DATADIR "/weston/grabbing.png", 20, 17 },
611 { DATADIR "/weston/left_ptr.png", 10, 5 },
612 { DATADIR "/weston/left_side.png", 10, 20 },
613 { DATADIR "/weston/right_side.png", 30, 19 },
614 { DATADIR "/weston/top_left_corner.png", 8, 8 },
615 { DATADIR "/weston/top_right_corner.png", 26, 8 },
616 { DATADIR "/weston/top_side.png", 18, 8 },
617 { DATADIR "/weston/xterm.png", 15, 15 },
618 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400619};
620
621static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400622create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400623{
624 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400626 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400627
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400628 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400629 display->pointer_surfaces =
630 malloc(count * sizeof *display->pointer_surfaces);
631 rect.width = width;
632 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400633 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400634 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635 display_create_surface_from_file(display,
636 pointer_images[i].filename,
637 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100638 if (!display->pointer_surfaces[i]) {
639 fprintf(stderr, "Error loading pointer image: %s\n",
640 pointer_images[i].filename);
641 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400642 }
643
644}
645
Pekka Paalanen325bb602011-12-19 10:31:45 +0200646static void
647destroy_pointer_surfaces(struct display *display)
648{
649 int i, count;
650
651 count = ARRAY_LENGTH(pointer_images);
652 for (i = 0; i < count; ++i) {
653 if (display->pointer_surfaces[i])
654 cairo_surface_destroy(display->pointer_surfaces[i]);
655 }
656 free(display->pointer_surfaces);
657}
658
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400659cairo_surface_t *
660display_get_pointer_surface(struct display *display, int pointer,
661 int *width, int *height,
662 int *hotspot_x, int *hotspot_y)
663{
664 cairo_surface_t *surface;
665
666 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500667#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400668 *width = cairo_gl_surface_get_width(surface);
669 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000670#else
671 *width = cairo_image_surface_get_width(surface);
672 *height = cairo_image_surface_get_height(surface);
673#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400674 *hotspot_x = pointer_images[pointer].hotspot_x;
675 *hotspot_y = pointer_images[pointer].hotspot_y;
676
677 return cairo_surface_reference(surface);
678}
679
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400680static void
681window_attach_surface(struct window *window);
682
683static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400684free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400685{
686 struct window *window = data;
687
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400688 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400689 cairo_surface_destroy(window->pending_surface);
690 window->pending_surface = NULL;
691 if (window->cairo_surface)
692 window_attach_surface(window);
693}
694
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400695static const struct wl_callback_listener free_surface_listener = {
696 free_surface
697};
698
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500699static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200700window_get_resize_dx_dy(struct window *window, int *x, int *y)
701{
702 if (window->resize_edges & WINDOW_RESIZING_LEFT)
703 *x = window->server_allocation.width - window->allocation.width;
704 else
705 *x = 0;
706
707 if (window->resize_edges & WINDOW_RESIZING_TOP)
708 *y = window->server_allocation.height -
709 window->allocation.height;
710 else
711 *y = 0;
712
713 window->resize_edges = 0;
714}
715
716static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500717window_attach_surface(struct window *window)
718{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400719 struct display *display = window->display;
720 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400721 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000722#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500725 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726
727 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000728#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
730 data = cairo_surface_get_user_data(window->cairo_surface,
731 &surface_data_key);
732
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 cairo_gl_surface_swapbuffers(window->cairo_surface);
734 wl_egl_window_get_attached_size(data->window,
735 &window->server_allocation.width,
736 &window->server_allocation.height);
737 break;
738 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000739#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 case WINDOW_BUFFER_TYPE_SHM:
741 if (window->pending_surface != NULL)
742 return;
743
744 window->pending_surface = window->cairo_surface;
745 window->cairo_surface = NULL;
746
747 buffer =
748 display_get_buffer_for_surface(display,
749 window->pending_surface);
750
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200751 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752 wl_surface_attach(window->surface, buffer, x, y);
753 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754 cb = wl_display_sync(display->display);
755 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100756 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000757 default:
758 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500760
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500761 if (window->input_region) {
762 wl_surface_set_input_region(window->surface,
763 window->input_region);
764 wl_region_destroy(window->input_region);
765 window->input_region = NULL;
766 }
767
768 if (window->opaque_region) {
769 wl_surface_set_opaque_region(window->surface,
770 window->opaque_region);
771 wl_region_destroy(window->opaque_region);
772 window->opaque_region = NULL;
773 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500774
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500775 wl_surface_damage(window->surface, 0, 0,
776 window->allocation.width,
777 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500778}
779
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500780void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400781window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500782{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100783 if (window->cairo_surface) {
784 switch (window->buffer_type) {
785 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
786 case WINDOW_BUFFER_TYPE_SHM:
787 display_surface_damage(window->display,
788 window->cairo_surface,
789 0, 0,
790 window->allocation.width,
791 window->allocation.height);
792 break;
793 default:
794 break;
795 }
796 window_attach_surface(window);
797 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500798}
799
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400800void
801window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400802{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500803 cairo_surface_reference(surface);
804
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400805 if (window->cairo_surface != NULL)
806 cairo_surface_destroy(window->cairo_surface);
807
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500808 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400809}
810
Benjamin Franzke22d54812011-07-16 19:50:32 +0000811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812static void
813window_resize_cairo_window_surface(struct window *window)
814{
815 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200816 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817
818 data = cairo_surface_get_user_data(window->cairo_surface,
819 &surface_data_key);
820
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200821 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 wl_egl_window_resize(data->window,
823 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200824 window->allocation.height,
825 x,y);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_set_size(window->cairo_surface,
828 window->allocation.width,
829 window->allocation.height);
830}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000831#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400833struct display *
834window_get_display(struct window *window)
835{
836 return window->display;
837}
838
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400839void
840window_create_surface(struct window *window)
841{
842 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400843 uint32_t flags = 0;
844
845 if (!window->transparent)
846 flags = SURFACE_OPAQUE;
847
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400848 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500849#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
851 if (window->cairo_surface) {
852 window_resize_cairo_window_surface(window);
853 return;
854 }
855 surface = display_create_surface(window->display,
856 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400857 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500859 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400860 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400862 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863 break;
864#endif
865 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400866 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400867 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400868 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800869 default:
870 surface = NULL;
871 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400872 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873
874 window_set_surface(window, surface);
875 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400876}
877
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200878static void frame_destroy(struct frame *frame);
879
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400880void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500881window_destroy(struct window *window)
882{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200883 struct display *display = window->display;
884 struct input *input;
885
886 if (window->redraw_scheduled)
887 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500888 if (window->resize_scheduled)
889 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200890
891 wl_list_for_each(input, &display->input_list, link) {
892 if (input->pointer_focus == window)
893 input->pointer_focus = NULL;
894 if (input->keyboard_focus == window)
895 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500896 if (input->focus_widget &&
897 input->focus_widget->window == window)
898 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200899 }
900
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500901 if (window->input_region)
902 wl_region_destroy(window->input_region);
903 if (window->opaque_region)
904 wl_region_destroy(window->opaque_region);
905
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200906 if (window->frame)
907 frame_destroy(window->frame);
908
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200909 if (window->shell_surface)
910 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500911 wl_surface_destroy(window->surface);
912 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200913
914 if (window->cairo_surface != NULL)
915 cairo_surface_destroy(window->cairo_surface);
916 if (window->pending_surface != NULL)
917 cairo_surface_destroy(window->pending_surface);
918
919 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500920 free(window);
921}
922
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500923static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400925{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500926 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400927
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500928 wl_list_for_each(child, &widget->child_list, link) {
929 target = widget_find_widget(child, x, y);
930 if (target)
931 return target;
932 }
933
934 if (widget->allocation.x <= x &&
935 x < widget->allocation.x + widget->allocation.width &&
936 widget->allocation.y <= y &&
937 y < widget->allocation.y + widget->allocation.height) {
938 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939 }
940
941 return NULL;
942}
943
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944static struct widget *
945widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500947 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500949 widget = malloc(sizeof *widget);
950 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500951 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500952 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500953 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500954 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500955 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500956
957 return widget;
958}
959
960struct widget *
961window_add_widget(struct window *window, void *data)
962{
963 window->widget = widget_create(window, data);
964 wl_list_init(&window->widget->link);
965
966 return window->widget;
967}
968
969struct widget *
970widget_add_widget(struct widget *parent, void *data)
971{
972 struct widget *widget;
973
974 widget = widget_create(parent->window, data);
975 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400976
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978}
979
980void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500981widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200983 struct display *display = widget->window->display;
984 struct input *input;
985
986 wl_list_for_each(input, &display->input_list, link) {
987 if (input->focus_widget == widget)
988 input->focus_widget = NULL;
989 }
990
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500991 wl_list_remove(&widget->link);
992 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993}
994
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999}
1000
1001void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001002widget_set_size(struct widget *widget, int32_t width, int32_t height)
1003{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001004 widget->allocation.width = width;
1005 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001006}
1007
1008void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009widget_set_allocation(struct widget *widget,
1010 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 widget->allocation.x = x;
1013 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001014 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015}
1016
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001017void
1018widget_set_transparent(struct widget *widget, int transparent)
1019{
1020 widget->opaque = !transparent;
1021}
1022
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027}
1028
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001029void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001030widget_set_resize_handler(struct widget *widget,
1031 widget_resize_handler_t handler)
1032{
1033 widget->resize_handler = handler;
1034}
1035
1036void
1037widget_set_redraw_handler(struct widget *widget,
1038 widget_redraw_handler_t handler)
1039{
1040 widget->redraw_handler = handler;
1041}
1042
1043void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001044widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001045{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001046 widget->enter_handler = handler;
1047}
1048
1049void
1050widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1051{
1052 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001053}
1054
1055void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001056widget_set_motion_handler(struct widget *widget,
1057 widget_motion_handler_t handler)
1058{
1059 widget->motion_handler = handler;
1060}
1061
1062void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001063widget_set_button_handler(struct widget *widget,
1064 widget_button_handler_t handler)
1065{
1066 widget->button_handler = handler;
1067}
1068
1069void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001070widget_schedule_redraw(struct widget *widget)
1071{
1072 window_schedule_redraw(widget->window);
1073}
1074
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001075cairo_surface_t *
1076window_get_surface(struct window *window)
1077{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001078 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001079}
1080
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001081struct wl_surface *
1082window_get_wl_surface(struct window *window)
1083{
1084 return window->surface;
1085}
1086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087struct wl_shell_surface *
1088window_get_wl_shell_surface(struct window *window)
1089{
1090 return window->shell_surface;
1091}
1092
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001093static void
1094frame_resize_handler(struct widget *widget,
1095 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001096{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001097 struct frame *frame = data;
1098 struct widget *child = frame->child;
1099 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001100 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001101 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001102 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001103
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001104 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001105 decoration_width = 20 + frame->margin * 2;
1106 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001107
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001108 allocation.x = 10 + frame->margin;
1109 allocation.y = 50 + frame->margin;
1110 allocation.width = width - decoration_width;
1111 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001112
1113 widget->window->input_region =
1114 wl_compositor_create_region(display->compositor);
1115 wl_region_add(widget->window->input_region,
1116 frame->margin, frame->margin,
1117 width - 2 * frame->margin,
1118 height - 2 * frame->margin);
1119
1120 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001121 } else {
1122 decoration_width = 0;
1123 decoration_height = 0;
1124
1125 allocation.x = 0;
1126 allocation.y = 0;
1127 allocation.width = width;
1128 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001129 opaque_margin = 0;
1130 }
1131
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001132 widget_set_allocation(child, allocation.x, allocation.y,
1133 allocation.width, allocation.height);
1134
1135 if (child->resize_handler)
1136 child->resize_handler(child,
1137 allocation.width,
1138 allocation.height,
1139 child->user_data);
1140
1141 widget_set_allocation(widget, 0, 0,
1142 child->allocation.width + decoration_width,
1143 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001144
1145 if (child->opaque) {
1146 widget->window->opaque_region =
1147 wl_compositor_create_region(display->compositor);
1148 wl_region_add(widget->window->opaque_region,
1149 opaque_margin, opaque_margin,
1150 widget->allocation.width - 2 * opaque_margin,
1151 widget->allocation.height - 2 * opaque_margin);
1152 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001153}
1154
1155static void
1156frame_redraw_handler(struct widget *widget, void *data)
1157{
1158 struct frame *frame = data;
1159 cairo_t *cr;
1160 cairo_text_extents_t extents;
1161 cairo_surface_t *source;
1162 int width, height, shadow_dx = 3, shadow_dy = 3;
1163 struct window *window = widget->window;
1164
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001165 if (window->type == TYPE_FULLSCREEN)
1166 return;
1167
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168 width = widget->allocation.width;
1169 height = widget->allocation.height;
1170
1171 cr = cairo_create(window->cairo_surface);
1172
1173 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1174 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1175 cairo_paint(cr);
1176
1177 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1178 tile_mask(cr, window->display->shadow,
1179 shadow_dx, shadow_dy, width, height,
1180 frame->margin + 10 - shadow_dx,
1181 frame->margin + 10 - shadow_dy);
1182
1183 if (window->keyboard_device)
1184 source = window->display->active_frame;
1185 else
1186 source = window->display->inactive_frame;
1187
1188 tile_source(cr, source, 0, 0, width, height,
1189 frame->margin + 10, frame->margin + 50);
1190
1191 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1192 cairo_set_font_size(cr, 14);
1193 cairo_text_extents(cr, window->title, &extents);
1194 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1195 if (window->keyboard_device)
1196 cairo_set_source_rgb(cr, 0, 0, 0);
1197 else
1198 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1199 cairo_show_text(cr, window->title);
1200
1201 cairo_destroy(cr);
1202}
1203
1204static int
1205frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1206{
1207 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 const int grip_size = 8;
1210
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001211 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001212 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001213 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001215 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001217 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001218 hlocation = WINDOW_RESIZING_RIGHT;
1219 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001220 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001222 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001224 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001227 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 vlocation = WINDOW_RESIZING_BOTTOM;
1230 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001233 location = vlocation | hlocation;
1234 if (location & WINDOW_EXTERIOR)
1235 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001237 location = WINDOW_TITLEBAR;
1238 else if (location == WINDOW_INTERIOR)
1239 location = WINDOW_CLIENT_AREA;
1240
1241 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242}
1243
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001244static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 switch (location) {
1251 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001252 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001254 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001256 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001258 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001260 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001262 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001267 case WINDOW_EXTERIOR:
1268 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001272}
1273
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274static void
1275frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001276{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 switch (index) {
1278 case 0: /* close */
1279 if (window->close_handler)
1280 window->close_handler(window->parent,
1281 window->user_data);
1282 else
1283 display_exit(window->display);
1284 break;
1285 case 1: /* fullscreen */
1286 /* we don't have a way to get out of fullscreen for now */
1287 window_set_fullscreen(window, 1);
1288 break;
1289 case 2: /* rotate */
1290 case 3: /* scale */
1291 break;
1292 }
1293}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001295void
1296window_show_frame_menu(struct window *window,
1297 struct input *input, uint32_t time)
1298{
1299 int32_t x, y;
1300
1301 static const char *entries[] = {
1302 "Close", "Fullscreen", "Rotate", "Scale"
1303 };
1304
1305 input_get_position(input, &x, &y);
1306 window_show_menu(window->display, input, time, window,
1307 x - 10, y - 10, frame_menu_func, entries, 4);
1308}
1309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310static int
1311frame_enter_handler(struct widget *widget,
1312 struct input *input, uint32_t time,
1313 int32_t x, int32_t y, void *data)
1314{
1315 return frame_get_pointer_image_for_location(data, input);
1316}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001317
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001318static int
1319frame_motion_handler(struct widget *widget,
1320 struct input *input, uint32_t time,
1321 int32_t x, int32_t y, void *data)
1322{
1323 return frame_get_pointer_image_for_location(data, input);
1324}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001325
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001326static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001327frame_button_handler(struct widget *widget,
1328 struct input *input, uint32_t time,
1329 int button, int state, void *data)
1330
1331{
1332 struct frame *frame = data;
1333 struct window *window = widget->window;
1334 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001335 location = frame_get_pointer_location(frame, input->sx, input->sy);
1336
1337 if (window->display->shell && button == BTN_LEFT && state == 1) {
1338 switch (location) {
1339 case WINDOW_TITLEBAR:
1340 if (!window->shell_surface)
1341 break;
1342 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001343 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001344 wl_shell_surface_move(window->shell_surface,
1345 input_get_input_device(input),
1346 time);
1347 break;
1348 case WINDOW_RESIZING_TOP:
1349 case WINDOW_RESIZING_BOTTOM:
1350 case WINDOW_RESIZING_LEFT:
1351 case WINDOW_RESIZING_RIGHT:
1352 case WINDOW_RESIZING_TOP_LEFT:
1353 case WINDOW_RESIZING_TOP_RIGHT:
1354 case WINDOW_RESIZING_BOTTOM_LEFT:
1355 case WINDOW_RESIZING_BOTTOM_RIGHT:
1356 if (!window->shell_surface)
1357 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001358 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001359 wl_shell_surface_resize(window->shell_surface,
1360 input_get_input_device(input),
1361 time, location);
1362 break;
1363 }
1364 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001365 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001366 }
1367}
1368
1369struct widget *
1370frame_create(struct window *window, void *data)
1371{
1372 struct frame *frame;
1373
1374 frame = malloc(sizeof *frame);
1375 memset(frame, 0, sizeof *frame);
1376
1377 frame->widget = window_add_widget(window, frame);
1378 frame->child = widget_add_widget(frame->widget, data);
1379 frame->margin = 16;
1380
1381 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1382 widget_set_resize_handler(frame->widget, frame_resize_handler);
1383 widget_set_enter_handler(frame->widget, frame_enter_handler);
1384 widget_set_motion_handler(frame->widget, frame_motion_handler);
1385 widget_set_button_handler(frame->widget, frame_button_handler);
1386
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001387 window->frame = frame;
1388
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001390}
1391
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001392static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001393frame_destroy(struct frame *frame)
1394{
1395 /* frame->child must be destroyed by the application */
1396 widget_destroy(frame->widget);
1397 free(frame);
1398}
1399
1400static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001401input_set_focus_widget(struct input *input, struct widget *focus,
1402 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001403{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001404 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001405 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001406
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001407 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001408 return;
1409
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001410 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001411 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001412 widget = old;
1413 if (input->grab)
1414 widget = input->grab;
1415 if (widget->leave_handler)
1416 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001417 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001418 }
1419
1420 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001421 widget = focus;
1422 if (input->grab)
1423 widget = input->grab;
1424 if (widget->enter_handler)
1425 pointer = widget->enter_handler(focus, input, time,
1426 x, y,
1427 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001428 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001429
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001430 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001431 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001432}
1433
1434static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001435input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001436 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001437{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001438 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001439 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001440 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001441 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001442
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001443 input->sx = sx;
1444 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001445
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001446 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001447 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001448 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001449 }
1450
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001451 if (input->grab)
1452 widget = input->grab;
1453 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001454 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001455 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001456 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001457 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001458 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001459
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001460 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001461}
1462
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001463void
1464input_grab(struct input *input, struct widget *widget, uint32_t button)
1465{
1466 input->grab = widget;
1467 input->grab_button = button;
1468}
1469
1470void
1471input_ungrab(struct input *input, uint32_t time)
1472{
1473 struct widget *widget;
1474
1475 input->grab = NULL;
1476 if (input->pointer_focus) {
1477 widget = widget_find_widget(input->pointer_focus->widget,
1478 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001479 input_set_focus_widget(input, widget,
1480 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001481 }
1482}
1483
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001484static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001485input_handle_button(void *data,
1486 struct wl_input_device *input_device,
1487 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001488{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001489 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001490 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001491
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001492 if (input->focus_widget && input->grab == NULL && state)
1493 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001494
Neil Roberts6b28aad2012-01-23 19:11:18 +00001495 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001497 (*widget->button_handler)(widget,
1498 input, time,
1499 button, state,
1500 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001501
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001502 if (input->grab && input->grab_button == button && !state)
1503 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001504}
1505
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001506static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001507input_handle_key(void *data, struct wl_input_device *input_device,
1508 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001509{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001510 struct input *input = data;
1511 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001512 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001513 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001514
Daniel Stone0d5a5092012-02-16 12:48:00 +00001515 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001516 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001517 return;
1518
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001519 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001520 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001521 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1522 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001523
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001524 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1525
1526 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001527 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001528 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001529 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001530
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001531 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1532 if (state)
1533 window_set_maximized(window,
1534 window->type != TYPE_MAXIMIZED);
1535 } else if (window->key_handler) {
1536 (*window->key_handler)(window, input, time, key,
1537 sym, state, window->user_data);
1538 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001539}
1540
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001541static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001542input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001543{
1544 struct window *window = input->pointer_focus;
1545
1546 if (!window)
1547 return;
1548
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001549 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001550
Pekka Paalanene1207c72011-12-16 12:02:09 +02001551 input->pointer_focus = NULL;
1552 input->current_pointer_image = POINTER_UNSET;
1553}
1554
1555static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001556input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001557 struct wl_input_device *input_device,
1558 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001559 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001560{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001561 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001562 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001563 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001564
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001565 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001566 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001567
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001568 input->sx = sx;
1569 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001570
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001571 widget = widget_find_widget(window->widget, sx, sy);
1572 input_set_focus_widget(input, widget, time, sx, sy);
1573}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001574
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001575static void
1576input_handle_pointer_leave(void *data,
1577 struct wl_input_device *input_device,
1578 uint32_t time, struct wl_surface *surface)
1579{
1580 struct input *input = data;
1581
1582 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001583}
1584
1585static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001586input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001587{
1588 struct window *window = input->keyboard_focus;
1589
1590 if (!window)
1591 return;
1592
1593 window->keyboard_device = NULL;
1594 if (window->keyboard_focus_handler)
1595 (*window->keyboard_focus_handler)(window, NULL,
1596 window->user_data);
1597
1598 input->keyboard_focus = NULL;
1599}
1600
1601static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001602input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001603 struct wl_input_device *input_device,
1604 uint32_t time,
1605 struct wl_surface *surface,
1606 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001607{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001608 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001609 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001610 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001611 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001612
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001613 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001614
1615 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001616 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001617 for (k = keys->data; k < end; k++)
1618 input->modifiers |= d->xkb->map->modmap[*k];
1619
1620 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001621 window->keyboard_device = input;
1622 if (window->keyboard_focus_handler)
1623 (*window->keyboard_focus_handler)(window,
1624 window->keyboard_device,
1625 window->user_data);
1626}
1627
1628static void
1629input_handle_keyboard_leave(void *data,
1630 struct wl_input_device *input_device,
1631 uint32_t time,
1632 struct wl_surface *surface)
1633{
1634 struct input *input = data;
1635
1636 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001637}
1638
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001639static void
1640input_handle_touch_down(void *data,
1641 struct wl_input_device *wl_input_device,
1642 uint32_t time, struct wl_surface *surface,
1643 int32_t id, int32_t x, int32_t y)
1644{
1645}
1646
1647static void
1648input_handle_touch_up(void *data,
1649 struct wl_input_device *wl_input_device,
1650 uint32_t time, int32_t id)
1651{
1652}
1653
1654static void
1655input_handle_touch_motion(void *data,
1656 struct wl_input_device *wl_input_device,
1657 uint32_t time, int32_t id, int32_t x, int32_t y)
1658{
1659}
1660
1661static void
1662input_handle_touch_frame(void *data,
1663 struct wl_input_device *wl_input_device)
1664{
1665}
1666
1667static void
1668input_handle_touch_cancel(void *data,
1669 struct wl_input_device *wl_input_device)
1670{
1671}
1672
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001673static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001674 input_handle_motion,
1675 input_handle_button,
1676 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001677 input_handle_pointer_enter,
1678 input_handle_pointer_leave,
1679 input_handle_keyboard_enter,
1680 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001681 input_handle_touch_down,
1682 input_handle_touch_up,
1683 input_handle_touch_motion,
1684 input_handle_touch_frame,
1685 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001686};
1687
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001688void
1689input_get_position(struct input *input, int32_t *x, int32_t *y)
1690{
1691 *x = input->sx;
1692 *y = input->sy;
1693}
1694
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001695struct wl_input_device *
1696input_get_input_device(struct input *input)
1697{
1698 return input->input_device;
1699}
1700
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001701uint32_t
1702input_get_modifiers(struct input *input)
1703{
1704 return input->modifiers;
1705}
1706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707struct widget *
1708input_get_focus_widget(struct input *input)
1709{
1710 return input->focus_widget;
1711}
1712
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001713struct data_offer {
1714 struct wl_data_offer *offer;
1715 struct input *input;
1716 struct wl_array types;
1717 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001718
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001719 struct task io_task;
1720 int fd;
1721 data_func_t func;
1722 int32_t x, y;
1723 void *user_data;
1724};
1725
1726static void
1727data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1728{
1729 struct data_offer *offer = data;
1730 char **p;
1731
1732 p = wl_array_add(&offer->types, sizeof *p);
1733 *p = strdup(type);
1734}
1735
1736static const struct wl_data_offer_listener data_offer_listener = {
1737 data_offer_offer,
1738};
1739
1740static void
1741data_offer_destroy(struct data_offer *offer)
1742{
1743 char **p;
1744
1745 offer->refcount--;
1746 if (offer->refcount == 0) {
1747 wl_data_offer_destroy(offer->offer);
1748 for (p = offer->types.data; *p; p++)
1749 free(*p);
1750 wl_array_release(&offer->types);
1751 free(offer);
1752 }
1753}
1754
1755static void
1756data_device_data_offer(void *data,
1757 struct wl_data_device *data_device, uint32_t id)
1758{
1759 struct data_offer *offer;
1760
1761 offer = malloc(sizeof *offer);
1762
1763 wl_array_init(&offer->types);
1764 offer->refcount = 1;
1765 offer->input = data;
1766
1767 /* FIXME: Generate typesafe wrappers for this */
1768 offer->offer = (struct wl_data_offer *)
1769 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1770 id, &wl_data_offer_interface);
1771
1772 wl_data_offer_add_listener(offer->offer,
1773 &data_offer_listener, offer);
1774}
1775
1776static void
1777data_device_enter(void *data, struct wl_data_device *data_device,
1778 uint32_t time, struct wl_surface *surface,
1779 int32_t x, int32_t y, struct wl_data_offer *offer)
1780{
1781 struct input *input = data;
1782 struct window *window;
1783 char **p;
1784
1785 input->drag_offer = wl_data_offer_get_user_data(offer);
1786 window = wl_surface_get_user_data(surface);
1787 input->pointer_focus = window;
1788
1789 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1790 *p = NULL;
1791
1792 window = input->pointer_focus;
1793 if (window->data_handler)
1794 window->data_handler(window, input, time, x, y,
1795 input->drag_offer->types.data,
1796 window->user_data);
1797}
1798
1799static void
1800data_device_leave(void *data, struct wl_data_device *data_device)
1801{
1802 struct input *input = data;
1803
1804 data_offer_destroy(input->drag_offer);
1805 input->drag_offer = NULL;
1806}
1807
1808static void
1809data_device_motion(void *data, struct wl_data_device *data_device,
1810 uint32_t time, int32_t x, int32_t y)
1811{
1812 struct input *input = data;
1813 struct window *window = input->pointer_focus;
1814
1815 input->sx = x;
1816 input->sy = y;
1817
1818 if (window->data_handler)
1819 window->data_handler(window, input, time, x, y,
1820 input->drag_offer->types.data,
1821 window->user_data);
1822}
1823
1824static void
1825data_device_drop(void *data, struct wl_data_device *data_device)
1826{
1827 struct input *input = data;
1828 struct window *window = input->pointer_focus;
1829
1830 if (window->drop_handler)
1831 window->drop_handler(window, input,
1832 input->sx, input->sy, window->user_data);
1833}
1834
1835static void
1836data_device_selection(void *data,
1837 struct wl_data_device *wl_data_device,
1838 struct wl_data_offer *offer)
1839{
1840 struct input *input = data;
1841 char **p;
1842
1843 if (input->selection_offer)
1844 data_offer_destroy(input->selection_offer);
1845
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001846 if (offer) {
1847 input->selection_offer = wl_data_offer_get_user_data(offer);
1848 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1849 *p = NULL;
1850 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001851}
1852
1853static const struct wl_data_device_listener data_device_listener = {
1854 data_device_data_offer,
1855 data_device_enter,
1856 data_device_leave,
1857 data_device_motion,
1858 data_device_drop,
1859 data_device_selection
1860};
1861
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001862void
1863input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1864{
1865 struct display *display = input->display;
1866 struct wl_buffer *buffer;
1867 cairo_surface_t *surface;
1868
1869 if (pointer == input->current_pointer_image)
1870 return;
1871
1872 input->current_pointer_image = pointer;
1873 surface = display->pointer_surfaces[pointer];
1874
1875 if (!surface)
1876 return;
1877
1878 buffer = display_get_buffer_for_surface(display, surface);
1879 wl_input_device_attach(input->input_device, time, buffer,
1880 pointer_images[pointer].hotspot_x,
1881 pointer_images[pointer].hotspot_y);
1882}
1883
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001884struct wl_data_device *
1885input_get_data_device(struct input *input)
1886{
1887 return input->data_device;
1888}
1889
1890void
1891input_set_selection(struct input *input,
1892 struct wl_data_source *source, uint32_t time)
1893{
1894 wl_data_device_set_selection(input->data_device, source, time);
1895}
1896
1897void
1898input_accept(struct input *input, uint32_t time, const char *type)
1899{
1900 wl_data_offer_accept(input->drag_offer->offer, time, type);
1901}
1902
1903static void
1904offer_io_func(struct task *task, uint32_t events)
1905{
1906 struct data_offer *offer =
1907 container_of(task, struct data_offer, io_task);
1908 unsigned int len;
1909 char buffer[4096];
1910
1911 len = read(offer->fd, buffer, sizeof buffer);
1912 offer->func(buffer, len,
1913 offer->x, offer->y, offer->user_data);
1914
1915 if (len == 0) {
1916 close(offer->fd);
1917 data_offer_destroy(offer);
1918 }
1919}
1920
1921static void
1922data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1923 data_func_t func, void *user_data)
1924{
1925 int p[2];
1926
1927 pipe2(p, O_CLOEXEC);
1928 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1929 close(p[1]);
1930
1931 offer->io_task.run = offer_io_func;
1932 offer->fd = p[0];
1933 offer->func = func;
1934 offer->refcount++;
1935 offer->user_data = user_data;
1936
1937 display_watch_fd(offer->input->display,
1938 offer->fd, EPOLLIN, &offer->io_task);
1939}
1940
1941void
1942input_receive_drag_data(struct input *input, const char *mime_type,
1943 data_func_t func, void *data)
1944{
1945 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1946 input->drag_offer->x = input->sx;
1947 input->drag_offer->y = input->sy;
1948}
1949
1950int
1951input_receive_selection_data(struct input *input, const char *mime_type,
1952 data_func_t func, void *data)
1953{
1954 char **p;
1955
1956 if (input->selection_offer == NULL)
1957 return -1;
1958
1959 for (p = input->selection_offer->types.data; *p; p++)
1960 if (strcmp(mime_type, *p) == 0)
1961 break;
1962
1963 if (*p == NULL)
1964 return -1;
1965
1966 data_offer_receive_data(input->selection_offer,
1967 mime_type, func, data);
1968 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001969}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001970
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001971int
1972input_receive_selection_data_to_fd(struct input *input,
1973 const char *mime_type, int fd)
1974{
1975 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1976
1977 return 0;
1978}
1979
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001980void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001981window_move(struct window *window, struct input *input, uint32_t time)
1982{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001983 if (!window->shell_surface)
1984 return;
1985
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001986 wl_shell_surface_move(window->shell_surface,
1987 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001988}
1989
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001990static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001991idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001992{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001993 struct window *window =
1994 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001995 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001996
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05001997 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001998 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001999 widget_set_allocation(widget,
2000 window->pending_allocation.x,
2001 window->pending_allocation.y,
2002 window->pending_allocation.width,
2003 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002004
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002005 if (window->input_region) {
2006 wl_region_destroy(window->input_region);
2007 window->input_region = NULL;
2008 }
2009
2010 if (window->opaque_region) {
2011 wl_region_destroy(window->opaque_region);
2012 window->opaque_region = NULL;
2013 }
2014
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002015 if (widget->resize_handler)
2016 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002017 widget->allocation.width,
2018 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002019 widget->user_data);
2020
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002021 if (window->allocation.width != widget->allocation.width ||
2022 window->allocation.height != widget->allocation.height) {
2023 window->allocation = widget->allocation;
2024 window_schedule_redraw(window);
2025 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002026}
2027
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002028void
2029window_schedule_resize(struct window *window, int width, int height)
2030{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002031 window->pending_allocation.x = 0;
2032 window->pending_allocation.y = 0;
2033 window->pending_allocation.width = width;
2034 window->pending_allocation.height = height;
2035
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002036 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002037 window->resize_task.run = idle_resize;
2038 display_defer(window->display, &window->resize_task);
2039 window->resize_scheduled = 1;
2040 }
2041}
2042
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002043void
2044widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2045{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002046 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002047}
2048
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002049static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002050handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002051 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002052 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002053{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002054 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002055
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002056 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002057 return;
2058
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002059 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002060 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002061}
2062
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002064menu_destroy(struct menu *menu)
2065{
2066 widget_destroy(menu->widget);
2067 window_destroy(menu->window);
2068 free(menu);
2069}
2070
2071static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2073{
2074 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002075 struct menu *menu = window->widget->user_data;
2076
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002078 * device. Or just use wl_callback. And this really needs to
2079 * be a window vfunc that the menu can set. And we need the
2080 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002082 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002083 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002084 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002085}
2086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002088 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002090};
2091
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002092void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002093window_get_allocation(struct window *window,
2094 struct rectangle *allocation)
2095{
2096 *allocation = window->allocation;
2097}
2098
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002099static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002100widget_redraw(struct widget *widget)
2101{
2102 struct widget *child;
2103
2104 if (widget->redraw_handler)
2105 widget->redraw_handler(widget, widget->user_data);
2106 wl_list_for_each(child, &widget->child_list, link)
2107 widget_redraw(child);
2108}
2109
2110static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002111idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002112{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002113 struct window *window =
2114 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002115
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002116 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002117 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002118 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002119 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120}
2121
2122void
2123window_schedule_redraw(struct window *window)
2124{
2125 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002126 window->redraw_task.run = idle_redraw;
2127 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002128 window->redraw_scheduled = 1;
2129 }
2130}
2131
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002132void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002133window_set_custom(struct window *window)
2134{
2135 window->type = TYPE_CUSTOM;
2136}
2137
2138void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002139window_set_fullscreen(struct window *window, int fullscreen)
2140{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002141 if (!window->display->shell)
2142 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002143
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002144 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002145 return;
2146
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002147 if (fullscreen) {
2148 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002149 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002150 wl_shell_surface_set_fullscreen(window->shell_surface,
2151 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2152 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002153 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002154 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002155 wl_shell_surface_set_toplevel(window->shell_surface);
2156 window_schedule_resize(window,
2157 window->saved_allocation.width,
2158 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002159 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002160}
2161
2162void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002163window_set_maximized(struct window *window, int maximized)
2164{
2165 if (!window->display->shell)
2166 return;
2167
2168 if ((window->type == TYPE_MAXIMIZED) == maximized)
2169 return;
2170
2171 if (window->type == TYPE_TOPLEVEL) {
2172 window->saved_allocation = window->allocation;
2173 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2174 window->type = TYPE_MAXIMIZED;
2175 } else {
2176 wl_shell_surface_set_toplevel(window->shell_surface);
2177 window->type = TYPE_TOPLEVEL;
2178 window_schedule_resize(window,
2179 window->saved_allocation.width,
2180 window->saved_allocation.height);
2181 }
2182}
2183
2184void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002185window_set_user_data(struct window *window, void *data)
2186{
2187 window->user_data = data;
2188}
2189
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002190void *
2191window_get_user_data(struct window *window)
2192{
2193 return window->user_data;
2194}
2195
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002196void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002197window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002198 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002199{
2200 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002201}
2202
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002203void
2204window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002205 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002206{
2207 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002208}
2209
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002210void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002211window_set_data_handler(struct window *window, window_data_handler_t handler)
2212{
2213 window->data_handler = handler;
2214}
2215
2216void
2217window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2218{
2219 window->drop_handler = handler;
2220}
2221
2222void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002223window_set_close_handler(struct window *window,
2224 window_close_handler_t handler)
2225{
2226 window->close_handler = handler;
2227}
2228
2229void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002230window_set_title(struct window *window, const char *title)
2231{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002232 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002233 window->title = strdup(title);
2234}
2235
2236const char *
2237window_get_title(struct window *window)
2238{
2239 return window->title;
2240}
2241
2242void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002243display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2244 int32_t x, int32_t y, int32_t width, int32_t height)
2245{
2246 struct wl_buffer *buffer;
2247
2248 buffer = display_get_buffer_for_surface(display, cairo_surface);
2249
2250 wl_buffer_damage(buffer, x, y, width, height);
2251}
2252
2253void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002254window_damage(struct window *window, int32_t x, int32_t y,
2255 int32_t width, int32_t height)
2256{
2257 wl_surface_damage(window->surface, x, y, width, height);
2258}
2259
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002260static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002261window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002262{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002263 struct window *window;
2264
2265 window = malloc(sizeof *window);
2266 if (window == NULL)
2267 return NULL;
2268
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002269 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002270 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002271 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002272 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002273 if (display->shell) {
2274 window->shell_surface =
2275 wl_shell_get_shell_surface(display->shell,
2276 window->surface);
2277 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002278 window->allocation.x = 0;
2279 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002280 window->allocation.width = 0;
2281 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002282 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002283 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002284 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002285 window->input_region = NULL;
2286 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002287
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002288 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002289#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002290 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2291 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002292#else
2293 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2294#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002295 else
2296 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002297
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002298 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002299 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002300
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002301 if (window->shell_surface) {
2302 wl_shell_surface_set_user_data(window->shell_surface, window);
2303 wl_shell_surface_add_listener(window->shell_surface,
2304 &shell_surface_listener, window);
2305 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002306
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002307 return window;
2308}
2309
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002310struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002311window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002312{
2313 struct window *window;
2314
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002315 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002316 if (!window)
2317 return NULL;
2318
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002319 window->type = TYPE_TOPLEVEL;
2320 if (display->shell)
2321 wl_shell_surface_set_toplevel(window->shell_surface);
2322
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002323 return window;
2324}
2325
2326struct window *
2327window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002328 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002329{
2330 struct window *window;
2331
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002332 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002333 if (!window)
2334 return NULL;
2335
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002336 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002337 window->x = x;
2338 window->y = y;
2339
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002340 if (display->shell)
2341 wl_shell_surface_set_transient(window->shell_surface,
2342 window->parent->shell_surface,
2343 window->x, window->y, 0);
2344
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002345 return window;
2346}
2347
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002348static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002349menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002350{
2351 int next;
2352
2353 next = (sy - 8) / 20;
2354 if (menu->current != next) {
2355 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002356 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002357 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002358}
2359
2360static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002361menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002362 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002363 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002364{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002365 struct menu *menu = data;
2366
2367 if (widget == menu->widget)
2368 menu_set_item(data, y);
2369
2370 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371}
2372
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002373static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002374menu_enter_handler(struct widget *widget,
2375 struct input *input, uint32_t time,
2376 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002377{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002378 struct menu *menu = data;
2379
2380 if (widget == menu->widget)
2381 menu_set_item(data, y);
2382
2383 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002384}
2385
2386static void
2387menu_leave_handler(struct widget *widget, struct input *input, void *data)
2388{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002389 struct menu *menu = data;
2390
2391 if (widget == menu->widget)
2392 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002393}
2394
2395static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002396menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397 struct input *input, uint32_t time,
2398 int button, int state, void *data)
2399
2400{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002401 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002402
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002403 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002404 /* Either relase after press-drag-release or
2405 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002406 menu->func(menu->window->parent,
2407 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002408 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002409 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002410 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411}
2412
2413static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002414menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002415{
2416 cairo_t *cr;
2417 const int32_t r = 3, margin = 3;
2418 struct menu *menu = data;
2419 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002420 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002421
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422 cr = cairo_create(window->cairo_surface);
2423 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2424 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2425 cairo_paint(cr);
2426
2427 width = window->allocation.width;
2428 height = window->allocation.height;
2429 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002430
2431 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002432 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2433 cairo_fill(cr);
2434
2435 for (i = 0; i < menu->count; i++) {
2436 if (i == menu->current) {
2437 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2438 cairo_rectangle(cr, margin, i * 20 + margin,
2439 width - 2 * margin, 20);
2440 cairo_fill(cr);
2441 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2442 cairo_move_to(cr, 10, i * 20 + 16);
2443 cairo_show_text(cr, menu->entries[i]);
2444 } else {
2445 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2446 cairo_move_to(cr, 10, i * 20 + 16);
2447 cairo_show_text(cr, menu->entries[i]);
2448 }
2449 }
2450
2451 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002452}
2453
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002454void
2455window_show_menu(struct display *display,
2456 struct input *input, uint32_t time, struct window *parent,
2457 int32_t x, int32_t y,
2458 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002459{
2460 struct window *window;
2461 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002462 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463
2464 menu = malloc(sizeof *menu);
2465 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002466 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002467
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002468 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002469 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002470 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002471
2472 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002473 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002474 menu->entries = entries;
2475 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002476 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002477 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002478 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002479 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002480 window->type = TYPE_MENU;
2481 window->x = x;
2482 window->y = y;
2483
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002484 wl_shell_surface_set_popup(window->shell_surface,
2485 input->input_device, time,
2486 window->parent->shell_surface,
2487 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002488
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002489 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002490 widget_set_enter_handler(menu->widget, menu_enter_handler);
2491 widget_set_leave_handler(menu->widget, menu_leave_handler);
2492 widget_set_motion_handler(menu->widget, menu_motion_handler);
2493 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002494
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002495 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002496 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002497}
2498
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002499void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002500window_set_buffer_type(struct window *window, enum window_buffer_type type)
2501{
2502 window->buffer_type = type;
2503}
2504
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002505
2506static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002507display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002508 struct wl_output *wl_output,
2509 int x, int y,
2510 int physical_width,
2511 int physical_height,
2512 int subpixel,
2513 const char *make,
2514 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002515{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002516 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002517
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002518 output->allocation.x = x;
2519 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002520}
2521
2522static void
2523display_handle_mode(void *data,
2524 struct wl_output *wl_output,
2525 uint32_t flags,
2526 int width,
2527 int height,
2528 int refresh)
2529{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002530 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002531 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002532
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002533 if (flags & WL_OUTPUT_MODE_CURRENT) {
2534 output->allocation.width = width;
2535 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002536 if (display->output_configure_handler)
2537 (*display->output_configure_handler)(
2538 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002539 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002540}
2541
2542static const struct wl_output_listener output_listener = {
2543 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002544 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002545};
2546
2547static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002548display_add_output(struct display *d, uint32_t id)
2549{
2550 struct output *output;
2551
2552 output = malloc(sizeof *output);
2553 if (output == NULL)
2554 return;
2555
2556 memset(output, 0, sizeof *output);
2557 output->display = d;
2558 output->output =
2559 wl_display_bind(d->display, id, &wl_output_interface);
2560 wl_list_insert(d->output_list.prev, &output->link);
2561
2562 wl_output_add_listener(output->output, &output_listener, output);
2563}
2564
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002565static void
2566output_destroy(struct output *output)
2567{
2568 if (output->destroy_handler)
2569 (*output->destroy_handler)(output, output->user_data);
2570
2571 wl_output_destroy(output->output);
2572 wl_list_remove(&output->link);
2573 free(output);
2574}
2575
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002576void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002577display_set_output_configure_handler(struct display *display,
2578 display_output_handler_t handler)
2579{
2580 struct output *output;
2581
2582 display->output_configure_handler = handler;
2583 if (!handler)
2584 return;
2585
2586 wl_list_for_each(output, &display->output_list, link)
2587 (*display->output_configure_handler)(output,
2588 display->user_data);
2589}
2590
2591void
2592output_set_user_data(struct output *output, void *data)
2593{
2594 output->user_data = data;
2595}
2596
2597void *
2598output_get_user_data(struct output *output)
2599{
2600 return output->user_data;
2601}
2602
2603void
2604output_set_destroy_handler(struct output *output,
2605 display_output_handler_t handler)
2606{
2607 output->destroy_handler = handler;
2608 /* FIXME: implement this, once we have way to remove outputs */
2609}
2610
2611void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002612output_get_allocation(struct output *output, struct rectangle *allocation)
2613{
2614 *allocation = output->allocation;
2615}
2616
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002617struct wl_output *
2618output_get_wl_output(struct output *output)
2619{
2620 return output->output;
2621}
2622
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002623static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002624display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002625{
2626 struct input *input;
2627
2628 input = malloc(sizeof *input);
2629 if (input == NULL)
2630 return;
2631
2632 memset(input, 0, sizeof *input);
2633 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002634 input->input_device =
2635 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002636 input->pointer_focus = NULL;
2637 input->keyboard_focus = NULL;
2638 wl_list_insert(d->input_list.prev, &input->link);
2639
2640 wl_input_device_add_listener(input->input_device,
2641 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002642 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002643
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002644 input->data_device =
2645 wl_data_device_manager_get_data_device(d->data_device_manager,
2646 input->input_device);
2647 wl_data_device_add_listener(input->data_device,
2648 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002649}
2650
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002651static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002652input_destroy(struct input *input)
2653{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002654 input_remove_keyboard_focus(input);
2655 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002656
2657 if (input->drag_offer)
2658 data_offer_destroy(input->drag_offer);
2659
2660 if (input->selection_offer)
2661 data_offer_destroy(input->selection_offer);
2662
2663 wl_data_device_destroy(input->data_device);
2664 wl_list_remove(&input->link);
2665 wl_input_device_destroy(input->input_device);
2666 free(input);
2667}
2668
2669static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002670display_handle_global(struct wl_display *display, uint32_t id,
2671 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002672{
2673 struct display *d = data;
2674
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002675 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002676 d->compositor =
2677 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002678 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002679 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002680 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002681 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002682 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002683 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002684 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002685 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002686 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2687 d->data_device_manager =
2688 wl_display_bind(display, id,
2689 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002690 }
2691}
2692
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002693static void
2694display_render_frame(struct display *d)
2695{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002696 cairo_t *cr;
2697
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002698 d->frame_radius = 8;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002699 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2700 cr = cairo_create(d->shadow);
2701 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2702 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002703 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002704 cairo_fill(cr);
2705 cairo_destroy(cr);
2706 blur_surface(d->shadow, 64);
2707
2708 d->active_frame =
2709 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2710 cr = cairo_create(d->active_frame);
2711 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2712 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002713 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002714 cairo_fill(cr);
2715 cairo_destroy(cr);
2716
2717 d->inactive_frame =
2718 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2719 cr = cairo_create(d->inactive_frame);
2720 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2721 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002722 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002723 cairo_fill(cr);
2724 cairo_destroy(cr);
2725}
2726
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002727static void
2728init_xkb(struct display *d)
2729{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002730 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002731
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002732 names.rules = "evdev";
2733 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002734 names.layout = option_xkb_layout;
2735 names.variant = option_xkb_variant;
2736 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002737
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002738 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002739 if (!d->xkb) {
2740 fprintf(stderr, "Failed to compile keymap\n");
2741 exit(1);
2742 }
2743}
2744
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002745static void
2746fini_xkb(struct display *display)
2747{
2748 xkb_free_keymap(display->xkb);
2749}
2750
Yuval Fledel45568f62010-12-06 09:18:12 -05002751static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002752init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002753{
2754 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002755 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002756
Rob Clark6396ed32012-03-11 19:48:41 -05002757#ifdef USE_CAIRO_GLESV2
2758# define GL_BIT EGL_OPENGL_ES2_BIT
2759#else
2760# define GL_BIT EGL_OPENGL_BIT
2761#endif
2762
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002763 static const EGLint argb_cfg_attribs[] = {
2764 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002765 EGL_RED_SIZE, 1,
2766 EGL_GREEN_SIZE, 1,
2767 EGL_BLUE_SIZE, 1,
2768 EGL_ALPHA_SIZE, 1,
2769 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002770 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002771 EGL_NONE
2772 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002773
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002774#ifdef USE_CAIRO_GLESV2
2775 static const EGLint context_attribs[] = {
2776 EGL_CONTEXT_CLIENT_VERSION, 2,
2777 EGL_NONE
2778 };
2779 EGLint api = EGL_OPENGL_ES_API;
2780#else
2781 EGLint *context_attribs = NULL;
2782 EGLint api = EGL_OPENGL_API;
2783#endif
2784
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002785 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002786 if (!eglInitialize(d->dpy, &major, &minor)) {
2787 fprintf(stderr, "failed to initialize display\n");
2788 return -1;
2789 }
2790
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002791 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002792 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2793 return -1;
2794 }
2795
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002796 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2797 &d->argb_config, 1, &n) || n != 1) {
2798 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002799 return -1;
2800 }
2801
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002802 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002803 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002804 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002805 fprintf(stderr, "failed to create context\n");
2806 return -1;
2807 }
2808
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002809 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002810 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002811 return -1;
2812 }
2813
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002815 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2816 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2817 fprintf(stderr, "failed to get cairo egl argb device\n");
2818 return -1;
2819 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002820#endif
2821
2822 return 0;
2823}
2824
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002825static void
2826fini_egl(struct display *display)
2827{
2828#ifdef HAVE_CAIRO_EGL
2829 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002830#endif
2831
2832 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2833 EGL_NO_CONTEXT);
2834
2835 eglTerminate(display->dpy);
2836 eglReleaseThread();
2837}
2838
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002839static int
2840event_mask_update(uint32_t mask, void *data)
2841{
2842 struct display *d = data;
2843
2844 d->mask = mask;
2845
2846 return 0;
2847}
2848
2849static void
2850handle_display_data(struct task *task, uint32_t events)
2851{
2852 struct display *display =
2853 container_of(task, struct display, display_task);
2854
2855 wl_display_iterate(display->display, display->mask);
2856}
2857
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002858struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002859display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002860{
2861 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002862
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002863 argc = parse_options(xkb_options,
2864 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002865
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002866 d = malloc(sizeof *d);
2867 if (d == NULL)
2868 return NULL;
2869
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002870 memset(d, 0, sizeof *d);
2871
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002872 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002873 if (d->display == NULL) {
2874 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002875 return NULL;
2876 }
2877
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002878 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2879 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2880 d->display_task.run = handle_display_data;
2881 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2882
2883 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002884 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002885 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002886
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002887 /* Set up listener so we'll catch all events. */
2888 wl_display_add_global_listener(d->display,
2889 display_handle_global, d);
2890
2891 /* Process connection events. */
2892 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002893 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002894 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002895
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002896 d->image_target_texture_2d =
2897 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2898 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2899 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2900
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002901 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002902
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002903 display_render_frame(d);
2904
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002905 wl_list_init(&d->window_list);
2906
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002907 init_xkb(d);
2908
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002909 return d;
2910}
2911
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002912static void
2913display_destroy_outputs(struct display *display)
2914{
2915 struct output *tmp;
2916 struct output *output;
2917
2918 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2919 output_destroy(output);
2920}
2921
Pekka Paalanene1207c72011-12-16 12:02:09 +02002922static void
2923display_destroy_inputs(struct display *display)
2924{
2925 struct input *tmp;
2926 struct input *input;
2927
2928 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2929 input_destroy(input);
2930}
2931
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002932void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002933display_destroy(struct display *display)
2934{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002935 if (!wl_list_empty(&display->window_list))
2936 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2937
2938 if (!wl_list_empty(&display->deferred_list))
2939 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2940
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002941 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002942 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002943
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002944 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002945
2946 cairo_surface_destroy(display->active_frame);
2947 cairo_surface_destroy(display->inactive_frame);
2948 cairo_surface_destroy(display->shadow);
2949 destroy_pointer_surfaces(display);
2950
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002951 fini_egl(display);
2952
Pekka Paalanenc2052982011-12-16 11:41:32 +02002953 if (display->shell)
2954 wl_shell_destroy(display->shell);
2955
2956 if (display->shm)
2957 wl_shm_destroy(display->shm);
2958
2959 if (display->data_device_manager)
2960 wl_data_device_manager_destroy(display->data_device_manager);
2961
2962 wl_compositor_destroy(display->compositor);
2963
2964 close(display->epoll_fd);
2965
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002966 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002967 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002968 free(display);
2969}
2970
2971void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002972display_set_user_data(struct display *display, void *data)
2973{
2974 display->user_data = data;
2975}
2976
2977void *
2978display_get_user_data(struct display *display)
2979{
2980 return display->user_data;
2981}
2982
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002983struct wl_display *
2984display_get_display(struct display *display)
2985{
2986 return display->display;
2987}
2988
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002989struct output *
2990display_get_output(struct display *display)
2991{
2992 return container_of(display->output_list.next, struct output, link);
2993}
2994
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002995struct wl_compositor *
2996display_get_compositor(struct display *display)
2997{
2998 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002999}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003000
3001EGLDisplay
3002display_get_egl_display(struct display *d)
3003{
3004 return d->dpy;
3005}
3006
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003007struct wl_data_source *
3008display_create_data_source(struct display *display)
3009{
3010 return wl_data_device_manager_create_data_source(display->data_device_manager);
3011}
3012
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003013EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003014display_get_argb_egl_config(struct display *d)
3015{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003016 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003017}
3018
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003019struct wl_shell *
3020display_get_shell(struct display *display)
3021{
3022 return display->shell;
3023}
3024
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003025int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003026display_acquire_window_surface(struct display *display,
3027 struct window *window,
3028 EGLContext ctx)
3029{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003030#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003031 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003032 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003033
3034 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003035 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003036 device = cairo_surface_get_device(window->cairo_surface);
3037 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003038 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003039
Benjamin Franzke0c991632011-09-27 21:57:31 +02003040 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003041 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003042 ctx = display->argb_ctx;
3043 else
3044 assert(0);
3045 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003046
3047 data = cairo_surface_get_user_data(window->cairo_surface,
3048 &surface_data_key);
3049
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003050 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003051 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003052 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3053 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003054
3055 return 0;
3056#else
3057 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003058#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003059}
3060
3061void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003062display_release_window_surface(struct display *display,
3063 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003064{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003065#ifdef HAVE_CAIRO_EGL
3066 cairo_device_t *device;
3067
3068 device = cairo_surface_get_device(window->cairo_surface);
3069 if (!device)
3070 return;
3071
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003072 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003073 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003074 cairo_device_release(device);
3075#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076}
3077
3078void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003079display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003080{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003081 wl_list_insert(&display->deferred_list, &task->link);
3082}
3083
3084void
3085display_watch_fd(struct display *display,
3086 int fd, uint32_t events, struct task *task)
3087{
3088 struct epoll_event ep;
3089
3090 ep.events = events;
3091 ep.data.ptr = task;
3092 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3093}
3094
3095void
3096display_run(struct display *display)
3097{
3098 struct task *task;
3099 struct epoll_event ep[16];
3100 int i, count;
3101
Pekka Paalanen826d7952011-12-15 10:14:07 +02003102 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003103 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003104 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003105
3106 while (!wl_list_empty(&display->deferred_list)) {
3107 task = container_of(display->deferred_list.next,
3108 struct task, link);
3109 wl_list_remove(&task->link);
3110 task->run(task, 0);
3111 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003112
3113 if (!display->running)
3114 break;
3115
3116 wl_display_flush(display->display);
3117
3118 count = epoll_wait(display->epoll_fd,
3119 ep, ARRAY_LENGTH(ep), -1);
3120 for (i = 0; i < count; i++) {
3121 task = ep[i].data.ptr;
3122 task->run(task, ep[i].events);
3123 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003124 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003125}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003126
3127void
3128display_exit(struct display *display)
3129{
3130 display->running = 0;
3131}