blob: bb17b8abc1d561e8ae9737156898dad0620efda4 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050068 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118 int resize_scheduled;
119 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400121 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400123 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500124
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500125 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500126
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500127 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500128 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400129 window_data_handler_t data_handler;
130 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500131 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400132
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200133 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500134 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500135
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500136 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400137 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138};
139
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500140struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500141 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500142 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400143 struct wl_list link;
144 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500145 widget_resize_handler_t resize_handler;
146 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500147 widget_enter_handler_t enter_handler;
148 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500149 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500150 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 void *user_data;
152};
153
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400154struct input {
155 struct display *display;
156 struct wl_input_device *input_device;
157 struct window *pointer_focus;
158 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400159 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160 uint32_t modifiers;
161 int32_t x, y, sx, sy;
162 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400163
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500164 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500165 struct widget *grab;
166 uint32_t grab_button;
167
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168 struct wl_data_device *data_device;
169 struct data_offer *drag_offer;
170 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171};
172
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500173struct output {
174 struct display *display;
175 struct wl_output *output;
176 struct rectangle allocation;
177 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200178
179 display_output_handler_t destroy_handler;
180 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500181};
182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500183struct frame {
184 struct widget *widget;
185 struct widget *child;
186 int margin;
187};
188
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500189struct menu {
190 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500191 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500192 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500193 const char **entries;
194 uint32_t time;
195 int current;
196 int count;
197 menu_func_t func;
198};
199
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400200enum {
201 POINTER_DEFAULT = 100,
202 POINTER_UNSET
203};
204
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500205enum window_location {
206 WINDOW_INTERIOR = 0,
207 WINDOW_RESIZING_TOP = 1,
208 WINDOW_RESIZING_BOTTOM = 2,
209 WINDOW_RESIZING_LEFT = 4,
210 WINDOW_RESIZING_TOP_LEFT = 5,
211 WINDOW_RESIZING_BOTTOM_LEFT = 6,
212 WINDOW_RESIZING_RIGHT = 8,
213 WINDOW_RESIZING_TOP_RIGHT = 9,
214 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
215 WINDOW_RESIZING_MASK = 15,
216 WINDOW_EXTERIOR = 16,
217 WINDOW_TITLEBAR = 17,
218 WINDOW_CLIENT_AREA = 18,
219};
220
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400221const char *option_xkb_layout = "us";
222const char *option_xkb_variant = "";
223const char *option_xkb_options = "";
224
225static const GOptionEntry xkb_option_entries[] = {
226 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_layout, "XKB Layout" },
228 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
229 &option_xkb_variant, "XKB Variant" },
230 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_options, "XKB Options" },
232 { NULL }
233};
234
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400235static const cairo_user_data_key_t surface_data_key;
236struct surface_data {
237 struct wl_buffer *buffer;
238};
239
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500240#define MULT(_d,c,a,t) \
241 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
242
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500243#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245struct egl_window_surface_data {
246 struct display *display;
247 struct wl_surface *surface;
248 struct wl_egl_window *window;
249 EGLSurface surf;
250};
251
252static void
253egl_window_surface_data_destroy(void *p)
254{
255 struct egl_window_surface_data *data = p;
256 struct display *d = data->display;
257
258 eglDestroySurface(d->dpy, data->surf);
259 wl_egl_window_destroy(data->window);
260 data->surface = NULL;
261
262 free(p);
263}
264
265static cairo_surface_t *
266display_create_egl_window_surface(struct display *display,
267 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269 struct rectangle *rectangle)
270{
271 cairo_surface_t *cairo_surface;
272 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200274 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275
276 data = malloc(sizeof *data);
277 if (data == NULL)
278 return NULL;
279
280 data->display = display;
281 data->surface = surface;
282
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 if (flags & SURFACE_OPAQUE) {
284 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200285 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500287 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 }
290
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400291 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100292 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500296 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Benjamin Franzke0c991632011-09-27 21:57:31 +0200298 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 data->surf,
300 rectangle->width,
301 rectangle->height);
302
303 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
304 data, egl_window_surface_data_destroy);
305
306 return cairo_surface;
307}
308
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500309struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200311 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 EGLImageKHR image;
313 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316};
317
318static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400320{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323
Benjamin Franzke0c991632011-09-27 21:57:31 +0200324 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500328 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500330 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332}
333
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500334EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335display_get_image_for_egl_image_surface(struct display *display,
336 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339
340 data = cairo_surface_get_user_data (surface, &surface_data_key);
341
342 return data->image;
343}
344
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500345static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400347 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351 EGLDisplay dpy = display->dpy;
352 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200353 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354
355 data = malloc(sizeof *data);
356 if (data == NULL)
357 return NULL;
358
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800359 data->display = display;
360
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400361 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000364 free(data);
365 return NULL;
366 }
367
Benjamin Franzke0c991632011-09-27 21:57:31 +0200368 if (flags & SURFACE_OPAQUE) {
369 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 content = CAIRO_CONTENT_COLOR;
371 } else {
372 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR_ALPHA;
374 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500376 data->image = display->create_image(dpy, NULL,
377 EGL_NATIVE_PIXMAP_KHR,
378 (EGLClientBuffer) data->pixmap,
379 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500380 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500381 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 free(data);
383 return NULL;
384 }
385
386 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400387 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388
Benjamin Franzke0c991632011-09-27 21:57:31 +0200389 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500392 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 surface = cairo_gl_surface_create_for_texture(data->device,
396 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 data->texture,
398 rectangle->width,
399 rectangle->height);
400
401 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500402 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
404 return surface;
405}
406
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500407static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500408display_create_egl_image_surface_from_file(struct display *display,
409 const char *filename,
410 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411{
412 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413 GdkPixbuf *pixbuf;
414 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400415 int stride, i;
416 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500417 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
419 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421 FALSE, &error);
422 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
426 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500427 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431
432 stride = gdk_pixbuf_get_rowstride(pixbuf);
433 pixels = gdk_pixbuf_get_pixels(pixbuf);
434
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438 while (p < end) {
439 unsigned int t;
440
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 MULT(p[0], p[0], p[3], t);
442 MULT(p[1], p[1], p[3], t);
443 MULT(p[2], p[2], p[3], t);
444 p += 4;
445
446 }
447 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400448
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200449 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000450 if (surface == NULL) {
451 g_object_unref(pixbuf);
452 return NULL;
453 }
454
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800455 data = cairo_surface_get_user_data(surface, &surface_data_key);
456
Benjamin Franzke0c991632011-09-27 21:57:31 +0200457 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800459 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
460 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200461 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400462
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500463 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465 return surface;
466}
467
468#endif
469
470struct wl_buffer *
471display_get_buffer_for_surface(struct display *display,
472 cairo_surface_t *surface)
473{
474 struct surface_data *data;
475
476 data = cairo_surface_get_user_data (surface, &surface_data_key);
477
478 return data->buffer;
479}
480
481struct shm_surface_data {
482 struct surface_data data;
483 void *map;
484 size_t length;
485};
486
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500487static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488shm_surface_data_destroy(void *p)
489{
490 struct shm_surface_data *data = p;
491
492 wl_buffer_destroy(data->data.buffer);
493 munmap(data->map, data->length);
494}
495
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500496static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400498 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499{
500 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800503 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504 char filename[] = "/tmp/wayland-shm-XXXXXX";
505
506 data = malloc(sizeof *data);
507 if (data == NULL)
508 return NULL;
509
510 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
511 rectangle->width);
512 data->length = stride * rectangle->height;
513 fd = mkstemp(filename);
514 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000515 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516 return NULL;
517 }
518 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000519 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 close(fd);
521 return NULL;
522 }
523
524 data->map = mmap(NULL, data->length,
525 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
526 unlink(filename);
527
528 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000529 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 close(fd);
531 return NULL;
532 }
533
534 surface = cairo_image_surface_create_for_data (data->map,
535 CAIRO_FORMAT_ARGB32,
536 rectangle->width,
537 rectangle->height,
538 stride);
539
540 cairo_surface_set_user_data (surface, &surface_data_key,
541 data, shm_surface_data_destroy);
542
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400543 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500544 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500546 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 data->data.buffer = wl_shm_create_buffer(display->shm,
549 fd,
550 rectangle->width,
551 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400552 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553
554 close(fd);
555
556 return surface;
557}
558
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500559static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560display_create_shm_surface_from_file(struct display *display,
561 const char *filename,
562 struct rectangle *rect)
563{
564 cairo_surface_t *surface;
565 GdkPixbuf *pixbuf;
566 GError *error = NULL;
567 int stride, i;
568 unsigned char *pixels, *p, *end, *dest_data;
569 int dest_stride;
570 uint32_t *d;
571
572 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
573 rect->width, rect->height,
574 FALSE, &error);
575 if (error != NULL)
576 return NULL;
577
578 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
579 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500580 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581 return NULL;
582 }
583
584 stride = gdk_pixbuf_get_rowstride(pixbuf);
585 pixels = gdk_pixbuf_get_pixels(pixbuf);
586
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400587 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000588 if (surface == NULL) {
589 g_object_unref(pixbuf);
590 return NULL;
591 }
592
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 dest_data = cairo_image_surface_get_data (surface);
594 dest_stride = cairo_image_surface_get_stride (surface);
595
596 for (i = 0; i < rect->height; i++) {
597 d = (uint32_t *) (dest_data + i * dest_stride);
598 p = pixels + i * stride;
599 end = p + rect->width * 4;
600 while (p < end) {
601 unsigned int t;
602 unsigned char a, r, g, b;
603
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604 a = p[3];
605 MULT(r, p[0], a, t);
606 MULT(g, p[1], a, t);
607 MULT(b, p[2], a, t);
608 p += 4;
609 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
610 }
611 }
612
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500613 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614
615 return surface;
616}
617
nobled7b87cb02011-02-01 18:51:47 +0000618static int
619check_size(struct rectangle *rect)
620{
621 if (rect->width && rect->height)
622 return 0;
623
624 fprintf(stderr, "tried to create surface of "
625 "width: %d, height: %d\n", rect->width, rect->height);
626 return -1;
627}
628
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629cairo_surface_t *
630display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400632 struct rectangle *rectangle,
633 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634{
nobled7b87cb02011-02-01 18:51:47 +0000635 if (check_size(rectangle) < 0)
636 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500637#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500638 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639 if (surface)
640 return display_create_egl_window_surface(display,
641 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400642 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643 rectangle);
644 else
645 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500648 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400650 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651}
652
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500653static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654display_create_surface_from_file(struct display *display,
655 const char *filename,
656 struct rectangle *rectangle)
657{
nobled7b87cb02011-02-01 18:51:47 +0000658 if (check_size(rectangle) < 0)
659 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500660#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500661 if (display->dpy) {
662 return display_create_egl_image_surface_from_file(display,
663 filename,
664 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500665 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400666#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500667 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668}
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670 const char *filename;
671 int hotspot_x, hotspot_y;
672} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500673 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
674 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
675 { DATADIR "/weston/bottom_side.png", 16, 20 },
676 { DATADIR "/weston/grabbing.png", 20, 17 },
677 { DATADIR "/weston/left_ptr.png", 10, 5 },
678 { DATADIR "/weston/left_side.png", 10, 20 },
679 { DATADIR "/weston/right_side.png", 30, 19 },
680 { DATADIR "/weston/top_left_corner.png", 8, 8 },
681 { DATADIR "/weston/top_right_corner.png", 26, 8 },
682 { DATADIR "/weston/top_side.png", 18, 8 },
683 { DATADIR "/weston/xterm.png", 15, 15 },
684 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685};
686
687static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689{
690 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 display->pointer_surfaces =
696 malloc(count * sizeof *display->pointer_surfaces);
697 rect.width = width;
698 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701 display_create_surface_from_file(display,
702 pointer_images[i].filename,
703 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100704 if (!display->pointer_surfaces[i]) {
705 fprintf(stderr, "Error loading pointer image: %s\n",
706 pointer_images[i].filename);
707 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400708 }
709
710}
711
Pekka Paalanen325bb602011-12-19 10:31:45 +0200712static void
713destroy_pointer_surfaces(struct display *display)
714{
715 int i, count;
716
717 count = ARRAY_LENGTH(pointer_images);
718 for (i = 0; i < count; ++i) {
719 if (display->pointer_surfaces[i])
720 cairo_surface_destroy(display->pointer_surfaces[i]);
721 }
722 free(display->pointer_surfaces);
723}
724
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400725cairo_surface_t *
726display_get_pointer_surface(struct display *display, int pointer,
727 int *width, int *height,
728 int *hotspot_x, int *hotspot_y)
729{
730 cairo_surface_t *surface;
731
732 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500733#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 *width = cairo_gl_surface_get_width(surface);
735 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000736#else
737 *width = cairo_image_surface_get_width(surface);
738 *height = cairo_image_surface_get_height(surface);
739#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740 *hotspot_x = pointer_images[pointer].hotspot_x;
741 *hotspot_y = pointer_images[pointer].hotspot_y;
742
743 return cairo_surface_reference(surface);
744}
745
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400746static void
747window_attach_surface(struct window *window);
748
749static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400751{
752 struct window *window = data;
753
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755 cairo_surface_destroy(window->pending_surface);
756 window->pending_surface = NULL;
757 if (window->cairo_surface)
758 window_attach_surface(window);
759}
760
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400761static const struct wl_callback_listener free_surface_listener = {
762 free_surface
763};
764
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500765static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200766window_get_resize_dx_dy(struct window *window, int *x, int *y)
767{
768 if (window->resize_edges & WINDOW_RESIZING_LEFT)
769 *x = window->server_allocation.width - window->allocation.width;
770 else
771 *x = 0;
772
773 if (window->resize_edges & WINDOW_RESIZING_TOP)
774 *y = window->server_allocation.height -
775 window->allocation.height;
776 else
777 *y = 0;
778
779 window->resize_edges = 0;
780}
781
782static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400783window_set_type(struct window *window)
784{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200785 if (!window->shell_surface)
786 return;
787
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400788 switch (window->type) {
789 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200790 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 break;
792 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
795 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796 wl_shell_surface_set_transient(window->shell_surface,
797 window->parent->shell_surface,
798 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500800 case TYPE_MENU:
801 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 case TYPE_CUSTOM:
803 break;
804 }
805}
806
807static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500808window_attach_surface(struct window *window)
809{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400810 struct display *display = window->display;
811 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400812 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500816 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400818 if (display->shell)
819 window_set_type(window);
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
832 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 case WINDOW_BUFFER_TYPE_SHM:
835 if (window->pending_surface != NULL)
836 return;
837
838 window->pending_surface = window->cairo_surface;
839 window->cairo_surface = NULL;
840
841 buffer =
842 display_get_buffer_for_surface(display,
843 window->pending_surface);
844
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200845 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 wl_surface_attach(window->surface, buffer, x, y);
847 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400848 cb = wl_display_sync(display->display);
849 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000851 default:
852 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500854
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500855 wl_surface_damage(window->surface, 0, 0,
856 window->allocation.width,
857 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500858}
859
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500860void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400861window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500862{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100863 if (window->cairo_surface) {
864 switch (window->buffer_type) {
865 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
866 case WINDOW_BUFFER_TYPE_SHM:
867 display_surface_damage(window->display,
868 window->cairo_surface,
869 0, 0,
870 window->allocation.width,
871 window->allocation.height);
872 break;
873 default:
874 break;
875 }
876 window_attach_surface(window);
877 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500878}
879
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400880void
881window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500883 cairo_surface_reference(surface);
884
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400885 if (window->cairo_surface != NULL)
886 cairo_surface_destroy(window->cairo_surface);
887
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500888 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889}
890
Benjamin Franzke22d54812011-07-16 19:50:32 +0000891#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892static void
893window_resize_cairo_window_surface(struct window *window)
894{
895 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200896 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897
898 data = cairo_surface_get_user_data(window->cairo_surface,
899 &surface_data_key);
900
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200901 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902 wl_egl_window_resize(data->window,
903 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 window->allocation.height,
905 x,y);
906
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907 cairo_gl_surface_set_size(window->cairo_surface,
908 window->allocation.width,
909 window->allocation.height);
910}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000911#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400913struct display *
914window_get_display(struct window *window)
915{
916 return window->display;
917}
918
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400919void
920window_create_surface(struct window *window)
921{
922 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400923 uint32_t flags = 0;
924
925 if (!window->transparent)
926 flags = SURFACE_OPAQUE;
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500929#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
931 if (window->cairo_surface) {
932 window_resize_cairo_window_surface(window);
933 return;
934 }
935 surface = display_create_surface(window->display,
936 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400937 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500939 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400940 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100941 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400942 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943 break;
944#endif
945 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400946 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400947 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400948 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800949 default:
950 surface = NULL;
951 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953
954 window_set_surface(window, surface);
955 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956}
957
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200958static void frame_destroy(struct frame *frame);
959
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400960void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500961window_destroy(struct window *window)
962{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200963 struct display *display = window->display;
964 struct input *input;
965
966 if (window->redraw_scheduled)
967 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500968 if (window->resize_scheduled)
969 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200970
971 wl_list_for_each(input, &display->input_list, link) {
972 if (input->pointer_focus == window)
973 input->pointer_focus = NULL;
974 if (input->keyboard_focus == window)
975 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500976 if (input->focus_widget &&
977 input->focus_widget->window == window)
978 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200979 }
980
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200981 if (window->frame)
982 frame_destroy(window->frame);
983
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200984 if (window->shell_surface)
985 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500986 wl_surface_destroy(window->surface);
987 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200988
989 if (window->cairo_surface != NULL)
990 cairo_surface_destroy(window->cairo_surface);
991 if (window->pending_surface != NULL)
992 cairo_surface_destroy(window->pending_surface);
993
994 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500995 free(window);
996}
997
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500999widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001001 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001003 wl_list_for_each(child, &widget->child_list, link) {
1004 target = widget_find_widget(child, x, y);
1005 if (target)
1006 return target;
1007 }
1008
1009 if (widget->allocation.x <= x &&
1010 x < widget->allocation.x + widget->allocation.width &&
1011 widget->allocation.y <= y &&
1012 y < widget->allocation.y + widget->allocation.height) {
1013 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014 }
1015
1016 return NULL;
1017}
1018
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019static struct widget *
1020widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001022 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024 widget = malloc(sizeof *widget);
1025 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001026 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001028 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001029 wl_list_init(&widget->child_list);
1030
1031 return widget;
1032}
1033
1034struct widget *
1035window_add_widget(struct window *window, void *data)
1036{
1037 window->widget = widget_create(window, data);
1038 wl_list_init(&window->widget->link);
1039
1040 return window->widget;
1041}
1042
1043struct widget *
1044widget_add_widget(struct widget *parent, void *data)
1045{
1046 struct widget *widget;
1047
1048 widget = widget_create(parent->window, data);
1049 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001050
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001051 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001052}
1053
1054void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001055widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001057 struct display *display = widget->window->display;
1058 struct input *input;
1059
1060 wl_list_for_each(input, &display->input_list, link) {
1061 if (input->focus_widget == widget)
1062 input->focus_widget = NULL;
1063 }
1064
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001065 wl_list_remove(&widget->link);
1066 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067}
1068
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
1075void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001076widget_set_size(struct widget *widget, int32_t width, int32_t height)
1077{
1078 struct window *window = widget->window;
1079
1080 widget->allocation.width = width;
1081 widget->allocation.height = height;
1082
1083 window->allocation.x = 0;
1084 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001085 window->allocation.width = width;
1086 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001087}
1088
1089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_set_allocation(struct widget *widget,
1091 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001093 widget->allocation.x = x;
1094 widget->allocation.y = y;
1095 widget->allocation.width = width;
1096 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097}
1098
1099void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103}
1104
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001105void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001106widget_set_resize_handler(struct widget *widget,
1107 widget_resize_handler_t handler)
1108{
1109 widget->resize_handler = handler;
1110}
1111
1112void
1113widget_set_redraw_handler(struct widget *widget,
1114 widget_redraw_handler_t handler)
1115{
1116 widget->redraw_handler = handler;
1117}
1118
1119void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001120widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001121{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001122 widget->enter_handler = handler;
1123}
1124
1125void
1126widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1127{
1128 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001129}
1130
1131void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001132widget_set_motion_handler(struct widget *widget,
1133 widget_motion_handler_t handler)
1134{
1135 widget->motion_handler = handler;
1136}
1137
1138void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001139widget_set_button_handler(struct widget *widget,
1140 widget_button_handler_t handler)
1141{
1142 widget->button_handler = handler;
1143}
1144
1145void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001146widget_schedule_redraw(struct widget *widget)
1147{
1148 window_schedule_redraw(widget->window);
1149}
1150
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151cairo_surface_t *
1152window_get_surface(struct window *window)
1153{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001154 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001155}
1156
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001157struct wl_surface *
1158window_get_wl_surface(struct window *window)
1159{
1160 return window->surface;
1161}
1162
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163struct wl_shell_surface *
1164window_get_wl_shell_surface(struct window *window)
1165{
1166 return window->shell_surface;
1167}
1168
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169static void
1170frame_resize_handler(struct widget *widget,
1171 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001172{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001173 struct frame *frame = data;
1174 struct widget *child = frame->child;
1175 struct rectangle allocation;
1176 int decoration_width, decoration_height;
1177
1178 decoration_width = 20 + frame->margin * 2;
1179 decoration_height = 60 + frame->margin * 2;
1180
1181 allocation.x = 10 + frame->margin;
1182 allocation.y = 50 + frame->margin;
1183 allocation.width = width - decoration_width;
1184 allocation.height = height - decoration_height;
1185
1186 widget_set_allocation(child, allocation.x, allocation.y,
1187 allocation.width, allocation.height);
1188
1189 if (child->resize_handler)
1190 child->resize_handler(child,
1191 allocation.width,
1192 allocation.height,
1193 child->user_data);
1194
1195 widget_set_allocation(widget, 0, 0,
1196 child->allocation.width + decoration_width,
1197 child->allocation.height + decoration_height);
1198}
1199
1200static void
1201frame_redraw_handler(struct widget *widget, void *data)
1202{
1203 struct frame *frame = data;
1204 cairo_t *cr;
1205 cairo_text_extents_t extents;
1206 cairo_surface_t *source;
1207 int width, height, shadow_dx = 3, shadow_dy = 3;
1208 struct window *window = widget->window;
1209
1210 width = widget->allocation.width;
1211 height = widget->allocation.height;
1212
1213 cr = cairo_create(window->cairo_surface);
1214
1215 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1216 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1217 cairo_paint(cr);
1218
1219 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1220 tile_mask(cr, window->display->shadow,
1221 shadow_dx, shadow_dy, width, height,
1222 frame->margin + 10 - shadow_dx,
1223 frame->margin + 10 - shadow_dy);
1224
1225 if (window->keyboard_device)
1226 source = window->display->active_frame;
1227 else
1228 source = window->display->inactive_frame;
1229
1230 tile_source(cr, source, 0, 0, width, height,
1231 frame->margin + 10, frame->margin + 50);
1232
1233 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1234 cairo_set_font_size(cr, 14);
1235 cairo_text_extents(cr, window->title, &extents);
1236 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1237 if (window->keyboard_device)
1238 cairo_set_source_rgb(cr, 0, 0, 0);
1239 else
1240 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1241 cairo_show_text(cr, window->title);
1242
1243 cairo_destroy(cr);
1244}
1245
1246static int
1247frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1248{
1249 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251 const int grip_size = 8;
1252
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001257 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001258 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260 hlocation = WINDOW_RESIZING_RIGHT;
1261 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001268 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 vlocation = WINDOW_RESIZING_BOTTOM;
1272 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275 location = vlocation | hlocation;
1276 if (location & WINDOW_EXTERIOR)
1277 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001278 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001279 location = WINDOW_TITLEBAR;
1280 else if (location == WINDOW_INTERIOR)
1281 location = WINDOW_CLIENT_AREA;
1282
1283 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284}
1285
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001289 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292 switch (location) {
1293 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001305 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001308 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001309 case WINDOW_EXTERIOR:
1310 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001311 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001312 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001313 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314}
1315
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001316static void
1317frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001318{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001319 switch (index) {
1320 case 0: /* close */
1321 if (window->close_handler)
1322 window->close_handler(window->parent,
1323 window->user_data);
1324 else
1325 display_exit(window->display);
1326 break;
1327 case 1: /* fullscreen */
1328 /* we don't have a way to get out of fullscreen for now */
1329 window_set_fullscreen(window, 1);
1330 break;
1331 case 2: /* rotate */
1332 case 3: /* scale */
1333 break;
1334 }
1335}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001336
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001337void
1338window_show_frame_menu(struct window *window,
1339 struct input *input, uint32_t time)
1340{
1341 int32_t x, y;
1342
1343 static const char *entries[] = {
1344 "Close", "Fullscreen", "Rotate", "Scale"
1345 };
1346
1347 input_get_position(input, &x, &y);
1348 window_show_menu(window->display, input, time, window,
1349 x - 10, y - 10, frame_menu_func, entries, 4);
1350}
1351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001352static int
1353frame_enter_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
1355 int32_t x, int32_t y, void *data)
1356{
1357 return frame_get_pointer_image_for_location(data, input);
1358}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001359
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001360static int
1361frame_motion_handler(struct widget *widget,
1362 struct input *input, uint32_t time,
1363 int32_t x, int32_t y, void *data)
1364{
1365 return frame_get_pointer_image_for_location(data, input);
1366}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001367
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001368static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001369frame_button_handler(struct widget *widget,
1370 struct input *input, uint32_t time,
1371 int button, int state, void *data)
1372
1373{
1374 struct frame *frame = data;
1375 struct window *window = widget->window;
1376 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001377 location = frame_get_pointer_location(frame, input->sx, input->sy);
1378
1379 if (window->display->shell && button == BTN_LEFT && state == 1) {
1380 switch (location) {
1381 case WINDOW_TITLEBAR:
1382 if (!window->shell_surface)
1383 break;
1384 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001385 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386 wl_shell_surface_move(window->shell_surface,
1387 input_get_input_device(input),
1388 time);
1389 break;
1390 case WINDOW_RESIZING_TOP:
1391 case WINDOW_RESIZING_BOTTOM:
1392 case WINDOW_RESIZING_LEFT:
1393 case WINDOW_RESIZING_RIGHT:
1394 case WINDOW_RESIZING_TOP_LEFT:
1395 case WINDOW_RESIZING_TOP_RIGHT:
1396 case WINDOW_RESIZING_BOTTOM_LEFT:
1397 case WINDOW_RESIZING_BOTTOM_RIGHT:
1398 if (!window->shell_surface)
1399 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001400 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001401 wl_shell_surface_resize(window->shell_surface,
1402 input_get_input_device(input),
1403 time, location);
1404 break;
1405 }
1406 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001407 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001408 }
1409}
1410
1411struct widget *
1412frame_create(struct window *window, void *data)
1413{
1414 struct frame *frame;
1415
1416 frame = malloc(sizeof *frame);
1417 memset(frame, 0, sizeof *frame);
1418
1419 frame->widget = window_add_widget(window, frame);
1420 frame->child = widget_add_widget(frame->widget, data);
1421 frame->margin = 16;
1422
1423 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1424 widget_set_resize_handler(frame->widget, frame_resize_handler);
1425 widget_set_enter_handler(frame->widget, frame_enter_handler);
1426 widget_set_motion_handler(frame->widget, frame_motion_handler);
1427 widget_set_button_handler(frame->widget, frame_button_handler);
1428
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001429 window->frame = frame;
1430
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001432}
1433
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001434static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001435frame_destroy(struct frame *frame)
1436{
1437 /* frame->child must be destroyed by the application */
1438 widget_destroy(frame->widget);
1439 free(frame);
1440}
1441
1442static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001443input_set_focus_widget(struct input *input, struct widget *focus,
1444 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001445{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001446 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001447 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001449 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001450 return;
1451
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001452 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001453 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001454 widget = old;
1455 if (input->grab)
1456 widget = input->grab;
1457 if (widget->leave_handler)
1458 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001459 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001460 }
1461
1462 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001463 widget = focus;
1464 if (input->grab)
1465 widget = input->grab;
1466 if (widget->enter_handler)
1467 pointer = widget->enter_handler(focus, input, time,
1468 x, y,
1469 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001470 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001471
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001472 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001473 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001474}
1475
1476static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001477input_handle_motion(void *data, struct wl_input_device *input_device,
1478 uint32_t time,
1479 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001480{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001481 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001482 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001483 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001484 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001485
1486 input->x = x;
1487 input->y = y;
1488 input->sx = sx;
1489 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001490
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001491 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001492 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001493 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001494 }
1495
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001496 if (input->grab)
1497 widget = input->grab;
1498 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001499 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001500 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001501 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001502 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001503 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001504
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001505 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001506}
1507
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001508void
1509input_grab(struct input *input, struct widget *widget, uint32_t button)
1510{
1511 input->grab = widget;
1512 input->grab_button = button;
1513}
1514
1515void
1516input_ungrab(struct input *input, uint32_t time)
1517{
1518 struct widget *widget;
1519
1520 input->grab = NULL;
1521 if (input->pointer_focus) {
1522 widget = widget_find_widget(input->pointer_focus->widget,
1523 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001524 input_set_focus_widget(input, widget,
1525 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001526 }
1527}
1528
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001529static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001530input_handle_button(void *data,
1531 struct wl_input_device *input_device,
1532 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001533{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001534 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001535 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001536
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001537 if (input->focus_widget && input->grab == NULL && state)
1538 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539
Neil Roberts6b28aad2012-01-23 19:11:18 +00001540 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001542 (*widget->button_handler)(widget,
1543 input, time,
1544 button, state,
1545 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001546
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001547 if (input->grab && input->grab_button == button && !state)
1548 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001549}
1550
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001551static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001552input_handle_key(void *data, struct wl_input_device *input_device,
1553 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001554{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001555 struct input *input = data;
1556 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001557 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001558 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001559
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001560 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001561 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001562 return;
1563
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001564 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001565 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001566 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1567 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001568
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001569 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1570
1571 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001572 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001573 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001574 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001575
1576 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001577 (*window->key_handler)(window, input, time, key, sym, state,
1578 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001579}
1580
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001581static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001582input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001583{
1584 struct window *window = input->pointer_focus;
1585
1586 if (!window)
1587 return;
1588
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001589 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001590
Pekka Paalanene1207c72011-12-16 12:02:09 +02001591 input->pointer_focus = NULL;
1592 input->current_pointer_image = POINTER_UNSET;
1593}
1594
1595static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001596input_handle_pointer_focus(void *data,
1597 struct wl_input_device *input_device,
1598 uint32_t time, struct wl_surface *surface,
1599 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001600{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001601 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001602 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001603 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001604
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001605 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001606 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001607 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001608
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001609 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001610 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001611 window = input->pointer_focus;
1612
Kristian Høgsberg59826582011-01-20 11:56:57 -05001613 input->x = x;
1614 input->y = y;
1615 input->sx = sx;
1616 input->sy = sy;
1617
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001619 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001620 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001621}
1622
1623static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001624input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001625{
1626 struct window *window = input->keyboard_focus;
1627
1628 if (!window)
1629 return;
1630
1631 window->keyboard_device = NULL;
1632 if (window->keyboard_focus_handler)
1633 (*window->keyboard_focus_handler)(window, NULL,
1634 window->user_data);
1635
1636 input->keyboard_focus = NULL;
1637}
1638
1639static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001640input_handle_keyboard_focus(void *data,
1641 struct wl_input_device *input_device,
1642 uint32_t time,
1643 struct wl_surface *surface,
1644 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001645{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001646 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001647 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001648 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001649 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001650
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001651 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001652
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001653 if (surface)
1654 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001655
1656 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001657 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001658 for (k = keys->data; k < end; k++)
1659 input->modifiers |= d->xkb->map->modmap[*k];
1660
1661 window = input->keyboard_focus;
1662 if (window) {
1663 window->keyboard_device = input;
1664 if (window->keyboard_focus_handler)
1665 (*window->keyboard_focus_handler)(window,
1666 window->keyboard_device,
1667 window->user_data);
1668 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001669}
1670
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001671static void
1672input_handle_touch_down(void *data,
1673 struct wl_input_device *wl_input_device,
1674 uint32_t time, struct wl_surface *surface,
1675 int32_t id, int32_t x, int32_t y)
1676{
1677}
1678
1679static void
1680input_handle_touch_up(void *data,
1681 struct wl_input_device *wl_input_device,
1682 uint32_t time, int32_t id)
1683{
1684}
1685
1686static void
1687input_handle_touch_motion(void *data,
1688 struct wl_input_device *wl_input_device,
1689 uint32_t time, int32_t id, int32_t x, int32_t y)
1690{
1691}
1692
1693static void
1694input_handle_touch_frame(void *data,
1695 struct wl_input_device *wl_input_device)
1696{
1697}
1698
1699static void
1700input_handle_touch_cancel(void *data,
1701 struct wl_input_device *wl_input_device)
1702{
1703}
1704
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001705static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001706 input_handle_motion,
1707 input_handle_button,
1708 input_handle_key,
1709 input_handle_pointer_focus,
1710 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001711 input_handle_touch_down,
1712 input_handle_touch_up,
1713 input_handle_touch_motion,
1714 input_handle_touch_frame,
1715 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001716};
1717
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001718void
1719input_get_position(struct input *input, int32_t *x, int32_t *y)
1720{
1721 *x = input->sx;
1722 *y = input->sy;
1723}
1724
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001725struct wl_input_device *
1726input_get_input_device(struct input *input)
1727{
1728 return input->input_device;
1729}
1730
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001731uint32_t
1732input_get_modifiers(struct input *input)
1733{
1734 return input->modifiers;
1735}
1736
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001737struct widget *
1738input_get_focus_widget(struct input *input)
1739{
1740 return input->focus_widget;
1741}
1742
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001743struct data_offer {
1744 struct wl_data_offer *offer;
1745 struct input *input;
1746 struct wl_array types;
1747 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001748
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001749 struct task io_task;
1750 int fd;
1751 data_func_t func;
1752 int32_t x, y;
1753 void *user_data;
1754};
1755
1756static void
1757data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1758{
1759 struct data_offer *offer = data;
1760 char **p;
1761
1762 p = wl_array_add(&offer->types, sizeof *p);
1763 *p = strdup(type);
1764}
1765
1766static const struct wl_data_offer_listener data_offer_listener = {
1767 data_offer_offer,
1768};
1769
1770static void
1771data_offer_destroy(struct data_offer *offer)
1772{
1773 char **p;
1774
1775 offer->refcount--;
1776 if (offer->refcount == 0) {
1777 wl_data_offer_destroy(offer->offer);
1778 for (p = offer->types.data; *p; p++)
1779 free(*p);
1780 wl_array_release(&offer->types);
1781 free(offer);
1782 }
1783}
1784
1785static void
1786data_device_data_offer(void *data,
1787 struct wl_data_device *data_device, uint32_t id)
1788{
1789 struct data_offer *offer;
1790
1791 offer = malloc(sizeof *offer);
1792
1793 wl_array_init(&offer->types);
1794 offer->refcount = 1;
1795 offer->input = data;
1796
1797 /* FIXME: Generate typesafe wrappers for this */
1798 offer->offer = (struct wl_data_offer *)
1799 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1800 id, &wl_data_offer_interface);
1801
1802 wl_data_offer_add_listener(offer->offer,
1803 &data_offer_listener, offer);
1804}
1805
1806static void
1807data_device_enter(void *data, struct wl_data_device *data_device,
1808 uint32_t time, struct wl_surface *surface,
1809 int32_t x, int32_t y, struct wl_data_offer *offer)
1810{
1811 struct input *input = data;
1812 struct window *window;
1813 char **p;
1814
1815 input->drag_offer = wl_data_offer_get_user_data(offer);
1816 window = wl_surface_get_user_data(surface);
1817 input->pointer_focus = window;
1818
1819 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1820 *p = NULL;
1821
1822 window = input->pointer_focus;
1823 if (window->data_handler)
1824 window->data_handler(window, input, time, x, y,
1825 input->drag_offer->types.data,
1826 window->user_data);
1827}
1828
1829static void
1830data_device_leave(void *data, struct wl_data_device *data_device)
1831{
1832 struct input *input = data;
1833
1834 data_offer_destroy(input->drag_offer);
1835 input->drag_offer = NULL;
1836}
1837
1838static void
1839data_device_motion(void *data, struct wl_data_device *data_device,
1840 uint32_t time, int32_t x, int32_t y)
1841{
1842 struct input *input = data;
1843 struct window *window = input->pointer_focus;
1844
1845 input->sx = x;
1846 input->sy = y;
1847
1848 if (window->data_handler)
1849 window->data_handler(window, input, time, x, y,
1850 input->drag_offer->types.data,
1851 window->user_data);
1852}
1853
1854static void
1855data_device_drop(void *data, struct wl_data_device *data_device)
1856{
1857 struct input *input = data;
1858 struct window *window = input->pointer_focus;
1859
1860 if (window->drop_handler)
1861 window->drop_handler(window, input,
1862 input->sx, input->sy, window->user_data);
1863}
1864
1865static void
1866data_device_selection(void *data,
1867 struct wl_data_device *wl_data_device,
1868 struct wl_data_offer *offer)
1869{
1870 struct input *input = data;
1871 char **p;
1872
1873 if (input->selection_offer)
1874 data_offer_destroy(input->selection_offer);
1875
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001876 if (offer) {
1877 input->selection_offer = wl_data_offer_get_user_data(offer);
1878 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1879 *p = NULL;
1880 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001881}
1882
1883static const struct wl_data_device_listener data_device_listener = {
1884 data_device_data_offer,
1885 data_device_enter,
1886 data_device_leave,
1887 data_device_motion,
1888 data_device_drop,
1889 data_device_selection
1890};
1891
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001892void
1893input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1894{
1895 struct display *display = input->display;
1896 struct wl_buffer *buffer;
1897 cairo_surface_t *surface;
1898
1899 if (pointer == input->current_pointer_image)
1900 return;
1901
1902 input->current_pointer_image = pointer;
1903 surface = display->pointer_surfaces[pointer];
1904
1905 if (!surface)
1906 return;
1907
1908 buffer = display_get_buffer_for_surface(display, surface);
1909 wl_input_device_attach(input->input_device, time, buffer,
1910 pointer_images[pointer].hotspot_x,
1911 pointer_images[pointer].hotspot_y);
1912}
1913
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001914struct wl_data_device *
1915input_get_data_device(struct input *input)
1916{
1917 return input->data_device;
1918}
1919
1920void
1921input_set_selection(struct input *input,
1922 struct wl_data_source *source, uint32_t time)
1923{
1924 wl_data_device_set_selection(input->data_device, source, time);
1925}
1926
1927void
1928input_accept(struct input *input, uint32_t time, const char *type)
1929{
1930 wl_data_offer_accept(input->drag_offer->offer, time, type);
1931}
1932
1933static void
1934offer_io_func(struct task *task, uint32_t events)
1935{
1936 struct data_offer *offer =
1937 container_of(task, struct data_offer, io_task);
1938 unsigned int len;
1939 char buffer[4096];
1940
1941 len = read(offer->fd, buffer, sizeof buffer);
1942 offer->func(buffer, len,
1943 offer->x, offer->y, offer->user_data);
1944
1945 if (len == 0) {
1946 close(offer->fd);
1947 data_offer_destroy(offer);
1948 }
1949}
1950
1951static void
1952data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1953 data_func_t func, void *user_data)
1954{
1955 int p[2];
1956
1957 pipe2(p, O_CLOEXEC);
1958 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1959 close(p[1]);
1960
1961 offer->io_task.run = offer_io_func;
1962 offer->fd = p[0];
1963 offer->func = func;
1964 offer->refcount++;
1965 offer->user_data = user_data;
1966
1967 display_watch_fd(offer->input->display,
1968 offer->fd, EPOLLIN, &offer->io_task);
1969}
1970
1971void
1972input_receive_drag_data(struct input *input, const char *mime_type,
1973 data_func_t func, void *data)
1974{
1975 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1976 input->drag_offer->x = input->sx;
1977 input->drag_offer->y = input->sy;
1978}
1979
1980int
1981input_receive_selection_data(struct input *input, const char *mime_type,
1982 data_func_t func, void *data)
1983{
1984 char **p;
1985
1986 if (input->selection_offer == NULL)
1987 return -1;
1988
1989 for (p = input->selection_offer->types.data; *p; p++)
1990 if (strcmp(mime_type, *p) == 0)
1991 break;
1992
1993 if (*p == NULL)
1994 return -1;
1995
1996 data_offer_receive_data(input->selection_offer,
1997 mime_type, func, data);
1998 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001999}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002000
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002001int
2002input_receive_selection_data_to_fd(struct input *input,
2003 const char *mime_type, int fd)
2004{
2005 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2006
2007 return 0;
2008}
2009
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002010void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002011window_move(struct window *window, struct input *input, uint32_t time)
2012{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002013 if (!window->shell_surface)
2014 return;
2015
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002016 wl_shell_surface_move(window->shell_surface,
2017 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002018}
2019
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002020static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002021window_resize(struct window *window, int32_t width, int32_t height)
2022{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002023 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002024 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002025
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002026 allocation.x = 0;
2027 allocation.y = 0;
2028 allocation.width = width;
2029 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002030
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002031 widget = window->widget;
2032 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002033 allocation.width, allocation.height);
2034
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002035 if (widget->resize_handler)
2036 widget->resize_handler(widget,
2037 allocation.width,
2038 allocation.height,
2039 widget->user_data);
2040
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002041 if (window->allocation.width != widget->allocation.width ||
2042 window->allocation.height != widget->allocation.height) {
2043 window->allocation = widget->allocation;
2044 window_schedule_redraw(window);
2045 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002046}
2047
2048static void
2049idle_resize(struct task *task, uint32_t events)
2050{
2051 struct window *window =
2052 container_of(task, struct window, resize_task);
2053
2054 window_resize(window,
2055 window->allocation.width, window->allocation.height);
2056 window->resize_scheduled = 0;
2057}
2058
2059void
2060window_schedule_resize(struct window *window, int width, int height)
2061{
2062 if (!window->resize_scheduled) {
2063 window->allocation.width = width;
2064 window->allocation.height = height;
2065 window->resize_task.run = idle_resize;
2066 display_defer(window->display, &window->resize_task);
2067 window->resize_scheduled = 1;
2068 }
2069}
2070
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002071void
2072widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2073{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002074 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002075}
2076
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002077static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002078handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002079 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002080 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002081{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002083
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002084 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002085 return;
2086
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002087 window->resize_edges = edges;
2088
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002089 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002090}
2091
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002092static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002093menu_destroy(struct menu *menu)
2094{
2095 widget_destroy(menu->widget);
2096 window_destroy(menu->window);
2097 free(menu);
2098}
2099
2100static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002101handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2102{
2103 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002104 struct menu *menu = window->widget->user_data;
2105
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002106 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002107 * device. Or just use wl_callback. And this really needs to
2108 * be a window vfunc that the menu can set. And we need the
2109 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002110
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002111 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002112 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002113 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002114}
2115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002117 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002118 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002119};
2120
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002121void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002122window_get_allocation(struct window *window,
2123 struct rectangle *allocation)
2124{
2125 *allocation = window->allocation;
2126}
2127
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002128static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002129widget_redraw(struct widget *widget)
2130{
2131 struct widget *child;
2132
2133 if (widget->redraw_handler)
2134 widget->redraw_handler(widget, widget->user_data);
2135 wl_list_for_each(child, &widget->child_list, link)
2136 widget_redraw(child);
2137}
2138
2139static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002140idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002141{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002142 struct window *window =
2143 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002144
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002145 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002146 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002147 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002148 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002149}
2150
2151void
2152window_schedule_redraw(struct window *window)
2153{
2154 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002155 window->redraw_task.run = idle_redraw;
2156 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002157 window->redraw_scheduled = 1;
2158 }
2159}
2160
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002161void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002162window_set_custom(struct window *window)
2163{
2164 window->type = TYPE_CUSTOM;
2165}
2166
2167void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002168window_set_fullscreen(struct window *window, int fullscreen)
2169{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002170 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002171 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002172
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002173 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002174 return;
2175
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002176 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002177 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002178 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002179 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002180 width = output->allocation.width;
2181 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002182 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002183 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184 width = window->saved_allocation.width;
2185 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002186 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002187
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002188 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002189}
2190
2191void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002192window_set_user_data(struct window *window, void *data)
2193{
2194 window->user_data = data;
2195}
2196
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002197void *
2198window_get_user_data(struct window *window)
2199{
2200 return window->user_data;
2201}
2202
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002203void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002204window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002205 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002206{
2207 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002208}
2209
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002210void
2211window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002212 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002213{
2214 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002215}
2216
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002217void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002218window_set_data_handler(struct window *window, window_data_handler_t handler)
2219{
2220 window->data_handler = handler;
2221}
2222
2223void
2224window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2225{
2226 window->drop_handler = handler;
2227}
2228
2229void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002230window_set_close_handler(struct window *window,
2231 window_close_handler_t handler)
2232{
2233 window->close_handler = handler;
2234}
2235
2236void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002237window_set_transparent(struct window *window, int transparent)
2238{
2239 window->transparent = transparent;
2240}
2241
2242void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002243window_set_title(struct window *window, const char *title)
2244{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002245 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002246 window->title = strdup(title);
2247}
2248
2249const char *
2250window_get_title(struct window *window)
2251{
2252 return window->title;
2253}
2254
2255void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002256display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2257 int32_t x, int32_t y, int32_t width, int32_t height)
2258{
2259 struct wl_buffer *buffer;
2260
2261 buffer = display_get_buffer_for_surface(display, cairo_surface);
2262
2263 wl_buffer_damage(buffer, x, y, width, height);
2264}
2265
2266void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002267window_damage(struct window *window, int32_t x, int32_t y,
2268 int32_t width, int32_t height)
2269{
2270 wl_surface_damage(window->surface, x, y, width, height);
2271}
2272
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002273static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002274window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002275{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002276 struct window *window;
2277
2278 window = malloc(sizeof *window);
2279 if (window == NULL)
2280 return NULL;
2281
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002282 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002283 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002284 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002285 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002286 if (display->shell) {
2287 window->shell_surface =
2288 wl_shell_get_shell_surface(display->shell,
2289 window->surface);
2290 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002291 window->allocation.x = 0;
2292 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002293 window->allocation.width = 0;
2294 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002295 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002296 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002297
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002298 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002299#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002300 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2301 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002302#else
2303 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2304#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002305 else
2306 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002307
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002308 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002309 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002310
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002311 if (window->shell_surface) {
2312 wl_shell_surface_set_user_data(window->shell_surface, window);
2313 wl_shell_surface_add_listener(window->shell_surface,
2314 &shell_surface_listener, window);
2315 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002316
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002317 return window;
2318}
2319
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002320struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002321window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002322{
2323 struct window *window;
2324
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002325 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002326 if (!window)
2327 return NULL;
2328
2329 return window;
2330}
2331
2332struct window *
2333window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002334 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002335{
2336 struct window *window;
2337
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002338 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002339 if (!window)
2340 return NULL;
2341
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002342 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002343 window->x = x;
2344 window->y = y;
2345
2346 return window;
2347}
2348
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002349static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002350menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351{
2352 int next;
2353
2354 next = (sy - 8) / 20;
2355 if (menu->current != next) {
2356 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002357 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002358 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002359}
2360
2361static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002362menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002363 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002364 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002365{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002366 struct menu *menu = data;
2367
2368 if (widget == menu->widget)
2369 menu_set_item(data, y);
2370
2371 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002372}
2373
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002374static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002375menu_enter_handler(struct widget *widget,
2376 struct input *input, uint32_t time,
2377 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002378{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002379 struct menu *menu = data;
2380
2381 if (widget == menu->widget)
2382 menu_set_item(data, y);
2383
2384 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002385}
2386
2387static void
2388menu_leave_handler(struct widget *widget, struct input *input, void *data)
2389{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002390 struct menu *menu = data;
2391
2392 if (widget == menu->widget)
2393 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002394}
2395
2396static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002397menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002398 struct input *input, uint32_t time,
2399 int button, int state, void *data)
2400
2401{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002402 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002403
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002404 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002405 /* Either relase after press-drag-release or
2406 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002407 menu->func(menu->window->parent,
2408 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002409 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002410 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002411 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002412}
2413
2414static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002415menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002416{
2417 cairo_t *cr;
2418 const int32_t r = 3, margin = 3;
2419 struct menu *menu = data;
2420 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002421 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423 cr = cairo_create(window->cairo_surface);
2424 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2425 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2426 cairo_paint(cr);
2427
2428 width = window->allocation.width;
2429 height = window->allocation.height;
2430 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002431
2432 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2434 cairo_fill(cr);
2435
2436 for (i = 0; i < menu->count; i++) {
2437 if (i == menu->current) {
2438 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2439 cairo_rectangle(cr, margin, i * 20 + margin,
2440 width - 2 * margin, 20);
2441 cairo_fill(cr);
2442 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2443 cairo_move_to(cr, 10, i * 20 + 16);
2444 cairo_show_text(cr, menu->entries[i]);
2445 } else {
2446 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2447 cairo_move_to(cr, 10, i * 20 + 16);
2448 cairo_show_text(cr, menu->entries[i]);
2449 }
2450 }
2451
2452 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002453}
2454
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002455void
2456window_show_menu(struct display *display,
2457 struct input *input, uint32_t time, struct window *parent,
2458 int32_t x, int32_t y,
2459 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002460{
2461 struct window *window;
2462 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002463 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002464
2465 menu = malloc(sizeof *menu);
2466 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002467 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002468
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002469 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002470 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002471 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002472
2473 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002474 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002475 menu->entries = entries;
2476 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002477 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002478 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002479 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002480 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002481 window->type = TYPE_MENU;
2482 window->x = x;
2483 window->y = y;
2484
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002485 wl_shell_surface_set_popup(window->shell_surface,
2486 input->input_device, time,
2487 window->parent->shell_surface,
2488 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002489
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002490 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002491 widget_set_enter_handler(menu->widget, menu_enter_handler);
2492 widget_set_leave_handler(menu->widget, menu_leave_handler);
2493 widget_set_motion_handler(menu->widget, menu_motion_handler);
2494 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002495
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002497 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002498}
2499
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002500void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002501window_set_buffer_type(struct window *window, enum window_buffer_type type)
2502{
2503 window->buffer_type = type;
2504}
2505
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002506
2507static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002508display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002509 struct wl_output *wl_output,
2510 int x, int y,
2511 int physical_width,
2512 int physical_height,
2513 int subpixel,
2514 const char *make,
2515 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002516{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002517 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002518
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002519 output->allocation.x = x;
2520 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002521}
2522
2523static void
2524display_handle_mode(void *data,
2525 struct wl_output *wl_output,
2526 uint32_t flags,
2527 int width,
2528 int height,
2529 int refresh)
2530{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002531 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002532 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002533
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534 if (flags & WL_OUTPUT_MODE_CURRENT) {
2535 output->allocation.width = width;
2536 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002537 if (display->output_configure_handler)
2538 (*display->output_configure_handler)(
2539 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002540 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002541}
2542
2543static const struct wl_output_listener output_listener = {
2544 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002545 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002546};
2547
2548static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002549display_add_output(struct display *d, uint32_t id)
2550{
2551 struct output *output;
2552
2553 output = malloc(sizeof *output);
2554 if (output == NULL)
2555 return;
2556
2557 memset(output, 0, sizeof *output);
2558 output->display = d;
2559 output->output =
2560 wl_display_bind(d->display, id, &wl_output_interface);
2561 wl_list_insert(d->output_list.prev, &output->link);
2562
2563 wl_output_add_listener(output->output, &output_listener, output);
2564}
2565
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002566static void
2567output_destroy(struct output *output)
2568{
2569 if (output->destroy_handler)
2570 (*output->destroy_handler)(output, output->user_data);
2571
2572 wl_output_destroy(output->output);
2573 wl_list_remove(&output->link);
2574 free(output);
2575}
2576
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002577void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002578display_set_output_configure_handler(struct display *display,
2579 display_output_handler_t handler)
2580{
2581 struct output *output;
2582
2583 display->output_configure_handler = handler;
2584 if (!handler)
2585 return;
2586
2587 wl_list_for_each(output, &display->output_list, link)
2588 (*display->output_configure_handler)(output,
2589 display->user_data);
2590}
2591
2592void
2593output_set_user_data(struct output *output, void *data)
2594{
2595 output->user_data = data;
2596}
2597
2598void *
2599output_get_user_data(struct output *output)
2600{
2601 return output->user_data;
2602}
2603
2604void
2605output_set_destroy_handler(struct output *output,
2606 display_output_handler_t handler)
2607{
2608 output->destroy_handler = handler;
2609 /* FIXME: implement this, once we have way to remove outputs */
2610}
2611
2612void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002613output_get_allocation(struct output *output, struct rectangle *allocation)
2614{
2615 *allocation = output->allocation;
2616}
2617
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002618struct wl_output *
2619output_get_wl_output(struct output *output)
2620{
2621 return output->output;
2622}
2623
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002624static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002625display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002626{
2627 struct input *input;
2628
2629 input = malloc(sizeof *input);
2630 if (input == NULL)
2631 return;
2632
2633 memset(input, 0, sizeof *input);
2634 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002635 input->input_device =
2636 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002637 input->pointer_focus = NULL;
2638 input->keyboard_focus = NULL;
2639 wl_list_insert(d->input_list.prev, &input->link);
2640
2641 wl_input_device_add_listener(input->input_device,
2642 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002643 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002644
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002645 input->data_device =
2646 wl_data_device_manager_get_data_device(d->data_device_manager,
2647 input->input_device);
2648 wl_data_device_add_listener(input->data_device,
2649 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002650}
2651
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002652static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002653input_destroy(struct input *input)
2654{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002655 input_remove_keyboard_focus(input);
2656 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002657
2658 if (input->drag_offer)
2659 data_offer_destroy(input->drag_offer);
2660
2661 if (input->selection_offer)
2662 data_offer_destroy(input->selection_offer);
2663
2664 wl_data_device_destroy(input->data_device);
2665 wl_list_remove(&input->link);
2666 wl_input_device_destroy(input->input_device);
2667 free(input);
2668}
2669
2670static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002671display_handle_global(struct wl_display *display, uint32_t id,
2672 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002673{
2674 struct display *d = data;
2675
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002676 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002677 d->compositor =
2678 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002679 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002680 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002681 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002682 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002683 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002684 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002685 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002686 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002687 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2688 d->data_device_manager =
2689 wl_display_bind(display, id,
2690 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002691 }
2692}
2693
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002694static void
2695display_render_frame(struct display *d)
2696{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002697 int radius = 8;
2698 cairo_t *cr;
2699
2700 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2701 cr = cairo_create(d->shadow);
2702 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2703 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2704 rounded_rect(cr, 16, 16, 112, 112, radius);
2705 cairo_fill(cr);
2706 cairo_destroy(cr);
2707 blur_surface(d->shadow, 64);
2708
2709 d->active_frame =
2710 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2711 cr = cairo_create(d->active_frame);
2712 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2713 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2714 rounded_rect(cr, 16, 16, 112, 112, radius);
2715 cairo_fill(cr);
2716 cairo_destroy(cr);
2717
2718 d->inactive_frame =
2719 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2720 cr = cairo_create(d->inactive_frame);
2721 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2722 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2723 rounded_rect(cr, 16, 16, 112, 112, radius);
2724 cairo_fill(cr);
2725 cairo_destroy(cr);
2726}
2727
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002728static void
2729init_xkb(struct display *d)
2730{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002731 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002732
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002733 names.rules = "evdev";
2734 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002735 names.layout = option_xkb_layout;
2736 names.variant = option_xkb_variant;
2737 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002738
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002739 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002740 if (!d->xkb) {
2741 fprintf(stderr, "Failed to compile keymap\n");
2742 exit(1);
2743 }
2744}
2745
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002746static void
2747fini_xkb(struct display *display)
2748{
2749 xkb_free_keymap(display->xkb);
2750}
2751
Yuval Fledel45568f62010-12-06 09:18:12 -05002752static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002753init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002754{
2755 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002756 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002757
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002758 static const EGLint argb_cfg_attribs[] = {
2759 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002760 EGL_RED_SIZE, 1,
2761 EGL_GREEN_SIZE, 1,
2762 EGL_BLUE_SIZE, 1,
2763 EGL_ALPHA_SIZE, 1,
2764 EGL_DEPTH_SIZE, 1,
2765 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2766 EGL_NONE
2767 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002768
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002769 static const EGLint rgb_cfg_attribs[] = {
2770 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2771 EGL_RED_SIZE, 1,
2772 EGL_GREEN_SIZE, 1,
2773 EGL_BLUE_SIZE, 1,
2774 EGL_ALPHA_SIZE, 0,
2775 EGL_DEPTH_SIZE, 1,
2776 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2777 EGL_NONE
2778 };
2779
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002780#ifdef USE_CAIRO_GLESV2
2781 static const EGLint context_attribs[] = {
2782 EGL_CONTEXT_CLIENT_VERSION, 2,
2783 EGL_NONE
2784 };
2785 EGLint api = EGL_OPENGL_ES_API;
2786#else
2787 EGLint *context_attribs = NULL;
2788 EGLint api = EGL_OPENGL_API;
2789#endif
2790
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002791 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002792 if (!eglInitialize(d->dpy, &major, &minor)) {
2793 fprintf(stderr, "failed to initialize display\n");
2794 return -1;
2795 }
2796
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002797 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002798 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2799 return -1;
2800 }
2801
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002802 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2803 &d->argb_config, 1, &n) || n != 1) {
2804 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002805 return -1;
2806 }
2807
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002808 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2809 &d->rgb_config, 1, &n) || n != 1) {
2810 fprintf(stderr, "failed to choose rgb config\n");
2811 return -1;
2812 }
2813
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002814 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2815 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002816 if (d->rgb_ctx == NULL) {
2817 fprintf(stderr, "failed to create context\n");
2818 return -1;
2819 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002820 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002821 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002822 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002823 fprintf(stderr, "failed to create context\n");
2824 return -1;
2825 }
2826
Benjamin Franzke0c991632011-09-27 21:57:31 +02002827 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002828 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002829 return -1;
2830 }
2831
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002832#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002833 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2834 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002835 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002836 return -1;
2837 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002838 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2839 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2840 fprintf(stderr, "failed to get cairo egl argb device\n");
2841 return -1;
2842 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002843#endif
2844
2845 return 0;
2846}
2847
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002848static void
2849fini_egl(struct display *display)
2850{
2851#ifdef HAVE_CAIRO_EGL
2852 cairo_device_destroy(display->argb_device);
2853 cairo_device_destroy(display->rgb_device);
2854#endif
2855
2856 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2857 EGL_NO_CONTEXT);
2858
2859 eglTerminate(display->dpy);
2860 eglReleaseThread();
2861}
2862
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002863static int
2864event_mask_update(uint32_t mask, void *data)
2865{
2866 struct display *d = data;
2867
2868 d->mask = mask;
2869
2870 return 0;
2871}
2872
2873static void
2874handle_display_data(struct task *task, uint32_t events)
2875{
2876 struct display *display =
2877 container_of(task, struct display, display_task);
2878
2879 wl_display_iterate(display->display, display->mask);
2880}
2881
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002882struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002883display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002884{
2885 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002886 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002887 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002888 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002889
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002890 g_type_init();
2891
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002892 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002893 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002894 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002895
2896 xkb_option_group = g_option_group_new("xkb",
2897 "Keyboard options",
2898 "Show all XKB options",
2899 NULL, NULL);
2900 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2901 g_option_context_add_group (context, xkb_option_group);
2902
2903 if (!g_option_context_parse(context, argc, argv, &error)) {
2904 fprintf(stderr, "option parsing failed: %s\n", error->message);
2905 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002906 }
2907
Tim Wiederhake748f6722011-01-23 23:25:25 +01002908 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002909
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002910 d = malloc(sizeof *d);
2911 if (d == NULL)
2912 return NULL;
2913
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002914 memset(d, 0, sizeof *d);
2915
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002916 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002917 if (d->display == NULL) {
2918 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002919 return NULL;
2920 }
2921
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002922 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2923 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2924 d->display_task.run = handle_display_data;
2925 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2926
2927 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002928 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002929 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002930
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002931 /* Set up listener so we'll catch all events. */
2932 wl_display_add_global_listener(d->display,
2933 display_handle_global, d);
2934
2935 /* Process connection events. */
2936 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002937 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002938 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002939
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002940 d->image_target_texture_2d =
2941 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2942 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2943 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2944
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002945 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002946
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002947 display_render_frame(d);
2948
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002949 wl_list_init(&d->window_list);
2950
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002951 init_xkb(d);
2952
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002953 return d;
2954}
2955
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002956static void
2957display_destroy_outputs(struct display *display)
2958{
2959 struct output *tmp;
2960 struct output *output;
2961
2962 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2963 output_destroy(output);
2964}
2965
Pekka Paalanene1207c72011-12-16 12:02:09 +02002966static void
2967display_destroy_inputs(struct display *display)
2968{
2969 struct input *tmp;
2970 struct input *input;
2971
2972 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2973 input_destroy(input);
2974}
2975
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002976void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002977display_destroy(struct display *display)
2978{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002979 if (!wl_list_empty(&display->window_list))
2980 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2981
2982 if (!wl_list_empty(&display->deferred_list))
2983 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2984
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002985 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002986 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002987
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002988 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002989
2990 cairo_surface_destroy(display->active_frame);
2991 cairo_surface_destroy(display->inactive_frame);
2992 cairo_surface_destroy(display->shadow);
2993 destroy_pointer_surfaces(display);
2994
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002995 fini_egl(display);
2996
Pekka Paalanenc2052982011-12-16 11:41:32 +02002997 if (display->shell)
2998 wl_shell_destroy(display->shell);
2999
3000 if (display->shm)
3001 wl_shm_destroy(display->shm);
3002
3003 if (display->data_device_manager)
3004 wl_data_device_manager_destroy(display->data_device_manager);
3005
3006 wl_compositor_destroy(display->compositor);
3007
3008 close(display->epoll_fd);
3009
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003010 wl_display_flush(display->display);
3011 wl_display_destroy(display->display);
3012 free(display);
3013}
3014
3015void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003016display_set_user_data(struct display *display, void *data)
3017{
3018 display->user_data = data;
3019}
3020
3021void *
3022display_get_user_data(struct display *display)
3023{
3024 return display->user_data;
3025}
3026
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003027struct wl_display *
3028display_get_display(struct display *display)
3029{
3030 return display->display;
3031}
3032
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003033struct output *
3034display_get_output(struct display *display)
3035{
3036 return container_of(display->output_list.next, struct output, link);
3037}
3038
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003039struct wl_compositor *
3040display_get_compositor(struct display *display)
3041{
3042 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003043}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003044
3045EGLDisplay
3046display_get_egl_display(struct display *d)
3047{
3048 return d->dpy;
3049}
3050
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003051struct wl_data_source *
3052display_create_data_source(struct display *display)
3053{
3054 return wl_data_device_manager_create_data_source(display->data_device_manager);
3055}
3056
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003057EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003058display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003059{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003060 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003061}
3062
Benjamin Franzke0c991632011-09-27 21:57:31 +02003063EGLConfig
3064display_get_argb_egl_config(struct display *d)
3065{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003066 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003067}
3068
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003069struct wl_shell *
3070display_get_shell(struct display *display)
3071{
3072 return display->shell;
3073}
3074
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003075int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076display_acquire_window_surface(struct display *display,
3077 struct window *window,
3078 EGLContext ctx)
3079{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003080#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003081 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003082 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003083
3084 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003085 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003086 device = cairo_surface_get_device(window->cairo_surface);
3087 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089
Benjamin Franzke0c991632011-09-27 21:57:31 +02003090 if (!ctx) {
3091 if (device == display->rgb_device)
3092 ctx = display->rgb_ctx;
3093 else if (device == display->argb_device)
3094 ctx = display->argb_ctx;
3095 else
3096 assert(0);
3097 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003098
3099 data = cairo_surface_get_user_data(window->cairo_surface,
3100 &surface_data_key);
3101
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003102 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003103 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003104 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3105 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003106
3107 return 0;
3108#else
3109 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003110#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003111}
3112
3113void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003114display_release_window_surface(struct display *display,
3115 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003116{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003117#ifdef HAVE_CAIRO_EGL
3118 cairo_device_t *device;
3119
3120 device = cairo_surface_get_device(window->cairo_surface);
3121 if (!device)
3122 return;
3123
3124 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003125 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003126 cairo_device_release(device);
3127#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003128}
3129
3130void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003131display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003132{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003133 wl_list_insert(&display->deferred_list, &task->link);
3134}
3135
3136void
3137display_watch_fd(struct display *display,
3138 int fd, uint32_t events, struct task *task)
3139{
3140 struct epoll_event ep;
3141
3142 ep.events = events;
3143 ep.data.ptr = task;
3144 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3145}
3146
3147void
3148display_run(struct display *display)
3149{
3150 struct task *task;
3151 struct epoll_event ep[16];
3152 int i, count;
3153
Pekka Paalanen826d7952011-12-15 10:14:07 +02003154 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003155 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003156 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003157
3158 while (!wl_list_empty(&display->deferred_list)) {
3159 task = container_of(display->deferred_list.next,
3160 struct task, link);
3161 wl_list_remove(&task->link);
3162 task->run(task, 0);
3163 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003164
3165 if (!display->running)
3166 break;
3167
3168 wl_display_flush(display->display);
3169
3170 count = epoll_wait(display->epoll_fd,
3171 ep, ARRAY_LENGTH(ep), -1);
3172 for (i = 0; i < count; i++) {
3173 task = ep[i].data.ptr;
3174 task->run(task, ep[i].events);
3175 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003176 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003177}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003178
3179void
3180display_exit(struct display *display)
3181{
3182 display->running = 0;
3183}