blob: 472aabfdeb6e22118aa9e5bbc83dee1e3a078fd9 [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 Paalanen4dde2fc2012-01-19 13:33:50 +0200168 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500169 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500170
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500171 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400172 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500173};
174
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500175struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500176 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300177 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500178 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 struct wl_list link;
180 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500181 widget_resize_handler_t resize_handler;
182 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500183 widget_enter_handler_t enter_handler;
184 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500185 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500186 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200187 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400188 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500189 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300190 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400191};
192
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193struct input {
194 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100195 struct wl_seat *seat;
196 struct wl_pointer *pointer;
197 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct window *pointer_focus;
199 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300200 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300201 uint32_t cursor_anim_start;
202 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300203 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400204 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400205 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400206 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400207 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400208 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400209
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500210 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500211 struct widget *grab;
212 uint32_t grab_button;
213
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400214 struct wl_data_device *data_device;
215 struct data_offer *drag_offer;
216 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100217
218 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100219 struct xkb_keymap *keymap;
220 struct xkb_state *state;
221 xkb_mod_mask_t control_mask;
222 xkb_mod_mask_t alt_mask;
223 xkb_mod_mask_t shift_mask;
224 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400225
226 struct task repeat_task;
227 int repeat_timer_fd;
228 uint32_t repeat_sym;
229 uint32_t repeat_key;
230 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400231};
232
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500233struct output {
234 struct display *display;
235 struct wl_output *output;
236 struct rectangle allocation;
237 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200238
239 display_output_handler_t destroy_handler;
240 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500241};
242
Martin Minarik1998b152012-05-10 02:04:35 +0200243enum frame_button_action {
244 FRAME_BUTTON_NULL = 0,
245 FRAME_BUTTON_ICON = 1,
246 FRAME_BUTTON_CLOSE = 2,
247 FRAME_BUTTON_MINIMIZE = 3,
248 FRAME_BUTTON_MAXIMIZE = 4,
249};
250
251enum frame_button_pointer {
252 FRAME_BUTTON_DEFAULT = 0,
253 FRAME_BUTTON_OVER = 1,
254 FRAME_BUTTON_ACTIVE = 2,
255};
256
257enum frame_button_align {
258 FRAME_BUTTON_RIGHT = 0,
259 FRAME_BUTTON_LEFT = 1,
260};
261
262enum frame_button_decoration {
263 FRAME_BUTTON_NONE = 0,
264 FRAME_BUTTON_FANCY = 1,
265};
266
267struct frame_button {
268 struct widget *widget;
269 struct frame *frame;
270 cairo_surface_t *icon;
271 enum frame_button_action type;
272 enum frame_button_pointer state;
273 struct wl_list link; /* buttons_list */
274 enum frame_button_align align;
275 enum frame_button_decoration decoration;
276};
277
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500278struct frame {
279 struct widget *widget;
280 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200281 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500282};
283
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500284struct menu {
285 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500286 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500287 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500288 const char **entries;
289 uint32_t time;
290 int current;
291 int count;
292 menu_func_t func;
293};
294
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300295struct tooltip {
296 struct widget *parent;
297 struct window *window;
298 struct widget *widget;
299 char *entry;
300 struct task tooltip_task;
301 int tooltip_fd;
302 float x, y;
303};
304
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700305struct shm_pool {
306 struct wl_shm_pool *pool;
307 size_t size;
308 size_t used;
309 void *data;
310};
311
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400312enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300313 CURSOR_DEFAULT = 100,
314 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400315};
316
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500317enum window_location {
318 WINDOW_INTERIOR = 0,
319 WINDOW_RESIZING_TOP = 1,
320 WINDOW_RESIZING_BOTTOM = 2,
321 WINDOW_RESIZING_LEFT = 4,
322 WINDOW_RESIZING_TOP_LEFT = 5,
323 WINDOW_RESIZING_BOTTOM_LEFT = 6,
324 WINDOW_RESIZING_RIGHT = 8,
325 WINDOW_RESIZING_TOP_RIGHT = 9,
326 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
327 WINDOW_RESIZING_MASK = 15,
328 WINDOW_EXTERIOR = 16,
329 WINDOW_TITLEBAR = 17,
330 WINDOW_CLIENT_AREA = 18,
331};
332
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333static const cairo_user_data_key_t surface_data_key;
334struct surface_data {
335 struct wl_buffer *buffer;
336};
337
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500338#define MULT(_d,c,a,t) \
339 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
340
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500341#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100343struct egl_window_surface_data {
344 struct display *display;
345 struct wl_surface *surface;
346 struct wl_egl_window *window;
347 EGLSurface surf;
348};
349
350static void
351egl_window_surface_data_destroy(void *p)
352{
353 struct egl_window_surface_data *data = p;
354 struct display *d = data->display;
355
356 eglDestroySurface(d->dpy, data->surf);
357 wl_egl_window_destroy(data->window);
358 data->surface = NULL;
359
360 free(p);
361}
362
363static cairo_surface_t *
364display_create_egl_window_surface(struct display *display,
365 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100367 struct rectangle *rectangle)
368{
369 cairo_surface_t *cairo_surface;
370 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373
374 data = malloc(sizeof *data);
375 if (data == NULL)
376 return NULL;
377
378 data->display = display;
379 data->surface = surface;
380
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500381 config = display->argb_config;
382 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400383
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400384 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500389 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100392 data->surf,
393 rectangle->width,
394 rectangle->height);
395
396 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
397 data, egl_window_surface_data_destroy);
398
399 return cairo_surface;
400}
401
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402#endif
403
404struct wl_buffer *
405display_get_buffer_for_surface(struct display *display,
406 cairo_surface_t *surface)
407{
408 struct surface_data *data;
409
410 data = cairo_surface_get_user_data (surface, &surface_data_key);
411
412 return data->buffer;
413}
414
415struct shm_surface_data {
416 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418};
419
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500420static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700421shm_pool_destroy(struct shm_pool *pool);
422
423static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424shm_surface_data_destroy(void *p)
425{
426 struct shm_surface_data *data = p;
427
428 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700429 if (data->pool)
430 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300431
432 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433}
434
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435static struct wl_shm_pool *
436make_shm_pool(struct display *display, int size, void **data)
437{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 struct wl_shm_pool *pool;
439 int fd;
440
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300441 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300443 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
444 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445 return NULL;
446 }
447
448 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400449 if (*data == MAP_FAILED) {
450 fprintf(stderr, "mmap failed: %m\n");
451 close(fd);
452 return NULL;
453 }
454
455 pool = wl_shm_create_pool(display->shm, fd, size);
456
457 close(fd);
458
459 return pool;
460}
461
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700462static struct shm_pool *
463shm_pool_create(struct display *display, size_t size)
464{
465 struct shm_pool *pool = malloc(sizeof *pool);
466
467 if (!pool)
468 return NULL;
469
470 pool->pool = make_shm_pool(display, size, &pool->data);
471 if (!pool->pool) {
472 free(pool);
473 return NULL;
474 }
475
476 pool->size = size;
477 pool->used = 0;
478
479 return pool;
480}
481
482static void *
483shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
484{
485 if (pool->used + size > pool->size)
486 return NULL;
487
488 *offset = pool->used;
489 pool->used += size;
490
491 return (char *) pool->data + *offset;
492}
493
494/* destroy the pool. this does not unmap the memory though */
495static void
496shm_pool_destroy(struct shm_pool *pool)
497{
498 munmap(pool->data, pool->size);
499 wl_shm_pool_destroy(pool->pool);
500 free(pool);
501}
502
503/* Start allocating from the beginning of the pool again */
504static void
505shm_pool_reset(struct shm_pool *pool)
506{
507 pool->used = 0;
508}
509
510static int
511data_length_for_shm_surface(struct rectangle *rect)
512{
513 int stride;
514
515 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
516 rect->width);
517 return stride * rect->height;
518}
519
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500520static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521display_create_shm_surface_from_pool(struct display *display,
522 struct rectangle *rectangle,
523 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524{
525 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400526 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400527 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700528 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400529 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530
531 data = malloc(sizeof *data);
532 if (data == NULL)
533 return NULL;
534
535 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
536 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700537 length = stride * rectangle->height;
538 data->pool = NULL;
539 map = shm_pool_allocate(pool, length, &offset);
540
541 if (!map) {
542 free(data);
543 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400544 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400545
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547 CAIRO_FORMAT_ARGB32,
548 rectangle->width,
549 rectangle->height,
550 stride);
551
552 cairo_surface_set_user_data (surface, &surface_data_key,
553 data, shm_surface_data_destroy);
554
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500556 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700560 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400561 rectangle->width,
562 rectangle->height,
563 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700565 return surface;
566}
567
568static cairo_surface_t *
569display_create_shm_surface(struct display *display,
570 struct rectangle *rectangle, uint32_t flags,
571 struct window *window)
572{
573 struct shm_surface_data *data;
574 struct shm_pool *pool;
575 cairo_surface_t *surface;
576
577 if (window && window->pool) {
578 shm_pool_reset(window->pool);
579 surface = display_create_shm_surface_from_pool(display,
580 rectangle,
581 flags,
582 window->pool);
583 if (surface)
584 return surface;
585 }
586
587 pool = shm_pool_create(display,
588 data_length_for_shm_surface(rectangle));
589 if (!pool)
590 return NULL;
591
592 surface =
593 display_create_shm_surface_from_pool(display, rectangle,
594 flags, pool);
595
596 if (!surface) {
597 shm_pool_destroy(pool);
598 return NULL;
599 }
600
601 /* make sure we destroy the pool when the surface is destroyed */
602 data = cairo_surface_get_user_data(surface, &surface_data_key);
603 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604
605 return surface;
606}
607
nobled7b87cb02011-02-01 18:51:47 +0000608static int
609check_size(struct rectangle *rect)
610{
611 if (rect->width && rect->height)
612 return 0;
613
614 fprintf(stderr, "tried to create surface of "
615 "width: %d, height: %d\n", rect->width, rect->height);
616 return -1;
617}
618
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619cairo_surface_t *
620display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100621 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400622 struct rectangle *rectangle,
623 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624{
nobled7b87cb02011-02-01 18:51:47 +0000625 if (check_size(rectangle) < 0)
626 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500627#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300628 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400629 return display_create_egl_window_surface(display,
630 surface,
631 flags,
632 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400634 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635}
636
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200637/*
638 * The following correspondences between file names and cursors was copied
639 * from: https://bugs.kde.org/attachment.cgi?id=67313
640 */
641
642static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300643 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200644 "sw-resize"
645};
646
647static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300648 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200649 "se-resize"
650};
651
652static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200654 "s-resize"
655};
656
657static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300658 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200659 "closedhand",
660 "208530c400c041818281048008011002"
661};
662
663static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300664 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200665 "default",
666 "top_left_arrow",
667 "left-arrow"
668};
669
670static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300671 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200672 "w-resize"
673};
674
675static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200677 "e-resize"
678};
679
680static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300681 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200682 "nw-resize"
683};
684
685static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300686 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200687 "ne-resize"
688};
689
690static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300691 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200692 "n-resize"
693};
694
695static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300696 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200697 "ibeam",
698 "text"
699};
700
701static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300702 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200703 "pointer",
704 "pointing_hand",
705 "e29285e634086352946a0e7090d73106"
706};
707
708static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400709 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200710 "wait",
711 "0426c94ea35c87780ff01dc239897213"
712};
713
714struct cursor_alternatives {
715 const char **names;
716 size_t count;
717};
718
719static const struct cursor_alternatives cursors[] = {
720 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
721 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
722 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
723 {grabbings, ARRAY_LENGTH(grabbings)},
724 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
725 {left_sides, ARRAY_LENGTH(left_sides)},
726 {right_sides, ARRAY_LENGTH(right_sides)},
727 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
728 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
729 {top_sides, ARRAY_LENGTH(top_sides)},
730 {xterms, ARRAY_LENGTH(xterms)},
731 {hand1s, ARRAY_LENGTH(hand1s)},
732 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300733};
734
735static void
736create_cursors(struct display *display)
737{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100738 char *config_file;
739 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200740 unsigned int i, j;
741 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100742 struct config_key shell_keys[] = {
743 { "cursor-theme", CONFIG_KEY_STRING, &theme },
744 };
745 struct config_section cs[] = {
746 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
747 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300748
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100749 config_file = config_file_path("weston.ini");
750 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
751 free(config_file);
752
753 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300754 display->cursors =
755 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
756
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300757 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200758 cursor = NULL;
759 for (j = 0; !cursor && j < cursors[i].count; ++j)
760 cursor = wl_cursor_theme_get_cursor(
761 display->cursor_theme, cursors[i].names[j]);
762
763 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300764 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200765 cursors[i].names[0]);
766
767 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300768 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300769}
770
771static void
772destroy_cursors(struct display *display)
773{
774 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800775 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300776}
777
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300778struct wl_cursor_image *
779display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400780{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200781 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400782
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300783 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400784}
785
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400786static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787window_get_resize_dx_dy(struct window *window, int *x, int *y)
788{
789 if (window->resize_edges & WINDOW_RESIZING_LEFT)
790 *x = window->server_allocation.width - window->allocation.width;
791 else
792 *x = 0;
793
794 if (window->resize_edges & WINDOW_RESIZING_TOP)
795 *y = window->server_allocation.height -
796 window->allocation.height;
797 else
798 *y = 0;
799
800 window->resize_edges = 0;
801}
802
803static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500804window_attach_surface(struct window *window)
805{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400806 struct display *display = window->display;
807 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000808#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500811 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400813 if (window->type == TYPE_NONE) {
814 window->type = TYPE_TOPLEVEL;
815 if (display->shell)
816 wl_shell_surface_set_toplevel(window->shell_surface);
817 }
818
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
822 data = cairo_surface_get_user_data(window->cairo_surface,
823 &surface_data_key);
824
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 cairo_gl_surface_swapbuffers(window->cairo_surface);
826 wl_egl_window_get_attached_size(data->window,
827 &window->server_allocation.width,
828 &window->server_allocation.height);
829 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832 buffer =
833 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400834 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200836 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400838 wl_surface_damage(window->surface, 0, 0,
839 window->allocation.width,
840 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400842 cairo_surface_destroy(window->cairo_surface);
843 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000845 default:
846 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500848
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500849 if (window->input_region) {
850 wl_surface_set_input_region(window->surface,
851 window->input_region);
852 wl_region_destroy(window->input_region);
853 window->input_region = NULL;
854 }
855
856 if (window->opaque_region) {
857 wl_surface_set_opaque_region(window->surface,
858 window->opaque_region);
859 wl_region_destroy(window->opaque_region);
860 window->opaque_region = NULL;
861 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500862}
863
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400864int
865window_has_focus(struct window *window)
866{
867 return window->focus_count > 0;
868}
869
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500870void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400871window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500872{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400873 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100874 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500875}
876
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400877void
878window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500880 cairo_surface_reference(surface);
881
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882 if (window->cairo_surface != NULL)
883 cairo_surface_destroy(window->cairo_surface);
884
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500885 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886}
887
Benjamin Franzke22d54812011-07-16 19:50:32 +0000888#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889static void
890window_resize_cairo_window_surface(struct window *window)
891{
892 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200893 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894
895 data = cairo_surface_get_user_data(window->cairo_surface,
896 &surface_data_key);
897
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200898 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899 wl_egl_window_resize(data->window,
900 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200901 window->allocation.height,
902 x,y);
903
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 cairo_gl_surface_set_size(window->cairo_surface,
905 window->allocation.width,
906 window->allocation.height);
907}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000908#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400910struct display *
911window_get_display(struct window *window)
912{
913 return window->display;
914}
915
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400916void
917window_create_surface(struct window *window)
918{
919 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400920 uint32_t flags = 0;
921
922 if (!window->transparent)
923 flags = SURFACE_OPAQUE;
924
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400925 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500926#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100927 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
928 if (window->cairo_surface) {
929 window_resize_cairo_window_surface(window);
930 return;
931 }
932 surface = display_create_surface(window->display,
933 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400934 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100935 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936#endif
937 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400938 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400939 &window->allocation,
940 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800942 default:
943 surface = NULL;
944 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400946
947 window_set_surface(window, surface);
948 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949}
950
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200951static void frame_destroy(struct frame *frame);
952
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400953void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500954window_destroy(struct window *window)
955{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200956 struct display *display = window->display;
957 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100958 struct window_output *window_output;
959 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200960
961 if (window->redraw_scheduled)
962 wl_list_remove(&window->redraw_task.link);
963
964 wl_list_for_each(input, &display->input_list, link) {
965 if (input->pointer_focus == window)
966 input->pointer_focus = NULL;
967 if (input->keyboard_focus == window)
968 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500969 if (input->focus_widget &&
970 input->focus_widget->window == window)
971 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200972 }
973
Rob Bradford7507b572012-05-15 17:55:34 +0100974 wl_list_for_each_safe(window_output, window_output_tmp,
975 &window->window_output_list, link) {
976 free (window_output);
977 }
978
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500979 if (window->input_region)
980 wl_region_destroy(window->input_region);
981 if (window->opaque_region)
982 wl_region_destroy(window->opaque_region);
983
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200984 if (window->frame)
985 frame_destroy(window->frame);
986
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200987 if (window->shell_surface)
988 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500989 wl_surface_destroy(window->surface);
990 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200991
992 if (window->cairo_surface != NULL)
993 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200994
995 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500996 free(window);
997}
998
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001000widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001002 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004 wl_list_for_each(child, &widget->child_list, link) {
1005 target = widget_find_widget(child, x, y);
1006 if (target)
1007 return target;
1008 }
1009
1010 if (widget->allocation.x <= x &&
1011 x < widget->allocation.x + widget->allocation.width &&
1012 widget->allocation.y <= y &&
1013 y < widget->allocation.y + widget->allocation.height) {
1014 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015 }
1016
1017 return NULL;
1018}
1019
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001020static struct widget *
1021widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001022{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001024
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001025 widget = malloc(sizeof *widget);
1026 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001027 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001029 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001030 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001031 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001032 widget->tooltip = NULL;
1033 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001034
1035 return widget;
1036}
1037
1038struct widget *
1039window_add_widget(struct window *window, void *data)
1040{
1041 window->widget = widget_create(window, data);
1042 wl_list_init(&window->widget->link);
1043
1044 return window->widget;
1045}
1046
1047struct widget *
1048widget_add_widget(struct widget *parent, void *data)
1049{
1050 struct widget *widget;
1051
1052 widget = widget_create(parent->window, data);
1053 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001055 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056}
1057
1058void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001059widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001061 struct display *display = widget->window->display;
1062 struct input *input;
1063
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001064 if (widget->tooltip) {
1065 free(widget->tooltip);
1066 widget->tooltip = NULL;
1067 }
1068
Pekka Paalanene156fb62012-01-19 13:51:38 +02001069 wl_list_for_each(input, &display->input_list, link) {
1070 if (input->focus_widget == widget)
1071 input->focus_widget = NULL;
1072 }
1073
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001074 wl_list_remove(&widget->link);
1075 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076}
1077
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001078void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001079widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001081 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001082}
1083
1084void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001085widget_set_size(struct widget *widget, int32_t width, int32_t height)
1086{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001087 widget->allocation.width = width;
1088 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001089}
1090
1091void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092widget_set_allocation(struct widget *widget,
1093 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001094{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001095 widget->allocation.x = x;
1096 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001097 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098}
1099
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001100void
1101widget_set_transparent(struct widget *widget, int transparent)
1102{
1103 widget->opaque = !transparent;
1104}
1105
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001106void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001107widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001108{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001110}
1111
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001112void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001113widget_set_resize_handler(struct widget *widget,
1114 widget_resize_handler_t handler)
1115{
1116 widget->resize_handler = handler;
1117}
1118
1119void
1120widget_set_redraw_handler(struct widget *widget,
1121 widget_redraw_handler_t handler)
1122{
1123 widget->redraw_handler = handler;
1124}
1125
1126void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001127widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001128{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001129 widget->enter_handler = handler;
1130}
1131
1132void
1133widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1134{
1135 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001136}
1137
1138void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001139widget_set_motion_handler(struct widget *widget,
1140 widget_motion_handler_t handler)
1141{
1142 widget->motion_handler = handler;
1143}
1144
1145void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001146widget_set_button_handler(struct widget *widget,
1147 widget_button_handler_t handler)
1148{
1149 widget->button_handler = handler;
1150}
1151
1152void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001153widget_set_axis_handler(struct widget *widget,
1154 widget_axis_handler_t handler)
1155{
1156 widget->axis_handler = handler;
1157}
1158
1159void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001160widget_schedule_redraw(struct widget *widget)
1161{
1162 window_schedule_redraw(widget->window);
1163}
1164
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001165cairo_surface_t *
1166window_get_surface(struct window *window)
1167{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001168 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001169}
1170
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001171struct wl_surface *
1172window_get_wl_surface(struct window *window)
1173{
1174 return window->surface;
1175}
1176
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177struct wl_shell_surface *
1178window_get_wl_shell_surface(struct window *window)
1179{
1180 return window->shell_surface;
1181}
1182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001184tooltip_redraw_handler(struct widget *widget, void *data)
1185{
1186 cairo_t *cr;
1187 const int32_t r = 3;
1188 struct tooltip *tooltip = data;
1189 int32_t width, height;
1190 struct window *window = widget->window;
1191
1192 cr = cairo_create(window->cairo_surface);
1193 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1194 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1195 cairo_paint(cr);
1196
1197 width = window->allocation.width;
1198 height = window->allocation.height;
1199 rounded_rect(cr, 0, 0, width, height, r);
1200
1201 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1202 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1203 cairo_fill(cr);
1204
1205 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1206 cairo_move_to(cr, 10, 16);
1207 cairo_show_text(cr, tooltip->entry);
1208 cairo_destroy(cr);
1209}
1210
1211static cairo_text_extents_t
1212get_text_extents(struct tooltip *tooltip)
1213{
1214 struct window *window;
1215 cairo_t *cr;
1216 cairo_text_extents_t extents;
1217
1218 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1219 * created yet */
1220 window = tooltip->widget->window->parent;
1221 cr = cairo_create(window->cairo_surface);
1222 cairo_text_extents(cr, tooltip->entry, &extents);
1223 cairo_destroy(cr);
1224
1225 return extents;
1226}
1227
1228static int
1229window_create_tooltip(struct tooltip *tooltip)
1230{
1231 struct widget *parent = tooltip->parent;
1232 struct display *display = parent->window->display;
1233 struct window *window;
1234 const int offset_y = 27;
1235 const int margin = 3;
1236 cairo_text_extents_t extents;
1237
1238 if (tooltip->widget)
1239 return 0;
1240
1241 window = window_create_transient(display, parent->window, tooltip->x,
1242 tooltip->y + offset_y,
1243 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1244 if (!window)
1245 return -1;
1246
1247 tooltip->window = window;
1248 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1249
1250 extents = get_text_extents(tooltip);
1251 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1252 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1253
1254 return 0;
1255}
1256
1257void
1258widget_destroy_tooltip(struct widget *parent)
1259{
1260 struct tooltip *tooltip = parent->tooltip;
1261
1262 parent->tooltip_count = 0;
1263 if (!tooltip)
1264 return;
1265
1266 if (tooltip->widget) {
1267 widget_destroy(tooltip->widget);
1268 window_destroy(tooltip->window);
1269 tooltip->widget = NULL;
1270 tooltip->window = NULL;
1271 }
1272
1273 close(tooltip->tooltip_fd);
1274 free(tooltip->entry);
1275 free(tooltip);
1276 parent->tooltip = NULL;
1277}
1278
1279static void
1280tooltip_func(struct task *task, uint32_t events)
1281{
1282 struct tooltip *tooltip =
1283 container_of(task, struct tooltip, tooltip_task);
1284 uint64_t exp;
1285
Martin Olsson8df662a2012-07-08 03:03:47 +02001286 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1287 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001288 window_create_tooltip(tooltip);
1289}
1290
1291#define TOOLTIP_TIMEOUT 500
1292static int
1293tooltip_timer_reset(struct tooltip *tooltip)
1294{
1295 struct itimerspec its;
1296
1297 its.it_interval.tv_sec = 0;
1298 its.it_interval.tv_nsec = 0;
1299 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1300 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1301 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1302 fprintf(stderr, "could not set timerfd\n: %m");
1303 return -1;
1304 }
1305
1306 return 0;
1307}
1308
1309int
1310widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1311{
1312 struct tooltip *tooltip = parent->tooltip;
1313
1314 parent->tooltip_count++;
1315 if (tooltip) {
1316 tooltip->x = x;
1317 tooltip->y = y;
1318 tooltip_timer_reset(tooltip);
1319 return 0;
1320 }
1321
1322 /* the handler might be triggered too fast via input device motion, so
1323 * we need this check here to make sure tooltip is fully initialized */
1324 if (parent->tooltip_count > 1)
1325 return 0;
1326
1327 tooltip = malloc(sizeof *tooltip);
1328 if (!tooltip)
1329 return -1;
1330
1331 parent->tooltip = tooltip;
1332 tooltip->parent = parent;
1333 tooltip->widget = NULL;
1334 tooltip->window = NULL;
1335 tooltip->x = x;
1336 tooltip->y = y;
1337 tooltip->entry = strdup(entry);
1338 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1339 if (tooltip->tooltip_fd < 0) {
1340 fprintf(stderr, "could not create timerfd\n: %m");
1341 return -1;
1342 }
1343
1344 tooltip->tooltip_task.run = tooltip_func;
1345 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1346 EPOLLIN, &tooltip->tooltip_task);
1347 tooltip_timer_reset(tooltip);
1348
1349 return 0;
1350}
1351
1352static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001353workspace_manager_state(void *data,
1354 struct workspace_manager *workspace_manager,
1355 uint32_t current,
1356 uint32_t count)
1357{
1358 struct display *display = data;
1359
1360 display->workspace = current;
1361 display->workspace_count = count;
1362}
1363
1364static const struct workspace_manager_listener workspace_manager_listener = {
1365 workspace_manager_state
1366};
1367
1368static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001369frame_resize_handler(struct widget *widget,
1370 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001371{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001372 struct frame *frame = data;
1373 struct widget *child = frame->child;
1374 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001375 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001376 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001377 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001378 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001379 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001380 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001381
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001382 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001383 decoration_width = (t->width + t->margin) * 2;
1384 decoration_height = t->width +
1385 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001387 allocation.x = t->width + t->margin;
1388 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001389 allocation.width = width - decoration_width;
1390 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001391
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001392 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001393
1394 wl_list_for_each(button, &frame->buttons_list, link)
1395 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001396 } else {
1397 decoration_width = 0;
1398 decoration_height = 0;
1399
1400 allocation.x = 0;
1401 allocation.y = 0;
1402 allocation.width = width;
1403 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001404 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001405
1406 wl_list_for_each(button, &frame->buttons_list, link)
1407 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001408 }
1409
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001410 widget_set_allocation(child, allocation.x, allocation.y,
1411 allocation.width, allocation.height);
1412
1413 if (child->resize_handler)
1414 child->resize_handler(child,
1415 allocation.width,
1416 allocation.height,
1417 child->user_data);
1418
Scott Moreauf7e498c2012-05-14 11:39:29 -06001419 width = child->allocation.width + decoration_width;
1420 height = child->allocation.height + decoration_height;
1421
Kristian Høgsberg023be102012-07-31 11:59:12 -04001422 if (widget->window->type != TYPE_FULLSCREEN) {
1423 widget->window->input_region =
1424 wl_compositor_create_region(display->compositor);
1425 wl_region_add(widget->window->input_region,
1426 t->margin, t->margin,
1427 width - 2 * t->margin,
1428 height - 2 * t->margin);
1429 }
1430
Scott Moreauf7e498c2012-05-14 11:39:29 -06001431 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001432
1433 if (child->opaque) {
1434 widget->window->opaque_region =
1435 wl_compositor_create_region(display->compositor);
1436 wl_region_add(widget->window->opaque_region,
1437 opaque_margin, opaque_margin,
1438 widget->allocation.width - 2 * opaque_margin,
1439 widget->allocation.height - 2 * opaque_margin);
1440 }
Martin Minarik1998b152012-05-10 02:04:35 +02001441
1442 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001443 x_r = frame->widget->allocation.width - t->width - t->margin;
1444 x_l = t->width + t->margin;
1445 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001446 wl_list_for_each(button, &frame->buttons_list, link) {
1447 const int button_padding = 4;
1448 w = cairo_image_surface_get_width(button->icon);
1449 h = cairo_image_surface_get_height(button->icon);
1450
1451 if (button->decoration == FRAME_BUTTON_FANCY)
1452 w += 10;
1453
1454 if (button->align == FRAME_BUTTON_LEFT) {
1455 widget_set_allocation(button->widget,
1456 x_l, y , w + 1, h + 1);
1457 x_l += w;
1458 x_l += button_padding;
1459 } else {
1460 x_r -= w;
1461 widget_set_allocation(button->widget,
1462 x_r, y , w + 1, h + 1);
1463 x_r -= button_padding;
1464 }
1465 }
1466}
1467
1468static int
1469frame_button_enter_handler(struct widget *widget,
1470 struct input *input, float x, float y, void *data)
1471{
1472 struct frame_button *frame_button = data;
1473
1474 widget_schedule_redraw(frame_button->widget);
1475 frame_button->state = FRAME_BUTTON_OVER;
1476
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001477 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001478}
1479
1480static void
1481frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1482{
1483 struct frame_button *frame_button = data;
1484
1485 widget_schedule_redraw(frame_button->widget);
1486 frame_button->state = FRAME_BUTTON_DEFAULT;
1487}
1488
1489static void
1490frame_button_button_handler(struct widget *widget,
1491 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001492 uint32_t button,
1493 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001494{
1495 struct frame_button *frame_button = data;
1496 struct window *window = widget->window;
1497
1498 if (button != BTN_LEFT)
1499 return;
1500
1501 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001503 frame_button->state = FRAME_BUTTON_ACTIVE;
1504 widget_schedule_redraw(frame_button->widget);
1505
1506 if (frame_button->type == FRAME_BUTTON_ICON)
1507 window_show_frame_menu(window, input, time);
1508 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001509 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001510 frame_button->state = FRAME_BUTTON_DEFAULT;
1511 widget_schedule_redraw(frame_button->widget);
1512 break;
1513 }
1514
1515 switch (frame_button->type) {
1516 case FRAME_BUTTON_CLOSE:
1517 if (window->close_handler)
1518 window->close_handler(window->parent,
1519 window->user_data);
1520 else
1521 display_exit(window->display);
1522 break;
1523 case FRAME_BUTTON_MINIMIZE:
1524 fprintf(stderr,"Minimize stub\n");
1525 break;
1526 case FRAME_BUTTON_MAXIMIZE:
1527 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1528 break;
1529 default:
1530 /* Unknown operation */
1531 break;
1532 }
1533}
1534
1535static void
1536frame_button_redraw_handler(struct widget *widget, void *data)
1537{
1538 struct frame_button *frame_button = data;
1539 cairo_t *cr;
1540 int width, height, x, y;
1541 struct window *window = widget->window;
1542
1543 x = widget->allocation.x;
1544 y = widget->allocation.y;
1545 width = widget->allocation.width;
1546 height = widget->allocation.height;
1547
1548 if (!width)
1549 return;
1550 if (!height)
1551 return;
1552 if (widget->opaque)
1553 return;
1554
1555 cr = cairo_create(window->cairo_surface);
1556
1557 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1558 cairo_set_line_width(cr, 1);
1559
1560 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1561 cairo_rectangle (cr, x, y, 25, 16);
1562
1563 cairo_stroke_preserve(cr);
1564
1565 switch (frame_button->state) {
1566 case FRAME_BUTTON_DEFAULT:
1567 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1568 break;
1569 case FRAME_BUTTON_OVER:
1570 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1571 break;
1572 case FRAME_BUTTON_ACTIVE:
1573 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1574 break;
1575 }
1576
1577 cairo_fill (cr);
1578
1579 x += 4;
1580 }
1581
1582 cairo_set_source_surface(cr, frame_button->icon, x, y);
1583 cairo_paint(cr);
1584
1585 cairo_destroy(cr);
1586}
1587
1588static struct widget *
1589frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1590 enum frame_button_align align, enum frame_button_decoration style)
1591{
1592 struct frame_button *frame_button;
1593 const char *icon = data;
1594
1595 frame_button = malloc (sizeof *frame_button);
1596 memset(frame_button, 0, sizeof *frame_button);
1597
1598 frame_button->icon = cairo_image_surface_create_from_png(icon);
1599 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1600 frame_button->frame = frame;
1601 frame_button->type = type;
1602 frame_button->align = align;
1603 frame_button->decoration = style;
1604
1605 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1606
1607 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1608 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1609 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1610 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1611 return frame_button->widget;
1612}
1613
1614static void
1615frame_button_destroy(struct frame_button *frame_button)
1616{
1617 widget_destroy(frame_button->widget);
1618 wl_list_remove(&frame_button->link);
1619 cairo_surface_destroy(frame_button->icon);
1620 free(frame_button);
1621
1622 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623}
1624
1625static void
1626frame_redraw_handler(struct widget *widget, void *data)
1627{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001630 struct theme *t = window->display->theme;
1631 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001632
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001633 if (window->type == TYPE_FULLSCREEN)
1634 return;
1635
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 cr = cairo_create(window->cairo_surface);
1637
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001638 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001639 flags |= THEME_FRAME_ACTIVE;
1640 theme_render_frame(t, cr, widget->allocation.width,
1641 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642
1643 cairo_destroy(cr);
1644}
1645
1646static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001648{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001649 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001650 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001651
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001652 location = theme_get_location(t, input->sx, input->sy,
1653 frame->widget->allocation.width,
1654 frame->widget->allocation.height);
1655
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001656 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001657 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001658 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001659 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001660 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001661 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001662 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001663 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001664 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001665 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001666 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001667 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001668 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001669 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001670 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001671 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001672 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001673 case THEME_LOCATION_EXTERIOR:
1674 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001675 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001676 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001677 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001678}
1679
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001680static void
1681frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001682{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001683 struct display *display;
1684
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001685 switch (index) {
1686 case 0: /* close */
1687 if (window->close_handler)
1688 window->close_handler(window->parent,
1689 window->user_data);
1690 else
1691 display_exit(window->display);
1692 break;
1693 case 1: /* fullscreen */
1694 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001695 if (window->fullscreen_handler)
1696 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001698 case 2: /* move to workspace above */
1699 display = window->display;
1700 if (display->workspace > 0)
1701 workspace_manager_move_surface(display->workspace_manager,
1702 window->surface,
1703 display->workspace - 1);
1704 break;
1705 case 3: /* move to workspace below */
1706 display = window->display;
1707 if (display->workspace < display->workspace_count)
1708 workspace_manager_move_surface(display->workspace_manager,
1709 window->surface,
1710 display->workspace + 1);
1711 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001712 }
1713}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001714
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001715void
1716window_show_frame_menu(struct window *window,
1717 struct input *input, uint32_t time)
1718{
1719 int32_t x, y;
1720
1721 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001722 "Close", "Fullscreen",
1723 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001724 };
1725
1726 input_get_position(input, &x, &y);
1727 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001728 x - 10, y - 10, frame_menu_func, entries,
1729 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001730}
1731
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001732static int
1733frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001734 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001735{
1736 return frame_get_pointer_image_for_location(data, input);
1737}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001738
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001739static int
1740frame_motion_handler(struct widget *widget,
1741 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001742 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001743{
1744 return frame_get_pointer_image_for_location(data, input);
1745}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001746
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001747static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001748frame_button_handler(struct widget *widget,
1749 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001750 uint32_t button, enum wl_pointer_button_state state,
1751 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752
1753{
1754 struct frame *frame = data;
1755 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001756 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001757 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001758
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001759 location = theme_get_location(display->theme, input->sx, input->sy,
1760 frame->widget->allocation.width,
1761 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001762
Daniel Stone4dbadb12012-05-30 16:31:51 +01001763 if (window->display->shell && button == BTN_LEFT &&
1764 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001765 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001766 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001767 if (!window->shell_surface)
1768 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001769 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001770 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001771 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001772 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001774 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001775 case THEME_LOCATION_RESIZING_TOP:
1776 case THEME_LOCATION_RESIZING_BOTTOM:
1777 case THEME_LOCATION_RESIZING_LEFT:
1778 case THEME_LOCATION_RESIZING_RIGHT:
1779 case THEME_LOCATION_RESIZING_TOP_LEFT:
1780 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1781 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1782 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001783 if (!window->shell_surface)
1784 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001785 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001786
1787 if (!display->dpy) {
1788 /* If we're using shm, allocate a big
1789 pool to create buffers out of while
1790 we resize. We should probably base
1791 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001792 window->pool =
1793 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001794 }
1795
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001797 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001798 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001799 break;
1800 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001801 } else if (button == BTN_RIGHT &&
1802 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001803 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804 }
1805}
1806
1807struct widget *
1808frame_create(struct window *window, void *data)
1809{
1810 struct frame *frame;
1811
1812 frame = malloc(sizeof *frame);
1813 memset(frame, 0, sizeof *frame);
1814
1815 frame->widget = window_add_widget(window, frame);
1816 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001817
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001818 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1819 widget_set_resize_handler(frame->widget, frame_resize_handler);
1820 widget_set_enter_handler(frame->widget, frame_enter_handler);
1821 widget_set_motion_handler(frame->widget, frame_motion_handler);
1822 widget_set_button_handler(frame->widget, frame_button_handler);
1823
Martin Minarik1998b152012-05-10 02:04:35 +02001824 /* Create empty list for frame buttons */
1825 wl_list_init(&frame->buttons_list);
1826
1827 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1828 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1829
1830 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1831 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1832
1833 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1834 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1835
1836 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1837 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1838
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001839 window->frame = frame;
1840
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001841 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001842}
1843
Kristian Høgsberga1627922012-06-20 17:30:03 -04001844void
1845frame_set_child_size(struct widget *widget, int child_width, int child_height)
1846{
1847 struct display *display = widget->window->display;
1848 struct theme *t = display->theme;
1849 int decoration_width, decoration_height;
1850 int width, height;
1851
1852 if (widget->window->type != TYPE_FULLSCREEN) {
1853 decoration_width = (t->width + t->margin) * 2;
1854 decoration_height = t->width +
1855 t->titlebar_height + t->margin * 2;
1856
1857 width = child_width + decoration_width;
1858 height = child_height + decoration_height;
1859 } else {
1860 width = child_width;
1861 height = child_height;
1862 }
1863
1864 window_schedule_resize(widget->window, width, height);
1865}
1866
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001867static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001868frame_destroy(struct frame *frame)
1869{
Martin Minarik1998b152012-05-10 02:04:35 +02001870 struct frame_button *button, *tmp;
1871
1872 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1873 frame_button_destroy(button);
1874
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001875 /* frame->child must be destroyed by the application */
1876 widget_destroy(frame->widget);
1877 free(frame);
1878}
1879
1880static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001881input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001882 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001883{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001884 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001885 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001886
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001887 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001888 return;
1889
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001890 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001891 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001892 widget = old;
1893 if (input->grab)
1894 widget = input->grab;
1895 if (widget->leave_handler)
1896 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001897 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001898 }
1899
1900 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001901 widget = focus;
1902 if (input->grab)
1903 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001904 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001905 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001906 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001907 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001908
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001909 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001910 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001911}
1912
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001913void
1914input_grab(struct input *input, struct widget *widget, uint32_t button)
1915{
1916 input->grab = widget;
1917 input->grab_button = button;
1918}
1919
1920void
1921input_ungrab(struct input *input)
1922{
1923 struct widget *widget;
1924
1925 input->grab = NULL;
1926 if (input->pointer_focus) {
1927 widget = widget_find_widget(input->pointer_focus->widget,
1928 input->sx, input->sy);
1929 input_set_focus_widget(input, widget, input->sx, input->sy);
1930 }
1931}
1932
1933static void
1934input_remove_pointer_focus(struct input *input)
1935{
1936 struct window *window = input->pointer_focus;
1937
1938 if (!window)
1939 return;
1940
1941 input_set_focus_widget(input, NULL, 0, 0);
1942
1943 input->pointer_focus = NULL;
1944 input->current_cursor = CURSOR_UNSET;
1945}
1946
1947static void
1948pointer_handle_enter(void *data, struct wl_pointer *pointer,
1949 uint32_t serial, struct wl_surface *surface,
1950 wl_fixed_t sx_w, wl_fixed_t sy_w)
1951{
1952 struct input *input = data;
1953 struct window *window;
1954 struct widget *widget;
1955 float sx = wl_fixed_to_double(sx_w);
1956 float sy = wl_fixed_to_double(sy_w);
1957
1958 if (!surface) {
1959 /* enter event for a window we've just destroyed */
1960 return;
1961 }
1962
1963 input->display->serial = serial;
1964 input->pointer_enter_serial = serial;
1965 input->pointer_focus = wl_surface_get_user_data(surface);
1966 window = input->pointer_focus;
1967
1968 if (window->pool) {
1969 shm_pool_destroy(window->pool);
1970 window->pool = NULL;
1971 /* Schedule a redraw to free the pool */
1972 window_schedule_redraw(window);
1973 }
1974
1975 input->sx = sx;
1976 input->sy = sy;
1977
1978 widget = widget_find_widget(window->widget, sx, sy);
1979 input_set_focus_widget(input, widget, sx, sy);
1980}
1981
1982static void
1983pointer_handle_leave(void *data, struct wl_pointer *pointer,
1984 uint32_t serial, struct wl_surface *surface)
1985{
1986 struct input *input = data;
1987
1988 input->display->serial = serial;
1989 input_remove_pointer_focus(input);
1990}
1991
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001992static void
Daniel Stone37816df2012-05-16 18:45:18 +01001993pointer_handle_motion(void *data, struct wl_pointer *pointer,
1994 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001995{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001996 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001997 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001998 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001999 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002000 float sx = wl_fixed_to_double(sx_w);
2001 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002002
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002003 input->sx = sx;
2004 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002005
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002006 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002007 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002008 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002009 }
2010
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002011 if (input->grab)
2012 widget = input->grab;
2013 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002014 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002015 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01002016 cursor = widget->motion_handler(input->focus_widget,
2017 input, time, sx, sy,
2018 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002019
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002020 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002021}
2022
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002023static void
Daniel Stone37816df2012-05-16 18:45:18 +01002024pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002025 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002026{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002027 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002028 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002029 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002030
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002031 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002032 if (input->focus_widget && input->grab == NULL &&
2033 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002034 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002035
Neil Roberts6b28aad2012-01-23 19:11:18 +00002036 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002038 (*widget->button_handler)(widget,
2039 input, time,
2040 button, state,
2041 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002042
Daniel Stone4dbadb12012-05-30 16:31:51 +01002043 if (input->grab && input->grab_button == button &&
2044 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002045 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002046}
2047
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002048static void
Daniel Stone37816df2012-05-16 18:45:18 +01002049pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002050 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002051{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002052 struct input *input = data;
2053 struct widget *widget;
2054
2055 widget = input->focus_widget;
2056 if (input->grab)
2057 widget = input->grab;
2058 if (widget && widget->axis_handler)
2059 (*widget->axis_handler)(widget,
2060 input, time,
2061 axis, value,
2062 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002063}
2064
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002065static const struct wl_pointer_listener pointer_listener = {
2066 pointer_handle_enter,
2067 pointer_handle_leave,
2068 pointer_handle_motion,
2069 pointer_handle_button,
2070 pointer_handle_axis,
2071};
2072
2073static void
2074input_remove_keyboard_focus(struct input *input)
2075{
2076 struct window *window = input->keyboard_focus;
2077 struct itimerspec its;
2078
2079 its.it_interval.tv_sec = 0;
2080 its.it_interval.tv_nsec = 0;
2081 its.it_value.tv_sec = 0;
2082 its.it_value.tv_nsec = 0;
2083 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2084
2085 if (!window)
2086 return;
2087
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002088 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002089 if (window->keyboard_focus_handler)
2090 (*window->keyboard_focus_handler)(window, NULL,
2091 window->user_data);
2092
2093 input->keyboard_focus = NULL;
2094}
2095
2096static void
2097keyboard_repeat_func(struct task *task, uint32_t events)
2098{
2099 struct input *input =
2100 container_of(task, struct input, repeat_task);
2101 struct window *window = input->keyboard_focus;
2102 uint64_t exp;
2103
2104 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2105 /* If we change the timer between the fd becoming
2106 * readable and getting here, there'll be nothing to
2107 * read and we get EAGAIN. */
2108 return;
2109
2110 if (window && window->key_handler) {
2111 (*window->key_handler)(window, input, input->repeat_time,
2112 input->repeat_key, input->repeat_sym,
2113 WL_KEYBOARD_KEY_STATE_PRESSED,
2114 window->user_data);
2115 }
2116}
2117
2118static void
2119keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2120 uint32_t format, int fd, uint32_t size)
2121{
2122 struct input *input = data;
2123 char *map_str;
2124
2125 if (!data) {
2126 close(fd);
2127 return;
2128 }
2129
2130 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2131 close(fd);
2132 return;
2133 }
2134
2135 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2136 if (map_str == MAP_FAILED) {
2137 close(fd);
2138 return;
2139 }
2140
2141 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2142 map_str,
2143 XKB_KEYMAP_FORMAT_TEXT_V1,
2144 0);
2145 munmap(map_str, size);
2146 close(fd);
2147
2148 if (!input->xkb.keymap) {
2149 fprintf(stderr, "failed to compile keymap\n");
2150 return;
2151 }
2152
2153 input->xkb.state = xkb_state_new(input->xkb.keymap);
2154 if (!input->xkb.state) {
2155 fprintf(stderr, "failed to create XKB state\n");
2156 xkb_map_unref(input->xkb.keymap);
2157 input->xkb.keymap = NULL;
2158 return;
2159 }
2160
2161 input->xkb.control_mask =
2162 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2163 input->xkb.alt_mask =
2164 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2165 input->xkb.shift_mask =
2166 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2167}
2168
2169static void
2170keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2171 uint32_t serial, struct wl_surface *surface,
2172 struct wl_array *keys)
2173{
2174 struct input *input = data;
2175 struct window *window;
2176
2177 input->display->serial = serial;
2178 input->keyboard_focus = wl_surface_get_user_data(surface);
2179
2180 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002181 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002182 if (window->keyboard_focus_handler)
2183 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002184 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002185}
2186
2187static void
2188keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2189 uint32_t serial, struct wl_surface *surface)
2190{
2191 struct input *input = data;
2192
2193 input->display->serial = serial;
2194 input_remove_keyboard_focus(input);
2195}
2196
Scott Moreau210d0792012-03-22 10:47:01 -06002197static void
Daniel Stone37816df2012-05-16 18:45:18 +01002198keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002199 uint32_t serial, uint32_t time, uint32_t key,
2200 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002201{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002202 struct input *input = data;
2203 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002204 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002205 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002206 const xkb_keysym_t *syms;
2207 xkb_keysym_t sym;
2208 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002209 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002210
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002211 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002212 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002213 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002214 return;
2215
Daniel Stone97f68542012-05-30 16:32:01 +01002216 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002217
Daniel Stone97f68542012-05-30 16:32:01 +01002218 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002219 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002220 XKB_STATE_LATCHED);
2221 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002222 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002223 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002224 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002225 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002226 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002227 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002228
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002229 sym = XKB_KEY_NoSymbol;
2230 if (num_syms == 1)
2231 sym = syms[0];
2232
2233 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002234 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002235 window_set_maximized(window,
2236 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002237 } else if (sym == XKB_KEY_F11 &&
2238 window->fullscreen_handler &&
2239 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2240 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002241 } else if (sym == XKB_KEY_F4 &&
2242 input->modifiers == MOD_ALT_MASK &&
2243 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2244 if (window->close_handler)
2245 window->close_handler(window->parent,
2246 window->user_data);
2247 else
2248 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002249 } else if (window->key_handler) {
2250 (*window->key_handler)(window, input, time, key,
2251 sym, state, window->user_data);
2252 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002253
2254 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2255 key == input->repeat_key) {
2256 its.it_interval.tv_sec = 0;
2257 its.it_interval.tv_nsec = 0;
2258 its.it_value.tv_sec = 0;
2259 its.it_value.tv_nsec = 0;
2260 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2261 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2262 input->repeat_sym = sym;
2263 input->repeat_key = key;
2264 input->repeat_time = time;
2265 its.it_interval.tv_sec = 0;
2266 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2267 its.it_value.tv_sec = 0;
2268 its.it_value.tv_nsec = 400 * 1000 * 1000;
2269 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2270 }
2271}
2272
2273static void
Daniel Stone351eb612012-05-31 15:27:47 -04002274keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2275 uint32_t serial, uint32_t mods_depressed,
2276 uint32_t mods_latched, uint32_t mods_locked,
2277 uint32_t group)
2278{
2279 struct input *input = data;
2280
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002281 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2282 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002283}
2284
Daniel Stone37816df2012-05-16 18:45:18 +01002285static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002286 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002287 keyboard_handle_enter,
2288 keyboard_handle_leave,
2289 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002290 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002291};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002292
2293static void
Daniel Stone37816df2012-05-16 18:45:18 +01002294seat_handle_capabilities(void *data, struct wl_seat *seat,
2295 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002296{
Daniel Stone37816df2012-05-16 18:45:18 +01002297 struct input *input = data;
2298
2299 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2300 input->pointer = wl_seat_get_pointer(seat);
2301 wl_pointer_set_user_data(input->pointer, input);
2302 wl_pointer_add_listener(input->pointer, &pointer_listener,
2303 input);
2304 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2305 wl_pointer_destroy(input->pointer);
2306 input->pointer = NULL;
2307 }
2308
2309 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2310 input->keyboard = wl_seat_get_keyboard(seat);
2311 wl_keyboard_set_user_data(input->keyboard, input);
2312 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2313 input);
2314 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2315 wl_keyboard_destroy(input->keyboard);
2316 input->keyboard = NULL;
2317 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002318}
2319
Daniel Stone37816df2012-05-16 18:45:18 +01002320static const struct wl_seat_listener seat_listener = {
2321 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002322};
2323
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002324void
2325input_get_position(struct input *input, int32_t *x, int32_t *y)
2326{
2327 *x = input->sx;
2328 *y = input->sy;
2329}
2330
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002331struct display *
2332input_get_display(struct input *input)
2333{
2334 return input->display;
2335}
2336
Daniel Stone37816df2012-05-16 18:45:18 +01002337struct wl_seat *
2338input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002339{
Daniel Stone37816df2012-05-16 18:45:18 +01002340 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002341}
2342
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002343uint32_t
2344input_get_modifiers(struct input *input)
2345{
2346 return input->modifiers;
2347}
2348
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002349struct widget *
2350input_get_focus_widget(struct input *input)
2351{
2352 return input->focus_widget;
2353}
2354
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002355struct data_offer {
2356 struct wl_data_offer *offer;
2357 struct input *input;
2358 struct wl_array types;
2359 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002360
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002361 struct task io_task;
2362 int fd;
2363 data_func_t func;
2364 int32_t x, y;
2365 void *user_data;
2366};
2367
2368static void
2369data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2370{
2371 struct data_offer *offer = data;
2372 char **p;
2373
2374 p = wl_array_add(&offer->types, sizeof *p);
2375 *p = strdup(type);
2376}
2377
2378static const struct wl_data_offer_listener data_offer_listener = {
2379 data_offer_offer,
2380};
2381
2382static void
2383data_offer_destroy(struct data_offer *offer)
2384{
2385 char **p;
2386
2387 offer->refcount--;
2388 if (offer->refcount == 0) {
2389 wl_data_offer_destroy(offer->offer);
2390 for (p = offer->types.data; *p; p++)
2391 free(*p);
2392 wl_array_release(&offer->types);
2393 free(offer);
2394 }
2395}
2396
2397static void
2398data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002399 struct wl_data_device *data_device,
2400 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002401{
2402 struct data_offer *offer;
2403
2404 offer = malloc(sizeof *offer);
2405
2406 wl_array_init(&offer->types);
2407 offer->refcount = 1;
2408 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002409 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002410 wl_data_offer_add_listener(offer->offer,
2411 &data_offer_listener, offer);
2412}
2413
2414static void
2415data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002416 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002417 wl_fixed_t x_w, wl_fixed_t y_w,
2418 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002419{
2420 struct input *input = data;
2421 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002422 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002423 float x = wl_fixed_to_double(x_w);
2424 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002425 char **p;
2426
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002427 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002428 window = wl_surface_get_user_data(surface);
2429 input->pointer_focus = window;
2430
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002431 if (offer) {
2432 input->drag_offer = wl_data_offer_get_user_data(offer);
2433
2434 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2435 *p = NULL;
2436
2437 types_data = input->drag_offer->types.data;
2438 } else {
2439 input->drag_offer = NULL;
2440 types_data = NULL;
2441 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002442
2443 window = input->pointer_focus;
2444 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002445 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002446 window->user_data);
2447}
2448
2449static void
2450data_device_leave(void *data, struct wl_data_device *data_device)
2451{
2452 struct input *input = data;
2453
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002454 if (input->drag_offer) {
2455 data_offer_destroy(input->drag_offer);
2456 input->drag_offer = NULL;
2457 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002458}
2459
2460static void
2461data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002462 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002463{
2464 struct input *input = data;
2465 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002466 float x = wl_fixed_to_double(x_w);
2467 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002468 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002469
2470 input->sx = x;
2471 input->sy = y;
2472
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002473 if (input->drag_offer)
2474 types_data = input->drag_offer->types.data;
2475 else
2476 types_data = NULL;
2477
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002478 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002479 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002480 window->user_data);
2481}
2482
2483static void
2484data_device_drop(void *data, struct wl_data_device *data_device)
2485{
2486 struct input *input = data;
2487 struct window *window = input->pointer_focus;
2488
2489 if (window->drop_handler)
2490 window->drop_handler(window, input,
2491 input->sx, input->sy, window->user_data);
2492}
2493
2494static void
2495data_device_selection(void *data,
2496 struct wl_data_device *wl_data_device,
2497 struct wl_data_offer *offer)
2498{
2499 struct input *input = data;
2500 char **p;
2501
2502 if (input->selection_offer)
2503 data_offer_destroy(input->selection_offer);
2504
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002505 if (offer) {
2506 input->selection_offer = wl_data_offer_get_user_data(offer);
2507 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2508 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002509 } else {
2510 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002511 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002512}
2513
2514static const struct wl_data_device_listener data_device_listener = {
2515 data_device_data_offer,
2516 data_device_enter,
2517 data_device_leave,
2518 data_device_motion,
2519 data_device_drop,
2520 data_device_selection
2521};
2522
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002523static void
2524input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002525{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002526 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002527 struct wl_cursor *cursor;
2528 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002530 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002531 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002532 return;
2533
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002534 if (index >= (int) cursor->image_count) {
2535 fprintf(stderr, "cursor index out of range\n");
2536 return;
2537 }
2538
2539 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002540 buffer = wl_cursor_image_get_buffer(image);
2541 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002542 return;
2543
Kristian Høgsbergae277372012-08-01 09:41:08 -04002544 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002545 input->pointer_surface,
2546 image->hotspot_x, image->hotspot_y);
2547 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2548 wl_surface_damage(input->pointer_surface, 0, 0,
2549 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002550}
2551
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002552static const struct wl_callback_listener pointer_surface_listener;
2553
2554static void
2555pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2556 uint32_t time)
2557{
2558 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002559 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002560 int i;
2561
2562 if (callback) {
2563 assert(callback == input->cursor_frame_cb);
2564 wl_callback_destroy(callback);
2565 input->cursor_frame_cb = NULL;
2566 }
2567
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002568 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002569 wl_pointer_set_cursor(input->pointer,
2570 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002571 NULL, 0, 0);
2572 return;
2573 }
2574
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002575 if (input->current_cursor == CURSOR_UNSET)
2576 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002577 cursor = input->display->cursors[input->current_cursor];
2578 if (!cursor)
2579 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002580
2581 /* FIXME We don't have the current time on the first call so we set
2582 * the animation start to the time of the first frame callback. */
2583 if (time == 0)
2584 input->cursor_anim_start = 0;
2585 else if (input->cursor_anim_start == 0)
2586 input->cursor_anim_start = time;
2587
2588 if (time == 0 || input->cursor_anim_start == 0)
2589 i = 0;
2590 else
2591 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2592
2593 input_set_pointer_image_index(input, i);
2594
2595 if (cursor->image_count == 1)
2596 return;
2597
2598 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2599 wl_callback_add_listener(input->cursor_frame_cb,
2600 &pointer_surface_listener, input);
2601}
2602
2603static const struct wl_callback_listener pointer_surface_listener = {
2604 pointer_surface_frame_callback
2605};
2606
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002607void
2608input_set_pointer_image(struct input *input, int pointer)
2609{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002610 int force = 0;
2611
2612 if (input->pointer_enter_serial > input->cursor_serial)
2613 force = 1;
2614
2615 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002616 return;
2617
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002618 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002619 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002620 if (!input->cursor_frame_cb)
2621 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002622 else if (force) {
2623 /* The current frame callback may be stuck if, for instance,
2624 * the set cursor request was processed by the server after
2625 * this client lost the focus. In this case the cursor surface
2626 * might not be mapped and the frame callback wouldn't ever
2627 * complete. Send a set_cursor and attach to try to map the
2628 * cursor surface again so that the callback will finish */
2629 input_set_pointer_image_index(input, 0);
2630 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002631}
2632
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002633struct wl_data_device *
2634input_get_data_device(struct input *input)
2635{
2636 return input->data_device;
2637}
2638
2639void
2640input_set_selection(struct input *input,
2641 struct wl_data_source *source, uint32_t time)
2642{
2643 wl_data_device_set_selection(input->data_device, source, time);
2644}
2645
2646void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002647input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002648{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002649 wl_data_offer_accept(input->drag_offer->offer,
2650 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002651}
2652
2653static void
2654offer_io_func(struct task *task, uint32_t events)
2655{
2656 struct data_offer *offer =
2657 container_of(task, struct data_offer, io_task);
2658 unsigned int len;
2659 char buffer[4096];
2660
2661 len = read(offer->fd, buffer, sizeof buffer);
2662 offer->func(buffer, len,
2663 offer->x, offer->y, offer->user_data);
2664
2665 if (len == 0) {
2666 close(offer->fd);
2667 data_offer_destroy(offer);
2668 }
2669}
2670
2671static void
2672data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2673 data_func_t func, void *user_data)
2674{
2675 int p[2];
2676
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002677 if (pipe2(p, O_CLOEXEC) == -1)
2678 return;
2679
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002680 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2681 close(p[1]);
2682
2683 offer->io_task.run = offer_io_func;
2684 offer->fd = p[0];
2685 offer->func = func;
2686 offer->refcount++;
2687 offer->user_data = user_data;
2688
2689 display_watch_fd(offer->input->display,
2690 offer->fd, EPOLLIN, &offer->io_task);
2691}
2692
2693void
2694input_receive_drag_data(struct input *input, const char *mime_type,
2695 data_func_t func, void *data)
2696{
2697 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2698 input->drag_offer->x = input->sx;
2699 input->drag_offer->y = input->sy;
2700}
2701
2702int
2703input_receive_selection_data(struct input *input, const char *mime_type,
2704 data_func_t func, void *data)
2705{
2706 char **p;
2707
2708 if (input->selection_offer == NULL)
2709 return -1;
2710
2711 for (p = input->selection_offer->types.data; *p; p++)
2712 if (strcmp(mime_type, *p) == 0)
2713 break;
2714
2715 if (*p == NULL)
2716 return -1;
2717
2718 data_offer_receive_data(input->selection_offer,
2719 mime_type, func, data);
2720 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002721}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002722
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002723int
2724input_receive_selection_data_to_fd(struct input *input,
2725 const char *mime_type, int fd)
2726{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002727 if (input->selection_offer)
2728 wl_data_offer_receive(input->selection_offer->offer,
2729 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002730
2731 return 0;
2732}
2733
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002734void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002735window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002736{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002737 if (!window->shell_surface)
2738 return;
2739
Daniel Stone37816df2012-05-16 18:45:18 +01002740 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002741}
2742
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002743static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002744idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002745{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002746 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002747
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002748 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002749 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002750 widget_set_allocation(widget,
2751 window->pending_allocation.x,
2752 window->pending_allocation.y,
2753 window->pending_allocation.width,
2754 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002755
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002756 if (window->input_region) {
2757 wl_region_destroy(window->input_region);
2758 window->input_region = NULL;
2759 }
2760
2761 if (window->opaque_region) {
2762 wl_region_destroy(window->opaque_region);
2763 window->opaque_region = NULL;
2764 }
2765
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002766 if (widget->resize_handler)
2767 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002768 widget->allocation.width,
2769 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002770 widget->user_data);
2771
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002772 if (window->allocation.width != widget->allocation.width ||
2773 window->allocation.height != widget->allocation.height) {
2774 window->allocation = widget->allocation;
2775 window_schedule_redraw(window);
2776 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002777}
2778
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002779void
2780window_schedule_resize(struct window *window, int width, int height)
2781{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002782 window->pending_allocation.x = 0;
2783 window->pending_allocation.y = 0;
2784 window->pending_allocation.width = width;
2785 window->pending_allocation.height = height;
2786
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002787 if (window->min_allocation.width == 0)
2788 window->min_allocation = window->pending_allocation;
2789 if (window->pending_allocation.width < window->min_allocation.width)
2790 window->pending_allocation.width = window->min_allocation.width;
2791 if (window->pending_allocation.height < window->min_allocation.height)
2792 window->pending_allocation.height = window->min_allocation.height;
2793
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002794 window->resize_needed = 1;
2795 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002796}
2797
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002798void
2799widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2800{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002801 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002802}
2803
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002804static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002805handle_ping(void *data, struct wl_shell_surface *shell_surface,
2806 uint32_t serial)
2807{
2808 wl_shell_surface_pong(shell_surface, serial);
2809}
2810
2811static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002812handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002813 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002814{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002815 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002816
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002817 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002818 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002819}
2820
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002822menu_destroy(struct menu *menu)
2823{
2824 widget_destroy(menu->widget);
2825 window_destroy(menu->window);
2826 free(menu);
2827}
2828
2829static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002830handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2831{
2832 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002833 struct menu *menu = window->widget->user_data;
2834
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002835 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002836 * device. Or just use wl_callback. And this really needs to
2837 * be a window vfunc that the menu can set. And we need the
2838 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002839
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002840 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002841 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002842 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002843}
2844
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002845static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002846 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002847 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002849};
2850
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002851void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002852window_get_allocation(struct window *window,
2853 struct rectangle *allocation)
2854{
2855 *allocation = window->allocation;
2856}
2857
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002858static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002859widget_redraw(struct widget *widget)
2860{
2861 struct widget *child;
2862
2863 if (widget->redraw_handler)
2864 widget->redraw_handler(widget, widget->user_data);
2865 wl_list_for_each(child, &widget->child_list, link)
2866 widget_redraw(child);
2867}
2868
2869static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002870frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2871{
2872 struct window *window = data;
2873
2874 wl_callback_destroy(callback);
2875 window->redraw_scheduled = 0;
2876 if (window->redraw_needed)
2877 window_schedule_redraw(window);
2878}
2879
2880static const struct wl_callback_listener listener = {
2881 frame_callback
2882};
2883
2884static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002885idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002886{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002887 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002888 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002889
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002890 if (window->resize_needed)
2891 idle_resize(window);
2892
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002893 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002894 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002895 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002896 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002897 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002898
2899 callback = wl_surface_frame(window->surface);
2900 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002901}
2902
2903void
2904window_schedule_redraw(struct window *window)
2905{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002906 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002907 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002908 window->redraw_task.run = idle_redraw;
2909 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002910 window->redraw_scheduled = 1;
2911 }
2912}
2913
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002914void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002915window_set_fullscreen(struct window *window, int fullscreen)
2916{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002917 if (!window->display->shell)
2918 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002919
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002920 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002921 return;
2922
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002923 if (fullscreen) {
2924 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002925 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002926 wl_shell_surface_set_fullscreen(window->shell_surface,
2927 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2928 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002929 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002930 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002931 wl_shell_surface_set_toplevel(window->shell_surface);
2932 window_schedule_resize(window,
2933 window->saved_allocation.width,
2934 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002935 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002936}
2937
2938void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002939window_set_maximized(struct window *window, int maximized)
2940{
2941 if (!window->display->shell)
2942 return;
2943
2944 if ((window->type == TYPE_MAXIMIZED) == maximized)
2945 return;
2946
2947 if (window->type == TYPE_TOPLEVEL) {
2948 window->saved_allocation = window->allocation;
2949 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2950 window->type = TYPE_MAXIMIZED;
2951 } else {
2952 wl_shell_surface_set_toplevel(window->shell_surface);
2953 window->type = TYPE_TOPLEVEL;
2954 window_schedule_resize(window,
2955 window->saved_allocation.width,
2956 window->saved_allocation.height);
2957 }
2958}
2959
2960void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002961window_set_user_data(struct window *window, void *data)
2962{
2963 window->user_data = data;
2964}
2965
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002966void *
2967window_get_user_data(struct window *window)
2968{
2969 return window->user_data;
2970}
2971
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002972void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002973window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002974 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002975{
2976 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002977}
2978
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002979void
2980window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002981 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002982{
2983 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002984}
2985
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002986void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002987window_set_data_handler(struct window *window, window_data_handler_t handler)
2988{
2989 window->data_handler = handler;
2990}
2991
2992void
2993window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2994{
2995 window->drop_handler = handler;
2996}
2997
2998void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002999window_set_close_handler(struct window *window,
3000 window_close_handler_t handler)
3001{
3002 window->close_handler = handler;
3003}
3004
3005void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003006window_set_fullscreen_handler(struct window *window,
3007 window_fullscreen_handler_t handler)
3008{
3009 window->fullscreen_handler = handler;
3010}
3011
3012void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003013window_set_title(struct window *window, const char *title)
3014{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003015 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003016 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003017 if (window->shell_surface)
3018 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003019}
3020
3021const char *
3022window_get_title(struct window *window)
3023{
3024 return window->title;
3025}
3026
3027void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003028window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3029{
3030 struct text_cursor_position *text_cursor_position =
3031 window->display->text_cursor_position;
3032
Scott Moreau9295ce02012-06-01 12:46:10 -06003033 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003034 return;
3035
3036 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003037 window->surface,
3038 wl_fixed_from_int(x),
3039 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003040}
3041
3042void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003043window_damage(struct window *window, int32_t x, int32_t y,
3044 int32_t width, int32_t height)
3045{
3046 wl_surface_damage(window->surface, x, y, width, height);
3047}
3048
Casey Dahlin9074db52012-04-19 22:50:09 -04003049static void
3050surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003051 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003052{
Rob Bradford7507b572012-05-15 17:55:34 +01003053 struct window *window = data;
3054 struct output *output;
3055 struct output *output_found = NULL;
3056 struct window_output *window_output;
3057
3058 wl_list_for_each(output, &window->display->output_list, link) {
3059 if (output->output == wl_output) {
3060 output_found = output;
3061 break;
3062 }
3063 }
3064
3065 if (!output_found)
3066 return;
3067
3068 window_output = malloc (sizeof *window_output);
3069 window_output->output = output_found;
3070
3071 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003072}
3073
3074static void
3075surface_leave(void *data,
3076 struct wl_surface *wl_surface, struct wl_output *output)
3077{
Rob Bradford7507b572012-05-15 17:55:34 +01003078 struct window *window = data;
3079 struct window_output *window_output;
3080 struct window_output *window_output_found = NULL;
3081
3082 wl_list_for_each(window_output, &window->window_output_list, link) {
3083 if (window_output->output->output == output) {
3084 window_output_found = window_output;
3085 break;
3086 }
3087 }
3088
3089 if (window_output_found) {
3090 wl_list_remove(&window_output_found->link);
3091 free(window_output_found);
3092 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003093}
3094
3095static const struct wl_surface_listener surface_listener = {
3096 surface_enter,
3097 surface_leave
3098};
3099
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003100static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003101window_create_internal(struct display *display,
3102 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003103{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003104 struct window *window;
3105
3106 window = malloc(sizeof *window);
3107 if (window == NULL)
3108 return NULL;
3109
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003110 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003111 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003112 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003113 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003114 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003115 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003116 window->shell_surface =
3117 wl_shell_get_shell_surface(display->shell,
3118 window->surface);
3119 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003120 window->allocation.x = 0;
3121 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003122 window->allocation.width = 0;
3123 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003124 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003125 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003126 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003127 window->input_region = NULL;
3128 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003129
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003130 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003131#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003132 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003133#else
3134 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3135#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003136 else
3137 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003138
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003139 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003140 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003141 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003142
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003143 if (window->shell_surface) {
3144 wl_shell_surface_set_user_data(window->shell_surface, window);
3145 wl_shell_surface_add_listener(window->shell_surface,
3146 &shell_surface_listener, window);
3147 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003148
Rob Bradford7507b572012-05-15 17:55:34 +01003149 wl_list_init (&window->window_output_list);
3150
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003151 return window;
3152}
3153
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003154struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003155window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003156{
3157 struct window *window;
3158
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003159 window = window_create_internal(display, NULL, TYPE_NONE);
3160 if (!window)
3161 return NULL;
3162
3163 return window;
3164}
3165
3166struct window *
3167window_create_custom(struct display *display)
3168{
3169 struct window *window;
3170
3171 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003172 if (!window)
3173 return NULL;
3174
3175 return window;
3176}
3177
3178struct window *
3179window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003180 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003181{
3182 struct window *window;
3183
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003184 window = window_create_internal(parent->display,
3185 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003186 if (!window)
3187 return NULL;
3188
3189 window->x = x;
3190 window->y = y;
3191
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003192 if (display->shell)
3193 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003194 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003195 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003196
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003197 return window;
3198}
3199
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003200static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003201menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003202{
3203 int next;
3204
3205 next = (sy - 8) / 20;
3206 if (menu->current != next) {
3207 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003208 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003209 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003210}
3211
3212static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003213menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003214 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003215 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003216{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003217 struct menu *menu = data;
3218
3219 if (widget == menu->widget)
3220 menu_set_item(data, y);
3221
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003222 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003223}
3224
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003225static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003226menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003227 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003228{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003229 struct menu *menu = data;
3230
3231 if (widget == menu->widget)
3232 menu_set_item(data, y);
3233
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003234 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003235}
3236
3237static void
3238menu_leave_handler(struct widget *widget, struct input *input, void *data)
3239{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003240 struct menu *menu = data;
3241
3242 if (widget == menu->widget)
3243 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003244}
3245
3246static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003247menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003248 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003249 uint32_t button, enum wl_pointer_button_state state,
3250 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003251
3252{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003253 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003254
Daniel Stone4dbadb12012-05-30 16:31:51 +01003255 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3256 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003257 /* Either relase after press-drag-release or
3258 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003259 menu->func(menu->window->parent,
3260 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003261 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003262 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003263 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003264}
3265
3266static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003267menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003268{
3269 cairo_t *cr;
3270 const int32_t r = 3, margin = 3;
3271 struct menu *menu = data;
3272 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003273 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003274
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003275 cr = cairo_create(window->cairo_surface);
3276 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3277 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3278 cairo_paint(cr);
3279
3280 width = window->allocation.width;
3281 height = window->allocation.height;
3282 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003283
3284 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003285 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3286 cairo_fill(cr);
3287
3288 for (i = 0; i < menu->count; i++) {
3289 if (i == menu->current) {
3290 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3291 cairo_rectangle(cr, margin, i * 20 + margin,
3292 width - 2 * margin, 20);
3293 cairo_fill(cr);
3294 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3295 cairo_move_to(cr, 10, i * 20 + 16);
3296 cairo_show_text(cr, menu->entries[i]);
3297 } else {
3298 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3299 cairo_move_to(cr, 10, i * 20 + 16);
3300 cairo_show_text(cr, menu->entries[i]);
3301 }
3302 }
3303
3304 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305}
3306
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003307void
3308window_show_menu(struct display *display,
3309 struct input *input, uint32_t time, struct window *parent,
3310 int32_t x, int32_t y,
3311 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003312{
3313 struct window *window;
3314 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003315 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003316
3317 menu = malloc(sizeof *menu);
3318 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003319 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003320
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003321 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003322 if (!window) {
3323 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003324 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003325 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003326
3327 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003328 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003329 menu->entries = entries;
3330 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003331 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003332 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003333 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003334 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003335 window->type = TYPE_MENU;
3336 window->x = x;
3337 window->y = y;
3338
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003339 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003340 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003341 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003342 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003343 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003344
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003345 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003346 widget_set_enter_handler(menu->widget, menu_enter_handler);
3347 widget_set_leave_handler(menu->widget, menu_leave_handler);
3348 widget_set_motion_handler(menu->widget, menu_motion_handler);
3349 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003350
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003351 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003352 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353}
3354
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003355void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003356window_set_buffer_type(struct window *window, enum window_buffer_type type)
3357{
3358 window->buffer_type = type;
3359}
3360
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003361
3362static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003363display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003364 struct wl_output *wl_output,
3365 int x, int y,
3366 int physical_width,
3367 int physical_height,
3368 int subpixel,
3369 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003370 const char *model,
3371 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003372{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003373 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003374
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003375 output->allocation.x = x;
3376 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003377}
3378
3379static void
3380display_handle_mode(void *data,
3381 struct wl_output *wl_output,
3382 uint32_t flags,
3383 int width,
3384 int height,
3385 int refresh)
3386{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003387 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003388 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003389
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003390 if (flags & WL_OUTPUT_MODE_CURRENT) {
3391 output->allocation.width = width;
3392 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003393 if (display->output_configure_handler)
3394 (*display->output_configure_handler)(
3395 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003396 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003397}
3398
3399static const struct wl_output_listener output_listener = {
3400 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003401 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003402};
3403
3404static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003405display_add_output(struct display *d, uint32_t id)
3406{
3407 struct output *output;
3408
3409 output = malloc(sizeof *output);
3410 if (output == NULL)
3411 return;
3412
3413 memset(output, 0, sizeof *output);
3414 output->display = d;
3415 output->output =
3416 wl_display_bind(d->display, id, &wl_output_interface);
3417 wl_list_insert(d->output_list.prev, &output->link);
3418
3419 wl_output_add_listener(output->output, &output_listener, output);
3420}
3421
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003422static void
3423output_destroy(struct output *output)
3424{
3425 if (output->destroy_handler)
3426 (*output->destroy_handler)(output, output->user_data);
3427
3428 wl_output_destroy(output->output);
3429 wl_list_remove(&output->link);
3430 free(output);
3431}
3432
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003433void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003434display_set_output_configure_handler(struct display *display,
3435 display_output_handler_t handler)
3436{
3437 struct output *output;
3438
3439 display->output_configure_handler = handler;
3440 if (!handler)
3441 return;
3442
3443 wl_list_for_each(output, &display->output_list, link)
3444 (*display->output_configure_handler)(output,
3445 display->user_data);
3446}
3447
3448void
3449output_set_user_data(struct output *output, void *data)
3450{
3451 output->user_data = data;
3452}
3453
3454void *
3455output_get_user_data(struct output *output)
3456{
3457 return output->user_data;
3458}
3459
3460void
3461output_set_destroy_handler(struct output *output,
3462 display_output_handler_t handler)
3463{
3464 output->destroy_handler = handler;
3465 /* FIXME: implement this, once we have way to remove outputs */
3466}
3467
3468void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003469output_get_allocation(struct output *output, struct rectangle *allocation)
3470{
3471 *allocation = output->allocation;
3472}
3473
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003474struct wl_output *
3475output_get_wl_output(struct output *output)
3476{
3477 return output->output;
3478}
3479
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003480static void
Daniel Stone97f68542012-05-30 16:32:01 +01003481fini_xkb(struct input *input)
3482{
3483 xkb_state_unref(input->xkb.state);
3484 xkb_map_unref(input->xkb.keymap);
3485}
3486
3487static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003488display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003489{
3490 struct input *input;
3491
3492 input = malloc(sizeof *input);
3493 if (input == NULL)
3494 return;
3495
3496 memset(input, 0, sizeof *input);
3497 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003498 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003499 input->pointer_focus = NULL;
3500 input->keyboard_focus = NULL;
3501 wl_list_insert(d->input_list.prev, &input->link);
3502
Daniel Stone37816df2012-05-16 18:45:18 +01003503 wl_seat_add_listener(input->seat, &seat_listener, input);
3504 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003505
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003506 input->data_device =
3507 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003508 input->seat);
3509 wl_data_device_add_listener(input->data_device, &data_device_listener,
3510 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003511
3512 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003513
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003514 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3515 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003516 input->repeat_task.run = keyboard_repeat_func;
3517 display_watch_fd(d, input->repeat_timer_fd,
3518 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003519}
3520
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003521static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003522input_destroy(struct input *input)
3523{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003524 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003525 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003526
3527 if (input->drag_offer)
3528 data_offer_destroy(input->drag_offer);
3529
3530 if (input->selection_offer)
3531 data_offer_destroy(input->selection_offer);
3532
3533 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003534 fini_xkb(input);
3535
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003536 wl_surface_destroy(input->pointer_surface);
3537
Pekka Paalanene1207c72011-12-16 12:02:09 +02003538 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003539 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003540 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003541 free(input);
3542}
3543
3544static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003545init_workspace_manager(struct display *d, uint32_t id)
3546{
3547 d->workspace_manager =
3548 wl_display_bind(d->display, id, &workspace_manager_interface);
3549 if (d->workspace_manager != NULL)
3550 workspace_manager_add_listener(d->workspace_manager,
3551 &workspace_manager_listener,
3552 d);
3553}
3554
3555static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003556display_handle_global(struct wl_display *display, uint32_t id,
3557 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003558{
3559 struct display *d = data;
3560
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003561 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003562 d->compositor =
3563 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003564 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003565 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003566 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003567 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003568 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003569 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003570 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003571 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3573 d->data_device_manager =
3574 wl_display_bind(display, id,
3575 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003576 } else if (strcmp(interface, "text_cursor_position") == 0) {
3577 d->text_cursor_position =
3578 wl_display_bind(display, id,
3579 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003580 } else if (strcmp(interface, "workspace_manager") == 0) {
3581 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003582 }
3583}
3584
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003585#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003586static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003587init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003588{
3589 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003590 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003591
Rob Clark6396ed32012-03-11 19:48:41 -05003592#ifdef USE_CAIRO_GLESV2
3593# define GL_BIT EGL_OPENGL_ES2_BIT
3594#else
3595# define GL_BIT EGL_OPENGL_BIT
3596#endif
3597
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003598 static const EGLint argb_cfg_attribs[] = {
3599 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003600 EGL_RED_SIZE, 1,
3601 EGL_GREEN_SIZE, 1,
3602 EGL_BLUE_SIZE, 1,
3603 EGL_ALPHA_SIZE, 1,
3604 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003605 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003606 EGL_NONE
3607 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003608
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003609#ifdef USE_CAIRO_GLESV2
3610 static const EGLint context_attribs[] = {
3611 EGL_CONTEXT_CLIENT_VERSION, 2,
3612 EGL_NONE
3613 };
3614 EGLint api = EGL_OPENGL_ES_API;
3615#else
3616 EGLint *context_attribs = NULL;
3617 EGLint api = EGL_OPENGL_API;
3618#endif
3619
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003620 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003621 if (!eglInitialize(d->dpy, &major, &minor)) {
3622 fprintf(stderr, "failed to initialize display\n");
3623 return -1;
3624 }
3625
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003626 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003627 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3628 return -1;
3629 }
3630
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003631 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3632 &d->argb_config, 1, &n) || n != 1) {
3633 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003634 return -1;
3635 }
3636
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003637 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003638 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003639 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003640 fprintf(stderr, "failed to create context\n");
3641 return -1;
3642 }
3643
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003644 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003645 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003646 return -1;
3647 }
3648
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003649#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003650 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3651 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3652 fprintf(stderr, "failed to get cairo egl argb device\n");
3653 return -1;
3654 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003655#endif
3656
3657 return 0;
3658}
3659
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003660static void
3661fini_egl(struct display *display)
3662{
3663#ifdef HAVE_CAIRO_EGL
3664 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003665#endif
3666
3667 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3668 EGL_NO_CONTEXT);
3669
3670 eglTerminate(display->dpy);
3671 eglReleaseThread();
3672}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003673#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003674
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003675static int
3676event_mask_update(uint32_t mask, void *data)
3677{
3678 struct display *d = data;
3679
3680 d->mask = mask;
3681
3682 return 0;
3683}
3684
3685static void
3686handle_display_data(struct task *task, uint32_t events)
3687{
3688 struct display *display =
3689 container_of(task, struct display, display_task);
3690
3691 wl_display_iterate(display->display, display->mask);
3692}
3693
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003694struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003695display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003696{
3697 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003698
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003699 d = malloc(sizeof *d);
3700 if (d == NULL)
3701 return NULL;
3702
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003703 memset(d, 0, sizeof *d);
3704
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003705 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003706 if (d->display == NULL) {
3707 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003708 return NULL;
3709 }
3710
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003711 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003712 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3713 d->display_task.run = handle_display_data;
3714 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3715
3716 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003717 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003718 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003719
Daniel Stone97f68542012-05-30 16:32:01 +01003720 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003721 if (d->xkb_context == NULL) {
3722 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003723 return NULL;
3724 }
3725
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003726 /* Set up listener so we'll catch all events. */
3727 wl_display_add_global_listener(d->display,
3728 display_handle_global, d);
3729
3730 /* Process connection events. */
3731 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003732#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003733 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003734 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003735#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003736
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003737 d->image_target_texture_2d =
3738 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3739 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3740 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3741
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003742 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003743
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003744 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003745
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003746 wl_list_init(&d->window_list);
3747
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003748 d->workspace = 0;
3749 d->workspace_count = 1;
3750
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003751 return d;
3752}
3753
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003754static void
3755display_destroy_outputs(struct display *display)
3756{
3757 struct output *tmp;
3758 struct output *output;
3759
3760 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3761 output_destroy(output);
3762}
3763
Pekka Paalanene1207c72011-12-16 12:02:09 +02003764static void
3765display_destroy_inputs(struct display *display)
3766{
3767 struct input *tmp;
3768 struct input *input;
3769
3770 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3771 input_destroy(input);
3772}
3773
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003774void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003775display_destroy(struct display *display)
3776{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003777 if (!wl_list_empty(&display->window_list))
3778 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3779
3780 if (!wl_list_empty(&display->deferred_list))
3781 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3782
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003783 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003784 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003785
Daniel Stone97f68542012-05-30 16:32:01 +01003786 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003787
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003788 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003789 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003790
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003791#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003792 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003793#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003794
Pekka Paalanenc2052982011-12-16 11:41:32 +02003795 if (display->shell)
3796 wl_shell_destroy(display->shell);
3797
3798 if (display->shm)
3799 wl_shm_destroy(display->shm);
3800
3801 if (display->data_device_manager)
3802 wl_data_device_manager_destroy(display->data_device_manager);
3803
3804 wl_compositor_destroy(display->compositor);
3805
3806 close(display->epoll_fd);
3807
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003808 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003809 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003810 free(display);
3811}
3812
3813void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003814display_set_user_data(struct display *display, void *data)
3815{
3816 display->user_data = data;
3817}
3818
3819void *
3820display_get_user_data(struct display *display)
3821{
3822 return display->user_data;
3823}
3824
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003825struct wl_display *
3826display_get_display(struct display *display)
3827{
3828 return display->display;
3829}
3830
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003831struct output *
3832display_get_output(struct display *display)
3833{
3834 return container_of(display->output_list.next, struct output, link);
3835}
3836
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003837struct wl_compositor *
3838display_get_compositor(struct display *display)
3839{
3840 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003841}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003842
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003843uint32_t
3844display_get_serial(struct display *display)
3845{
3846 return display->serial;
3847}
3848
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003849EGLDisplay
3850display_get_egl_display(struct display *d)
3851{
3852 return d->dpy;
3853}
3854
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003855struct wl_data_source *
3856display_create_data_source(struct display *display)
3857{
3858 return wl_data_device_manager_create_data_source(display->data_device_manager);
3859}
3860
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003861EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003862display_get_argb_egl_config(struct display *d)
3863{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003864 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003865}
3866
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003867struct wl_shell *
3868display_get_shell(struct display *display)
3869{
3870 return display->shell;
3871}
3872
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003873int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003874display_acquire_window_surface(struct display *display,
3875 struct window *window,
3876 EGLContext ctx)
3877{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003878#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003879 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003880 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003881
3882 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003883 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003884 device = cairo_surface_get_device(window->cairo_surface);
3885 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003886 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003887
Benjamin Franzke0c991632011-09-27 21:57:31 +02003888 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003889 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003890 ctx = display->argb_ctx;
3891 else
3892 assert(0);
3893 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003894
3895 data = cairo_surface_get_user_data(window->cairo_surface,
3896 &surface_data_key);
3897
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003898 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003899 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003900 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3901 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003902
3903 return 0;
3904#else
3905 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003906#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003907}
3908
3909void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003910display_release_window_surface(struct display *display,
3911 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003912{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003913#ifdef HAVE_CAIRO_EGL
3914 cairo_device_t *device;
3915
3916 device = cairo_surface_get_device(window->cairo_surface);
3917 if (!device)
3918 return;
3919
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003920 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003921 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003922 cairo_device_release(device);
3923#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003924}
3925
3926void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003927display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003928{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003929 wl_list_insert(&display->deferred_list, &task->link);
3930}
3931
3932void
3933display_watch_fd(struct display *display,
3934 int fd, uint32_t events, struct task *task)
3935{
3936 struct epoll_event ep;
3937
3938 ep.events = events;
3939 ep.data.ptr = task;
3940 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3941}
3942
3943void
3944display_run(struct display *display)
3945{
3946 struct task *task;
3947 struct epoll_event ep[16];
3948 int i, count;
3949
Pekka Paalanen826d7952011-12-15 10:14:07 +02003950 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003951 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003952 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003953
3954 while (!wl_list_empty(&display->deferred_list)) {
3955 task = container_of(display->deferred_list.next,
3956 struct task, link);
3957 wl_list_remove(&task->link);
3958 task->run(task, 0);
3959 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003960
3961 if (!display->running)
3962 break;
3963
3964 wl_display_flush(display->display);
3965
3966 count = epoll_wait(display->epoll_fd,
3967 ep, ARRAY_LENGTH(ep), -1);
3968 for (i = 0; i < count; i++) {
3969 task = ep[i].data.ptr;
3970 task->run(task, ep[i].events);
3971 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003972 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003973}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003974
3975void
3976display_exit(struct display *display)
3977{
3978 display->running = 0;
3979}