blob: c5bccdc740325d275c7ffc7542309a961a38efeb [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"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020065#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030066#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050069
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070070struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050074 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040076 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040077 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020079 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040080 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050081 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020082 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020083 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040084 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040085
86 int display_fd;
87 uint32_t mask;
88 struct task display_task;
89
90 int epoll_fd;
91 struct wl_list deferred_list;
92
Pekka Paalanen826d7952011-12-15 10:14:07 +020093 int running;
94
Kristian Høgsberg478d9262010-06-08 20:34:11 -040095 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040096 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050097 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040098
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040099 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400100
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300101 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300102 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400103
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500104 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
105 PFNEGLCREATEIMAGEKHRPROC create_image;
106 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200107
108 display_output_handler_t output_configure_handler;
109
110 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100111
112 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200113
114 uint32_t workspace;
115 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400143 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500144 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500145 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400146 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400147 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400148 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400149 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400150 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400151 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400152 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400153 int focus_count;
154
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400155 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500156
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400157 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500158
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700159 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400160
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500161 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500162 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400163 window_data_handler_t data_handler;
164 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500165 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400166 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400167
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300168 struct wl_callback *frame_cb;
169
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200170 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500171 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500172
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500173 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400174 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500175};
176
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500177struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500178 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300179 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500180 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 struct wl_list link;
182 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500183 widget_resize_handler_t resize_handler;
184 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500185 widget_enter_handler_t enter_handler;
186 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500187 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500188 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200189 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400190 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500191 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300192 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193};
194
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195struct input {
196 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100197 struct wl_seat *seat;
198 struct wl_pointer *pointer;
199 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 struct window *pointer_focus;
201 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300202 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300203 uint32_t cursor_anim_start;
204 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300205 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400206 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400207 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400208 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400209 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400210 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400211
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500212 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500213 struct widget *grab;
214 uint32_t grab_button;
215
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400216 struct wl_data_device *data_device;
217 struct data_offer *drag_offer;
218 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100219
220 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100221 struct xkb_keymap *keymap;
222 struct xkb_state *state;
223 xkb_mod_mask_t control_mask;
224 xkb_mod_mask_t alt_mask;
225 xkb_mod_mask_t shift_mask;
226 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400227
228 struct task repeat_task;
229 int repeat_timer_fd;
230 uint32_t repeat_sym;
231 uint32_t repeat_key;
232 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400233};
234
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500235struct output {
236 struct display *display;
237 struct wl_output *output;
238 struct rectangle allocation;
239 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200240
241 display_output_handler_t destroy_handler;
242 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500243};
244
Martin Minarik1998b152012-05-10 02:04:35 +0200245enum frame_button_action {
246 FRAME_BUTTON_NULL = 0,
247 FRAME_BUTTON_ICON = 1,
248 FRAME_BUTTON_CLOSE = 2,
249 FRAME_BUTTON_MINIMIZE = 3,
250 FRAME_BUTTON_MAXIMIZE = 4,
251};
252
253enum frame_button_pointer {
254 FRAME_BUTTON_DEFAULT = 0,
255 FRAME_BUTTON_OVER = 1,
256 FRAME_BUTTON_ACTIVE = 2,
257};
258
259enum frame_button_align {
260 FRAME_BUTTON_RIGHT = 0,
261 FRAME_BUTTON_LEFT = 1,
262};
263
264enum frame_button_decoration {
265 FRAME_BUTTON_NONE = 0,
266 FRAME_BUTTON_FANCY = 1,
267};
268
269struct frame_button {
270 struct widget *widget;
271 struct frame *frame;
272 cairo_surface_t *icon;
273 enum frame_button_action type;
274 enum frame_button_pointer state;
275 struct wl_list link; /* buttons_list */
276 enum frame_button_align align;
277 enum frame_button_decoration decoration;
278};
279
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500280struct frame {
281 struct widget *widget;
282 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200283 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500284};
285
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500286struct menu {
287 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500288 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500289 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500290 const char **entries;
291 uint32_t time;
292 int current;
293 int count;
294 menu_func_t func;
295};
296
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300297struct tooltip {
298 struct widget *parent;
299 struct window *window;
300 struct widget *widget;
301 char *entry;
302 struct task tooltip_task;
303 int tooltip_fd;
304 float x, y;
305};
306
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700307struct shm_pool {
308 struct wl_shm_pool *pool;
309 size_t size;
310 size_t used;
311 void *data;
312};
313
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400314enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300315 CURSOR_DEFAULT = 100,
316 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400317};
318
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500319enum window_location {
320 WINDOW_INTERIOR = 0,
321 WINDOW_RESIZING_TOP = 1,
322 WINDOW_RESIZING_BOTTOM = 2,
323 WINDOW_RESIZING_LEFT = 4,
324 WINDOW_RESIZING_TOP_LEFT = 5,
325 WINDOW_RESIZING_BOTTOM_LEFT = 6,
326 WINDOW_RESIZING_RIGHT = 8,
327 WINDOW_RESIZING_TOP_RIGHT = 9,
328 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
329 WINDOW_RESIZING_MASK = 15,
330 WINDOW_EXTERIOR = 16,
331 WINDOW_TITLEBAR = 17,
332 WINDOW_CLIENT_AREA = 18,
333};
334
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335static const cairo_user_data_key_t surface_data_key;
336struct surface_data {
337 struct wl_buffer *buffer;
338};
339
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500340#define MULT(_d,c,a,t) \
341 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
342
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500343#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400344
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100345struct egl_window_surface_data {
346 struct display *display;
347 struct wl_surface *surface;
348 struct wl_egl_window *window;
349 EGLSurface surf;
350};
351
352static void
353egl_window_surface_data_destroy(void *p)
354{
355 struct egl_window_surface_data *data = p;
356 struct display *d = data->display;
357
358 eglDestroySurface(d->dpy, data->surf);
359 wl_egl_window_destroy(data->window);
360 data->surface = NULL;
361
362 free(p);
363}
364
365static cairo_surface_t *
366display_create_egl_window_surface(struct display *display,
367 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400368 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100369 struct rectangle *rectangle)
370{
371 cairo_surface_t *cairo_surface;
372 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
376 data = malloc(sizeof *data);
377 if (data == NULL)
378 return NULL;
379
380 data->display = display;
381 data->surface = surface;
382
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500383 config = display->argb_config;
384 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100389
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400390 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500391 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100394 data->surf,
395 rectangle->width,
396 rectangle->height);
397
398 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
399 data, egl_window_surface_data_destroy);
400
401 return cairo_surface;
402}
403
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404#endif
405
406struct wl_buffer *
407display_get_buffer_for_surface(struct display *display,
408 cairo_surface_t *surface)
409{
410 struct surface_data *data;
411
412 data = cairo_surface_get_user_data (surface, &surface_data_key);
413
414 return data->buffer;
415}
416
417struct shm_surface_data {
418 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700419 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420};
421
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500422static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423shm_pool_destroy(struct shm_pool *pool);
424
425static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426shm_surface_data_destroy(void *p)
427{
428 struct shm_surface_data *data = p;
429
430 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431 if (data->pool)
432 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300433
434 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435}
436
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437static struct wl_shm_pool *
438make_shm_pool(struct display *display, int size, void **data)
439{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400440 struct wl_shm_pool *pool;
441 int fd;
442
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300443 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400444 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300445 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
446 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400447 return NULL;
448 }
449
450 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400451 if (*data == MAP_FAILED) {
452 fprintf(stderr, "mmap failed: %m\n");
453 close(fd);
454 return NULL;
455 }
456
457 pool = wl_shm_create_pool(display->shm, fd, size);
458
459 close(fd);
460
461 return pool;
462}
463
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700464static struct shm_pool *
465shm_pool_create(struct display *display, size_t size)
466{
467 struct shm_pool *pool = malloc(sizeof *pool);
468
469 if (!pool)
470 return NULL;
471
472 pool->pool = make_shm_pool(display, size, &pool->data);
473 if (!pool->pool) {
474 free(pool);
475 return NULL;
476 }
477
478 pool->size = size;
479 pool->used = 0;
480
481 return pool;
482}
483
484static void *
485shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
486{
487 if (pool->used + size > pool->size)
488 return NULL;
489
490 *offset = pool->used;
491 pool->used += size;
492
493 return (char *) pool->data + *offset;
494}
495
496/* destroy the pool. this does not unmap the memory though */
497static void
498shm_pool_destroy(struct shm_pool *pool)
499{
500 munmap(pool->data, pool->size);
501 wl_shm_pool_destroy(pool->pool);
502 free(pool);
503}
504
505/* Start allocating from the beginning of the pool again */
506static void
507shm_pool_reset(struct shm_pool *pool)
508{
509 pool->used = 0;
510}
511
512static int
513data_length_for_shm_surface(struct rectangle *rect)
514{
515 int stride;
516
517 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 rect->width);
519 return stride * rect->height;
520}
521
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500522static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523display_create_shm_surface_from_pool(struct display *display,
524 struct rectangle *rectangle,
525 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526{
527 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400528 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400529 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400531 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532
533 data = malloc(sizeof *data);
534 if (data == NULL)
535 return NULL;
536
537 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
538 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700539 length = stride * rectangle->height;
540 data->pool = NULL;
541 map = shm_pool_allocate(pool, length, &offset);
542
543 if (!map) {
544 free(data);
545 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 CAIRO_FORMAT_ARGB32,
550 rectangle->width,
551 rectangle->height,
552 stride);
553
554 cairo_surface_set_user_data (surface, &surface_data_key,
555 data, shm_surface_data_destroy);
556
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500560 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400561
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700562 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400563 rectangle->width,
564 rectangle->height,
565 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700567 return surface;
568}
569
570static cairo_surface_t *
571display_create_shm_surface(struct display *display,
572 struct rectangle *rectangle, uint32_t flags,
573 struct window *window)
574{
575 struct shm_surface_data *data;
576 struct shm_pool *pool;
577 cairo_surface_t *surface;
578
579 if (window && window->pool) {
580 shm_pool_reset(window->pool);
581 surface = display_create_shm_surface_from_pool(display,
582 rectangle,
583 flags,
584 window->pool);
585 if (surface)
586 return surface;
587 }
588
589 pool = shm_pool_create(display,
590 data_length_for_shm_surface(rectangle));
591 if (!pool)
592 return NULL;
593
594 surface =
595 display_create_shm_surface_from_pool(display, rectangle,
596 flags, pool);
597
598 if (!surface) {
599 shm_pool_destroy(pool);
600 return NULL;
601 }
602
603 /* make sure we destroy the pool when the surface is destroyed */
604 data = cairo_surface_get_user_data(surface, &surface_data_key);
605 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606
607 return surface;
608}
609
nobled7b87cb02011-02-01 18:51:47 +0000610static int
611check_size(struct rectangle *rect)
612{
613 if (rect->width && rect->height)
614 return 0;
615
616 fprintf(stderr, "tried to create surface of "
617 "width: %d, height: %d\n", rect->width, rect->height);
618 return -1;
619}
620
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621cairo_surface_t *
622display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400624 struct rectangle *rectangle,
625 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
nobled7b87cb02011-02-01 18:51:47 +0000627 if (check_size(rectangle) < 0)
628 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500629#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300630 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400631 return display_create_egl_window_surface(display,
632 surface,
633 flags,
634 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400636 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637}
638
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200639/*
640 * The following correspondences between file names and cursors was copied
641 * from: https://bugs.kde.org/attachment.cgi?id=67313
642 */
643
644static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300645 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200646 "sw-resize"
647};
648
649static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300650 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200651 "se-resize"
652};
653
654static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300655 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200656 "s-resize"
657};
658
659static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300660 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200661 "closedhand",
662 "208530c400c041818281048008011002"
663};
664
665static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200667 "default",
668 "top_left_arrow",
669 "left-arrow"
670};
671
672static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300673 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200674 "w-resize"
675};
676
677static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300678 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200679 "e-resize"
680};
681
682static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300683 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200684 "nw-resize"
685};
686
687static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300688 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200689 "ne-resize"
690};
691
692static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300693 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200694 "n-resize"
695};
696
697static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300698 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200699 "ibeam",
700 "text"
701};
702
703static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300704 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200705 "pointer",
706 "pointing_hand",
707 "e29285e634086352946a0e7090d73106"
708};
709
710static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400711 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200712 "wait",
713 "0426c94ea35c87780ff01dc239897213"
714};
715
716struct cursor_alternatives {
717 const char **names;
718 size_t count;
719};
720
721static const struct cursor_alternatives cursors[] = {
722 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
723 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
724 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
725 {grabbings, ARRAY_LENGTH(grabbings)},
726 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
727 {left_sides, ARRAY_LENGTH(left_sides)},
728 {right_sides, ARRAY_LENGTH(right_sides)},
729 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
730 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
731 {top_sides, ARRAY_LENGTH(top_sides)},
732 {xterms, ARRAY_LENGTH(xterms)},
733 {hand1s, ARRAY_LENGTH(hand1s)},
734 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300735};
736
737static void
738create_cursors(struct display *display)
739{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100740 char *config_file;
741 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200742 unsigned int i, j;
743 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100744 struct config_key shell_keys[] = {
745 { "cursor-theme", CONFIG_KEY_STRING, &theme },
746 };
747 struct config_section cs[] = {
748 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
749 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300750
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100751 config_file = config_file_path("weston.ini");
752 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
753 free(config_file);
754
755 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300756 display->cursors =
757 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
758
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300759 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200760 cursor = NULL;
761 for (j = 0; !cursor && j < cursors[i].count; ++j)
762 cursor = wl_cursor_theme_get_cursor(
763 display->cursor_theme, cursors[i].names[j]);
764
765 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300766 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200767 cursors[i].names[0]);
768
769 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300770 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300771}
772
773static void
774destroy_cursors(struct display *display)
775{
776 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800777 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300778}
779
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300780struct wl_cursor_image *
781display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400782{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200783 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400784
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300785 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400786}
787
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400788static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789window_get_resize_dx_dy(struct window *window, int *x, int *y)
790{
791 if (window->resize_edges & WINDOW_RESIZING_LEFT)
792 *x = window->server_allocation.width - window->allocation.width;
793 else
794 *x = 0;
795
796 if (window->resize_edges & WINDOW_RESIZING_TOP)
797 *y = window->server_allocation.height -
798 window->allocation.height;
799 else
800 *y = 0;
801
802 window->resize_edges = 0;
803}
804
805static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500806window_attach_surface(struct window *window)
807{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400808 struct display *display = window->display;
809 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500813 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400815 if (window->type == TYPE_NONE) {
816 window->type = TYPE_TOPLEVEL;
817 if (display->shell)
818 wl_shell_surface_set_toplevel(window->shell_surface);
819 }
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 buffer =
835 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400836 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200838 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400844 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847 default:
848 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500850
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500851 if (window->input_region) {
852 wl_surface_set_input_region(window->surface,
853 window->input_region);
854 wl_region_destroy(window->input_region);
855 window->input_region = NULL;
856 }
857
858 if (window->opaque_region) {
859 wl_surface_set_opaque_region(window->surface,
860 window->opaque_region);
861 wl_region_destroy(window->opaque_region);
862 window->opaque_region = NULL;
863 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500864}
865
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400866int
867window_has_focus(struct window *window)
868{
869 return window->focus_count > 0;
870}
871
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500872void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400873window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500874{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400875 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100876 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500877}
878
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400879void
880window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500882 cairo_surface_reference(surface);
883
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884 if (window->cairo_surface != NULL)
885 cairo_surface_destroy(window->cairo_surface);
886
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500887 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888}
889
Benjamin Franzke22d54812011-07-16 19:50:32 +0000890#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100891static void
892window_resize_cairo_window_surface(struct window *window)
893{
894 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200895 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896
897 data = cairo_surface_get_user_data(window->cairo_surface,
898 &surface_data_key);
899
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200900 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901 wl_egl_window_resize(data->window,
902 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 window->allocation.height,
904 x,y);
905
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 cairo_gl_surface_set_size(window->cairo_surface,
907 window->allocation.width,
908 window->allocation.height);
909}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000910#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400912struct display *
913window_get_display(struct window *window)
914{
915 return window->display;
916}
917
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400918void
919window_create_surface(struct window *window)
920{
921 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 uint32_t flags = 0;
923
924 if (!window->transparent)
925 flags = SURFACE_OPAQUE;
926
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400927 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500928#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100929 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
930 if (window->cairo_surface) {
931 window_resize_cairo_window_surface(window);
932 return;
933 }
934 surface = display_create_surface(window->display,
935 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400936 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100937 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938#endif
939 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400940 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400941 &window->allocation,
942 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800944 default:
945 surface = NULL;
946 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400948
949 window_set_surface(window, surface);
950 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951}
952
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200953static void frame_destroy(struct frame *frame);
954
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400955void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500956window_destroy(struct window *window)
957{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200958 struct display *display = window->display;
959 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100960 struct window_output *window_output;
961 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200962
963 if (window->redraw_scheduled)
964 wl_list_remove(&window->redraw_task.link);
965
966 wl_list_for_each(input, &display->input_list, link) {
967 if (input->pointer_focus == window)
968 input->pointer_focus = NULL;
969 if (input->keyboard_focus == window)
970 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500971 if (input->focus_widget &&
972 input->focus_widget->window == window)
973 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200974 }
975
Rob Bradford7507b572012-05-15 17:55:34 +0100976 wl_list_for_each_safe(window_output, window_output_tmp,
977 &window->window_output_list, link) {
978 free (window_output);
979 }
980
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500981 if (window->input_region)
982 wl_region_destroy(window->input_region);
983 if (window->opaque_region)
984 wl_region_destroy(window->opaque_region);
985
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200986 if (window->frame)
987 frame_destroy(window->frame);
988
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200989 if (window->shell_surface)
990 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500991 wl_surface_destroy(window->surface);
992 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200993
994 if (window->cairo_surface != NULL)
995 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200996
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300997 if (window->frame_cb)
998 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200999 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000 free(window);
1001}
1002
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001003static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 wl_list_for_each(child, &widget->child_list, link) {
1009 target = widget_find_widget(child, x, y);
1010 if (target)
1011 return target;
1012 }
1013
1014 if (widget->allocation.x <= x &&
1015 x < widget->allocation.x + widget->allocation.width &&
1016 widget->allocation.y <= y &&
1017 y < widget->allocation.y + widget->allocation.height) {
1018 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019 }
1020
1021 return NULL;
1022}
1023
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001024static struct widget *
1025widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 widget = malloc(sizeof *widget);
1030 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001031 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001032 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001033 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001034 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001035 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001036 widget->tooltip = NULL;
1037 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001038
1039 return widget;
1040}
1041
1042struct widget *
1043window_add_widget(struct window *window, void *data)
1044{
1045 window->widget = widget_create(window, data);
1046 wl_list_init(&window->widget->link);
1047
1048 return window->widget;
1049}
1050
1051struct widget *
1052widget_add_widget(struct widget *parent, void *data)
1053{
1054 struct widget *widget;
1055
1056 widget = widget_create(parent->window, data);
1057 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060}
1061
1062void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001063widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001065 struct display *display = widget->window->display;
1066 struct input *input;
1067
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001068 if (widget->tooltip) {
1069 free(widget->tooltip);
1070 widget->tooltip = NULL;
1071 }
1072
Pekka Paalanene156fb62012-01-19 13:51:38 +02001073 wl_list_for_each(input, &display->input_list, link) {
1074 if (input->focus_widget == widget)
1075 input->focus_widget = NULL;
1076 }
1077
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001078 wl_list_remove(&widget->link);
1079 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001082void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001083widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086}
1087
1088void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001089widget_set_size(struct widget *widget, int32_t width, int32_t height)
1090{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001091 widget->allocation.width = width;
1092 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001093}
1094
1095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_set_allocation(struct widget *widget,
1097 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099 widget->allocation.x = x;
1100 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001101 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001104void
1105widget_set_transparent(struct widget *widget, int transparent)
1106{
1107 widget->opaque = !transparent;
1108}
1109
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001110void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001111widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001113 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001114}
1115
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001116void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001117widget_set_resize_handler(struct widget *widget,
1118 widget_resize_handler_t handler)
1119{
1120 widget->resize_handler = handler;
1121}
1122
1123void
1124widget_set_redraw_handler(struct widget *widget,
1125 widget_redraw_handler_t handler)
1126{
1127 widget->redraw_handler = handler;
1128}
1129
1130void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001131widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001132{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001133 widget->enter_handler = handler;
1134}
1135
1136void
1137widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1138{
1139 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001140}
1141
1142void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001143widget_set_motion_handler(struct widget *widget,
1144 widget_motion_handler_t handler)
1145{
1146 widget->motion_handler = handler;
1147}
1148
1149void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001150widget_set_button_handler(struct widget *widget,
1151 widget_button_handler_t handler)
1152{
1153 widget->button_handler = handler;
1154}
1155
1156void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001157widget_set_axis_handler(struct widget *widget,
1158 widget_axis_handler_t handler)
1159{
1160 widget->axis_handler = handler;
1161}
1162
1163void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001164widget_schedule_redraw(struct widget *widget)
1165{
1166 window_schedule_redraw(widget->window);
1167}
1168
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001169cairo_surface_t *
1170window_get_surface(struct window *window)
1171{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001172 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001173}
1174
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001175struct wl_surface *
1176window_get_wl_surface(struct window *window)
1177{
1178 return window->surface;
1179}
1180
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001181struct wl_shell_surface *
1182window_get_wl_shell_surface(struct window *window)
1183{
1184 return window->shell_surface;
1185}
1186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001187static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001188tooltip_redraw_handler(struct widget *widget, void *data)
1189{
1190 cairo_t *cr;
1191 const int32_t r = 3;
1192 struct tooltip *tooltip = data;
1193 int32_t width, height;
1194 struct window *window = widget->window;
1195
1196 cr = cairo_create(window->cairo_surface);
1197 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1198 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1199 cairo_paint(cr);
1200
1201 width = window->allocation.width;
1202 height = window->allocation.height;
1203 rounded_rect(cr, 0, 0, width, height, r);
1204
1205 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1206 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1207 cairo_fill(cr);
1208
1209 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1210 cairo_move_to(cr, 10, 16);
1211 cairo_show_text(cr, tooltip->entry);
1212 cairo_destroy(cr);
1213}
1214
1215static cairo_text_extents_t
1216get_text_extents(struct tooltip *tooltip)
1217{
1218 struct window *window;
1219 cairo_t *cr;
1220 cairo_text_extents_t extents;
1221
1222 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1223 * created yet */
1224 window = tooltip->widget->window->parent;
1225 cr = cairo_create(window->cairo_surface);
1226 cairo_text_extents(cr, tooltip->entry, &extents);
1227 cairo_destroy(cr);
1228
1229 return extents;
1230}
1231
1232static int
1233window_create_tooltip(struct tooltip *tooltip)
1234{
1235 struct widget *parent = tooltip->parent;
1236 struct display *display = parent->window->display;
1237 struct window *window;
1238 const int offset_y = 27;
1239 const int margin = 3;
1240 cairo_text_extents_t extents;
1241
1242 if (tooltip->widget)
1243 return 0;
1244
1245 window = window_create_transient(display, parent->window, tooltip->x,
1246 tooltip->y + offset_y,
1247 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1248 if (!window)
1249 return -1;
1250
1251 tooltip->window = window;
1252 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1253
1254 extents = get_text_extents(tooltip);
1255 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1256 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1257
1258 return 0;
1259}
1260
1261void
1262widget_destroy_tooltip(struct widget *parent)
1263{
1264 struct tooltip *tooltip = parent->tooltip;
1265
1266 parent->tooltip_count = 0;
1267 if (!tooltip)
1268 return;
1269
1270 if (tooltip->widget) {
1271 widget_destroy(tooltip->widget);
1272 window_destroy(tooltip->window);
1273 tooltip->widget = NULL;
1274 tooltip->window = NULL;
1275 }
1276
1277 close(tooltip->tooltip_fd);
1278 free(tooltip->entry);
1279 free(tooltip);
1280 parent->tooltip = NULL;
1281}
1282
1283static void
1284tooltip_func(struct task *task, uint32_t events)
1285{
1286 struct tooltip *tooltip =
1287 container_of(task, struct tooltip, tooltip_task);
1288 uint64_t exp;
1289
Martin Olsson8df662a2012-07-08 03:03:47 +02001290 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1291 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001292 window_create_tooltip(tooltip);
1293}
1294
1295#define TOOLTIP_TIMEOUT 500
1296static int
1297tooltip_timer_reset(struct tooltip *tooltip)
1298{
1299 struct itimerspec its;
1300
1301 its.it_interval.tv_sec = 0;
1302 its.it_interval.tv_nsec = 0;
1303 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1304 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1305 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1306 fprintf(stderr, "could not set timerfd\n: %m");
1307 return -1;
1308 }
1309
1310 return 0;
1311}
1312
1313int
1314widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1315{
1316 struct tooltip *tooltip = parent->tooltip;
1317
1318 parent->tooltip_count++;
1319 if (tooltip) {
1320 tooltip->x = x;
1321 tooltip->y = y;
1322 tooltip_timer_reset(tooltip);
1323 return 0;
1324 }
1325
1326 /* the handler might be triggered too fast via input device motion, so
1327 * we need this check here to make sure tooltip is fully initialized */
1328 if (parent->tooltip_count > 1)
1329 return 0;
1330
1331 tooltip = malloc(sizeof *tooltip);
1332 if (!tooltip)
1333 return -1;
1334
1335 parent->tooltip = tooltip;
1336 tooltip->parent = parent;
1337 tooltip->widget = NULL;
1338 tooltip->window = NULL;
1339 tooltip->x = x;
1340 tooltip->y = y;
1341 tooltip->entry = strdup(entry);
1342 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1343 if (tooltip->tooltip_fd < 0) {
1344 fprintf(stderr, "could not create timerfd\n: %m");
1345 return -1;
1346 }
1347
1348 tooltip->tooltip_task.run = tooltip_func;
1349 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1350 EPOLLIN, &tooltip->tooltip_task);
1351 tooltip_timer_reset(tooltip);
1352
1353 return 0;
1354}
1355
1356static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001357workspace_manager_state(void *data,
1358 struct workspace_manager *workspace_manager,
1359 uint32_t current,
1360 uint32_t count)
1361{
1362 struct display *display = data;
1363
1364 display->workspace = current;
1365 display->workspace_count = count;
1366}
1367
1368static const struct workspace_manager_listener workspace_manager_listener = {
1369 workspace_manager_state
1370};
1371
1372static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373frame_resize_handler(struct widget *widget,
1374 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001375{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001376 struct frame *frame = data;
1377 struct widget *child = frame->child;
1378 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001379 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001380 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001381 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001382 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001384 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001386 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001387 decoration_width = (t->width + t->margin) * 2;
1388 decoration_height = t->width +
1389 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001390
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001391 allocation.x = t->width + t->margin;
1392 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001393 allocation.width = width - decoration_width;
1394 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001395
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001396 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001397
1398 wl_list_for_each(button, &frame->buttons_list, link)
1399 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001400 } else {
1401 decoration_width = 0;
1402 decoration_height = 0;
1403
1404 allocation.x = 0;
1405 allocation.y = 0;
1406 allocation.width = width;
1407 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001408 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001409
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001412 }
1413
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001414 widget_set_allocation(child, allocation.x, allocation.y,
1415 allocation.width, allocation.height);
1416
1417 if (child->resize_handler)
1418 child->resize_handler(child,
1419 allocation.width,
1420 allocation.height,
1421 child->user_data);
1422
Scott Moreauf7e498c2012-05-14 11:39:29 -06001423 width = child->allocation.width + decoration_width;
1424 height = child->allocation.height + decoration_height;
1425
Kristian Høgsberg023be102012-07-31 11:59:12 -04001426 if (widget->window->type != TYPE_FULLSCREEN) {
1427 widget->window->input_region =
1428 wl_compositor_create_region(display->compositor);
1429 wl_region_add(widget->window->input_region,
1430 t->margin, t->margin,
1431 width - 2 * t->margin,
1432 height - 2 * t->margin);
1433 }
1434
Scott Moreauf7e498c2012-05-14 11:39:29 -06001435 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001436
1437 if (child->opaque) {
1438 widget->window->opaque_region =
1439 wl_compositor_create_region(display->compositor);
1440 wl_region_add(widget->window->opaque_region,
1441 opaque_margin, opaque_margin,
1442 widget->allocation.width - 2 * opaque_margin,
1443 widget->allocation.height - 2 * opaque_margin);
1444 }
Martin Minarik1998b152012-05-10 02:04:35 +02001445
1446 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001447 x_r = frame->widget->allocation.width - t->width - t->margin;
1448 x_l = t->width + t->margin;
1449 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001450 wl_list_for_each(button, &frame->buttons_list, link) {
1451 const int button_padding = 4;
1452 w = cairo_image_surface_get_width(button->icon);
1453 h = cairo_image_surface_get_height(button->icon);
1454
1455 if (button->decoration == FRAME_BUTTON_FANCY)
1456 w += 10;
1457
1458 if (button->align == FRAME_BUTTON_LEFT) {
1459 widget_set_allocation(button->widget,
1460 x_l, y , w + 1, h + 1);
1461 x_l += w;
1462 x_l += button_padding;
1463 } else {
1464 x_r -= w;
1465 widget_set_allocation(button->widget,
1466 x_r, y , w + 1, h + 1);
1467 x_r -= button_padding;
1468 }
1469 }
1470}
1471
1472static int
1473frame_button_enter_handler(struct widget *widget,
1474 struct input *input, float x, float y, void *data)
1475{
1476 struct frame_button *frame_button = data;
1477
1478 widget_schedule_redraw(frame_button->widget);
1479 frame_button->state = FRAME_BUTTON_OVER;
1480
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001481 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001482}
1483
1484static void
1485frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1486{
1487 struct frame_button *frame_button = data;
1488
1489 widget_schedule_redraw(frame_button->widget);
1490 frame_button->state = FRAME_BUTTON_DEFAULT;
1491}
1492
1493static void
1494frame_button_button_handler(struct widget *widget,
1495 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001496 uint32_t button,
1497 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001498{
1499 struct frame_button *frame_button = data;
1500 struct window *window = widget->window;
1501
1502 if (button != BTN_LEFT)
1503 return;
1504
1505 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001506 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001507 frame_button->state = FRAME_BUTTON_ACTIVE;
1508 widget_schedule_redraw(frame_button->widget);
1509
1510 if (frame_button->type == FRAME_BUTTON_ICON)
1511 window_show_frame_menu(window, input, time);
1512 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001513 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001514 frame_button->state = FRAME_BUTTON_DEFAULT;
1515 widget_schedule_redraw(frame_button->widget);
1516 break;
1517 }
1518
1519 switch (frame_button->type) {
1520 case FRAME_BUTTON_CLOSE:
1521 if (window->close_handler)
1522 window->close_handler(window->parent,
1523 window->user_data);
1524 else
1525 display_exit(window->display);
1526 break;
1527 case FRAME_BUTTON_MINIMIZE:
1528 fprintf(stderr,"Minimize stub\n");
1529 break;
1530 case FRAME_BUTTON_MAXIMIZE:
1531 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1532 break;
1533 default:
1534 /* Unknown operation */
1535 break;
1536 }
1537}
1538
1539static void
1540frame_button_redraw_handler(struct widget *widget, void *data)
1541{
1542 struct frame_button *frame_button = data;
1543 cairo_t *cr;
1544 int width, height, x, y;
1545 struct window *window = widget->window;
1546
1547 x = widget->allocation.x;
1548 y = widget->allocation.y;
1549 width = widget->allocation.width;
1550 height = widget->allocation.height;
1551
1552 if (!width)
1553 return;
1554 if (!height)
1555 return;
1556 if (widget->opaque)
1557 return;
1558
1559 cr = cairo_create(window->cairo_surface);
1560
1561 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1562 cairo_set_line_width(cr, 1);
1563
1564 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1565 cairo_rectangle (cr, x, y, 25, 16);
1566
1567 cairo_stroke_preserve(cr);
1568
1569 switch (frame_button->state) {
1570 case FRAME_BUTTON_DEFAULT:
1571 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1572 break;
1573 case FRAME_BUTTON_OVER:
1574 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1575 break;
1576 case FRAME_BUTTON_ACTIVE:
1577 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1578 break;
1579 }
1580
1581 cairo_fill (cr);
1582
1583 x += 4;
1584 }
1585
1586 cairo_set_source_surface(cr, frame_button->icon, x, y);
1587 cairo_paint(cr);
1588
1589 cairo_destroy(cr);
1590}
1591
1592static struct widget *
1593frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1594 enum frame_button_align align, enum frame_button_decoration style)
1595{
1596 struct frame_button *frame_button;
1597 const char *icon = data;
1598
1599 frame_button = malloc (sizeof *frame_button);
1600 memset(frame_button, 0, sizeof *frame_button);
1601
1602 frame_button->icon = cairo_image_surface_create_from_png(icon);
1603 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1604 frame_button->frame = frame;
1605 frame_button->type = type;
1606 frame_button->align = align;
1607 frame_button->decoration = style;
1608
1609 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1610
1611 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1612 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1613 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1614 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1615 return frame_button->widget;
1616}
1617
1618static void
1619frame_button_destroy(struct frame_button *frame_button)
1620{
1621 widget_destroy(frame_button->widget);
1622 wl_list_remove(&frame_button->link);
1623 cairo_surface_destroy(frame_button->icon);
1624 free(frame_button);
1625
1626 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001627}
1628
1629static void
1630frame_redraw_handler(struct widget *widget, void *data)
1631{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001632 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001633 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001634 struct theme *t = window->display->theme;
1635 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001637 if (window->type == TYPE_FULLSCREEN)
1638 return;
1639
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001640 cr = cairo_create(window->cairo_surface);
1641
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001642 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001643 flags |= THEME_FRAME_ACTIVE;
1644 theme_render_frame(t, cr, widget->allocation.width,
1645 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001646
1647 cairo_destroy(cr);
1648}
1649
1650static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001652{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001653 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001654 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001655
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001656 location = theme_get_location(t, input->sx, input->sy,
1657 frame->widget->allocation.width,
1658 frame->widget->allocation.height);
1659
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001660 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001661 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001662 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001663 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001664 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001665 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001666 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001667 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001668 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001669 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001670 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001671 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001672 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001673 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001674 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001675 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001676 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001677 case THEME_LOCATION_EXTERIOR:
1678 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001679 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001680 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001681 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001682}
1683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684static void
1685frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001686{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001687 struct display *display;
1688
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001689 switch (index) {
1690 case 0: /* close */
1691 if (window->close_handler)
1692 window->close_handler(window->parent,
1693 window->user_data);
1694 else
1695 display_exit(window->display);
1696 break;
1697 case 1: /* fullscreen */
1698 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001699 if (window->fullscreen_handler)
1700 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001702 case 2: /* move to workspace above */
1703 display = window->display;
1704 if (display->workspace > 0)
1705 workspace_manager_move_surface(display->workspace_manager,
1706 window->surface,
1707 display->workspace - 1);
1708 break;
1709 case 3: /* move to workspace below */
1710 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001711 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001712 workspace_manager_move_surface(display->workspace_manager,
1713 window->surface,
1714 display->workspace + 1);
1715 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716 }
1717}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001718
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001719void
1720window_show_frame_menu(struct window *window,
1721 struct input *input, uint32_t time)
1722{
1723 int32_t x, y;
1724
1725 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001726 "Close", "Fullscreen",
1727 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001728 };
1729
1730 input_get_position(input, &x, &y);
1731 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001732 x - 10, y - 10, frame_menu_func, entries,
1733 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001734}
1735
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001736static int
1737frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001738 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001739{
1740 return frame_get_pointer_image_for_location(data, input);
1741}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001742
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001743static int
1744frame_motion_handler(struct widget *widget,
1745 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001746 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001747{
1748 return frame_get_pointer_image_for_location(data, input);
1749}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001750
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001751static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752frame_button_handler(struct widget *widget,
1753 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001754 uint32_t button, enum wl_pointer_button_state state,
1755 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001756
1757{
1758 struct frame *frame = data;
1759 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001760 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001761 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001762
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001763 location = theme_get_location(display->theme, input->sx, input->sy,
1764 frame->widget->allocation.width,
1765 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001766
Daniel Stone4dbadb12012-05-30 16:31:51 +01001767 if (window->display->shell && button == BTN_LEFT &&
1768 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001769 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001770 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001771 if (!window->shell_surface)
1772 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001773 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001774 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001775 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001776 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001778 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001779 case THEME_LOCATION_RESIZING_TOP:
1780 case THEME_LOCATION_RESIZING_BOTTOM:
1781 case THEME_LOCATION_RESIZING_LEFT:
1782 case THEME_LOCATION_RESIZING_RIGHT:
1783 case THEME_LOCATION_RESIZING_TOP_LEFT:
1784 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1785 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1786 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001787 if (!window->shell_surface)
1788 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001789 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001790
1791 if (!display->dpy) {
1792 /* If we're using shm, allocate a big
1793 pool to create buffers out of while
1794 we resize. We should probably base
1795 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001796 window->pool =
1797 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001798 }
1799
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001800 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001801 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001802 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001803 break;
1804 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001805 } else if (button == BTN_RIGHT &&
1806 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001807 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808 }
1809}
1810
1811struct widget *
1812frame_create(struct window *window, void *data)
1813{
1814 struct frame *frame;
1815
1816 frame = malloc(sizeof *frame);
1817 memset(frame, 0, sizeof *frame);
1818
1819 frame->widget = window_add_widget(window, frame);
1820 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001821
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001822 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1823 widget_set_resize_handler(frame->widget, frame_resize_handler);
1824 widget_set_enter_handler(frame->widget, frame_enter_handler);
1825 widget_set_motion_handler(frame->widget, frame_motion_handler);
1826 widget_set_button_handler(frame->widget, frame_button_handler);
1827
Martin Minarik1998b152012-05-10 02:04:35 +02001828 /* Create empty list for frame buttons */
1829 wl_list_init(&frame->buttons_list);
1830
1831 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1832 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1833
1834 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1835 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1836
1837 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1838 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1839
1840 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1841 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1842
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001843 window->frame = frame;
1844
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001846}
1847
Kristian Høgsberga1627922012-06-20 17:30:03 -04001848void
1849frame_set_child_size(struct widget *widget, int child_width, int child_height)
1850{
1851 struct display *display = widget->window->display;
1852 struct theme *t = display->theme;
1853 int decoration_width, decoration_height;
1854 int width, height;
1855
1856 if (widget->window->type != TYPE_FULLSCREEN) {
1857 decoration_width = (t->width + t->margin) * 2;
1858 decoration_height = t->width +
1859 t->titlebar_height + t->margin * 2;
1860
1861 width = child_width + decoration_width;
1862 height = child_height + decoration_height;
1863 } else {
1864 width = child_width;
1865 height = child_height;
1866 }
1867
1868 window_schedule_resize(widget->window, width, height);
1869}
1870
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001871static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001872frame_destroy(struct frame *frame)
1873{
Martin Minarik1998b152012-05-10 02:04:35 +02001874 struct frame_button *button, *tmp;
1875
1876 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1877 frame_button_destroy(button);
1878
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001879 /* frame->child must be destroyed by the application */
1880 widget_destroy(frame->widget);
1881 free(frame);
1882}
1883
1884static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001885input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001886 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001887{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001888 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001889 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001890
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001891 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001892 return;
1893
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001894 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001895 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001896 widget = old;
1897 if (input->grab)
1898 widget = input->grab;
1899 if (widget->leave_handler)
1900 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001901 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001902 }
1903
1904 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001905 widget = focus;
1906 if (input->grab)
1907 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001908 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001909 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001910 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001911 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001912
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001913 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001914 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001915}
1916
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001917void
1918input_grab(struct input *input, struct widget *widget, uint32_t button)
1919{
1920 input->grab = widget;
1921 input->grab_button = button;
1922}
1923
1924void
1925input_ungrab(struct input *input)
1926{
1927 struct widget *widget;
1928
1929 input->grab = NULL;
1930 if (input->pointer_focus) {
1931 widget = widget_find_widget(input->pointer_focus->widget,
1932 input->sx, input->sy);
1933 input_set_focus_widget(input, widget, input->sx, input->sy);
1934 }
1935}
1936
1937static void
1938input_remove_pointer_focus(struct input *input)
1939{
1940 struct window *window = input->pointer_focus;
1941
1942 if (!window)
1943 return;
1944
1945 input_set_focus_widget(input, NULL, 0, 0);
1946
1947 input->pointer_focus = NULL;
1948 input->current_cursor = CURSOR_UNSET;
1949}
1950
1951static void
1952pointer_handle_enter(void *data, struct wl_pointer *pointer,
1953 uint32_t serial, struct wl_surface *surface,
1954 wl_fixed_t sx_w, wl_fixed_t sy_w)
1955{
1956 struct input *input = data;
1957 struct window *window;
1958 struct widget *widget;
1959 float sx = wl_fixed_to_double(sx_w);
1960 float sy = wl_fixed_to_double(sy_w);
1961
1962 if (!surface) {
1963 /* enter event for a window we've just destroyed */
1964 return;
1965 }
1966
1967 input->display->serial = serial;
1968 input->pointer_enter_serial = serial;
1969 input->pointer_focus = wl_surface_get_user_data(surface);
1970 window = input->pointer_focus;
1971
1972 if (window->pool) {
1973 shm_pool_destroy(window->pool);
1974 window->pool = NULL;
1975 /* Schedule a redraw to free the pool */
1976 window_schedule_redraw(window);
1977 }
1978
1979 input->sx = sx;
1980 input->sy = sy;
1981
1982 widget = widget_find_widget(window->widget, sx, sy);
1983 input_set_focus_widget(input, widget, sx, sy);
1984}
1985
1986static void
1987pointer_handle_leave(void *data, struct wl_pointer *pointer,
1988 uint32_t serial, struct wl_surface *surface)
1989{
1990 struct input *input = data;
1991
1992 input->display->serial = serial;
1993 input_remove_pointer_focus(input);
1994}
1995
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001996static void
Daniel Stone37816df2012-05-16 18:45:18 +01001997pointer_handle_motion(void *data, struct wl_pointer *pointer,
1998 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001999{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002000 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002001 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002002 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002003 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002004 float sx = wl_fixed_to_double(sx_w);
2005 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002006
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002007 input->sx = sx;
2008 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002009
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002010 if (!window)
2011 return;
2012
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002013 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002014 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002015 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002016 }
2017
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002018 if (input->grab)
2019 widget = input->grab;
2020 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002021 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002022 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002023 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002024 input, time, sx, sy,
2025 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002026
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002027 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002028}
2029
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002030static void
Daniel Stone37816df2012-05-16 18:45:18 +01002031pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002032 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002033{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002034 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002035 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002036 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002037
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002038 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002039 if (input->focus_widget && input->grab == NULL &&
2040 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002041 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002042
Neil Roberts6b28aad2012-01-23 19:11:18 +00002043 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002045 (*widget->button_handler)(widget,
2046 input, time,
2047 button, state,
2048 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002049
Daniel Stone4dbadb12012-05-30 16:31:51 +01002050 if (input->grab && input->grab_button == button &&
2051 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002052 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002053}
2054
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002055static void
Daniel Stone37816df2012-05-16 18:45:18 +01002056pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002057 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002058{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002059 struct input *input = data;
2060 struct widget *widget;
2061
2062 widget = input->focus_widget;
2063 if (input->grab)
2064 widget = input->grab;
2065 if (widget && widget->axis_handler)
2066 (*widget->axis_handler)(widget,
2067 input, time,
2068 axis, value,
2069 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002070}
2071
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002072static const struct wl_pointer_listener pointer_listener = {
2073 pointer_handle_enter,
2074 pointer_handle_leave,
2075 pointer_handle_motion,
2076 pointer_handle_button,
2077 pointer_handle_axis,
2078};
2079
2080static void
2081input_remove_keyboard_focus(struct input *input)
2082{
2083 struct window *window = input->keyboard_focus;
2084 struct itimerspec its;
2085
2086 its.it_interval.tv_sec = 0;
2087 its.it_interval.tv_nsec = 0;
2088 its.it_value.tv_sec = 0;
2089 its.it_value.tv_nsec = 0;
2090 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2091
2092 if (!window)
2093 return;
2094
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002095 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002096 if (window->keyboard_focus_handler)
2097 (*window->keyboard_focus_handler)(window, NULL,
2098 window->user_data);
2099
2100 input->keyboard_focus = NULL;
2101}
2102
2103static void
2104keyboard_repeat_func(struct task *task, uint32_t events)
2105{
2106 struct input *input =
2107 container_of(task, struct input, repeat_task);
2108 struct window *window = input->keyboard_focus;
2109 uint64_t exp;
2110
2111 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2112 /* If we change the timer between the fd becoming
2113 * readable and getting here, there'll be nothing to
2114 * read and we get EAGAIN. */
2115 return;
2116
2117 if (window && window->key_handler) {
2118 (*window->key_handler)(window, input, input->repeat_time,
2119 input->repeat_key, input->repeat_sym,
2120 WL_KEYBOARD_KEY_STATE_PRESSED,
2121 window->user_data);
2122 }
2123}
2124
2125static void
2126keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2127 uint32_t format, int fd, uint32_t size)
2128{
2129 struct input *input = data;
2130 char *map_str;
2131
2132 if (!data) {
2133 close(fd);
2134 return;
2135 }
2136
2137 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2138 close(fd);
2139 return;
2140 }
2141
2142 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2143 if (map_str == MAP_FAILED) {
2144 close(fd);
2145 return;
2146 }
2147
2148 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2149 map_str,
2150 XKB_KEYMAP_FORMAT_TEXT_V1,
2151 0);
2152 munmap(map_str, size);
2153 close(fd);
2154
2155 if (!input->xkb.keymap) {
2156 fprintf(stderr, "failed to compile keymap\n");
2157 return;
2158 }
2159
2160 input->xkb.state = xkb_state_new(input->xkb.keymap);
2161 if (!input->xkb.state) {
2162 fprintf(stderr, "failed to create XKB state\n");
2163 xkb_map_unref(input->xkb.keymap);
2164 input->xkb.keymap = NULL;
2165 return;
2166 }
2167
2168 input->xkb.control_mask =
2169 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2170 input->xkb.alt_mask =
2171 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2172 input->xkb.shift_mask =
2173 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2174}
2175
2176static void
2177keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2178 uint32_t serial, struct wl_surface *surface,
2179 struct wl_array *keys)
2180{
2181 struct input *input = data;
2182 struct window *window;
2183
2184 input->display->serial = serial;
2185 input->keyboard_focus = wl_surface_get_user_data(surface);
2186
2187 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002188 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002189 if (window->keyboard_focus_handler)
2190 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002191 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002192}
2193
2194static void
2195keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2196 uint32_t serial, struct wl_surface *surface)
2197{
2198 struct input *input = data;
2199
2200 input->display->serial = serial;
2201 input_remove_keyboard_focus(input);
2202}
2203
Scott Moreau210d0792012-03-22 10:47:01 -06002204static void
Daniel Stone37816df2012-05-16 18:45:18 +01002205keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002206 uint32_t serial, uint32_t time, uint32_t key,
2207 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002208{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002209 struct input *input = data;
2210 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002211 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002212 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002213 const xkb_keysym_t *syms;
2214 xkb_keysym_t sym;
2215 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002216 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002217
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002218 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002219 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002220 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002221 return;
2222
Daniel Stone97f68542012-05-30 16:32:01 +01002223 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002224
Daniel Stone97f68542012-05-30 16:32:01 +01002225 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002226 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002227 XKB_STATE_LATCHED);
2228 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002229 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002230 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002231 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002232 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002233 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002234 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002235
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002236 sym = XKB_KEY_NoSymbol;
2237 if (num_syms == 1)
2238 sym = syms[0];
2239
2240 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002241 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002242 window_set_maximized(window,
2243 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002244 } else if (sym == XKB_KEY_F11 &&
2245 window->fullscreen_handler &&
2246 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2247 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002248 } else if (sym == XKB_KEY_F4 &&
2249 input->modifiers == MOD_ALT_MASK &&
2250 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2251 if (window->close_handler)
2252 window->close_handler(window->parent,
2253 window->user_data);
2254 else
2255 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002256 } else if (window->key_handler) {
2257 (*window->key_handler)(window, input, time, key,
2258 sym, state, window->user_data);
2259 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002260
2261 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2262 key == input->repeat_key) {
2263 its.it_interval.tv_sec = 0;
2264 its.it_interval.tv_nsec = 0;
2265 its.it_value.tv_sec = 0;
2266 its.it_value.tv_nsec = 0;
2267 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2268 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2269 input->repeat_sym = sym;
2270 input->repeat_key = key;
2271 input->repeat_time = time;
2272 its.it_interval.tv_sec = 0;
2273 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2274 its.it_value.tv_sec = 0;
2275 its.it_value.tv_nsec = 400 * 1000 * 1000;
2276 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2277 }
2278}
2279
2280static void
Daniel Stone351eb612012-05-31 15:27:47 -04002281keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2282 uint32_t serial, uint32_t mods_depressed,
2283 uint32_t mods_latched, uint32_t mods_locked,
2284 uint32_t group)
2285{
2286 struct input *input = data;
2287
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002288 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2289 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002290}
2291
Daniel Stone37816df2012-05-16 18:45:18 +01002292static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002293 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002294 keyboard_handle_enter,
2295 keyboard_handle_leave,
2296 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002297 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002298};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002299
2300static void
Daniel Stone37816df2012-05-16 18:45:18 +01002301seat_handle_capabilities(void *data, struct wl_seat *seat,
2302 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002303{
Daniel Stone37816df2012-05-16 18:45:18 +01002304 struct input *input = data;
2305
2306 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2307 input->pointer = wl_seat_get_pointer(seat);
2308 wl_pointer_set_user_data(input->pointer, input);
2309 wl_pointer_add_listener(input->pointer, &pointer_listener,
2310 input);
2311 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2312 wl_pointer_destroy(input->pointer);
2313 input->pointer = NULL;
2314 }
2315
2316 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2317 input->keyboard = wl_seat_get_keyboard(seat);
2318 wl_keyboard_set_user_data(input->keyboard, input);
2319 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2320 input);
2321 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2322 wl_keyboard_destroy(input->keyboard);
2323 input->keyboard = NULL;
2324 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002325}
2326
Daniel Stone37816df2012-05-16 18:45:18 +01002327static const struct wl_seat_listener seat_listener = {
2328 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002329};
2330
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002331void
2332input_get_position(struct input *input, int32_t *x, int32_t *y)
2333{
2334 *x = input->sx;
2335 *y = input->sy;
2336}
2337
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002338struct display *
2339input_get_display(struct input *input)
2340{
2341 return input->display;
2342}
2343
Daniel Stone37816df2012-05-16 18:45:18 +01002344struct wl_seat *
2345input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002346{
Daniel Stone37816df2012-05-16 18:45:18 +01002347 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002348}
2349
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002350uint32_t
2351input_get_modifiers(struct input *input)
2352{
2353 return input->modifiers;
2354}
2355
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002356struct widget *
2357input_get_focus_widget(struct input *input)
2358{
2359 return input->focus_widget;
2360}
2361
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002362struct data_offer {
2363 struct wl_data_offer *offer;
2364 struct input *input;
2365 struct wl_array types;
2366 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002367
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002368 struct task io_task;
2369 int fd;
2370 data_func_t func;
2371 int32_t x, y;
2372 void *user_data;
2373};
2374
2375static void
2376data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2377{
2378 struct data_offer *offer = data;
2379 char **p;
2380
2381 p = wl_array_add(&offer->types, sizeof *p);
2382 *p = strdup(type);
2383}
2384
2385static const struct wl_data_offer_listener data_offer_listener = {
2386 data_offer_offer,
2387};
2388
2389static void
2390data_offer_destroy(struct data_offer *offer)
2391{
2392 char **p;
2393
2394 offer->refcount--;
2395 if (offer->refcount == 0) {
2396 wl_data_offer_destroy(offer->offer);
2397 for (p = offer->types.data; *p; p++)
2398 free(*p);
2399 wl_array_release(&offer->types);
2400 free(offer);
2401 }
2402}
2403
2404static void
2405data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002406 struct wl_data_device *data_device,
2407 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002408{
2409 struct data_offer *offer;
2410
2411 offer = malloc(sizeof *offer);
2412
2413 wl_array_init(&offer->types);
2414 offer->refcount = 1;
2415 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002416 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002417 wl_data_offer_add_listener(offer->offer,
2418 &data_offer_listener, offer);
2419}
2420
2421static void
2422data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002423 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002424 wl_fixed_t x_w, wl_fixed_t y_w,
2425 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002426{
2427 struct input *input = data;
2428 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002429 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002430 float x = wl_fixed_to_double(x_w);
2431 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002432 char **p;
2433
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002434 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002435 window = wl_surface_get_user_data(surface);
2436 input->pointer_focus = window;
2437
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002438 if (offer) {
2439 input->drag_offer = wl_data_offer_get_user_data(offer);
2440
2441 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2442 *p = NULL;
2443
2444 types_data = input->drag_offer->types.data;
2445 } else {
2446 input->drag_offer = NULL;
2447 types_data = NULL;
2448 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002449
2450 window = input->pointer_focus;
2451 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002452 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002453 window->user_data);
2454}
2455
2456static void
2457data_device_leave(void *data, struct wl_data_device *data_device)
2458{
2459 struct input *input = data;
2460
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002461 if (input->drag_offer) {
2462 data_offer_destroy(input->drag_offer);
2463 input->drag_offer = NULL;
2464 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002465}
2466
2467static void
2468data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002469 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002470{
2471 struct input *input = data;
2472 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002473 float x = wl_fixed_to_double(x_w);
2474 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002475 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002476
2477 input->sx = x;
2478 input->sy = y;
2479
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002480 if (input->drag_offer)
2481 types_data = input->drag_offer->types.data;
2482 else
2483 types_data = NULL;
2484
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002485 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002486 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002487 window->user_data);
2488}
2489
2490static void
2491data_device_drop(void *data, struct wl_data_device *data_device)
2492{
2493 struct input *input = data;
2494 struct window *window = input->pointer_focus;
2495
2496 if (window->drop_handler)
2497 window->drop_handler(window, input,
2498 input->sx, input->sy, window->user_data);
2499}
2500
2501static void
2502data_device_selection(void *data,
2503 struct wl_data_device *wl_data_device,
2504 struct wl_data_offer *offer)
2505{
2506 struct input *input = data;
2507 char **p;
2508
2509 if (input->selection_offer)
2510 data_offer_destroy(input->selection_offer);
2511
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002512 if (offer) {
2513 input->selection_offer = wl_data_offer_get_user_data(offer);
2514 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2515 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002516 } else {
2517 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002518 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002519}
2520
2521static const struct wl_data_device_listener data_device_listener = {
2522 data_device_data_offer,
2523 data_device_enter,
2524 data_device_leave,
2525 data_device_motion,
2526 data_device_drop,
2527 data_device_selection
2528};
2529
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002530static void
2531input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002532{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002533 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002534 struct wl_cursor *cursor;
2535 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002536
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002537 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002538 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002539 return;
2540
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002541 if (index >= (int) cursor->image_count) {
2542 fprintf(stderr, "cursor index out of range\n");
2543 return;
2544 }
2545
2546 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002547 buffer = wl_cursor_image_get_buffer(image);
2548 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002549 return;
2550
Kristian Høgsbergae277372012-08-01 09:41:08 -04002551 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002552 input->pointer_surface,
2553 image->hotspot_x, image->hotspot_y);
2554 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2555 wl_surface_damage(input->pointer_surface, 0, 0,
2556 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002557}
2558
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002559static const struct wl_callback_listener pointer_surface_listener;
2560
2561static void
2562pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2563 uint32_t time)
2564{
2565 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002566 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002567 int i;
2568
2569 if (callback) {
2570 assert(callback == input->cursor_frame_cb);
2571 wl_callback_destroy(callback);
2572 input->cursor_frame_cb = NULL;
2573 }
2574
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002575 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002576 wl_pointer_set_cursor(input->pointer,
2577 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002578 NULL, 0, 0);
2579 return;
2580 }
2581
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002582 if (input->current_cursor == CURSOR_UNSET)
2583 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002584 cursor = input->display->cursors[input->current_cursor];
2585 if (!cursor)
2586 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002587
2588 /* FIXME We don't have the current time on the first call so we set
2589 * the animation start to the time of the first frame callback. */
2590 if (time == 0)
2591 input->cursor_anim_start = 0;
2592 else if (input->cursor_anim_start == 0)
2593 input->cursor_anim_start = time;
2594
2595 if (time == 0 || input->cursor_anim_start == 0)
2596 i = 0;
2597 else
2598 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2599
2600 input_set_pointer_image_index(input, i);
2601
2602 if (cursor->image_count == 1)
2603 return;
2604
2605 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2606 wl_callback_add_listener(input->cursor_frame_cb,
2607 &pointer_surface_listener, input);
2608}
2609
2610static const struct wl_callback_listener pointer_surface_listener = {
2611 pointer_surface_frame_callback
2612};
2613
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002614void
2615input_set_pointer_image(struct input *input, int pointer)
2616{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002617 int force = 0;
2618
2619 if (input->pointer_enter_serial > input->cursor_serial)
2620 force = 1;
2621
2622 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002623 return;
2624
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002625 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002626 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002627 if (!input->cursor_frame_cb)
2628 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002629 else if (force) {
2630 /* The current frame callback may be stuck if, for instance,
2631 * the set cursor request was processed by the server after
2632 * this client lost the focus. In this case the cursor surface
2633 * might not be mapped and the frame callback wouldn't ever
2634 * complete. Send a set_cursor and attach to try to map the
2635 * cursor surface again so that the callback will finish */
2636 input_set_pointer_image_index(input, 0);
2637 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002638}
2639
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002640struct wl_data_device *
2641input_get_data_device(struct input *input)
2642{
2643 return input->data_device;
2644}
2645
2646void
2647input_set_selection(struct input *input,
2648 struct wl_data_source *source, uint32_t time)
2649{
2650 wl_data_device_set_selection(input->data_device, source, time);
2651}
2652
2653void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002654input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002655{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002656 wl_data_offer_accept(input->drag_offer->offer,
2657 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002658}
2659
2660static void
2661offer_io_func(struct task *task, uint32_t events)
2662{
2663 struct data_offer *offer =
2664 container_of(task, struct data_offer, io_task);
2665 unsigned int len;
2666 char buffer[4096];
2667
2668 len = read(offer->fd, buffer, sizeof buffer);
2669 offer->func(buffer, len,
2670 offer->x, offer->y, offer->user_data);
2671
2672 if (len == 0) {
2673 close(offer->fd);
2674 data_offer_destroy(offer);
2675 }
2676}
2677
2678static void
2679data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2680 data_func_t func, void *user_data)
2681{
2682 int p[2];
2683
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002684 if (pipe2(p, O_CLOEXEC) == -1)
2685 return;
2686
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002687 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2688 close(p[1]);
2689
2690 offer->io_task.run = offer_io_func;
2691 offer->fd = p[0];
2692 offer->func = func;
2693 offer->refcount++;
2694 offer->user_data = user_data;
2695
2696 display_watch_fd(offer->input->display,
2697 offer->fd, EPOLLIN, &offer->io_task);
2698}
2699
2700void
2701input_receive_drag_data(struct input *input, const char *mime_type,
2702 data_func_t func, void *data)
2703{
2704 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2705 input->drag_offer->x = input->sx;
2706 input->drag_offer->y = input->sy;
2707}
2708
2709int
2710input_receive_selection_data(struct input *input, const char *mime_type,
2711 data_func_t func, void *data)
2712{
2713 char **p;
2714
2715 if (input->selection_offer == NULL)
2716 return -1;
2717
2718 for (p = input->selection_offer->types.data; *p; p++)
2719 if (strcmp(mime_type, *p) == 0)
2720 break;
2721
2722 if (*p == NULL)
2723 return -1;
2724
2725 data_offer_receive_data(input->selection_offer,
2726 mime_type, func, data);
2727 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002728}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002729
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002730int
2731input_receive_selection_data_to_fd(struct input *input,
2732 const char *mime_type, int fd)
2733{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002734 if (input->selection_offer)
2735 wl_data_offer_receive(input->selection_offer->offer,
2736 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002737
2738 return 0;
2739}
2740
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002741void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002742window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002743{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002744 if (!window->shell_surface)
2745 return;
2746
Daniel Stone37816df2012-05-16 18:45:18 +01002747 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002748}
2749
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002750static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002751idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002752{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002753 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002754
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002755 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002756 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002757 widget_set_allocation(widget,
2758 window->pending_allocation.x,
2759 window->pending_allocation.y,
2760 window->pending_allocation.width,
2761 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002762
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002763 if (window->input_region) {
2764 wl_region_destroy(window->input_region);
2765 window->input_region = NULL;
2766 }
2767
2768 if (window->opaque_region) {
2769 wl_region_destroy(window->opaque_region);
2770 window->opaque_region = NULL;
2771 }
2772
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002773 if (widget->resize_handler)
2774 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002775 widget->allocation.width,
2776 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002777 widget->user_data);
2778
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002779 if (window->allocation.width != widget->allocation.width ||
2780 window->allocation.height != widget->allocation.height) {
2781 window->allocation = widget->allocation;
2782 window_schedule_redraw(window);
2783 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002784}
2785
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002786void
2787window_schedule_resize(struct window *window, int width, int height)
2788{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002789 window->pending_allocation.x = 0;
2790 window->pending_allocation.y = 0;
2791 window->pending_allocation.width = width;
2792 window->pending_allocation.height = height;
2793
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002794 if (window->min_allocation.width == 0)
2795 window->min_allocation = window->pending_allocation;
2796 if (window->pending_allocation.width < window->min_allocation.width)
2797 window->pending_allocation.width = window->min_allocation.width;
2798 if (window->pending_allocation.height < window->min_allocation.height)
2799 window->pending_allocation.height = window->min_allocation.height;
2800
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002801 window->resize_needed = 1;
2802 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002803}
2804
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002805void
2806widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2807{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002808 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002809}
2810
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002811static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002812handle_ping(void *data, struct wl_shell_surface *shell_surface,
2813 uint32_t serial)
2814{
2815 wl_shell_surface_pong(shell_surface, serial);
2816}
2817
2818static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002819handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002820 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002821{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002822 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002823
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002824 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002825 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002826}
2827
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002828static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002829menu_destroy(struct menu *menu)
2830{
2831 widget_destroy(menu->widget);
2832 window_destroy(menu->window);
2833 free(menu);
2834}
2835
2836static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002837handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2838{
2839 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002840 struct menu *menu = window->widget->user_data;
2841
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002842 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002843 * device. Or just use wl_callback. And this really needs to
2844 * be a window vfunc that the menu can set. And we need the
2845 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002847 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002848 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002849 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850}
2851
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002852static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002853 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002854 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002855 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002856};
2857
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002858void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002859window_get_allocation(struct window *window,
2860 struct rectangle *allocation)
2861{
2862 *allocation = window->allocation;
2863}
2864
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002865static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002866widget_redraw(struct widget *widget)
2867{
2868 struct widget *child;
2869
2870 if (widget->redraw_handler)
2871 widget->redraw_handler(widget, widget->user_data);
2872 wl_list_for_each(child, &widget->child_list, link)
2873 widget_redraw(child);
2874}
2875
2876static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002877frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2878{
2879 struct window *window = data;
2880
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002881 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002882 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002883 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002884 window->redraw_scheduled = 0;
2885 if (window->redraw_needed)
2886 window_schedule_redraw(window);
2887}
2888
2889static const struct wl_callback_listener listener = {
2890 frame_callback
2891};
2892
2893static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002894idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002895{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002896 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002897
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002898 if (window->resize_needed)
2899 idle_resize(window);
2900
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002901 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002902 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002903 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002904 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002905 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002906
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002907 window->frame_cb = wl_surface_frame(window->surface);
2908 wl_callback_add_listener(window->frame_cb, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002909}
2910
2911void
2912window_schedule_redraw(struct window *window)
2913{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002914 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002915 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002916 window->redraw_task.run = idle_redraw;
2917 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002918 window->redraw_scheduled = 1;
2919 }
2920}
2921
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002922void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002923window_set_fullscreen(struct window *window, int fullscreen)
2924{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002925 if (!window->display->shell)
2926 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002927
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002928 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002929 return;
2930
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002931 if (fullscreen) {
2932 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002933 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002934 wl_shell_surface_set_fullscreen(window->shell_surface,
2935 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2936 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002937 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002938 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002939 wl_shell_surface_set_toplevel(window->shell_surface);
2940 window_schedule_resize(window,
2941 window->saved_allocation.width,
2942 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002943 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002944}
2945
2946void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002947window_set_maximized(struct window *window, int maximized)
2948{
2949 if (!window->display->shell)
2950 return;
2951
2952 if ((window->type == TYPE_MAXIMIZED) == maximized)
2953 return;
2954
2955 if (window->type == TYPE_TOPLEVEL) {
2956 window->saved_allocation = window->allocation;
2957 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2958 window->type = TYPE_MAXIMIZED;
2959 } else {
2960 wl_shell_surface_set_toplevel(window->shell_surface);
2961 window->type = TYPE_TOPLEVEL;
2962 window_schedule_resize(window,
2963 window->saved_allocation.width,
2964 window->saved_allocation.height);
2965 }
2966}
2967
2968void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002969window_set_user_data(struct window *window, void *data)
2970{
2971 window->user_data = data;
2972}
2973
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002974void *
2975window_get_user_data(struct window *window)
2976{
2977 return window->user_data;
2978}
2979
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002980void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002981window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002982 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002983{
2984 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002985}
2986
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002987void
2988window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002989 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002990{
2991 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002992}
2993
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002994void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002995window_set_data_handler(struct window *window, window_data_handler_t handler)
2996{
2997 window->data_handler = handler;
2998}
2999
3000void
3001window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3002{
3003 window->drop_handler = handler;
3004}
3005
3006void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003007window_set_close_handler(struct window *window,
3008 window_close_handler_t handler)
3009{
3010 window->close_handler = handler;
3011}
3012
3013void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003014window_set_fullscreen_handler(struct window *window,
3015 window_fullscreen_handler_t handler)
3016{
3017 window->fullscreen_handler = handler;
3018}
3019
3020void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003021window_set_title(struct window *window, const char *title)
3022{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003023 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003024 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003025 if (window->shell_surface)
3026 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003027}
3028
3029const char *
3030window_get_title(struct window *window)
3031{
3032 return window->title;
3033}
3034
3035void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003036window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3037{
3038 struct text_cursor_position *text_cursor_position =
3039 window->display->text_cursor_position;
3040
Scott Moreau9295ce02012-06-01 12:46:10 -06003041 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003042 return;
3043
3044 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003045 window->surface,
3046 wl_fixed_from_int(x),
3047 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003048}
3049
3050void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003051window_damage(struct window *window, int32_t x, int32_t y,
3052 int32_t width, int32_t height)
3053{
3054 wl_surface_damage(window->surface, x, y, width, height);
3055}
3056
Casey Dahlin9074db52012-04-19 22:50:09 -04003057static void
3058surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003059 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003060{
Rob Bradford7507b572012-05-15 17:55:34 +01003061 struct window *window = data;
3062 struct output *output;
3063 struct output *output_found = NULL;
3064 struct window_output *window_output;
3065
3066 wl_list_for_each(output, &window->display->output_list, link) {
3067 if (output->output == wl_output) {
3068 output_found = output;
3069 break;
3070 }
3071 }
3072
3073 if (!output_found)
3074 return;
3075
3076 window_output = malloc (sizeof *window_output);
3077 window_output->output = output_found;
3078
3079 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003080}
3081
3082static void
3083surface_leave(void *data,
3084 struct wl_surface *wl_surface, struct wl_output *output)
3085{
Rob Bradford7507b572012-05-15 17:55:34 +01003086 struct window *window = data;
3087 struct window_output *window_output;
3088 struct window_output *window_output_found = NULL;
3089
3090 wl_list_for_each(window_output, &window->window_output_list, link) {
3091 if (window_output->output->output == output) {
3092 window_output_found = window_output;
3093 break;
3094 }
3095 }
3096
3097 if (window_output_found) {
3098 wl_list_remove(&window_output_found->link);
3099 free(window_output_found);
3100 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003101}
3102
3103static const struct wl_surface_listener surface_listener = {
3104 surface_enter,
3105 surface_leave
3106};
3107
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003108static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003109window_create_internal(struct display *display,
3110 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003111{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003112 struct window *window;
3113
3114 window = malloc(sizeof *window);
3115 if (window == NULL)
3116 return NULL;
3117
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003118 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003119 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003120 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003121 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003122 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003123 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003124 window->shell_surface =
3125 wl_shell_get_shell_surface(display->shell,
3126 window->surface);
3127 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003128 window->allocation.x = 0;
3129 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003130 window->allocation.width = 0;
3131 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003132 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003133 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003134 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003135 window->input_region = NULL;
3136 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003137
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003138 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003139#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003140 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003141#else
3142 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3143#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003144 else
3145 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003146
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003147 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003148 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003149 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003150
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003151 if (window->shell_surface) {
3152 wl_shell_surface_set_user_data(window->shell_surface, window);
3153 wl_shell_surface_add_listener(window->shell_surface,
3154 &shell_surface_listener, window);
3155 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003156
Rob Bradford7507b572012-05-15 17:55:34 +01003157 wl_list_init (&window->window_output_list);
3158
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003159 return window;
3160}
3161
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003162struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003163window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003164{
3165 struct window *window;
3166
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003167 window = window_create_internal(display, NULL, TYPE_NONE);
3168 if (!window)
3169 return NULL;
3170
3171 return window;
3172}
3173
3174struct window *
3175window_create_custom(struct display *display)
3176{
3177 struct window *window;
3178
3179 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003180 if (!window)
3181 return NULL;
3182
3183 return window;
3184}
3185
3186struct window *
3187window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003188 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003189{
3190 struct window *window;
3191
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003192 window = window_create_internal(parent->display,
3193 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003194 if (!window)
3195 return NULL;
3196
3197 window->x = x;
3198 window->y = y;
3199
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003200 if (display->shell)
3201 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003202 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003203 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003204
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003205 return window;
3206}
3207
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003208static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003209menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003210{
3211 int next;
3212
3213 next = (sy - 8) / 20;
3214 if (menu->current != next) {
3215 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003216 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003217 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003218}
3219
3220static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003221menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003222 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003223 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003224{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003225 struct menu *menu = data;
3226
3227 if (widget == menu->widget)
3228 menu_set_item(data, y);
3229
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003230 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003231}
3232
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003233static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003234menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003235 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003236{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003237 struct menu *menu = data;
3238
3239 if (widget == menu->widget)
3240 menu_set_item(data, y);
3241
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003242 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003243}
3244
3245static void
3246menu_leave_handler(struct widget *widget, struct input *input, void *data)
3247{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003248 struct menu *menu = data;
3249
3250 if (widget == menu->widget)
3251 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003252}
3253
3254static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003255menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003256 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003257 uint32_t button, enum wl_pointer_button_state state,
3258 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003259
3260{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003261 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003262
Daniel Stone4dbadb12012-05-30 16:31:51 +01003263 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3264 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003265 /* Either relase after press-drag-release or
3266 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003267 menu->func(menu->window->parent,
3268 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003269 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003270 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003271 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003272}
3273
3274static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003275menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003276{
3277 cairo_t *cr;
3278 const int32_t r = 3, margin = 3;
3279 struct menu *menu = data;
3280 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003281 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003282
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003283 cr = cairo_create(window->cairo_surface);
3284 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3285 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3286 cairo_paint(cr);
3287
3288 width = window->allocation.width;
3289 height = window->allocation.height;
3290 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003291
3292 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003293 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3294 cairo_fill(cr);
3295
3296 for (i = 0; i < menu->count; i++) {
3297 if (i == menu->current) {
3298 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3299 cairo_rectangle(cr, margin, i * 20 + margin,
3300 width - 2 * margin, 20);
3301 cairo_fill(cr);
3302 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3303 cairo_move_to(cr, 10, i * 20 + 16);
3304 cairo_show_text(cr, menu->entries[i]);
3305 } else {
3306 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3307 cairo_move_to(cr, 10, i * 20 + 16);
3308 cairo_show_text(cr, menu->entries[i]);
3309 }
3310 }
3311
3312 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003313}
3314
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003315void
3316window_show_menu(struct display *display,
3317 struct input *input, uint32_t time, struct window *parent,
3318 int32_t x, int32_t y,
3319 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003320{
3321 struct window *window;
3322 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003323 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003324
3325 menu = malloc(sizeof *menu);
3326 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003327 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003328
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003329 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003330 if (!window) {
3331 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003332 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003333 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003334
3335 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003336 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003337 menu->entries = entries;
3338 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003339 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003340 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003341 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003342 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003343 window->type = TYPE_MENU;
3344 window->x = x;
3345 window->y = y;
3346
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003347 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003348 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003349 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003350 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003351 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003352
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003353 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003354 widget_set_enter_handler(menu->widget, menu_enter_handler);
3355 widget_set_leave_handler(menu->widget, menu_leave_handler);
3356 widget_set_motion_handler(menu->widget, menu_motion_handler);
3357 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003358
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003359 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003360 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003361}
3362
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003363void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003364window_set_buffer_type(struct window *window, enum window_buffer_type type)
3365{
3366 window->buffer_type = type;
3367}
3368
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003369
3370static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003371display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003372 struct wl_output *wl_output,
3373 int x, int y,
3374 int physical_width,
3375 int physical_height,
3376 int subpixel,
3377 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003378 const char *model,
3379 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003380{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003381 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003382
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003383 output->allocation.x = x;
3384 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003385}
3386
3387static void
3388display_handle_mode(void *data,
3389 struct wl_output *wl_output,
3390 uint32_t flags,
3391 int width,
3392 int height,
3393 int refresh)
3394{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003395 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003396 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003397
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003398 if (flags & WL_OUTPUT_MODE_CURRENT) {
3399 output->allocation.width = width;
3400 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003401 if (display->output_configure_handler)
3402 (*display->output_configure_handler)(
3403 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003404 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003405}
3406
3407static const struct wl_output_listener output_listener = {
3408 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003409 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003410};
3411
3412static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003413display_add_output(struct display *d, uint32_t id)
3414{
3415 struct output *output;
3416
3417 output = malloc(sizeof *output);
3418 if (output == NULL)
3419 return;
3420
3421 memset(output, 0, sizeof *output);
3422 output->display = d;
3423 output->output =
3424 wl_display_bind(d->display, id, &wl_output_interface);
3425 wl_list_insert(d->output_list.prev, &output->link);
3426
3427 wl_output_add_listener(output->output, &output_listener, output);
3428}
3429
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003430static void
3431output_destroy(struct output *output)
3432{
3433 if (output->destroy_handler)
3434 (*output->destroy_handler)(output, output->user_data);
3435
3436 wl_output_destroy(output->output);
3437 wl_list_remove(&output->link);
3438 free(output);
3439}
3440
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003441void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003442display_set_output_configure_handler(struct display *display,
3443 display_output_handler_t handler)
3444{
3445 struct output *output;
3446
3447 display->output_configure_handler = handler;
3448 if (!handler)
3449 return;
3450
3451 wl_list_for_each(output, &display->output_list, link)
3452 (*display->output_configure_handler)(output,
3453 display->user_data);
3454}
3455
3456void
3457output_set_user_data(struct output *output, void *data)
3458{
3459 output->user_data = data;
3460}
3461
3462void *
3463output_get_user_data(struct output *output)
3464{
3465 return output->user_data;
3466}
3467
3468void
3469output_set_destroy_handler(struct output *output,
3470 display_output_handler_t handler)
3471{
3472 output->destroy_handler = handler;
3473 /* FIXME: implement this, once we have way to remove outputs */
3474}
3475
3476void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003477output_get_allocation(struct output *output, struct rectangle *allocation)
3478{
3479 *allocation = output->allocation;
3480}
3481
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003482struct wl_output *
3483output_get_wl_output(struct output *output)
3484{
3485 return output->output;
3486}
3487
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003488static void
Daniel Stone97f68542012-05-30 16:32:01 +01003489fini_xkb(struct input *input)
3490{
3491 xkb_state_unref(input->xkb.state);
3492 xkb_map_unref(input->xkb.keymap);
3493}
3494
3495static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003496display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003497{
3498 struct input *input;
3499
3500 input = malloc(sizeof *input);
3501 if (input == NULL)
3502 return;
3503
3504 memset(input, 0, sizeof *input);
3505 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003506 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003507 input->pointer_focus = NULL;
3508 input->keyboard_focus = NULL;
3509 wl_list_insert(d->input_list.prev, &input->link);
3510
Daniel Stone37816df2012-05-16 18:45:18 +01003511 wl_seat_add_listener(input->seat, &seat_listener, input);
3512 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003513
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003514 input->data_device =
3515 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003516 input->seat);
3517 wl_data_device_add_listener(input->data_device, &data_device_listener,
3518 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003519
3520 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003521
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003522 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3523 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003524 input->repeat_task.run = keyboard_repeat_func;
3525 display_watch_fd(d, input->repeat_timer_fd,
3526 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003527}
3528
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003529static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003530input_destroy(struct input *input)
3531{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003532 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003533 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003534
3535 if (input->drag_offer)
3536 data_offer_destroy(input->drag_offer);
3537
3538 if (input->selection_offer)
3539 data_offer_destroy(input->selection_offer);
3540
3541 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003542 fini_xkb(input);
3543
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003544 wl_surface_destroy(input->pointer_surface);
3545
Pekka Paalanene1207c72011-12-16 12:02:09 +02003546 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003547 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003548 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003549 free(input);
3550}
3551
3552static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003553init_workspace_manager(struct display *d, uint32_t id)
3554{
3555 d->workspace_manager =
3556 wl_display_bind(d->display, id, &workspace_manager_interface);
3557 if (d->workspace_manager != NULL)
3558 workspace_manager_add_listener(d->workspace_manager,
3559 &workspace_manager_listener,
3560 d);
3561}
3562
3563static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003564display_handle_global(struct wl_display *display, uint32_t id,
3565 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003566{
3567 struct display *d = data;
3568
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003569 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003570 d->compositor =
3571 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003572 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003573 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003574 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003575 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003576 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003577 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003578 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003579 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3581 d->data_device_manager =
3582 wl_display_bind(display, id,
3583 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003584 } else if (strcmp(interface, "text_cursor_position") == 0) {
3585 d->text_cursor_position =
3586 wl_display_bind(display, id,
3587 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003588 } else if (strcmp(interface, "workspace_manager") == 0) {
3589 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003590 }
3591}
3592
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003593#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003594static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003595init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003596{
3597 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003598 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003599
Rob Clark6396ed32012-03-11 19:48:41 -05003600#ifdef USE_CAIRO_GLESV2
3601# define GL_BIT EGL_OPENGL_ES2_BIT
3602#else
3603# define GL_BIT EGL_OPENGL_BIT
3604#endif
3605
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003606 static const EGLint argb_cfg_attribs[] = {
3607 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003608 EGL_RED_SIZE, 1,
3609 EGL_GREEN_SIZE, 1,
3610 EGL_BLUE_SIZE, 1,
3611 EGL_ALPHA_SIZE, 1,
3612 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003613 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003614 EGL_NONE
3615 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003616
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003617#ifdef USE_CAIRO_GLESV2
3618 static const EGLint context_attribs[] = {
3619 EGL_CONTEXT_CLIENT_VERSION, 2,
3620 EGL_NONE
3621 };
3622 EGLint api = EGL_OPENGL_ES_API;
3623#else
3624 EGLint *context_attribs = NULL;
3625 EGLint api = EGL_OPENGL_API;
3626#endif
3627
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003628 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003629 if (!eglInitialize(d->dpy, &major, &minor)) {
3630 fprintf(stderr, "failed to initialize display\n");
3631 return -1;
3632 }
3633
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003634 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003635 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3636 return -1;
3637 }
3638
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003639 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3640 &d->argb_config, 1, &n) || n != 1) {
3641 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003642 return -1;
3643 }
3644
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003645 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003646 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003647 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003648 fprintf(stderr, "failed to create context\n");
3649 return -1;
3650 }
3651
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003652 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003653 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003654 return -1;
3655 }
3656
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003657#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003658 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3659 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3660 fprintf(stderr, "failed to get cairo egl argb device\n");
3661 return -1;
3662 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003663#endif
3664
3665 return 0;
3666}
3667
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003668static void
3669fini_egl(struct display *display)
3670{
3671#ifdef HAVE_CAIRO_EGL
3672 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003673#endif
3674
3675 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3676 EGL_NO_CONTEXT);
3677
3678 eglTerminate(display->dpy);
3679 eglReleaseThread();
3680}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003681#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003682
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003683static int
3684event_mask_update(uint32_t mask, void *data)
3685{
3686 struct display *d = data;
3687
3688 d->mask = mask;
3689
3690 return 0;
3691}
3692
3693static void
3694handle_display_data(struct task *task, uint32_t events)
3695{
3696 struct display *display =
3697 container_of(task, struct display, display_task);
3698
3699 wl_display_iterate(display->display, display->mask);
3700}
3701
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003702struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003703display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003704{
3705 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003706
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003707 d = malloc(sizeof *d);
3708 if (d == NULL)
3709 return NULL;
3710
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003711 memset(d, 0, sizeof *d);
3712
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003713 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003714 if (d->display == NULL) {
3715 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003716 return NULL;
3717 }
3718
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003719 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003720 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3721 d->display_task.run = handle_display_data;
3722 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3723
3724 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003725 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003726 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003727
Daniel Stone97f68542012-05-30 16:32:01 +01003728 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003729 if (d->xkb_context == NULL) {
3730 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003731 return NULL;
3732 }
3733
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003734 /* Set up listener so we'll catch all events. */
3735 wl_display_add_global_listener(d->display,
3736 display_handle_global, d);
3737
3738 /* Process connection events. */
3739 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003740#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003741 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003742 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003743#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003744
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003745 d->image_target_texture_2d =
3746 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3747 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3748 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3749
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003750 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003751
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003752 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003753
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003754 wl_list_init(&d->window_list);
3755
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003756 d->workspace = 0;
3757 d->workspace_count = 1;
3758
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003759 return d;
3760}
3761
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003762static void
3763display_destroy_outputs(struct display *display)
3764{
3765 struct output *tmp;
3766 struct output *output;
3767
3768 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3769 output_destroy(output);
3770}
3771
Pekka Paalanene1207c72011-12-16 12:02:09 +02003772static void
3773display_destroy_inputs(struct display *display)
3774{
3775 struct input *tmp;
3776 struct input *input;
3777
3778 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3779 input_destroy(input);
3780}
3781
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003782void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003783display_destroy(struct display *display)
3784{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003785 if (!wl_list_empty(&display->window_list))
3786 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3787
3788 if (!wl_list_empty(&display->deferred_list))
3789 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3790
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003791 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003792 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003793
Daniel Stone97f68542012-05-30 16:32:01 +01003794 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003795
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003796 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003797 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003798
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003799#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003800 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003801#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003802
Pekka Paalanenc2052982011-12-16 11:41:32 +02003803 if (display->shell)
3804 wl_shell_destroy(display->shell);
3805
3806 if (display->shm)
3807 wl_shm_destroy(display->shm);
3808
3809 if (display->data_device_manager)
3810 wl_data_device_manager_destroy(display->data_device_manager);
3811
3812 wl_compositor_destroy(display->compositor);
3813
3814 close(display->epoll_fd);
3815
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003816 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003817 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003818 free(display);
3819}
3820
3821void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003822display_set_user_data(struct display *display, void *data)
3823{
3824 display->user_data = data;
3825}
3826
3827void *
3828display_get_user_data(struct display *display)
3829{
3830 return display->user_data;
3831}
3832
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003833struct wl_display *
3834display_get_display(struct display *display)
3835{
3836 return display->display;
3837}
3838
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003839struct output *
3840display_get_output(struct display *display)
3841{
3842 return container_of(display->output_list.next, struct output, link);
3843}
3844
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003845struct wl_compositor *
3846display_get_compositor(struct display *display)
3847{
3848 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003849}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003850
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003851uint32_t
3852display_get_serial(struct display *display)
3853{
3854 return display->serial;
3855}
3856
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003857EGLDisplay
3858display_get_egl_display(struct display *d)
3859{
3860 return d->dpy;
3861}
3862
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003863struct wl_data_source *
3864display_create_data_source(struct display *display)
3865{
3866 return wl_data_device_manager_create_data_source(display->data_device_manager);
3867}
3868
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003869EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003870display_get_argb_egl_config(struct display *d)
3871{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003872 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003873}
3874
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003875struct wl_shell *
3876display_get_shell(struct display *display)
3877{
3878 return display->shell;
3879}
3880
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003881int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003882display_acquire_window_surface(struct display *display,
3883 struct window *window,
3884 EGLContext ctx)
3885{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003886#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003887 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003888 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003889
3890 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003891 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003892 device = cairo_surface_get_device(window->cairo_surface);
3893 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003894 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003895
Benjamin Franzke0c991632011-09-27 21:57:31 +02003896 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003897 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003898 ctx = display->argb_ctx;
3899 else
3900 assert(0);
3901 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003902
3903 data = cairo_surface_get_user_data(window->cairo_surface,
3904 &surface_data_key);
3905
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003906 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003907 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003908 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3909 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003910
3911 return 0;
3912#else
3913 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003914#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003915}
3916
3917void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003918display_release_window_surface(struct display *display,
3919 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003920{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003921#ifdef HAVE_CAIRO_EGL
3922 cairo_device_t *device;
3923
3924 device = cairo_surface_get_device(window->cairo_surface);
3925 if (!device)
3926 return;
3927
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003928 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003929 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003930 cairo_device_release(device);
3931#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003932}
3933
3934void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003935display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003936{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003937 wl_list_insert(&display->deferred_list, &task->link);
3938}
3939
3940void
3941display_watch_fd(struct display *display,
3942 int fd, uint32_t events, struct task *task)
3943{
3944 struct epoll_event ep;
3945
3946 ep.events = events;
3947 ep.data.ptr = task;
3948 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3949}
3950
3951void
3952display_run(struct display *display)
3953{
3954 struct task *task;
3955 struct epoll_event ep[16];
3956 int i, count;
3957
Pekka Paalanen826d7952011-12-15 10:14:07 +02003958 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003959 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003960 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003961
3962 while (!wl_list_empty(&display->deferred_list)) {
3963 task = container_of(display->deferred_list.next,
3964 struct task, link);
3965 wl_list_remove(&task->link);
3966 task->run(task, 0);
3967 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003968
3969 if (!display->running)
3970 break;
3971
3972 wl_display_flush(display->display);
3973
3974 count = epoll_wait(display->epoll_fd,
3975 ep, ARRAY_LENGTH(ep), -1);
3976 for (i = 0; i < count; i++) {
3977 task = ep[i].data.ptr;
3978 task->run(task, ep[i].events);
3979 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003980 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003981}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003982
3983void
3984display_exit(struct display *display)
3985{
3986 display->running = 0;
3987}