blob: 57443d9164b2b209ed2d64401da06fd8202a28b6 [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>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300192 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100205
206 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100207 struct xkb_keymap *keymap;
208 struct xkb_state *state;
209 xkb_mod_mask_t control_mask;
210 xkb_mod_mask_t alt_mask;
211 xkb_mod_mask_t shift_mask;
212 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400213};
214
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215struct output {
216 struct display *display;
217 struct wl_output *output;
218 struct rectangle allocation;
219 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200220
221 display_output_handler_t destroy_handler;
222 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500223};
224
Martin Minarik1998b152012-05-10 02:04:35 +0200225enum frame_button_action {
226 FRAME_BUTTON_NULL = 0,
227 FRAME_BUTTON_ICON = 1,
228 FRAME_BUTTON_CLOSE = 2,
229 FRAME_BUTTON_MINIMIZE = 3,
230 FRAME_BUTTON_MAXIMIZE = 4,
231};
232
233enum frame_button_pointer {
234 FRAME_BUTTON_DEFAULT = 0,
235 FRAME_BUTTON_OVER = 1,
236 FRAME_BUTTON_ACTIVE = 2,
237};
238
239enum frame_button_align {
240 FRAME_BUTTON_RIGHT = 0,
241 FRAME_BUTTON_LEFT = 1,
242};
243
244enum frame_button_decoration {
245 FRAME_BUTTON_NONE = 0,
246 FRAME_BUTTON_FANCY = 1,
247};
248
249struct frame_button {
250 struct widget *widget;
251 struct frame *frame;
252 cairo_surface_t *icon;
253 enum frame_button_action type;
254 enum frame_button_pointer state;
255 struct wl_list link; /* buttons_list */
256 enum frame_button_align align;
257 enum frame_button_decoration decoration;
258};
259
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500260struct frame {
261 struct widget *widget;
262 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200263 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500264};
265
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500266struct menu {
267 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500268 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500269 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500270 const char **entries;
271 uint32_t time;
272 int current;
273 int count;
274 menu_func_t func;
275};
276
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300277struct tooltip {
278 struct widget *parent;
279 struct window *window;
280 struct widget *widget;
281 char *entry;
282 struct task tooltip_task;
283 int tooltip_fd;
284 float x, y;
285};
286
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700287struct shm_pool {
288 struct wl_shm_pool *pool;
289 size_t size;
290 size_t used;
291 void *data;
292};
293
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400294enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300295 CURSOR_DEFAULT = 100,
296 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400297};
298
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500299enum window_location {
300 WINDOW_INTERIOR = 0,
301 WINDOW_RESIZING_TOP = 1,
302 WINDOW_RESIZING_BOTTOM = 2,
303 WINDOW_RESIZING_LEFT = 4,
304 WINDOW_RESIZING_TOP_LEFT = 5,
305 WINDOW_RESIZING_BOTTOM_LEFT = 6,
306 WINDOW_RESIZING_RIGHT = 8,
307 WINDOW_RESIZING_TOP_RIGHT = 9,
308 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
309 WINDOW_RESIZING_MASK = 15,
310 WINDOW_EXTERIOR = 16,
311 WINDOW_TITLEBAR = 17,
312 WINDOW_CLIENT_AREA = 18,
313};
314
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400315const char *option_xkb_layout = "us";
316const char *option_xkb_variant = "";
317const char *option_xkb_options = "";
318
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400319static const struct weston_option xkb_options[] = {
320 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
321 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
322 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400323};
324
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325static const cairo_user_data_key_t surface_data_key;
326struct surface_data {
327 struct wl_buffer *buffer;
328};
329
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500330#define MULT(_d,c,a,t) \
331 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
332
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500333#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100335struct egl_window_surface_data {
336 struct display *display;
337 struct wl_surface *surface;
338 struct wl_egl_window *window;
339 EGLSurface surf;
340};
341
342static void
343egl_window_surface_data_destroy(void *p)
344{
345 struct egl_window_surface_data *data = p;
346 struct display *d = data->display;
347
348 eglDestroySurface(d->dpy, data->surf);
349 wl_egl_window_destroy(data->window);
350 data->surface = NULL;
351
352 free(p);
353}
354
355static cairo_surface_t *
356display_create_egl_window_surface(struct display *display,
357 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400358 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359 struct rectangle *rectangle)
360{
361 cairo_surface_t *cairo_surface;
362 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200364 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100365
366 data = malloc(sizeof *data);
367 if (data == NULL)
368 return NULL;
369
370 data->display = display;
371 data->surface = surface;
372
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500373 config = display->argb_config;
374 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400376 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500381 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100382
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384 data->surf,
385 rectangle->width,
386 rectangle->height);
387
388 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
389 data, egl_window_surface_data_destroy);
390
391 return cairo_surface;
392}
393
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394#endif
395
396struct wl_buffer *
397display_get_buffer_for_surface(struct display *display,
398 cairo_surface_t *surface)
399{
400 struct surface_data *data;
401
402 data = cairo_surface_get_user_data (surface, &surface_data_key);
403
404 return data->buffer;
405}
406
407struct shm_surface_data {
408 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410};
411
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500412static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700413shm_pool_destroy(struct shm_pool *pool);
414
415static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416shm_surface_data_destroy(void *p)
417{
418 struct shm_surface_data *data = p;
419
420 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700421 if (data->pool)
422 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423}
424
Kristian Høgsberg16626282012-04-03 11:21:27 -0400425static struct wl_shm_pool *
426make_shm_pool(struct display *display, int size, void **data)
427{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400428 struct wl_shm_pool *pool;
429 int fd;
430
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300431 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400432 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300433 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
434 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435 return NULL;
436 }
437
438 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400439 if (*data == MAP_FAILED) {
440 fprintf(stderr, "mmap failed: %m\n");
441 close(fd);
442 return NULL;
443 }
444
445 pool = wl_shm_create_pool(display->shm, fd, size);
446
447 close(fd);
448
449 return pool;
450}
451
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700452static struct shm_pool *
453shm_pool_create(struct display *display, size_t size)
454{
455 struct shm_pool *pool = malloc(sizeof *pool);
456
457 if (!pool)
458 return NULL;
459
460 pool->pool = make_shm_pool(display, size, &pool->data);
461 if (!pool->pool) {
462 free(pool);
463 return NULL;
464 }
465
466 pool->size = size;
467 pool->used = 0;
468
469 return pool;
470}
471
472static void *
473shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
474{
475 if (pool->used + size > pool->size)
476 return NULL;
477
478 *offset = pool->used;
479 pool->used += size;
480
481 return (char *) pool->data + *offset;
482}
483
484/* destroy the pool. this does not unmap the memory though */
485static void
486shm_pool_destroy(struct shm_pool *pool)
487{
488 munmap(pool->data, pool->size);
489 wl_shm_pool_destroy(pool->pool);
490 free(pool);
491}
492
493/* Start allocating from the beginning of the pool again */
494static void
495shm_pool_reset(struct shm_pool *pool)
496{
497 pool->used = 0;
498}
499
500static int
501data_length_for_shm_surface(struct rectangle *rect)
502{
503 int stride;
504
505 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
506 rect->width);
507 return stride * rect->height;
508}
509
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500510static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700511display_create_shm_surface_from_pool(struct display *display,
512 struct rectangle *rectangle,
513 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400514{
515 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400516 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400517 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700518 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400519 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520
521 data = malloc(sizeof *data);
522 if (data == NULL)
523 return NULL;
524
525 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
526 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700527 length = stride * rectangle->height;
528 data->pool = NULL;
529 map = shm_pool_allocate(pool, length, &offset);
530
531 if (!map) {
532 free(data);
533 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400534 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400535
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400536 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537 CAIRO_FORMAT_ARGB32,
538 rectangle->width,
539 rectangle->height,
540 stride);
541
542 cairo_surface_set_user_data (surface, &surface_data_key,
543 data, shm_surface_data_destroy);
544
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500546 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500548 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700550 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400551 rectangle->width,
552 rectangle->height,
553 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700555 return surface;
556}
557
558static cairo_surface_t *
559display_create_shm_surface(struct display *display,
560 struct rectangle *rectangle, uint32_t flags,
561 struct window *window)
562{
563 struct shm_surface_data *data;
564 struct shm_pool *pool;
565 cairo_surface_t *surface;
566
567 if (window && window->pool) {
568 shm_pool_reset(window->pool);
569 surface = display_create_shm_surface_from_pool(display,
570 rectangle,
571 flags,
572 window->pool);
573 if (surface)
574 return surface;
575 }
576
577 pool = shm_pool_create(display,
578 data_length_for_shm_surface(rectangle));
579 if (!pool)
580 return NULL;
581
582 surface =
583 display_create_shm_surface_from_pool(display, rectangle,
584 flags, pool);
585
586 if (!surface) {
587 shm_pool_destroy(pool);
588 return NULL;
589 }
590
591 /* make sure we destroy the pool when the surface is destroyed */
592 data = cairo_surface_get_user_data(surface, &surface_data_key);
593 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594
595 return surface;
596}
597
nobled7b87cb02011-02-01 18:51:47 +0000598static int
599check_size(struct rectangle *rect)
600{
601 if (rect->width && rect->height)
602 return 0;
603
604 fprintf(stderr, "tried to create surface of "
605 "width: %d, height: %d\n", rect->width, rect->height);
606 return -1;
607}
608
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400609cairo_surface_t *
610display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100611 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400612 struct rectangle *rectangle,
613 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614{
nobled7b87cb02011-02-01 18:51:47 +0000615 if (check_size(rectangle) < 0)
616 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500617#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300618 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400619 return display_create_egl_window_surface(display,
620 surface,
621 flags,
622 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400624 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625}
626
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300627static const char *cursors[] = {
628 "bottom_left_corner",
629 "bottom_right_corner",
630 "bottom_side",
631 "grabbing",
632 "left_ptr",
633 "left_side",
634 "right_side",
635 "top_left_corner",
636 "top_right_corner",
637 "top_side",
638 "xterm",
639 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400640 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300641};
642
643static void
644create_cursors(struct display *display)
645{
646 unsigned int i;
647
648 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
649 display->cursors =
650 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
651
652 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
653 display->cursors[i] =
654 wl_cursor_theme_get_cursor(display->cursor_theme,
655 cursors[i]);
656}
657
658static void
659destroy_cursors(struct display *display)
660{
661 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800662 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663}
664
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300665struct wl_cursor_image *
666display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400667{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300668 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300669 wl_cursor_theme_get_cursor(display->cursor_theme,
670 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300672 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400673}
674
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400675static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200676window_get_resize_dx_dy(struct window *window, int *x, int *y)
677{
678 if (window->resize_edges & WINDOW_RESIZING_LEFT)
679 *x = window->server_allocation.width - window->allocation.width;
680 else
681 *x = 0;
682
683 if (window->resize_edges & WINDOW_RESIZING_TOP)
684 *y = window->server_allocation.height -
685 window->allocation.height;
686 else
687 *y = 0;
688
689 window->resize_edges = 0;
690}
691
692static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500693window_attach_surface(struct window *window)
694{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400695 struct display *display = window->display;
696 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000697#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100698 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000699#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500700 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100701
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400702 if (window->type == TYPE_NONE) {
703 window->type = TYPE_TOPLEVEL;
704 if (display->shell)
705 wl_shell_surface_set_toplevel(window->shell_surface);
706 }
707
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000709#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100710 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
711 data = cairo_surface_get_user_data(window->cairo_surface,
712 &surface_data_key);
713
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 cairo_gl_surface_swapbuffers(window->cairo_surface);
715 wl_egl_window_get_attached_size(data->window,
716 &window->server_allocation.width,
717 &window->server_allocation.height);
718 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000719#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 buffer =
722 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400723 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200725 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400727 wl_surface_damage(window->surface, 0, 0,
728 window->allocation.width,
729 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400731 cairo_surface_destroy(window->cairo_surface);
732 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000734 default:
735 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500737
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500738 if (window->input_region) {
739 wl_surface_set_input_region(window->surface,
740 window->input_region);
741 wl_region_destroy(window->input_region);
742 window->input_region = NULL;
743 }
744
745 if (window->opaque_region) {
746 wl_surface_set_opaque_region(window->surface,
747 window->opaque_region);
748 wl_region_destroy(window->opaque_region);
749 window->opaque_region = NULL;
750 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500751}
752
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500753void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500755{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400756 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100757 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500758}
759
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400760void
761window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400762{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500763 cairo_surface_reference(surface);
764
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400765 if (window->cairo_surface != NULL)
766 cairo_surface_destroy(window->cairo_surface);
767
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500768 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400769}
770
Benjamin Franzke22d54812011-07-16 19:50:32 +0000771#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772static void
773window_resize_cairo_window_surface(struct window *window)
774{
775 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200776 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777
778 data = cairo_surface_get_user_data(window->cairo_surface,
779 &surface_data_key);
780
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200781 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782 wl_egl_window_resize(data->window,
783 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200784 window->allocation.height,
785 x,y);
786
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 cairo_gl_surface_set_size(window->cairo_surface,
788 window->allocation.width,
789 window->allocation.height);
790}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000791#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400793struct display *
794window_get_display(struct window *window)
795{
796 return window->display;
797}
798
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400799void
800window_create_surface(struct window *window)
801{
802 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400803 uint32_t flags = 0;
804
805 if (!window->transparent)
806 flags = SURFACE_OPAQUE;
807
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400808 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500809#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
811 if (window->cairo_surface) {
812 window_resize_cairo_window_surface(window);
813 return;
814 }
815 surface = display_create_surface(window->display,
816 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400817 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819#endif
820 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400821 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400822 &window->allocation,
823 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800825 default:
826 surface = NULL;
827 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400829
830 window_set_surface(window, surface);
831 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832}
833
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200834static void frame_destroy(struct frame *frame);
835
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400836void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500837window_destroy(struct window *window)
838{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200839 struct display *display = window->display;
840 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100841 struct window_output *window_output;
842 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200843
844 if (window->redraw_scheduled)
845 wl_list_remove(&window->redraw_task.link);
846
847 wl_list_for_each(input, &display->input_list, link) {
848 if (input->pointer_focus == window)
849 input->pointer_focus = NULL;
850 if (input->keyboard_focus == window)
851 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500852 if (input->focus_widget &&
853 input->focus_widget->window == window)
854 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200855 }
856
Rob Bradford7507b572012-05-15 17:55:34 +0100857 wl_list_for_each_safe(window_output, window_output_tmp,
858 &window->window_output_list, link) {
859 free (window_output);
860 }
861
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500862 if (window->input_region)
863 wl_region_destroy(window->input_region);
864 if (window->opaque_region)
865 wl_region_destroy(window->opaque_region);
866
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200867 if (window->frame)
868 frame_destroy(window->frame);
869
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200870 if (window->shell_surface)
871 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500872 wl_surface_destroy(window->surface);
873 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200874
875 if (window->cairo_surface != NULL)
876 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200877
878 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500879 free(window);
880}
881
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500882static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500883widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400884{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500885 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400886
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500887 wl_list_for_each(child, &widget->child_list, link) {
888 target = widget_find_widget(child, x, y);
889 if (target)
890 return target;
891 }
892
893 if (widget->allocation.x <= x &&
894 x < widget->allocation.x + widget->allocation.width &&
895 widget->allocation.y <= y &&
896 y < widget->allocation.y + widget->allocation.height) {
897 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400898 }
899
900 return NULL;
901}
902
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500903static struct widget *
904widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500906 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400907
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908 widget = malloc(sizeof *widget);
909 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500910 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500911 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500912 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500913 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500914 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300915 widget->tooltip = NULL;
916 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500917
918 return widget;
919}
920
921struct widget *
922window_add_widget(struct window *window, void *data)
923{
924 window->widget = widget_create(window, data);
925 wl_list_init(&window->widget->link);
926
927 return window->widget;
928}
929
930struct widget *
931widget_add_widget(struct widget *parent, void *data)
932{
933 struct widget *widget;
934
935 widget = widget_create(parent->window, data);
936 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400937
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500938 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939}
940
941void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500942widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400943{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200944 struct display *display = widget->window->display;
945 struct input *input;
946
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300947 if (widget->tooltip) {
948 free(widget->tooltip);
949 widget->tooltip = NULL;
950 }
951
Pekka Paalanene156fb62012-01-19 13:51:38 +0200952 wl_list_for_each(input, &display->input_list, link) {
953 if (input->focus_widget == widget)
954 input->focus_widget = NULL;
955 }
956
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500957 wl_list_remove(&widget->link);
958 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400959}
960
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400961void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500962widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965}
966
967void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500968widget_set_size(struct widget *widget, int32_t width, int32_t height)
969{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500970 widget->allocation.width = width;
971 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500972}
973
974void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500975widget_set_allocation(struct widget *widget,
976 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500978 widget->allocation.x = x;
979 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200980 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400981}
982
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500983void
984widget_set_transparent(struct widget *widget, int transparent)
985{
986 widget->opaque = !transparent;
987}
988
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500990widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500992 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993}
994
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500995void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500996widget_set_resize_handler(struct widget *widget,
997 widget_resize_handler_t handler)
998{
999 widget->resize_handler = handler;
1000}
1001
1002void
1003widget_set_redraw_handler(struct widget *widget,
1004 widget_redraw_handler_t handler)
1005{
1006 widget->redraw_handler = handler;
1007}
1008
1009void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001010widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001011{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001012 widget->enter_handler = handler;
1013}
1014
1015void
1016widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1017{
1018 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001019}
1020
1021void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001022widget_set_motion_handler(struct widget *widget,
1023 widget_motion_handler_t handler)
1024{
1025 widget->motion_handler = handler;
1026}
1027
1028void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001029widget_set_button_handler(struct widget *widget,
1030 widget_button_handler_t handler)
1031{
1032 widget->button_handler = handler;
1033}
1034
1035void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001036widget_schedule_redraw(struct widget *widget)
1037{
1038 window_schedule_redraw(widget->window);
1039}
1040
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001041cairo_surface_t *
1042window_get_surface(struct window *window)
1043{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001044 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001045}
1046
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001047struct wl_surface *
1048window_get_wl_surface(struct window *window)
1049{
1050 return window->surface;
1051}
1052
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001053struct wl_shell_surface *
1054window_get_wl_shell_surface(struct window *window)
1055{
1056 return window->shell_surface;
1057}
1058
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001059static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001060tooltip_redraw_handler(struct widget *widget, void *data)
1061{
1062 cairo_t *cr;
1063 const int32_t r = 3;
1064 struct tooltip *tooltip = data;
1065 int32_t width, height;
1066 struct window *window = widget->window;
1067
1068 cr = cairo_create(window->cairo_surface);
1069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1070 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1071 cairo_paint(cr);
1072
1073 width = window->allocation.width;
1074 height = window->allocation.height;
1075 rounded_rect(cr, 0, 0, width, height, r);
1076
1077 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1078 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1079 cairo_fill(cr);
1080
1081 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1082 cairo_move_to(cr, 10, 16);
1083 cairo_show_text(cr, tooltip->entry);
1084 cairo_destroy(cr);
1085}
1086
1087static cairo_text_extents_t
1088get_text_extents(struct tooltip *tooltip)
1089{
1090 struct window *window;
1091 cairo_t *cr;
1092 cairo_text_extents_t extents;
1093
1094 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1095 * created yet */
1096 window = tooltip->widget->window->parent;
1097 cr = cairo_create(window->cairo_surface);
1098 cairo_text_extents(cr, tooltip->entry, &extents);
1099 cairo_destroy(cr);
1100
1101 return extents;
1102}
1103
1104static int
1105window_create_tooltip(struct tooltip *tooltip)
1106{
1107 struct widget *parent = tooltip->parent;
1108 struct display *display = parent->window->display;
1109 struct window *window;
1110 const int offset_y = 27;
1111 const int margin = 3;
1112 cairo_text_extents_t extents;
1113
1114 if (tooltip->widget)
1115 return 0;
1116
1117 window = window_create_transient(display, parent->window, tooltip->x,
1118 tooltip->y + offset_y,
1119 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1120 if (!window)
1121 return -1;
1122
1123 tooltip->window = window;
1124 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1125
1126 extents = get_text_extents(tooltip);
1127 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1128 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1129
1130 return 0;
1131}
1132
1133void
1134widget_destroy_tooltip(struct widget *parent)
1135{
1136 struct tooltip *tooltip = parent->tooltip;
1137
1138 parent->tooltip_count = 0;
1139 if (!tooltip)
1140 return;
1141
1142 if (tooltip->widget) {
1143 widget_destroy(tooltip->widget);
1144 window_destroy(tooltip->window);
1145 tooltip->widget = NULL;
1146 tooltip->window = NULL;
1147 }
1148
1149 close(tooltip->tooltip_fd);
1150 free(tooltip->entry);
1151 free(tooltip);
1152 parent->tooltip = NULL;
1153}
1154
1155static void
1156tooltip_func(struct task *task, uint32_t events)
1157{
1158 struct tooltip *tooltip =
1159 container_of(task, struct tooltip, tooltip_task);
1160 uint64_t exp;
1161
1162 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1163 window_create_tooltip(tooltip);
1164}
1165
1166#define TOOLTIP_TIMEOUT 500
1167static int
1168tooltip_timer_reset(struct tooltip *tooltip)
1169{
1170 struct itimerspec its;
1171
1172 its.it_interval.tv_sec = 0;
1173 its.it_interval.tv_nsec = 0;
1174 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1175 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1176 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1177 fprintf(stderr, "could not set timerfd\n: %m");
1178 return -1;
1179 }
1180
1181 return 0;
1182}
1183
1184int
1185widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1186{
1187 struct tooltip *tooltip = parent->tooltip;
1188
1189 parent->tooltip_count++;
1190 if (tooltip) {
1191 tooltip->x = x;
1192 tooltip->y = y;
1193 tooltip_timer_reset(tooltip);
1194 return 0;
1195 }
1196
1197 /* the handler might be triggered too fast via input device motion, so
1198 * we need this check here to make sure tooltip is fully initialized */
1199 if (parent->tooltip_count > 1)
1200 return 0;
1201
1202 tooltip = malloc(sizeof *tooltip);
1203 if (!tooltip)
1204 return -1;
1205
1206 parent->tooltip = tooltip;
1207 tooltip->parent = parent;
1208 tooltip->widget = NULL;
1209 tooltip->window = NULL;
1210 tooltip->x = x;
1211 tooltip->y = y;
1212 tooltip->entry = strdup(entry);
1213 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1214 if (tooltip->tooltip_fd < 0) {
1215 fprintf(stderr, "could not create timerfd\n: %m");
1216 return -1;
1217 }
1218
1219 tooltip->tooltip_task.run = tooltip_func;
1220 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1221 EPOLLIN, &tooltip->tooltip_task);
1222 tooltip_timer_reset(tooltip);
1223
1224 return 0;
1225}
1226
1227static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228frame_resize_handler(struct widget *widget,
1229 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001230{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 struct frame *frame = data;
1232 struct widget *child = frame->child;
1233 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001234 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001235 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001236 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001237 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001239 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001241 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001242 decoration_width = (t->width + t->margin) * 2;
1243 decoration_height = t->width +
1244 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001246 allocation.x = t->width + t->margin;
1247 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001248 allocation.width = width - decoration_width;
1249 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001250
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001251 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001252
1253 wl_list_for_each(button, &frame->buttons_list, link)
1254 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001255 } else {
1256 decoration_width = 0;
1257 decoration_height = 0;
1258
1259 allocation.x = 0;
1260 allocation.y = 0;
1261 allocation.width = width;
1262 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001263 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001264
1265 wl_list_for_each(button, &frame->buttons_list, link)
1266 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001267 }
1268
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001269 widget_set_allocation(child, allocation.x, allocation.y,
1270 allocation.width, allocation.height);
1271
1272 if (child->resize_handler)
1273 child->resize_handler(child,
1274 allocation.width,
1275 allocation.height,
1276 child->user_data);
1277
Scott Moreauf7e498c2012-05-14 11:39:29 -06001278 width = child->allocation.width + decoration_width;
1279 height = child->allocation.height + decoration_height;
1280
1281 widget->window->input_region =
1282 wl_compositor_create_region(display->compositor);
1283 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001284 t->margin, t->margin,
1285 width - 2 * t->margin,
1286 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001287
1288 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001289
1290 if (child->opaque) {
1291 widget->window->opaque_region =
1292 wl_compositor_create_region(display->compositor);
1293 wl_region_add(widget->window->opaque_region,
1294 opaque_margin, opaque_margin,
1295 widget->allocation.width - 2 * opaque_margin,
1296 widget->allocation.height - 2 * opaque_margin);
1297 }
Martin Minarik1998b152012-05-10 02:04:35 +02001298
1299 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001300 x_r = frame->widget->allocation.width - t->width - t->margin;
1301 x_l = t->width + t->margin;
1302 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001303 wl_list_for_each(button, &frame->buttons_list, link) {
1304 const int button_padding = 4;
1305 w = cairo_image_surface_get_width(button->icon);
1306 h = cairo_image_surface_get_height(button->icon);
1307
1308 if (button->decoration == FRAME_BUTTON_FANCY)
1309 w += 10;
1310
1311 if (button->align == FRAME_BUTTON_LEFT) {
1312 widget_set_allocation(button->widget,
1313 x_l, y , w + 1, h + 1);
1314 x_l += w;
1315 x_l += button_padding;
1316 } else {
1317 x_r -= w;
1318 widget_set_allocation(button->widget,
1319 x_r, y , w + 1, h + 1);
1320 x_r -= button_padding;
1321 }
1322 }
1323}
1324
1325static int
1326frame_button_enter_handler(struct widget *widget,
1327 struct input *input, float x, float y, void *data)
1328{
1329 struct frame_button *frame_button = data;
1330
1331 widget_schedule_redraw(frame_button->widget);
1332 frame_button->state = FRAME_BUTTON_OVER;
1333
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001334 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001335}
1336
1337static void
1338frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1339{
1340 struct frame_button *frame_button = data;
1341
1342 widget_schedule_redraw(frame_button->widget);
1343 frame_button->state = FRAME_BUTTON_DEFAULT;
1344}
1345
1346static void
1347frame_button_button_handler(struct widget *widget,
1348 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001349 uint32_t button,
1350 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001351{
1352 struct frame_button *frame_button = data;
1353 struct window *window = widget->window;
1354
1355 if (button != BTN_LEFT)
1356 return;
1357
1358 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001360 frame_button->state = FRAME_BUTTON_ACTIVE;
1361 widget_schedule_redraw(frame_button->widget);
1362
1363 if (frame_button->type == FRAME_BUTTON_ICON)
1364 window_show_frame_menu(window, input, time);
1365 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001366 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001367 frame_button->state = FRAME_BUTTON_DEFAULT;
1368 widget_schedule_redraw(frame_button->widget);
1369 break;
1370 }
1371
1372 switch (frame_button->type) {
1373 case FRAME_BUTTON_CLOSE:
1374 if (window->close_handler)
1375 window->close_handler(window->parent,
1376 window->user_data);
1377 else
1378 display_exit(window->display);
1379 break;
1380 case FRAME_BUTTON_MINIMIZE:
1381 fprintf(stderr,"Minimize stub\n");
1382 break;
1383 case FRAME_BUTTON_MAXIMIZE:
1384 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1385 break;
1386 default:
1387 /* Unknown operation */
1388 break;
1389 }
1390}
1391
1392static void
1393frame_button_redraw_handler(struct widget *widget, void *data)
1394{
1395 struct frame_button *frame_button = data;
1396 cairo_t *cr;
1397 int width, height, x, y;
1398 struct window *window = widget->window;
1399
1400 x = widget->allocation.x;
1401 y = widget->allocation.y;
1402 width = widget->allocation.width;
1403 height = widget->allocation.height;
1404
1405 if (!width)
1406 return;
1407 if (!height)
1408 return;
1409 if (widget->opaque)
1410 return;
1411
1412 cr = cairo_create(window->cairo_surface);
1413
1414 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1415 cairo_set_line_width(cr, 1);
1416
1417 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1418 cairo_rectangle (cr, x, y, 25, 16);
1419
1420 cairo_stroke_preserve(cr);
1421
1422 switch (frame_button->state) {
1423 case FRAME_BUTTON_DEFAULT:
1424 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1425 break;
1426 case FRAME_BUTTON_OVER:
1427 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1428 break;
1429 case FRAME_BUTTON_ACTIVE:
1430 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1431 break;
1432 }
1433
1434 cairo_fill (cr);
1435
1436 x += 4;
1437 }
1438
1439 cairo_set_source_surface(cr, frame_button->icon, x, y);
1440 cairo_paint(cr);
1441
1442 cairo_destroy(cr);
1443}
1444
1445static struct widget *
1446frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1447 enum frame_button_align align, enum frame_button_decoration style)
1448{
1449 struct frame_button *frame_button;
1450 const char *icon = data;
1451
1452 frame_button = malloc (sizeof *frame_button);
1453 memset(frame_button, 0, sizeof *frame_button);
1454
1455 frame_button->icon = cairo_image_surface_create_from_png(icon);
1456 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1457 frame_button->frame = frame;
1458 frame_button->type = type;
1459 frame_button->align = align;
1460 frame_button->decoration = style;
1461
1462 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1463
1464 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1465 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1466 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1467 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1468 return frame_button->widget;
1469}
1470
1471static void
1472frame_button_destroy(struct frame_button *frame_button)
1473{
1474 widget_destroy(frame_button->widget);
1475 wl_list_remove(&frame_button->link);
1476 cairo_surface_destroy(frame_button->icon);
1477 free(frame_button);
1478
1479 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001480}
1481
1482static void
1483frame_redraw_handler(struct widget *widget, void *data)
1484{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001487 struct theme *t = window->display->theme;
1488 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001490 if (window->type == TYPE_FULLSCREEN)
1491 return;
1492
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 cr = cairo_create(window->cairo_surface);
1494
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001496 flags |= THEME_FRAME_ACTIVE;
1497 theme_render_frame(t, cr, widget->allocation.width,
1498 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499
1500 cairo_destroy(cr);
1501}
1502
1503static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001505{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001506 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001507 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001509 location = theme_get_location(t, input->sx, input->sy,
1510 frame->widget->allocation.width,
1511 frame->widget->allocation.height);
1512
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001514 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001515 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001517 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001519 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_EXTERIOR:
1531 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001532 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001535}
1536
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001537static void
1538frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001540 switch (index) {
1541 case 0: /* close */
1542 if (window->close_handler)
1543 window->close_handler(window->parent,
1544 window->user_data);
1545 else
1546 display_exit(window->display);
1547 break;
1548 case 1: /* fullscreen */
1549 /* we don't have a way to get out of fullscreen for now */
1550 window_set_fullscreen(window, 1);
1551 break;
1552 case 2: /* rotate */
1553 case 3: /* scale */
1554 break;
1555 }
1556}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001557
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001558void
1559window_show_frame_menu(struct window *window,
1560 struct input *input, uint32_t time)
1561{
1562 int32_t x, y;
1563
1564 static const char *entries[] = {
1565 "Close", "Fullscreen", "Rotate", "Scale"
1566 };
1567
1568 input_get_position(input, &x, &y);
1569 window_show_menu(window->display, input, time, window,
1570 x - 10, y - 10, frame_menu_func, entries, 4);
1571}
1572
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001573static int
1574frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001575 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001576{
1577 return frame_get_pointer_image_for_location(data, input);
1578}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001579
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580static int
1581frame_motion_handler(struct widget *widget,
1582 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001583 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584{
1585 return frame_get_pointer_image_for_location(data, input);
1586}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001587
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589frame_button_handler(struct widget *widget,
1590 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001591 uint32_t button, enum wl_pointer_button_state state,
1592 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593
1594{
1595 struct frame *frame = data;
1596 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001597 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001600 location = theme_get_location(display->theme, input->sx, input->sy,
1601 frame->widget->allocation.width,
1602 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603
Daniel Stone4dbadb12012-05-30 16:31:51 +01001604 if (window->display->shell && button == BTN_LEFT &&
1605 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001607 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608 if (!window->shell_surface)
1609 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001610 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001611 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001613 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001614 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001616 case THEME_LOCATION_RESIZING_TOP:
1617 case THEME_LOCATION_RESIZING_BOTTOM:
1618 case THEME_LOCATION_RESIZING_LEFT:
1619 case THEME_LOCATION_RESIZING_RIGHT:
1620 case THEME_LOCATION_RESIZING_TOP_LEFT:
1621 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1622 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1623 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001624 if (!window->shell_surface)
1625 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001626 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001627
1628 if (!display->dpy) {
1629 /* If we're using shm, allocate a big
1630 pool to create buffers out of while
1631 we resize. We should probably base
1632 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001633 window->pool =
1634 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001635 }
1636
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001637 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001638 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001640 break;
1641 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001642 } else if (button == BTN_RIGHT &&
1643 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001644 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001645 }
1646}
1647
1648struct widget *
1649frame_create(struct window *window, void *data)
1650{
1651 struct frame *frame;
1652
1653 frame = malloc(sizeof *frame);
1654 memset(frame, 0, sizeof *frame);
1655
1656 frame->widget = window_add_widget(window, frame);
1657 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001658
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001659 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1660 widget_set_resize_handler(frame->widget, frame_resize_handler);
1661 widget_set_enter_handler(frame->widget, frame_enter_handler);
1662 widget_set_motion_handler(frame->widget, frame_motion_handler);
1663 widget_set_button_handler(frame->widget, frame_button_handler);
1664
Martin Minarik1998b152012-05-10 02:04:35 +02001665 /* Create empty list for frame buttons */
1666 wl_list_init(&frame->buttons_list);
1667
1668 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1669 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1670
1671 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1672 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1673
1674 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1675 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1676
1677 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1678 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1679
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001680 window->frame = frame;
1681
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001682 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001683}
1684
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001685static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001686frame_destroy(struct frame *frame)
1687{
Martin Minarik1998b152012-05-10 02:04:35 +02001688 struct frame_button *button, *tmp;
1689
1690 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1691 frame_button_destroy(button);
1692
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001693 /* frame->child must be destroyed by the application */
1694 widget_destroy(frame->widget);
1695 free(frame);
1696}
1697
1698static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001699input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001700 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001702 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001703 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001705 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706 return;
1707
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001708 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001709 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001710 widget = old;
1711 if (input->grab)
1712 widget = input->grab;
1713 if (widget->leave_handler)
1714 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001715 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001716 }
1717
1718 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001719 widget = focus;
1720 if (input->grab)
1721 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001722 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001723 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001724 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001726
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001727 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001728 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001729}
1730
1731static void
Daniel Stone37816df2012-05-16 18:45:18 +01001732pointer_handle_motion(void *data, struct wl_pointer *pointer,
1733 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001734{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001735 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001736 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001737 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001739 float sx = wl_fixed_to_double(sx_w);
1740 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001741
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001742 input->sx = sx;
1743 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001744
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001745 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001746 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001747 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001748 }
1749
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001750 if (input->grab)
1751 widget = input->grab;
1752 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001753 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001754 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001755 cursor = widget->motion_handler(input->focus_widget,
1756 input, time, sx, sy,
1757 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001758
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001759 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001760}
1761
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001762void
1763input_grab(struct input *input, struct widget *widget, uint32_t button)
1764{
1765 input->grab = widget;
1766 input->grab_button = button;
1767}
1768
1769void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001770input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001771{
1772 struct widget *widget;
1773
1774 input->grab = NULL;
1775 if (input->pointer_focus) {
1776 widget = widget_find_widget(input->pointer_focus->widget,
1777 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001778 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001779 }
1780}
1781
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001782static void
Daniel Stone37816df2012-05-16 18:45:18 +01001783pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001784 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001785{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001786 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001787 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001788 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001789
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001790 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001791 if (input->focus_widget && input->grab == NULL &&
1792 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001793 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001794
Neil Roberts6b28aad2012-01-23 19:11:18 +00001795 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 (*widget->button_handler)(widget,
1798 input, time,
1799 button, state,
1800 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001801
Daniel Stone4dbadb12012-05-30 16:31:51 +01001802 if (input->grab && input->grab_button == button &&
1803 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001805}
1806
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001807static void
Daniel Stone37816df2012-05-16 18:45:18 +01001808pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001809 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001810{
1811}
1812
1813static void
Daniel Stone37816df2012-05-16 18:45:18 +01001814keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001815 uint32_t serial, uint32_t time, uint32_t key,
1816 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001817{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001818 struct input *input = data;
1819 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001820 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001821 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001822 const xkb_keysym_t *syms;
1823 xkb_keysym_t sym;
1824 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001825
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001826 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001827 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001828 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001829 return;
1830
Daniel Stone97f68542012-05-30 16:32:01 +01001831 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001832
Daniel Stone97f68542012-05-30 16:32:01 +01001833 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001834 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001835 XKB_STATE_LATCHED);
1836 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001837 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001838 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001839 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001840 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001841 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001843
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001844 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001845 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001846 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001847 window_set_maximized(window,
1848 window->type != TYPE_MAXIMIZED);
1849 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850 if (num_syms == 1)
1851 sym = syms[0];
1852 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001853 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001854
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001855 (*window->key_handler)(window, input, time, key,
1856 sym, state, window->user_data);
1857 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001858}
1859
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001860static void
Daniel Stone351eb612012-05-31 15:27:47 -04001861keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1862 uint32_t serial, uint32_t mods_depressed,
1863 uint32_t mods_latched, uint32_t mods_locked,
1864 uint32_t group)
1865{
1866 struct input *input = data;
1867
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001868 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1869 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001870}
1871
1872static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001873input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001874{
1875 struct window *window = input->pointer_focus;
1876
1877 if (!window)
1878 return;
1879
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001880 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001881
Pekka Paalanene1207c72011-12-16 12:02:09 +02001882 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001883 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001884}
1885
1886static void
Daniel Stone37816df2012-05-16 18:45:18 +01001887pointer_handle_enter(void *data, struct wl_pointer *pointer,
1888 uint32_t serial, struct wl_surface *surface,
1889 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001890{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001891 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001892 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001893 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001894 float sx = wl_fixed_to_double(sx_w);
1895 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001896
Martin Minarik1998b152012-05-10 02:04:35 +02001897 if (!surface) {
1898 /* enter event for a window we've just destroyed */
1899 return;
1900 }
1901
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001902 input->display->serial = serial;
1903 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001904 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001905 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001906
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001907 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001908 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001909 window->pool = NULL;
1910 /* Schedule a redraw to free the pool */
1911 window_schedule_redraw(window);
1912 }
1913
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001914 input->sx = sx;
1915 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001916
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001917 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001920
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001921static void
Daniel Stone37816df2012-05-16 18:45:18 +01001922pointer_handle_leave(void *data, struct wl_pointer *pointer,
1923 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001924{
1925 struct input *input = data;
1926
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001927 input->display->serial = serial;
1928 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001929}
1930
1931static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001932input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001933{
1934 struct window *window = input->keyboard_focus;
1935
1936 if (!window)
1937 return;
1938
1939 window->keyboard_device = NULL;
1940 if (window->keyboard_focus_handler)
1941 (*window->keyboard_focus_handler)(window, NULL,
1942 window->user_data);
1943
1944 input->keyboard_focus = NULL;
1945}
1946
1947static void
Daniel Stone37816df2012-05-16 18:45:18 +01001948keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1949 uint32_t serial, struct wl_surface *surface,
1950 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001951{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001952 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001953 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001954
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001955 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001956 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001957
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001958 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001959 window->keyboard_device = input;
1960 if (window->keyboard_focus_handler)
1961 (*window->keyboard_focus_handler)(window,
1962 window->keyboard_device,
1963 window->user_data);
1964}
1965
1966static void
Daniel Stone37816df2012-05-16 18:45:18 +01001967keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1968 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001969{
1970 struct input *input = data;
1971
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001972 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001973 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001974}
1975
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001976static void
1977keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1978 uint32_t format, int fd, uint32_t size)
1979{
1980 struct input *input = data;
1981 char *map_str;
1982
1983 if (!data) {
1984 close(fd);
1985 return;
1986 }
1987
1988 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1989 close(fd);
1990 return;
1991 }
1992
1993 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1994 if (map_str == MAP_FAILED) {
1995 close(fd);
1996 return;
1997 }
1998
1999 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2000 map_str,
2001 XKB_KEYMAP_FORMAT_TEXT_V1,
2002 0);
2003 munmap(map_str, size);
2004 close(fd);
2005
2006 if (!input->xkb.keymap) {
2007 fprintf(stderr, "failed to compile keymap\n");
2008 return;
2009 }
2010
2011 input->xkb.state = xkb_state_new(input->xkb.keymap);
2012 if (!input->xkb.state) {
2013 fprintf(stderr, "failed to create XKB state\n");
2014 xkb_map_unref(input->xkb.keymap);
2015 input->xkb.keymap = NULL;
2016 return;
2017 }
2018
2019 input->xkb.control_mask =
2020 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2021 input->xkb.alt_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2023 input->xkb.shift_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2025}
2026
Daniel Stone37816df2012-05-16 18:45:18 +01002027static const struct wl_pointer_listener pointer_listener = {
2028 pointer_handle_enter,
2029 pointer_handle_leave,
2030 pointer_handle_motion,
2031 pointer_handle_button,
2032 pointer_handle_axis,
2033};
2034
2035static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002036 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002037 keyboard_handle_enter,
2038 keyboard_handle_leave,
2039 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002040 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002041};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002042
2043static void
Daniel Stone37816df2012-05-16 18:45:18 +01002044seat_handle_capabilities(void *data, struct wl_seat *seat,
2045 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002046{
Daniel Stone37816df2012-05-16 18:45:18 +01002047 struct input *input = data;
2048
2049 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2050 input->pointer = wl_seat_get_pointer(seat);
2051 wl_pointer_set_user_data(input->pointer, input);
2052 wl_pointer_add_listener(input->pointer, &pointer_listener,
2053 input);
2054 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2055 wl_pointer_destroy(input->pointer);
2056 input->pointer = NULL;
2057 }
2058
2059 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2060 input->keyboard = wl_seat_get_keyboard(seat);
2061 wl_keyboard_set_user_data(input->keyboard, input);
2062 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2063 input);
2064 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2065 wl_keyboard_destroy(input->keyboard);
2066 input->keyboard = NULL;
2067 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002068}
2069
Daniel Stone37816df2012-05-16 18:45:18 +01002070static const struct wl_seat_listener seat_listener = {
2071 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002072};
2073
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002074void
2075input_get_position(struct input *input, int32_t *x, int32_t *y)
2076{
2077 *x = input->sx;
2078 *y = input->sy;
2079}
2080
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002081struct display *
2082input_get_display(struct input *input)
2083{
2084 return input->display;
2085}
2086
Daniel Stone37816df2012-05-16 18:45:18 +01002087struct wl_seat *
2088input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002089{
Daniel Stone37816df2012-05-16 18:45:18 +01002090 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002091}
2092
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002093uint32_t
2094input_get_modifiers(struct input *input)
2095{
2096 return input->modifiers;
2097}
2098
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002099struct widget *
2100input_get_focus_widget(struct input *input)
2101{
2102 return input->focus_widget;
2103}
2104
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002105struct data_offer {
2106 struct wl_data_offer *offer;
2107 struct input *input;
2108 struct wl_array types;
2109 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002110
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002111 struct task io_task;
2112 int fd;
2113 data_func_t func;
2114 int32_t x, y;
2115 void *user_data;
2116};
2117
2118static void
2119data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2120{
2121 struct data_offer *offer = data;
2122 char **p;
2123
2124 p = wl_array_add(&offer->types, sizeof *p);
2125 *p = strdup(type);
2126}
2127
2128static const struct wl_data_offer_listener data_offer_listener = {
2129 data_offer_offer,
2130};
2131
2132static void
2133data_offer_destroy(struct data_offer *offer)
2134{
2135 char **p;
2136
2137 offer->refcount--;
2138 if (offer->refcount == 0) {
2139 wl_data_offer_destroy(offer->offer);
2140 for (p = offer->types.data; *p; p++)
2141 free(*p);
2142 wl_array_release(&offer->types);
2143 free(offer);
2144 }
2145}
2146
2147static void
2148data_device_data_offer(void *data,
2149 struct wl_data_device *data_device, uint32_t id)
2150{
2151 struct data_offer *offer;
2152
2153 offer = malloc(sizeof *offer);
2154
2155 wl_array_init(&offer->types);
2156 offer->refcount = 1;
2157 offer->input = data;
2158
2159 /* FIXME: Generate typesafe wrappers for this */
2160 offer->offer = (struct wl_data_offer *)
2161 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2162 id, &wl_data_offer_interface);
2163
2164 wl_data_offer_add_listener(offer->offer,
2165 &data_offer_listener, offer);
2166}
2167
2168static void
2169data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002170 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002171 wl_fixed_t x_w, wl_fixed_t y_w,
2172 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002173{
2174 struct input *input = data;
2175 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002176 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002177 float x = wl_fixed_to_double(x_w);
2178 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002179 char **p;
2180
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002181 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002182 window = wl_surface_get_user_data(surface);
2183 input->pointer_focus = window;
2184
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002185 if (offer) {
2186 input->drag_offer = wl_data_offer_get_user_data(offer);
2187
2188 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2189 *p = NULL;
2190
2191 types_data = input->drag_offer->types.data;
2192 } else {
2193 input->drag_offer = NULL;
2194 types_data = NULL;
2195 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196
2197 window = input->pointer_focus;
2198 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002199 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002200 window->user_data);
2201}
2202
2203static void
2204data_device_leave(void *data, struct wl_data_device *data_device)
2205{
2206 struct input *input = data;
2207
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002208 if (input->drag_offer) {
2209 data_offer_destroy(input->drag_offer);
2210 input->drag_offer = NULL;
2211 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002212}
2213
2214static void
2215data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002216 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002217{
2218 struct input *input = data;
2219 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002220 float x = wl_fixed_to_double(x_w);
2221 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002222 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002223
2224 input->sx = x;
2225 input->sy = y;
2226
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002227 if (input->drag_offer)
2228 types_data = input->drag_offer->types.data;
2229 else
2230 types_data = NULL;
2231
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002232 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002233 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002234 window->user_data);
2235}
2236
2237static void
2238data_device_drop(void *data, struct wl_data_device *data_device)
2239{
2240 struct input *input = data;
2241 struct window *window = input->pointer_focus;
2242
2243 if (window->drop_handler)
2244 window->drop_handler(window, input,
2245 input->sx, input->sy, window->user_data);
2246}
2247
2248static void
2249data_device_selection(void *data,
2250 struct wl_data_device *wl_data_device,
2251 struct wl_data_offer *offer)
2252{
2253 struct input *input = data;
2254 char **p;
2255
2256 if (input->selection_offer)
2257 data_offer_destroy(input->selection_offer);
2258
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002259 if (offer) {
2260 input->selection_offer = wl_data_offer_get_user_data(offer);
2261 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2262 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002263 } else {
2264 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002265 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002266}
2267
2268static const struct wl_data_device_listener data_device_listener = {
2269 data_device_data_offer,
2270 data_device_enter,
2271 data_device_leave,
2272 data_device_motion,
2273 data_device_drop,
2274 data_device_selection
2275};
2276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002277void
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002278input_set_pointer_image_index(struct input *input, int pointer, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002279{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002280 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002281 struct wl_cursor *cursor;
2282 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002284 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002285 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002286 return;
2287
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002288 if (index >= (int) cursor->image_count) {
2289 fprintf(stderr, "cursor index out of range\n");
2290 return;
2291 }
2292
2293 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002294 buffer = wl_cursor_image_get_buffer(image);
2295 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296 return;
2297
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002298 input->current_cursor = pointer;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002299 wl_pointer_set_cursor(input->pointer, input->display->serial,
2300 input->pointer_surface,
2301 image->hotspot_x, image->hotspot_y);
2302 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2303 wl_surface_damage(input->pointer_surface, 0, 0,
2304 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002305}
2306
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002307void
2308input_set_pointer_image(struct input *input, int pointer)
2309{
2310 if (pointer == input->current_cursor)
2311 return;
2312
2313 input_set_pointer_image_index(input, pointer, 0);
2314}
2315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316struct wl_data_device *
2317input_get_data_device(struct input *input)
2318{
2319 return input->data_device;
2320}
2321
2322void
2323input_set_selection(struct input *input,
2324 struct wl_data_source *source, uint32_t time)
2325{
2326 wl_data_device_set_selection(input->data_device, source, time);
2327}
2328
2329void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002330input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002331{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002332 wl_data_offer_accept(input->drag_offer->offer,
2333 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002334}
2335
2336static void
2337offer_io_func(struct task *task, uint32_t events)
2338{
2339 struct data_offer *offer =
2340 container_of(task, struct data_offer, io_task);
2341 unsigned int len;
2342 char buffer[4096];
2343
2344 len = read(offer->fd, buffer, sizeof buffer);
2345 offer->func(buffer, len,
2346 offer->x, offer->y, offer->user_data);
2347
2348 if (len == 0) {
2349 close(offer->fd);
2350 data_offer_destroy(offer);
2351 }
2352}
2353
2354static void
2355data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2356 data_func_t func, void *user_data)
2357{
2358 int p[2];
2359
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002360 if (pipe2(p, O_CLOEXEC) == -1)
2361 return;
2362
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002363 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2364 close(p[1]);
2365
2366 offer->io_task.run = offer_io_func;
2367 offer->fd = p[0];
2368 offer->func = func;
2369 offer->refcount++;
2370 offer->user_data = user_data;
2371
2372 display_watch_fd(offer->input->display,
2373 offer->fd, EPOLLIN, &offer->io_task);
2374}
2375
2376void
2377input_receive_drag_data(struct input *input, const char *mime_type,
2378 data_func_t func, void *data)
2379{
2380 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2381 input->drag_offer->x = input->sx;
2382 input->drag_offer->y = input->sy;
2383}
2384
2385int
2386input_receive_selection_data(struct input *input, const char *mime_type,
2387 data_func_t func, void *data)
2388{
2389 char **p;
2390
2391 if (input->selection_offer == NULL)
2392 return -1;
2393
2394 for (p = input->selection_offer->types.data; *p; p++)
2395 if (strcmp(mime_type, *p) == 0)
2396 break;
2397
2398 if (*p == NULL)
2399 return -1;
2400
2401 data_offer_receive_data(input->selection_offer,
2402 mime_type, func, data);
2403 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002404}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002405
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002406int
2407input_receive_selection_data_to_fd(struct input *input,
2408 const char *mime_type, int fd)
2409{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002410 if (input->selection_offer)
2411 wl_data_offer_receive(input->selection_offer->offer,
2412 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002413
2414 return 0;
2415}
2416
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002417void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002418window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002419{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002420 if (!window->shell_surface)
2421 return;
2422
Daniel Stone37816df2012-05-16 18:45:18 +01002423 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002424}
2425
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002426static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002427idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002428{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002429 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002430
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002431 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002432 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002433 widget_set_allocation(widget,
2434 window->pending_allocation.x,
2435 window->pending_allocation.y,
2436 window->pending_allocation.width,
2437 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002438
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002439 if (window->input_region) {
2440 wl_region_destroy(window->input_region);
2441 window->input_region = NULL;
2442 }
2443
2444 if (window->opaque_region) {
2445 wl_region_destroy(window->opaque_region);
2446 window->opaque_region = NULL;
2447 }
2448
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002449 if (widget->resize_handler)
2450 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002451 widget->allocation.width,
2452 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002453 widget->user_data);
2454
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002455 if (window->allocation.width != widget->allocation.width ||
2456 window->allocation.height != widget->allocation.height) {
2457 window->allocation = widget->allocation;
2458 window_schedule_redraw(window);
2459 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002460}
2461
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002462void
2463window_schedule_resize(struct window *window, int width, int height)
2464{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002465 window->pending_allocation.x = 0;
2466 window->pending_allocation.y = 0;
2467 window->pending_allocation.width = width;
2468 window->pending_allocation.height = height;
2469
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002470 window->resize_needed = 1;
2471 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002472}
2473
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002474void
2475widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2476{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002477 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002478}
2479
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002480static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002481handle_ping(void *data, struct wl_shell_surface *shell_surface,
2482 uint32_t serial)
2483{
2484 wl_shell_surface_pong(shell_surface, serial);
2485}
2486
2487static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002488handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002489 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002490{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002491 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002492
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002493 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002494 return;
2495
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002496 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002497 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002498}
2499
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002500static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002501menu_destroy(struct menu *menu)
2502{
2503 widget_destroy(menu->widget);
2504 window_destroy(menu->window);
2505 free(menu);
2506}
2507
2508static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002509handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2510{
2511 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002512 struct menu *menu = window->widget->user_data;
2513
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002514 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002515 * device. Or just use wl_callback. And this really needs to
2516 * be a window vfunc that the menu can set. And we need the
2517 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002518
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002519 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002520 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002521 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002522}
2523
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002524static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002525 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002526 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002527 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002528};
2529
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002530void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002531window_get_allocation(struct window *window,
2532 struct rectangle *allocation)
2533{
2534 *allocation = window->allocation;
2535}
2536
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002537static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002538widget_redraw(struct widget *widget)
2539{
2540 struct widget *child;
2541
2542 if (widget->redraw_handler)
2543 widget->redraw_handler(widget, widget->user_data);
2544 wl_list_for_each(child, &widget->child_list, link)
2545 widget_redraw(child);
2546}
2547
2548static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002549frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2550{
2551 struct window *window = data;
2552
2553 wl_callback_destroy(callback);
2554 window->redraw_scheduled = 0;
2555 if (window->redraw_needed)
2556 window_schedule_redraw(window);
2557}
2558
2559static const struct wl_callback_listener listener = {
2560 frame_callback
2561};
2562
2563static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002564idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002565{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002566 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002567 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002568
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002569 if (window->resize_needed)
2570 idle_resize(window);
2571
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002572 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002573 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002574 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002575 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002576 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002577
2578 callback = wl_surface_frame(window->surface);
2579 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002580}
2581
2582void
2583window_schedule_redraw(struct window *window)
2584{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002585 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002586 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002587 window->redraw_task.run = idle_redraw;
2588 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002589 window->redraw_scheduled = 1;
2590 }
2591}
2592
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002593void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002594window_set_custom(struct window *window)
2595{
2596 window->type = TYPE_CUSTOM;
2597}
2598
2599void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002600window_set_fullscreen(struct window *window, int fullscreen)
2601{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002602 if (!window->display->shell)
2603 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002604
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002605 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002606 return;
2607
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002608 if (fullscreen) {
2609 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002610 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002611 wl_shell_surface_set_fullscreen(window->shell_surface,
2612 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2613 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002614 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002615 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002616 wl_shell_surface_set_toplevel(window->shell_surface);
2617 window_schedule_resize(window,
2618 window->saved_allocation.width,
2619 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002620 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002621}
2622
2623void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002624window_set_maximized(struct window *window, int maximized)
2625{
2626 if (!window->display->shell)
2627 return;
2628
2629 if ((window->type == TYPE_MAXIMIZED) == maximized)
2630 return;
2631
2632 if (window->type == TYPE_TOPLEVEL) {
2633 window->saved_allocation = window->allocation;
2634 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2635 window->type = TYPE_MAXIMIZED;
2636 } else {
2637 wl_shell_surface_set_toplevel(window->shell_surface);
2638 window->type = TYPE_TOPLEVEL;
2639 window_schedule_resize(window,
2640 window->saved_allocation.width,
2641 window->saved_allocation.height);
2642 }
2643}
2644
2645void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002646window_set_user_data(struct window *window, void *data)
2647{
2648 window->user_data = data;
2649}
2650
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002651void *
2652window_get_user_data(struct window *window)
2653{
2654 return window->user_data;
2655}
2656
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002657void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002658window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002659 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002660{
2661 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002662}
2663
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002664void
2665window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002666 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002667{
2668 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002669}
2670
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002671void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002672window_set_data_handler(struct window *window, window_data_handler_t handler)
2673{
2674 window->data_handler = handler;
2675}
2676
2677void
2678window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2679{
2680 window->drop_handler = handler;
2681}
2682
2683void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002684window_set_close_handler(struct window *window,
2685 window_close_handler_t handler)
2686{
2687 window->close_handler = handler;
2688}
2689
2690void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002691window_set_title(struct window *window, const char *title)
2692{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002693 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002694 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002695 if (window->shell_surface)
2696 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002697}
2698
2699const char *
2700window_get_title(struct window *window)
2701{
2702 return window->title;
2703}
2704
2705void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002706window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2707{
2708 struct text_cursor_position *text_cursor_position =
2709 window->display->text_cursor_position;
2710
Scott Moreau9295ce02012-06-01 12:46:10 -06002711 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002712 return;
2713
2714 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002715 window->surface,
2716 wl_fixed_from_int(x),
2717 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002718}
2719
2720void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002721window_damage(struct window *window, int32_t x, int32_t y,
2722 int32_t width, int32_t height)
2723{
2724 wl_surface_damage(window->surface, x, y, width, height);
2725}
2726
Casey Dahlin9074db52012-04-19 22:50:09 -04002727static void
2728surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002729 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002730{
Rob Bradford7507b572012-05-15 17:55:34 +01002731 struct window *window = data;
2732 struct output *output;
2733 struct output *output_found = NULL;
2734 struct window_output *window_output;
2735
2736 wl_list_for_each(output, &window->display->output_list, link) {
2737 if (output->output == wl_output) {
2738 output_found = output;
2739 break;
2740 }
2741 }
2742
2743 if (!output_found)
2744 return;
2745
2746 window_output = malloc (sizeof *window_output);
2747 window_output->output = output_found;
2748
2749 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002750}
2751
2752static void
2753surface_leave(void *data,
2754 struct wl_surface *wl_surface, struct wl_output *output)
2755{
Rob Bradford7507b572012-05-15 17:55:34 +01002756 struct window *window = data;
2757 struct window_output *window_output;
2758 struct window_output *window_output_found = NULL;
2759
2760 wl_list_for_each(window_output, &window->window_output_list, link) {
2761 if (window_output->output->output == output) {
2762 window_output_found = window_output;
2763 break;
2764 }
2765 }
2766
2767 if (window_output_found) {
2768 wl_list_remove(&window_output_found->link);
2769 free(window_output_found);
2770 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002771}
2772
2773static const struct wl_surface_listener surface_listener = {
2774 surface_enter,
2775 surface_leave
2776};
2777
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002778static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002779window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002780{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002781 struct window *window;
2782
2783 window = malloc(sizeof *window);
2784 if (window == NULL)
2785 return NULL;
2786
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002787 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002788 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002789 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002790 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002791 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002792 if (display->shell) {
2793 window->shell_surface =
2794 wl_shell_get_shell_surface(display->shell,
2795 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002796 if (window->title)
2797 wl_shell_surface_set_title(window->shell_surface,
2798 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002799 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002800 window->allocation.x = 0;
2801 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002802 window->allocation.width = 0;
2803 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002804 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002805 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002806 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002807 window->input_region = NULL;
2808 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002809
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002810 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002812 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002813#else
2814 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2815#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002816 else
2817 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002818
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002819 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002820 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002821 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002822
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002823 if (window->shell_surface) {
2824 wl_shell_surface_set_user_data(window->shell_surface, window);
2825 wl_shell_surface_add_listener(window->shell_surface,
2826 &shell_surface_listener, window);
2827 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002828
Rob Bradford7507b572012-05-15 17:55:34 +01002829 wl_list_init (&window->window_output_list);
2830
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002831 return window;
2832}
2833
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002834struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002835window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002836{
2837 struct window *window;
2838
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002839 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002840 if (!window)
2841 return NULL;
2842
2843 return window;
2844}
2845
2846struct window *
2847window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002848 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002849{
2850 struct window *window;
2851
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002852 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002853 if (!window)
2854 return NULL;
2855
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002856 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002857 window->x = x;
2858 window->y = y;
2859
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002860 if (display->shell)
2861 wl_shell_surface_set_transient(window->shell_surface,
2862 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002863 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002864
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002865 return window;
2866}
2867
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002868static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002869menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002870{
2871 int next;
2872
2873 next = (sy - 8) / 20;
2874 if (menu->current != next) {
2875 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002876 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002877 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002878}
2879
2880static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002881menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002882 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002883 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002884{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002885 struct menu *menu = data;
2886
2887 if (widget == menu->widget)
2888 menu_set_item(data, y);
2889
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002890 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002891}
2892
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002893static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002894menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002895 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002896{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002897 struct menu *menu = data;
2898
2899 if (widget == menu->widget)
2900 menu_set_item(data, y);
2901
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002902 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002903}
2904
2905static void
2906menu_leave_handler(struct widget *widget, struct input *input, void *data)
2907{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002908 struct menu *menu = data;
2909
2910 if (widget == menu->widget)
2911 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002912}
2913
2914static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002915menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002916 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002917 uint32_t button, enum wl_pointer_button_state state,
2918 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002919
2920{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002921 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002922
Daniel Stone4dbadb12012-05-30 16:31:51 +01002923 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2924 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002925 /* Either relase after press-drag-release or
2926 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002927 menu->func(menu->window->parent,
2928 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002929 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002930 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002931 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002932}
2933
2934static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002935menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002936{
2937 cairo_t *cr;
2938 const int32_t r = 3, margin = 3;
2939 struct menu *menu = data;
2940 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002941 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002942
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002943 cr = cairo_create(window->cairo_surface);
2944 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2945 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2946 cairo_paint(cr);
2947
2948 width = window->allocation.width;
2949 height = window->allocation.height;
2950 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002951
2952 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002953 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2954 cairo_fill(cr);
2955
2956 for (i = 0; i < menu->count; i++) {
2957 if (i == menu->current) {
2958 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2959 cairo_rectangle(cr, margin, i * 20 + margin,
2960 width - 2 * margin, 20);
2961 cairo_fill(cr);
2962 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2963 cairo_move_to(cr, 10, i * 20 + 16);
2964 cairo_show_text(cr, menu->entries[i]);
2965 } else {
2966 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2967 cairo_move_to(cr, 10, i * 20 + 16);
2968 cairo_show_text(cr, menu->entries[i]);
2969 }
2970 }
2971
2972 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002973}
2974
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002975void
2976window_show_menu(struct display *display,
2977 struct input *input, uint32_t time, struct window *parent,
2978 int32_t x, int32_t y,
2979 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002980{
2981 struct window *window;
2982 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002983 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002984
2985 menu = malloc(sizeof *menu);
2986 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002987 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002988
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002989 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002990 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002991 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002992
2993 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002994 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002995 menu->entries = entries;
2996 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002997 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002999 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003000 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003001 window->type = TYPE_MENU;
3002 window->x = x;
3003 window->y = y;
3004
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003005 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003006 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003007 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003008 window->parent->shell_surface,
3009 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003010
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003011 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003012 widget_set_enter_handler(menu->widget, menu_enter_handler);
3013 widget_set_leave_handler(menu->widget, menu_leave_handler);
3014 widget_set_motion_handler(menu->widget, menu_motion_handler);
3015 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003016
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003017 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003018 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003019}
3020
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003021void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003022window_set_buffer_type(struct window *window, enum window_buffer_type type)
3023{
3024 window->buffer_type = type;
3025}
3026
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003027
3028static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003029display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003030 struct wl_output *wl_output,
3031 int x, int y,
3032 int physical_width,
3033 int physical_height,
3034 int subpixel,
3035 const char *make,
3036 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003037{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003038 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003039
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003040 output->allocation.x = x;
3041 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003042}
3043
3044static void
3045display_handle_mode(void *data,
3046 struct wl_output *wl_output,
3047 uint32_t flags,
3048 int width,
3049 int height,
3050 int refresh)
3051{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003052 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003053 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003054
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003055 if (flags & WL_OUTPUT_MODE_CURRENT) {
3056 output->allocation.width = width;
3057 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003058 if (display->output_configure_handler)
3059 (*display->output_configure_handler)(
3060 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003061 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003062}
3063
3064static const struct wl_output_listener output_listener = {
3065 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003066 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003067};
3068
3069static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003070display_add_output(struct display *d, uint32_t id)
3071{
3072 struct output *output;
3073
3074 output = malloc(sizeof *output);
3075 if (output == NULL)
3076 return;
3077
3078 memset(output, 0, sizeof *output);
3079 output->display = d;
3080 output->output =
3081 wl_display_bind(d->display, id, &wl_output_interface);
3082 wl_list_insert(d->output_list.prev, &output->link);
3083
3084 wl_output_add_listener(output->output, &output_listener, output);
3085}
3086
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003087static void
3088output_destroy(struct output *output)
3089{
3090 if (output->destroy_handler)
3091 (*output->destroy_handler)(output, output->user_data);
3092
3093 wl_output_destroy(output->output);
3094 wl_list_remove(&output->link);
3095 free(output);
3096}
3097
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003098void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003099display_set_output_configure_handler(struct display *display,
3100 display_output_handler_t handler)
3101{
3102 struct output *output;
3103
3104 display->output_configure_handler = handler;
3105 if (!handler)
3106 return;
3107
3108 wl_list_for_each(output, &display->output_list, link)
3109 (*display->output_configure_handler)(output,
3110 display->user_data);
3111}
3112
3113void
3114output_set_user_data(struct output *output, void *data)
3115{
3116 output->user_data = data;
3117}
3118
3119void *
3120output_get_user_data(struct output *output)
3121{
3122 return output->user_data;
3123}
3124
3125void
3126output_set_destroy_handler(struct output *output,
3127 display_output_handler_t handler)
3128{
3129 output->destroy_handler = handler;
3130 /* FIXME: implement this, once we have way to remove outputs */
3131}
3132
3133void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003134output_get_allocation(struct output *output, struct rectangle *allocation)
3135{
3136 *allocation = output->allocation;
3137}
3138
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003139struct wl_output *
3140output_get_wl_output(struct output *output)
3141{
3142 return output->output;
3143}
3144
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003145static void
Daniel Stone97f68542012-05-30 16:32:01 +01003146fini_xkb(struct input *input)
3147{
3148 xkb_state_unref(input->xkb.state);
3149 xkb_map_unref(input->xkb.keymap);
3150}
3151
3152static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003153display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003154{
3155 struct input *input;
3156
3157 input = malloc(sizeof *input);
3158 if (input == NULL)
3159 return;
3160
3161 memset(input, 0, sizeof *input);
3162 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003163 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003164 input->pointer_focus = NULL;
3165 input->keyboard_focus = NULL;
3166 wl_list_insert(d->input_list.prev, &input->link);
3167
Daniel Stone37816df2012-05-16 18:45:18 +01003168 wl_seat_add_listener(input->seat, &seat_listener, input);
3169 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003170
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003171 input->data_device =
3172 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003173 input->seat);
3174 wl_data_device_add_listener(input->data_device, &data_device_listener,
3175 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003176
3177 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003178}
3179
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003180static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003181input_destroy(struct input *input)
3182{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003183 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003184 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003185
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003186 if (input->xkb.state)
3187 xkb_state_unref(input->xkb.state);
3188 if (input->xkb.keymap)
3189 xkb_map_unref(input->xkb.keymap);
3190
Pekka Paalanene1207c72011-12-16 12:02:09 +02003191 if (input->drag_offer)
3192 data_offer_destroy(input->drag_offer);
3193
3194 if (input->selection_offer)
3195 data_offer_destroy(input->selection_offer);
3196
3197 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003198 fini_xkb(input);
3199
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003200 wl_surface_destroy(input->pointer_surface);
3201
Pekka Paalanene1207c72011-12-16 12:02:09 +02003202 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003203 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003204 free(input);
3205}
3206
3207static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003208display_handle_global(struct wl_display *display, uint32_t id,
3209 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003210{
3211 struct display *d = data;
3212
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003213 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003214 d->compositor =
3215 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003216 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003217 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003218 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003219 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003220 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003221 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003222 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003223 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003224 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3225 d->data_device_manager =
3226 wl_display_bind(display, id,
3227 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003228 } else if (strcmp(interface, "text_cursor_position") == 0) {
3229 d->text_cursor_position =
3230 wl_display_bind(display, id,
3231 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003232 }
3233}
3234
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003235#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003236static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003237init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003238{
3239 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003240 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003241
Rob Clark6396ed32012-03-11 19:48:41 -05003242#ifdef USE_CAIRO_GLESV2
3243# define GL_BIT EGL_OPENGL_ES2_BIT
3244#else
3245# define GL_BIT EGL_OPENGL_BIT
3246#endif
3247
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003248 static const EGLint argb_cfg_attribs[] = {
3249 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003250 EGL_RED_SIZE, 1,
3251 EGL_GREEN_SIZE, 1,
3252 EGL_BLUE_SIZE, 1,
3253 EGL_ALPHA_SIZE, 1,
3254 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003255 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003256 EGL_NONE
3257 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003258
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003259#ifdef USE_CAIRO_GLESV2
3260 static const EGLint context_attribs[] = {
3261 EGL_CONTEXT_CLIENT_VERSION, 2,
3262 EGL_NONE
3263 };
3264 EGLint api = EGL_OPENGL_ES_API;
3265#else
3266 EGLint *context_attribs = NULL;
3267 EGLint api = EGL_OPENGL_API;
3268#endif
3269
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003270 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003271 if (!eglInitialize(d->dpy, &major, &minor)) {
3272 fprintf(stderr, "failed to initialize display\n");
3273 return -1;
3274 }
3275
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003276 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003277 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3278 return -1;
3279 }
3280
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003281 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3282 &d->argb_config, 1, &n) || n != 1) {
3283 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003284 return -1;
3285 }
3286
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003287 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003288 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003289 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003290 fprintf(stderr, "failed to create context\n");
3291 return -1;
3292 }
3293
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003294 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003295 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003296 return -1;
3297 }
3298
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003299#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003300 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3301 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3302 fprintf(stderr, "failed to get cairo egl argb device\n");
3303 return -1;
3304 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003305#endif
3306
3307 return 0;
3308}
3309
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003310static void
3311fini_egl(struct display *display)
3312{
3313#ifdef HAVE_CAIRO_EGL
3314 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003315#endif
3316
3317 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3318 EGL_NO_CONTEXT);
3319
3320 eglTerminate(display->dpy);
3321 eglReleaseThread();
3322}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003323#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003324
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003325static int
3326event_mask_update(uint32_t mask, void *data)
3327{
3328 struct display *d = data;
3329
3330 d->mask = mask;
3331
3332 return 0;
3333}
3334
3335static void
3336handle_display_data(struct task *task, uint32_t events)
3337{
3338 struct display *display =
3339 container_of(task, struct display, display_task);
3340
3341 wl_display_iterate(display->display, display->mask);
3342}
3343
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003344struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003345display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003346{
3347 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003348
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003349 argc = parse_options(xkb_options,
3350 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003351
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003352 d = malloc(sizeof *d);
3353 if (d == NULL)
3354 return NULL;
3355
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003356 memset(d, 0, sizeof *d);
3357
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003358 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003359 if (d->display == NULL) {
3360 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003361 return NULL;
3362 }
3363
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003364 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003365 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3366 d->display_task.run = handle_display_data;
3367 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3368
3369 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003370 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003371 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003372
Daniel Stone97f68542012-05-30 16:32:01 +01003373 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003374 if (d->xkb_context == NULL) {
3375 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003376 return NULL;
3377 }
3378
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003379 /* Set up listener so we'll catch all events. */
3380 wl_display_add_global_listener(d->display,
3381 display_handle_global, d);
3382
3383 /* Process connection events. */
3384 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003385#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003386 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003387 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003388#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003389
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003390 d->image_target_texture_2d =
3391 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3392 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3393 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3394
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003395 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003396
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003397 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003398
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003399 wl_list_init(&d->window_list);
3400
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003401 return d;
3402}
3403
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003404static void
3405display_destroy_outputs(struct display *display)
3406{
3407 struct output *tmp;
3408 struct output *output;
3409
3410 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3411 output_destroy(output);
3412}
3413
Pekka Paalanene1207c72011-12-16 12:02:09 +02003414static void
3415display_destroy_inputs(struct display *display)
3416{
3417 struct input *tmp;
3418 struct input *input;
3419
3420 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3421 input_destroy(input);
3422}
3423
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003424void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003425display_destroy(struct display *display)
3426{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003427 if (!wl_list_empty(&display->window_list))
3428 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3429
3430 if (!wl_list_empty(&display->deferred_list))
3431 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3432
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003433 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003434 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003435
Daniel Stone97f68542012-05-30 16:32:01 +01003436 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003437
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003438 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003439 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003440
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003441#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003442 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003443#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003444
Pekka Paalanenc2052982011-12-16 11:41:32 +02003445 if (display->shell)
3446 wl_shell_destroy(display->shell);
3447
3448 if (display->shm)
3449 wl_shm_destroy(display->shm);
3450
3451 if (display->data_device_manager)
3452 wl_data_device_manager_destroy(display->data_device_manager);
3453
3454 wl_compositor_destroy(display->compositor);
3455
3456 close(display->epoll_fd);
3457
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003458 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003459 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003460 free(display);
3461}
3462
3463void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003464display_set_user_data(struct display *display, void *data)
3465{
3466 display->user_data = data;
3467}
3468
3469void *
3470display_get_user_data(struct display *display)
3471{
3472 return display->user_data;
3473}
3474
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003475struct wl_display *
3476display_get_display(struct display *display)
3477{
3478 return display->display;
3479}
3480
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003481struct output *
3482display_get_output(struct display *display)
3483{
3484 return container_of(display->output_list.next, struct output, link);
3485}
3486
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003487struct wl_compositor *
3488display_get_compositor(struct display *display)
3489{
3490 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003491}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003492
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003493uint32_t
3494display_get_serial(struct display *display)
3495{
3496 return display->serial;
3497}
3498
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003499EGLDisplay
3500display_get_egl_display(struct display *d)
3501{
3502 return d->dpy;
3503}
3504
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003505struct wl_data_source *
3506display_create_data_source(struct display *display)
3507{
3508 return wl_data_device_manager_create_data_source(display->data_device_manager);
3509}
3510
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003511EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003512display_get_argb_egl_config(struct display *d)
3513{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003514 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003515}
3516
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003517struct wl_shell *
3518display_get_shell(struct display *display)
3519{
3520 return display->shell;
3521}
3522
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003523int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003524display_acquire_window_surface(struct display *display,
3525 struct window *window,
3526 EGLContext ctx)
3527{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003528#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003529 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003530 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003531
3532 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003533 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003534 device = cairo_surface_get_device(window->cairo_surface);
3535 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003536 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003537
Benjamin Franzke0c991632011-09-27 21:57:31 +02003538 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003539 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003540 ctx = display->argb_ctx;
3541 else
3542 assert(0);
3543 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003544
3545 data = cairo_surface_get_user_data(window->cairo_surface,
3546 &surface_data_key);
3547
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003548 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003549 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003550 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3551 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003552
3553 return 0;
3554#else
3555 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003556#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003557}
3558
3559void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003560display_release_window_surface(struct display *display,
3561 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003562{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003563#ifdef HAVE_CAIRO_EGL
3564 cairo_device_t *device;
3565
3566 device = cairo_surface_get_device(window->cairo_surface);
3567 if (!device)
3568 return;
3569
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003570 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003571 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003572 cairo_device_release(device);
3573#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003574}
3575
3576void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003577display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003578{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003579 wl_list_insert(&display->deferred_list, &task->link);
3580}
3581
3582void
3583display_watch_fd(struct display *display,
3584 int fd, uint32_t events, struct task *task)
3585{
3586 struct epoll_event ep;
3587
3588 ep.events = events;
3589 ep.data.ptr = task;
3590 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3591}
3592
3593void
3594display_run(struct display *display)
3595{
3596 struct task *task;
3597 struct epoll_event ep[16];
3598 int i, count;
3599
Pekka Paalanen826d7952011-12-15 10:14:07 +02003600 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003601 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003602 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003603
3604 while (!wl_list_empty(&display->deferred_list)) {
3605 task = container_of(display->deferred_list.next,
3606 struct task, link);
3607 wl_list_remove(&task->link);
3608 task->run(task, 0);
3609 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003610
3611 if (!display->running)
3612 break;
3613
3614 wl_display_flush(display->display);
3615
3616 count = epoll_wait(display->epoll_fd,
3617 ep, ARRAY_LENGTH(ep), -1);
3618 for (i = 0; i < count; i++) {
3619 task = ep[i].data.ptr;
3620 task->run(task, ep[i].events);
3621 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003622 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003623}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003624
3625void
3626display_exit(struct display *display)
3627{
3628 display->running = 0;
3629}