blob: 6a4cc9339e8b4c01208c443318109e630deaebaa [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øgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040040#include <pixman.h>
41
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030058#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040062#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030066struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040069struct theme {
70 cairo_surface_t *active_frame;
71 cairo_surface_t *inactive_frame;
72 cairo_surface_t *shadow;
73 int frame_radius;
74 int margin;
75 int width;
76 int titlebar_height;
77};
78
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050079struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050080 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050081 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040082 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040083 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040084 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040085 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050086 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020087 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020088 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040089 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040090
91 int display_fd;
92 uint32_t mask;
93 struct task display_task;
94
95 int epoll_fd;
96 struct wl_list deferred_list;
97
Pekka Paalanen826d7952011-12-15 10:14:07 +020098 int running;
99
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400100 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400101 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500102 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400103
104 struct theme theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400105
106 struct {
107 struct xkb_rule_names names;
108 struct xkb_keymap *keymap;
109 struct xkb_state *state;
110 struct xkb_context *context;
111 xkb_mod_mask_t control_mask;
112 xkb_mod_mask_t alt_mask;
113 xkb_mod_mask_t shift_mask;
114 } xkb;
115
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300116 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700117 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500119 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
120 PFNEGLCREATEIMAGEKHRPROC create_image;
121 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 display_output_handler_t output_configure_handler;
124
125 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500126};
127
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400129 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400131 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500132 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500134 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_CUSTOM
136};
Rob Bradford7507b572012-05-15 17:55:34 +0100137
138struct window_output {
139 struct output *output;
140 struct wl_list link;
141};
142
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500143struct window {
144 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500145 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100146 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200148 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500149 struct wl_region *input_region;
150 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500151 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500152 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500153 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500154 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400155 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400156 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400157 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400158 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400159 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400160 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400161 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400162 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400163 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500164
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400165 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500166
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700167 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400168
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500169 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500170 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400171 window_data_handler_t data_handler;
172 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500173 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400174
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200175 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500176 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500177
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500178 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400179 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500180};
181
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500182struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500183 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500184 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400185 struct wl_list link;
186 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187 widget_resize_handler_t resize_handler;
188 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500189 widget_enter_handler_t enter_handler;
190 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500191 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500192 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500194 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400195};
196
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197struct input {
198 struct display *display;
199 struct wl_input_device *input_device;
200 struct window *pointer_focus;
201 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300202 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400203 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400204 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400205 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400206 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400207
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500208 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500209 struct widget *grab;
210 uint32_t grab_button;
211
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400212 struct wl_data_device *data_device;
213 struct data_offer *drag_offer;
214 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400215};
216
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500217struct output {
218 struct display *display;
219 struct wl_output *output;
220 struct rectangle allocation;
221 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200222
223 display_output_handler_t destroy_handler;
224 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500225};
226
Martin Minarik1998b152012-05-10 02:04:35 +0200227enum frame_button_action {
228 FRAME_BUTTON_NULL = 0,
229 FRAME_BUTTON_ICON = 1,
230 FRAME_BUTTON_CLOSE = 2,
231 FRAME_BUTTON_MINIMIZE = 3,
232 FRAME_BUTTON_MAXIMIZE = 4,
233};
234
235enum frame_button_pointer {
236 FRAME_BUTTON_DEFAULT = 0,
237 FRAME_BUTTON_OVER = 1,
238 FRAME_BUTTON_ACTIVE = 2,
239};
240
241enum frame_button_align {
242 FRAME_BUTTON_RIGHT = 0,
243 FRAME_BUTTON_LEFT = 1,
244};
245
246enum frame_button_decoration {
247 FRAME_BUTTON_NONE = 0,
248 FRAME_BUTTON_FANCY = 1,
249};
250
251struct frame_button {
252 struct widget *widget;
253 struct frame *frame;
254 cairo_surface_t *icon;
255 enum frame_button_action type;
256 enum frame_button_pointer state;
257 struct wl_list link; /* buttons_list */
258 enum frame_button_align align;
259 enum frame_button_decoration decoration;
260};
261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500262struct frame {
263 struct widget *widget;
264 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200265 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500266};
267
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500268struct menu {
269 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500270 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500271 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500272 const char **entries;
273 uint32_t time;
274 int current;
275 int count;
276 menu_func_t func;
277};
278
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300279struct cursor_image {
280 cairo_surface_t *surface;
281 int width, height;
282 int hotspot_x, hotspot_y;
283 int delay;
284};
285
286struct cursor {
287 int n_images;
288 struct cursor_image *images;
289};
290
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700291struct shm_pool {
292 struct wl_shm_pool *pool;
293 size_t size;
294 size_t used;
295 void *data;
296};
297
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400298enum {
299 POINTER_DEFAULT = 100,
300 POINTER_UNSET
301};
302
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500303enum window_location {
304 WINDOW_INTERIOR = 0,
305 WINDOW_RESIZING_TOP = 1,
306 WINDOW_RESIZING_BOTTOM = 2,
307 WINDOW_RESIZING_LEFT = 4,
308 WINDOW_RESIZING_TOP_LEFT = 5,
309 WINDOW_RESIZING_BOTTOM_LEFT = 6,
310 WINDOW_RESIZING_RIGHT = 8,
311 WINDOW_RESIZING_TOP_RIGHT = 9,
312 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
313 WINDOW_RESIZING_MASK = 15,
314 WINDOW_EXTERIOR = 16,
315 WINDOW_TITLEBAR = 17,
316 WINDOW_CLIENT_AREA = 18,
317};
318
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400319const char *option_xkb_layout = "us";
320const char *option_xkb_variant = "";
321const char *option_xkb_options = "";
322
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400323static const struct weston_option xkb_options[] = {
324 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
325 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
326 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400327};
328
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329static const cairo_user_data_key_t surface_data_key;
330struct surface_data {
331 struct wl_buffer *buffer;
332};
333
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500334#define MULT(_d,c,a,t) \
335 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
336
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500337#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400338
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100339struct egl_window_surface_data {
340 struct display *display;
341 struct wl_surface *surface;
342 struct wl_egl_window *window;
343 EGLSurface surf;
344};
345
346static void
347egl_window_surface_data_destroy(void *p)
348{
349 struct egl_window_surface_data *data = p;
350 struct display *d = data->display;
351
352 eglDestroySurface(d->dpy, data->surf);
353 wl_egl_window_destroy(data->window);
354 data->surface = NULL;
355
356 free(p);
357}
358
359static cairo_surface_t *
360display_create_egl_window_surface(struct display *display,
361 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100363 struct rectangle *rectangle)
364{
365 cairo_surface_t *cairo_surface;
366 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400367 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200368 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100369
370 data = malloc(sizeof *data);
371 if (data == NULL)
372 return NULL;
373
374 data->display = display;
375 data->surface = surface;
376
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500377 config = display->argb_config;
378 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400380 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400384 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500385 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100386
Benjamin Franzke0c991632011-09-27 21:57:31 +0200387 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100388 data->surf,
389 rectangle->width,
390 rectangle->height);
391
392 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
393 data, egl_window_surface_data_destroy);
394
395 return cairo_surface;
396}
397
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398#endif
399
400struct wl_buffer *
401display_get_buffer_for_surface(struct display *display,
402 cairo_surface_t *surface)
403{
404 struct surface_data *data;
405
406 data = cairo_surface_get_user_data (surface, &surface_data_key);
407
408 return data->buffer;
409}
410
411struct shm_surface_data {
412 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700413 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414};
415
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500416static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417shm_pool_destroy(struct shm_pool *pool);
418
419static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420shm_surface_data_destroy(void *p)
421{
422 struct shm_surface_data *data = p;
423
424 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700425 if (data->pool)
426 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427}
428
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300429static void
430shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
431{
432 void *dest = cairo_image_surface_get_data(surface);
433
434 memcpy(dest, data, count);
435}
436
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437static struct wl_shm_pool *
438make_shm_pool(struct display *display, int size, void **data)
439{
440 char filename[] = "/tmp/wayland-shm-XXXXXX";
441 struct wl_shm_pool *pool;
442 int fd;
443
444 fd = mkstemp(filename);
445 if (fd < 0) {
446 fprintf(stderr, "open %s failed: %m\n", filename);
447 return NULL;
448 }
449 if (ftruncate(fd, size) < 0) {
450 fprintf(stderr, "ftruncate failed: %m\n");
451 close(fd);
452 return NULL;
453 }
454
455 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
456 unlink(filename);
457
458 if (*data == MAP_FAILED) {
459 fprintf(stderr, "mmap failed: %m\n");
460 close(fd);
461 return NULL;
462 }
463
464 pool = wl_shm_create_pool(display->shm, fd, size);
465
466 close(fd);
467
468 return pool;
469}
470
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700471static struct shm_pool *
472shm_pool_create(struct display *display, size_t size)
473{
474 struct shm_pool *pool = malloc(sizeof *pool);
475
476 if (!pool)
477 return NULL;
478
479 pool->pool = make_shm_pool(display, size, &pool->data);
480 if (!pool->pool) {
481 free(pool);
482 return NULL;
483 }
484
485 pool->size = size;
486 pool->used = 0;
487
488 return pool;
489}
490
491static void *
492shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
493{
494 if (pool->used + size > pool->size)
495 return NULL;
496
497 *offset = pool->used;
498 pool->used += size;
499
500 return (char *) pool->data + *offset;
501}
502
503/* destroy the pool. this does not unmap the memory though */
504static void
505shm_pool_destroy(struct shm_pool *pool)
506{
507 munmap(pool->data, pool->size);
508 wl_shm_pool_destroy(pool->pool);
509 free(pool);
510}
511
512/* Start allocating from the beginning of the pool again */
513static void
514shm_pool_reset(struct shm_pool *pool)
515{
516 pool->used = 0;
517}
518
519static int
520data_length_for_shm_surface(struct rectangle *rect)
521{
522 int stride;
523
524 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
525 rect->width);
526 return stride * rect->height;
527}
528
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500529static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530display_create_shm_surface_from_pool(struct display *display,
531 struct rectangle *rectangle,
532 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533{
534 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400535 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400536 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700537 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539
540 data = malloc(sizeof *data);
541 if (data == NULL)
542 return NULL;
543
544 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
545 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700546 length = stride * rectangle->height;
547 data->pool = NULL;
548 map = shm_pool_allocate(pool, length, &offset);
549
550 if (!map) {
551 free(data);
552 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400553 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400555 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556 CAIRO_FORMAT_ARGB32,
557 rectangle->width,
558 rectangle->height,
559 stride);
560
561 cairo_surface_set_user_data (surface, &surface_data_key,
562 data, shm_surface_data_destroy);
563
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400564 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500565 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400566 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500567 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400568
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700569 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400570 rectangle->width,
571 rectangle->height,
572 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700574 return surface;
575}
576
577static cairo_surface_t *
578display_create_shm_surface(struct display *display,
579 struct rectangle *rectangle, uint32_t flags,
580 struct window *window)
581{
582 struct shm_surface_data *data;
583 struct shm_pool *pool;
584 cairo_surface_t *surface;
585
586 if (window && window->pool) {
587 shm_pool_reset(window->pool);
588 surface = display_create_shm_surface_from_pool(display,
589 rectangle,
590 flags,
591 window->pool);
592 if (surface)
593 return surface;
594 }
595
596 pool = shm_pool_create(display,
597 data_length_for_shm_surface(rectangle));
598 if (!pool)
599 return NULL;
600
601 surface =
602 display_create_shm_surface_from_pool(display, rectangle,
603 flags, pool);
604
605 if (!surface) {
606 shm_pool_destroy(pool);
607 return NULL;
608 }
609
610 /* make sure we destroy the pool when the surface is destroyed */
611 data = cairo_surface_get_user_data(surface, &surface_data_key);
612 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613
614 return surface;
615}
616
nobled7b87cb02011-02-01 18:51:47 +0000617static int
618check_size(struct rectangle *rect)
619{
620 if (rect->width && rect->height)
621 return 0;
622
623 fprintf(stderr, "tried to create surface of "
624 "width: %d, height: %d\n", rect->width, rect->height);
625 return -1;
626}
627
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628cairo_surface_t *
629display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400631 struct rectangle *rectangle,
632 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633{
nobled7b87cb02011-02-01 18:51:47 +0000634 if (check_size(rectangle) < 0)
635 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500636#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400637 if (display->dpy)
638 return display_create_egl_window_surface(display,
639 surface,
640 flags,
641 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400643 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400644}
645
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300646static const char *cursors[] = {
647 "bottom_left_corner",
648 "bottom_right_corner",
649 "bottom_side",
650 "grabbing",
651 "left_ptr",
652 "left_side",
653 "right_side",
654 "top_left_corner",
655 "top_right_corner",
656 "top_side",
657 "xterm",
658 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659};
660
661static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300662create_cursor_from_images(struct display *display, struct cursor *cursor,
663 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300665 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400666 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300667 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300669 cursor->images = malloc(images->nimage * sizeof *cursor->images);
670 cursor->n_images = images->nimage;
671
672 for (i = 0; i < images->nimage; i++) {
673 image = images->images[i];
674
675 rect.width = image->width;
676 rect.height = image->height;
677
678 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679 display_create_shm_surface_from_pool(display, &rect, 0,
680 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300681
682 shm_surface_write(cursor->images[i].surface,
683 (unsigned char *) image->pixels,
684 image->width * image->height * sizeof image->pixels[0]);
685
686 cursor->images[i].width = image->width;
687 cursor->images[i].height = image->height;
688 cursor->images[i].hotspot_x = image->xhot;
689 cursor->images[i].hotspot_y = image->yhot;
690 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691 }
692
693}
694
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700695static size_t
696data_length_for_cursor_images(XcursorImages *images)
697{
698 int i;
699 size_t length = 0;
700 struct rectangle rect;
701
702 for (i = 0; i < images->nimage; i++) {
703 rect.width = images->images[i]->width;
704 rect.height = images->images[i]->height;
705 length += data_length_for_shm_surface(&rect);
706 }
707
708 return length;
709}
710
Pekka Paalanen325bb602011-12-19 10:31:45 +0200711static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300712create_cursors(struct display *display)
713{
714 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700715 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300716 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700717 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300718
719 count = ARRAY_LENGTH(cursors);
720 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700721 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 for (i = 0; i < count; i++) {
724 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200725 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300726 fprintf(stderr, "Error loading cursor: %s\n",
727 cursors[i]);
728 continue;
729 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700730 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300731 }
732
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733 display->cursor_shm_pool = shm_pool_create(display, pool_size);
734
735 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700737
738 if (!images[i]) {
739 cursor->n_images = 0;
740 cursor->images = NULL;
741 continue;
742 }
743
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700744 create_cursor_from_images(display, cursor, images[i]);
745
746 XcursorImagesDestroy(images[i]);
747 }
748
749 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300750}
751
752static void
753destroy_cursor_images(struct cursor *cursor)
754{
755 int i;
756
757 for (i = 0; i < cursor->n_images; i++)
758 if (cursor->images[i].surface)
759 cairo_surface_destroy(cursor->images[i].surface);
760
761 free(cursor->images);
762}
763
764static void
765destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200766{
767 int i, count;
768
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300769 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200770 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300771 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200772 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700773
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300774 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700775 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200776}
777
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400778cairo_surface_t *
779display_get_pointer_surface(struct display *display, int pointer,
780 int *width, int *height,
781 int *hotspot_x, int *hotspot_y)
782{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300783 struct cursor *cursor = &display->cursors[pointer];
784 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400785
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300786 /* FIXME returning information for the first image. Something better
787 * is needed for animated cursors */
788
nobledf8475c92011-01-05 17:41:55 +0000789 *width = cairo_image_surface_get_width(surface);
790 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300791
792 *hotspot_x = cursor->images[0].hotspot_x;
793 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400794
795 return cairo_surface_reference(surface);
796}
797
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400798static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200799window_get_resize_dx_dy(struct window *window, int *x, int *y)
800{
801 if (window->resize_edges & WINDOW_RESIZING_LEFT)
802 *x = window->server_allocation.width - window->allocation.width;
803 else
804 *x = 0;
805
806 if (window->resize_edges & WINDOW_RESIZING_TOP)
807 *y = window->server_allocation.height -
808 window->allocation.height;
809 else
810 *y = 0;
811
812 window->resize_edges = 0;
813}
814
815static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500816window_attach_surface(struct window *window)
817{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400818 struct display *display = window->display;
819 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500823 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400825 if (window->type == TYPE_NONE) {
826 window->type = TYPE_TOPLEVEL;
827 if (display->shell)
828 wl_shell_surface_set_toplevel(window->shell_surface);
829 }
830
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
834 data = cairo_surface_get_user_data(window->cairo_surface,
835 &surface_data_key);
836
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 cairo_gl_surface_swapbuffers(window->cairo_surface);
838 wl_egl_window_get_attached_size(data->window,
839 &window->server_allocation.width,
840 &window->server_allocation.height);
841 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000842#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 buffer =
845 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400846 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200848 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400850 wl_surface_damage(window->surface, 0, 0,
851 window->allocation.width,
852 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400854 cairo_surface_destroy(window->cairo_surface);
855 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000857 default:
858 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500860
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500861 if (window->input_region) {
862 wl_surface_set_input_region(window->surface,
863 window->input_region);
864 wl_region_destroy(window->input_region);
865 window->input_region = NULL;
866 }
867
868 if (window->opaque_region) {
869 wl_surface_set_opaque_region(window->surface,
870 window->opaque_region);
871 wl_region_destroy(window->opaque_region);
872 window->opaque_region = NULL;
873 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500874}
875
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500876void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400877window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500878{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400879 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100880 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500881}
882
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400883void
884window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500886 cairo_surface_reference(surface);
887
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888 if (window->cairo_surface != NULL)
889 cairo_surface_destroy(window->cairo_surface);
890
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500891 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892}
893
Benjamin Franzke22d54812011-07-16 19:50:32 +0000894#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895static void
896window_resize_cairo_window_surface(struct window *window)
897{
898 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200899 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900
901 data = cairo_surface_get_user_data(window->cairo_surface,
902 &surface_data_key);
903
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 wl_egl_window_resize(data->window,
906 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 window->allocation.height,
908 x,y);
909
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 cairo_gl_surface_set_size(window->cairo_surface,
911 window->allocation.width,
912 window->allocation.height);
913}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000914#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400916struct display *
917window_get_display(struct window *window)
918{
919 return window->display;
920}
921
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400922void
923window_create_surface(struct window *window)
924{
925 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 uint32_t flags = 0;
927
928 if (!window->transparent)
929 flags = SURFACE_OPAQUE;
930
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500932#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100933 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
934 if (window->cairo_surface) {
935 window_resize_cairo_window_surface(window);
936 return;
937 }
938 surface = display_create_surface(window->display,
939 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400940 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100941 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400942#endif
943 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400945 &window->allocation,
946 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800948 default:
949 surface = NULL;
950 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400952
953 window_set_surface(window, surface);
954 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400955}
956
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200957static void frame_destroy(struct frame *frame);
958
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400959void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500960window_destroy(struct window *window)
961{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200962 struct display *display = window->display;
963 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100964 struct window_output *window_output;
965 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200966
967 if (window->redraw_scheduled)
968 wl_list_remove(&window->redraw_task.link);
969
970 wl_list_for_each(input, &display->input_list, link) {
971 if (input->pointer_focus == window)
972 input->pointer_focus = NULL;
973 if (input->keyboard_focus == window)
974 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500975 if (input->focus_widget &&
976 input->focus_widget->window == window)
977 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200978 }
979
Rob Bradford7507b572012-05-15 17:55:34 +0100980 wl_list_for_each_safe(window_output, window_output_tmp,
981 &window->window_output_list, link) {
982 free (window_output);
983 }
984
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500985 if (window->input_region)
986 wl_region_destroy(window->input_region);
987 if (window->opaque_region)
988 wl_region_destroy(window->opaque_region);
989
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200990 if (window->frame)
991 frame_destroy(window->frame);
992
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200993 if (window->shell_surface)
994 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500995 wl_surface_destroy(window->surface);
996 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200997
998 if (window->cairo_surface != NULL)
999 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001000
1001 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002 free(window);
1003}
1004
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001005static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001009
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001010 wl_list_for_each(child, &widget->child_list, link) {
1011 target = widget_find_widget(child, x, y);
1012 if (target)
1013 return target;
1014 }
1015
1016 if (widget->allocation.x <= x &&
1017 x < widget->allocation.x + widget->allocation.width &&
1018 widget->allocation.y <= y &&
1019 y < widget->allocation.y + widget->allocation.height) {
1020 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021 }
1022
1023 return NULL;
1024}
1025
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001026static struct widget *
1027widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001030
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001031 widget = malloc(sizeof *widget);
1032 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001033 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001034 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001035 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001036 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001037 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001038
1039 return widget;
1040}
1041
1042struct widget *
1043window_add_widget(struct window *window, void *data)
1044{
1045 window->widget = widget_create(window, data);
1046 wl_list_init(&window->widget->link);
1047
1048 return window->widget;
1049}
1050
1051struct widget *
1052widget_add_widget(struct widget *parent, void *data)
1053{
1054 struct widget *widget;
1055
1056 widget = widget_create(parent->window, data);
1057 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060}
1061
1062void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001063widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001065 struct display *display = widget->window->display;
1066 struct input *input;
1067
1068 wl_list_for_each(input, &display->input_list, link) {
1069 if (input->focus_widget == widget)
1070 input->focus_widget = NULL;
1071 }
1072
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001073 wl_list_remove(&widget->link);
1074 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075}
1076
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001078widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001079{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001080 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081}
1082
1083void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001084widget_set_size(struct widget *widget, int32_t width, int32_t height)
1085{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001086 widget->allocation.width = width;
1087 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001088}
1089
1090void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001091widget_set_allocation(struct widget *widget,
1092 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001094 widget->allocation.x = x;
1095 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001096 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097}
1098
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001099void
1100widget_set_transparent(struct widget *widget, int transparent)
1101{
1102 widget->opaque = !transparent;
1103}
1104
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001105void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001106widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001107{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001108 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001109}
1110
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001111void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001112widget_set_resize_handler(struct widget *widget,
1113 widget_resize_handler_t handler)
1114{
1115 widget->resize_handler = handler;
1116}
1117
1118void
1119widget_set_redraw_handler(struct widget *widget,
1120 widget_redraw_handler_t handler)
1121{
1122 widget->redraw_handler = handler;
1123}
1124
1125void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001126widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001127{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001128 widget->enter_handler = handler;
1129}
1130
1131void
1132widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1133{
1134 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001135}
1136
1137void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001138widget_set_motion_handler(struct widget *widget,
1139 widget_motion_handler_t handler)
1140{
1141 widget->motion_handler = handler;
1142}
1143
1144void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001145widget_set_button_handler(struct widget *widget,
1146 widget_button_handler_t handler)
1147{
1148 widget->button_handler = handler;
1149}
1150
1151void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001152widget_schedule_redraw(struct widget *widget)
1153{
1154 window_schedule_redraw(widget->window);
1155}
1156
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001157cairo_surface_t *
1158window_get_surface(struct window *window)
1159{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001160 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001161}
1162
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001163struct wl_surface *
1164window_get_wl_surface(struct window *window)
1165{
1166 return window->surface;
1167}
1168
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001169struct wl_shell_surface *
1170window_get_wl_shell_surface(struct window *window)
1171{
1172 return window->shell_surface;
1173}
1174
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001175static void
1176frame_resize_handler(struct widget *widget,
1177 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001179 struct frame *frame = data;
1180 struct widget *child = frame->child;
1181 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001182 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001183 struct frame_button * button;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001184 struct theme *t = &display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001185 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001186 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001187 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001188
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001189 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001190 decoration_width = (t->width + t->margin) * 2;
1191 decoration_height = t->width +
1192 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001194 allocation.x = t->width + t->margin;
1195 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001196 allocation.width = width - decoration_width;
1197 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001198
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001199 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001200
1201 wl_list_for_each(button, &frame->buttons_list, link)
1202 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001203 } else {
1204 decoration_width = 0;
1205 decoration_height = 0;
1206
1207 allocation.x = 0;
1208 allocation.y = 0;
1209 allocation.width = width;
1210 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001211 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001212
1213 wl_list_for_each(button, &frame->buttons_list, link)
1214 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001215 }
1216
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001217 widget_set_allocation(child, allocation.x, allocation.y,
1218 allocation.width, allocation.height);
1219
1220 if (child->resize_handler)
1221 child->resize_handler(child,
1222 allocation.width,
1223 allocation.height,
1224 child->user_data);
1225
Scott Moreauf7e498c2012-05-14 11:39:29 -06001226 width = child->allocation.width + decoration_width;
1227 height = child->allocation.height + decoration_height;
1228
1229 widget->window->input_region =
1230 wl_compositor_create_region(display->compositor);
1231 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001232 t->margin, t->margin,
1233 width - 2 * t->margin,
1234 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001235
1236 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001237
1238 if (child->opaque) {
1239 widget->window->opaque_region =
1240 wl_compositor_create_region(display->compositor);
1241 wl_region_add(widget->window->opaque_region,
1242 opaque_margin, opaque_margin,
1243 widget->allocation.width - 2 * opaque_margin,
1244 widget->allocation.height - 2 * opaque_margin);
1245 }
Martin Minarik1998b152012-05-10 02:04:35 +02001246
1247 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 x_r = frame->widget->allocation.width - t->width - t->margin;
1249 x_l = t->width + t->margin;
1250 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001251 wl_list_for_each(button, &frame->buttons_list, link) {
1252 const int button_padding = 4;
1253 w = cairo_image_surface_get_width(button->icon);
1254 h = cairo_image_surface_get_height(button->icon);
1255
1256 if (button->decoration == FRAME_BUTTON_FANCY)
1257 w += 10;
1258
1259 if (button->align == FRAME_BUTTON_LEFT) {
1260 widget_set_allocation(button->widget,
1261 x_l, y , w + 1, h + 1);
1262 x_l += w;
1263 x_l += button_padding;
1264 } else {
1265 x_r -= w;
1266 widget_set_allocation(button->widget,
1267 x_r, y , w + 1, h + 1);
1268 x_r -= button_padding;
1269 }
1270 }
1271}
1272
1273static int
1274frame_button_enter_handler(struct widget *widget,
1275 struct input *input, float x, float y, void *data)
1276{
1277 struct frame_button *frame_button = data;
1278
1279 widget_schedule_redraw(frame_button->widget);
1280 frame_button->state = FRAME_BUTTON_OVER;
1281
1282 return POINTER_LEFT_PTR;
1283}
1284
1285static void
1286frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1287{
1288 struct frame_button *frame_button = data;
1289
1290 widget_schedule_redraw(frame_button->widget);
1291 frame_button->state = FRAME_BUTTON_DEFAULT;
1292}
1293
1294static void
1295frame_button_button_handler(struct widget *widget,
1296 struct input *input, uint32_t time,
1297 uint32_t button, uint32_t state, void *data)
1298{
1299 struct frame_button *frame_button = data;
1300 struct window *window = widget->window;
1301
1302 if (button != BTN_LEFT)
1303 return;
1304
1305 switch (state) {
1306 case 1:
1307 frame_button->state = FRAME_BUTTON_ACTIVE;
1308 widget_schedule_redraw(frame_button->widget);
1309
1310 if (frame_button->type == FRAME_BUTTON_ICON)
1311 window_show_frame_menu(window, input, time);
1312 return;
1313 case 0:
1314 frame_button->state = FRAME_BUTTON_DEFAULT;
1315 widget_schedule_redraw(frame_button->widget);
1316 break;
1317 }
1318
1319 switch (frame_button->type) {
1320 case FRAME_BUTTON_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 FRAME_BUTTON_MINIMIZE:
1328 fprintf(stderr,"Minimize stub\n");
1329 break;
1330 case FRAME_BUTTON_MAXIMIZE:
1331 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1332 break;
1333 default:
1334 /* Unknown operation */
1335 break;
1336 }
1337}
1338
1339static void
1340frame_button_redraw_handler(struct widget *widget, void *data)
1341{
1342 struct frame_button *frame_button = data;
1343 cairo_t *cr;
1344 int width, height, x, y;
1345 struct window *window = widget->window;
1346
1347 x = widget->allocation.x;
1348 y = widget->allocation.y;
1349 width = widget->allocation.width;
1350 height = widget->allocation.height;
1351
1352 if (!width)
1353 return;
1354 if (!height)
1355 return;
1356 if (widget->opaque)
1357 return;
1358
1359 cr = cairo_create(window->cairo_surface);
1360
1361 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1362 cairo_set_line_width(cr, 1);
1363
1364 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1365 cairo_rectangle (cr, x, y, 25, 16);
1366
1367 cairo_stroke_preserve(cr);
1368
1369 switch (frame_button->state) {
1370 case FRAME_BUTTON_DEFAULT:
1371 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1372 break;
1373 case FRAME_BUTTON_OVER:
1374 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1375 break;
1376 case FRAME_BUTTON_ACTIVE:
1377 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1378 break;
1379 }
1380
1381 cairo_fill (cr);
1382
1383 x += 4;
1384 }
1385
1386 cairo_set_source_surface(cr, frame_button->icon, x, y);
1387 cairo_paint(cr);
1388
1389 cairo_destroy(cr);
1390}
1391
1392static struct widget *
1393frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1394 enum frame_button_align align, enum frame_button_decoration style)
1395{
1396 struct frame_button *frame_button;
1397 const char *icon = data;
1398
1399 frame_button = malloc (sizeof *frame_button);
1400 memset(frame_button, 0, sizeof *frame_button);
1401
1402 frame_button->icon = cairo_image_surface_create_from_png(icon);
1403 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1404 frame_button->frame = frame;
1405 frame_button->type = type;
1406 frame_button->align = align;
1407 frame_button->decoration = style;
1408
1409 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1410
1411 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1412 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1413 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1414 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1415 return frame_button->widget;
1416}
1417
1418static void
1419frame_button_destroy(struct frame_button *frame_button)
1420{
1421 widget_destroy(frame_button->widget);
1422 wl_list_remove(&frame_button->link);
1423 cairo_surface_destroy(frame_button->icon);
1424 free(frame_button);
1425
1426 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427}
1428
1429static void
1430frame_redraw_handler(struct widget *widget, void *data)
1431{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001432 cairo_t *cr;
1433 cairo_text_extents_t extents;
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001434 cairo_font_extents_t font_extents;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001435 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001436 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001437 struct window *window = widget->window;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001438 struct theme *t = &window->display->theme;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001439
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001440 if (window->type == TYPE_FULLSCREEN)
1441 return;
1442
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001443 width = widget->allocation.width;
1444 height = widget->allocation.height;
1445
1446 cr = cairo_create(window->cairo_surface);
1447
1448 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1449 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1450 cairo_paint(cr);
1451
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001452 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001453 tile_mask(cr, t->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001454 2, 2, width + 8, height + 8,
1455 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001456
1457 if (window->keyboard_device)
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001458 source = t->active_frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001459 else
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001460 source = t->inactive_frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001461
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001462 tile_source(cr, source,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001463 t->margin, t->margin,
1464 width - t->margin * 2, height - t->margin * 2,
1465 t->width, t->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001466
1467 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001468 cairo_select_font_face(cr, "sans",
1469 CAIRO_FONT_SLANT_NORMAL,
1470 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001471 cairo_set_font_size(cr, 14);
1472 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001473 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001474 x = (width - extents.width) / 2;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001475 y = t->margin +
1476 (t->titlebar_height -
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001477 font_extents.ascent - font_extents.descent) / 2 +
1478 font_extents.ascent;
1479
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001480 if (window->keyboard_device) {
1481 cairo_move_to(cr, x + 1, y + 1);
1482 cairo_set_source_rgb(cr, 1, 1, 1);
1483 cairo_show_text(cr, window->title);
1484 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001486 cairo_show_text(cr, window->title);
1487 } else {
1488 cairo_move_to(cr, x, y);
1489 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1490 cairo_show_text(cr, window->title);
1491 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001492
1493 cairo_destroy(cr);
1494}
1495
1496static int
1497frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1498{
1499 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001500 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001501 const int grip_size = 8;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001502 struct theme *t = &widget->window->display->theme;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001503
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001504 if (x < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001505 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001506 else if (t->margin <= x && x < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001508 else if (x < widget->allocation.width - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001509 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001510 else if (x < widget->allocation.width - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001511 hlocation = WINDOW_RESIZING_RIGHT;
1512 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001513 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001514
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001515 if (y < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001516 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001517 else if (t->margin <= y && y < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001519 else if (y < widget->allocation.height - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001520 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001521 else if (y < widget->allocation.height - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001522 vlocation = WINDOW_RESIZING_BOTTOM;
1523 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001524 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001525
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001526 location = vlocation | hlocation;
1527 if (location & WINDOW_EXTERIOR)
1528 location = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001529 if (location == WINDOW_INTERIOR && y < t->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001530 location = WINDOW_TITLEBAR;
1531 else if (location == WINDOW_INTERIOR)
1532 location = WINDOW_CLIENT_AREA;
1533
1534 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001535}
1536
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001537static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001538frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001539{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001540 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001543 switch (location) {
1544 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001546 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001547 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001548 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001550 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001551 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001552 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001553 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001554 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001555 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001556 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001557 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001558 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001559 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001560 case WINDOW_EXTERIOR:
1561 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001562 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001563 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001564 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001565}
1566
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001567static void
1568frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001569{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001570 switch (index) {
1571 case 0: /* close */
1572 if (window->close_handler)
1573 window->close_handler(window->parent,
1574 window->user_data);
1575 else
1576 display_exit(window->display);
1577 break;
1578 case 1: /* fullscreen */
1579 /* we don't have a way to get out of fullscreen for now */
1580 window_set_fullscreen(window, 1);
1581 break;
1582 case 2: /* rotate */
1583 case 3: /* scale */
1584 break;
1585 }
1586}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001587
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001588void
1589window_show_frame_menu(struct window *window,
1590 struct input *input, uint32_t time)
1591{
1592 int32_t x, y;
1593
1594 static const char *entries[] = {
1595 "Close", "Fullscreen", "Rotate", "Scale"
1596 };
1597
1598 input_get_position(input, &x, &y);
1599 window_show_menu(window->display, input, time, window,
1600 x - 10, y - 10, frame_menu_func, entries, 4);
1601}
1602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603static int
1604frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001605 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606{
1607 return frame_get_pointer_image_for_location(data, input);
1608}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001609
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610static int
1611frame_motion_handler(struct widget *widget,
1612 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001613 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614{
1615 return frame_get_pointer_image_for_location(data, input);
1616}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001617
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619frame_button_handler(struct widget *widget,
1620 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001621 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001622
1623{
1624 struct frame *frame = data;
1625 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001626 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001627 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001628
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629 location = frame_get_pointer_location(frame, input->sx, input->sy);
1630
1631 if (window->display->shell && button == BTN_LEFT && state == 1) {
1632 switch (location) {
1633 case WINDOW_TITLEBAR:
1634 if (!window->shell_surface)
1635 break;
1636 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638 wl_shell_surface_move(window->shell_surface,
1639 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001640 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001641 break;
1642 case WINDOW_RESIZING_TOP:
1643 case WINDOW_RESIZING_BOTTOM:
1644 case WINDOW_RESIZING_LEFT:
1645 case WINDOW_RESIZING_RIGHT:
1646 case WINDOW_RESIZING_TOP_LEFT:
1647 case WINDOW_RESIZING_TOP_RIGHT:
1648 case WINDOW_RESIZING_BOTTOM_LEFT:
1649 case WINDOW_RESIZING_BOTTOM_RIGHT:
1650 if (!window->shell_surface)
1651 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001652 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001653
1654 if (!display->dpy) {
1655 /* If we're using shm, allocate a big
1656 pool to create buffers out of while
1657 we resize. We should probably base
1658 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001659 window->pool =
1660 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001661 }
1662
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001663 wl_shell_surface_resize(window->shell_surface,
1664 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001665 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001666 break;
1667 }
1668 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001669 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 }
1671}
1672
1673struct widget *
1674frame_create(struct window *window, void *data)
1675{
1676 struct frame *frame;
1677
1678 frame = malloc(sizeof *frame);
1679 memset(frame, 0, sizeof *frame);
1680
1681 frame->widget = window_add_widget(window, frame);
1682 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1685 widget_set_resize_handler(frame->widget, frame_resize_handler);
1686 widget_set_enter_handler(frame->widget, frame_enter_handler);
1687 widget_set_motion_handler(frame->widget, frame_motion_handler);
1688 widget_set_button_handler(frame->widget, frame_button_handler);
1689
Martin Minarik1998b152012-05-10 02:04:35 +02001690 /* Create empty list for frame buttons */
1691 wl_list_init(&frame->buttons_list);
1692
1693 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1694 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1695
1696 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1697 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1698
1699 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1700 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1701
1702 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1703 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1704
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001705 window->frame = frame;
1706
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001707 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001708}
1709
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001710static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001711frame_destroy(struct frame *frame)
1712{
Martin Minarik1998b152012-05-10 02:04:35 +02001713 struct frame_button *button, *tmp;
1714
1715 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1716 frame_button_destroy(button);
1717
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001718 /* frame->child must be destroyed by the application */
1719 widget_destroy(frame->widget);
1720 free(frame);
1721}
1722
1723static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001724input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001725 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001726{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001727 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001728 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001729
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001730 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731 return;
1732
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001734 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001735 widget = old;
1736 if (input->grab)
1737 widget = input->grab;
1738 if (widget->leave_handler)
1739 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001740 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001741 }
1742
1743 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 widget = focus;
1745 if (input->grab)
1746 widget = input->grab;
1747 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001750 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001751
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001752 input_set_pointer_image(input, input->pointer_enter_serial,
1753 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001754 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001755}
1756
1757static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001758input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001759 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001760{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001761 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001762 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001763 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001764 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001765 float sx = wl_fixed_to_double(sx_w);
1766 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001767
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001768 input->sx = sx;
1769 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001770
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001771 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001772 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001774 }
1775
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001776 if (input->grab)
1777 widget = input->grab;
1778 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001779 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001780 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001781 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001782 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001783 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001784
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001785 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001786}
1787
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001788void
1789input_grab(struct input *input, struct widget *widget, uint32_t button)
1790{
1791 input->grab = widget;
1792 input->grab_button = button;
1793}
1794
1795void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001796input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001797{
1798 struct widget *widget;
1799
1800 input->grab = NULL;
1801 if (input->pointer_focus) {
1802 widget = widget_find_widget(input->pointer_focus->widget,
1803 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001805 }
1806}
1807
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001808static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001809input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001810 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001811 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001812{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001813 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001814 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001815
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001816 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001817 if (input->focus_widget && input->grab == NULL && state)
1818 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001819
Neil Roberts6b28aad2012-01-23 19:11:18 +00001820 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001821 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001822 (*widget->button_handler)(widget,
1823 input, time,
1824 button, state,
1825 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001826
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001827 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001828 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001829}
1830
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831static void
Scott Moreau210d0792012-03-22 10:47:01 -06001832input_handle_axis(void *data,
1833 struct wl_input_device *input_device,
1834 uint32_t time, uint32_t axis, int32_t value)
1835{
1836}
1837
1838static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001839input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001840 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001841{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001842 struct input *input = data;
1843 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001844 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001845 uint32_t code, num_syms;
1846 const xkb_keysym_t *syms;
1847 xkb_keysym_t sym;
1848 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001849
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001850 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001851 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001852 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001853 return;
1854
Kristian Høgsberg70163132012-05-08 15:55:39 -04001855 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1856 xkb_state_update_key(d->xkb.state, code,
1857 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001858
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001859 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001860 XKB_STATE_DEPRESSED |
1861 XKB_STATE_LATCHED);
1862 input->modifiers = 0;
1863 if (mask & input->display->xkb.control_mask)
1864 input->modifiers |= MOD_CONTROL_MASK;
1865 if (mask & input->display->xkb.alt_mask)
1866 input->modifiers |= MOD_ALT_MASK;
1867 if (mask & input->display->xkb.shift_mask)
1868 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001869
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001870 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001871 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001872 if (state)
1873 window_set_maximized(window,
1874 window->type != TYPE_MAXIMIZED);
1875 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001876 if (num_syms == 1)
1877 sym = syms[0];
1878 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001879 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001880
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001881 (*window->key_handler)(window, input, time, key,
1882 sym, state, window->user_data);
1883 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001884}
1885
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001886static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001887input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001888{
1889 struct window *window = input->pointer_focus;
1890
1891 if (!window)
1892 return;
1893
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001894 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001895
Pekka Paalanene1207c72011-12-16 12:02:09 +02001896 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001897 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001898}
1899
1900static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001901input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001902 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001903 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001904 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001905{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001906 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001907 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001908 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001909 float sx = wl_fixed_to_double(sx_w);
1910 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001911
Martin Minarik1998b152012-05-10 02:04:35 +02001912 if (!surface) {
1913 /* enter event for a window we've just destroyed */
1914 return;
1915 }
1916
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001917 input->display->serial = serial;
1918 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001920 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001921
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001922 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001923 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001924 window->pool = NULL;
1925 /* Schedule a redraw to free the pool */
1926 window_schedule_redraw(window);
1927 }
1928
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001929 input->sx = sx;
1930 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001931
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001932 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001933 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001934}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001935
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001936static void
1937input_handle_pointer_leave(void *data,
1938 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001939 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001940{
1941 struct input *input = data;
1942
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001943 input->display->serial = serial;
1944 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001945}
1946
1947static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001948input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001949{
1950 struct window *window = input->keyboard_focus;
1951
1952 if (!window)
1953 return;
1954
1955 window->keyboard_device = NULL;
1956 if (window->keyboard_focus_handler)
1957 (*window->keyboard_focus_handler)(window, NULL,
1958 window->user_data);
1959
1960 input->keyboard_focus = NULL;
1961}
1962
1963static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001964input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001965 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001966 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001967 struct wl_surface *surface,
1968 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001969{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001970 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001971 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001972
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001973 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001974 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001975
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001976 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001977 window->keyboard_device = input;
1978 if (window->keyboard_focus_handler)
1979 (*window->keyboard_focus_handler)(window,
1980 window->keyboard_device,
1981 window->user_data);
1982}
1983
1984static void
1985input_handle_keyboard_leave(void *data,
1986 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001987 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001988 struct wl_surface *surface)
1989{
1990 struct input *input = data;
1991
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001992 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001993 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001994}
1995
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001996static void
1997input_handle_touch_down(void *data,
1998 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001999 uint32_t serial, uint32_t time,
2000 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002001 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002002{
2003}
2004
2005static void
2006input_handle_touch_up(void *data,
2007 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002008 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002009{
2010}
2011
2012static void
2013input_handle_touch_motion(void *data,
2014 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002015 uint32_t time, int32_t id,
2016 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002017{
2018}
2019
2020static void
2021input_handle_touch_frame(void *data,
2022 struct wl_input_device *wl_input_device)
2023{
2024}
2025
2026static void
2027input_handle_touch_cancel(void *data,
2028 struct wl_input_device *wl_input_device)
2029{
2030}
2031
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002032static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002033 input_handle_motion,
2034 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06002035 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002036 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002037 input_handle_pointer_enter,
2038 input_handle_pointer_leave,
2039 input_handle_keyboard_enter,
2040 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002041 input_handle_touch_down,
2042 input_handle_touch_up,
2043 input_handle_touch_motion,
2044 input_handle_touch_frame,
2045 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002046};
2047
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002048void
2049input_get_position(struct input *input, int32_t *x, int32_t *y)
2050{
2051 *x = input->sx;
2052 *y = input->sy;
2053}
2054
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002055struct wl_input_device *
2056input_get_input_device(struct input *input)
2057{
2058 return input->input_device;
2059}
2060
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002061uint32_t
2062input_get_modifiers(struct input *input)
2063{
2064 return input->modifiers;
2065}
2066
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002067struct widget *
2068input_get_focus_widget(struct input *input)
2069{
2070 return input->focus_widget;
2071}
2072
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002073struct data_offer {
2074 struct wl_data_offer *offer;
2075 struct input *input;
2076 struct wl_array types;
2077 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002078
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002079 struct task io_task;
2080 int fd;
2081 data_func_t func;
2082 int32_t x, y;
2083 void *user_data;
2084};
2085
2086static void
2087data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2088{
2089 struct data_offer *offer = data;
2090 char **p;
2091
2092 p = wl_array_add(&offer->types, sizeof *p);
2093 *p = strdup(type);
2094}
2095
2096static const struct wl_data_offer_listener data_offer_listener = {
2097 data_offer_offer,
2098};
2099
2100static void
2101data_offer_destroy(struct data_offer *offer)
2102{
2103 char **p;
2104
2105 offer->refcount--;
2106 if (offer->refcount == 0) {
2107 wl_data_offer_destroy(offer->offer);
2108 for (p = offer->types.data; *p; p++)
2109 free(*p);
2110 wl_array_release(&offer->types);
2111 free(offer);
2112 }
2113}
2114
2115static void
2116data_device_data_offer(void *data,
2117 struct wl_data_device *data_device, uint32_t id)
2118{
2119 struct data_offer *offer;
2120
2121 offer = malloc(sizeof *offer);
2122
2123 wl_array_init(&offer->types);
2124 offer->refcount = 1;
2125 offer->input = data;
2126
2127 /* FIXME: Generate typesafe wrappers for this */
2128 offer->offer = (struct wl_data_offer *)
2129 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2130 id, &wl_data_offer_interface);
2131
2132 wl_data_offer_add_listener(offer->offer,
2133 &data_offer_listener, offer);
2134}
2135
2136static void
2137data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002138 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002139 wl_fixed_t x_w, wl_fixed_t y_w,
2140 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002141{
2142 struct input *input = data;
2143 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002144 float x = wl_fixed_to_double(x_w);
2145 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002146 char **p;
2147
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002148 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002149 input->drag_offer = wl_data_offer_get_user_data(offer);
2150 window = wl_surface_get_user_data(surface);
2151 input->pointer_focus = window;
2152
2153 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2154 *p = NULL;
2155
2156 window = input->pointer_focus;
2157 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002158 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002159 input->drag_offer->types.data,
2160 window->user_data);
2161}
2162
2163static void
2164data_device_leave(void *data, struct wl_data_device *data_device)
2165{
2166 struct input *input = data;
2167
2168 data_offer_destroy(input->drag_offer);
2169 input->drag_offer = NULL;
2170}
2171
2172static void
2173data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002174 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175{
2176 struct input *input = data;
2177 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002178 float x = wl_fixed_to_double(x_w);
2179 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002180
2181 input->sx = x;
2182 input->sy = y;
2183
2184 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002185 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002186 input->drag_offer->types.data,
2187 window->user_data);
2188}
2189
2190static void
2191data_device_drop(void *data, struct wl_data_device *data_device)
2192{
2193 struct input *input = data;
2194 struct window *window = input->pointer_focus;
2195
2196 if (window->drop_handler)
2197 window->drop_handler(window, input,
2198 input->sx, input->sy, window->user_data);
2199}
2200
2201static void
2202data_device_selection(void *data,
2203 struct wl_data_device *wl_data_device,
2204 struct wl_data_offer *offer)
2205{
2206 struct input *input = data;
2207 char **p;
2208
2209 if (input->selection_offer)
2210 data_offer_destroy(input->selection_offer);
2211
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002212 if (offer) {
2213 input->selection_offer = wl_data_offer_get_user_data(offer);
2214 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2215 *p = NULL;
2216 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002217}
2218
2219static const struct wl_data_device_listener data_device_listener = {
2220 data_device_data_offer,
2221 data_device_enter,
2222 data_device_leave,
2223 data_device_motion,
2224 data_device_drop,
2225 data_device_selection
2226};
2227
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228void
2229input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2230{
2231 struct display *display = input->display;
2232 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002233 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002235 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 return;
2237
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002238 if (display->cursors[pointer].n_images == 0)
2239 return;
2240
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002241 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002242
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002243 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002244 return;
2245
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002246 input->current_cursor = pointer;
2247 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002248 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002249 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002250}
2251
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002252struct wl_data_device *
2253input_get_data_device(struct input *input)
2254{
2255 return input->data_device;
2256}
2257
2258void
2259input_set_selection(struct input *input,
2260 struct wl_data_source *source, uint32_t time)
2261{
2262 wl_data_device_set_selection(input->data_device, source, time);
2263}
2264
2265void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002266input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002267{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002268 wl_data_offer_accept(input->drag_offer->offer,
2269 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002270}
2271
2272static void
2273offer_io_func(struct task *task, uint32_t events)
2274{
2275 struct data_offer *offer =
2276 container_of(task, struct data_offer, io_task);
2277 unsigned int len;
2278 char buffer[4096];
2279
2280 len = read(offer->fd, buffer, sizeof buffer);
2281 offer->func(buffer, len,
2282 offer->x, offer->y, offer->user_data);
2283
2284 if (len == 0) {
2285 close(offer->fd);
2286 data_offer_destroy(offer);
2287 }
2288}
2289
2290static void
2291data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2292 data_func_t func, void *user_data)
2293{
2294 int p[2];
2295
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002296 if (pipe2(p, O_CLOEXEC) == -1)
2297 return;
2298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002299 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2300 close(p[1]);
2301
2302 offer->io_task.run = offer_io_func;
2303 offer->fd = p[0];
2304 offer->func = func;
2305 offer->refcount++;
2306 offer->user_data = user_data;
2307
2308 display_watch_fd(offer->input->display,
2309 offer->fd, EPOLLIN, &offer->io_task);
2310}
2311
2312void
2313input_receive_drag_data(struct input *input, const char *mime_type,
2314 data_func_t func, void *data)
2315{
2316 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2317 input->drag_offer->x = input->sx;
2318 input->drag_offer->y = input->sy;
2319}
2320
2321int
2322input_receive_selection_data(struct input *input, const char *mime_type,
2323 data_func_t func, void *data)
2324{
2325 char **p;
2326
2327 if (input->selection_offer == NULL)
2328 return -1;
2329
2330 for (p = input->selection_offer->types.data; *p; p++)
2331 if (strcmp(mime_type, *p) == 0)
2332 break;
2333
2334 if (*p == NULL)
2335 return -1;
2336
2337 data_offer_receive_data(input->selection_offer,
2338 mime_type, func, data);
2339 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002340}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002341
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002342int
2343input_receive_selection_data_to_fd(struct input *input,
2344 const char *mime_type, int fd)
2345{
2346 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2347
2348 return 0;
2349}
2350
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002351void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002352window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002353{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002354 if (!window->shell_surface)
2355 return;
2356
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002357 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002358 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002359}
2360
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002361static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002362idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002363{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002364 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002365
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002366 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002367 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002368 widget_set_allocation(widget,
2369 window->pending_allocation.x,
2370 window->pending_allocation.y,
2371 window->pending_allocation.width,
2372 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002373
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002374 if (window->input_region) {
2375 wl_region_destroy(window->input_region);
2376 window->input_region = NULL;
2377 }
2378
2379 if (window->opaque_region) {
2380 wl_region_destroy(window->opaque_region);
2381 window->opaque_region = NULL;
2382 }
2383
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002384 if (widget->resize_handler)
2385 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002386 widget->allocation.width,
2387 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002388 widget->user_data);
2389
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002390 if (window->allocation.width != widget->allocation.width ||
2391 window->allocation.height != widget->allocation.height) {
2392 window->allocation = widget->allocation;
2393 window_schedule_redraw(window);
2394 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002395}
2396
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002397void
2398window_schedule_resize(struct window *window, int width, int height)
2399{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002400 window->pending_allocation.x = 0;
2401 window->pending_allocation.y = 0;
2402 window->pending_allocation.width = width;
2403 window->pending_allocation.height = height;
2404
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002405 window->resize_needed = 1;
2406 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002407}
2408
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002409void
2410widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2411{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002412 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002413}
2414
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002415static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002416handle_ping(void *data, struct wl_shell_surface *shell_surface,
2417 uint32_t serial)
2418{
2419 wl_shell_surface_pong(shell_surface, serial);
2420}
2421
2422static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002423handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002424 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002425{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002426 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002427
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002428 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002429 return;
2430
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002431 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002432 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002433}
2434
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002435static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002436menu_destroy(struct menu *menu)
2437{
2438 widget_destroy(menu->widget);
2439 window_destroy(menu->window);
2440 free(menu);
2441}
2442
2443static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002444handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2445{
2446 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002447 struct menu *menu = window->widget->user_data;
2448
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002449 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002450 * device. Or just use wl_callback. And this really needs to
2451 * be a window vfunc that the menu can set. And we need the
2452 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002453
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002454 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002455 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002456 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002457}
2458
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002459static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002460 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002461 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002462 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002463};
2464
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002465void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002466window_get_allocation(struct window *window,
2467 struct rectangle *allocation)
2468{
2469 *allocation = window->allocation;
2470}
2471
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002472static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002473widget_redraw(struct widget *widget)
2474{
2475 struct widget *child;
2476
2477 if (widget->redraw_handler)
2478 widget->redraw_handler(widget, widget->user_data);
2479 wl_list_for_each(child, &widget->child_list, link)
2480 widget_redraw(child);
2481}
2482
2483static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002484frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2485{
2486 struct window *window = data;
2487
2488 wl_callback_destroy(callback);
2489 window->redraw_scheduled = 0;
2490 if (window->redraw_needed)
2491 window_schedule_redraw(window);
2492}
2493
2494static const struct wl_callback_listener listener = {
2495 frame_callback
2496};
2497
2498static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002499idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002500{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002501 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002502 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002503
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002504 if (window->resize_needed)
2505 idle_resize(window);
2506
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002507 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002508 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002509 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002510 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002511 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002512
2513 callback = wl_surface_frame(window->surface);
2514 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002515}
2516
2517void
2518window_schedule_redraw(struct window *window)
2519{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002520 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002521 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002522 window->redraw_task.run = idle_redraw;
2523 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002524 window->redraw_scheduled = 1;
2525 }
2526}
2527
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002528void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002529window_set_custom(struct window *window)
2530{
2531 window->type = TYPE_CUSTOM;
2532}
2533
2534void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002535window_set_fullscreen(struct window *window, int fullscreen)
2536{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002537 if (!window->display->shell)
2538 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002539
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002540 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002541 return;
2542
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002543 if (fullscreen) {
2544 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002545 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002546 wl_shell_surface_set_fullscreen(window->shell_surface,
2547 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2548 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002549 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002550 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002551 wl_shell_surface_set_toplevel(window->shell_surface);
2552 window_schedule_resize(window,
2553 window->saved_allocation.width,
2554 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002555 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002556}
2557
2558void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002559window_set_maximized(struct window *window, int maximized)
2560{
2561 if (!window->display->shell)
2562 return;
2563
2564 if ((window->type == TYPE_MAXIMIZED) == maximized)
2565 return;
2566
2567 if (window->type == TYPE_TOPLEVEL) {
2568 window->saved_allocation = window->allocation;
2569 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2570 window->type = TYPE_MAXIMIZED;
2571 } else {
2572 wl_shell_surface_set_toplevel(window->shell_surface);
2573 window->type = TYPE_TOPLEVEL;
2574 window_schedule_resize(window,
2575 window->saved_allocation.width,
2576 window->saved_allocation.height);
2577 }
2578}
2579
2580void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002581window_set_user_data(struct window *window, void *data)
2582{
2583 window->user_data = data;
2584}
2585
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002586void *
2587window_get_user_data(struct window *window)
2588{
2589 return window->user_data;
2590}
2591
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002592void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002593window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002594 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002595{
2596 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002597}
2598
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002599void
2600window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002601 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002602{
2603 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002604}
2605
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002606void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002607window_set_data_handler(struct window *window, window_data_handler_t handler)
2608{
2609 window->data_handler = handler;
2610}
2611
2612void
2613window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2614{
2615 window->drop_handler = handler;
2616}
2617
2618void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002619window_set_close_handler(struct window *window,
2620 window_close_handler_t handler)
2621{
2622 window->close_handler = handler;
2623}
2624
2625void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002626window_set_title(struct window *window, const char *title)
2627{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002628 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002629 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002630 if (window->shell_surface)
2631 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002632}
2633
2634const char *
2635window_get_title(struct window *window)
2636{
2637 return window->title;
2638}
2639
2640void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002641window_damage(struct window *window, int32_t x, int32_t y,
2642 int32_t width, int32_t height)
2643{
2644 wl_surface_damage(window->surface, x, y, width, height);
2645}
2646
Casey Dahlin9074db52012-04-19 22:50:09 -04002647static void
2648surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002649 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002650{
Rob Bradford7507b572012-05-15 17:55:34 +01002651 struct window *window = data;
2652 struct output *output;
2653 struct output *output_found = NULL;
2654 struct window_output *window_output;
2655
2656 wl_list_for_each(output, &window->display->output_list, link) {
2657 if (output->output == wl_output) {
2658 output_found = output;
2659 break;
2660 }
2661 }
2662
2663 if (!output_found)
2664 return;
2665
2666 window_output = malloc (sizeof *window_output);
2667 window_output->output = output_found;
2668
2669 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002670}
2671
2672static void
2673surface_leave(void *data,
2674 struct wl_surface *wl_surface, struct wl_output *output)
2675{
Rob Bradford7507b572012-05-15 17:55:34 +01002676 struct window *window = data;
2677 struct window_output *window_output;
2678 struct window_output *window_output_found = NULL;
2679
2680 wl_list_for_each(window_output, &window->window_output_list, link) {
2681 if (window_output->output->output == output) {
2682 window_output_found = window_output;
2683 break;
2684 }
2685 }
2686
2687 if (window_output_found) {
2688 wl_list_remove(&window_output_found->link);
2689 free(window_output_found);
2690 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002691}
2692
2693static const struct wl_surface_listener surface_listener = {
2694 surface_enter,
2695 surface_leave
2696};
2697
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002698static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002699window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002700{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002701 struct window *window;
2702
2703 window = malloc(sizeof *window);
2704 if (window == NULL)
2705 return NULL;
2706
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002707 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002708 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002709 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002710 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002711 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002712 if (display->shell) {
2713 window->shell_surface =
2714 wl_shell_get_shell_surface(display->shell,
2715 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002716 if (window->title)
2717 wl_shell_surface_set_title(window->shell_surface,
2718 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002719 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002720 window->allocation.x = 0;
2721 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002722 window->allocation.width = 0;
2723 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002724 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002725 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002726 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002727 window->input_region = NULL;
2728 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002729
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002730 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002731#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002732 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002733#else
2734 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2735#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002736 else
2737 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002738
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002739 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002740 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002741 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002742
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002743 if (window->shell_surface) {
2744 wl_shell_surface_set_user_data(window->shell_surface, window);
2745 wl_shell_surface_add_listener(window->shell_surface,
2746 &shell_surface_listener, window);
2747 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002748
Rob Bradford7507b572012-05-15 17:55:34 +01002749 wl_list_init (&window->window_output_list);
2750
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002751 return window;
2752}
2753
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002754struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002755window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002756{
2757 struct window *window;
2758
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002759 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002760 if (!window)
2761 return NULL;
2762
2763 return window;
2764}
2765
2766struct window *
2767window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002768 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002769{
2770 struct window *window;
2771
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002772 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002773 if (!window)
2774 return NULL;
2775
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002776 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002777 window->x = x;
2778 window->y = y;
2779
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002780 if (display->shell)
2781 wl_shell_surface_set_transient(window->shell_surface,
2782 window->parent->shell_surface,
2783 window->x, window->y, 0);
2784
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002785 return window;
2786}
2787
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002788static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002789menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002790{
2791 int next;
2792
2793 next = (sy - 8) / 20;
2794 if (menu->current != next) {
2795 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002796 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002797 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002798}
2799
2800static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002801menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002802 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002803 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002804{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002805 struct menu *menu = data;
2806
2807 if (widget == menu->widget)
2808 menu_set_item(data, y);
2809
2810 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002811}
2812
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002813static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002814menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002815 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002816{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002817 struct menu *menu = data;
2818
2819 if (widget == menu->widget)
2820 menu_set_item(data, y);
2821
2822 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002823}
2824
2825static void
2826menu_leave_handler(struct widget *widget, struct input *input, void *data)
2827{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002828 struct menu *menu = data;
2829
2830 if (widget == menu->widget)
2831 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832}
2833
2834static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002835menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002836 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002837 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002838
2839{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002840 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002841
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002842 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002843 /* Either relase after press-drag-release or
2844 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002845 menu->func(menu->window->parent,
2846 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002847 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002848 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002849 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002850}
2851
2852static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002853menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854{
2855 cairo_t *cr;
2856 const int32_t r = 3, margin = 3;
2857 struct menu *menu = data;
2858 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002859 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002860
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002861 cr = cairo_create(window->cairo_surface);
2862 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2863 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2864 cairo_paint(cr);
2865
2866 width = window->allocation.width;
2867 height = window->allocation.height;
2868 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002869
2870 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002871 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2872 cairo_fill(cr);
2873
2874 for (i = 0; i < menu->count; i++) {
2875 if (i == menu->current) {
2876 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2877 cairo_rectangle(cr, margin, i * 20 + margin,
2878 width - 2 * margin, 20);
2879 cairo_fill(cr);
2880 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2881 cairo_move_to(cr, 10, i * 20 + 16);
2882 cairo_show_text(cr, menu->entries[i]);
2883 } else {
2884 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2885 cairo_move_to(cr, 10, i * 20 + 16);
2886 cairo_show_text(cr, menu->entries[i]);
2887 }
2888 }
2889
2890 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002891}
2892
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002893void
2894window_show_menu(struct display *display,
2895 struct input *input, uint32_t time, struct window *parent,
2896 int32_t x, int32_t y,
2897 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002898{
2899 struct window *window;
2900 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002901 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002902
2903 menu = malloc(sizeof *menu);
2904 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002905 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002906
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002907 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002908 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002909 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002910
2911 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002912 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002913 menu->entries = entries;
2914 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002915 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002917 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002918 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002919 window->type = TYPE_MENU;
2920 window->x = x;
2921 window->y = y;
2922
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002923 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002924 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2925 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926 window->parent->shell_surface,
2927 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002928
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002929 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002930 widget_set_enter_handler(menu->widget, menu_enter_handler);
2931 widget_set_leave_handler(menu->widget, menu_leave_handler);
2932 widget_set_motion_handler(menu->widget, menu_motion_handler);
2933 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002934
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002935 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002936 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002937}
2938
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002939void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002940window_set_buffer_type(struct window *window, enum window_buffer_type type)
2941{
2942 window->buffer_type = type;
2943}
2944
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002945
2946static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002947display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002948 struct wl_output *wl_output,
2949 int x, int y,
2950 int physical_width,
2951 int physical_height,
2952 int subpixel,
2953 const char *make,
2954 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002955{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002956 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002957
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002958 output->allocation.x = x;
2959 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002960}
2961
2962static void
2963display_handle_mode(void *data,
2964 struct wl_output *wl_output,
2965 uint32_t flags,
2966 int width,
2967 int height,
2968 int refresh)
2969{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002970 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002971 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002972
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002973 if (flags & WL_OUTPUT_MODE_CURRENT) {
2974 output->allocation.width = width;
2975 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002976 if (display->output_configure_handler)
2977 (*display->output_configure_handler)(
2978 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002979 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002980}
2981
2982static const struct wl_output_listener output_listener = {
2983 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002984 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002985};
2986
2987static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002988display_add_output(struct display *d, uint32_t id)
2989{
2990 struct output *output;
2991
2992 output = malloc(sizeof *output);
2993 if (output == NULL)
2994 return;
2995
2996 memset(output, 0, sizeof *output);
2997 output->display = d;
2998 output->output =
2999 wl_display_bind(d->display, id, &wl_output_interface);
3000 wl_list_insert(d->output_list.prev, &output->link);
3001
3002 wl_output_add_listener(output->output, &output_listener, output);
3003}
3004
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003005static void
3006output_destroy(struct output *output)
3007{
3008 if (output->destroy_handler)
3009 (*output->destroy_handler)(output, output->user_data);
3010
3011 wl_output_destroy(output->output);
3012 wl_list_remove(&output->link);
3013 free(output);
3014}
3015
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003016void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003017display_set_output_configure_handler(struct display *display,
3018 display_output_handler_t handler)
3019{
3020 struct output *output;
3021
3022 display->output_configure_handler = handler;
3023 if (!handler)
3024 return;
3025
3026 wl_list_for_each(output, &display->output_list, link)
3027 (*display->output_configure_handler)(output,
3028 display->user_data);
3029}
3030
3031void
3032output_set_user_data(struct output *output, void *data)
3033{
3034 output->user_data = data;
3035}
3036
3037void *
3038output_get_user_data(struct output *output)
3039{
3040 return output->user_data;
3041}
3042
3043void
3044output_set_destroy_handler(struct output *output,
3045 display_output_handler_t handler)
3046{
3047 output->destroy_handler = handler;
3048 /* FIXME: implement this, once we have way to remove outputs */
3049}
3050
3051void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003052output_get_allocation(struct output *output, struct rectangle *allocation)
3053{
3054 *allocation = output->allocation;
3055}
3056
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003057struct wl_output *
3058output_get_wl_output(struct output *output)
3059{
3060 return output->output;
3061}
3062
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003063static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003064display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003065{
3066 struct input *input;
3067
3068 input = malloc(sizeof *input);
3069 if (input == NULL)
3070 return;
3071
3072 memset(input, 0, sizeof *input);
3073 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003074 input->input_device =
3075 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003076 input->pointer_focus = NULL;
3077 input->keyboard_focus = NULL;
3078 wl_list_insert(d->input_list.prev, &input->link);
3079
3080 wl_input_device_add_listener(input->input_device,
3081 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003082 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003083
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003084 input->data_device =
3085 wl_data_device_manager_get_data_device(d->data_device_manager,
3086 input->input_device);
3087 wl_data_device_add_listener(input->data_device,
3088 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003089}
3090
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003091static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003092input_destroy(struct input *input)
3093{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003094 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003095 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003096
3097 if (input->drag_offer)
3098 data_offer_destroy(input->drag_offer);
3099
3100 if (input->selection_offer)
3101 data_offer_destroy(input->selection_offer);
3102
3103 wl_data_device_destroy(input->data_device);
3104 wl_list_remove(&input->link);
3105 wl_input_device_destroy(input->input_device);
3106 free(input);
3107}
3108
3109static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003110display_handle_global(struct wl_display *display, uint32_t id,
3111 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003112{
3113 struct display *d = data;
3114
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003115 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003116 d->compositor =
3117 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003118 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003119 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003120 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003121 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003122 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003123 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003124 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003125 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003126 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3127 d->data_device_manager =
3128 wl_display_bind(display, id,
3129 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003130 }
3131}
3132
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003133static void
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003134display_render_theme(struct theme *t)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003135{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003136 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003137 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003138
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003139 t->margin = 32;
3140 t->width = 6;
3141 t->titlebar_height = 27;
3142 t->frame_radius = 3;
3143 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3144 cr = cairo_create(t->shadow);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003145 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3146 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003147 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003148 cairo_fill(cr);
3149 cairo_destroy(cr);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003150 blur_surface(t->shadow, 64);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003151
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003152 t->active_frame =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003153 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003154 cr = cairo_create(t->active_frame);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003155 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003156
3157 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
3158 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
3159 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
3160 cairo_set_source(cr, pattern);
3161 cairo_pattern_destroy(pattern);
3162
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003163 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003164 cairo_fill(cr);
3165 cairo_destroy(cr);
3166
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003167 t->inactive_frame =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003168 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003169 cr = cairo_create(t->inactive_frame);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003170 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003171 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003172 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003173 cairo_fill(cr);
3174 cairo_destroy(cr);
3175}
3176
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003177static void
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003178fini_theme(struct theme *t)
3179{
3180 cairo_surface_destroy(t->active_frame);
3181 cairo_surface_destroy(t->inactive_frame);
3182 cairo_surface_destroy(t->shadow);
3183}
3184
3185static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003186init_xkb(struct display *d)
3187{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003188 d->xkb.names.rules = "evdev";
3189 d->xkb.names.model = "pc105";
3190 d->xkb.names.layout = (char *) option_xkb_layout;
3191 d->xkb.names.variant = (char *) option_xkb_variant;
3192 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003193
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003194 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003195 if (!d->xkb.context) {
3196 fprintf(stderr, "Failed to create XKB context\n");
3197 exit(1);
3198 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003199
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003200 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003201 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003202 fprintf(stderr, "Failed to compile keymap\n");
3203 exit(1);
3204 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003205
3206 d->xkb.state = xkb_state_new(d->xkb.keymap);
3207 if (!d->xkb.state) {
3208 fprintf(stderr, "Failed to create XKB state\n");
3209 exit(1);
3210 }
3211
3212 d->xkb.control_mask =
3213 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3214 d->xkb.alt_mask =
3215 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3216 d->xkb.shift_mask =
3217 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3218
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003219}
3220
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003221static void
3222fini_xkb(struct display *display)
3223{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003224 xkb_state_unref(display->xkb.state);
3225 xkb_map_unref(display->xkb.keymap);
3226 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003227}
3228
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003229#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003230static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003231init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003232{
3233 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003234 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003235
Rob Clark6396ed32012-03-11 19:48:41 -05003236#ifdef USE_CAIRO_GLESV2
3237# define GL_BIT EGL_OPENGL_ES2_BIT
3238#else
3239# define GL_BIT EGL_OPENGL_BIT
3240#endif
3241
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003242 static const EGLint argb_cfg_attribs[] = {
3243 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003244 EGL_RED_SIZE, 1,
3245 EGL_GREEN_SIZE, 1,
3246 EGL_BLUE_SIZE, 1,
3247 EGL_ALPHA_SIZE, 1,
3248 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003249 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003250 EGL_NONE
3251 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003252
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003253#ifdef USE_CAIRO_GLESV2
3254 static const EGLint context_attribs[] = {
3255 EGL_CONTEXT_CLIENT_VERSION, 2,
3256 EGL_NONE
3257 };
3258 EGLint api = EGL_OPENGL_ES_API;
3259#else
3260 EGLint *context_attribs = NULL;
3261 EGLint api = EGL_OPENGL_API;
3262#endif
3263
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003264 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003265 if (!eglInitialize(d->dpy, &major, &minor)) {
3266 fprintf(stderr, "failed to initialize display\n");
3267 return -1;
3268 }
3269
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003270 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003271 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3272 return -1;
3273 }
3274
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003275 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3276 &d->argb_config, 1, &n) || n != 1) {
3277 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003278 return -1;
3279 }
3280
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003281 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003282 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003283 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003284 fprintf(stderr, "failed to create context\n");
3285 return -1;
3286 }
3287
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003288 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003289 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003290 return -1;
3291 }
3292
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003293#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003294 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3295 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3296 fprintf(stderr, "failed to get cairo egl argb device\n");
3297 return -1;
3298 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003299#endif
3300
3301 return 0;
3302}
3303
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003304static void
3305fini_egl(struct display *display)
3306{
3307#ifdef HAVE_CAIRO_EGL
3308 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003309#endif
3310
3311 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3312 EGL_NO_CONTEXT);
3313
3314 eglTerminate(display->dpy);
3315 eglReleaseThread();
3316}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003317#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003318
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003319static int
3320event_mask_update(uint32_t mask, void *data)
3321{
3322 struct display *d = data;
3323
3324 d->mask = mask;
3325
3326 return 0;
3327}
3328
3329static void
3330handle_display_data(struct task *task, uint32_t events)
3331{
3332 struct display *display =
3333 container_of(task, struct display, display_task);
3334
3335 wl_display_iterate(display->display, display->mask);
3336}
3337
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003338struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003339display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003340{
3341 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003342
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003343 argc = parse_options(xkb_options,
3344 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003345
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003346 d = malloc(sizeof *d);
3347 if (d == NULL)
3348 return NULL;
3349
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003350 memset(d, 0, sizeof *d);
3351
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003352 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003353 if (d->display == NULL) {
3354 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003355 return NULL;
3356 }
3357
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003358 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3359 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3360 d->display_task.run = handle_display_data;
3361 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3362
3363 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003364 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003365 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003366
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003367 /* Set up listener so we'll catch all events. */
3368 wl_display_add_global_listener(d->display,
3369 display_handle_global, d);
3370
3371 /* Process connection events. */
3372 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003373#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003374 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003375 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003376#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003377
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003378 d->image_target_texture_2d =
3379 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3380 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3381 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3382
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003383 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003384
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003385 display_render_theme(&d->theme);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003386
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003387 wl_list_init(&d->window_list);
3388
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003389 init_xkb(d);
3390
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003391 return d;
3392}
3393
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003394static void
3395display_destroy_outputs(struct display *display)
3396{
3397 struct output *tmp;
3398 struct output *output;
3399
3400 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3401 output_destroy(output);
3402}
3403
Pekka Paalanene1207c72011-12-16 12:02:09 +02003404static void
3405display_destroy_inputs(struct display *display)
3406{
3407 struct input *tmp;
3408 struct input *input;
3409
3410 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3411 input_destroy(input);
3412}
3413
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003414void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003415display_destroy(struct display *display)
3416{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003417 if (!wl_list_empty(&display->window_list))
3418 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3419
3420 if (!wl_list_empty(&display->deferred_list))
3421 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3422
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003423 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003424 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003425
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003426 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003427
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003428 fini_theme(&display->theme);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003429 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003430
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003431#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003432 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003433#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003434
Pekka Paalanenc2052982011-12-16 11:41:32 +02003435 if (display->shell)
3436 wl_shell_destroy(display->shell);
3437
3438 if (display->shm)
3439 wl_shm_destroy(display->shm);
3440
3441 if (display->data_device_manager)
3442 wl_data_device_manager_destroy(display->data_device_manager);
3443
3444 wl_compositor_destroy(display->compositor);
3445
3446 close(display->epoll_fd);
3447
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003448 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003449 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003450 free(display);
3451}
3452
3453void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003454display_set_user_data(struct display *display, void *data)
3455{
3456 display->user_data = data;
3457}
3458
3459void *
3460display_get_user_data(struct display *display)
3461{
3462 return display->user_data;
3463}
3464
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003465struct wl_display *
3466display_get_display(struct display *display)
3467{
3468 return display->display;
3469}
3470
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003471struct output *
3472display_get_output(struct display *display)
3473{
3474 return container_of(display->output_list.next, struct output, link);
3475}
3476
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003477struct wl_compositor *
3478display_get_compositor(struct display *display)
3479{
3480 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003481}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003482
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003483uint32_t
3484display_get_serial(struct display *display)
3485{
3486 return display->serial;
3487}
3488
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003489EGLDisplay
3490display_get_egl_display(struct display *d)
3491{
3492 return d->dpy;
3493}
3494
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495struct wl_data_source *
3496display_create_data_source(struct display *display)
3497{
3498 return wl_data_device_manager_create_data_source(display->data_device_manager);
3499}
3500
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003501EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003502display_get_argb_egl_config(struct display *d)
3503{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003504 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003505}
3506
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003507struct wl_shell *
3508display_get_shell(struct display *display)
3509{
3510 return display->shell;
3511}
3512
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003513int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003514display_acquire_window_surface(struct display *display,
3515 struct window *window,
3516 EGLContext ctx)
3517{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003518#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003519 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003520 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003521
3522 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003523 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003524 device = cairo_surface_get_device(window->cairo_surface);
3525 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003526 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003527
Benjamin Franzke0c991632011-09-27 21:57:31 +02003528 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003529 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003530 ctx = display->argb_ctx;
3531 else
3532 assert(0);
3533 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003534
3535 data = cairo_surface_get_user_data(window->cairo_surface,
3536 &surface_data_key);
3537
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003538 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003539 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003540 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3541 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003542
3543 return 0;
3544#else
3545 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003546#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003547}
3548
3549void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003550display_release_window_surface(struct display *display,
3551 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003552{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003553#ifdef HAVE_CAIRO_EGL
3554 cairo_device_t *device;
3555
3556 device = cairo_surface_get_device(window->cairo_surface);
3557 if (!device)
3558 return;
3559
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003560 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003561 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003562 cairo_device_release(device);
3563#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003564}
3565
3566void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003567display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003568{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003569 wl_list_insert(&display->deferred_list, &task->link);
3570}
3571
3572void
3573display_watch_fd(struct display *display,
3574 int fd, uint32_t events, struct task *task)
3575{
3576 struct epoll_event ep;
3577
3578 ep.events = events;
3579 ep.data.ptr = task;
3580 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3581}
3582
3583void
3584display_run(struct display *display)
3585{
3586 struct task *task;
3587 struct epoll_event ep[16];
3588 int i, count;
3589
Pekka Paalanen826d7952011-12-15 10:14:07 +02003590 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003591 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003592 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003593
3594 while (!wl_list_empty(&display->deferred_list)) {
3595 task = container_of(display->deferred_list.next,
3596 struct task, link);
3597 wl_list_remove(&task->link);
3598 task->run(task, 0);
3599 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003600
3601 if (!display->running)
3602 break;
3603
3604 wl_display_flush(display->display);
3605
3606 count = epoll_wait(display->epoll_fd,
3607 ep, ARRAY_LENGTH(ep), -1);
3608 for (i = 0; i < count; i++) {
3609 task = ep[i].data.ptr;
3610 task->run(task, ep[i].events);
3611 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003612 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003613}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003614
3615void
3616display_exit(struct display *display)
3617{
3618 display->running = 0;
3619}