blob: f3b61dec4fbc6d09529db8d9e0069730e58cac0a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020065#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030066#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050069
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070070struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050074 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040076 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040077 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020079 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040080 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050081 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020082 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020083 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040084 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040085
86 int display_fd;
87 uint32_t mask;
88 struct task display_task;
89
90 int epoll_fd;
91 struct wl_list deferred_list;
92
Pekka Paalanen826d7952011-12-15 10:14:07 +020093 int running;
94
Kristian Høgsberg478d9262010-06-08 20:34:11 -040095 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040096 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050097 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040098
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040099 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400100
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300101 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300102 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400103
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500104 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
105 PFNEGLCREATEIMAGEKHRPROC create_image;
106 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200107
108 display_output_handler_t output_configure_handler;
109
110 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100111
112 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200113
114 uint32_t workspace;
115 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400143 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500144 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500145 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400146 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400147 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400148 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400149 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400150 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400151 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400152 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400153 int focus_count;
154
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400155 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500156
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400157 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500158
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700159 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400160
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500161 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500162 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400163 window_data_handler_t data_handler;
164 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500165 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400166 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400167
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300168 struct wl_callback *frame_cb;
169
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200170 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500171 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500172
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500173 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400174 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500175};
176
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500177struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500178 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300179 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500180 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 struct wl_list link;
182 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500183 widget_resize_handler_t resize_handler;
184 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500185 widget_enter_handler_t enter_handler;
186 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500187 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500188 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200189 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400190 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500191 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300192 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193};
194
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195struct input {
196 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100197 struct wl_seat *seat;
198 struct wl_pointer *pointer;
199 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 struct window *pointer_focus;
201 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300202 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300203 uint32_t cursor_anim_start;
204 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300205 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400206 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400207 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400208 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400209 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400210 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400211
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500212 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500213 struct widget *grab;
214 uint32_t grab_button;
215
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400216 struct wl_data_device *data_device;
217 struct data_offer *drag_offer;
218 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100219
220 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100221 struct xkb_keymap *keymap;
222 struct xkb_state *state;
223 xkb_mod_mask_t control_mask;
224 xkb_mod_mask_t alt_mask;
225 xkb_mod_mask_t shift_mask;
226 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400227
228 struct task repeat_task;
229 int repeat_timer_fd;
230 uint32_t repeat_sym;
231 uint32_t repeat_key;
232 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400233};
234
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500235struct output {
236 struct display *display;
237 struct wl_output *output;
238 struct rectangle allocation;
239 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200240
241 display_output_handler_t destroy_handler;
242 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500243};
244
Martin Minarik1998b152012-05-10 02:04:35 +0200245enum frame_button_action {
246 FRAME_BUTTON_NULL = 0,
247 FRAME_BUTTON_ICON = 1,
248 FRAME_BUTTON_CLOSE = 2,
249 FRAME_BUTTON_MINIMIZE = 3,
250 FRAME_BUTTON_MAXIMIZE = 4,
251};
252
253enum frame_button_pointer {
254 FRAME_BUTTON_DEFAULT = 0,
255 FRAME_BUTTON_OVER = 1,
256 FRAME_BUTTON_ACTIVE = 2,
257};
258
259enum frame_button_align {
260 FRAME_BUTTON_RIGHT = 0,
261 FRAME_BUTTON_LEFT = 1,
262};
263
264enum frame_button_decoration {
265 FRAME_BUTTON_NONE = 0,
266 FRAME_BUTTON_FANCY = 1,
267};
268
269struct frame_button {
270 struct widget *widget;
271 struct frame *frame;
272 cairo_surface_t *icon;
273 enum frame_button_action type;
274 enum frame_button_pointer state;
275 struct wl_list link; /* buttons_list */
276 enum frame_button_align align;
277 enum frame_button_decoration decoration;
278};
279
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500280struct frame {
281 struct widget *widget;
282 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200283 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500284};
285
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500286struct menu {
287 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500288 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500289 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500290 const char **entries;
291 uint32_t time;
292 int current;
293 int count;
294 menu_func_t func;
295};
296
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300297struct tooltip {
298 struct widget *parent;
299 struct window *window;
300 struct widget *widget;
301 char *entry;
302 struct task tooltip_task;
303 int tooltip_fd;
304 float x, y;
305};
306
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700307struct shm_pool {
308 struct wl_shm_pool *pool;
309 size_t size;
310 size_t used;
311 void *data;
312};
313
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400314enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300315 CURSOR_DEFAULT = 100,
316 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400317};
318
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500319enum window_location {
320 WINDOW_INTERIOR = 0,
321 WINDOW_RESIZING_TOP = 1,
322 WINDOW_RESIZING_BOTTOM = 2,
323 WINDOW_RESIZING_LEFT = 4,
324 WINDOW_RESIZING_TOP_LEFT = 5,
325 WINDOW_RESIZING_BOTTOM_LEFT = 6,
326 WINDOW_RESIZING_RIGHT = 8,
327 WINDOW_RESIZING_TOP_RIGHT = 9,
328 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
329 WINDOW_RESIZING_MASK = 15,
330 WINDOW_EXTERIOR = 16,
331 WINDOW_TITLEBAR = 17,
332 WINDOW_CLIENT_AREA = 18,
333};
334
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335static const cairo_user_data_key_t surface_data_key;
336struct surface_data {
337 struct wl_buffer *buffer;
338};
339
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500340#define MULT(_d,c,a,t) \
341 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
342
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500343#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400344
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100345struct egl_window_surface_data {
346 struct display *display;
347 struct wl_surface *surface;
348 struct wl_egl_window *window;
349 EGLSurface surf;
350};
351
352static void
353egl_window_surface_data_destroy(void *p)
354{
355 struct egl_window_surface_data *data = p;
356 struct display *d = data->display;
357
358 eglDestroySurface(d->dpy, data->surf);
359 wl_egl_window_destroy(data->window);
360 data->surface = NULL;
361
362 free(p);
363}
364
365static cairo_surface_t *
366display_create_egl_window_surface(struct display *display,
367 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400368 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100369 struct rectangle *rectangle)
370{
371 cairo_surface_t *cairo_surface;
372 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
376 data = malloc(sizeof *data);
377 if (data == NULL)
378 return NULL;
379
380 data->display = display;
381 data->surface = surface;
382
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500383 config = display->argb_config;
384 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100389
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400390 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500391 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100394 data->surf,
395 rectangle->width,
396 rectangle->height);
397
398 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
399 data, egl_window_surface_data_destroy);
400
401 return cairo_surface;
402}
403
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404#endif
405
406struct wl_buffer *
407display_get_buffer_for_surface(struct display *display,
408 cairo_surface_t *surface)
409{
410 struct surface_data *data;
411
412 data = cairo_surface_get_user_data (surface, &surface_data_key);
413
414 return data->buffer;
415}
416
417struct shm_surface_data {
418 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700419 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420};
421
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500422static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423shm_pool_destroy(struct shm_pool *pool);
424
425static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426shm_surface_data_destroy(void *p)
427{
428 struct shm_surface_data *data = p;
429
430 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431 if (data->pool)
432 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300433
434 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435}
436
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437static struct wl_shm_pool *
438make_shm_pool(struct display *display, int size, void **data)
439{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400440 struct wl_shm_pool *pool;
441 int fd;
442
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300443 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400444 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300445 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
446 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400447 return NULL;
448 }
449
450 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400451 if (*data == MAP_FAILED) {
452 fprintf(stderr, "mmap failed: %m\n");
453 close(fd);
454 return NULL;
455 }
456
457 pool = wl_shm_create_pool(display->shm, fd, size);
458
459 close(fd);
460
461 return pool;
462}
463
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700464static struct shm_pool *
465shm_pool_create(struct display *display, size_t size)
466{
467 struct shm_pool *pool = malloc(sizeof *pool);
468
469 if (!pool)
470 return NULL;
471
472 pool->pool = make_shm_pool(display, size, &pool->data);
473 if (!pool->pool) {
474 free(pool);
475 return NULL;
476 }
477
478 pool->size = size;
479 pool->used = 0;
480
481 return pool;
482}
483
484static void *
485shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
486{
487 if (pool->used + size > pool->size)
488 return NULL;
489
490 *offset = pool->used;
491 pool->used += size;
492
493 return (char *) pool->data + *offset;
494}
495
496/* destroy the pool. this does not unmap the memory though */
497static void
498shm_pool_destroy(struct shm_pool *pool)
499{
500 munmap(pool->data, pool->size);
501 wl_shm_pool_destroy(pool->pool);
502 free(pool);
503}
504
505/* Start allocating from the beginning of the pool again */
506static void
507shm_pool_reset(struct shm_pool *pool)
508{
509 pool->used = 0;
510}
511
512static int
513data_length_for_shm_surface(struct rectangle *rect)
514{
515 int stride;
516
517 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 rect->width);
519 return stride * rect->height;
520}
521
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500522static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523display_create_shm_surface_from_pool(struct display *display,
524 struct rectangle *rectangle,
525 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526{
527 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400528 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400529 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400531 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532
533 data = malloc(sizeof *data);
534 if (data == NULL)
535 return NULL;
536
537 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
538 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700539 length = stride * rectangle->height;
540 data->pool = NULL;
541 map = shm_pool_allocate(pool, length, &offset);
542
543 if (!map) {
544 free(data);
545 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 CAIRO_FORMAT_ARGB32,
550 rectangle->width,
551 rectangle->height,
552 stride);
553
554 cairo_surface_set_user_data (surface, &surface_data_key,
555 data, shm_surface_data_destroy);
556
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500560 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400561
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700562 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400563 rectangle->width,
564 rectangle->height,
565 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700567 return surface;
568}
569
570static cairo_surface_t *
571display_create_shm_surface(struct display *display,
572 struct rectangle *rectangle, uint32_t flags,
573 struct window *window)
574{
575 struct shm_surface_data *data;
576 struct shm_pool *pool;
577 cairo_surface_t *surface;
578
579 if (window && window->pool) {
580 shm_pool_reset(window->pool);
581 surface = display_create_shm_surface_from_pool(display,
582 rectangle,
583 flags,
584 window->pool);
585 if (surface)
586 return surface;
587 }
588
589 pool = shm_pool_create(display,
590 data_length_for_shm_surface(rectangle));
591 if (!pool)
592 return NULL;
593
594 surface =
595 display_create_shm_surface_from_pool(display, rectangle,
596 flags, pool);
597
598 if (!surface) {
599 shm_pool_destroy(pool);
600 return NULL;
601 }
602
603 /* make sure we destroy the pool when the surface is destroyed */
604 data = cairo_surface_get_user_data(surface, &surface_data_key);
605 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606
607 return surface;
608}
609
nobled7b87cb02011-02-01 18:51:47 +0000610static int
611check_size(struct rectangle *rect)
612{
613 if (rect->width && rect->height)
614 return 0;
615
616 fprintf(stderr, "tried to create surface of "
617 "width: %d, height: %d\n", rect->width, rect->height);
618 return -1;
619}
620
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621cairo_surface_t *
622display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400624 struct rectangle *rectangle,
625 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
nobled7b87cb02011-02-01 18:51:47 +0000627 if (check_size(rectangle) < 0)
628 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500629#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300630 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400631 return display_create_egl_window_surface(display,
632 surface,
633 flags,
634 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400636 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637}
638
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200639/*
640 * The following correspondences between file names and cursors was copied
641 * from: https://bugs.kde.org/attachment.cgi?id=67313
642 */
643
644static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300645 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200646 "sw-resize"
647};
648
649static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300650 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200651 "se-resize"
652};
653
654static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300655 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200656 "s-resize"
657};
658
659static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300660 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200661 "closedhand",
662 "208530c400c041818281048008011002"
663};
664
665static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200667 "default",
668 "top_left_arrow",
669 "left-arrow"
670};
671
672static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300673 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200674 "w-resize"
675};
676
677static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300678 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200679 "e-resize"
680};
681
682static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300683 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200684 "nw-resize"
685};
686
687static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300688 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200689 "ne-resize"
690};
691
692static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300693 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200694 "n-resize"
695};
696
697static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300698 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200699 "ibeam",
700 "text"
701};
702
703static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300704 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200705 "pointer",
706 "pointing_hand",
707 "e29285e634086352946a0e7090d73106"
708};
709
710static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400711 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200712 "wait",
713 "0426c94ea35c87780ff01dc239897213"
714};
715
716struct cursor_alternatives {
717 const char **names;
718 size_t count;
719};
720
721static const struct cursor_alternatives cursors[] = {
722 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
723 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
724 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
725 {grabbings, ARRAY_LENGTH(grabbings)},
726 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
727 {left_sides, ARRAY_LENGTH(left_sides)},
728 {right_sides, ARRAY_LENGTH(right_sides)},
729 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
730 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
731 {top_sides, ARRAY_LENGTH(top_sides)},
732 {xterms, ARRAY_LENGTH(xterms)},
733 {hand1s, ARRAY_LENGTH(hand1s)},
734 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300735};
736
737static void
738create_cursors(struct display *display)
739{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100740 char *config_file;
741 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200742 unsigned int i, j;
743 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100744 struct config_key shell_keys[] = {
745 { "cursor-theme", CONFIG_KEY_STRING, &theme },
746 };
747 struct config_section cs[] = {
748 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
749 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300750
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100751 config_file = config_file_path("weston.ini");
752 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
753 free(config_file);
754
755 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300756 display->cursors =
757 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
758
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300759 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200760 cursor = NULL;
761 for (j = 0; !cursor && j < cursors[i].count; ++j)
762 cursor = wl_cursor_theme_get_cursor(
763 display->cursor_theme, cursors[i].names[j]);
764
765 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300766 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200767 cursors[i].names[0]);
768
769 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300770 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300771}
772
773static void
774destroy_cursors(struct display *display)
775{
776 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800777 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300778}
779
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300780struct wl_cursor_image *
781display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400782{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200783 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400784
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300785 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400786}
787
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400788static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789window_get_resize_dx_dy(struct window *window, int *x, int *y)
790{
791 if (window->resize_edges & WINDOW_RESIZING_LEFT)
792 *x = window->server_allocation.width - window->allocation.width;
793 else
794 *x = 0;
795
796 if (window->resize_edges & WINDOW_RESIZING_TOP)
797 *y = window->server_allocation.height -
798 window->allocation.height;
799 else
800 *y = 0;
801
802 window->resize_edges = 0;
803}
804
805static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500806window_attach_surface(struct window *window)
807{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400808 struct display *display = window->display;
809 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500813 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400815 if (window->type == TYPE_NONE) {
816 window->type = TYPE_TOPLEVEL;
817 if (display->shell)
818 wl_shell_surface_set_toplevel(window->shell_surface);
819 }
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 buffer =
835 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400836 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200838 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400844 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847 default:
848 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500850
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500851 if (window->input_region) {
852 wl_surface_set_input_region(window->surface,
853 window->input_region);
854 wl_region_destroy(window->input_region);
855 window->input_region = NULL;
856 }
857
858 if (window->opaque_region) {
859 wl_surface_set_opaque_region(window->surface,
860 window->opaque_region);
861 wl_region_destroy(window->opaque_region);
862 window->opaque_region = NULL;
863 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500864}
865
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400866int
867window_has_focus(struct window *window)
868{
869 return window->focus_count > 0;
870}
871
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500872void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400873window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500874{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400875 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100876 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500877}
878
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400879void
880window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500882 cairo_surface_reference(surface);
883
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884 if (window->cairo_surface != NULL)
885 cairo_surface_destroy(window->cairo_surface);
886
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500887 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888}
889
Benjamin Franzke22d54812011-07-16 19:50:32 +0000890#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100891static void
892window_resize_cairo_window_surface(struct window *window)
893{
894 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200895 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896
897 data = cairo_surface_get_user_data(window->cairo_surface,
898 &surface_data_key);
899
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200900 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901 wl_egl_window_resize(data->window,
902 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 window->allocation.height,
904 x,y);
905
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 cairo_gl_surface_set_size(window->cairo_surface,
907 window->allocation.width,
908 window->allocation.height);
909}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000910#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400912struct display *
913window_get_display(struct window *window)
914{
915 return window->display;
916}
917
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400918void
919window_create_surface(struct window *window)
920{
921 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 uint32_t flags = 0;
923
924 if (!window->transparent)
925 flags = SURFACE_OPAQUE;
926
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400927 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500928#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100929 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
930 if (window->cairo_surface) {
931 window_resize_cairo_window_surface(window);
932 return;
933 }
934 surface = display_create_surface(window->display,
935 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400936 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100937 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938#endif
939 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400940 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400941 &window->allocation,
942 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800944 default:
945 surface = NULL;
946 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400948
949 window_set_surface(window, surface);
950 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951}
952
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200953static void frame_destroy(struct frame *frame);
954
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400955void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500956window_destroy(struct window *window)
957{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200958 struct display *display = window->display;
959 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100960 struct window_output *window_output;
961 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200962
963 if (window->redraw_scheduled)
964 wl_list_remove(&window->redraw_task.link);
965
966 wl_list_for_each(input, &display->input_list, link) {
967 if (input->pointer_focus == window)
968 input->pointer_focus = NULL;
969 if (input->keyboard_focus == window)
970 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500971 if (input->focus_widget &&
972 input->focus_widget->window == window)
973 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200974 }
975
Rob Bradford7507b572012-05-15 17:55:34 +0100976 wl_list_for_each_safe(window_output, window_output_tmp,
977 &window->window_output_list, link) {
978 free (window_output);
979 }
980
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500981 if (window->input_region)
982 wl_region_destroy(window->input_region);
983 if (window->opaque_region)
984 wl_region_destroy(window->opaque_region);
985
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200986 if (window->frame)
987 frame_destroy(window->frame);
988
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200989 if (window->shell_surface)
990 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500991 wl_surface_destroy(window->surface);
992 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200993
994 if (window->cairo_surface != NULL)
995 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200996
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300997 if (window->frame_cb)
998 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200999 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000 free(window);
1001}
1002
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001003static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 wl_list_for_each(child, &widget->child_list, link) {
1009 target = widget_find_widget(child, x, y);
1010 if (target)
1011 return target;
1012 }
1013
1014 if (widget->allocation.x <= x &&
1015 x < widget->allocation.x + widget->allocation.width &&
1016 widget->allocation.y <= y &&
1017 y < widget->allocation.y + widget->allocation.height) {
1018 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019 }
1020
1021 return NULL;
1022}
1023
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001024static struct widget *
1025widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 widget = malloc(sizeof *widget);
1030 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001031 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001032 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001033 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001034 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001035 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001036 widget->tooltip = NULL;
1037 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001038
1039 return widget;
1040}
1041
1042struct widget *
1043window_add_widget(struct window *window, void *data)
1044{
1045 window->widget = widget_create(window, data);
1046 wl_list_init(&window->widget->link);
1047
1048 return window->widget;
1049}
1050
1051struct widget *
1052widget_add_widget(struct widget *parent, void *data)
1053{
1054 struct widget *widget;
1055
1056 widget = widget_create(parent->window, data);
1057 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060}
1061
1062void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001063widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001065 struct display *display = widget->window->display;
1066 struct input *input;
1067
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001068 if (widget->tooltip) {
1069 free(widget->tooltip);
1070 widget->tooltip = NULL;
1071 }
1072
Pekka Paalanene156fb62012-01-19 13:51:38 +02001073 wl_list_for_each(input, &display->input_list, link) {
1074 if (input->focus_widget == widget)
1075 input->focus_widget = NULL;
1076 }
1077
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001078 wl_list_remove(&widget->link);
1079 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001082void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001083widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086}
1087
1088void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001089widget_set_size(struct widget *widget, int32_t width, int32_t height)
1090{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001091 widget->allocation.width = width;
1092 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001093}
1094
1095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_set_allocation(struct widget *widget,
1097 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099 widget->allocation.x = x;
1100 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001101 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001104void
1105widget_set_transparent(struct widget *widget, int transparent)
1106{
1107 widget->opaque = !transparent;
1108}
1109
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001110void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001111widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001113 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001114}
1115
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001116void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001117widget_set_resize_handler(struct widget *widget,
1118 widget_resize_handler_t handler)
1119{
1120 widget->resize_handler = handler;
1121}
1122
1123void
1124widget_set_redraw_handler(struct widget *widget,
1125 widget_redraw_handler_t handler)
1126{
1127 widget->redraw_handler = handler;
1128}
1129
1130void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001131widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001132{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001133 widget->enter_handler = handler;
1134}
1135
1136void
1137widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1138{
1139 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001140}
1141
1142void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001143widget_set_motion_handler(struct widget *widget,
1144 widget_motion_handler_t handler)
1145{
1146 widget->motion_handler = handler;
1147}
1148
1149void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001150widget_set_button_handler(struct widget *widget,
1151 widget_button_handler_t handler)
1152{
1153 widget->button_handler = handler;
1154}
1155
1156void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001157widget_set_axis_handler(struct widget *widget,
1158 widget_axis_handler_t handler)
1159{
1160 widget->axis_handler = handler;
1161}
1162
1163void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001164widget_schedule_redraw(struct widget *widget)
1165{
1166 window_schedule_redraw(widget->window);
1167}
1168
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001169cairo_surface_t *
1170window_get_surface(struct window *window)
1171{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001172 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001173}
1174
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001175struct wl_surface *
1176window_get_wl_surface(struct window *window)
1177{
1178 return window->surface;
1179}
1180
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001181struct wl_shell_surface *
1182window_get_wl_shell_surface(struct window *window)
1183{
1184 return window->shell_surface;
1185}
1186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001187static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001188tooltip_redraw_handler(struct widget *widget, void *data)
1189{
1190 cairo_t *cr;
1191 const int32_t r = 3;
1192 struct tooltip *tooltip = data;
1193 int32_t width, height;
1194 struct window *window = widget->window;
1195
1196 cr = cairo_create(window->cairo_surface);
1197 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1198 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1199 cairo_paint(cr);
1200
1201 width = window->allocation.width;
1202 height = window->allocation.height;
1203 rounded_rect(cr, 0, 0, width, height, r);
1204
1205 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1206 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1207 cairo_fill(cr);
1208
1209 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1210 cairo_move_to(cr, 10, 16);
1211 cairo_show_text(cr, tooltip->entry);
1212 cairo_destroy(cr);
1213}
1214
1215static cairo_text_extents_t
1216get_text_extents(struct tooltip *tooltip)
1217{
1218 struct window *window;
1219 cairo_t *cr;
1220 cairo_text_extents_t extents;
1221
1222 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1223 * created yet */
1224 window = tooltip->widget->window->parent;
1225 cr = cairo_create(window->cairo_surface);
1226 cairo_text_extents(cr, tooltip->entry, &extents);
1227 cairo_destroy(cr);
1228
1229 return extents;
1230}
1231
1232static int
1233window_create_tooltip(struct tooltip *tooltip)
1234{
1235 struct widget *parent = tooltip->parent;
1236 struct display *display = parent->window->display;
1237 struct window *window;
1238 const int offset_y = 27;
1239 const int margin = 3;
1240 cairo_text_extents_t extents;
1241
1242 if (tooltip->widget)
1243 return 0;
1244
1245 window = window_create_transient(display, parent->window, tooltip->x,
1246 tooltip->y + offset_y,
1247 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1248 if (!window)
1249 return -1;
1250
1251 tooltip->window = window;
1252 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1253
1254 extents = get_text_extents(tooltip);
1255 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1256 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1257
1258 return 0;
1259}
1260
1261void
1262widget_destroy_tooltip(struct widget *parent)
1263{
1264 struct tooltip *tooltip = parent->tooltip;
1265
1266 parent->tooltip_count = 0;
1267 if (!tooltip)
1268 return;
1269
1270 if (tooltip->widget) {
1271 widget_destroy(tooltip->widget);
1272 window_destroy(tooltip->window);
1273 tooltip->widget = NULL;
1274 tooltip->window = NULL;
1275 }
1276
1277 close(tooltip->tooltip_fd);
1278 free(tooltip->entry);
1279 free(tooltip);
1280 parent->tooltip = NULL;
1281}
1282
1283static void
1284tooltip_func(struct task *task, uint32_t events)
1285{
1286 struct tooltip *tooltip =
1287 container_of(task, struct tooltip, tooltip_task);
1288 uint64_t exp;
1289
Martin Olsson8df662a2012-07-08 03:03:47 +02001290 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1291 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001292 window_create_tooltip(tooltip);
1293}
1294
1295#define TOOLTIP_TIMEOUT 500
1296static int
1297tooltip_timer_reset(struct tooltip *tooltip)
1298{
1299 struct itimerspec its;
1300
1301 its.it_interval.tv_sec = 0;
1302 its.it_interval.tv_nsec = 0;
1303 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1304 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1305 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1306 fprintf(stderr, "could not set timerfd\n: %m");
1307 return -1;
1308 }
1309
1310 return 0;
1311}
1312
1313int
1314widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1315{
1316 struct tooltip *tooltip = parent->tooltip;
1317
1318 parent->tooltip_count++;
1319 if (tooltip) {
1320 tooltip->x = x;
1321 tooltip->y = y;
1322 tooltip_timer_reset(tooltip);
1323 return 0;
1324 }
1325
1326 /* the handler might be triggered too fast via input device motion, so
1327 * we need this check here to make sure tooltip is fully initialized */
1328 if (parent->tooltip_count > 1)
1329 return 0;
1330
1331 tooltip = malloc(sizeof *tooltip);
1332 if (!tooltip)
1333 return -1;
1334
1335 parent->tooltip = tooltip;
1336 tooltip->parent = parent;
1337 tooltip->widget = NULL;
1338 tooltip->window = NULL;
1339 tooltip->x = x;
1340 tooltip->y = y;
1341 tooltip->entry = strdup(entry);
1342 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1343 if (tooltip->tooltip_fd < 0) {
1344 fprintf(stderr, "could not create timerfd\n: %m");
1345 return -1;
1346 }
1347
1348 tooltip->tooltip_task.run = tooltip_func;
1349 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1350 EPOLLIN, &tooltip->tooltip_task);
1351 tooltip_timer_reset(tooltip);
1352
1353 return 0;
1354}
1355
1356static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001357workspace_manager_state(void *data,
1358 struct workspace_manager *workspace_manager,
1359 uint32_t current,
1360 uint32_t count)
1361{
1362 struct display *display = data;
1363
1364 display->workspace = current;
1365 display->workspace_count = count;
1366}
1367
1368static const struct workspace_manager_listener workspace_manager_listener = {
1369 workspace_manager_state
1370};
1371
1372static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373frame_resize_handler(struct widget *widget,
1374 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001375{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001376 struct frame *frame = data;
1377 struct widget *child = frame->child;
1378 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001379 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001380 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001381 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001382 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001384 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001386 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001387 decoration_width = (t->width + t->margin) * 2;
1388 decoration_height = t->width +
1389 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001390
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001391 allocation.x = t->width + t->margin;
1392 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001393 allocation.width = width - decoration_width;
1394 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001395
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001396 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001397
1398 wl_list_for_each(button, &frame->buttons_list, link)
1399 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001400 } else {
1401 decoration_width = 0;
1402 decoration_height = 0;
1403
1404 allocation.x = 0;
1405 allocation.y = 0;
1406 allocation.width = width;
1407 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001408 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001409
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001412 }
1413
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001414 widget_set_allocation(child, allocation.x, allocation.y,
1415 allocation.width, allocation.height);
1416
1417 if (child->resize_handler)
1418 child->resize_handler(child,
1419 allocation.width,
1420 allocation.height,
1421 child->user_data);
1422
Scott Moreauf7e498c2012-05-14 11:39:29 -06001423 width = child->allocation.width + decoration_width;
1424 height = child->allocation.height + decoration_height;
1425
Kristian Høgsberg023be102012-07-31 11:59:12 -04001426 if (widget->window->type != TYPE_FULLSCREEN) {
1427 widget->window->input_region =
1428 wl_compositor_create_region(display->compositor);
1429 wl_region_add(widget->window->input_region,
1430 t->margin, t->margin,
1431 width - 2 * t->margin,
1432 height - 2 * t->margin);
1433 }
1434
Scott Moreauf7e498c2012-05-14 11:39:29 -06001435 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001436
1437 if (child->opaque) {
1438 widget->window->opaque_region =
1439 wl_compositor_create_region(display->compositor);
1440 wl_region_add(widget->window->opaque_region,
1441 opaque_margin, opaque_margin,
1442 widget->allocation.width - 2 * opaque_margin,
1443 widget->allocation.height - 2 * opaque_margin);
1444 }
Martin Minarik1998b152012-05-10 02:04:35 +02001445
1446 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001447 x_r = frame->widget->allocation.width - t->width - t->margin;
1448 x_l = t->width + t->margin;
1449 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001450 wl_list_for_each(button, &frame->buttons_list, link) {
1451 const int button_padding = 4;
1452 w = cairo_image_surface_get_width(button->icon);
1453 h = cairo_image_surface_get_height(button->icon);
1454
1455 if (button->decoration == FRAME_BUTTON_FANCY)
1456 w += 10;
1457
1458 if (button->align == FRAME_BUTTON_LEFT) {
1459 widget_set_allocation(button->widget,
1460 x_l, y , w + 1, h + 1);
1461 x_l += w;
1462 x_l += button_padding;
1463 } else {
1464 x_r -= w;
1465 widget_set_allocation(button->widget,
1466 x_r, y , w + 1, h + 1);
1467 x_r -= button_padding;
1468 }
1469 }
1470}
1471
1472static int
1473frame_button_enter_handler(struct widget *widget,
1474 struct input *input, float x, float y, void *data)
1475{
1476 struct frame_button *frame_button = data;
1477
1478 widget_schedule_redraw(frame_button->widget);
1479 frame_button->state = FRAME_BUTTON_OVER;
1480
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001481 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001482}
1483
1484static void
1485frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1486{
1487 struct frame_button *frame_button = data;
1488
1489 widget_schedule_redraw(frame_button->widget);
1490 frame_button->state = FRAME_BUTTON_DEFAULT;
1491}
1492
1493static void
1494frame_button_button_handler(struct widget *widget,
1495 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001496 uint32_t button,
1497 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001498{
1499 struct frame_button *frame_button = data;
1500 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001501 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001502
1503 if (button != BTN_LEFT)
1504 return;
1505
1506 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001507 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001508 frame_button->state = FRAME_BUTTON_ACTIVE;
1509 widget_schedule_redraw(frame_button->widget);
1510
1511 if (frame_button->type == FRAME_BUTTON_ICON)
1512 window_show_frame_menu(window, input, time);
1513 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001514 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001515 frame_button->state = FRAME_BUTTON_DEFAULT;
1516 widget_schedule_redraw(frame_button->widget);
1517 break;
1518 }
1519
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001520 if (!was_pressed)
1521 return;
1522
Martin Minarik1998b152012-05-10 02:04:35 +02001523 switch (frame_button->type) {
1524 case FRAME_BUTTON_CLOSE:
1525 if (window->close_handler)
1526 window->close_handler(window->parent,
1527 window->user_data);
1528 else
1529 display_exit(window->display);
1530 break;
1531 case FRAME_BUTTON_MINIMIZE:
1532 fprintf(stderr,"Minimize stub\n");
1533 break;
1534 case FRAME_BUTTON_MAXIMIZE:
1535 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1536 break;
1537 default:
1538 /* Unknown operation */
1539 break;
1540 }
1541}
1542
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001543static int
1544frame_button_motion_handler(struct widget *widget,
1545 struct input *input, uint32_t time,
1546 float x, float y, void *data)
1547{
1548 struct frame_button *frame_button = data;
1549 enum frame_button_pointer previous_button_state = frame_button->state;
1550
1551 /* only track state for a pressed button */
1552 if (input->grab != widget)
1553 return CURSOR_LEFT_PTR;
1554
1555 if (x > widget->allocation.x &&
1556 x < (widget->allocation.x + widget->allocation.width) &&
1557 y > widget->allocation.y &&
1558 y < (widget->allocation.y + widget->allocation.height)) {
1559 frame_button->state = FRAME_BUTTON_ACTIVE;
1560 } else {
1561 frame_button->state = FRAME_BUTTON_DEFAULT;
1562 }
1563
1564 if (frame_button->state != previous_button_state)
1565 widget_schedule_redraw(frame_button->widget);
1566
1567 return CURSOR_LEFT_PTR;
1568}
1569
Martin Minarik1998b152012-05-10 02:04:35 +02001570static void
1571frame_button_redraw_handler(struct widget *widget, void *data)
1572{
1573 struct frame_button *frame_button = data;
1574 cairo_t *cr;
1575 int width, height, x, y;
1576 struct window *window = widget->window;
1577
1578 x = widget->allocation.x;
1579 y = widget->allocation.y;
1580 width = widget->allocation.width;
1581 height = widget->allocation.height;
1582
1583 if (!width)
1584 return;
1585 if (!height)
1586 return;
1587 if (widget->opaque)
1588 return;
1589
1590 cr = cairo_create(window->cairo_surface);
1591
1592 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1593 cairo_set_line_width(cr, 1);
1594
1595 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1596 cairo_rectangle (cr, x, y, 25, 16);
1597
1598 cairo_stroke_preserve(cr);
1599
1600 switch (frame_button->state) {
1601 case FRAME_BUTTON_DEFAULT:
1602 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1603 break;
1604 case FRAME_BUTTON_OVER:
1605 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1606 break;
1607 case FRAME_BUTTON_ACTIVE:
1608 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1609 break;
1610 }
1611
1612 cairo_fill (cr);
1613
1614 x += 4;
1615 }
1616
1617 cairo_set_source_surface(cr, frame_button->icon, x, y);
1618 cairo_paint(cr);
1619
1620 cairo_destroy(cr);
1621}
1622
1623static struct widget *
1624frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1625 enum frame_button_align align, enum frame_button_decoration style)
1626{
1627 struct frame_button *frame_button;
1628 const char *icon = data;
1629
1630 frame_button = malloc (sizeof *frame_button);
1631 memset(frame_button, 0, sizeof *frame_button);
1632
1633 frame_button->icon = cairo_image_surface_create_from_png(icon);
1634 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1635 frame_button->frame = frame;
1636 frame_button->type = type;
1637 frame_button->align = align;
1638 frame_button->decoration = style;
1639
1640 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1641
1642 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1643 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1644 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1645 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001646 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001647 return frame_button->widget;
1648}
1649
1650static void
1651frame_button_destroy(struct frame_button *frame_button)
1652{
1653 widget_destroy(frame_button->widget);
1654 wl_list_remove(&frame_button->link);
1655 cairo_surface_destroy(frame_button->icon);
1656 free(frame_button);
1657
1658 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001659}
1660
1661static void
1662frame_redraw_handler(struct widget *widget, void *data)
1663{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001664 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001665 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001666 struct theme *t = window->display->theme;
1667 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001668
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001669 if (window->type == TYPE_FULLSCREEN)
1670 return;
1671
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001672 cr = cairo_create(window->cairo_surface);
1673
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001674 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001675 flags |= THEME_FRAME_ACTIVE;
1676 theme_render_frame(t, cr, widget->allocation.width,
1677 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001678
1679 cairo_destroy(cr);
1680}
1681
1682static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001683frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001684{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001685 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001686 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001687
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001688 location = theme_get_location(t, input->sx, input->sy,
1689 frame->widget->allocation.width,
1690 frame->widget->allocation.height);
1691
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001692 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001693 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001694 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001695 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001696 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001697 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001698 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001699 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001700 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001701 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001702 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001703 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001704 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001705 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001706 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001707 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001708 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001709 case THEME_LOCATION_EXTERIOR:
1710 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001711 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001712 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001713 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001714}
1715
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716static void
1717frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001718{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001719 struct display *display;
1720
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001721 switch (index) {
1722 case 0: /* close */
1723 if (window->close_handler)
1724 window->close_handler(window->parent,
1725 window->user_data);
1726 else
1727 display_exit(window->display);
1728 break;
1729 case 1: /* fullscreen */
1730 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001731 if (window->fullscreen_handler)
1732 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001733 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001734 case 2: /* move to workspace above */
1735 display = window->display;
1736 if (display->workspace > 0)
1737 workspace_manager_move_surface(display->workspace_manager,
1738 window->surface,
1739 display->workspace - 1);
1740 break;
1741 case 3: /* move to workspace below */
1742 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001743 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001744 workspace_manager_move_surface(display->workspace_manager,
1745 window->surface,
1746 display->workspace + 1);
1747 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001748 }
1749}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001750
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001751void
1752window_show_frame_menu(struct window *window,
1753 struct input *input, uint32_t time)
1754{
1755 int32_t x, y;
1756
1757 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001758 "Close", "Fullscreen",
1759 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001760 };
1761
1762 input_get_position(input, &x, &y);
1763 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001764 x - 10, y - 10, frame_menu_func, entries,
1765 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001766}
1767
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001768static int
1769frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001770 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001771{
1772 return frame_get_pointer_image_for_location(data, input);
1773}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001774
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001775static int
1776frame_motion_handler(struct widget *widget,
1777 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001778 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001779{
1780 return frame_get_pointer_image_for_location(data, input);
1781}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001782
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001783static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001784frame_button_handler(struct widget *widget,
1785 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001786 uint32_t button, enum wl_pointer_button_state state,
1787 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001788
1789{
1790 struct frame *frame = data;
1791 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001792 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001793 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001794
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001795 location = theme_get_location(display->theme, input->sx, input->sy,
1796 frame->widget->allocation.width,
1797 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798
Daniel Stone4dbadb12012-05-30 16:31:51 +01001799 if (window->display->shell && button == BTN_LEFT &&
1800 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001801 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001802 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001803 if (!window->shell_surface)
1804 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001805 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001807 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001808 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001809 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001810 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001811 case THEME_LOCATION_RESIZING_TOP:
1812 case THEME_LOCATION_RESIZING_BOTTOM:
1813 case THEME_LOCATION_RESIZING_LEFT:
1814 case THEME_LOCATION_RESIZING_RIGHT:
1815 case THEME_LOCATION_RESIZING_TOP_LEFT:
1816 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1817 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1818 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001819 if (!window->shell_surface)
1820 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001821 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001822
1823 if (!display->dpy) {
1824 /* If we're using shm, allocate a big
1825 pool to create buffers out of while
1826 we resize. We should probably base
1827 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001828 window->pool =
1829 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001830 }
1831
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001832 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001833 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001834 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001835 break;
1836 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001837 } else if (button == BTN_RIGHT &&
1838 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001839 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001840 }
1841}
1842
1843struct widget *
1844frame_create(struct window *window, void *data)
1845{
1846 struct frame *frame;
1847
1848 frame = malloc(sizeof *frame);
1849 memset(frame, 0, sizeof *frame);
1850
1851 frame->widget = window_add_widget(window, frame);
1852 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001853
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001854 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1855 widget_set_resize_handler(frame->widget, frame_resize_handler);
1856 widget_set_enter_handler(frame->widget, frame_enter_handler);
1857 widget_set_motion_handler(frame->widget, frame_motion_handler);
1858 widget_set_button_handler(frame->widget, frame_button_handler);
1859
Martin Minarik1998b152012-05-10 02:04:35 +02001860 /* Create empty list for frame buttons */
1861 wl_list_init(&frame->buttons_list);
1862
1863 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1864 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1865
1866 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1867 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1868
1869 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1870 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1871
1872 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1873 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1874
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001875 window->frame = frame;
1876
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001877 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001878}
1879
Kristian Høgsberga1627922012-06-20 17:30:03 -04001880void
1881frame_set_child_size(struct widget *widget, int child_width, int child_height)
1882{
1883 struct display *display = widget->window->display;
1884 struct theme *t = display->theme;
1885 int decoration_width, decoration_height;
1886 int width, height;
1887
1888 if (widget->window->type != TYPE_FULLSCREEN) {
1889 decoration_width = (t->width + t->margin) * 2;
1890 decoration_height = t->width +
1891 t->titlebar_height + t->margin * 2;
1892
1893 width = child_width + decoration_width;
1894 height = child_height + decoration_height;
1895 } else {
1896 width = child_width;
1897 height = child_height;
1898 }
1899
1900 window_schedule_resize(widget->window, width, height);
1901}
1902
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001903static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001904frame_destroy(struct frame *frame)
1905{
Martin Minarik1998b152012-05-10 02:04:35 +02001906 struct frame_button *button, *tmp;
1907
1908 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1909 frame_button_destroy(button);
1910
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001911 /* frame->child must be destroyed by the application */
1912 widget_destroy(frame->widget);
1913 free(frame);
1914}
1915
1916static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001917input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001918 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001919{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001920 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001921 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001922
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001923 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001924 return;
1925
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001926 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001927 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001928 widget = old;
1929 if (input->grab)
1930 widget = input->grab;
1931 if (widget->leave_handler)
1932 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001933 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001934 }
1935
1936 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001937 widget = focus;
1938 if (input->grab)
1939 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001940 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001941 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001942 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001943 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001944
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001945 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001946 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001947}
1948
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001949void
1950input_grab(struct input *input, struct widget *widget, uint32_t button)
1951{
1952 input->grab = widget;
1953 input->grab_button = button;
1954}
1955
1956void
1957input_ungrab(struct input *input)
1958{
1959 struct widget *widget;
1960
1961 input->grab = NULL;
1962 if (input->pointer_focus) {
1963 widget = widget_find_widget(input->pointer_focus->widget,
1964 input->sx, input->sy);
1965 input_set_focus_widget(input, widget, input->sx, input->sy);
1966 }
1967}
1968
1969static void
1970input_remove_pointer_focus(struct input *input)
1971{
1972 struct window *window = input->pointer_focus;
1973
1974 if (!window)
1975 return;
1976
1977 input_set_focus_widget(input, NULL, 0, 0);
1978
1979 input->pointer_focus = NULL;
1980 input->current_cursor = CURSOR_UNSET;
1981}
1982
1983static void
1984pointer_handle_enter(void *data, struct wl_pointer *pointer,
1985 uint32_t serial, struct wl_surface *surface,
1986 wl_fixed_t sx_w, wl_fixed_t sy_w)
1987{
1988 struct input *input = data;
1989 struct window *window;
1990 struct widget *widget;
1991 float sx = wl_fixed_to_double(sx_w);
1992 float sy = wl_fixed_to_double(sy_w);
1993
1994 if (!surface) {
1995 /* enter event for a window we've just destroyed */
1996 return;
1997 }
1998
1999 input->display->serial = serial;
2000 input->pointer_enter_serial = serial;
2001 input->pointer_focus = wl_surface_get_user_data(surface);
2002 window = input->pointer_focus;
2003
2004 if (window->pool) {
2005 shm_pool_destroy(window->pool);
2006 window->pool = NULL;
2007 /* Schedule a redraw to free the pool */
2008 window_schedule_redraw(window);
2009 }
2010
2011 input->sx = sx;
2012 input->sy = sy;
2013
2014 widget = widget_find_widget(window->widget, sx, sy);
2015 input_set_focus_widget(input, widget, sx, sy);
2016}
2017
2018static void
2019pointer_handle_leave(void *data, struct wl_pointer *pointer,
2020 uint32_t serial, struct wl_surface *surface)
2021{
2022 struct input *input = data;
2023
2024 input->display->serial = serial;
2025 input_remove_pointer_focus(input);
2026}
2027
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002028static void
Daniel Stone37816df2012-05-16 18:45:18 +01002029pointer_handle_motion(void *data, struct wl_pointer *pointer,
2030 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002031{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002032 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002033 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002034 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002035 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002036 float sx = wl_fixed_to_double(sx_w);
2037 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002038
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002039 input->sx = sx;
2040 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002041
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002042 if (!window)
2043 return;
2044
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002045 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002046 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002047 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002048 }
2049
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002050 if (input->grab)
2051 widget = input->grab;
2052 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002053 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002054 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002055 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002056 input, time, sx, sy,
2057 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002058
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002059 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002060}
2061
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002062static void
Daniel Stone37816df2012-05-16 18:45:18 +01002063pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002064 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002065{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002066 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002067 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002068 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002069
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002070 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002071 if (input->focus_widget && input->grab == NULL &&
2072 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002073 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002074
Neil Roberts6b28aad2012-01-23 19:11:18 +00002075 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002076 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002077 (*widget->button_handler)(widget,
2078 input, time,
2079 button, state,
2080 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002081
Daniel Stone4dbadb12012-05-30 16:31:51 +01002082 if (input->grab && input->grab_button == button &&
2083 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002084 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002085}
2086
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002087static void
Daniel Stone37816df2012-05-16 18:45:18 +01002088pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002089 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002090{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002091 struct input *input = data;
2092 struct widget *widget;
2093
2094 widget = input->focus_widget;
2095 if (input->grab)
2096 widget = input->grab;
2097 if (widget && widget->axis_handler)
2098 (*widget->axis_handler)(widget,
2099 input, time,
2100 axis, value,
2101 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002102}
2103
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002104static const struct wl_pointer_listener pointer_listener = {
2105 pointer_handle_enter,
2106 pointer_handle_leave,
2107 pointer_handle_motion,
2108 pointer_handle_button,
2109 pointer_handle_axis,
2110};
2111
2112static void
2113input_remove_keyboard_focus(struct input *input)
2114{
2115 struct window *window = input->keyboard_focus;
2116 struct itimerspec its;
2117
2118 its.it_interval.tv_sec = 0;
2119 its.it_interval.tv_nsec = 0;
2120 its.it_value.tv_sec = 0;
2121 its.it_value.tv_nsec = 0;
2122 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2123
2124 if (!window)
2125 return;
2126
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002127 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002128 if (window->keyboard_focus_handler)
2129 (*window->keyboard_focus_handler)(window, NULL,
2130 window->user_data);
2131
2132 input->keyboard_focus = NULL;
2133}
2134
2135static void
2136keyboard_repeat_func(struct task *task, uint32_t events)
2137{
2138 struct input *input =
2139 container_of(task, struct input, repeat_task);
2140 struct window *window = input->keyboard_focus;
2141 uint64_t exp;
2142
2143 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2144 /* If we change the timer between the fd becoming
2145 * readable and getting here, there'll be nothing to
2146 * read and we get EAGAIN. */
2147 return;
2148
2149 if (window && window->key_handler) {
2150 (*window->key_handler)(window, input, input->repeat_time,
2151 input->repeat_key, input->repeat_sym,
2152 WL_KEYBOARD_KEY_STATE_PRESSED,
2153 window->user_data);
2154 }
2155}
2156
2157static void
2158keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2159 uint32_t format, int fd, uint32_t size)
2160{
2161 struct input *input = data;
2162 char *map_str;
2163
2164 if (!data) {
2165 close(fd);
2166 return;
2167 }
2168
2169 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2170 close(fd);
2171 return;
2172 }
2173
2174 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2175 if (map_str == MAP_FAILED) {
2176 close(fd);
2177 return;
2178 }
2179
2180 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2181 map_str,
2182 XKB_KEYMAP_FORMAT_TEXT_V1,
2183 0);
2184 munmap(map_str, size);
2185 close(fd);
2186
2187 if (!input->xkb.keymap) {
2188 fprintf(stderr, "failed to compile keymap\n");
2189 return;
2190 }
2191
2192 input->xkb.state = xkb_state_new(input->xkb.keymap);
2193 if (!input->xkb.state) {
2194 fprintf(stderr, "failed to create XKB state\n");
2195 xkb_map_unref(input->xkb.keymap);
2196 input->xkb.keymap = NULL;
2197 return;
2198 }
2199
2200 input->xkb.control_mask =
2201 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2202 input->xkb.alt_mask =
2203 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2204 input->xkb.shift_mask =
2205 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2206}
2207
2208static void
2209keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2210 uint32_t serial, struct wl_surface *surface,
2211 struct wl_array *keys)
2212{
2213 struct input *input = data;
2214 struct window *window;
2215
2216 input->display->serial = serial;
2217 input->keyboard_focus = wl_surface_get_user_data(surface);
2218
2219 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002220 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002221 if (window->keyboard_focus_handler)
2222 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002223 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002224}
2225
2226static void
2227keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2228 uint32_t serial, struct wl_surface *surface)
2229{
2230 struct input *input = data;
2231
2232 input->display->serial = serial;
2233 input_remove_keyboard_focus(input);
2234}
2235
Scott Moreau210d0792012-03-22 10:47:01 -06002236static void
Daniel Stone37816df2012-05-16 18:45:18 +01002237keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002238 uint32_t serial, uint32_t time, uint32_t key,
2239 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002240{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002241 struct input *input = data;
2242 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002243 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002244 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002245 const xkb_keysym_t *syms;
2246 xkb_keysym_t sym;
2247 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002248 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002249
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002250 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002251 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002252 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002253 return;
2254
Daniel Stone97f68542012-05-30 16:32:01 +01002255 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002256
Daniel Stone97f68542012-05-30 16:32:01 +01002257 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002258 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002259 XKB_STATE_LATCHED);
2260 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002261 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002262 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002263 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002264 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002265 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002266 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002267
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002268 sym = XKB_KEY_NoSymbol;
2269 if (num_syms == 1)
2270 sym = syms[0];
2271
2272 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002273 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002274 window_set_maximized(window,
2275 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002276 } else if (sym == XKB_KEY_F11 &&
2277 window->fullscreen_handler &&
2278 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2279 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002280 } else if (sym == XKB_KEY_F4 &&
2281 input->modifiers == MOD_ALT_MASK &&
2282 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2283 if (window->close_handler)
2284 window->close_handler(window->parent,
2285 window->user_data);
2286 else
2287 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002288 } else if (window->key_handler) {
2289 (*window->key_handler)(window, input, time, key,
2290 sym, state, window->user_data);
2291 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002292
2293 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2294 key == input->repeat_key) {
2295 its.it_interval.tv_sec = 0;
2296 its.it_interval.tv_nsec = 0;
2297 its.it_value.tv_sec = 0;
2298 its.it_value.tv_nsec = 0;
2299 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2300 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2301 input->repeat_sym = sym;
2302 input->repeat_key = key;
2303 input->repeat_time = time;
2304 its.it_interval.tv_sec = 0;
2305 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2306 its.it_value.tv_sec = 0;
2307 its.it_value.tv_nsec = 400 * 1000 * 1000;
2308 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2309 }
2310}
2311
2312static void
Daniel Stone351eb612012-05-31 15:27:47 -04002313keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2314 uint32_t serial, uint32_t mods_depressed,
2315 uint32_t mods_latched, uint32_t mods_locked,
2316 uint32_t group)
2317{
2318 struct input *input = data;
2319
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002320 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2321 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002322}
2323
Daniel Stone37816df2012-05-16 18:45:18 +01002324static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002325 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002326 keyboard_handle_enter,
2327 keyboard_handle_leave,
2328 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002329 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002330};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002331
2332static void
Daniel Stone37816df2012-05-16 18:45:18 +01002333seat_handle_capabilities(void *data, struct wl_seat *seat,
2334 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002335{
Daniel Stone37816df2012-05-16 18:45:18 +01002336 struct input *input = data;
2337
2338 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2339 input->pointer = wl_seat_get_pointer(seat);
2340 wl_pointer_set_user_data(input->pointer, input);
2341 wl_pointer_add_listener(input->pointer, &pointer_listener,
2342 input);
2343 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2344 wl_pointer_destroy(input->pointer);
2345 input->pointer = NULL;
2346 }
2347
2348 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2349 input->keyboard = wl_seat_get_keyboard(seat);
2350 wl_keyboard_set_user_data(input->keyboard, input);
2351 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2352 input);
2353 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2354 wl_keyboard_destroy(input->keyboard);
2355 input->keyboard = NULL;
2356 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002357}
2358
Daniel Stone37816df2012-05-16 18:45:18 +01002359static const struct wl_seat_listener seat_listener = {
2360 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002361};
2362
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002363void
2364input_get_position(struct input *input, int32_t *x, int32_t *y)
2365{
2366 *x = input->sx;
2367 *y = input->sy;
2368}
2369
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002370struct display *
2371input_get_display(struct input *input)
2372{
2373 return input->display;
2374}
2375
Daniel Stone37816df2012-05-16 18:45:18 +01002376struct wl_seat *
2377input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002378{
Daniel Stone37816df2012-05-16 18:45:18 +01002379 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002380}
2381
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002382uint32_t
2383input_get_modifiers(struct input *input)
2384{
2385 return input->modifiers;
2386}
2387
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002388struct widget *
2389input_get_focus_widget(struct input *input)
2390{
2391 return input->focus_widget;
2392}
2393
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002394struct data_offer {
2395 struct wl_data_offer *offer;
2396 struct input *input;
2397 struct wl_array types;
2398 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002399
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002400 struct task io_task;
2401 int fd;
2402 data_func_t func;
2403 int32_t x, y;
2404 void *user_data;
2405};
2406
2407static void
2408data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2409{
2410 struct data_offer *offer = data;
2411 char **p;
2412
2413 p = wl_array_add(&offer->types, sizeof *p);
2414 *p = strdup(type);
2415}
2416
2417static const struct wl_data_offer_listener data_offer_listener = {
2418 data_offer_offer,
2419};
2420
2421static void
2422data_offer_destroy(struct data_offer *offer)
2423{
2424 char **p;
2425
2426 offer->refcount--;
2427 if (offer->refcount == 0) {
2428 wl_data_offer_destroy(offer->offer);
2429 for (p = offer->types.data; *p; p++)
2430 free(*p);
2431 wl_array_release(&offer->types);
2432 free(offer);
2433 }
2434}
2435
2436static void
2437data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002438 struct wl_data_device *data_device,
2439 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002440{
2441 struct data_offer *offer;
2442
2443 offer = malloc(sizeof *offer);
2444
2445 wl_array_init(&offer->types);
2446 offer->refcount = 1;
2447 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002448 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002449 wl_data_offer_add_listener(offer->offer,
2450 &data_offer_listener, offer);
2451}
2452
2453static void
2454data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002455 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002456 wl_fixed_t x_w, wl_fixed_t y_w,
2457 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002458{
2459 struct input *input = data;
2460 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002461 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002462 float x = wl_fixed_to_double(x_w);
2463 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002464 char **p;
2465
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002466 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002467 window = wl_surface_get_user_data(surface);
2468 input->pointer_focus = window;
2469
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002470 if (offer) {
2471 input->drag_offer = wl_data_offer_get_user_data(offer);
2472
2473 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2474 *p = NULL;
2475
2476 types_data = input->drag_offer->types.data;
2477 } else {
2478 input->drag_offer = NULL;
2479 types_data = NULL;
2480 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002481
2482 window = input->pointer_focus;
2483 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002484 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002485 window->user_data);
2486}
2487
2488static void
2489data_device_leave(void *data, struct wl_data_device *data_device)
2490{
2491 struct input *input = data;
2492
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002493 if (input->drag_offer) {
2494 data_offer_destroy(input->drag_offer);
2495 input->drag_offer = NULL;
2496 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002497}
2498
2499static void
2500data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002501 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002502{
2503 struct input *input = data;
2504 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002505 float x = wl_fixed_to_double(x_w);
2506 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002507 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002508
2509 input->sx = x;
2510 input->sy = y;
2511
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002512 if (input->drag_offer)
2513 types_data = input->drag_offer->types.data;
2514 else
2515 types_data = NULL;
2516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002517 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002518 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002519 window->user_data);
2520}
2521
2522static void
2523data_device_drop(void *data, struct wl_data_device *data_device)
2524{
2525 struct input *input = data;
2526 struct window *window = input->pointer_focus;
2527
2528 if (window->drop_handler)
2529 window->drop_handler(window, input,
2530 input->sx, input->sy, window->user_data);
2531}
2532
2533static void
2534data_device_selection(void *data,
2535 struct wl_data_device *wl_data_device,
2536 struct wl_data_offer *offer)
2537{
2538 struct input *input = data;
2539 char **p;
2540
2541 if (input->selection_offer)
2542 data_offer_destroy(input->selection_offer);
2543
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002544 if (offer) {
2545 input->selection_offer = wl_data_offer_get_user_data(offer);
2546 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2547 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002548 } else {
2549 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002550 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002551}
2552
2553static const struct wl_data_device_listener data_device_listener = {
2554 data_device_data_offer,
2555 data_device_enter,
2556 data_device_leave,
2557 data_device_motion,
2558 data_device_drop,
2559 data_device_selection
2560};
2561
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002562static void
2563input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002564{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002566 struct wl_cursor *cursor;
2567 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002568
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002569 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002570 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002571 return;
2572
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002573 if (index >= (int) cursor->image_count) {
2574 fprintf(stderr, "cursor index out of range\n");
2575 return;
2576 }
2577
2578 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002579 buffer = wl_cursor_image_get_buffer(image);
2580 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002581 return;
2582
Kristian Høgsbergae277372012-08-01 09:41:08 -04002583 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002584 input->pointer_surface,
2585 image->hotspot_x, image->hotspot_y);
2586 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2587 wl_surface_damage(input->pointer_surface, 0, 0,
2588 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002589}
2590
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002591static const struct wl_callback_listener pointer_surface_listener;
2592
2593static void
2594pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2595 uint32_t time)
2596{
2597 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002598 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002599 int i;
2600
2601 if (callback) {
2602 assert(callback == input->cursor_frame_cb);
2603 wl_callback_destroy(callback);
2604 input->cursor_frame_cb = NULL;
2605 }
2606
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002607 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002608 wl_pointer_set_cursor(input->pointer,
2609 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002610 NULL, 0, 0);
2611 return;
2612 }
2613
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002614 if (input->current_cursor == CURSOR_UNSET)
2615 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002616 cursor = input->display->cursors[input->current_cursor];
2617 if (!cursor)
2618 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002619
2620 /* FIXME We don't have the current time on the first call so we set
2621 * the animation start to the time of the first frame callback. */
2622 if (time == 0)
2623 input->cursor_anim_start = 0;
2624 else if (input->cursor_anim_start == 0)
2625 input->cursor_anim_start = time;
2626
2627 if (time == 0 || input->cursor_anim_start == 0)
2628 i = 0;
2629 else
2630 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2631
2632 input_set_pointer_image_index(input, i);
2633
2634 if (cursor->image_count == 1)
2635 return;
2636
2637 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2638 wl_callback_add_listener(input->cursor_frame_cb,
2639 &pointer_surface_listener, input);
2640}
2641
2642static const struct wl_callback_listener pointer_surface_listener = {
2643 pointer_surface_frame_callback
2644};
2645
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002646void
2647input_set_pointer_image(struct input *input, int pointer)
2648{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002649 int force = 0;
2650
2651 if (input->pointer_enter_serial > input->cursor_serial)
2652 force = 1;
2653
2654 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002655 return;
2656
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002657 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002658 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002659 if (!input->cursor_frame_cb)
2660 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002661 else if (force) {
2662 /* The current frame callback may be stuck if, for instance,
2663 * the set cursor request was processed by the server after
2664 * this client lost the focus. In this case the cursor surface
2665 * might not be mapped and the frame callback wouldn't ever
2666 * complete. Send a set_cursor and attach to try to map the
2667 * cursor surface again so that the callback will finish */
2668 input_set_pointer_image_index(input, 0);
2669 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002670}
2671
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002672struct wl_data_device *
2673input_get_data_device(struct input *input)
2674{
2675 return input->data_device;
2676}
2677
2678void
2679input_set_selection(struct input *input,
2680 struct wl_data_source *source, uint32_t time)
2681{
2682 wl_data_device_set_selection(input->data_device, source, time);
2683}
2684
2685void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002686input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002687{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002688 wl_data_offer_accept(input->drag_offer->offer,
2689 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002690}
2691
2692static void
2693offer_io_func(struct task *task, uint32_t events)
2694{
2695 struct data_offer *offer =
2696 container_of(task, struct data_offer, io_task);
2697 unsigned int len;
2698 char buffer[4096];
2699
2700 len = read(offer->fd, buffer, sizeof buffer);
2701 offer->func(buffer, len,
2702 offer->x, offer->y, offer->user_data);
2703
2704 if (len == 0) {
2705 close(offer->fd);
2706 data_offer_destroy(offer);
2707 }
2708}
2709
2710static void
2711data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2712 data_func_t func, void *user_data)
2713{
2714 int p[2];
2715
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002716 if (pipe2(p, O_CLOEXEC) == -1)
2717 return;
2718
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002719 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2720 close(p[1]);
2721
2722 offer->io_task.run = offer_io_func;
2723 offer->fd = p[0];
2724 offer->func = func;
2725 offer->refcount++;
2726 offer->user_data = user_data;
2727
2728 display_watch_fd(offer->input->display,
2729 offer->fd, EPOLLIN, &offer->io_task);
2730}
2731
2732void
2733input_receive_drag_data(struct input *input, const char *mime_type,
2734 data_func_t func, void *data)
2735{
2736 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2737 input->drag_offer->x = input->sx;
2738 input->drag_offer->y = input->sy;
2739}
2740
2741int
2742input_receive_selection_data(struct input *input, const char *mime_type,
2743 data_func_t func, void *data)
2744{
2745 char **p;
2746
2747 if (input->selection_offer == NULL)
2748 return -1;
2749
2750 for (p = input->selection_offer->types.data; *p; p++)
2751 if (strcmp(mime_type, *p) == 0)
2752 break;
2753
2754 if (*p == NULL)
2755 return -1;
2756
2757 data_offer_receive_data(input->selection_offer,
2758 mime_type, func, data);
2759 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002760}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002761
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002762int
2763input_receive_selection_data_to_fd(struct input *input,
2764 const char *mime_type, int fd)
2765{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002766 if (input->selection_offer)
2767 wl_data_offer_receive(input->selection_offer->offer,
2768 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002769
2770 return 0;
2771}
2772
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002773void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002774window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002775{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002776 if (!window->shell_surface)
2777 return;
2778
Daniel Stone37816df2012-05-16 18:45:18 +01002779 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002780}
2781
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002782static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002783idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002784{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002785 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002786
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002787 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002788 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002789 widget_set_allocation(widget,
2790 window->pending_allocation.x,
2791 window->pending_allocation.y,
2792 window->pending_allocation.width,
2793 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002794
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002795 if (window->input_region) {
2796 wl_region_destroy(window->input_region);
2797 window->input_region = NULL;
2798 }
2799
2800 if (window->opaque_region) {
2801 wl_region_destroy(window->opaque_region);
2802 window->opaque_region = NULL;
2803 }
2804
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002805 if (widget->resize_handler)
2806 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002807 widget->allocation.width,
2808 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002809 widget->user_data);
2810
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002811 if (window->allocation.width != widget->allocation.width ||
2812 window->allocation.height != widget->allocation.height) {
2813 window->allocation = widget->allocation;
2814 window_schedule_redraw(window);
2815 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002816}
2817
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002818void
2819window_schedule_resize(struct window *window, int width, int height)
2820{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002821 window->pending_allocation.x = 0;
2822 window->pending_allocation.y = 0;
2823 window->pending_allocation.width = width;
2824 window->pending_allocation.height = height;
2825
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002826 if (window->min_allocation.width == 0)
2827 window->min_allocation = window->pending_allocation;
2828 if (window->pending_allocation.width < window->min_allocation.width)
2829 window->pending_allocation.width = window->min_allocation.width;
2830 if (window->pending_allocation.height < window->min_allocation.height)
2831 window->pending_allocation.height = window->min_allocation.height;
2832
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002833 window->resize_needed = 1;
2834 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002835}
2836
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002837void
2838widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2839{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002840 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002841}
2842
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002843static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002844handle_ping(void *data, struct wl_shell_surface *shell_surface,
2845 uint32_t serial)
2846{
2847 wl_shell_surface_pong(shell_surface, serial);
2848}
2849
2850static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002851handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002852 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002853{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002854 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002855
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002856 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002857 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002858}
2859
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002860static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002861menu_destroy(struct menu *menu)
2862{
2863 widget_destroy(menu->widget);
2864 window_destroy(menu->window);
2865 free(menu);
2866}
2867
2868static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002869handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2870{
2871 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002872 struct menu *menu = window->widget->user_data;
2873
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002874 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002875 * device. Or just use wl_callback. And this really needs to
2876 * be a window vfunc that the menu can set. And we need the
2877 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002878
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002879 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002880 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002881 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002882}
2883
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002884static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002885 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002886 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002887 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002888};
2889
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002890void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002891window_get_allocation(struct window *window,
2892 struct rectangle *allocation)
2893{
2894 *allocation = window->allocation;
2895}
2896
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002897static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002898widget_redraw(struct widget *widget)
2899{
2900 struct widget *child;
2901
2902 if (widget->redraw_handler)
2903 widget->redraw_handler(widget, widget->user_data);
2904 wl_list_for_each(child, &widget->child_list, link)
2905 widget_redraw(child);
2906}
2907
2908static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002909frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2910{
2911 struct window *window = data;
2912
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002913 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002914 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002915 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002916 window->redraw_scheduled = 0;
2917 if (window->redraw_needed)
2918 window_schedule_redraw(window);
2919}
2920
2921static const struct wl_callback_listener listener = {
2922 frame_callback
2923};
2924
2925static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002926idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002927{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002928 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002929
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002930 if (window->resize_needed)
2931 idle_resize(window);
2932
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002933 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002934 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002935 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002936 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002937 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002938
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002939 window->frame_cb = wl_surface_frame(window->surface);
2940 wl_callback_add_listener(window->frame_cb, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002941}
2942
2943void
2944window_schedule_redraw(struct window *window)
2945{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002946 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002947 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002948 window->redraw_task.run = idle_redraw;
2949 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002950 window->redraw_scheduled = 1;
2951 }
2952}
2953
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002954void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002955window_set_fullscreen(struct window *window, int fullscreen)
2956{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002957 if (!window->display->shell)
2958 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002959
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002960 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002961 return;
2962
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002963 if (fullscreen) {
2964 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002965 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002966 wl_shell_surface_set_fullscreen(window->shell_surface,
2967 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2968 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002969 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002970 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002971 wl_shell_surface_set_toplevel(window->shell_surface);
2972 window_schedule_resize(window,
2973 window->saved_allocation.width,
2974 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002975 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002976}
2977
2978void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002979window_set_maximized(struct window *window, int maximized)
2980{
2981 if (!window->display->shell)
2982 return;
2983
2984 if ((window->type == TYPE_MAXIMIZED) == maximized)
2985 return;
2986
2987 if (window->type == TYPE_TOPLEVEL) {
2988 window->saved_allocation = window->allocation;
2989 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2990 window->type = TYPE_MAXIMIZED;
2991 } else {
2992 wl_shell_surface_set_toplevel(window->shell_surface);
2993 window->type = TYPE_TOPLEVEL;
2994 window_schedule_resize(window,
2995 window->saved_allocation.width,
2996 window->saved_allocation.height);
2997 }
2998}
2999
3000void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003001window_set_user_data(struct window *window, void *data)
3002{
3003 window->user_data = data;
3004}
3005
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003006void *
3007window_get_user_data(struct window *window)
3008{
3009 return window->user_data;
3010}
3011
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003012void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003013window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003014 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003015{
3016 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003017}
3018
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003019void
3020window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003021 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003022{
3023 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003024}
3025
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003026void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003027window_set_data_handler(struct window *window, window_data_handler_t handler)
3028{
3029 window->data_handler = handler;
3030}
3031
3032void
3033window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3034{
3035 window->drop_handler = handler;
3036}
3037
3038void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003039window_set_close_handler(struct window *window,
3040 window_close_handler_t handler)
3041{
3042 window->close_handler = handler;
3043}
3044
3045void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003046window_set_fullscreen_handler(struct window *window,
3047 window_fullscreen_handler_t handler)
3048{
3049 window->fullscreen_handler = handler;
3050}
3051
3052void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003053window_set_title(struct window *window, const char *title)
3054{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003055 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003056 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003057 if (window->shell_surface)
3058 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003059}
3060
3061const char *
3062window_get_title(struct window *window)
3063{
3064 return window->title;
3065}
3066
3067void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003068window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3069{
3070 struct text_cursor_position *text_cursor_position =
3071 window->display->text_cursor_position;
3072
Scott Moreau9295ce02012-06-01 12:46:10 -06003073 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003074 return;
3075
3076 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003077 window->surface,
3078 wl_fixed_from_int(x),
3079 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003080}
3081
3082void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003083window_damage(struct window *window, int32_t x, int32_t y,
3084 int32_t width, int32_t height)
3085{
3086 wl_surface_damage(window->surface, x, y, width, height);
3087}
3088
Casey Dahlin9074db52012-04-19 22:50:09 -04003089static void
3090surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003091 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003092{
Rob Bradford7507b572012-05-15 17:55:34 +01003093 struct window *window = data;
3094 struct output *output;
3095 struct output *output_found = NULL;
3096 struct window_output *window_output;
3097
3098 wl_list_for_each(output, &window->display->output_list, link) {
3099 if (output->output == wl_output) {
3100 output_found = output;
3101 break;
3102 }
3103 }
3104
3105 if (!output_found)
3106 return;
3107
3108 window_output = malloc (sizeof *window_output);
3109 window_output->output = output_found;
3110
3111 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003112}
3113
3114static void
3115surface_leave(void *data,
3116 struct wl_surface *wl_surface, struct wl_output *output)
3117{
Rob Bradford7507b572012-05-15 17:55:34 +01003118 struct window *window = data;
3119 struct window_output *window_output;
3120 struct window_output *window_output_found = NULL;
3121
3122 wl_list_for_each(window_output, &window->window_output_list, link) {
3123 if (window_output->output->output == output) {
3124 window_output_found = window_output;
3125 break;
3126 }
3127 }
3128
3129 if (window_output_found) {
3130 wl_list_remove(&window_output_found->link);
3131 free(window_output_found);
3132 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003133}
3134
3135static const struct wl_surface_listener surface_listener = {
3136 surface_enter,
3137 surface_leave
3138};
3139
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003140static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003141window_create_internal(struct display *display,
3142 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003143{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003144 struct window *window;
3145
3146 window = malloc(sizeof *window);
3147 if (window == NULL)
3148 return NULL;
3149
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003150 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003151 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003152 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003153 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003154 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003155 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003156 window->shell_surface =
3157 wl_shell_get_shell_surface(display->shell,
3158 window->surface);
3159 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003160 window->allocation.x = 0;
3161 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003162 window->allocation.width = 0;
3163 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003164 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003165 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003166 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003167 window->input_region = NULL;
3168 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003169
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003170 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003171#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003172 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003173#else
3174 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3175#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003176 else
3177 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003178
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003179 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003180 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003181 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003182
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003183 if (window->shell_surface) {
3184 wl_shell_surface_set_user_data(window->shell_surface, window);
3185 wl_shell_surface_add_listener(window->shell_surface,
3186 &shell_surface_listener, window);
3187 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003188
Rob Bradford7507b572012-05-15 17:55:34 +01003189 wl_list_init (&window->window_output_list);
3190
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003191 return window;
3192}
3193
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003194struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003195window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003196{
3197 struct window *window;
3198
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003199 window = window_create_internal(display, NULL, TYPE_NONE);
3200 if (!window)
3201 return NULL;
3202
3203 return window;
3204}
3205
3206struct window *
3207window_create_custom(struct display *display)
3208{
3209 struct window *window;
3210
3211 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003212 if (!window)
3213 return NULL;
3214
3215 return window;
3216}
3217
3218struct window *
3219window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003220 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003221{
3222 struct window *window;
3223
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003224 window = window_create_internal(parent->display,
3225 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003226 if (!window)
3227 return NULL;
3228
3229 window->x = x;
3230 window->y = y;
3231
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003232 if (display->shell)
3233 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003234 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003235 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003236
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003237 return window;
3238}
3239
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003240static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003241menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003242{
3243 int next;
3244
3245 next = (sy - 8) / 20;
3246 if (menu->current != next) {
3247 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003248 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003249 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003250}
3251
3252static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003253menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003254 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003255 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003256{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003257 struct menu *menu = data;
3258
3259 if (widget == menu->widget)
3260 menu_set_item(data, y);
3261
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003262 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003263}
3264
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003265static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003266menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003267 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003268{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003269 struct menu *menu = data;
3270
3271 if (widget == menu->widget)
3272 menu_set_item(data, y);
3273
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003274 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003275}
3276
3277static void
3278menu_leave_handler(struct widget *widget, struct input *input, void *data)
3279{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003280 struct menu *menu = data;
3281
3282 if (widget == menu->widget)
3283 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003284}
3285
3286static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003287menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003288 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003289 uint32_t button, enum wl_pointer_button_state state,
3290 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003291
3292{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003293 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003294
Daniel Stone4dbadb12012-05-30 16:31:51 +01003295 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3296 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003297 /* Either relase after press-drag-release or
3298 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003299 menu->func(menu->window->parent,
3300 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003301 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003302 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003303 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003304}
3305
3306static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003307menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003308{
3309 cairo_t *cr;
3310 const int32_t r = 3, margin = 3;
3311 struct menu *menu = data;
3312 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003313 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003314
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315 cr = cairo_create(window->cairo_surface);
3316 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3317 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3318 cairo_paint(cr);
3319
3320 width = window->allocation.width;
3321 height = window->allocation.height;
3322 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003323
3324 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003325 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3326 cairo_fill(cr);
3327
3328 for (i = 0; i < menu->count; i++) {
3329 if (i == menu->current) {
3330 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3331 cairo_rectangle(cr, margin, i * 20 + margin,
3332 width - 2 * margin, 20);
3333 cairo_fill(cr);
3334 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3335 cairo_move_to(cr, 10, i * 20 + 16);
3336 cairo_show_text(cr, menu->entries[i]);
3337 } else {
3338 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3339 cairo_move_to(cr, 10, i * 20 + 16);
3340 cairo_show_text(cr, menu->entries[i]);
3341 }
3342 }
3343
3344 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003345}
3346
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003347void
3348window_show_menu(struct display *display,
3349 struct input *input, uint32_t time, struct window *parent,
3350 int32_t x, int32_t y,
3351 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003352{
3353 struct window *window;
3354 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003355 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003356
3357 menu = malloc(sizeof *menu);
3358 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003359 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003360
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003361 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003362 if (!window) {
3363 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003364 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003365 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366
3367 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003368 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003369 menu->entries = entries;
3370 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003371 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003372 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003373 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003374 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003375 window->type = TYPE_MENU;
3376 window->x = x;
3377 window->y = y;
3378
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003379 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003380 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003381 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003382 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003383 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003384
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003385 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003386 widget_set_enter_handler(menu->widget, menu_enter_handler);
3387 widget_set_leave_handler(menu->widget, menu_leave_handler);
3388 widget_set_motion_handler(menu->widget, menu_motion_handler);
3389 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003390
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003391 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003392 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003393}
3394
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003395void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003396window_set_buffer_type(struct window *window, enum window_buffer_type type)
3397{
3398 window->buffer_type = type;
3399}
3400
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003401
3402static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003403display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003404 struct wl_output *wl_output,
3405 int x, int y,
3406 int physical_width,
3407 int physical_height,
3408 int subpixel,
3409 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003410 const char *model,
3411 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003412{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003413 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003414
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003415 output->allocation.x = x;
3416 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003417}
3418
3419static void
3420display_handle_mode(void *data,
3421 struct wl_output *wl_output,
3422 uint32_t flags,
3423 int width,
3424 int height,
3425 int refresh)
3426{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003427 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003428 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003429
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003430 if (flags & WL_OUTPUT_MODE_CURRENT) {
3431 output->allocation.width = width;
3432 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003433 if (display->output_configure_handler)
3434 (*display->output_configure_handler)(
3435 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003436 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003437}
3438
3439static const struct wl_output_listener output_listener = {
3440 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003441 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003442};
3443
3444static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003445display_add_output(struct display *d, uint32_t id)
3446{
3447 struct output *output;
3448
3449 output = malloc(sizeof *output);
3450 if (output == NULL)
3451 return;
3452
3453 memset(output, 0, sizeof *output);
3454 output->display = d;
3455 output->output =
3456 wl_display_bind(d->display, id, &wl_output_interface);
3457 wl_list_insert(d->output_list.prev, &output->link);
3458
3459 wl_output_add_listener(output->output, &output_listener, output);
3460}
3461
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003462static void
3463output_destroy(struct output *output)
3464{
3465 if (output->destroy_handler)
3466 (*output->destroy_handler)(output, output->user_data);
3467
3468 wl_output_destroy(output->output);
3469 wl_list_remove(&output->link);
3470 free(output);
3471}
3472
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003473void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003474display_set_output_configure_handler(struct display *display,
3475 display_output_handler_t handler)
3476{
3477 struct output *output;
3478
3479 display->output_configure_handler = handler;
3480 if (!handler)
3481 return;
3482
3483 wl_list_for_each(output, &display->output_list, link)
3484 (*display->output_configure_handler)(output,
3485 display->user_data);
3486}
3487
3488void
3489output_set_user_data(struct output *output, void *data)
3490{
3491 output->user_data = data;
3492}
3493
3494void *
3495output_get_user_data(struct output *output)
3496{
3497 return output->user_data;
3498}
3499
3500void
3501output_set_destroy_handler(struct output *output,
3502 display_output_handler_t handler)
3503{
3504 output->destroy_handler = handler;
3505 /* FIXME: implement this, once we have way to remove outputs */
3506}
3507
3508void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003509output_get_allocation(struct output *output, struct rectangle *allocation)
3510{
3511 *allocation = output->allocation;
3512}
3513
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003514struct wl_output *
3515output_get_wl_output(struct output *output)
3516{
3517 return output->output;
3518}
3519
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003520static void
Daniel Stone97f68542012-05-30 16:32:01 +01003521fini_xkb(struct input *input)
3522{
3523 xkb_state_unref(input->xkb.state);
3524 xkb_map_unref(input->xkb.keymap);
3525}
3526
3527static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003528display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003529{
3530 struct input *input;
3531
3532 input = malloc(sizeof *input);
3533 if (input == NULL)
3534 return;
3535
3536 memset(input, 0, sizeof *input);
3537 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003538 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003539 input->pointer_focus = NULL;
3540 input->keyboard_focus = NULL;
3541 wl_list_insert(d->input_list.prev, &input->link);
3542
Daniel Stone37816df2012-05-16 18:45:18 +01003543 wl_seat_add_listener(input->seat, &seat_listener, input);
3544 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003545
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546 input->data_device =
3547 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003548 input->seat);
3549 wl_data_device_add_listener(input->data_device, &data_device_listener,
3550 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003551
3552 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003553
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003554 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3555 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003556 input->repeat_task.run = keyboard_repeat_func;
3557 display_watch_fd(d, input->repeat_timer_fd,
3558 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003559}
3560
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003561static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003562input_destroy(struct input *input)
3563{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003564 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003565 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003566
3567 if (input->drag_offer)
3568 data_offer_destroy(input->drag_offer);
3569
3570 if (input->selection_offer)
3571 data_offer_destroy(input->selection_offer);
3572
3573 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003574 fini_xkb(input);
3575
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003576 wl_surface_destroy(input->pointer_surface);
3577
Pekka Paalanene1207c72011-12-16 12:02:09 +02003578 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003579 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003580 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003581 free(input);
3582}
3583
3584static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003585init_workspace_manager(struct display *d, uint32_t id)
3586{
3587 d->workspace_manager =
3588 wl_display_bind(d->display, id, &workspace_manager_interface);
3589 if (d->workspace_manager != NULL)
3590 workspace_manager_add_listener(d->workspace_manager,
3591 &workspace_manager_listener,
3592 d);
3593}
3594
3595static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003596display_handle_global(struct wl_display *display, uint32_t id,
3597 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003598{
3599 struct display *d = data;
3600
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003601 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003602 d->compositor =
3603 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003604 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003605 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003606 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003607 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003608 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003609 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003610 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003611 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3613 d->data_device_manager =
3614 wl_display_bind(display, id,
3615 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003616 } else if (strcmp(interface, "text_cursor_position") == 0) {
3617 d->text_cursor_position =
3618 wl_display_bind(display, id,
3619 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003620 } else if (strcmp(interface, "workspace_manager") == 0) {
3621 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003622 }
3623}
3624
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003625#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003626static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003627init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003628{
3629 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003630 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003631
Rob Clark6396ed32012-03-11 19:48:41 -05003632#ifdef USE_CAIRO_GLESV2
3633# define GL_BIT EGL_OPENGL_ES2_BIT
3634#else
3635# define GL_BIT EGL_OPENGL_BIT
3636#endif
3637
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003638 static const EGLint argb_cfg_attribs[] = {
3639 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003640 EGL_RED_SIZE, 1,
3641 EGL_GREEN_SIZE, 1,
3642 EGL_BLUE_SIZE, 1,
3643 EGL_ALPHA_SIZE, 1,
3644 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003645 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003646 EGL_NONE
3647 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003648
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003649#ifdef USE_CAIRO_GLESV2
3650 static const EGLint context_attribs[] = {
3651 EGL_CONTEXT_CLIENT_VERSION, 2,
3652 EGL_NONE
3653 };
3654 EGLint api = EGL_OPENGL_ES_API;
3655#else
3656 EGLint *context_attribs = NULL;
3657 EGLint api = EGL_OPENGL_API;
3658#endif
3659
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003660 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003661 if (!eglInitialize(d->dpy, &major, &minor)) {
3662 fprintf(stderr, "failed to initialize display\n");
3663 return -1;
3664 }
3665
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003666 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003667 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3668 return -1;
3669 }
3670
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003671 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3672 &d->argb_config, 1, &n) || n != 1) {
3673 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003674 return -1;
3675 }
3676
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003677 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003678 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003679 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003680 fprintf(stderr, "failed to create context\n");
3681 return -1;
3682 }
3683
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003684 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003685 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003686 return -1;
3687 }
3688
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003689#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003690 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3691 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3692 fprintf(stderr, "failed to get cairo egl argb device\n");
3693 return -1;
3694 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003695#endif
3696
3697 return 0;
3698}
3699
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003700static void
3701fini_egl(struct display *display)
3702{
3703#ifdef HAVE_CAIRO_EGL
3704 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003705#endif
3706
3707 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3708 EGL_NO_CONTEXT);
3709
3710 eglTerminate(display->dpy);
3711 eglReleaseThread();
3712}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003713#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003714
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003715static int
3716event_mask_update(uint32_t mask, void *data)
3717{
3718 struct display *d = data;
3719
3720 d->mask = mask;
3721
3722 return 0;
3723}
3724
3725static void
3726handle_display_data(struct task *task, uint32_t events)
3727{
3728 struct display *display =
3729 container_of(task, struct display, display_task);
3730
3731 wl_display_iterate(display->display, display->mask);
3732}
3733
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003734struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003735display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003736{
3737 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003738
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003739 d = malloc(sizeof *d);
3740 if (d == NULL)
3741 return NULL;
3742
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003743 memset(d, 0, sizeof *d);
3744
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003745 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003746 if (d->display == NULL) {
3747 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003748 return NULL;
3749 }
3750
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003751 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003752 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3753 d->display_task.run = handle_display_data;
3754 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3755
3756 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003757 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003758 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003759
Daniel Stone97f68542012-05-30 16:32:01 +01003760 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003761 if (d->xkb_context == NULL) {
3762 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003763 return NULL;
3764 }
3765
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003766 /* Set up listener so we'll catch all events. */
3767 wl_display_add_global_listener(d->display,
3768 display_handle_global, d);
3769
3770 /* Process connection events. */
3771 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003772#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003773 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003774 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003775#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003776
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003777 d->image_target_texture_2d =
3778 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3779 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3780 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3781
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003782 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003783
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003784 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003785
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003786 wl_list_init(&d->window_list);
3787
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003788 d->workspace = 0;
3789 d->workspace_count = 1;
3790
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003791 return d;
3792}
3793
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003794static void
3795display_destroy_outputs(struct display *display)
3796{
3797 struct output *tmp;
3798 struct output *output;
3799
3800 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3801 output_destroy(output);
3802}
3803
Pekka Paalanene1207c72011-12-16 12:02:09 +02003804static void
3805display_destroy_inputs(struct display *display)
3806{
3807 struct input *tmp;
3808 struct input *input;
3809
3810 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3811 input_destroy(input);
3812}
3813
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003814void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003815display_destroy(struct display *display)
3816{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003817 if (!wl_list_empty(&display->window_list))
3818 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3819
3820 if (!wl_list_empty(&display->deferred_list))
3821 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3822
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003823 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003824 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003825
Daniel Stone97f68542012-05-30 16:32:01 +01003826 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003827
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003828 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003829 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003830
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003831#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003832 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003833#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003834
Pekka Paalanenc2052982011-12-16 11:41:32 +02003835 if (display->shell)
3836 wl_shell_destroy(display->shell);
3837
3838 if (display->shm)
3839 wl_shm_destroy(display->shm);
3840
3841 if (display->data_device_manager)
3842 wl_data_device_manager_destroy(display->data_device_manager);
3843
3844 wl_compositor_destroy(display->compositor);
3845
3846 close(display->epoll_fd);
3847
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003848 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003849 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003850 free(display);
3851}
3852
3853void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003854display_set_user_data(struct display *display, void *data)
3855{
3856 display->user_data = data;
3857}
3858
3859void *
3860display_get_user_data(struct display *display)
3861{
3862 return display->user_data;
3863}
3864
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003865struct wl_display *
3866display_get_display(struct display *display)
3867{
3868 return display->display;
3869}
3870
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003871struct output *
3872display_get_output(struct display *display)
3873{
3874 return container_of(display->output_list.next, struct output, link);
3875}
3876
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003877struct wl_compositor *
3878display_get_compositor(struct display *display)
3879{
3880 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003881}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003882
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003883uint32_t
3884display_get_serial(struct display *display)
3885{
3886 return display->serial;
3887}
3888
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003889EGLDisplay
3890display_get_egl_display(struct display *d)
3891{
3892 return d->dpy;
3893}
3894
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003895struct wl_data_source *
3896display_create_data_source(struct display *display)
3897{
3898 return wl_data_device_manager_create_data_source(display->data_device_manager);
3899}
3900
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003901EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003902display_get_argb_egl_config(struct display *d)
3903{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003904 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003905}
3906
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003907struct wl_shell *
3908display_get_shell(struct display *display)
3909{
3910 return display->shell;
3911}
3912
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003913int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003914display_acquire_window_surface(struct display *display,
3915 struct window *window,
3916 EGLContext ctx)
3917{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003918#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003919 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003920 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003921
3922 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003923 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003924 device = cairo_surface_get_device(window->cairo_surface);
3925 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003926 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003927
Benjamin Franzke0c991632011-09-27 21:57:31 +02003928 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003929 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003930 ctx = display->argb_ctx;
3931 else
3932 assert(0);
3933 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003934
3935 data = cairo_surface_get_user_data(window->cairo_surface,
3936 &surface_data_key);
3937
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003938 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003939 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003940 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3941 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003942
3943 return 0;
3944#else
3945 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003946#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003947}
3948
3949void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003950display_release_window_surface(struct display *display,
3951 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003952{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003953#ifdef HAVE_CAIRO_EGL
3954 cairo_device_t *device;
3955
3956 device = cairo_surface_get_device(window->cairo_surface);
3957 if (!device)
3958 return;
3959
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003960 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003961 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003962 cairo_device_release(device);
3963#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003964}
3965
3966void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003967display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003968{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003969 wl_list_insert(&display->deferred_list, &task->link);
3970}
3971
3972void
3973display_watch_fd(struct display *display,
3974 int fd, uint32_t events, struct task *task)
3975{
3976 struct epoll_event ep;
3977
3978 ep.events = events;
3979 ep.data.ptr = task;
3980 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3981}
3982
3983void
3984display_run(struct display *display)
3985{
3986 struct task *task;
3987 struct epoll_event ep[16];
3988 int i, count;
3989
Pekka Paalanen826d7952011-12-15 10:14:07 +02003990 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003991 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003992 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003993
3994 while (!wl_list_empty(&display->deferred_list)) {
3995 task = container_of(display->deferred_list.next,
3996 struct task, link);
3997 wl_list_remove(&task->link);
3998 task->run(task, 0);
3999 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004000
4001 if (!display->running)
4002 break;
4003
4004 wl_display_flush(display->display);
4005
4006 count = epoll_wait(display->epoll_fd,
4007 ep, ARRAY_LENGTH(ep), -1);
4008 for (i = 0; i < count; i++) {
4009 task = ep[i].data.ptr;
4010 task->run(task, ep[i].events);
4011 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004012 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004013}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004014
4015void
4016display_exit(struct display *display)
4017{
4018 display->running = 0;
4019}