blob: 44a057c4c224ec980971cd71827ebdf337aa0dec [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>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040089 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040090 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040091 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060092 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020093 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128};
129
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400131 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400132 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400133 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500134 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500136 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_CUSTOM
138};
Rob Bradford7507b572012-05-15 17:55:34 +0100139
140struct window_output {
141 struct output *output;
142 struct wl_list link;
143};
144
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500145struct window {
146 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500147 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100148 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500149 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500151 struct wl_region *input_region;
152 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500153 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500154 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400155 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500156 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500157 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400158 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400159 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400160 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400161 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400162 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400163 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400164 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400165 int focus_count;
166
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400167 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500168
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400169 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500170
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700171 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400172
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500173 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500174 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400175 window_data_handler_t data_handler;
176 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500177 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400178 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300180 struct wl_callback *frame_cb;
181
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200182 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500183 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500185 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400186 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187};
188
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500189struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500190 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300191 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500192 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193 struct wl_list link;
194 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195 widget_resize_handler_t resize_handler;
196 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500197 widget_enter_handler_t enter_handler;
198 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500199 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500200 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200201 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400202 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500203 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300204 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400205};
206
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207struct input {
208 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100209 struct wl_seat *seat;
210 struct wl_pointer *pointer;
211 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212 struct window *pointer_focus;
213 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300214 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300215 uint32_t cursor_anim_start;
216 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300217 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400218 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400219 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400220 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400221 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400222 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400223
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500224 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500225 struct widget *grab;
226 uint32_t grab_button;
227
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400228 struct wl_data_device *data_device;
229 struct data_offer *drag_offer;
230 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100231
232 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100233 struct xkb_keymap *keymap;
234 struct xkb_state *state;
235 xkb_mod_mask_t control_mask;
236 xkb_mod_mask_t alt_mask;
237 xkb_mod_mask_t shift_mask;
238 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400239
240 struct task repeat_task;
241 int repeat_timer_fd;
242 uint32_t repeat_sym;
243 uint32_t repeat_key;
244 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400245};
246
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500247struct output {
248 struct display *display;
249 struct wl_output *output;
250 struct rectangle allocation;
251 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600252 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200253
254 display_output_handler_t destroy_handler;
255 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500256};
257
Martin Minarik1998b152012-05-10 02:04:35 +0200258enum frame_button_action {
259 FRAME_BUTTON_NULL = 0,
260 FRAME_BUTTON_ICON = 1,
261 FRAME_BUTTON_CLOSE = 2,
262 FRAME_BUTTON_MINIMIZE = 3,
263 FRAME_BUTTON_MAXIMIZE = 4,
264};
265
266enum frame_button_pointer {
267 FRAME_BUTTON_DEFAULT = 0,
268 FRAME_BUTTON_OVER = 1,
269 FRAME_BUTTON_ACTIVE = 2,
270};
271
272enum frame_button_align {
273 FRAME_BUTTON_RIGHT = 0,
274 FRAME_BUTTON_LEFT = 1,
275};
276
277enum frame_button_decoration {
278 FRAME_BUTTON_NONE = 0,
279 FRAME_BUTTON_FANCY = 1,
280};
281
282struct frame_button {
283 struct widget *widget;
284 struct frame *frame;
285 cairo_surface_t *icon;
286 enum frame_button_action type;
287 enum frame_button_pointer state;
288 struct wl_list link; /* buttons_list */
289 enum frame_button_align align;
290 enum frame_button_decoration decoration;
291};
292
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500293struct frame {
294 struct widget *widget;
295 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200296 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500297};
298
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500299struct menu {
300 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500301 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500302 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500303 const char **entries;
304 uint32_t time;
305 int current;
306 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400307 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500308 menu_func_t func;
309};
310
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300311struct tooltip {
312 struct widget *parent;
313 struct window *window;
314 struct widget *widget;
315 char *entry;
316 struct task tooltip_task;
317 int tooltip_fd;
318 float x, y;
319};
320
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700321struct shm_pool {
322 struct wl_shm_pool *pool;
323 size_t size;
324 size_t used;
325 void *data;
326};
327
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400328enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300329 CURSOR_DEFAULT = 100,
330 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400331};
332
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500333enum window_location {
334 WINDOW_INTERIOR = 0,
335 WINDOW_RESIZING_TOP = 1,
336 WINDOW_RESIZING_BOTTOM = 2,
337 WINDOW_RESIZING_LEFT = 4,
338 WINDOW_RESIZING_TOP_LEFT = 5,
339 WINDOW_RESIZING_BOTTOM_LEFT = 6,
340 WINDOW_RESIZING_RIGHT = 8,
341 WINDOW_RESIZING_TOP_RIGHT = 9,
342 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
343 WINDOW_RESIZING_MASK = 15,
344 WINDOW_EXTERIOR = 16,
345 WINDOW_TITLEBAR = 17,
346 WINDOW_CLIENT_AREA = 18,
347};
348
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200349static const cairo_user_data_key_t shm_surface_data_key;
350static const cairo_user_data_key_t egl_window_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500352#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100354struct egl_window_surface_data {
355 struct display *display;
356 struct wl_surface *surface;
357 struct wl_egl_window *window;
358 EGLSurface surf;
359};
360
361static void
362egl_window_surface_data_destroy(void *p)
363{
364 struct egl_window_surface_data *data = p;
365 struct display *d = data->display;
366
367 eglDestroySurface(d->dpy, data->surf);
368 wl_egl_window_destroy(data->window);
369 data->surface = NULL;
370
371 free(p);
372}
373
374static cairo_surface_t *
375display_create_egl_window_surface(struct display *display,
376 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 struct rectangle *rectangle)
379{
380 cairo_surface_t *cairo_surface;
381 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384
385 data = malloc(sizeof *data);
386 if (data == NULL)
387 return NULL;
388
389 data->display = display;
390 data->surface = surface;
391
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500392 config = display->argb_config;
393 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400394
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400395 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100396 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400397 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100398
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400399 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500400 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100401
Benjamin Franzke0c991632011-09-27 21:57:31 +0200402 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100403 data->surf,
404 rectangle->width,
405 rectangle->height);
406
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200407 cairo_surface_set_user_data(cairo_surface,
408 &egl_window_surface_data_key,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100409 data, egl_window_surface_data_destroy);
410
411 return cairo_surface;
412}
413
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414#endif
415
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200416struct shm_surface_data {
417 struct wl_buffer *buffer;
418 struct shm_pool *pool;
419};
420
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421struct wl_buffer *
422display_get_buffer_for_surface(struct display *display,
423 cairo_surface_t *surface)
424{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200425 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200427 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
429 return data->buffer;
430}
431
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500432static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700433shm_pool_destroy(struct shm_pool *pool);
434
435static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436shm_surface_data_destroy(void *p)
437{
438 struct shm_surface_data *data = p;
439
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200440 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700441 if (data->pool)
442 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300443
444 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445}
446
Kristian Høgsberg16626282012-04-03 11:21:27 -0400447static struct wl_shm_pool *
448make_shm_pool(struct display *display, int size, void **data)
449{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400450 struct wl_shm_pool *pool;
451 int fd;
452
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300453 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400454 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300455 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
456 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400457 return NULL;
458 }
459
460 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400461 if (*data == MAP_FAILED) {
462 fprintf(stderr, "mmap failed: %m\n");
463 close(fd);
464 return NULL;
465 }
466
467 pool = wl_shm_create_pool(display->shm, fd, size);
468
469 close(fd);
470
471 return pool;
472}
473
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700474static struct shm_pool *
475shm_pool_create(struct display *display, size_t size)
476{
477 struct shm_pool *pool = malloc(sizeof *pool);
478
479 if (!pool)
480 return NULL;
481
482 pool->pool = make_shm_pool(display, size, &pool->data);
483 if (!pool->pool) {
484 free(pool);
485 return NULL;
486 }
487
488 pool->size = size;
489 pool->used = 0;
490
491 return pool;
492}
493
494static void *
495shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
496{
497 if (pool->used + size > pool->size)
498 return NULL;
499
500 *offset = pool->used;
501 pool->used += size;
502
503 return (char *) pool->data + *offset;
504}
505
506/* destroy the pool. this does not unmap the memory though */
507static void
508shm_pool_destroy(struct shm_pool *pool)
509{
510 munmap(pool->data, pool->size);
511 wl_shm_pool_destroy(pool->pool);
512 free(pool);
513}
514
515/* Start allocating from the beginning of the pool again */
516static void
517shm_pool_reset(struct shm_pool *pool)
518{
519 pool->used = 0;
520}
521
522static int
523data_length_for_shm_surface(struct rectangle *rect)
524{
525 int stride;
526
527 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
528 rect->width);
529 return stride * rect->height;
530}
531
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500532static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700533display_create_shm_surface_from_pool(struct display *display,
534 struct rectangle *rectangle,
535 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536{
537 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400538 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400539 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700540 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400541 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542
543 data = malloc(sizeof *data);
544 if (data == NULL)
545 return NULL;
546
547 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
548 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700549 length = stride * rectangle->height;
550 data->pool = NULL;
551 map = shm_pool_allocate(pool, length, &offset);
552
553 if (!map) {
554 free(data);
555 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400556 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400558 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559 CAIRO_FORMAT_ARGB32,
560 rectangle->width,
561 rectangle->height,
562 stride);
563
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200564 cairo_surface_set_user_data(surface, &shm_surface_data_key,
565 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400567 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500568 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500570 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400571
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200572 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
573 rectangle->width,
574 rectangle->height,
575 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700577 return surface;
578}
579
580static cairo_surface_t *
581display_create_shm_surface(struct display *display,
582 struct rectangle *rectangle, uint32_t flags,
583 struct window *window)
584{
585 struct shm_surface_data *data;
586 struct shm_pool *pool;
587 cairo_surface_t *surface;
588
589 if (window && window->pool) {
590 shm_pool_reset(window->pool);
591 surface = display_create_shm_surface_from_pool(display,
592 rectangle,
593 flags,
594 window->pool);
595 if (surface)
596 return surface;
597 }
598
599 pool = shm_pool_create(display,
600 data_length_for_shm_surface(rectangle));
601 if (!pool)
602 return NULL;
603
604 surface =
605 display_create_shm_surface_from_pool(display, rectangle,
606 flags, pool);
607
608 if (!surface) {
609 shm_pool_destroy(pool);
610 return NULL;
611 }
612
613 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200614 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700615 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616
617 return surface;
618}
619
nobled7b87cb02011-02-01 18:51:47 +0000620static int
621check_size(struct rectangle *rect)
622{
623 if (rect->width && rect->height)
624 return 0;
625
626 fprintf(stderr, "tried to create surface of "
627 "width: %d, height: %d\n", rect->width, rect->height);
628 return -1;
629}
630
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631cairo_surface_t *
632display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400634 struct rectangle *rectangle,
635 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636{
nobled7b87cb02011-02-01 18:51:47 +0000637 if (check_size(rectangle) < 0)
638 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500639#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300640 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400641 return display_create_egl_window_surface(display,
642 surface,
643 flags,
644 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400646 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647}
648
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200649/*
650 * The following correspondences between file names and cursors was copied
651 * from: https://bugs.kde.org/attachment.cgi?id=67313
652 */
653
654static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300655 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200656 "sw-resize"
657};
658
659static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300660 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200661 "se-resize"
662};
663
664static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300665 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200666 "s-resize"
667};
668
669static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200671 "closedhand",
672 "208530c400c041818281048008011002"
673};
674
675static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200677 "default",
678 "top_left_arrow",
679 "left-arrow"
680};
681
682static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300683 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200684 "w-resize"
685};
686
687static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300688 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200689 "e-resize"
690};
691
692static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300693 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200694 "nw-resize"
695};
696
697static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300698 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200699 "ne-resize"
700};
701
702static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300703 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200704 "n-resize"
705};
706
707static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300708 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200709 "ibeam",
710 "text"
711};
712
713static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300714 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200715 "pointer",
716 "pointing_hand",
717 "e29285e634086352946a0e7090d73106"
718};
719
720static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400721 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200722 "wait",
723 "0426c94ea35c87780ff01dc239897213"
724};
725
726struct cursor_alternatives {
727 const char **names;
728 size_t count;
729};
730
731static const struct cursor_alternatives cursors[] = {
732 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
733 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
734 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
735 {grabbings, ARRAY_LENGTH(grabbings)},
736 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
737 {left_sides, ARRAY_LENGTH(left_sides)},
738 {right_sides, ARRAY_LENGTH(right_sides)},
739 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
740 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
741 {top_sides, ARRAY_LENGTH(top_sides)},
742 {xterms, ARRAY_LENGTH(xterms)},
743 {hand1s, ARRAY_LENGTH(hand1s)},
744 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300745};
746
747static void
748create_cursors(struct display *display)
749{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100750 char *config_file;
751 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200752 unsigned int i, j;
753 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100754 struct config_key shell_keys[] = {
755 { "cursor-theme", CONFIG_KEY_STRING, &theme },
756 };
757 struct config_section cs[] = {
758 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
759 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300760
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100761 config_file = config_file_path("weston.ini");
762 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
763 free(config_file);
764
765 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300766 display->cursors =
767 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
768
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300769 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200770 cursor = NULL;
771 for (j = 0; !cursor && j < cursors[i].count; ++j)
772 cursor = wl_cursor_theme_get_cursor(
773 display->cursor_theme, cursors[i].names[j]);
774
775 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300776 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200777 cursors[i].names[0]);
778
779 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300780 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300781}
782
783static void
784destroy_cursors(struct display *display)
785{
786 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800787 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300788}
789
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300790struct wl_cursor_image *
791display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400792{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200793 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400794
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300795 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400796}
797
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400798static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200799window_get_resize_dx_dy(struct window *window, int *x, int *y)
800{
801 if (window->resize_edges & WINDOW_RESIZING_LEFT)
802 *x = window->server_allocation.width - window->allocation.width;
803 else
804 *x = 0;
805
806 if (window->resize_edges & WINDOW_RESIZING_TOP)
807 *y = window->server_allocation.height -
808 window->allocation.height;
809 else
810 *y = 0;
811
812 window->resize_edges = 0;
813}
814
815static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500816window_attach_surface(struct window *window)
817{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400818 struct display *display = window->display;
819 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500823 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400825 if (window->type == TYPE_NONE) {
826 window->type = TYPE_TOPLEVEL;
827 if (display->shell)
828 wl_shell_surface_set_toplevel(window->shell_surface);
829 }
830
Pekka Paalanen512dde82012-10-10 12:49:27 +0300831 if (window->opaque_region) {
832 wl_surface_set_opaque_region(window->surface,
833 window->opaque_region);
834 wl_region_destroy(window->opaque_region);
835 window->opaque_region = NULL;
836 }
837
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300838 if (window->input_region) {
839 wl_surface_set_input_region(window->surface,
840 window->input_region);
841 wl_region_destroy(window->input_region);
842 window->input_region = NULL;
843 }
844
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000846#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
848 data = cairo_surface_get_user_data(window->cairo_surface,
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200849 &egl_window_surface_data_key);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 cairo_gl_surface_swapbuffers(window->cairo_surface);
852 wl_egl_window_get_attached_size(data->window,
853 &window->server_allocation.width,
854 &window->server_allocation.height);
855 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000856#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 buffer =
859 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400860 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200862 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400864 wl_surface_damage(window->surface, 0, 0,
865 window->allocation.width,
866 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300867 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100868 window->server_allocation = window->allocation;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100869 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000870 default:
871 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500873}
874
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400875int
876window_has_focus(struct window *window)
877{
878 return window->focus_count > 0;
879}
880
Pekka Paalanena8d4c842012-11-19 15:32:48 +0200881static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400882window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500883{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400884 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100885 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500886}
887
Pekka Paalanence36f6d2012-11-19 15:32:47 +0200888static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500891 cairo_surface_reference(surface);
892
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893 if (window->cairo_surface != NULL)
894 cairo_surface_destroy(window->cairo_surface);
895
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500896 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897}
898
Benjamin Franzke22d54812011-07-16 19:50:32 +0000899#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900static void
901window_resize_cairo_window_surface(struct window *window)
902{
903 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905
906 data = cairo_surface_get_user_data(window->cairo_surface,
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200907 &egl_window_surface_data_key);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200909 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 wl_egl_window_resize(data->window,
911 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200912 window->allocation.height,
913 x,y);
914
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 cairo_gl_surface_set_size(window->cairo_surface,
916 window->allocation.width,
917 window->allocation.height);
918}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000919#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400921struct display *
922window_get_display(struct window *window)
923{
924 return window->display;
925}
926
Pekka Paalanence36f6d2012-11-19 15:32:47 +0200927static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928window_create_surface(struct window *window)
929{
930 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 uint32_t flags = 0;
932
933 if (!window->transparent)
934 flags = SURFACE_OPAQUE;
935
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500937#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
939 if (window->cairo_surface) {
940 window_resize_cairo_window_surface(window);
941 return;
942 }
943 surface = display_create_surface(window->display,
944 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100946 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947#endif
948 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400950 &window->allocation,
951 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800953 default:
954 surface = NULL;
955 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400957
958 window_set_surface(window, surface);
959 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960}
961
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200962static void frame_destroy(struct frame *frame);
963
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400964void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500965window_destroy(struct window *window)
966{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200967 struct display *display = window->display;
968 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100969 struct window_output *window_output;
970 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971
972 if (window->redraw_scheduled)
973 wl_list_remove(&window->redraw_task.link);
974
975 wl_list_for_each(input, &display->input_list, link) {
976 if (input->pointer_focus == window)
977 input->pointer_focus = NULL;
978 if (input->keyboard_focus == window)
979 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500980 if (input->focus_widget &&
981 input->focus_widget->window == window)
982 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200983 }
984
Rob Bradford7507b572012-05-15 17:55:34 +0100985 wl_list_for_each_safe(window_output, window_output_tmp,
986 &window->window_output_list, link) {
987 free (window_output);
988 }
989
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500990 if (window->input_region)
991 wl_region_destroy(window->input_region);
992 if (window->opaque_region)
993 wl_region_destroy(window->opaque_region);
994
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200995 if (window->frame)
996 frame_destroy(window->frame);
997
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200998 if (window->shell_surface)
999 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000 wl_surface_destroy(window->surface);
1001 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001002
1003 if (window->cairo_surface != NULL)
1004 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001005
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001006 if (window->frame_cb)
1007 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001008 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001009 free(window);
1010}
1011
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001013widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001016
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017 wl_list_for_each(child, &widget->child_list, link) {
1018 target = widget_find_widget(child, x, y);
1019 if (target)
1020 return target;
1021 }
1022
1023 if (widget->allocation.x <= x &&
1024 x < widget->allocation.x + widget->allocation.width &&
1025 widget->allocation.y <= y &&
1026 y < widget->allocation.y + widget->allocation.height) {
1027 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028 }
1029
1030 return NULL;
1031}
1032
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001033static struct widget *
1034widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001035{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001036 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001037
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038 widget = malloc(sizeof *widget);
1039 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001040 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001042 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001043 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001044 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001045 widget->tooltip = NULL;
1046 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001047
1048 return widget;
1049}
1050
1051struct widget *
1052window_add_widget(struct window *window, void *data)
1053{
1054 window->widget = widget_create(window, data);
1055 wl_list_init(&window->widget->link);
1056
1057 return window->widget;
1058}
1059
1060struct widget *
1061widget_add_widget(struct widget *parent, void *data)
1062{
1063 struct widget *widget;
1064
1065 widget = widget_create(parent->window, data);
1066 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069}
1070
1071void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001072widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001074 struct display *display = widget->window->display;
1075 struct input *input;
1076
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001077 if (widget->tooltip) {
1078 free(widget->tooltip);
1079 widget->tooltip = NULL;
1080 }
1081
Pekka Paalanene156fb62012-01-19 13:51:38 +02001082 wl_list_for_each(input, &display->input_list, link) {
1083 if (input->focus_widget == widget)
1084 input->focus_widget = NULL;
1085 }
1086
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001087 wl_list_remove(&widget->link);
1088 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001094 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095}
1096
1097void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001098widget_set_size(struct widget *widget, int32_t width, int32_t height)
1099{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001100 widget->allocation.width = width;
1101 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001102}
1103
1104void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001105widget_set_allocation(struct widget *widget,
1106 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001107{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001108 widget->allocation.x = x;
1109 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001110 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111}
1112
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001113void
1114widget_set_transparent(struct widget *widget, int transparent)
1115{
1116 widget->opaque = !transparent;
1117}
1118
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001119void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001120widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001121{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001122 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123}
1124
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001125void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001126widget_set_resize_handler(struct widget *widget,
1127 widget_resize_handler_t handler)
1128{
1129 widget->resize_handler = handler;
1130}
1131
1132void
1133widget_set_redraw_handler(struct widget *widget,
1134 widget_redraw_handler_t handler)
1135{
1136 widget->redraw_handler = handler;
1137}
1138
1139void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001140widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001141{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001142 widget->enter_handler = handler;
1143}
1144
1145void
1146widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1147{
1148 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001149}
1150
1151void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001152widget_set_motion_handler(struct widget *widget,
1153 widget_motion_handler_t handler)
1154{
1155 widget->motion_handler = handler;
1156}
1157
1158void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001159widget_set_button_handler(struct widget *widget,
1160 widget_button_handler_t handler)
1161{
1162 widget->button_handler = handler;
1163}
1164
1165void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001166widget_set_axis_handler(struct widget *widget,
1167 widget_axis_handler_t handler)
1168{
1169 widget->axis_handler = handler;
1170}
1171
1172void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001173widget_schedule_redraw(struct widget *widget)
1174{
1175 window_schedule_redraw(widget->window);
1176}
1177
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001178cairo_surface_t *
1179window_get_surface(struct window *window)
1180{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001181 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001182}
1183
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001184struct wl_surface *
1185window_get_wl_surface(struct window *window)
1186{
1187 return window->surface;
1188}
1189
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001190struct wl_shell_surface *
1191window_get_wl_shell_surface(struct window *window)
1192{
1193 return window->shell_surface;
1194}
1195
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001197tooltip_redraw_handler(struct widget *widget, void *data)
1198{
1199 cairo_t *cr;
1200 const int32_t r = 3;
1201 struct tooltip *tooltip = data;
1202 int32_t width, height;
1203 struct window *window = widget->window;
1204
1205 cr = cairo_create(window->cairo_surface);
1206 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1207 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1208 cairo_paint(cr);
1209
1210 width = window->allocation.width;
1211 height = window->allocation.height;
1212 rounded_rect(cr, 0, 0, width, height, r);
1213
1214 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1215 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1216 cairo_fill(cr);
1217
1218 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1219 cairo_move_to(cr, 10, 16);
1220 cairo_show_text(cr, tooltip->entry);
1221 cairo_destroy(cr);
1222}
1223
1224static cairo_text_extents_t
1225get_text_extents(struct tooltip *tooltip)
1226{
1227 struct window *window;
1228 cairo_t *cr;
1229 cairo_text_extents_t extents;
1230
1231 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1232 * created yet */
1233 window = tooltip->widget->window->parent;
1234 cr = cairo_create(window->cairo_surface);
1235 cairo_text_extents(cr, tooltip->entry, &extents);
1236 cairo_destroy(cr);
1237
1238 return extents;
1239}
1240
1241static int
1242window_create_tooltip(struct tooltip *tooltip)
1243{
1244 struct widget *parent = tooltip->parent;
1245 struct display *display = parent->window->display;
1246 struct window *window;
1247 const int offset_y = 27;
1248 const int margin = 3;
1249 cairo_text_extents_t extents;
1250
1251 if (tooltip->widget)
1252 return 0;
1253
1254 window = window_create_transient(display, parent->window, tooltip->x,
1255 tooltip->y + offset_y,
1256 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1257 if (!window)
1258 return -1;
1259
1260 tooltip->window = window;
1261 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1262
1263 extents = get_text_extents(tooltip);
1264 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1265 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1266
1267 return 0;
1268}
1269
1270void
1271widget_destroy_tooltip(struct widget *parent)
1272{
1273 struct tooltip *tooltip = parent->tooltip;
1274
1275 parent->tooltip_count = 0;
1276 if (!tooltip)
1277 return;
1278
1279 if (tooltip->widget) {
1280 widget_destroy(tooltip->widget);
1281 window_destroy(tooltip->window);
1282 tooltip->widget = NULL;
1283 tooltip->window = NULL;
1284 }
1285
1286 close(tooltip->tooltip_fd);
1287 free(tooltip->entry);
1288 free(tooltip);
1289 parent->tooltip = NULL;
1290}
1291
1292static void
1293tooltip_func(struct task *task, uint32_t events)
1294{
1295 struct tooltip *tooltip =
1296 container_of(task, struct tooltip, tooltip_task);
1297 uint64_t exp;
1298
Martin Olsson8df662a2012-07-08 03:03:47 +02001299 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1300 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001301 window_create_tooltip(tooltip);
1302}
1303
1304#define TOOLTIP_TIMEOUT 500
1305static int
1306tooltip_timer_reset(struct tooltip *tooltip)
1307{
1308 struct itimerspec its;
1309
1310 its.it_interval.tv_sec = 0;
1311 its.it_interval.tv_nsec = 0;
1312 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1313 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1314 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1315 fprintf(stderr, "could not set timerfd\n: %m");
1316 return -1;
1317 }
1318
1319 return 0;
1320}
1321
1322int
1323widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1324{
1325 struct tooltip *tooltip = parent->tooltip;
1326
1327 parent->tooltip_count++;
1328 if (tooltip) {
1329 tooltip->x = x;
1330 tooltip->y = y;
1331 tooltip_timer_reset(tooltip);
1332 return 0;
1333 }
1334
1335 /* the handler might be triggered too fast via input device motion, so
1336 * we need this check here to make sure tooltip is fully initialized */
1337 if (parent->tooltip_count > 1)
1338 return 0;
1339
1340 tooltip = malloc(sizeof *tooltip);
1341 if (!tooltip)
1342 return -1;
1343
1344 parent->tooltip = tooltip;
1345 tooltip->parent = parent;
1346 tooltip->widget = NULL;
1347 tooltip->window = NULL;
1348 tooltip->x = x;
1349 tooltip->y = y;
1350 tooltip->entry = strdup(entry);
1351 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1352 if (tooltip->tooltip_fd < 0) {
1353 fprintf(stderr, "could not create timerfd\n: %m");
1354 return -1;
1355 }
1356
1357 tooltip->tooltip_task.run = tooltip_func;
1358 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1359 EPOLLIN, &tooltip->tooltip_task);
1360 tooltip_timer_reset(tooltip);
1361
1362 return 0;
1363}
1364
1365static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001366workspace_manager_state(void *data,
1367 struct workspace_manager *workspace_manager,
1368 uint32_t current,
1369 uint32_t count)
1370{
1371 struct display *display = data;
1372
1373 display->workspace = current;
1374 display->workspace_count = count;
1375}
1376
1377static const struct workspace_manager_listener workspace_manager_listener = {
1378 workspace_manager_state
1379};
1380
1381static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001382frame_resize_handler(struct widget *widget,
1383 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001384{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385 struct frame *frame = data;
1386 struct widget *child = frame->child;
1387 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001388 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001389 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001390 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001391 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001392 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001393 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001394
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001395 switch (widget->window->type) {
1396 case TYPE_FULLSCREEN:
1397 decoration_width = 0;
1398 decoration_height = 0;
1399
1400 allocation.x = 0;
1401 allocation.y = 0;
1402 allocation.width = width;
1403 allocation.height = height;
1404 opaque_margin = 0;
1405
1406 wl_list_for_each(button, &frame->buttons_list, link)
1407 button->widget->opaque = 1;
1408 break;
1409 case TYPE_MAXIMIZED:
1410 decoration_width = t->width * 2;
1411 decoration_height = t->width + t->titlebar_height;
1412
1413 allocation.x = t->width;
1414 allocation.y = t->titlebar_height;
1415 allocation.width = width - decoration_width;
1416 allocation.height = height - decoration_height;
1417
1418 opaque_margin = 0;
1419
1420 wl_list_for_each(button, &frame->buttons_list, link)
1421 button->widget->opaque = 0;
1422 break;
1423 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001424 decoration_width = (t->width + t->margin) * 2;
1425 decoration_height = t->width +
1426 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 allocation.x = t->width + t->margin;
1429 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001430 allocation.width = width - decoration_width;
1431 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001432
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001433 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001434
1435 wl_list_for_each(button, &frame->buttons_list, link)
1436 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001437 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001438 }
1439
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001440 widget_set_allocation(child, allocation.x, allocation.y,
1441 allocation.width, allocation.height);
1442
1443 if (child->resize_handler)
1444 child->resize_handler(child,
1445 allocation.width,
1446 allocation.height,
1447 child->user_data);
1448
Scott Moreauf7e498c2012-05-14 11:39:29 -06001449 width = child->allocation.width + decoration_width;
1450 height = child->allocation.height + decoration_height;
1451
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001452 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1453
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001454 widget->window->input_region =
1455 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001456 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001457 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001458 shadow_margin, shadow_margin,
1459 width - 2 * shadow_margin,
1460 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001461 } else {
1462 wl_region_add(widget->window->input_region,
1463 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001464 }
1465
Scott Moreauf7e498c2012-05-14 11:39:29 -06001466 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001467
1468 if (child->opaque) {
1469 widget->window->opaque_region =
1470 wl_compositor_create_region(display->compositor);
1471 wl_region_add(widget->window->opaque_region,
1472 opaque_margin, opaque_margin,
1473 widget->allocation.width - 2 * opaque_margin,
1474 widget->allocation.height - 2 * opaque_margin);
1475 }
Martin Minarik1998b152012-05-10 02:04:35 +02001476
1477 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001478 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1479 x_l = t->width + shadow_margin;
1480 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001481 wl_list_for_each(button, &frame->buttons_list, link) {
1482 const int button_padding = 4;
1483 w = cairo_image_surface_get_width(button->icon);
1484 h = cairo_image_surface_get_height(button->icon);
1485
1486 if (button->decoration == FRAME_BUTTON_FANCY)
1487 w += 10;
1488
1489 if (button->align == FRAME_BUTTON_LEFT) {
1490 widget_set_allocation(button->widget,
1491 x_l, y , w + 1, h + 1);
1492 x_l += w;
1493 x_l += button_padding;
1494 } else {
1495 x_r -= w;
1496 widget_set_allocation(button->widget,
1497 x_r, y , w + 1, h + 1);
1498 x_r -= button_padding;
1499 }
1500 }
1501}
1502
1503static int
1504frame_button_enter_handler(struct widget *widget,
1505 struct input *input, float x, float y, void *data)
1506{
1507 struct frame_button *frame_button = data;
1508
1509 widget_schedule_redraw(frame_button->widget);
1510 frame_button->state = FRAME_BUTTON_OVER;
1511
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001512 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001513}
1514
1515static void
1516frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1517{
1518 struct frame_button *frame_button = data;
1519
1520 widget_schedule_redraw(frame_button->widget);
1521 frame_button->state = FRAME_BUTTON_DEFAULT;
1522}
1523
1524static void
1525frame_button_button_handler(struct widget *widget,
1526 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001527 uint32_t button,
1528 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001529{
1530 struct frame_button *frame_button = data;
1531 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001532 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001533
1534 if (button != BTN_LEFT)
1535 return;
1536
1537 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001538 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001539 frame_button->state = FRAME_BUTTON_ACTIVE;
1540 widget_schedule_redraw(frame_button->widget);
1541
1542 if (frame_button->type == FRAME_BUTTON_ICON)
1543 window_show_frame_menu(window, input, time);
1544 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001545 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001546 frame_button->state = FRAME_BUTTON_DEFAULT;
1547 widget_schedule_redraw(frame_button->widget);
1548 break;
1549 }
1550
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001551 if (!was_pressed)
1552 return;
1553
Martin Minarik1998b152012-05-10 02:04:35 +02001554 switch (frame_button->type) {
1555 case FRAME_BUTTON_CLOSE:
1556 if (window->close_handler)
1557 window->close_handler(window->parent,
1558 window->user_data);
1559 else
1560 display_exit(window->display);
1561 break;
1562 case FRAME_BUTTON_MINIMIZE:
1563 fprintf(stderr,"Minimize stub\n");
1564 break;
1565 case FRAME_BUTTON_MAXIMIZE:
1566 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1567 break;
1568 default:
1569 /* Unknown operation */
1570 break;
1571 }
1572}
1573
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001574static int
1575frame_button_motion_handler(struct widget *widget,
1576 struct input *input, uint32_t time,
1577 float x, float y, void *data)
1578{
1579 struct frame_button *frame_button = data;
1580 enum frame_button_pointer previous_button_state = frame_button->state;
1581
1582 /* only track state for a pressed button */
1583 if (input->grab != widget)
1584 return CURSOR_LEFT_PTR;
1585
1586 if (x > widget->allocation.x &&
1587 x < (widget->allocation.x + widget->allocation.width) &&
1588 y > widget->allocation.y &&
1589 y < (widget->allocation.y + widget->allocation.height)) {
1590 frame_button->state = FRAME_BUTTON_ACTIVE;
1591 } else {
1592 frame_button->state = FRAME_BUTTON_DEFAULT;
1593 }
1594
1595 if (frame_button->state != previous_button_state)
1596 widget_schedule_redraw(frame_button->widget);
1597
1598 return CURSOR_LEFT_PTR;
1599}
1600
Martin Minarik1998b152012-05-10 02:04:35 +02001601static void
1602frame_button_redraw_handler(struct widget *widget, void *data)
1603{
1604 struct frame_button *frame_button = data;
1605 cairo_t *cr;
1606 int width, height, x, y;
1607 struct window *window = widget->window;
1608
1609 x = widget->allocation.x;
1610 y = widget->allocation.y;
1611 width = widget->allocation.width;
1612 height = widget->allocation.height;
1613
1614 if (!width)
1615 return;
1616 if (!height)
1617 return;
1618 if (widget->opaque)
1619 return;
1620
1621 cr = cairo_create(window->cairo_surface);
1622
1623 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1624 cairo_set_line_width(cr, 1);
1625
1626 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1627 cairo_rectangle (cr, x, y, 25, 16);
1628
1629 cairo_stroke_preserve(cr);
1630
1631 switch (frame_button->state) {
1632 case FRAME_BUTTON_DEFAULT:
1633 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1634 break;
1635 case FRAME_BUTTON_OVER:
1636 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1637 break;
1638 case FRAME_BUTTON_ACTIVE:
1639 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1640 break;
1641 }
1642
1643 cairo_fill (cr);
1644
1645 x += 4;
1646 }
1647
1648 cairo_set_source_surface(cr, frame_button->icon, x, y);
1649 cairo_paint(cr);
1650
1651 cairo_destroy(cr);
1652}
1653
1654static struct widget *
1655frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1656 enum frame_button_align align, enum frame_button_decoration style)
1657{
1658 struct frame_button *frame_button;
1659 const char *icon = data;
1660
1661 frame_button = malloc (sizeof *frame_button);
1662 memset(frame_button, 0, sizeof *frame_button);
1663
1664 frame_button->icon = cairo_image_surface_create_from_png(icon);
1665 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1666 frame_button->frame = frame;
1667 frame_button->type = type;
1668 frame_button->align = align;
1669 frame_button->decoration = style;
1670
1671 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1672
1673 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1674 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1675 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1676 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001677 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001678 return frame_button->widget;
1679}
1680
1681static void
1682frame_button_destroy(struct frame_button *frame_button)
1683{
1684 widget_destroy(frame_button->widget);
1685 wl_list_remove(&frame_button->link);
1686 cairo_surface_destroy(frame_button->icon);
1687 free(frame_button);
1688
1689 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001690}
1691
1692static void
1693frame_redraw_handler(struct widget *widget, void *data)
1694{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001696 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001697 struct theme *t = window->display->theme;
1698 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001699
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001700 if (window->type == TYPE_FULLSCREEN)
1701 return;
1702
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001703 cr = cairo_create(window->cairo_surface);
1704
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001705 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001706 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001707 if (window->type == TYPE_MAXIMIZED)
1708 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001709 theme_render_frame(t, cr, widget->allocation.width,
1710 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001711
1712 cairo_destroy(cr);
1713}
1714
1715static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001717{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001718 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001719 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001720 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001721
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001722 location = theme_get_location(t, input->sx, input->sy,
1723 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001724 frame->widget->allocation.height,
1725 window->type == TYPE_MAXIMIZED ?
1726 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001727
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001728 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001729 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001730 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001732 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001745 case THEME_LOCATION_EXTERIOR:
1746 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001747 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001748 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001749 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001750}
1751
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752static void
1753frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001754{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001755 struct display *display;
1756
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001757 switch (index) {
1758 case 0: /* close */
1759 if (window->close_handler)
1760 window->close_handler(window->parent,
1761 window->user_data);
1762 else
1763 display_exit(window->display);
1764 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001765 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001766 display = window->display;
1767 if (display->workspace > 0)
1768 workspace_manager_move_surface(display->workspace_manager,
1769 window->surface,
1770 display->workspace - 1);
1771 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001772 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001773 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001774 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001775 workspace_manager_move_surface(display->workspace_manager,
1776 window->surface,
1777 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001778 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001779 case 3: /* fullscreen */
1780 /* we don't have a way to get out of fullscreen for now */
1781 if (window->fullscreen_handler)
1782 window->fullscreen_handler(window, window->user_data);
1783 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001784 }
1785}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001786
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001787void
1788window_show_frame_menu(struct window *window,
1789 struct input *input, uint32_t time)
1790{
1791 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001792 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001793
1794 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001795 "Close",
1796 "Move to workspace above", "Move to workspace below",
1797 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001798 };
1799
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001800 if (window->fullscreen_handler)
1801 count = ARRAY_LENGTH(entries);
1802 else
1803 count = ARRAY_LENGTH(entries) - 1;
1804
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001805 input_get_position(input, &x, &y);
1806 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001807 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001808}
1809
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001810static int
1811frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001812 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001813{
1814 return frame_get_pointer_image_for_location(data, input);
1815}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001816
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001817static int
1818frame_motion_handler(struct widget *widget,
1819 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001820 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001821{
1822 return frame_get_pointer_image_for_location(data, input);
1823}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001824
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001826frame_button_handler(struct widget *widget,
1827 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001828 uint32_t button, enum wl_pointer_button_state state,
1829 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830
1831{
1832 struct frame *frame = data;
1833 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001834 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001835 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001836
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001837 location = theme_get_location(display->theme, input->sx, input->sy,
1838 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001839 frame->widget->allocation.height,
1840 window->type == TYPE_MAXIMIZED ?
1841 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001842
Daniel Stone4dbadb12012-05-30 16:31:51 +01001843 if (window->display->shell && button == BTN_LEFT &&
1844 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001846 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001847 if (!window->shell_surface)
1848 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001849 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001850 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001851 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001853 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001854 case THEME_LOCATION_RESIZING_TOP:
1855 case THEME_LOCATION_RESIZING_BOTTOM:
1856 case THEME_LOCATION_RESIZING_LEFT:
1857 case THEME_LOCATION_RESIZING_RIGHT:
1858 case THEME_LOCATION_RESIZING_TOP_LEFT:
1859 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1860 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1861 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001862 if (!window->shell_surface)
1863 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001865
1866 if (!display->dpy) {
1867 /* If we're using shm, allocate a big
1868 pool to create buffers out of while
1869 we resize. We should probably base
1870 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001871 window->pool =
1872 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001873 }
1874
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001875 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001876 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001878 break;
1879 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001880 } else if (button == BTN_RIGHT &&
1881 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001882 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001883 }
1884}
1885
1886struct widget *
1887frame_create(struct window *window, void *data)
1888{
1889 struct frame *frame;
1890
1891 frame = malloc(sizeof *frame);
1892 memset(frame, 0, sizeof *frame);
1893
1894 frame->widget = window_add_widget(window, frame);
1895 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001896
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1898 widget_set_resize_handler(frame->widget, frame_resize_handler);
1899 widget_set_enter_handler(frame->widget, frame_enter_handler);
1900 widget_set_motion_handler(frame->widget, frame_motion_handler);
1901 widget_set_button_handler(frame->widget, frame_button_handler);
1902
Martin Minarik1998b152012-05-10 02:04:35 +02001903 /* Create empty list for frame buttons */
1904 wl_list_init(&frame->buttons_list);
1905
1906 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1907 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1908
1909 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1910 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1911
1912 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1913 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1914
1915 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1916 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1917
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001918 window->frame = frame;
1919
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001921}
1922
Kristian Høgsberga1627922012-06-20 17:30:03 -04001923void
1924frame_set_child_size(struct widget *widget, int child_width, int child_height)
1925{
1926 struct display *display = widget->window->display;
1927 struct theme *t = display->theme;
1928 int decoration_width, decoration_height;
1929 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001930 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001931
1932 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001933 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001934 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001936
1937 width = child_width + decoration_width;
1938 height = child_height + decoration_height;
1939 } else {
1940 width = child_width;
1941 height = child_height;
1942 }
1943
1944 window_schedule_resize(widget->window, width, height);
1945}
1946
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001947static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001948frame_destroy(struct frame *frame)
1949{
Martin Minarik1998b152012-05-10 02:04:35 +02001950 struct frame_button *button, *tmp;
1951
1952 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1953 frame_button_destroy(button);
1954
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001955 /* frame->child must be destroyed by the application */
1956 widget_destroy(frame->widget);
1957 free(frame);
1958}
1959
1960static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001961input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001962 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001963{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001964 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001965 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001966
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001967 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001968 return;
1969
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001970 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001971 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001972 widget = old;
1973 if (input->grab)
1974 widget = input->grab;
1975 if (widget->leave_handler)
1976 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001977 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001978 }
1979
1980 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001981 widget = focus;
1982 if (input->grab)
1983 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001984 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001985 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001986 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001987 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001988
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001989 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001990 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001991}
1992
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001993void
1994input_grab(struct input *input, struct widget *widget, uint32_t button)
1995{
1996 input->grab = widget;
1997 input->grab_button = button;
1998}
1999
2000void
2001input_ungrab(struct input *input)
2002{
2003 struct widget *widget;
2004
2005 input->grab = NULL;
2006 if (input->pointer_focus) {
2007 widget = widget_find_widget(input->pointer_focus->widget,
2008 input->sx, input->sy);
2009 input_set_focus_widget(input, widget, input->sx, input->sy);
2010 }
2011}
2012
2013static void
2014input_remove_pointer_focus(struct input *input)
2015{
2016 struct window *window = input->pointer_focus;
2017
2018 if (!window)
2019 return;
2020
2021 input_set_focus_widget(input, NULL, 0, 0);
2022
2023 input->pointer_focus = NULL;
2024 input->current_cursor = CURSOR_UNSET;
2025}
2026
2027static void
2028pointer_handle_enter(void *data, struct wl_pointer *pointer,
2029 uint32_t serial, struct wl_surface *surface,
2030 wl_fixed_t sx_w, wl_fixed_t sy_w)
2031{
2032 struct input *input = data;
2033 struct window *window;
2034 struct widget *widget;
2035 float sx = wl_fixed_to_double(sx_w);
2036 float sy = wl_fixed_to_double(sy_w);
2037
2038 if (!surface) {
2039 /* enter event for a window we've just destroyed */
2040 return;
2041 }
2042
2043 input->display->serial = serial;
2044 input->pointer_enter_serial = serial;
2045 input->pointer_focus = wl_surface_get_user_data(surface);
2046 window = input->pointer_focus;
2047
2048 if (window->pool) {
2049 shm_pool_destroy(window->pool);
2050 window->pool = NULL;
2051 /* Schedule a redraw to free the pool */
2052 window_schedule_redraw(window);
2053 }
2054
2055 input->sx = sx;
2056 input->sy = sy;
2057
2058 widget = widget_find_widget(window->widget, sx, sy);
2059 input_set_focus_widget(input, widget, sx, sy);
2060}
2061
2062static void
2063pointer_handle_leave(void *data, struct wl_pointer *pointer,
2064 uint32_t serial, struct wl_surface *surface)
2065{
2066 struct input *input = data;
2067
2068 input->display->serial = serial;
2069 input_remove_pointer_focus(input);
2070}
2071
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002072static void
Daniel Stone37816df2012-05-16 18:45:18 +01002073pointer_handle_motion(void *data, struct wl_pointer *pointer,
2074 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002075{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002076 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002077 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002078 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002080 float sx = wl_fixed_to_double(sx_w);
2081 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002083 input->sx = sx;
2084 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002085
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002086 if (!window)
2087 return;
2088
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002089 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002090 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002091 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002092 }
2093
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 if (input->grab)
2095 widget = input->grab;
2096 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002097 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002098 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002099 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002100 input, time, sx, sy,
2101 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002102
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002103 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002104}
2105
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002106static void
Daniel Stone37816df2012-05-16 18:45:18 +01002107pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002108 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002109{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002110 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002111 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002112 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002113
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002115 if (input->focus_widget && input->grab == NULL &&
2116 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002117 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002118
Neil Roberts6b28aad2012-01-23 19:11:18 +00002119 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002121 (*widget->button_handler)(widget,
2122 input, time,
2123 button, state,
2124 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002125
Daniel Stone4dbadb12012-05-30 16:31:51 +01002126 if (input->grab && input->grab_button == button &&
2127 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002129}
2130
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002133 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002134{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002135 struct input *input = data;
2136 struct widget *widget;
2137
2138 widget = input->focus_widget;
2139 if (input->grab)
2140 widget = input->grab;
2141 if (widget && widget->axis_handler)
2142 (*widget->axis_handler)(widget,
2143 input, time,
2144 axis, value,
2145 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002146}
2147
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002148static const struct wl_pointer_listener pointer_listener = {
2149 pointer_handle_enter,
2150 pointer_handle_leave,
2151 pointer_handle_motion,
2152 pointer_handle_button,
2153 pointer_handle_axis,
2154};
2155
2156static void
2157input_remove_keyboard_focus(struct input *input)
2158{
2159 struct window *window = input->keyboard_focus;
2160 struct itimerspec its;
2161
2162 its.it_interval.tv_sec = 0;
2163 its.it_interval.tv_nsec = 0;
2164 its.it_value.tv_sec = 0;
2165 its.it_value.tv_nsec = 0;
2166 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2167
2168 if (!window)
2169 return;
2170
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002171 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002172 if (window->keyboard_focus_handler)
2173 (*window->keyboard_focus_handler)(window, NULL,
2174 window->user_data);
2175
2176 input->keyboard_focus = NULL;
2177}
2178
2179static void
2180keyboard_repeat_func(struct task *task, uint32_t events)
2181{
2182 struct input *input =
2183 container_of(task, struct input, repeat_task);
2184 struct window *window = input->keyboard_focus;
2185 uint64_t exp;
2186
2187 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2188 /* If we change the timer between the fd becoming
2189 * readable and getting here, there'll be nothing to
2190 * read and we get EAGAIN. */
2191 return;
2192
2193 if (window && window->key_handler) {
2194 (*window->key_handler)(window, input, input->repeat_time,
2195 input->repeat_key, input->repeat_sym,
2196 WL_KEYBOARD_KEY_STATE_PRESSED,
2197 window->user_data);
2198 }
2199}
2200
2201static void
2202keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2203 uint32_t format, int fd, uint32_t size)
2204{
2205 struct input *input = data;
2206 char *map_str;
2207
2208 if (!data) {
2209 close(fd);
2210 return;
2211 }
2212
2213 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2214 close(fd);
2215 return;
2216 }
2217
2218 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2219 if (map_str == MAP_FAILED) {
2220 close(fd);
2221 return;
2222 }
2223
2224 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2225 map_str,
2226 XKB_KEYMAP_FORMAT_TEXT_V1,
2227 0);
2228 munmap(map_str, size);
2229 close(fd);
2230
2231 if (!input->xkb.keymap) {
2232 fprintf(stderr, "failed to compile keymap\n");
2233 return;
2234 }
2235
2236 input->xkb.state = xkb_state_new(input->xkb.keymap);
2237 if (!input->xkb.state) {
2238 fprintf(stderr, "failed to create XKB state\n");
2239 xkb_map_unref(input->xkb.keymap);
2240 input->xkb.keymap = NULL;
2241 return;
2242 }
2243
2244 input->xkb.control_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2246 input->xkb.alt_mask =
2247 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2248 input->xkb.shift_mask =
2249 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2250}
2251
2252static void
2253keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2254 uint32_t serial, struct wl_surface *surface,
2255 struct wl_array *keys)
2256{
2257 struct input *input = data;
2258 struct window *window;
2259
2260 input->display->serial = serial;
2261 input->keyboard_focus = wl_surface_get_user_data(surface);
2262
2263 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002264 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002265 if (window->keyboard_focus_handler)
2266 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002267 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002268}
2269
2270static void
2271keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2272 uint32_t serial, struct wl_surface *surface)
2273{
2274 struct input *input = data;
2275
2276 input->display->serial = serial;
2277 input_remove_keyboard_focus(input);
2278}
2279
Scott Moreau210d0792012-03-22 10:47:01 -06002280static void
Daniel Stone37816df2012-05-16 18:45:18 +01002281keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002282 uint32_t serial, uint32_t time, uint32_t key,
2283 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002284{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002285 struct input *input = data;
2286 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002287 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002288 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002289 const xkb_keysym_t *syms;
2290 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002291 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002292
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002294 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002295 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002296 return;
2297
Daniel Stone97f68542012-05-30 16:32:01 +01002298 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002299
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002300 sym = XKB_KEY_NoSymbol;
2301 if (num_syms == 1)
2302 sym = syms[0];
2303
2304 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002305 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002306 window_set_maximized(window,
2307 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002308 } else if (sym == XKB_KEY_F11 &&
2309 window->fullscreen_handler &&
2310 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2311 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002312 } else if (sym == XKB_KEY_F4 &&
2313 input->modifiers == MOD_ALT_MASK &&
2314 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2315 if (window->close_handler)
2316 window->close_handler(window->parent,
2317 window->user_data);
2318 else
2319 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002320 } else if (window->key_handler) {
2321 (*window->key_handler)(window, input, time, key,
2322 sym, state, window->user_data);
2323 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002324
2325 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2326 key == input->repeat_key) {
2327 its.it_interval.tv_sec = 0;
2328 its.it_interval.tv_nsec = 0;
2329 its.it_value.tv_sec = 0;
2330 its.it_value.tv_nsec = 0;
2331 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2332 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2333 input->repeat_sym = sym;
2334 input->repeat_key = key;
2335 input->repeat_time = time;
2336 its.it_interval.tv_sec = 0;
2337 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2338 its.it_value.tv_sec = 0;
2339 its.it_value.tv_nsec = 400 * 1000 * 1000;
2340 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2341 }
2342}
2343
2344static void
Daniel Stone351eb612012-05-31 15:27:47 -04002345keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2346 uint32_t serial, uint32_t mods_depressed,
2347 uint32_t mods_latched, uint32_t mods_locked,
2348 uint32_t group)
2349{
2350 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002351 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002352
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002353 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2354 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002355 mask = xkb_state_serialize_mods(input->xkb.state,
2356 XKB_STATE_DEPRESSED |
2357 XKB_STATE_LATCHED);
2358 input->modifiers = 0;
2359 if (mask & input->xkb.control_mask)
2360 input->modifiers |= MOD_CONTROL_MASK;
2361 if (mask & input->xkb.alt_mask)
2362 input->modifiers |= MOD_ALT_MASK;
2363 if (mask & input->xkb.shift_mask)
2364 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002365}
2366
Daniel Stone37816df2012-05-16 18:45:18 +01002367static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002368 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002369 keyboard_handle_enter,
2370 keyboard_handle_leave,
2371 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002372 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002373};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002374
2375static void
Daniel Stone37816df2012-05-16 18:45:18 +01002376seat_handle_capabilities(void *data, struct wl_seat *seat,
2377 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002378{
Daniel Stone37816df2012-05-16 18:45:18 +01002379 struct input *input = data;
2380
2381 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2382 input->pointer = wl_seat_get_pointer(seat);
2383 wl_pointer_set_user_data(input->pointer, input);
2384 wl_pointer_add_listener(input->pointer, &pointer_listener,
2385 input);
2386 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2387 wl_pointer_destroy(input->pointer);
2388 input->pointer = NULL;
2389 }
2390
2391 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2392 input->keyboard = wl_seat_get_keyboard(seat);
2393 wl_keyboard_set_user_data(input->keyboard, input);
2394 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2395 input);
2396 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2397 wl_keyboard_destroy(input->keyboard);
2398 input->keyboard = NULL;
2399 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002400}
2401
Daniel Stone37816df2012-05-16 18:45:18 +01002402static const struct wl_seat_listener seat_listener = {
2403 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002404};
2405
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002406void
2407input_get_position(struct input *input, int32_t *x, int32_t *y)
2408{
2409 *x = input->sx;
2410 *y = input->sy;
2411}
2412
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002413struct display *
2414input_get_display(struct input *input)
2415{
2416 return input->display;
2417}
2418
Daniel Stone37816df2012-05-16 18:45:18 +01002419struct wl_seat *
2420input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002421{
Daniel Stone37816df2012-05-16 18:45:18 +01002422 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002423}
2424
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002425uint32_t
2426input_get_modifiers(struct input *input)
2427{
2428 return input->modifiers;
2429}
2430
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002431struct widget *
2432input_get_focus_widget(struct input *input)
2433{
2434 return input->focus_widget;
2435}
2436
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002437struct data_offer {
2438 struct wl_data_offer *offer;
2439 struct input *input;
2440 struct wl_array types;
2441 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002443 struct task io_task;
2444 int fd;
2445 data_func_t func;
2446 int32_t x, y;
2447 void *user_data;
2448};
2449
2450static void
2451data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2452{
2453 struct data_offer *offer = data;
2454 char **p;
2455
2456 p = wl_array_add(&offer->types, sizeof *p);
2457 *p = strdup(type);
2458}
2459
2460static const struct wl_data_offer_listener data_offer_listener = {
2461 data_offer_offer,
2462};
2463
2464static void
2465data_offer_destroy(struct data_offer *offer)
2466{
2467 char **p;
2468
2469 offer->refcount--;
2470 if (offer->refcount == 0) {
2471 wl_data_offer_destroy(offer->offer);
2472 for (p = offer->types.data; *p; p++)
2473 free(*p);
2474 wl_array_release(&offer->types);
2475 free(offer);
2476 }
2477}
2478
2479static void
2480data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002481 struct wl_data_device *data_device,
2482 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002483{
2484 struct data_offer *offer;
2485
2486 offer = malloc(sizeof *offer);
2487
2488 wl_array_init(&offer->types);
2489 offer->refcount = 1;
2490 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002491 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002492 wl_data_offer_add_listener(offer->offer,
2493 &data_offer_listener, offer);
2494}
2495
2496static void
2497data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002498 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002499 wl_fixed_t x_w, wl_fixed_t y_w,
2500 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002501{
2502 struct input *input = data;
2503 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002504 void *types_data;
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);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002507 char **p;
2508
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002509 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002510 window = wl_surface_get_user_data(surface);
2511 input->pointer_focus = window;
2512
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002513 if (offer) {
2514 input->drag_offer = wl_data_offer_get_user_data(offer);
2515
2516 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2517 *p = NULL;
2518
2519 types_data = input->drag_offer->types.data;
2520 } else {
2521 input->drag_offer = NULL;
2522 types_data = NULL;
2523 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002524
2525 window = input->pointer_focus;
2526 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002527 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002528 window->user_data);
2529}
2530
2531static void
2532data_device_leave(void *data, struct wl_data_device *data_device)
2533{
2534 struct input *input = data;
2535
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002536 if (input->drag_offer) {
2537 data_offer_destroy(input->drag_offer);
2538 input->drag_offer = NULL;
2539 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002540}
2541
2542static void
2543data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002544 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002545{
2546 struct input *input = data;
2547 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002548 float x = wl_fixed_to_double(x_w);
2549 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002550 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002551
2552 input->sx = x;
2553 input->sy = y;
2554
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002555 if (input->drag_offer)
2556 types_data = input->drag_offer->types.data;
2557 else
2558 types_data = NULL;
2559
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002560 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002561 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002562 window->user_data);
2563}
2564
2565static void
2566data_device_drop(void *data, struct wl_data_device *data_device)
2567{
2568 struct input *input = data;
2569 struct window *window = input->pointer_focus;
2570
2571 if (window->drop_handler)
2572 window->drop_handler(window, input,
2573 input->sx, input->sy, window->user_data);
2574}
2575
2576static void
2577data_device_selection(void *data,
2578 struct wl_data_device *wl_data_device,
2579 struct wl_data_offer *offer)
2580{
2581 struct input *input = data;
2582 char **p;
2583
2584 if (input->selection_offer)
2585 data_offer_destroy(input->selection_offer);
2586
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002587 if (offer) {
2588 input->selection_offer = wl_data_offer_get_user_data(offer);
2589 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2590 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002591 } else {
2592 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002593 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002594}
2595
2596static const struct wl_data_device_listener data_device_listener = {
2597 data_device_data_offer,
2598 data_device_enter,
2599 data_device_leave,
2600 data_device_motion,
2601 data_device_drop,
2602 data_device_selection
2603};
2604
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002605static void
2606input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002608 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002609 struct wl_cursor *cursor;
2610 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002611
Daniel Stone80972742012-11-07 17:51:39 +11002612 if (!input->pointer)
2613 return;
2614
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002615 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002616 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002617 return;
2618
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002619 if (index >= (int) cursor->image_count) {
2620 fprintf(stderr, "cursor index out of range\n");
2621 return;
2622 }
2623
2624 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002625 buffer = wl_cursor_image_get_buffer(image);
2626 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002627 return;
2628
Kristian Høgsbergae277372012-08-01 09:41:08 -04002629 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002630 input->pointer_surface,
2631 image->hotspot_x, image->hotspot_y);
2632 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2633 wl_surface_damage(input->pointer_surface, 0, 0,
2634 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002635 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002636}
2637
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002638static const struct wl_callback_listener pointer_surface_listener;
2639
2640static void
2641pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2642 uint32_t time)
2643{
2644 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002645 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002646 int i;
2647
2648 if (callback) {
2649 assert(callback == input->cursor_frame_cb);
2650 wl_callback_destroy(callback);
2651 input->cursor_frame_cb = NULL;
2652 }
2653
Daniel Stone80972742012-11-07 17:51:39 +11002654 if (!input->pointer)
2655 return;
2656
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002657 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002658 wl_pointer_set_cursor(input->pointer,
2659 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002660 NULL, 0, 0);
2661 return;
2662 }
2663
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002664 if (input->current_cursor == CURSOR_UNSET)
2665 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002666 cursor = input->display->cursors[input->current_cursor];
2667 if (!cursor)
2668 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002669
2670 /* FIXME We don't have the current time on the first call so we set
2671 * the animation start to the time of the first frame callback. */
2672 if (time == 0)
2673 input->cursor_anim_start = 0;
2674 else if (input->cursor_anim_start == 0)
2675 input->cursor_anim_start = time;
2676
2677 if (time == 0 || input->cursor_anim_start == 0)
2678 i = 0;
2679 else
2680 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2681
Pekka Paalanenbc106382012-10-10 12:49:31 +03002682 if (cursor->image_count > 1) {
2683 input->cursor_frame_cb =
2684 wl_surface_frame(input->pointer_surface);
2685 wl_callback_add_listener(input->cursor_frame_cb,
2686 &pointer_surface_listener, input);
2687 }
2688
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002689 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002690}
2691
2692static const struct wl_callback_listener pointer_surface_listener = {
2693 pointer_surface_frame_callback
2694};
2695
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002696void
2697input_set_pointer_image(struct input *input, int pointer)
2698{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002699 int force = 0;
2700
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002701 if (!input->pointer)
2702 return;
2703
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002704 if (input->pointer_enter_serial > input->cursor_serial)
2705 force = 1;
2706
2707 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002708 return;
2709
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002710 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002711 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002712 if (!input->cursor_frame_cb)
2713 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002714 else if (force) {
2715 /* The current frame callback may be stuck if, for instance,
2716 * the set cursor request was processed by the server after
2717 * this client lost the focus. In this case the cursor surface
2718 * might not be mapped and the frame callback wouldn't ever
2719 * complete. Send a set_cursor and attach to try to map the
2720 * cursor surface again so that the callback will finish */
2721 input_set_pointer_image_index(input, 0);
2722 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002723}
2724
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002725struct wl_data_device *
2726input_get_data_device(struct input *input)
2727{
2728 return input->data_device;
2729}
2730
2731void
2732input_set_selection(struct input *input,
2733 struct wl_data_source *source, uint32_t time)
2734{
2735 wl_data_device_set_selection(input->data_device, source, time);
2736}
2737
2738void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002740{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002741 wl_data_offer_accept(input->drag_offer->offer,
2742 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002743}
2744
2745static void
2746offer_io_func(struct task *task, uint32_t events)
2747{
2748 struct data_offer *offer =
2749 container_of(task, struct data_offer, io_task);
2750 unsigned int len;
2751 char buffer[4096];
2752
2753 len = read(offer->fd, buffer, sizeof buffer);
2754 offer->func(buffer, len,
2755 offer->x, offer->y, offer->user_data);
2756
2757 if (len == 0) {
2758 close(offer->fd);
2759 data_offer_destroy(offer);
2760 }
2761}
2762
2763static void
2764data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2765 data_func_t func, void *user_data)
2766{
2767 int p[2];
2768
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002769 if (pipe2(p, O_CLOEXEC) == -1)
2770 return;
2771
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002772 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2773 close(p[1]);
2774
2775 offer->io_task.run = offer_io_func;
2776 offer->fd = p[0];
2777 offer->func = func;
2778 offer->refcount++;
2779 offer->user_data = user_data;
2780
2781 display_watch_fd(offer->input->display,
2782 offer->fd, EPOLLIN, &offer->io_task);
2783}
2784
2785void
2786input_receive_drag_data(struct input *input, const char *mime_type,
2787 data_func_t func, void *data)
2788{
2789 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2790 input->drag_offer->x = input->sx;
2791 input->drag_offer->y = input->sy;
2792}
2793
2794int
2795input_receive_selection_data(struct input *input, const char *mime_type,
2796 data_func_t func, void *data)
2797{
2798 char **p;
2799
2800 if (input->selection_offer == NULL)
2801 return -1;
2802
2803 for (p = input->selection_offer->types.data; *p; p++)
2804 if (strcmp(mime_type, *p) == 0)
2805 break;
2806
2807 if (*p == NULL)
2808 return -1;
2809
2810 data_offer_receive_data(input->selection_offer,
2811 mime_type, func, data);
2812 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002813}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002814
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002815int
2816input_receive_selection_data_to_fd(struct input *input,
2817 const char *mime_type, int fd)
2818{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002819 if (input->selection_offer)
2820 wl_data_offer_receive(input->selection_offer->offer,
2821 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002822
2823 return 0;
2824}
2825
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002826void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002827window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002828{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002829 if (!window->shell_surface)
2830 return;
2831
Daniel Stone37816df2012-05-16 18:45:18 +01002832 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002833}
2834
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002835static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002836idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002837{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002838 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002839
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002840 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002841 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002842 widget_set_allocation(widget,
2843 window->pending_allocation.x,
2844 window->pending_allocation.y,
2845 window->pending_allocation.width,
2846 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002847
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002848 if (window->input_region) {
2849 wl_region_destroy(window->input_region);
2850 window->input_region = NULL;
2851 }
2852
2853 if (window->opaque_region) {
2854 wl_region_destroy(window->opaque_region);
2855 window->opaque_region = NULL;
2856 }
2857
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002858 if (widget->resize_handler)
2859 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002860 widget->allocation.width,
2861 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002862 widget->user_data);
2863
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002864 if (window->allocation.width != widget->allocation.width ||
2865 window->allocation.height != widget->allocation.height) {
2866 window->allocation = widget->allocation;
2867 window_schedule_redraw(window);
2868 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002869}
2870
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002871void
2872window_schedule_resize(struct window *window, int width, int height)
2873{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002874 window->pending_allocation.x = 0;
2875 window->pending_allocation.y = 0;
2876 window->pending_allocation.width = width;
2877 window->pending_allocation.height = height;
2878
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002879 if (window->min_allocation.width == 0)
2880 window->min_allocation = window->pending_allocation;
2881 if (window->pending_allocation.width < window->min_allocation.width)
2882 window->pending_allocation.width = window->min_allocation.width;
2883 if (window->pending_allocation.height < window->min_allocation.height)
2884 window->pending_allocation.height = window->min_allocation.height;
2885
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002886 window->resize_needed = 1;
2887 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002888}
2889
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002890void
2891widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2892{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002893 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002894}
2895
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002896static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002897handle_ping(void *data, struct wl_shell_surface *shell_surface,
2898 uint32_t serial)
2899{
2900 wl_shell_surface_pong(shell_surface, serial);
2901}
2902
2903static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002904handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002905 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002906{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002907 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002908
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002909 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002910 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002911}
2912
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002913static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002914menu_destroy(struct menu *menu)
2915{
2916 widget_destroy(menu->widget);
2917 window_destroy(menu->window);
2918 free(menu);
2919}
2920
2921static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2923{
2924 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002925 struct menu *menu = window->widget->user_data;
2926
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002928 * device. Or just use wl_callback. And this really needs to
2929 * be a window vfunc that the menu can set. And we need the
2930 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002932 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002933 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002934 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002935}
2936
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002937static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002938 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002939 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002940 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002941};
2942
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002943void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002944window_get_allocation(struct window *window,
2945 struct rectangle *allocation)
2946{
2947 *allocation = window->allocation;
2948}
2949
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002950static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002951widget_redraw(struct widget *widget)
2952{
2953 struct widget *child;
2954
2955 if (widget->redraw_handler)
2956 widget->redraw_handler(widget, widget->user_data);
2957 wl_list_for_each(child, &widget->child_list, link)
2958 widget_redraw(child);
2959}
2960
2961static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002962frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2963{
2964 struct window *window = data;
2965
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002966 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002967 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002968 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002969 window->redraw_scheduled = 0;
2970 if (window->redraw_needed)
2971 window_schedule_redraw(window);
2972}
2973
2974static const struct wl_callback_listener listener = {
2975 frame_callback
2976};
2977
2978static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002979idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002980{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002981 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002982
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002983 if (window->resize_needed)
2984 idle_resize(window);
2985
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002986 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002987 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002988 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002989 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002990
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002991 window->frame_cb = wl_surface_frame(window->surface);
2992 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002993 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002994}
2995
2996void
2997window_schedule_redraw(struct window *window)
2998{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002999 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003000 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003001 window->redraw_task.run = idle_redraw;
3002 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003003 window->redraw_scheduled = 1;
3004 }
3005}
3006
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003007int
3008window_is_fullscreen(struct window *window)
3009{
3010 return window->type == TYPE_FULLSCREEN;
3011}
3012
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003013void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003014window_set_fullscreen(struct window *window, int fullscreen)
3015{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003016 if (!window->display->shell)
3017 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003018
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003019 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003020 return;
3021
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003022 if (fullscreen) {
3023 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003024 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003025 wl_shell_surface_set_fullscreen(window->shell_surface,
3026 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3027 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003028 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003029 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003030 wl_shell_surface_set_toplevel(window->shell_surface);
3031 window_schedule_resize(window,
3032 window->saved_allocation.width,
3033 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003034 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003035}
3036
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003037int
3038window_is_maximized(struct window *window)
3039{
3040 return window->type == TYPE_MAXIMIZED;
3041}
3042
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003043void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003044window_set_maximized(struct window *window, int maximized)
3045{
3046 if (!window->display->shell)
3047 return;
3048
3049 if ((window->type == TYPE_MAXIMIZED) == maximized)
3050 return;
3051
3052 if (window->type == TYPE_TOPLEVEL) {
3053 window->saved_allocation = window->allocation;
3054 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3055 window->type = TYPE_MAXIMIZED;
3056 } else {
3057 wl_shell_surface_set_toplevel(window->shell_surface);
3058 window->type = TYPE_TOPLEVEL;
3059 window_schedule_resize(window,
3060 window->saved_allocation.width,
3061 window->saved_allocation.height);
3062 }
3063}
3064
3065void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003066window_set_user_data(struct window *window, void *data)
3067{
3068 window->user_data = data;
3069}
3070
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003071void *
3072window_get_user_data(struct window *window)
3073{
3074 return window->user_data;
3075}
3076
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003077void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003078window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003079 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003080{
3081 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003082}
3083
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003084void
3085window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003086 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003087{
3088 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003089}
3090
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003091void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003092window_set_data_handler(struct window *window, window_data_handler_t handler)
3093{
3094 window->data_handler = handler;
3095}
3096
3097void
3098window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3099{
3100 window->drop_handler = handler;
3101}
3102
3103void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003104window_set_close_handler(struct window *window,
3105 window_close_handler_t handler)
3106{
3107 window->close_handler = handler;
3108}
3109
3110void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003111window_set_fullscreen_handler(struct window *window,
3112 window_fullscreen_handler_t handler)
3113{
3114 window->fullscreen_handler = handler;
3115}
3116
3117void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003118window_set_title(struct window *window, const char *title)
3119{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003120 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003121 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003122 if (window->shell_surface)
3123 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003124}
3125
3126const char *
3127window_get_title(struct window *window)
3128{
3129 return window->title;
3130}
3131
3132void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003133window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3134{
3135 struct text_cursor_position *text_cursor_position =
3136 window->display->text_cursor_position;
3137
Scott Moreau9295ce02012-06-01 12:46:10 -06003138 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003139 return;
3140
3141 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003142 window->surface,
3143 wl_fixed_from_int(x),
3144 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003145}
3146
3147void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003148window_damage(struct window *window, int32_t x, int32_t y,
3149 int32_t width, int32_t height)
3150{
3151 wl_surface_damage(window->surface, x, y, width, height);
3152}
3153
Casey Dahlin9074db52012-04-19 22:50:09 -04003154static void
3155surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003156 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003157{
Rob Bradford7507b572012-05-15 17:55:34 +01003158 struct window *window = data;
3159 struct output *output;
3160 struct output *output_found = NULL;
3161 struct window_output *window_output;
3162
3163 wl_list_for_each(output, &window->display->output_list, link) {
3164 if (output->output == wl_output) {
3165 output_found = output;
3166 break;
3167 }
3168 }
3169
3170 if (!output_found)
3171 return;
3172
3173 window_output = malloc (sizeof *window_output);
3174 window_output->output = output_found;
3175
3176 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003177}
3178
3179static void
3180surface_leave(void *data,
3181 struct wl_surface *wl_surface, struct wl_output *output)
3182{
Rob Bradford7507b572012-05-15 17:55:34 +01003183 struct window *window = data;
3184 struct window_output *window_output;
3185 struct window_output *window_output_found = NULL;
3186
3187 wl_list_for_each(window_output, &window->window_output_list, link) {
3188 if (window_output->output->output == output) {
3189 window_output_found = window_output;
3190 break;
3191 }
3192 }
3193
3194 if (window_output_found) {
3195 wl_list_remove(&window_output_found->link);
3196 free(window_output_found);
3197 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003198}
3199
3200static const struct wl_surface_listener surface_listener = {
3201 surface_enter,
3202 surface_leave
3203};
3204
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003205static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003206window_create_internal(struct display *display,
3207 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003208{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003209 struct window *window;
3210
3211 window = malloc(sizeof *window);
3212 if (window == NULL)
3213 return NULL;
3214
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003215 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003216 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003217 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003218 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003219 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003220 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003221 window->shell_surface =
3222 wl_shell_get_shell_surface(display->shell,
3223 window->surface);
3224 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003225 window->allocation.x = 0;
3226 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003227 window->allocation.width = 0;
3228 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003229 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003230 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003231 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003232 window->input_region = NULL;
3233 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003234
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003235 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003236#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003237 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003238#else
3239 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3240#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003241 else
3242 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003243
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003244 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003245 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003246 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003247
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003248 if (window->shell_surface) {
3249 wl_shell_surface_set_user_data(window->shell_surface, window);
3250 wl_shell_surface_add_listener(window->shell_surface,
3251 &shell_surface_listener, window);
3252 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003253
Rob Bradford7507b572012-05-15 17:55:34 +01003254 wl_list_init (&window->window_output_list);
3255
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003256 return window;
3257}
3258
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003259struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003260window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003261{
3262 struct window *window;
3263
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003264 window = window_create_internal(display, NULL, TYPE_NONE);
3265 if (!window)
3266 return NULL;
3267
3268 return window;
3269}
3270
3271struct window *
3272window_create_custom(struct display *display)
3273{
3274 struct window *window;
3275
3276 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003277 if (!window)
3278 return NULL;
3279
3280 return window;
3281}
3282
3283struct window *
3284window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003285 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003286{
3287 struct window *window;
3288
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003289 window = window_create_internal(parent->display,
3290 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003291 if (!window)
3292 return NULL;
3293
3294 window->x = x;
3295 window->y = y;
3296
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003297 if (display->shell)
3298 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003299 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003300 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003301
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003302 return window;
3303}
3304
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003305static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003306menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003307{
3308 int next;
3309
3310 next = (sy - 8) / 20;
3311 if (menu->current != next) {
3312 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003313 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003314 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315}
3316
3317static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003318menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003320 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003321{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003322 struct menu *menu = data;
3323
3324 if (widget == menu->widget)
3325 menu_set_item(data, y);
3326
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003327 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003328}
3329
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003330static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003331menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003332 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003333{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003334 struct menu *menu = data;
3335
3336 if (widget == menu->widget)
3337 menu_set_item(data, y);
3338
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003339 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003340}
3341
3342static void
3343menu_leave_handler(struct widget *widget, struct input *input, void *data)
3344{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003345 struct menu *menu = data;
3346
3347 if (widget == menu->widget)
3348 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003349}
3350
3351static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003352menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003354 uint32_t button, enum wl_pointer_button_state state,
3355 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003356
3357{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003358 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003359
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003360 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3361 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003362 /* Either relase after press-drag-release or
3363 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003364 menu->func(menu->window->parent,
3365 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003366 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003367 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003368 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003369 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003370 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003371}
3372
3373static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003374menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003375{
3376 cairo_t *cr;
3377 const int32_t r = 3, margin = 3;
3378 struct menu *menu = data;
3379 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003380 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003381
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003382 cr = cairo_create(window->cairo_surface);
3383 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3384 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3385 cairo_paint(cr);
3386
3387 width = window->allocation.width;
3388 height = window->allocation.height;
3389 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003390
3391 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003392 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3393 cairo_fill(cr);
3394
3395 for (i = 0; i < menu->count; i++) {
3396 if (i == menu->current) {
3397 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3398 cairo_rectangle(cr, margin, i * 20 + margin,
3399 width - 2 * margin, 20);
3400 cairo_fill(cr);
3401 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3402 cairo_move_to(cr, 10, i * 20 + 16);
3403 cairo_show_text(cr, menu->entries[i]);
3404 } else {
3405 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3406 cairo_move_to(cr, 10, i * 20 + 16);
3407 cairo_show_text(cr, menu->entries[i]);
3408 }
3409 }
3410
3411 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003412}
3413
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003414void
3415window_show_menu(struct display *display,
3416 struct input *input, uint32_t time, struct window *parent,
3417 int32_t x, int32_t y,
3418 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003419{
3420 struct window *window;
3421 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003422 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003423
3424 menu = malloc(sizeof *menu);
3425 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003426 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003427
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003428 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003429 if (!window) {
3430 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003431 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003432 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003433
3434 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003435 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003436 menu->entries = entries;
3437 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003438 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003439 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003440 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003441 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003442 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003443 window->type = TYPE_MENU;
3444 window->x = x;
3445 window->y = y;
3446
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003447 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003448 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003449 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003450 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003451 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003452
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003453 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003454 widget_set_enter_handler(menu->widget, menu_enter_handler);
3455 widget_set_leave_handler(menu->widget, menu_leave_handler);
3456 widget_set_motion_handler(menu->widget, menu_motion_handler);
3457 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003458
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003459 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003460 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003461}
3462
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003463void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003464window_set_buffer_type(struct window *window, enum window_buffer_type type)
3465{
3466 window->buffer_type = type;
3467}
3468
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003469
3470static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003471display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003472 struct wl_output *wl_output,
3473 int x, int y,
3474 int physical_width,
3475 int physical_height,
3476 int subpixel,
3477 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003478 const char *model,
3479 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003480{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003481 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003482
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003483 output->allocation.x = x;
3484 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003485 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003486}
3487
3488static void
3489display_handle_mode(void *data,
3490 struct wl_output *wl_output,
3491 uint32_t flags,
3492 int width,
3493 int height,
3494 int refresh)
3495{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003496 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003497 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003498
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003499 if (flags & WL_OUTPUT_MODE_CURRENT) {
3500 output->allocation.width = width;
3501 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003502 if (display->output_configure_handler)
3503 (*display->output_configure_handler)(
3504 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003505 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003506}
3507
3508static const struct wl_output_listener output_listener = {
3509 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003510 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003511};
3512
3513static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003514display_add_output(struct display *d, uint32_t id)
3515{
3516 struct output *output;
3517
3518 output = malloc(sizeof *output);
3519 if (output == NULL)
3520 return;
3521
3522 memset(output, 0, sizeof *output);
3523 output->display = d;
3524 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003525 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003526 wl_list_insert(d->output_list.prev, &output->link);
3527
3528 wl_output_add_listener(output->output, &output_listener, output);
3529}
3530
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003531static void
3532output_destroy(struct output *output)
3533{
3534 if (output->destroy_handler)
3535 (*output->destroy_handler)(output, output->user_data);
3536
3537 wl_output_destroy(output->output);
3538 wl_list_remove(&output->link);
3539 free(output);
3540}
3541
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003542void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003543display_set_global_handler(struct display *display,
3544 display_global_handler_t handler)
3545{
3546 struct global *global;
3547
3548 display->global_handler = handler;
3549 if (!handler)
3550 return;
3551
3552 wl_list_for_each(global, &display->global_list, link)
3553 display->global_handler(display,
3554 global->name, global->interface,
3555 global->version, display->user_data);
3556}
3557
3558void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003559display_set_output_configure_handler(struct display *display,
3560 display_output_handler_t handler)
3561{
3562 struct output *output;
3563
3564 display->output_configure_handler = handler;
3565 if (!handler)
3566 return;
3567
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003568 wl_list_for_each(output, &display->output_list, link) {
3569 if (output->allocation.width == 0 &&
3570 output->allocation.height == 0)
3571 continue;
3572
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003573 (*display->output_configure_handler)(output,
3574 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003575 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003576}
3577
3578void
3579output_set_user_data(struct output *output, void *data)
3580{
3581 output->user_data = data;
3582}
3583
3584void *
3585output_get_user_data(struct output *output)
3586{
3587 return output->user_data;
3588}
3589
3590void
3591output_set_destroy_handler(struct output *output,
3592 display_output_handler_t handler)
3593{
3594 output->destroy_handler = handler;
3595 /* FIXME: implement this, once we have way to remove outputs */
3596}
3597
3598void
Scott Moreau4e072362012-09-29 02:03:11 -06003599output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003600{
Scott Moreau4e072362012-09-29 02:03:11 -06003601 struct rectangle allocation = output->allocation;
3602
3603 switch (output->transform) {
3604 case WL_OUTPUT_TRANSFORM_90:
3605 case WL_OUTPUT_TRANSFORM_270:
3606 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3607 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3608 /* Swap width and height */
3609 allocation.width = output->allocation.height;
3610 allocation.height = output->allocation.width;
3611 break;
3612 }
3613
3614 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003615}
3616
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003617struct wl_output *
3618output_get_wl_output(struct output *output)
3619{
3620 return output->output;
3621}
3622
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003623static void
Daniel Stone97f68542012-05-30 16:32:01 +01003624fini_xkb(struct input *input)
3625{
3626 xkb_state_unref(input->xkb.state);
3627 xkb_map_unref(input->xkb.keymap);
3628}
3629
3630static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003631display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003632{
3633 struct input *input;
3634
3635 input = malloc(sizeof *input);
3636 if (input == NULL)
3637 return;
3638
3639 memset(input, 0, sizeof *input);
3640 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003641 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003642 input->pointer_focus = NULL;
3643 input->keyboard_focus = NULL;
3644 wl_list_insert(d->input_list.prev, &input->link);
3645
Daniel Stone37816df2012-05-16 18:45:18 +01003646 wl_seat_add_listener(input->seat, &seat_listener, input);
3647 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003648
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003649 input->data_device =
3650 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003651 input->seat);
3652 wl_data_device_add_listener(input->data_device, &data_device_listener,
3653 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003654
3655 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003656
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003657 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3658 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003659 input->repeat_task.run = keyboard_repeat_func;
3660 display_watch_fd(d, input->repeat_timer_fd,
3661 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003662}
3663
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003664static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003665input_destroy(struct input *input)
3666{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003667 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003668 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003669
3670 if (input->drag_offer)
3671 data_offer_destroy(input->drag_offer);
3672
3673 if (input->selection_offer)
3674 data_offer_destroy(input->selection_offer);
3675
3676 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003677 fini_xkb(input);
3678
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003679 wl_surface_destroy(input->pointer_surface);
3680
Pekka Paalanene1207c72011-12-16 12:02:09 +02003681 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003682 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003683 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003684 free(input);
3685}
3686
3687static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003688init_workspace_manager(struct display *d, uint32_t id)
3689{
3690 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003691 wl_registry_bind(d->registry, id,
3692 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003693 if (d->workspace_manager != NULL)
3694 workspace_manager_add_listener(d->workspace_manager,
3695 &workspace_manager_listener,
3696 d);
3697}
3698
3699static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003700registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3701 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003702{
3703 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003704 struct global *global;
3705
3706 global = malloc(sizeof *global);
3707 global->name = id;
3708 global->interface = strdup(interface);
3709 global->version = version;
3710 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003711
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003712 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003713 d->compositor = wl_registry_bind(registry, id,
3714 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003715 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003716 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003717 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003718 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003719 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003720 d->shell = wl_registry_bind(registry,
3721 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003722 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003723 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003724 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3725 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003726 wl_registry_bind(registry, id,
3727 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003728 } else if (strcmp(interface, "text_cursor_position") == 0) {
3729 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003730 wl_registry_bind(registry, id,
3731 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003732 } else if (strcmp(interface, "workspace_manager") == 0) {
3733 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003734 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003735
3736 if (d->global_handler)
3737 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003738}
3739
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003740void *
3741display_bind(struct display *display, uint32_t name,
3742 const struct wl_interface *interface, uint32_t version)
3743{
3744 return wl_registry_bind(display->registry, name, interface, version);
3745}
3746
3747static const struct wl_registry_listener registry_listener = {
3748 registry_handle_global
3749};
3750
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003751#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003752static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003753init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003754{
3755 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003756 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003757
Rob Clark6396ed32012-03-11 19:48:41 -05003758#ifdef USE_CAIRO_GLESV2
3759# define GL_BIT EGL_OPENGL_ES2_BIT
3760#else
3761# define GL_BIT EGL_OPENGL_BIT
3762#endif
3763
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003764 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003765 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003766 EGL_RED_SIZE, 1,
3767 EGL_GREEN_SIZE, 1,
3768 EGL_BLUE_SIZE, 1,
3769 EGL_ALPHA_SIZE, 1,
3770 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003771 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003772 EGL_NONE
3773 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003774
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003775#ifdef USE_CAIRO_GLESV2
3776 static const EGLint context_attribs[] = {
3777 EGL_CONTEXT_CLIENT_VERSION, 2,
3778 EGL_NONE
3779 };
3780 EGLint api = EGL_OPENGL_ES_API;
3781#else
3782 EGLint *context_attribs = NULL;
3783 EGLint api = EGL_OPENGL_API;
3784#endif
3785
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003786 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003787 if (!eglInitialize(d->dpy, &major, &minor)) {
3788 fprintf(stderr, "failed to initialize display\n");
3789 return -1;
3790 }
3791
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003792 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003793 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3794 return -1;
3795 }
3796
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003797 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3798 &d->argb_config, 1, &n) || n != 1) {
3799 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003800 return -1;
3801 }
3802
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003803 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003804 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003805 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003806 fprintf(stderr, "failed to create context\n");
3807 return -1;
3808 }
3809
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003810 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003811 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003812 return -1;
3813 }
3814
Benjamin Franzke0c991632011-09-27 21:57:31 +02003815 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3816 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3817 fprintf(stderr, "failed to get cairo egl argb device\n");
3818 return -1;
3819 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003820
3821 return 0;
3822}
3823
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003824static void
3825fini_egl(struct display *display)
3826{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003827 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003828
3829 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3830 EGL_NO_CONTEXT);
3831
3832 eglTerminate(display->dpy);
3833 eglReleaseThread();
3834}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003835#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003836
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003837static void
3838handle_display_data(struct task *task, uint32_t events)
3839{
3840 struct display *display =
3841 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003842 struct epoll_event ep;
3843 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003844
3845 display->display_fd_events = events;
3846
3847 if (events & EPOLLERR || events & EPOLLHUP) {
3848 display_exit(display);
3849 return;
3850 }
3851
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003852 if (events & EPOLLIN) {
3853 ret = wl_display_dispatch(display->display);
3854 if (ret == -1) {
3855 display_exit(display);
3856 return;
3857 }
3858 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003859
3860 if (events & EPOLLOUT) {
3861 ret = wl_display_flush(display->display);
3862 if (ret == 0) {
3863 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3864 ep.data.ptr = &display->display_task;
3865 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3866 display->display_fd, &ep);
3867 } else if (ret == -1 && errno != EAGAIN) {
3868 display_exit(display);
3869 return;
3870 }
3871 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003872}
3873
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003874struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003875display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003876{
3877 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003878
Pekka Paalanen32127ca2012-11-19 15:32:51 +02003879 assert(&egl_window_surface_data_key != &shm_surface_data_key);
3880
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003881 d = malloc(sizeof *d);
3882 if (d == NULL)
3883 return NULL;
3884
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003885 memset(d, 0, sizeof *d);
3886
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003887 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003888 if (d->display == NULL) {
3889 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003890 return NULL;
3891 }
3892
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003893 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003894 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003895 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003896 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3897 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003898
3899 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003900 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003901 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003902 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003903
Daniel Stone97f68542012-05-30 16:32:01 +01003904 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003905 if (d->xkb_context == NULL) {
3906 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003907 return NULL;
3908 }
3909
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003910 d->workspace = 0;
3911 d->workspace_count = 1;
3912
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003913 d->registry = wl_display_get_registry(d->display);
3914 wl_registry_add_listener(d->registry, &registry_listener, d);
3915 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003916#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003917 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003918 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003919#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003920
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003921 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003922
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003923 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003924
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003925 wl_list_init(&d->window_list);
3926
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003927 return d;
3928}
3929
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003930static void
3931display_destroy_outputs(struct display *display)
3932{
3933 struct output *tmp;
3934 struct output *output;
3935
3936 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3937 output_destroy(output);
3938}
3939
Pekka Paalanene1207c72011-12-16 12:02:09 +02003940static void
3941display_destroy_inputs(struct display *display)
3942{
3943 struct input *tmp;
3944 struct input *input;
3945
3946 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3947 input_destroy(input);
3948}
3949
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003950void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003951display_destroy(struct display *display)
3952{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003953 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003954 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3955 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003956
3957 if (!wl_list_empty(&display->deferred_list))
3958 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3959
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003960 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003961 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003962
Daniel Stone97f68542012-05-30 16:32:01 +01003963 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003964
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003965 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003966 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003967
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003968#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003969 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003970#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003971
Pekka Paalanenc2052982011-12-16 11:41:32 +02003972 if (display->shell)
3973 wl_shell_destroy(display->shell);
3974
3975 if (display->shm)
3976 wl_shm_destroy(display->shm);
3977
3978 if (display->data_device_manager)
3979 wl_data_device_manager_destroy(display->data_device_manager);
3980
3981 wl_compositor_destroy(display->compositor);
3982
3983 close(display->epoll_fd);
3984
U. Artie Eoff44874d92012-10-02 21:12:35 -07003985 if (!(display->display_fd_events & EPOLLERR) &&
3986 !(display->display_fd_events & EPOLLHUP))
3987 wl_display_flush(display->display);
3988
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003989 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003990 free(display);
3991}
3992
3993void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003994display_set_user_data(struct display *display, void *data)
3995{
3996 display->user_data = data;
3997}
3998
3999void *
4000display_get_user_data(struct display *display)
4001{
4002 return display->user_data;
4003}
4004
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004005struct wl_display *
4006display_get_display(struct display *display)
4007{
4008 return display->display;
4009}
4010
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004011struct output *
4012display_get_output(struct display *display)
4013{
4014 return container_of(display->output_list.next, struct output, link);
4015}
4016
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004017struct wl_compositor *
4018display_get_compositor(struct display *display)
4019{
4020 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004021}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004022
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004023uint32_t
4024display_get_serial(struct display *display)
4025{
4026 return display->serial;
4027}
4028
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004029EGLDisplay
4030display_get_egl_display(struct display *d)
4031{
4032 return d->dpy;
4033}
4034
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004035struct wl_data_source *
4036display_create_data_source(struct display *display)
4037{
4038 return wl_data_device_manager_create_data_source(display->data_device_manager);
4039}
4040
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004041EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004042display_get_argb_egl_config(struct display *d)
4043{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004044 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004045}
4046
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004047struct wl_shell *
4048display_get_shell(struct display *display)
4049{
4050 return display->shell;
4051}
4052
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004053int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004054display_acquire_window_surface(struct display *display,
4055 struct window *window,
4056 EGLContext ctx)
4057{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004058#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004059 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004060 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004061
4062 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004063 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004064 device = cairo_surface_get_device(window->cairo_surface);
4065 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004066 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004067
Benjamin Franzke0c991632011-09-27 21:57:31 +02004068 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004069 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004070 ctx = display->argb_ctx;
4071 else
4072 assert(0);
4073 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004074
4075 data = cairo_surface_get_user_data(window->cairo_surface,
Pekka Paalanen32127ca2012-11-19 15:32:51 +02004076 &egl_window_surface_data_key);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004077
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004078 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004079 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004080 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4081 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004082
4083 return 0;
4084#else
4085 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004086#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004087}
4088
4089void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004090display_release_window_surface(struct display *display,
4091 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004092{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004093#ifdef HAVE_CAIRO_EGL
4094 cairo_device_t *device;
4095
4096 device = cairo_surface_get_device(window->cairo_surface);
4097 if (!device)
4098 return;
4099
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004100 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004101 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004102 cairo_device_release(device);
4103#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004104}
4105
4106void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004107display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004108{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004109 wl_list_insert(&display->deferred_list, &task->link);
4110}
4111
4112void
4113display_watch_fd(struct display *display,
4114 int fd, uint32_t events, struct task *task)
4115{
4116 struct epoll_event ep;
4117
4118 ep.events = events;
4119 ep.data.ptr = task;
4120 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4121}
4122
4123void
4124display_run(struct display *display)
4125{
4126 struct task *task;
4127 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004128 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004129
Pekka Paalanen826d7952011-12-15 10:14:07 +02004130 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004131 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004132 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004133 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004134 struct task, link);
4135 wl_list_remove(&task->link);
4136 task->run(task, 0);
4137 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004138
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004139 wl_display_dispatch_pending(display->display);
4140
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004141 if (!display->running)
4142 break;
4143
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004144 ret = wl_display_flush(display->display);
4145 if (ret < 0 && errno == EAGAIN) {
4146 ep[0].events =
4147 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4148 ep[0].data.ptr = &display->display_task;
4149
4150 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4151 display->display_fd, &ep[0]);
4152 } else if (ret < 0) {
4153 break;
4154 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004155
4156 count = epoll_wait(display->epoll_fd,
4157 ep, ARRAY_LENGTH(ep), -1);
4158 for (i = 0; i < count; i++) {
4159 task = ep[i].data.ptr;
4160 task->run(task, ep[i].events);
4161 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004162 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004163}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004164
4165void
4166display_exit(struct display *display)
4167{
4168 display->running = 0;
4169}