blob: b12c21c9397c26d87f853eac4413ab9f613df6bf [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030043#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030056#else /* HAVE_CAIRO_EGL */
57typedef void *EGLDisplay;
58typedef void *EGLConfig;
59typedef void *EGLContext;
60#define EGL_NO_DISPLAY ((EGLDisplay)0)
61#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062
Daniel Stone9d4f0302012-02-15 16:33:21 +000063#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030064#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040065
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020067#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040068#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
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;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200128
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500132};
133
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400134enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400135 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400137 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500138 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500140 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400141 TYPE_CUSTOM
142};
Rob Bradford7507b572012-05-15 17:55:34 +0100143
144struct window_output {
145 struct output *output;
146 struct wl_list link;
147};
148
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200149struct toysurface {
150 /*
151 * Prepare the surface for drawing. Makes sure there is a surface
152 * of the right size available for rendering, and returns it.
153 * dx,dy are the x,y of wl_surface.attach.
154 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200155 * If flags has SURFACE_HINT_RESIZE set, the user is
156 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200160 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Pekka Paalanen4e373742013-02-13 16:17:13 +0200189struct surface {
190 struct window *window;
191
192 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300193 struct wl_subsurface *subsurface;
194 int synchronized;
195 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200196 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200197 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300198 int redraw_needed;
199 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300200 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201
202 struct rectangle allocation;
203 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200205 struct wl_region *input_region;
206 struct wl_region *opaque_region;
207
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208 enum window_buffer_type buffer_type;
209 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200210
211 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300212
213 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200214};
215
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500216struct window {
217 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500218 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100219 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500220 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200221 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400222 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500223 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400225 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400226 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300227 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400228 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400229 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100230 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400231 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400232 int focus_count;
233
Pekka Paalanen99436862012-11-19 17:15:59 +0200234 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200235 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000236 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400237
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500238 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500239 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400240 window_data_handler_t data_handler;
241 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500242 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400243 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200244 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245
Pekka Paalanen4e373742013-02-13 16:17:13 +0200246 struct surface *main_surface;
247 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300248
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200249 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500250
Pekka Paalanen35e82632013-04-25 13:57:48 +0300251 /* struct surface::link, contains also main_surface */
252 struct wl_list subsurface_list;
253
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500254 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400255 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500256};
257
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500258struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500259 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200260 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300261 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500262 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400263 struct wl_list link;
264 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500265 widget_resize_handler_t resize_handler;
266 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500267 widget_enter_handler_t enter_handler;
268 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500269 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500270 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200271 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400272 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500273 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300274 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500275 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400276};
277
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400278struct input {
279 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100280 struct wl_seat *seat;
281 struct wl_pointer *pointer;
282 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400283 struct window *pointer_focus;
284 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300285 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300286 uint32_t cursor_anim_start;
287 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300288 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400289 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400290 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400291 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400292 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400293 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400294
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500295 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500296 struct widget *grab;
297 uint32_t grab_button;
298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400299 struct wl_data_device *data_device;
300 struct data_offer *drag_offer;
301 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100302
303 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100304 struct xkb_keymap *keymap;
305 struct xkb_state *state;
306 xkb_mod_mask_t control_mask;
307 xkb_mod_mask_t alt_mask;
308 xkb_mod_mask_t shift_mask;
309 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400310
311 struct task repeat_task;
312 int repeat_timer_fd;
313 uint32_t repeat_sym;
314 uint32_t repeat_key;
315 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316};
317
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500318struct output {
319 struct display *display;
320 struct wl_output *output;
321 struct rectangle allocation;
322 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600323 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200324
325 display_output_handler_t destroy_handler;
326 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500327};
328
Martin Minarik1998b152012-05-10 02:04:35 +0200329enum frame_button_action {
330 FRAME_BUTTON_NULL = 0,
331 FRAME_BUTTON_ICON = 1,
332 FRAME_BUTTON_CLOSE = 2,
333 FRAME_BUTTON_MINIMIZE = 3,
334 FRAME_BUTTON_MAXIMIZE = 4,
335};
336
337enum frame_button_pointer {
338 FRAME_BUTTON_DEFAULT = 0,
339 FRAME_BUTTON_OVER = 1,
340 FRAME_BUTTON_ACTIVE = 2,
341};
342
343enum frame_button_align {
344 FRAME_BUTTON_RIGHT = 0,
345 FRAME_BUTTON_LEFT = 1,
346};
347
348enum frame_button_decoration {
349 FRAME_BUTTON_NONE = 0,
350 FRAME_BUTTON_FANCY = 1,
351};
352
353struct frame_button {
354 struct widget *widget;
355 struct frame *frame;
356 cairo_surface_t *icon;
357 enum frame_button_action type;
358 enum frame_button_pointer state;
359 struct wl_list link; /* buttons_list */
360 enum frame_button_align align;
361 enum frame_button_decoration decoration;
362};
363
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500364struct frame {
365 struct widget *widget;
366 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200367 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500368};
369
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500370struct menu {
371 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500372 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500373 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 const char **entries;
375 uint32_t time;
376 int current;
377 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400378 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500379 menu_func_t func;
380};
381
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300382struct tooltip {
383 struct widget *parent;
384 struct window *window;
385 struct widget *widget;
386 char *entry;
387 struct task tooltip_task;
388 int tooltip_fd;
389 float x, y;
390};
391
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700392struct shm_pool {
393 struct wl_shm_pool *pool;
394 size_t size;
395 size_t used;
396 void *data;
397};
398
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400399enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300400 CURSOR_DEFAULT = 100,
401 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400402};
403
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500404enum window_location {
405 WINDOW_INTERIOR = 0,
406 WINDOW_RESIZING_TOP = 1,
407 WINDOW_RESIZING_BOTTOM = 2,
408 WINDOW_RESIZING_LEFT = 4,
409 WINDOW_RESIZING_TOP_LEFT = 5,
410 WINDOW_RESIZING_BOTTOM_LEFT = 6,
411 WINDOW_RESIZING_RIGHT = 8,
412 WINDOW_RESIZING_TOP_RIGHT = 9,
413 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
414 WINDOW_RESIZING_MASK = 15,
415 WINDOW_EXTERIOR = 16,
416 WINDOW_TITLEBAR = 17,
417 WINDOW_CLIENT_AREA = 18,
418};
419
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200420static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500422#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200424struct egl_window_surface {
425 struct toysurface base;
426 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100427 struct display *display;
428 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429 struct wl_egl_window *egl_window;
430 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100431};
432
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200433static struct egl_window_surface *
434to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100435{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200436 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100437}
438
439static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200440egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200441 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100442{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200443 struct egl_window_surface *surface = to_egl_window_surface(base);
444
445 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
446 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
447
448 return cairo_surface_reference(surface->cairo_surface);
449}
450
451static void
452egl_window_surface_swap(struct toysurface *base,
453 struct rectangle *server_allocation)
454{
455 struct egl_window_surface *surface = to_egl_window_surface(base);
456
457 cairo_gl_surface_swapbuffers(surface->cairo_surface);
458 wl_egl_window_get_attached_size(surface->egl_window,
459 &server_allocation->width,
460 &server_allocation->height);
461}
462
463static int
464egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
465{
466 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200467 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100468
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200469 device = cairo_surface_get_device(surface->cairo_surface);
470 if (!device)
471 return -1;
472
473 if (!ctx) {
474 if (device == surface->display->argb_device)
475 ctx = surface->display->argb_ctx;
476 else
477 assert(0);
478 }
479
480 cairo_device_flush(device);
481 cairo_device_acquire(device);
482 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
483 surface->egl_surface, ctx))
484 fprintf(stderr, "failed to make surface current\n");
485
486 return 0;
487}
488
489static void
490egl_window_surface_release(struct toysurface *base)
491{
492 struct egl_window_surface *surface = to_egl_window_surface(base);
493 cairo_device_t *device;
494
495 device = cairo_surface_get_device(surface->cairo_surface);
496 if (!device)
497 return;
498
499 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
500 surface->display->argb_ctx))
501 fprintf(stderr, "failed to make context current\n");
502
503 cairo_device_release(device);
504}
505
506static void
507egl_window_surface_destroy(struct toysurface *base)
508{
509 struct egl_window_surface *surface = to_egl_window_surface(base);
510 struct display *d = surface->display;
511
512 cairo_surface_destroy(surface->cairo_surface);
513 eglDestroySurface(d->dpy, surface->egl_surface);
514 wl_egl_window_destroy(surface->egl_window);
515 surface->surface = NULL;
516
517 free(surface);
518}
519
520static struct toysurface *
521egl_window_surface_create(struct display *display,
522 struct wl_surface *wl_surface,
523 uint32_t flags,
524 struct rectangle *rectangle)
525{
526 struct egl_window_surface *surface;
527
Pekka Paalanenb3627362012-11-19 17:16:00 +0200528 if (display->dpy == EGL_NO_DISPLAY)
529 return NULL;
530
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 surface = calloc(1, sizeof *surface);
532 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533 return NULL;
534
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 surface->base.prepare = egl_window_surface_prepare;
536 surface->base.swap = egl_window_surface_swap;
537 surface->base.acquire = egl_window_surface_acquire;
538 surface->base.release = egl_window_surface_release;
539 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 surface->display = display;
542 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400543
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544 surface->egl_window = wl_egl_window_create(surface->surface,
545 rectangle->width,
546 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548 surface->egl_surface = eglCreateWindowSurface(display->dpy,
549 display->argb_config,
550 surface->egl_window,
551 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200553 surface->cairo_surface =
554 cairo_gl_surface_create_for_egl(display->argb_device,
555 surface->egl_surface,
556 rectangle->width,
557 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560}
561
Pekka Paalanenb3627362012-11-19 17:16:00 +0200562#else
563
564static struct toysurface *
565egl_window_surface_create(struct display *display,
566 struct wl_surface *wl_surface,
567 uint32_t flags,
568 struct rectangle *rectangle)
569{
570 return NULL;
571}
572
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573#endif
574
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200575struct shm_surface_data {
576 struct wl_buffer *buffer;
577 struct shm_pool *pool;
578};
579
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400580struct wl_buffer *
581display_get_buffer_for_surface(struct display *display,
582 cairo_surface_t *surface)
583{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200584 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200586 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587
588 return data->buffer;
589}
590
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500591static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700592shm_pool_destroy(struct shm_pool *pool);
593
594static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595shm_surface_data_destroy(void *p)
596{
597 struct shm_surface_data *data = p;
598
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200599 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700600 if (data->pool)
601 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300602
603 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604}
605
Kristian Høgsberg16626282012-04-03 11:21:27 -0400606static struct wl_shm_pool *
607make_shm_pool(struct display *display, int size, void **data)
608{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400609 struct wl_shm_pool *pool;
610 int fd;
611
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300612 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400613 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300614 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
615 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400616 return NULL;
617 }
618
619 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400620 if (*data == MAP_FAILED) {
621 fprintf(stderr, "mmap failed: %m\n");
622 close(fd);
623 return NULL;
624 }
625
626 pool = wl_shm_create_pool(display->shm, fd, size);
627
628 close(fd);
629
630 return pool;
631}
632
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700633static struct shm_pool *
634shm_pool_create(struct display *display, size_t size)
635{
636 struct shm_pool *pool = malloc(sizeof *pool);
637
638 if (!pool)
639 return NULL;
640
641 pool->pool = make_shm_pool(display, size, &pool->data);
642 if (!pool->pool) {
643 free(pool);
644 return NULL;
645 }
646
647 pool->size = size;
648 pool->used = 0;
649
650 return pool;
651}
652
653static void *
654shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
655{
656 if (pool->used + size > pool->size)
657 return NULL;
658
659 *offset = pool->used;
660 pool->used += size;
661
662 return (char *) pool->data + *offset;
663}
664
665/* destroy the pool. this does not unmap the memory though */
666static void
667shm_pool_destroy(struct shm_pool *pool)
668{
669 munmap(pool->data, pool->size);
670 wl_shm_pool_destroy(pool->pool);
671 free(pool);
672}
673
674/* Start allocating from the beginning of the pool again */
675static void
676shm_pool_reset(struct shm_pool *pool)
677{
678 pool->used = 0;
679}
680
681static int
682data_length_for_shm_surface(struct rectangle *rect)
683{
684 int stride;
685
686 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
687 rect->width);
688 return stride * rect->height;
689}
690
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500691static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700692display_create_shm_surface_from_pool(struct display *display,
693 struct rectangle *rectangle,
694 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400695{
696 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400697 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400698 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700699 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400700 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701
702 data = malloc(sizeof *data);
703 if (data == NULL)
704 return NULL;
705
706 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
707 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708 length = stride * rectangle->height;
709 data->pool = NULL;
710 map = shm_pool_allocate(pool, length, &offset);
711
712 if (!map) {
713 free(data);
714 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400715 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400717 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718 CAIRO_FORMAT_ARGB32,
719 rectangle->width,
720 rectangle->height,
721 stride);
722
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200723 cairo_surface_set_user_data(surface, &shm_surface_data_key,
724 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400725
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400726 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500727 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400728 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500729 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400730
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200731 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
732 rectangle->width,
733 rectangle->height,
734 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400735
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736 return surface;
737}
738
739static cairo_surface_t *
740display_create_shm_surface(struct display *display,
741 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200742 struct shm_pool *alternate_pool,
743 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700744{
745 struct shm_surface_data *data;
746 struct shm_pool *pool;
747 cairo_surface_t *surface;
748
Pekka Paalanen99436862012-11-19 17:15:59 +0200749 if (alternate_pool) {
750 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700751 surface = display_create_shm_surface_from_pool(display,
752 rectangle,
753 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200754 alternate_pool);
755 if (surface) {
756 data = cairo_surface_get_user_data(surface,
757 &shm_surface_data_key);
758 goto out;
759 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700760 }
761
762 pool = shm_pool_create(display,
763 data_length_for_shm_surface(rectangle));
764 if (!pool)
765 return NULL;
766
767 surface =
768 display_create_shm_surface_from_pool(display, rectangle,
769 flags, pool);
770
771 if (!surface) {
772 shm_pool_destroy(pool);
773 return NULL;
774 }
775
776 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200777 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700778 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779
Pekka Paalanen99436862012-11-19 17:15:59 +0200780out:
781 if (data_ret)
782 *data_ret = data;
783
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784 return surface;
785}
786
nobled7b87cb02011-02-01 18:51:47 +0000787static int
788check_size(struct rectangle *rect)
789{
790 if (rect->width && rect->height)
791 return 0;
792
793 fprintf(stderr, "tried to create surface of "
794 "width: %d, height: %d\n", rect->width, rect->height);
795 return -1;
796}
797
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798cairo_surface_t *
799display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400801 struct rectangle *rectangle,
802 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400803{
nobled7b87cb02011-02-01 18:51:47 +0000804 if (check_size(rectangle) < 0)
805 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200806
807 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200808 return display_create_shm_surface(display, rectangle, flags,
809 NULL, NULL);
810}
811
Pekka Paalanena4eda732012-11-19 17:16:02 +0200812struct shm_surface_leaf {
813 cairo_surface_t *cairo_surface;
814 /* 'data' is automatically destroyed, when 'cairo_surface' is */
815 struct shm_surface_data *data;
816
817 struct shm_pool *resize_pool;
818 int busy;
819};
820
821static void
822shm_surface_leaf_release(struct shm_surface_leaf *leaf)
823{
824 if (leaf->cairo_surface)
825 cairo_surface_destroy(leaf->cairo_surface);
826 /* leaf->data already destroyed via cairo private */
827
828 if (leaf->resize_pool)
829 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200830
831 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200832}
833
Pekka Paalanenaef02542013-04-25 13:57:47 +0300834#define MAX_LEAVES 3
835
Pekka Paalanen99436862012-11-19 17:15:59 +0200836struct shm_surface {
837 struct toysurface base;
838 struct display *display;
839 struct wl_surface *surface;
840 uint32_t flags;
841 int dx, dy;
842
Pekka Paalanenaef02542013-04-25 13:57:47 +0300843 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200844 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200845};
846
847static struct shm_surface *
848to_shm_surface(struct toysurface *base)
849{
850 return container_of(base, struct shm_surface, base);
851}
852
Pekka Paalanena4eda732012-11-19 17:16:02 +0200853static void
854shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
855{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200856 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300857 struct shm_surface_leaf *leaf;
858 int i;
859 int free_found;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200860
Pekka Paalanenaef02542013-04-25 13:57:47 +0300861 for (i = 0; i < MAX_LEAVES; i++) {
862 leaf = &surface->leaf[i];
863 if (leaf->data && leaf->data->buffer == buffer) {
864 leaf->busy = 0;
865 break;
866 }
867 }
868 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200869
Pekka Paalanenaef02542013-04-25 13:57:47 +0300870 /* Leave one free leaf with storage, release others */
871 free_found = 0;
872 for (i = 0; i < MAX_LEAVES; i++) {
873 leaf = &surface->leaf[i];
874
875 if (!leaf->cairo_surface || leaf->busy)
876 continue;
877
878 if (!free_found)
879 free_found = 1;
880 else
881 shm_surface_leaf_release(leaf);
882 }
Pekka Paalanena4eda732012-11-19 17:16:02 +0200883}
884
885static const struct wl_buffer_listener shm_surface_buffer_listener = {
886 shm_surface_buffer_release
887};
888
Pekka Paalanen99436862012-11-19 17:15:59 +0200889static cairo_surface_t *
890shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200891 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200892{
Pekka Paalanenec076692012-11-30 13:37:27 +0200893 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200894 struct shm_surface *surface = to_shm_surface(base);
895 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300896 struct shm_surface_leaf *leaf = NULL;
897 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200898
899 surface->dx = dx;
900 surface->dy = dy;
901
Pekka Paalanenaef02542013-04-25 13:57:47 +0300902 /* pick a free buffer, preferrably one that already has storage */
903 for (i = 0; i < MAX_LEAVES; i++) {
904 if (surface->leaf[i].busy)
905 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200906
Pekka Paalanenaef02542013-04-25 13:57:47 +0300907 if (!leaf || surface->leaf[i].cairo_surface)
908 leaf = &surface->leaf[i];
909 }
910 if (!leaf) {
911 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912 __func__);
913 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 }
915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 if (!resize_hint && leaf->resize_pool) {
917 cairo_surface_destroy(leaf->cairo_surface);
918 leaf->cairo_surface = NULL;
919 shm_pool_destroy(leaf->resize_pool);
920 leaf->resize_pool = NULL;
921 }
922
923 if (leaf->cairo_surface &&
924 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
925 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200926 goto out;
927
Pekka Paalanena4eda732012-11-19 17:16:02 +0200928 if (leaf->cairo_surface)
929 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200930
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200932 /* Create a big pool to allocate from, while continuously
933 * resizing. Mmapping a new pool in the server
934 * is relatively expensive, so reusing a pool performs
935 * better, but may temporarily reserve unneeded memory.
936 */
937 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 leaf->resize_pool = shm_pool_create(surface->display,
939 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200940 }
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200943 display_create_shm_surface(surface->display, &rect,
944 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945 leaf->resize_pool,
946 &leaf->data);
947 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200948 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200949
950out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200951 surface->current = leaf;
952
953 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200954}
955
956static void
957shm_surface_swap(struct toysurface *base,
958 struct rectangle *server_allocation)
959{
960 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200961 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200962
963 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200965 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200966 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200967
Pekka Paalanena4eda732012-11-19 17:16:02 +0200968 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200969 surface->dx, surface->dy);
970 wl_surface_damage(surface->surface, 0, 0,
971 server_allocation->width, server_allocation->height);
972 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200973
974 leaf->busy = 1;
975 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200976}
977
978static int
979shm_surface_acquire(struct toysurface *base, EGLContext ctx)
980{
981 return -1;
982}
983
984static void
985shm_surface_release(struct toysurface *base)
986{
987}
988
989static void
990shm_surface_destroy(struct toysurface *base)
991{
992 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200994
Pekka Paalanenaef02542013-04-25 13:57:47 +0300995 for (i = 0; i < MAX_LEAVES; i++)
996 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200997
998 free(surface);
999}
1000
1001static struct toysurface *
1002shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1003 uint32_t flags, struct rectangle *rectangle)
1004{
1005 struct shm_surface *surface;
1006
1007 surface = calloc(1, sizeof *surface);
1008 if (!surface)
1009 return NULL;
1010
1011 surface->base.prepare = shm_surface_prepare;
1012 surface->base.swap = shm_surface_swap;
1013 surface->base.acquire = shm_surface_acquire;
1014 surface->base.release = shm_surface_release;
1015 surface->base.destroy = shm_surface_destroy;
1016
1017 surface->display = display;
1018 surface->surface = wl_surface;
1019 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001020
1021 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001022}
1023
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001024/*
1025 * The following correspondences between file names and cursors was copied
1026 * from: https://bugs.kde.org/attachment.cgi?id=67313
1027 */
1028
1029static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "sw-resize"
1032};
1033
1034static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "se-resize"
1037};
1038
1039static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001040 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 "s-resize"
1042};
1043
1044static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001046 "closedhand",
1047 "208530c400c041818281048008011002"
1048};
1049
1050static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001051 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 "default",
1053 "top_left_arrow",
1054 "left-arrow"
1055};
1056
1057static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001058 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001059 "w-resize"
1060};
1061
1062static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001063 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001064 "e-resize"
1065};
1066
1067static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001068 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001069 "nw-resize"
1070};
1071
1072static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001073 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001074 "ne-resize"
1075};
1076
1077static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001078 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001079 "n-resize"
1080};
1081
1082static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001083 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001084 "ibeam",
1085 "text"
1086};
1087
1088static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001089 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001090 "pointer",
1091 "pointing_hand",
1092 "e29285e634086352946a0e7090d73106"
1093};
1094
1095static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001096 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001097 "wait",
1098 "0426c94ea35c87780ff01dc239897213"
1099};
1100
1101struct cursor_alternatives {
1102 const char **names;
1103 size_t count;
1104};
1105
1106static const struct cursor_alternatives cursors[] = {
1107 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1108 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1109 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1110 {grabbings, ARRAY_LENGTH(grabbings)},
1111 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1112 {left_sides, ARRAY_LENGTH(left_sides)},
1113 {right_sides, ARRAY_LENGTH(right_sides)},
1114 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1115 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1116 {top_sides, ARRAY_LENGTH(top_sides)},
1117 {xterms, ARRAY_LENGTH(xterms)},
1118 {hand1s, ARRAY_LENGTH(hand1s)},
1119 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001120};
1121
1122static void
1123create_cursors(struct display *display)
1124{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001125 char *config_file;
1126 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001127 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001128 unsigned int i, j;
1129 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001130 struct config_key shell_keys[] = {
1131 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001132 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001133 };
1134 struct config_section cs[] = {
1135 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1136 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001137
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001138 config_file = config_file_path("weston.ini");
1139 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1140 free(config_file);
1141
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001142 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001143 display->cursors =
1144 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1145
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001146 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001147 cursor = NULL;
1148 for (j = 0; !cursor && j < cursors[i].count; ++j)
1149 cursor = wl_cursor_theme_get_cursor(
1150 display->cursor_theme, cursors[i].names[j]);
1151
1152 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001153 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001154 cursors[i].names[0]);
1155
1156 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001157 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001158}
1159
1160static void
1161destroy_cursors(struct display *display)
1162{
1163 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001164 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001165}
1166
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001167struct wl_cursor_image *
1168display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001170 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001172 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001173}
1174
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001175static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001176surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001177{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001178 if (!surface->cairo_surface)
1179 return;
1180
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001181 if (surface->opaque_region) {
1182 wl_surface_set_opaque_region(surface->surface,
1183 surface->opaque_region);
1184 wl_region_destroy(surface->opaque_region);
1185 surface->opaque_region = NULL;
1186 }
1187
1188 if (surface->input_region) {
1189 wl_surface_set_input_region(surface->surface,
1190 surface->input_region);
1191 wl_region_destroy(surface->input_region);
1192 surface->input_region = NULL;
1193 }
1194
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001195 surface->toysurface->swap(surface->toysurface,
1196 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001197
Pekka Paalanen89dee002013-02-13 16:17:20 +02001198 cairo_surface_destroy(surface->cairo_surface);
1199 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001200}
1201
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001202int
1203window_has_focus(struct window *window)
1204{
1205 return window->focus_count > 0;
1206}
1207
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001208static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001209window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001210{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001211 struct surface *surface;
1212
Pekka Paalanen89dee002013-02-13 16:17:20 +02001213 if (window->type == TYPE_NONE) {
1214 window->type = TYPE_TOPLEVEL;
1215 if (window->shell_surface)
1216 wl_shell_surface_set_toplevel(window->shell_surface);
1217 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001218
Pekka Paalanen35e82632013-04-25 13:57:48 +03001219 wl_list_for_each(surface, &window->subsurface_list, link) {
1220 if (surface == window->main_surface)
1221 continue;
1222
1223 surface_flush(surface);
1224 }
1225
Pekka Paalanen89dee002013-02-13 16:17:20 +02001226 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001227}
1228
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001229struct display *
1230window_get_display(struct window *window)
1231{
1232 return window->display;
1233}
1234
Pekka Paalanen89dee002013-02-13 16:17:20 +02001235static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001236surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001237{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001238 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001239 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001240
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001241 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001242 case WL_OUTPUT_TRANSFORM_90:
1243 case WL_OUTPUT_TRANSFORM_270:
1244 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1245 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001246 allocation.width = surface->allocation.height;
1247 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001248 break;
1249 default:
1250 break;
1251 }
1252
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001253 if (!surface->toysurface && display->dpy &&
1254 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001255 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001256 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001257 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001258 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001259 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001260 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001261
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001262 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001263 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001264 surface->surface,
1265 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001266
Pekka Paalanen89dee002013-02-13 16:17:20 +02001267 surface->cairo_surface = surface->toysurface->prepare(
1268 surface->toysurface, dx, dy,
1269 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001270}
1271
1272static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001273window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001274{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001275 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001276 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001277 int dx = 0;
1278 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001279
Pekka Paalanenec076692012-11-30 13:37:27 +02001280 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001281 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001282
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001283 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1284 dx = surface->server_allocation.width -
1285 surface->allocation.width;
1286
1287 if (window->resize_edges & WINDOW_RESIZING_TOP)
1288 dy = surface->server_allocation.height -
1289 surface->allocation.height;
1290
1291 window->resize_edges = 0;
1292
Pekka Paalanen89dee002013-02-13 16:17:20 +02001293 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001294}
1295
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001296int
1297window_get_buffer_transform(struct window *window)
1298{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001299 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001300}
1301
1302void
1303window_set_buffer_transform(struct window *window,
1304 enum wl_output_transform transform)
1305{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001306 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001307 wl_surface_set_buffer_transform(window->main_surface->surface,
1308 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001309}
1310
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001311static void frame_destroy(struct frame *frame);
1312
Pekka Paalanen4e373742013-02-13 16:17:13 +02001313static void
1314surface_destroy(struct surface *surface)
1315{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001316 if (surface->frame_cb)
1317 wl_callback_destroy(surface->frame_cb);
1318
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001319 if (surface->input_region)
1320 wl_region_destroy(surface->input_region);
1321
1322 if (surface->opaque_region)
1323 wl_region_destroy(surface->opaque_region);
1324
Pekka Paalanen35e82632013-04-25 13:57:48 +03001325 if (surface->subsurface)
1326 wl_subsurface_destroy(surface->subsurface);
1327
Pekka Paalanen4e373742013-02-13 16:17:13 +02001328 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001329
1330 if (surface->toysurface)
1331 surface->toysurface->destroy(surface->toysurface);
1332
Pekka Paalanen35e82632013-04-25 13:57:48 +03001333 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001334 free(surface);
1335}
1336
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001337void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001338window_destroy(struct window *window)
1339{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001340 struct display *display = window->display;
1341 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001342 struct window_output *window_output;
1343 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001344
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001345 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001346
1347 wl_list_for_each(input, &display->input_list, link) {
1348 if (input->pointer_focus == window)
1349 input->pointer_focus = NULL;
1350 if (input->keyboard_focus == window)
1351 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001352 if (input->focus_widget &&
1353 input->focus_widget->window == window)
1354 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001355 }
1356
Rob Bradford7507b572012-05-15 17:55:34 +01001357 wl_list_for_each_safe(window_output, window_output_tmp,
1358 &window->window_output_list, link) {
1359 free (window_output);
1360 }
1361
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001362 if (window->frame)
1363 frame_destroy(window->frame);
1364
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001365 if (window->shell_surface)
1366 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001367
1368 surface_destroy(window->main_surface);
1369
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001370 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001371
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001372 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001373 free(window);
1374}
1375
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001377widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001378{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001379 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001380
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001381 wl_list_for_each(child, &widget->child_list, link) {
1382 target = widget_find_widget(child, x, y);
1383 if (target)
1384 return target;
1385 }
1386
1387 if (widget->allocation.x <= x &&
1388 x < widget->allocation.x + widget->allocation.width &&
1389 widget->allocation.y <= y &&
1390 y < widget->allocation.y + widget->allocation.height) {
1391 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001392 }
1393
1394 return NULL;
1395}
1396
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001397static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001398window_find_widget(struct window *window, int32_t x, int32_t y)
1399{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001400 struct surface *surface;
1401 struct widget *widget;
1402
1403 wl_list_for_each(surface, &window->subsurface_list, link) {
1404 widget = widget_find_widget(surface->widget, x, y);
1405 if (widget)
1406 return widget;
1407 }
1408
1409 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001410}
1411
1412static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001413widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001414{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001415 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001416
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001417 widget = malloc(sizeof *widget);
1418 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001419 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001420 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001421 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001422 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001423 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001424 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001425 widget->tooltip = NULL;
1426 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001427 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001428
1429 return widget;
1430}
1431
1432struct widget *
1433window_add_widget(struct window *window, void *data)
1434{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001435 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001436
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001437 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001438 wl_list_init(&widget->link);
1439 window->main_surface->widget = widget;
1440
1441 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001442}
1443
1444struct widget *
1445widget_add_widget(struct widget *parent, void *data)
1446{
1447 struct widget *widget;
1448
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001449 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001450 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001451
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001452 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001453}
1454
1455void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001456widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001458 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001459 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001460 struct input *input;
1461
Pekka Paalanen35e82632013-04-25 13:57:48 +03001462 /* Destroy the sub-surface along with the root widget */
1463 if (surface->widget == widget && surface->subsurface)
1464 surface_destroy(widget->surface);
1465
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001466 if (widget->tooltip) {
1467 free(widget->tooltip);
1468 widget->tooltip = NULL;
1469 }
1470
Pekka Paalanene156fb62012-01-19 13:51:38 +02001471 wl_list_for_each(input, &display->input_list, link) {
1472 if (input->focus_widget == widget)
1473 input->focus_widget = NULL;
1474 }
1475
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001476 wl_list_remove(&widget->link);
1477 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001478}
1479
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001481widget_set_default_cursor(struct widget *widget, int cursor)
1482{
1483 widget->default_cursor = cursor;
1484}
1485
1486void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001487widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001488{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001489 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001490}
1491
1492void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001493widget_set_size(struct widget *widget, int32_t width, int32_t height)
1494{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001495 widget->allocation.width = width;
1496 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001497}
1498
1499void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001500widget_set_allocation(struct widget *widget,
1501 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001502{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001503 widget->allocation.x = x;
1504 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001505 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001506}
1507
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001508void
1509widget_set_transparent(struct widget *widget, int transparent)
1510{
1511 widget->opaque = !transparent;
1512}
1513
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001514void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001515widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001516{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001517 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001518}
1519
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001520static cairo_surface_t *
1521widget_get_cairo_surface(struct widget *widget)
1522{
1523 struct surface *surface = widget->surface;
1524 struct window *window = widget->window;
1525
1526 if (!surface->cairo_surface) {
1527 if (surface == window->main_surface)
1528 window_create_main_surface(window);
1529 else
1530 surface_create_surface(surface, 0, 0, 0);
1531 }
1532
1533 return surface->cairo_surface;
1534}
1535
1536cairo_t *
1537widget_cairo_create(struct widget *widget)
1538{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001539 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001540 cairo_surface_t *cairo_surface;
1541 cairo_t *cr;
1542
1543 cairo_surface = widget_get_cairo_surface(widget);
1544 cr = cairo_create(cairo_surface);
1545
Pekka Paalanen35e82632013-04-25 13:57:48 +03001546 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1547
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001548 return cr;
1549}
1550
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001551struct wl_surface *
1552widget_get_wl_surface(struct widget *widget)
1553{
1554 return widget->surface->surface;
1555}
1556
1557uint32_t
1558widget_get_last_time(struct widget *widget)
1559{
1560 return widget->surface->last_time;
1561}
1562
1563void
1564widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1565{
1566 struct wl_compositor *comp = widget->window->display->compositor;
1567 struct surface *surface = widget->surface;
1568
1569 if (!surface->input_region)
1570 surface->input_region = wl_compositor_create_region(comp);
1571
1572 if (rect) {
1573 wl_region_add(surface->input_region,
1574 rect->x, rect->y, rect->width, rect->height);
1575 }
1576}
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001579widget_set_resize_handler(struct widget *widget,
1580 widget_resize_handler_t handler)
1581{
1582 widget->resize_handler = handler;
1583}
1584
1585void
1586widget_set_redraw_handler(struct widget *widget,
1587 widget_redraw_handler_t handler)
1588{
1589 widget->redraw_handler = handler;
1590}
1591
1592void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001593widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001594{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001595 widget->enter_handler = handler;
1596}
1597
1598void
1599widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1600{
1601 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001602}
1603
1604void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001605widget_set_motion_handler(struct widget *widget,
1606 widget_motion_handler_t handler)
1607{
1608 widget->motion_handler = handler;
1609}
1610
1611void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001612widget_set_button_handler(struct widget *widget,
1613 widget_button_handler_t handler)
1614{
1615 widget->button_handler = handler;
1616}
1617
1618void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001619widget_set_axis_handler(struct widget *widget,
1620 widget_axis_handler_t handler)
1621{
1622 widget->axis_handler = handler;
1623}
1624
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001625static void
1626window_schedule_redraw_task(struct window *window);
1627
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001628void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001629widget_schedule_redraw(struct widget *widget)
1630{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001631 widget->surface->redraw_needed = 1;
1632 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001633}
1634
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001635cairo_surface_t *
1636window_get_surface(struct window *window)
1637{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001638 cairo_surface_t *cairo_surface;
1639
1640 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1641
1642 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001643}
1644
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001645struct wl_surface *
1646window_get_wl_surface(struct window *window)
1647{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001648 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001649}
1650
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001651struct wl_shell_surface *
1652window_get_wl_shell_surface(struct window *window)
1653{
1654 return window->shell_surface;
1655}
1656
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001657static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001658tooltip_redraw_handler(struct widget *widget, void *data)
1659{
1660 cairo_t *cr;
1661 const int32_t r = 3;
1662 struct tooltip *tooltip = data;
1663 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001664
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001665 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001666 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1667 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1668 cairo_paint(cr);
1669
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001670 width = widget->allocation.width;
1671 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001672 rounded_rect(cr, 0, 0, width, height, r);
1673
1674 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1675 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1676 cairo_fill(cr);
1677
1678 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1679 cairo_move_to(cr, 10, 16);
1680 cairo_show_text(cr, tooltip->entry);
1681 cairo_destroy(cr);
1682}
1683
1684static cairo_text_extents_t
1685get_text_extents(struct tooltip *tooltip)
1686{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001687 cairo_t *cr;
1688 cairo_text_extents_t extents;
1689
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001690 /* Use the dummy_surface because tooltip's surface was not
1691 * created yet, and parent does not have a valid surface
1692 * outside repaint, either.
1693 */
1694 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001695 cairo_text_extents(cr, tooltip->entry, &extents);
1696 cairo_destroy(cr);
1697
1698 return extents;
1699}
1700
1701static int
1702window_create_tooltip(struct tooltip *tooltip)
1703{
1704 struct widget *parent = tooltip->parent;
1705 struct display *display = parent->window->display;
1706 struct window *window;
1707 const int offset_y = 27;
1708 const int margin = 3;
1709 cairo_text_extents_t extents;
1710
1711 if (tooltip->widget)
1712 return 0;
1713
1714 window = window_create_transient(display, parent->window, tooltip->x,
1715 tooltip->y + offset_y,
1716 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1717 if (!window)
1718 return -1;
1719
1720 tooltip->window = window;
1721 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1722
1723 extents = get_text_extents(tooltip);
1724 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1725 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1726
1727 return 0;
1728}
1729
1730void
1731widget_destroy_tooltip(struct widget *parent)
1732{
1733 struct tooltip *tooltip = parent->tooltip;
1734
1735 parent->tooltip_count = 0;
1736 if (!tooltip)
1737 return;
1738
1739 if (tooltip->widget) {
1740 widget_destroy(tooltip->widget);
1741 window_destroy(tooltip->window);
1742 tooltip->widget = NULL;
1743 tooltip->window = NULL;
1744 }
1745
1746 close(tooltip->tooltip_fd);
1747 free(tooltip->entry);
1748 free(tooltip);
1749 parent->tooltip = NULL;
1750}
1751
1752static void
1753tooltip_func(struct task *task, uint32_t events)
1754{
1755 struct tooltip *tooltip =
1756 container_of(task, struct tooltip, tooltip_task);
1757 uint64_t exp;
1758
Martin Olsson8df662a2012-07-08 03:03:47 +02001759 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1760 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001761 window_create_tooltip(tooltip);
1762}
1763
1764#define TOOLTIP_TIMEOUT 500
1765static int
1766tooltip_timer_reset(struct tooltip *tooltip)
1767{
1768 struct itimerspec its;
1769
1770 its.it_interval.tv_sec = 0;
1771 its.it_interval.tv_nsec = 0;
1772 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1773 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1774 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1775 fprintf(stderr, "could not set timerfd\n: %m");
1776 return -1;
1777 }
1778
1779 return 0;
1780}
1781
1782int
1783widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1784{
1785 struct tooltip *tooltip = parent->tooltip;
1786
1787 parent->tooltip_count++;
1788 if (tooltip) {
1789 tooltip->x = x;
1790 tooltip->y = y;
1791 tooltip_timer_reset(tooltip);
1792 return 0;
1793 }
1794
1795 /* the handler might be triggered too fast via input device motion, so
1796 * we need this check here to make sure tooltip is fully initialized */
1797 if (parent->tooltip_count > 1)
1798 return 0;
1799
1800 tooltip = malloc(sizeof *tooltip);
1801 if (!tooltip)
1802 return -1;
1803
1804 parent->tooltip = tooltip;
1805 tooltip->parent = parent;
1806 tooltip->widget = NULL;
1807 tooltip->window = NULL;
1808 tooltip->x = x;
1809 tooltip->y = y;
1810 tooltip->entry = strdup(entry);
1811 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1812 if (tooltip->tooltip_fd < 0) {
1813 fprintf(stderr, "could not create timerfd\n: %m");
1814 return -1;
1815 }
1816
1817 tooltip->tooltip_task.run = tooltip_func;
1818 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1819 EPOLLIN, &tooltip->tooltip_task);
1820 tooltip_timer_reset(tooltip);
1821
1822 return 0;
1823}
1824
1825static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001826workspace_manager_state(void *data,
1827 struct workspace_manager *workspace_manager,
1828 uint32_t current,
1829 uint32_t count)
1830{
1831 struct display *display = data;
1832
1833 display->workspace = current;
1834 display->workspace_count = count;
1835}
1836
1837static const struct workspace_manager_listener workspace_manager_listener = {
1838 workspace_manager_state
1839};
1840
1841static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001842frame_resize_handler(struct widget *widget,
1843 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001844{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 struct frame *frame = data;
1846 struct widget *child = frame->child;
1847 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001848 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001849 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001850 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001851 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001852 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001853 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001854 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001855
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001856 switch (widget->window->type) {
1857 case TYPE_FULLSCREEN:
1858 decoration_width = 0;
1859 decoration_height = 0;
1860
1861 allocation.x = 0;
1862 allocation.y = 0;
1863 allocation.width = width;
1864 allocation.height = height;
1865 opaque_margin = 0;
1866
1867 wl_list_for_each(button, &frame->buttons_list, link)
1868 button->widget->opaque = 1;
1869 break;
1870 case TYPE_MAXIMIZED:
1871 decoration_width = t->width * 2;
1872 decoration_height = t->width + t->titlebar_height;
1873
1874 allocation.x = t->width;
1875 allocation.y = t->titlebar_height;
1876 allocation.width = width - decoration_width;
1877 allocation.height = height - decoration_height;
1878
1879 opaque_margin = 0;
1880
1881 wl_list_for_each(button, &frame->buttons_list, link)
1882 button->widget->opaque = 0;
1883 break;
1884 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001885 decoration_width = (t->width + t->margin) * 2;
1886 decoration_height = t->width +
1887 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001888
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001889 allocation.x = t->width + t->margin;
1890 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001891 allocation.width = width - decoration_width;
1892 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001893
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001894 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001895
1896 wl_list_for_each(button, &frame->buttons_list, link)
1897 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001898 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001899 }
1900
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901 widget_set_allocation(child, allocation.x, allocation.y,
1902 allocation.width, allocation.height);
1903
1904 if (child->resize_handler)
1905 child->resize_handler(child,
1906 allocation.width,
1907 allocation.height,
1908 child->user_data);
1909
Scott Moreauf7e498c2012-05-14 11:39:29 -06001910 width = child->allocation.width + decoration_width;
1911 height = child->allocation.height + decoration_height;
1912
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001913 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1914
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001915 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001916 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001917 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001918 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001919 shadow_margin, shadow_margin,
1920 width - 2 * shadow_margin,
1921 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001922 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001923 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001924 }
1925
Scott Moreauf7e498c2012-05-14 11:39:29 -06001926 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001927
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001928 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001929 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001930 opaque_margin, opaque_margin,
1931 widget->allocation.width - 2 * opaque_margin,
1932 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001933
1934 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1936 x_l = t->width + shadow_margin;
1937 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001938 wl_list_for_each(button, &frame->buttons_list, link) {
1939 const int button_padding = 4;
1940 w = cairo_image_surface_get_width(button->icon);
1941 h = cairo_image_surface_get_height(button->icon);
1942
1943 if (button->decoration == FRAME_BUTTON_FANCY)
1944 w += 10;
1945
1946 if (button->align == FRAME_BUTTON_LEFT) {
1947 widget_set_allocation(button->widget,
1948 x_l, y , w + 1, h + 1);
1949 x_l += w;
1950 x_l += button_padding;
1951 } else {
1952 x_r -= w;
1953 widget_set_allocation(button->widget,
1954 x_r, y , w + 1, h + 1);
1955 x_r -= button_padding;
1956 }
1957 }
1958}
1959
1960static int
1961frame_button_enter_handler(struct widget *widget,
1962 struct input *input, float x, float y, void *data)
1963{
1964 struct frame_button *frame_button = data;
1965
1966 widget_schedule_redraw(frame_button->widget);
1967 frame_button->state = FRAME_BUTTON_OVER;
1968
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001969 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001970}
1971
1972static void
1973frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1974{
1975 struct frame_button *frame_button = data;
1976
1977 widget_schedule_redraw(frame_button->widget);
1978 frame_button->state = FRAME_BUTTON_DEFAULT;
1979}
1980
1981static void
1982frame_button_button_handler(struct widget *widget,
1983 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001984 uint32_t button,
1985 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001986{
1987 struct frame_button *frame_button = data;
1988 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001989 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001990
1991 if (button != BTN_LEFT)
1992 return;
1993
1994 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001995 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001996 frame_button->state = FRAME_BUTTON_ACTIVE;
1997 widget_schedule_redraw(frame_button->widget);
1998
1999 if (frame_button->type == FRAME_BUTTON_ICON)
2000 window_show_frame_menu(window, input, time);
2001 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002002 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002003 frame_button->state = FRAME_BUTTON_DEFAULT;
2004 widget_schedule_redraw(frame_button->widget);
2005 break;
2006 }
2007
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002008 if (!was_pressed)
2009 return;
2010
Martin Minarik1998b152012-05-10 02:04:35 +02002011 switch (frame_button->type) {
2012 case FRAME_BUTTON_CLOSE:
2013 if (window->close_handler)
2014 window->close_handler(window->parent,
2015 window->user_data);
2016 else
2017 display_exit(window->display);
2018 break;
2019 case FRAME_BUTTON_MINIMIZE:
2020 fprintf(stderr,"Minimize stub\n");
2021 break;
2022 case FRAME_BUTTON_MAXIMIZE:
2023 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2024 break;
2025 default:
2026 /* Unknown operation */
2027 break;
2028 }
2029}
2030
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002031static int
2032frame_button_motion_handler(struct widget *widget,
2033 struct input *input, uint32_t time,
2034 float x, float y, void *data)
2035{
2036 struct frame_button *frame_button = data;
2037 enum frame_button_pointer previous_button_state = frame_button->state;
2038
2039 /* only track state for a pressed button */
2040 if (input->grab != widget)
2041 return CURSOR_LEFT_PTR;
2042
2043 if (x > widget->allocation.x &&
2044 x < (widget->allocation.x + widget->allocation.width) &&
2045 y > widget->allocation.y &&
2046 y < (widget->allocation.y + widget->allocation.height)) {
2047 frame_button->state = FRAME_BUTTON_ACTIVE;
2048 } else {
2049 frame_button->state = FRAME_BUTTON_DEFAULT;
2050 }
2051
2052 if (frame_button->state != previous_button_state)
2053 widget_schedule_redraw(frame_button->widget);
2054
2055 return CURSOR_LEFT_PTR;
2056}
2057
Martin Minarik1998b152012-05-10 02:04:35 +02002058static void
2059frame_button_redraw_handler(struct widget *widget, void *data)
2060{
2061 struct frame_button *frame_button = data;
2062 cairo_t *cr;
2063 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002064
2065 x = widget->allocation.x;
2066 y = widget->allocation.y;
2067 width = widget->allocation.width;
2068 height = widget->allocation.height;
2069
2070 if (!width)
2071 return;
2072 if (!height)
2073 return;
2074 if (widget->opaque)
2075 return;
2076
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002077 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002078
2079 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2080 cairo_set_line_width(cr, 1);
2081
2082 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2083 cairo_rectangle (cr, x, y, 25, 16);
2084
2085 cairo_stroke_preserve(cr);
2086
2087 switch (frame_button->state) {
2088 case FRAME_BUTTON_DEFAULT:
2089 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2090 break;
2091 case FRAME_BUTTON_OVER:
2092 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2093 break;
2094 case FRAME_BUTTON_ACTIVE:
2095 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2096 break;
2097 }
2098
2099 cairo_fill (cr);
2100
2101 x += 4;
2102 }
2103
2104 cairo_set_source_surface(cr, frame_button->icon, x, y);
2105 cairo_paint(cr);
2106
2107 cairo_destroy(cr);
2108}
2109
2110static struct widget *
2111frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2112 enum frame_button_align align, enum frame_button_decoration style)
2113{
2114 struct frame_button *frame_button;
2115 const char *icon = data;
2116
2117 frame_button = malloc (sizeof *frame_button);
2118 memset(frame_button, 0, sizeof *frame_button);
2119
2120 frame_button->icon = cairo_image_surface_create_from_png(icon);
2121 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2122 frame_button->frame = frame;
2123 frame_button->type = type;
2124 frame_button->align = align;
2125 frame_button->decoration = style;
2126
2127 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2128
2129 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2130 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2131 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2132 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002133 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002134 return frame_button->widget;
2135}
2136
2137static void
2138frame_button_destroy(struct frame_button *frame_button)
2139{
2140 widget_destroy(frame_button->widget);
2141 wl_list_remove(&frame_button->link);
2142 cairo_surface_destroy(frame_button->icon);
2143 free(frame_button);
2144
2145 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146}
2147
2148static void
2149frame_redraw_handler(struct widget *widget, void *data)
2150{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002153 struct theme *t = window->display->theme;
2154 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002156 if (window->type == TYPE_FULLSCREEN)
2157 return;
2158
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002159 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002161 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002162 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002163 if (window->type == TYPE_MAXIMIZED)
2164 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002165 theme_render_frame(t, cr, widget->allocation.width,
2166 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167
2168 cairo_destroy(cr);
2169}
2170
2171static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002173{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002174 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002175 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002176 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002177
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002178 if (window->type != TYPE_TOPLEVEL)
2179 return CURSOR_LEFT_PTR;
2180
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002181 location = theme_get_location(t, input->sx, input->sy,
2182 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002183 frame->widget->allocation.height,
2184 window->type == TYPE_MAXIMIZED ?
2185 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002186
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002187 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002188 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002189 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002190 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002191 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002192 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002193 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002194 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002195 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002196 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002197 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002198 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002199 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002200 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002201 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002202 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002203 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002204 case THEME_LOCATION_EXTERIOR:
2205 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002206 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002207 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002208 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002209}
2210
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211static void
2212frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002213{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002214 struct display *display;
2215
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 switch (index) {
2217 case 0: /* close */
2218 if (window->close_handler)
2219 window->close_handler(window->parent,
2220 window->user_data);
2221 else
2222 display_exit(window->display);
2223 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002224 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002225 display = window->display;
2226 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002227 workspace_manager_move_surface(
2228 display->workspace_manager,
2229 window->main_surface->surface,
2230 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002231 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002232 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002233 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002234 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002235 workspace_manager_move_surface(
2236 display->workspace_manager,
2237 window->main_surface->surface,
2238 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002239 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002240 case 3: /* fullscreen */
2241 /* we don't have a way to get out of fullscreen for now */
2242 if (window->fullscreen_handler)
2243 window->fullscreen_handler(window, window->user_data);
2244 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245 }
2246}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002248void
2249window_show_frame_menu(struct window *window,
2250 struct input *input, uint32_t time)
2251{
2252 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002253 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002254
2255 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002256 "Close",
2257 "Move to workspace above", "Move to workspace below",
2258 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002259 };
2260
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002261 if (window->fullscreen_handler)
2262 count = ARRAY_LENGTH(entries);
2263 else
2264 count = ARRAY_LENGTH(entries) - 1;
2265
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002266 input_get_position(input, &x, &y);
2267 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002268 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002269}
2270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002271static int
2272frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002273 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274{
2275 return frame_get_pointer_image_for_location(data, input);
2276}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002277
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278static int
2279frame_motion_handler(struct widget *widget,
2280 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002281 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282{
2283 return frame_get_pointer_image_for_location(data, input);
2284}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002285
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287frame_button_handler(struct widget *widget,
2288 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002289 uint32_t button, enum wl_pointer_button_state state,
2290 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002291
2292{
2293 struct frame *frame = data;
2294 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002295 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002297
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002298 if (window->type != TYPE_TOPLEVEL)
2299 return;
2300
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002301 location = theme_get_location(display->theme, input->sx, input->sy,
2302 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002303 frame->widget->allocation.height,
2304 window->type == TYPE_MAXIMIZED ?
2305 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306
Daniel Stone4dbadb12012-05-30 16:31:51 +01002307 if (window->display->shell && button == BTN_LEFT &&
2308 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002309 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002310 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311 if (!window->shell_surface)
2312 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002313 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002315 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002316 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002318 case THEME_LOCATION_RESIZING_TOP:
2319 case THEME_LOCATION_RESIZING_BOTTOM:
2320 case THEME_LOCATION_RESIZING_LEFT:
2321 case THEME_LOCATION_RESIZING_RIGHT:
2322 case THEME_LOCATION_RESIZING_TOP_LEFT:
2323 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2324 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2325 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326 if (!window->shell_surface)
2327 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002328 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002329
Pekka Paalanen99436862012-11-19 17:15:59 +02002330 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002332 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002333 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334 break;
2335 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002336 } else if (button == BTN_RIGHT &&
2337 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002338 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339 }
2340}
2341
2342struct widget *
2343frame_create(struct window *window, void *data)
2344{
2345 struct frame *frame;
2346
2347 frame = malloc(sizeof *frame);
2348 memset(frame, 0, sizeof *frame);
2349
2350 frame->widget = window_add_widget(window, frame);
2351 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002353 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2354 widget_set_resize_handler(frame->widget, frame_resize_handler);
2355 widget_set_enter_handler(frame->widget, frame_enter_handler);
2356 widget_set_motion_handler(frame->widget, frame_motion_handler);
2357 widget_set_button_handler(frame->widget, frame_button_handler);
2358
Martin Minarik1998b152012-05-10 02:04:35 +02002359 /* Create empty list for frame buttons */
2360 wl_list_init(&frame->buttons_list);
2361
2362 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2363 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2364
2365 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2366 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2367
2368 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2369 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2370
2371 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2372 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2373
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002374 window->frame = frame;
2375
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002376 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002377}
2378
Kristian Høgsberga1627922012-06-20 17:30:03 -04002379void
2380frame_set_child_size(struct widget *widget, int child_width, int child_height)
2381{
2382 struct display *display = widget->window->display;
2383 struct theme *t = display->theme;
2384 int decoration_width, decoration_height;
2385 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002386 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002387
2388 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002389 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002390 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002391 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002392
2393 width = child_width + decoration_width;
2394 height = child_height + decoration_height;
2395 } else {
2396 width = child_width;
2397 height = child_height;
2398 }
2399
2400 window_schedule_resize(widget->window, width, height);
2401}
2402
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002403static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002404frame_destroy(struct frame *frame)
2405{
Martin Minarik1998b152012-05-10 02:04:35 +02002406 struct frame_button *button, *tmp;
2407
2408 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2409 frame_button_destroy(button);
2410
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002411 /* frame->child must be destroyed by the application */
2412 widget_destroy(frame->widget);
2413 free(frame);
2414}
2415
2416static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002417input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002418 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002419{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002420 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002421 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002422
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002423 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002424 return;
2425
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002426 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002427 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002428 widget = old;
2429 if (input->grab)
2430 widget = input->grab;
2431 if (widget->leave_handler)
2432 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002433 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002434 }
2435
2436 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002437 widget = focus;
2438 if (input->grab)
2439 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002440 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002441 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002442 cursor = widget->enter_handler(focus, input, x, y,
2443 widget->user_data);
2444 else
2445 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002446
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002447 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002448 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002449}
2450
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002451void
2452input_grab(struct input *input, struct widget *widget, uint32_t button)
2453{
2454 input->grab = widget;
2455 input->grab_button = button;
2456}
2457
2458void
2459input_ungrab(struct input *input)
2460{
2461 struct widget *widget;
2462
2463 input->grab = NULL;
2464 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002465 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002466 input->sx, input->sy);
2467 input_set_focus_widget(input, widget, input->sx, input->sy);
2468 }
2469}
2470
2471static void
2472input_remove_pointer_focus(struct input *input)
2473{
2474 struct window *window = input->pointer_focus;
2475
2476 if (!window)
2477 return;
2478
2479 input_set_focus_widget(input, NULL, 0, 0);
2480
2481 input->pointer_focus = NULL;
2482 input->current_cursor = CURSOR_UNSET;
2483}
2484
2485static void
2486pointer_handle_enter(void *data, struct wl_pointer *pointer,
2487 uint32_t serial, struct wl_surface *surface,
2488 wl_fixed_t sx_w, wl_fixed_t sy_w)
2489{
2490 struct input *input = data;
2491 struct window *window;
2492 struct widget *widget;
2493 float sx = wl_fixed_to_double(sx_w);
2494 float sy = wl_fixed_to_double(sy_w);
2495
2496 if (!surface) {
2497 /* enter event for a window we've just destroyed */
2498 return;
2499 }
2500
2501 input->display->serial = serial;
2502 input->pointer_enter_serial = serial;
2503 input->pointer_focus = wl_surface_get_user_data(surface);
2504 window = input->pointer_focus;
2505
Pekka Paalanen99436862012-11-19 17:15:59 +02002506 if (window->resizing) {
2507 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002508 /* Schedule a redraw to free the pool */
2509 window_schedule_redraw(window);
2510 }
2511
2512 input->sx = sx;
2513 input->sy = sy;
2514
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002515 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002516 input_set_focus_widget(input, widget, sx, sy);
2517}
2518
2519static void
2520pointer_handle_leave(void *data, struct wl_pointer *pointer,
2521 uint32_t serial, struct wl_surface *surface)
2522{
2523 struct input *input = data;
2524
2525 input->display->serial = serial;
2526 input_remove_pointer_focus(input);
2527}
2528
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002529static void
Daniel Stone37816df2012-05-16 18:45:18 +01002530pointer_handle_motion(void *data, struct wl_pointer *pointer,
2531 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002532{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002533 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002534 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002535 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002536 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002537 float sx = wl_fixed_to_double(sx_w);
2538 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002539
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002540 input->sx = sx;
2541 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002542
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002543 if (!window)
2544 return;
2545
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002546 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002547 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002548 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002549 }
2550
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002551 if (input->grab)
2552 widget = input->grab;
2553 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002554 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002555 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002556 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002557 input, time, sx, sy,
2558 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002559 else
2560 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002561
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002562 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002563}
2564
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002565static void
Daniel Stone37816df2012-05-16 18:45:18 +01002566pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002567 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002568{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002569 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002570 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002571 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002572
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002573 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002574 if (input->focus_widget && input->grab == NULL &&
2575 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002577
Neil Roberts6b28aad2012-01-23 19:11:18 +00002578 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002579 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002580 (*widget->button_handler)(widget,
2581 input, time,
2582 button, state,
2583 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584
Daniel Stone4dbadb12012-05-30 16:31:51 +01002585 if (input->grab && input->grab_button == button &&
2586 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002587 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002588}
2589
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002590static void
Daniel Stone37816df2012-05-16 18:45:18 +01002591pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002592 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002593{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002594 struct input *input = data;
2595 struct widget *widget;
2596
2597 widget = input->focus_widget;
2598 if (input->grab)
2599 widget = input->grab;
2600 if (widget && widget->axis_handler)
2601 (*widget->axis_handler)(widget,
2602 input, time,
2603 axis, value,
2604 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002605}
2606
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002607static const struct wl_pointer_listener pointer_listener = {
2608 pointer_handle_enter,
2609 pointer_handle_leave,
2610 pointer_handle_motion,
2611 pointer_handle_button,
2612 pointer_handle_axis,
2613};
2614
2615static void
2616input_remove_keyboard_focus(struct input *input)
2617{
2618 struct window *window = input->keyboard_focus;
2619 struct itimerspec its;
2620
2621 its.it_interval.tv_sec = 0;
2622 its.it_interval.tv_nsec = 0;
2623 its.it_value.tv_sec = 0;
2624 its.it_value.tv_nsec = 0;
2625 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2626
2627 if (!window)
2628 return;
2629
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002630 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002631 if (window->keyboard_focus_handler)
2632 (*window->keyboard_focus_handler)(window, NULL,
2633 window->user_data);
2634
2635 input->keyboard_focus = NULL;
2636}
2637
2638static void
2639keyboard_repeat_func(struct task *task, uint32_t events)
2640{
2641 struct input *input =
2642 container_of(task, struct input, repeat_task);
2643 struct window *window = input->keyboard_focus;
2644 uint64_t exp;
2645
2646 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2647 /* If we change the timer between the fd becoming
2648 * readable and getting here, there'll be nothing to
2649 * read and we get EAGAIN. */
2650 return;
2651
2652 if (window && window->key_handler) {
2653 (*window->key_handler)(window, input, input->repeat_time,
2654 input->repeat_key, input->repeat_sym,
2655 WL_KEYBOARD_KEY_STATE_PRESSED,
2656 window->user_data);
2657 }
2658}
2659
2660static void
2661keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2662 uint32_t format, int fd, uint32_t size)
2663{
2664 struct input *input = data;
2665 char *map_str;
2666
2667 if (!data) {
2668 close(fd);
2669 return;
2670 }
2671
2672 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2673 close(fd);
2674 return;
2675 }
2676
2677 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2678 if (map_str == MAP_FAILED) {
2679 close(fd);
2680 return;
2681 }
2682
2683 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2684 map_str,
2685 XKB_KEYMAP_FORMAT_TEXT_V1,
2686 0);
2687 munmap(map_str, size);
2688 close(fd);
2689
2690 if (!input->xkb.keymap) {
2691 fprintf(stderr, "failed to compile keymap\n");
2692 return;
2693 }
2694
2695 input->xkb.state = xkb_state_new(input->xkb.keymap);
2696 if (!input->xkb.state) {
2697 fprintf(stderr, "failed to create XKB state\n");
2698 xkb_map_unref(input->xkb.keymap);
2699 input->xkb.keymap = NULL;
2700 return;
2701 }
2702
2703 input->xkb.control_mask =
2704 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2705 input->xkb.alt_mask =
2706 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2707 input->xkb.shift_mask =
2708 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2709}
2710
2711static void
2712keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2713 uint32_t serial, struct wl_surface *surface,
2714 struct wl_array *keys)
2715{
2716 struct input *input = data;
2717 struct window *window;
2718
2719 input->display->serial = serial;
2720 input->keyboard_focus = wl_surface_get_user_data(surface);
2721
2722 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002723 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002724 if (window->keyboard_focus_handler)
2725 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002726 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002727}
2728
2729static void
2730keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2731 uint32_t serial, struct wl_surface *surface)
2732{
2733 struct input *input = data;
2734
2735 input->display->serial = serial;
2736 input_remove_keyboard_focus(input);
2737}
2738
Scott Moreau210d0792012-03-22 10:47:01 -06002739static void
Daniel Stone37816df2012-05-16 18:45:18 +01002740keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002741 uint32_t serial, uint32_t time, uint32_t key,
2742 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002743{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002744 struct input *input = data;
2745 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002746 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002747 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002748 const xkb_keysym_t *syms;
2749 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002750 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002751
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002752 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002753 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002754 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002755 return;
2756
Daniel Stone97f68542012-05-30 16:32:01 +01002757 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002758
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002759 sym = XKB_KEY_NoSymbol;
2760 if (num_syms == 1)
2761 sym = syms[0];
2762
2763 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002764 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002765 window_set_maximized(window,
2766 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002767 } else if (sym == XKB_KEY_F11 &&
2768 window->fullscreen_handler &&
2769 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2770 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002771 } else if (sym == XKB_KEY_F4 &&
2772 input->modifiers == MOD_ALT_MASK &&
2773 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2774 if (window->close_handler)
2775 window->close_handler(window->parent,
2776 window->user_data);
2777 else
2778 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002779 } else if (window->key_handler) {
2780 (*window->key_handler)(window, input, time, key,
2781 sym, state, window->user_data);
2782 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002783
2784 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2785 key == input->repeat_key) {
2786 its.it_interval.tv_sec = 0;
2787 its.it_interval.tv_nsec = 0;
2788 its.it_value.tv_sec = 0;
2789 its.it_value.tv_nsec = 0;
2790 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2791 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2792 input->repeat_sym = sym;
2793 input->repeat_key = key;
2794 input->repeat_time = time;
2795 its.it_interval.tv_sec = 0;
2796 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2797 its.it_value.tv_sec = 0;
2798 its.it_value.tv_nsec = 400 * 1000 * 1000;
2799 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2800 }
2801}
2802
2803static void
Daniel Stone351eb612012-05-31 15:27:47 -04002804keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2805 uint32_t serial, uint32_t mods_depressed,
2806 uint32_t mods_latched, uint32_t mods_locked,
2807 uint32_t group)
2808{
2809 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002810 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002811
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002812 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2813 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002814 mask = xkb_state_serialize_mods(input->xkb.state,
2815 XKB_STATE_DEPRESSED |
2816 XKB_STATE_LATCHED);
2817 input->modifiers = 0;
2818 if (mask & input->xkb.control_mask)
2819 input->modifiers |= MOD_CONTROL_MASK;
2820 if (mask & input->xkb.alt_mask)
2821 input->modifiers |= MOD_ALT_MASK;
2822 if (mask & input->xkb.shift_mask)
2823 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002824}
2825
Daniel Stone37816df2012-05-16 18:45:18 +01002826static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002827 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002828 keyboard_handle_enter,
2829 keyboard_handle_leave,
2830 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002831 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002832};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002833
2834static void
Daniel Stone37816df2012-05-16 18:45:18 +01002835seat_handle_capabilities(void *data, struct wl_seat *seat,
2836 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002837{
Daniel Stone37816df2012-05-16 18:45:18 +01002838 struct input *input = data;
2839
2840 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2841 input->pointer = wl_seat_get_pointer(seat);
2842 wl_pointer_set_user_data(input->pointer, input);
2843 wl_pointer_add_listener(input->pointer, &pointer_listener,
2844 input);
2845 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2846 wl_pointer_destroy(input->pointer);
2847 input->pointer = NULL;
2848 }
2849
2850 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2851 input->keyboard = wl_seat_get_keyboard(seat);
2852 wl_keyboard_set_user_data(input->keyboard, input);
2853 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2854 input);
2855 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2856 wl_keyboard_destroy(input->keyboard);
2857 input->keyboard = NULL;
2858 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002859}
2860
Daniel Stone37816df2012-05-16 18:45:18 +01002861static const struct wl_seat_listener seat_listener = {
2862 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002863};
2864
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002865void
2866input_get_position(struct input *input, int32_t *x, int32_t *y)
2867{
2868 *x = input->sx;
2869 *y = input->sy;
2870}
2871
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002872struct display *
2873input_get_display(struct input *input)
2874{
2875 return input->display;
2876}
2877
Daniel Stone37816df2012-05-16 18:45:18 +01002878struct wl_seat *
2879input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002880{
Daniel Stone37816df2012-05-16 18:45:18 +01002881 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002882}
2883
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002884uint32_t
2885input_get_modifiers(struct input *input)
2886{
2887 return input->modifiers;
2888}
2889
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002890struct widget *
2891input_get_focus_widget(struct input *input)
2892{
2893 return input->focus_widget;
2894}
2895
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002896struct data_offer {
2897 struct wl_data_offer *offer;
2898 struct input *input;
2899 struct wl_array types;
2900 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002901
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002902 struct task io_task;
2903 int fd;
2904 data_func_t func;
2905 int32_t x, y;
2906 void *user_data;
2907};
2908
2909static void
2910data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2911{
2912 struct data_offer *offer = data;
2913 char **p;
2914
2915 p = wl_array_add(&offer->types, sizeof *p);
2916 *p = strdup(type);
2917}
2918
2919static const struct wl_data_offer_listener data_offer_listener = {
2920 data_offer_offer,
2921};
2922
2923static void
2924data_offer_destroy(struct data_offer *offer)
2925{
2926 char **p;
2927
2928 offer->refcount--;
2929 if (offer->refcount == 0) {
2930 wl_data_offer_destroy(offer->offer);
2931 for (p = offer->types.data; *p; p++)
2932 free(*p);
2933 wl_array_release(&offer->types);
2934 free(offer);
2935 }
2936}
2937
2938static void
2939data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002940 struct wl_data_device *data_device,
2941 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002942{
2943 struct data_offer *offer;
2944
2945 offer = malloc(sizeof *offer);
2946
2947 wl_array_init(&offer->types);
2948 offer->refcount = 1;
2949 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002950 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002951 wl_data_offer_add_listener(offer->offer,
2952 &data_offer_listener, offer);
2953}
2954
2955static void
2956data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002957 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002958 wl_fixed_t x_w, wl_fixed_t y_w,
2959 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002960{
2961 struct input *input = data;
2962 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002963 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002964 float x = wl_fixed_to_double(x_w);
2965 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002966 char **p;
2967
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002968 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002969 window = wl_surface_get_user_data(surface);
2970 input->pointer_focus = window;
2971
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002972 if (offer) {
2973 input->drag_offer = wl_data_offer_get_user_data(offer);
2974
2975 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2976 *p = NULL;
2977
2978 types_data = input->drag_offer->types.data;
2979 } else {
2980 input->drag_offer = NULL;
2981 types_data = NULL;
2982 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002983
2984 window = input->pointer_focus;
2985 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002986 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002987 window->user_data);
2988}
2989
2990static void
2991data_device_leave(void *data, struct wl_data_device *data_device)
2992{
2993 struct input *input = data;
2994
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002995 if (input->drag_offer) {
2996 data_offer_destroy(input->drag_offer);
2997 input->drag_offer = NULL;
2998 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002999}
3000
3001static void
3002data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003003 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003004{
3005 struct input *input = data;
3006 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003007 float x = wl_fixed_to_double(x_w);
3008 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003009 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003010
3011 input->sx = x;
3012 input->sy = y;
3013
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003014 if (input->drag_offer)
3015 types_data = input->drag_offer->types.data;
3016 else
3017 types_data = NULL;
3018
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003019 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003020 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003021 window->user_data);
3022}
3023
3024static void
3025data_device_drop(void *data, struct wl_data_device *data_device)
3026{
3027 struct input *input = data;
3028 struct window *window = input->pointer_focus;
3029
3030 if (window->drop_handler)
3031 window->drop_handler(window, input,
3032 input->sx, input->sy, window->user_data);
3033}
3034
3035static void
3036data_device_selection(void *data,
3037 struct wl_data_device *wl_data_device,
3038 struct wl_data_offer *offer)
3039{
3040 struct input *input = data;
3041 char **p;
3042
3043 if (input->selection_offer)
3044 data_offer_destroy(input->selection_offer);
3045
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003046 if (offer) {
3047 input->selection_offer = wl_data_offer_get_user_data(offer);
3048 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3049 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003050 } else {
3051 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003052 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003053}
3054
3055static const struct wl_data_device_listener data_device_listener = {
3056 data_device_data_offer,
3057 data_device_enter,
3058 data_device_leave,
3059 data_device_motion,
3060 data_device_drop,
3061 data_device_selection
3062};
3063
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003064static void
3065input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003066{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003067 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003068 struct wl_cursor *cursor;
3069 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003070
Daniel Stone80972742012-11-07 17:51:39 +11003071 if (!input->pointer)
3072 return;
3073
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003074 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003075 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003076 return;
3077
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003078 if (index >= (int) cursor->image_count) {
3079 fprintf(stderr, "cursor index out of range\n");
3080 return;
3081 }
3082
3083 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003084 buffer = wl_cursor_image_get_buffer(image);
3085 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003086 return;
3087
Kristian Høgsbergae277372012-08-01 09:41:08 -04003088 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003089 input->pointer_surface,
3090 image->hotspot_x, image->hotspot_y);
3091 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3092 wl_surface_damage(input->pointer_surface, 0, 0,
3093 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003094 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003095}
3096
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003097static const struct wl_callback_listener pointer_surface_listener;
3098
3099static void
3100pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3101 uint32_t time)
3102{
3103 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003104 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003105 int i;
3106
3107 if (callback) {
3108 assert(callback == input->cursor_frame_cb);
3109 wl_callback_destroy(callback);
3110 input->cursor_frame_cb = NULL;
3111 }
3112
Daniel Stone80972742012-11-07 17:51:39 +11003113 if (!input->pointer)
3114 return;
3115
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003116 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003117 wl_pointer_set_cursor(input->pointer,
3118 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003119 NULL, 0, 0);
3120 return;
3121 }
3122
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003123 if (input->current_cursor == CURSOR_UNSET)
3124 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003125 cursor = input->display->cursors[input->current_cursor];
3126 if (!cursor)
3127 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003128
3129 /* FIXME We don't have the current time on the first call so we set
3130 * the animation start to the time of the first frame callback. */
3131 if (time == 0)
3132 input->cursor_anim_start = 0;
3133 else if (input->cursor_anim_start == 0)
3134 input->cursor_anim_start = time;
3135
3136 if (time == 0 || input->cursor_anim_start == 0)
3137 i = 0;
3138 else
3139 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3140
Pekka Paalanenbc106382012-10-10 12:49:31 +03003141 if (cursor->image_count > 1) {
3142 input->cursor_frame_cb =
3143 wl_surface_frame(input->pointer_surface);
3144 wl_callback_add_listener(input->cursor_frame_cb,
3145 &pointer_surface_listener, input);
3146 }
3147
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003148 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003149}
3150
3151static const struct wl_callback_listener pointer_surface_listener = {
3152 pointer_surface_frame_callback
3153};
3154
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003155void
3156input_set_pointer_image(struct input *input, int pointer)
3157{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003158 int force = 0;
3159
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003160 if (!input->pointer)
3161 return;
3162
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003163 if (input->pointer_enter_serial > input->cursor_serial)
3164 force = 1;
3165
3166 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003167 return;
3168
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003169 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003170 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003171 if (!input->cursor_frame_cb)
3172 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003173 else if (force) {
3174 /* The current frame callback may be stuck if, for instance,
3175 * the set cursor request was processed by the server after
3176 * this client lost the focus. In this case the cursor surface
3177 * might not be mapped and the frame callback wouldn't ever
3178 * complete. Send a set_cursor and attach to try to map the
3179 * cursor surface again so that the callback will finish */
3180 input_set_pointer_image_index(input, 0);
3181 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003182}
3183
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003184struct wl_data_device *
3185input_get_data_device(struct input *input)
3186{
3187 return input->data_device;
3188}
3189
3190void
3191input_set_selection(struct input *input,
3192 struct wl_data_source *source, uint32_t time)
3193{
3194 wl_data_device_set_selection(input->data_device, source, time);
3195}
3196
3197void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003198input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003199{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003200 wl_data_offer_accept(input->drag_offer->offer,
3201 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003202}
3203
3204static void
3205offer_io_func(struct task *task, uint32_t events)
3206{
3207 struct data_offer *offer =
3208 container_of(task, struct data_offer, io_task);
3209 unsigned int len;
3210 char buffer[4096];
3211
3212 len = read(offer->fd, buffer, sizeof buffer);
3213 offer->func(buffer, len,
3214 offer->x, offer->y, offer->user_data);
3215
3216 if (len == 0) {
3217 close(offer->fd);
3218 data_offer_destroy(offer);
3219 }
3220}
3221
3222static void
3223data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3224 data_func_t func, void *user_data)
3225{
3226 int p[2];
3227
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003228 if (pipe2(p, O_CLOEXEC) == -1)
3229 return;
3230
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003231 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3232 close(p[1]);
3233
3234 offer->io_task.run = offer_io_func;
3235 offer->fd = p[0];
3236 offer->func = func;
3237 offer->refcount++;
3238 offer->user_data = user_data;
3239
3240 display_watch_fd(offer->input->display,
3241 offer->fd, EPOLLIN, &offer->io_task);
3242}
3243
3244void
3245input_receive_drag_data(struct input *input, const char *mime_type,
3246 data_func_t func, void *data)
3247{
3248 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3249 input->drag_offer->x = input->sx;
3250 input->drag_offer->y = input->sy;
3251}
3252
3253int
3254input_receive_selection_data(struct input *input, const char *mime_type,
3255 data_func_t func, void *data)
3256{
3257 char **p;
3258
3259 if (input->selection_offer == NULL)
3260 return -1;
3261
3262 for (p = input->selection_offer->types.data; *p; p++)
3263 if (strcmp(mime_type, *p) == 0)
3264 break;
3265
3266 if (*p == NULL)
3267 return -1;
3268
3269 data_offer_receive_data(input->selection_offer,
3270 mime_type, func, data);
3271 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003272}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003273
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003274int
3275input_receive_selection_data_to_fd(struct input *input,
3276 const char *mime_type, int fd)
3277{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003278 if (input->selection_offer)
3279 wl_data_offer_receive(input->selection_offer->offer,
3280 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003281
3282 return 0;
3283}
3284
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003285void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003286window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003287{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003288 if (!window->shell_surface)
3289 return;
3290
Daniel Stone37816df2012-05-16 18:45:18 +01003291 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003292}
3293
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003294static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003295surface_set_synchronized(struct surface *surface)
3296{
3297 if (!surface->subsurface)
3298 return;
3299
3300 if (surface->synchronized)
3301 return;
3302
3303 wl_subsurface_set_sync(surface->subsurface);
3304 surface->synchronized = 1;
3305}
3306
3307static void
3308surface_set_synchronized_default(struct surface *surface)
3309{
3310 if (!surface->subsurface)
3311 return;
3312
3313 if (surface->synchronized == surface->synchronized_default)
3314 return;
3315
3316 if (surface->synchronized_default)
3317 wl_subsurface_set_sync(surface->subsurface);
3318 else
3319 wl_subsurface_set_desync(surface->subsurface);
3320
3321 surface->synchronized = surface->synchronized_default;
3322}
3323
3324static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003325surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003326{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003327 struct widget *widget = surface->widget;
3328 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003329
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003330 if (surface->input_region) {
3331 wl_region_destroy(surface->input_region);
3332 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003333 }
3334
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003335 if (surface->opaque_region)
3336 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003337
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003338 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003339
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003340 if (widget->resize_handler)
3341 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003342 widget->allocation.width,
3343 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003344 widget->user_data);
3345
Pekka Paalanen35e82632013-04-25 13:57:48 +03003346 if (surface->subsurface &&
3347 (surface->allocation.x != widget->allocation.x ||
3348 surface->allocation.y != widget->allocation.y)) {
3349 wl_subsurface_set_position(surface->subsurface,
3350 widget->allocation.x,
3351 widget->allocation.y);
3352 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003353 if (surface->allocation.width != widget->allocation.width ||
3354 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003355 window_schedule_redraw(widget->window);
3356 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003357 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003358
3359 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003360 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003361 widget->allocation.width,
3362 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003363}
3364
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003365static void
3366idle_resize(struct window *window)
3367{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003368 struct surface *surface;
3369
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003370 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003371 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003372
3373 widget_set_allocation(window->main_surface->widget,
3374 window->pending_allocation.x,
3375 window->pending_allocation.y,
3376 window->pending_allocation.width,
3377 window->pending_allocation.height);
3378
3379 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003380
3381 /* The main surface is in the list, too. Main surface's
3382 * resize_handler is responsible for calling widget_set_allocation()
3383 * on all sub-surface root widgets, so they will be resized
3384 * properly.
3385 */
3386 wl_list_for_each(surface, &window->subsurface_list, link) {
3387 if (surface == window->main_surface)
3388 continue;
3389
3390 surface_set_synchronized(surface);
3391 surface_resize(surface);
3392 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003393}
3394
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003395void
3396window_schedule_resize(struct window *window, int width, int height)
3397{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003398 window->pending_allocation.x = 0;
3399 window->pending_allocation.y = 0;
3400 window->pending_allocation.width = width;
3401 window->pending_allocation.height = height;
3402
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003403 if (window->min_allocation.width == 0)
3404 window->min_allocation = window->pending_allocation;
3405 if (window->pending_allocation.width < window->min_allocation.width)
3406 window->pending_allocation.width = window->min_allocation.width;
3407 if (window->pending_allocation.height < window->min_allocation.height)
3408 window->pending_allocation.height = window->min_allocation.height;
3409
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003410 window->resize_needed = 1;
3411 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003412}
3413
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003414void
3415widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3416{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003417 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003418}
3419
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003420static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003421handle_ping(void *data, struct wl_shell_surface *shell_surface,
3422 uint32_t serial)
3423{
3424 wl_shell_surface_pong(shell_surface, serial);
3425}
3426
3427static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003428handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003429 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003430{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003431 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003432
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003433 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003434 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003435}
3436
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003437static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003438menu_destroy(struct menu *menu)
3439{
3440 widget_destroy(menu->widget);
3441 window_destroy(menu->window);
3442 free(menu);
3443}
3444
3445static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003446handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3447{
3448 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003449 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003450
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003451 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003452 * device. Or just use wl_callback. And this really needs to
3453 * be a window vfunc that the menu can set. And we need the
3454 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003455
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003456 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003457 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003458}
3459
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003460static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003461 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003462 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003463 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003464};
3465
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003466void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003467window_get_allocation(struct window *window,
3468 struct rectangle *allocation)
3469{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003470 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003471}
3472
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003473static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003474widget_redraw(struct widget *widget)
3475{
3476 struct widget *child;
3477
3478 if (widget->redraw_handler)
3479 widget->redraw_handler(widget, widget->user_data);
3480 wl_list_for_each(child, &widget->child_list, link)
3481 widget_redraw(child);
3482}
3483
3484static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003485frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3486{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003487 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003488
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003489 assert(callback == surface->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003490 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003491 surface->frame_cb = NULL;
3492
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003493 surface->last_time = time;
3494
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003495 if (surface->redraw_needed || surface->window->redraw_needed)
3496 window_schedule_redraw_task(surface->window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003497}
3498
3499static const struct wl_callback_listener listener = {
3500 frame_callback
3501};
3502
3503static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003504surface_redraw(struct surface *surface)
3505{
3506 if (!surface->window->redraw_needed && !surface->redraw_needed)
3507 return;
3508
3509 /* Whole-window redraw forces a redraw even if the previous has
3510 * not yet hit the screen.
3511 */
3512 if (surface->frame_cb) {
3513 if (!surface->window->redraw_needed)
3514 return;
3515
3516 wl_callback_destroy(surface->frame_cb);
3517 }
3518
3519 surface->frame_cb = wl_surface_frame(surface->surface);
3520 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3521
3522 surface->redraw_needed = 0;
3523 widget_redraw(surface->widget);
3524}
3525
3526static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003527idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003528{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003529 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003530 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003531
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003532 if (window->resize_needed)
3533 idle_resize(window);
3534
Pekka Paalanen35e82632013-04-25 13:57:48 +03003535 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003536 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003537
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003538 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003539 wl_list_init(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003540 window->redraw_task_scheduled = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003541
Pekka Paalanenbc106382012-10-10 12:49:31 +03003542 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003543
3544 wl_list_for_each(surface, &window->subsurface_list, link)
3545 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003546}
3547
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003548static void
3549window_schedule_redraw_task(struct window *window)
3550{
3551 if (window->configure_requests)
3552 return;
3553 if (!window->redraw_task_scheduled) {
3554 window->redraw_task.run = idle_redraw;
3555 display_defer(window->display, &window->redraw_task);
3556 window->redraw_task_scheduled = 1;
3557 }
3558}
3559
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003560void
3561window_schedule_redraw(struct window *window)
3562{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003563 struct surface *surface;
3564
3565 wl_list_for_each(surface, &window->subsurface_list, link)
3566 surface->redraw_needed = 1;
3567
3568 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003569}
3570
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003571int
3572window_is_fullscreen(struct window *window)
3573{
3574 return window->type == TYPE_FULLSCREEN;
3575}
3576
MoD063a8822013-03-02 23:43:57 +00003577static void
3578configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3579{
3580 struct window *window = data;
3581
3582 wl_callback_destroy(callback);
3583 window->configure_requests--;
3584
3585 if (!window->configure_requests)
3586 window_schedule_redraw(window);
3587}
3588
3589static struct wl_callback_listener configure_request_listener = {
3590 configure_request_completed,
3591};
3592
3593static void
3594window_defer_redraw_until_configure(struct window* window)
3595{
3596 struct wl_callback *callback;
3597
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003598 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003599 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003600 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003601 }
3602
3603 callback = wl_display_sync(window->display->display);
3604 wl_callback_add_listener(callback, &configure_request_listener, window);
3605 window->configure_requests++;
3606}
3607
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003608void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003609window_set_fullscreen(struct window *window, int fullscreen)
3610{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003611 if (!window->display->shell)
3612 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003613
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003614 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003615 return;
3616
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003617 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003618 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003619 if (window->type == TYPE_TOPLEVEL) {
3620 window->saved_allocation = window->main_surface->allocation;
3621 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003622 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003623 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003624 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003625 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003626 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003627 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003628 if (window->saved_type == TYPE_MAXIMIZED) {
3629 window_set_maximized(window, 1);
3630 } else {
3631 window->type = TYPE_TOPLEVEL;
3632 wl_shell_surface_set_toplevel(window->shell_surface);
3633 window_schedule_resize(window,
3634 window->saved_allocation.width,
3635 window->saved_allocation.height);
3636 }
3637
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003638 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003639}
3640
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003641void
3642window_set_fullscreen_method(struct window *window,
3643 enum wl_shell_surface_fullscreen_method method)
3644{
3645 window->fullscreen_method = method;
3646}
3647
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003648int
3649window_is_maximized(struct window *window)
3650{
3651 return window->type == TYPE_MAXIMIZED;
3652}
3653
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003654void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003655window_set_maximized(struct window *window, int maximized)
3656{
3657 if (!window->display->shell)
3658 return;
3659
3660 if ((window->type == TYPE_MAXIMIZED) == maximized)
3661 return;
3662
3663 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003664 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003665 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3666 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003667 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003668 } else if (window->type == TYPE_FULLSCREEN) {
3669 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3670 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003671 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003672 } else {
3673 wl_shell_surface_set_toplevel(window->shell_surface);
3674 window->type = TYPE_TOPLEVEL;
3675 window_schedule_resize(window,
3676 window->saved_allocation.width,
3677 window->saved_allocation.height);
3678 }
3679}
3680
3681void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003682window_set_user_data(struct window *window, void *data)
3683{
3684 window->user_data = data;
3685}
3686
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003687void *
3688window_get_user_data(struct window *window)
3689{
3690 return window->user_data;
3691}
3692
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003693void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003694window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003695 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003696{
3697 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003698}
3699
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003700void
3701window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003702 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003703{
3704 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003705}
3706
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003707void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003708window_set_data_handler(struct window *window, window_data_handler_t handler)
3709{
3710 window->data_handler = handler;
3711}
3712
3713void
3714window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3715{
3716 window->drop_handler = handler;
3717}
3718
3719void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003720window_set_close_handler(struct window *window,
3721 window_close_handler_t handler)
3722{
3723 window->close_handler = handler;
3724}
3725
3726void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003727window_set_fullscreen_handler(struct window *window,
3728 window_fullscreen_handler_t handler)
3729{
3730 window->fullscreen_handler = handler;
3731}
3732
3733void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003734window_set_output_handler(struct window *window,
3735 window_output_handler_t handler)
3736{
3737 window->output_handler = handler;
3738}
3739
3740void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003741window_set_title(struct window *window, const char *title)
3742{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003743 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003744 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003745 if (window->shell_surface)
3746 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003747}
3748
3749const char *
3750window_get_title(struct window *window)
3751{
3752 return window->title;
3753}
3754
3755void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003756window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3757{
3758 struct text_cursor_position *text_cursor_position =
3759 window->display->text_cursor_position;
3760
Scott Moreau9295ce02012-06-01 12:46:10 -06003761 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003762 return;
3763
3764 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003765 window->main_surface->surface,
3766 wl_fixed_from_int(x),
3767 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003768}
3769
3770void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003771window_damage(struct window *window, int32_t x, int32_t y,
3772 int32_t width, int32_t height)
3773{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003774 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003775}
3776
Casey Dahlin9074db52012-04-19 22:50:09 -04003777static void
3778surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003779 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003780{
Rob Bradford7507b572012-05-15 17:55:34 +01003781 struct window *window = data;
3782 struct output *output;
3783 struct output *output_found = NULL;
3784 struct window_output *window_output;
3785
3786 wl_list_for_each(output, &window->display->output_list, link) {
3787 if (output->output == wl_output) {
3788 output_found = output;
3789 break;
3790 }
3791 }
3792
3793 if (!output_found)
3794 return;
3795
3796 window_output = malloc (sizeof *window_output);
3797 window_output->output = output_found;
3798
3799 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003800
3801 if (window->output_handler)
3802 window->output_handler(window, output_found, 1,
3803 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003804}
3805
3806static void
3807surface_leave(void *data,
3808 struct wl_surface *wl_surface, struct wl_output *output)
3809{
Rob Bradford7507b572012-05-15 17:55:34 +01003810 struct window *window = data;
3811 struct window_output *window_output;
3812 struct window_output *window_output_found = NULL;
3813
3814 wl_list_for_each(window_output, &window->window_output_list, link) {
3815 if (window_output->output->output == output) {
3816 window_output_found = window_output;
3817 break;
3818 }
3819 }
3820
3821 if (window_output_found) {
3822 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003823
3824 if (window->output_handler)
3825 window->output_handler(window, window_output->output,
3826 0, window->user_data);
3827
Rob Bradford7507b572012-05-15 17:55:34 +01003828 free(window_output_found);
3829 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003830}
3831
3832static const struct wl_surface_listener surface_listener = {
3833 surface_enter,
3834 surface_leave
3835};
3836
Pekka Paalanen4e373742013-02-13 16:17:13 +02003837static struct surface *
3838surface_create(struct window *window)
3839{
3840 struct display *display = window->display;
3841 struct surface *surface;
3842
3843 surface = calloc(1, sizeof *surface);
3844 if (!surface)
3845 return NULL;
3846
3847 surface->window = window;
3848 surface->surface = wl_compositor_create_surface(display->compositor);
3849 wl_surface_add_listener(surface->surface, &surface_listener, window);
3850
Pekka Paalanen35e82632013-04-25 13:57:48 +03003851 wl_list_insert(&window->subsurface_list, &surface->link);
3852
Pekka Paalanen4e373742013-02-13 16:17:13 +02003853 return surface;
3854}
3855
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003856static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003857window_create_internal(struct display *display,
3858 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003859{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003860 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003861 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003862
3863 window = malloc(sizeof *window);
3864 if (window == NULL)
3865 return NULL;
3866
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003867 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003868 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003869 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003870 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003871
3872 surface = surface_create(window);
3873 window->main_surface = surface;
3874
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003875 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003876 window->shell_surface =
3877 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003878 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003879 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003880
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003881 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003882 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00003883 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003884
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003885 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003886#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003887 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003888#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003889 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003890#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003891 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003892 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003893
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003894 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003895 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003896 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003897
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003898 if (window->shell_surface) {
3899 wl_shell_surface_set_user_data(window->shell_surface, window);
3900 wl_shell_surface_add_listener(window->shell_surface,
3901 &shell_surface_listener, window);
3902 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003903
Rob Bradford7507b572012-05-15 17:55:34 +01003904 wl_list_init (&window->window_output_list);
3905
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003906 return window;
3907}
3908
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003909struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003910window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003911{
3912 struct window *window;
3913
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003914 window = window_create_internal(display, NULL, TYPE_NONE);
3915 if (!window)
3916 return NULL;
3917
3918 return window;
3919}
3920
3921struct window *
3922window_create_custom(struct display *display)
3923{
3924 struct window *window;
3925
3926 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003927 if (!window)
3928 return NULL;
3929
3930 return window;
3931}
3932
3933struct window *
3934window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003935 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003936{
3937 struct window *window;
3938
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003939 window = window_create_internal(parent->display,
3940 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003941 if (!window)
3942 return NULL;
3943
3944 window->x = x;
3945 window->y = y;
3946
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003947 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003948 wl_shell_surface_set_transient(
3949 window->shell_surface,
3950 window->parent->main_surface->surface,
3951 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003952
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003953 return window;
3954}
3955
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003956static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003957menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003958{
3959 int next;
3960
3961 next = (sy - 8) / 20;
3962 if (menu->current != next) {
3963 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003964 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003965 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003966}
3967
3968static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003969menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003970 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003971 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003972{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003973 struct menu *menu = data;
3974
3975 if (widget == menu->widget)
3976 menu_set_item(data, y);
3977
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003978 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003979}
3980
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003981static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003982menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003983 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003984{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003985 struct menu *menu = data;
3986
3987 if (widget == menu->widget)
3988 menu_set_item(data, y);
3989
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003990 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003991}
3992
3993static void
3994menu_leave_handler(struct widget *widget, struct input *input, void *data)
3995{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003996 struct menu *menu = data;
3997
3998 if (widget == menu->widget)
3999 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004000}
4001
4002static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004003menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004004 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004005 uint32_t button, enum wl_pointer_button_state state,
4006 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004007
4008{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004009 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004010
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004011 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4012 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004013 /* Either relase after press-drag-release or
4014 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004015 menu->func(menu->window->parent,
4016 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004017 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004018 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004019 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004020 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004021 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004022}
4023
4024static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004025menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004026{
4027 cairo_t *cr;
4028 const int32_t r = 3, margin = 3;
4029 struct menu *menu = data;
4030 int32_t width, height, i;
4031
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004032 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004033 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4034 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4035 cairo_paint(cr);
4036
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004037 width = widget->allocation.width;
4038 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004039 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004040
4041 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004042 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4043 cairo_fill(cr);
4044
4045 for (i = 0; i < menu->count; i++) {
4046 if (i == menu->current) {
4047 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4048 cairo_rectangle(cr, margin, i * 20 + margin,
4049 width - 2 * margin, 20);
4050 cairo_fill(cr);
4051 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4052 cairo_move_to(cr, 10, i * 20 + 16);
4053 cairo_show_text(cr, menu->entries[i]);
4054 } else {
4055 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4056 cairo_move_to(cr, 10, i * 20 + 16);
4057 cairo_show_text(cr, menu->entries[i]);
4058 }
4059 }
4060
4061 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004062}
4063
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004064void
4065window_show_menu(struct display *display,
4066 struct input *input, uint32_t time, struct window *parent,
4067 int32_t x, int32_t y,
4068 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004069{
4070 struct window *window;
4071 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004072 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004073
4074 menu = malloc(sizeof *menu);
4075 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004076 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004077
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004078 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004079 if (!window) {
4080 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004081 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004082 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004083
4084 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004085 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004086 menu->entries = entries;
4087 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004088 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004089 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004090 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004091 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004092 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004093 window->type = TYPE_MENU;
4094 window->x = x;
4095 window->y = y;
4096
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004097 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004098 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004099 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004100 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004101 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004102
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004103 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004104 widget_set_enter_handler(menu->widget, menu_enter_handler);
4105 widget_set_leave_handler(menu->widget, menu_leave_handler);
4106 widget_set_motion_handler(menu->widget, menu_motion_handler);
4107 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004108
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004109 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004110 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004111}
4112
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004113void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004114window_set_buffer_type(struct window *window, enum window_buffer_type type)
4115{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004116 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004117}
4118
Pekka Paalanen35e82632013-04-25 13:57:48 +03004119struct widget *
4120window_add_subsurface(struct window *window, void *data,
4121 enum subsurface_mode default_mode)
4122{
4123 struct widget *widget;
4124 struct surface *surface;
4125 struct wl_surface *parent;
4126 struct wl_subcompositor *subcompo = window->display->subcompositor;
4127
4128 if (!subcompo)
4129 return NULL;
4130
4131 surface = surface_create(window);
4132 widget = widget_create(window, surface, data);
4133 wl_list_init(&widget->link);
4134 surface->widget = widget;
4135
4136 parent = window->main_surface->surface;
4137 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4138 surface->surface,
4139 parent);
4140 surface->synchronized = 1;
4141
4142 switch (default_mode) {
4143 case SUBSURFACE_SYNCHRONIZED:
4144 surface->synchronized_default = 1;
4145 break;
4146 case SUBSURFACE_DESYNCHRONIZED:
4147 surface->synchronized_default = 0;
4148 break;
4149 default:
4150 assert(!"bad enum subsurface_mode");
4151 }
4152
4153 return widget;
4154}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004155
4156static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004157display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004158 struct wl_output *wl_output,
4159 int x, int y,
4160 int physical_width,
4161 int physical_height,
4162 int subpixel,
4163 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004164 const char *model,
4165 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004166{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004167 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004168
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004169 output->allocation.x = x;
4170 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004171 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004172}
4173
4174static void
4175display_handle_mode(void *data,
4176 struct wl_output *wl_output,
4177 uint32_t flags,
4178 int width,
4179 int height,
4180 int refresh)
4181{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004182 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004183 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004184
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004185 if (flags & WL_OUTPUT_MODE_CURRENT) {
4186 output->allocation.width = width;
4187 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004188 if (display->output_configure_handler)
4189 (*display->output_configure_handler)(
4190 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004191 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004192}
4193
4194static const struct wl_output_listener output_listener = {
4195 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004196 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004197};
4198
4199static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004200display_add_output(struct display *d, uint32_t id)
4201{
4202 struct output *output;
4203
4204 output = malloc(sizeof *output);
4205 if (output == NULL)
4206 return;
4207
4208 memset(output, 0, sizeof *output);
4209 output->display = d;
4210 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004211 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004212 wl_list_insert(d->output_list.prev, &output->link);
4213
4214 wl_output_add_listener(output->output, &output_listener, output);
4215}
4216
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004217static void
4218output_destroy(struct output *output)
4219{
4220 if (output->destroy_handler)
4221 (*output->destroy_handler)(output, output->user_data);
4222
4223 wl_output_destroy(output->output);
4224 wl_list_remove(&output->link);
4225 free(output);
4226}
4227
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004228void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004229display_set_global_handler(struct display *display,
4230 display_global_handler_t handler)
4231{
4232 struct global *global;
4233
4234 display->global_handler = handler;
4235 if (!handler)
4236 return;
4237
4238 wl_list_for_each(global, &display->global_list, link)
4239 display->global_handler(display,
4240 global->name, global->interface,
4241 global->version, display->user_data);
4242}
4243
4244void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004245display_set_output_configure_handler(struct display *display,
4246 display_output_handler_t handler)
4247{
4248 struct output *output;
4249
4250 display->output_configure_handler = handler;
4251 if (!handler)
4252 return;
4253
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004254 wl_list_for_each(output, &display->output_list, link) {
4255 if (output->allocation.width == 0 &&
4256 output->allocation.height == 0)
4257 continue;
4258
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004259 (*display->output_configure_handler)(output,
4260 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004261 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004262}
4263
4264void
4265output_set_user_data(struct output *output, void *data)
4266{
4267 output->user_data = data;
4268}
4269
4270void *
4271output_get_user_data(struct output *output)
4272{
4273 return output->user_data;
4274}
4275
4276void
4277output_set_destroy_handler(struct output *output,
4278 display_output_handler_t handler)
4279{
4280 output->destroy_handler = handler;
4281 /* FIXME: implement this, once we have way to remove outputs */
4282}
4283
4284void
Scott Moreau4e072362012-09-29 02:03:11 -06004285output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004286{
Scott Moreau4e072362012-09-29 02:03:11 -06004287 struct rectangle allocation = output->allocation;
4288
4289 switch (output->transform) {
4290 case WL_OUTPUT_TRANSFORM_90:
4291 case WL_OUTPUT_TRANSFORM_270:
4292 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4293 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4294 /* Swap width and height */
4295 allocation.width = output->allocation.height;
4296 allocation.height = output->allocation.width;
4297 break;
4298 }
4299
4300 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004301}
4302
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004303struct wl_output *
4304output_get_wl_output(struct output *output)
4305{
4306 return output->output;
4307}
4308
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004309enum wl_output_transform
4310output_get_transform(struct output *output)
4311{
4312 return output->transform;
4313}
4314
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004315static void
Daniel Stone97f68542012-05-30 16:32:01 +01004316fini_xkb(struct input *input)
4317{
4318 xkb_state_unref(input->xkb.state);
4319 xkb_map_unref(input->xkb.keymap);
4320}
4321
4322static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004323display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004324{
4325 struct input *input;
4326
4327 input = malloc(sizeof *input);
4328 if (input == NULL)
4329 return;
4330
4331 memset(input, 0, sizeof *input);
4332 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004333 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004334 input->pointer_focus = NULL;
4335 input->keyboard_focus = NULL;
4336 wl_list_insert(d->input_list.prev, &input->link);
4337
Daniel Stone37816df2012-05-16 18:45:18 +01004338 wl_seat_add_listener(input->seat, &seat_listener, input);
4339 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004340
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004341 input->data_device =
4342 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004343 input->seat);
4344 wl_data_device_add_listener(input->data_device, &data_device_listener,
4345 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004346
4347 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004348
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004349 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4350 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004351 input->repeat_task.run = keyboard_repeat_func;
4352 display_watch_fd(d, input->repeat_timer_fd,
4353 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004354}
4355
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004356static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004357input_destroy(struct input *input)
4358{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004359 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004360 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004361
4362 if (input->drag_offer)
4363 data_offer_destroy(input->drag_offer);
4364
4365 if (input->selection_offer)
4366 data_offer_destroy(input->selection_offer);
4367
4368 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004369 fini_xkb(input);
4370
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004371 wl_surface_destroy(input->pointer_surface);
4372
Pekka Paalanene1207c72011-12-16 12:02:09 +02004373 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004374 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004375 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004376 free(input);
4377}
4378
4379static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004380init_workspace_manager(struct display *d, uint32_t id)
4381{
4382 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004383 wl_registry_bind(d->registry, id,
4384 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004385 if (d->workspace_manager != NULL)
4386 workspace_manager_add_listener(d->workspace_manager,
4387 &workspace_manager_listener,
4388 d);
4389}
4390
4391static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004392registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4393 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004394{
4395 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004396 struct global *global;
4397
4398 global = malloc(sizeof *global);
4399 global->name = id;
4400 global->interface = strdup(interface);
4401 global->version = version;
4402 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004403
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004404 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004405 d->compositor = wl_registry_bind(registry, id,
4406 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004407 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004408 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004409 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004410 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004411 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004412 d->shell = wl_registry_bind(registry,
4413 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004414 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004415 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004416 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4417 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004418 wl_registry_bind(registry, id,
4419 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004420 } else if (strcmp(interface, "text_cursor_position") == 0) {
4421 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004422 wl_registry_bind(registry, id,
4423 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004424 } else if (strcmp(interface, "workspace_manager") == 0) {
4425 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004426 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4427 d->subcompositor =
4428 wl_registry_bind(registry, id,
4429 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004430 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004431
4432 if (d->global_handler)
4433 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004434}
4435
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004436static void
4437registry_handle_global_remove(void *data, struct wl_registry *registry,
4438 uint32_t name)
4439{
4440 struct display *d = data;
4441 struct global *global;
4442 struct global *tmp;
4443
4444 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4445 if (global->name != name)
4446 continue;
4447
4448 /* XXX: Should destroy bound globals, and call
4449 * the counterpart of display::global_handler
4450 */
4451 wl_list_remove(&global->link);
4452 free(global->interface);
4453 free(global);
4454 }
4455}
4456
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004457void *
4458display_bind(struct display *display, uint32_t name,
4459 const struct wl_interface *interface, uint32_t version)
4460{
4461 return wl_registry_bind(display->registry, name, interface, version);
4462}
4463
4464static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004465 registry_handle_global,
4466 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004467};
4468
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004469#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004470static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004471init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004472{
4473 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004474 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004475
Rob Clark6396ed32012-03-11 19:48:41 -05004476#ifdef USE_CAIRO_GLESV2
4477# define GL_BIT EGL_OPENGL_ES2_BIT
4478#else
4479# define GL_BIT EGL_OPENGL_BIT
4480#endif
4481
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004482 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004483 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004484 EGL_RED_SIZE, 1,
4485 EGL_GREEN_SIZE, 1,
4486 EGL_BLUE_SIZE, 1,
4487 EGL_ALPHA_SIZE, 1,
4488 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004489 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004490 EGL_NONE
4491 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004492
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004493#ifdef USE_CAIRO_GLESV2
4494 static const EGLint context_attribs[] = {
4495 EGL_CONTEXT_CLIENT_VERSION, 2,
4496 EGL_NONE
4497 };
4498 EGLint api = EGL_OPENGL_ES_API;
4499#else
4500 EGLint *context_attribs = NULL;
4501 EGLint api = EGL_OPENGL_API;
4502#endif
4503
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004504 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004505 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004506 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004507 return -1;
4508 }
4509
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004510 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004511 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004512 return -1;
4513 }
4514
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004515 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4516 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004517 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004518 return -1;
4519 }
4520
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004521 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004522 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004523 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004524 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004525 return -1;
4526 }
4527
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004528 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004529 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004530 return -1;
4531 }
4532
Benjamin Franzke0c991632011-09-27 21:57:31 +02004533 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4534 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004535 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004536 return -1;
4537 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004538
4539 return 0;
4540}
4541
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004542static void
4543fini_egl(struct display *display)
4544{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004545 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004546
4547 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4548 EGL_NO_CONTEXT);
4549
4550 eglTerminate(display->dpy);
4551 eglReleaseThread();
4552}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004553#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004554
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004555static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004556init_dummy_surface(struct display *display)
4557{
4558 int len;
4559 void *data;
4560
4561 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4562 data = malloc(len);
4563 display->dummy_surface =
4564 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4565 1, 1, len);
4566 display->dummy_surface_data = data;
4567}
4568
4569static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004570handle_display_data(struct task *task, uint32_t events)
4571{
4572 struct display *display =
4573 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004574 struct epoll_event ep;
4575 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004576
4577 display->display_fd_events = events;
4578
4579 if (events & EPOLLERR || events & EPOLLHUP) {
4580 display_exit(display);
4581 return;
4582 }
4583
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004584 if (events & EPOLLIN) {
4585 ret = wl_display_dispatch(display->display);
4586 if (ret == -1) {
4587 display_exit(display);
4588 return;
4589 }
4590 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004591
4592 if (events & EPOLLOUT) {
4593 ret = wl_display_flush(display->display);
4594 if (ret == 0) {
4595 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4596 ep.data.ptr = &display->display_task;
4597 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4598 display->display_fd, &ep);
4599 } else if (ret == -1 && errno != EAGAIN) {
4600 display_exit(display);
4601 return;
4602 }
4603 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004604}
4605
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004606static void
4607log_handler(const char *format, va_list args)
4608{
4609 vfprintf(stderr, format, args);
4610}
4611
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004612struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004613display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004614{
4615 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004616
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004617 wl_log_set_handler_client(log_handler);
4618
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004619 d = malloc(sizeof *d);
4620 if (d == NULL)
4621 return NULL;
4622
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004623 memset(d, 0, sizeof *d);
4624
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004625 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004626 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004627 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004628 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004629 return NULL;
4630 }
4631
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004632 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004633 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004634 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004635 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4636 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004637
4638 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004639 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004640 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004641 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004642
Daniel Stone97f68542012-05-30 16:32:01 +01004643 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004644 if (d->xkb_context == NULL) {
4645 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004646 return NULL;
4647 }
4648
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004649 d->workspace = 0;
4650 d->workspace_count = 1;
4651
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004652 d->registry = wl_display_get_registry(d->display);
4653 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004654
4655 if (wl_display_dispatch(d->display) < 0) {
4656 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4657 return NULL;
4658 }
4659
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004660#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004661 if (init_egl(d) < 0)
4662 fprintf(stderr, "EGL does not seem to work, "
4663 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004664#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004665
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004666 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004667
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004668 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004669
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004670 wl_list_init(&d->window_list);
4671
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004672 init_dummy_surface(d);
4673
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004674 return d;
4675}
4676
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004677static void
4678display_destroy_outputs(struct display *display)
4679{
4680 struct output *tmp;
4681 struct output *output;
4682
4683 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4684 output_destroy(output);
4685}
4686
Pekka Paalanene1207c72011-12-16 12:02:09 +02004687static void
4688display_destroy_inputs(struct display *display)
4689{
4690 struct input *tmp;
4691 struct input *input;
4692
4693 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4694 input_destroy(input);
4695}
4696
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004697void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004698display_destroy(struct display *display)
4699{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004700 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004701 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4702 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004703
4704 if (!wl_list_empty(&display->deferred_list))
4705 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4706
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004707 cairo_surface_destroy(display->dummy_surface);
4708 free(display->dummy_surface_data);
4709
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004710 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004711 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004712
Daniel Stone97f68542012-05-30 16:32:01 +01004713 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004714
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004715 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004716 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004717
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004718#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004719 if (display->argb_device)
4720 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004721#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004722
Pekka Paalanen35e82632013-04-25 13:57:48 +03004723 if (display->subcompositor)
4724 wl_subcompositor_destroy(display->subcompositor);
4725
Pekka Paalanenc2052982011-12-16 11:41:32 +02004726 if (display->shell)
4727 wl_shell_destroy(display->shell);
4728
4729 if (display->shm)
4730 wl_shm_destroy(display->shm);
4731
4732 if (display->data_device_manager)
4733 wl_data_device_manager_destroy(display->data_device_manager);
4734
4735 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004736 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004737
4738 close(display->epoll_fd);
4739
U. Artie Eoff44874d92012-10-02 21:12:35 -07004740 if (!(display->display_fd_events & EPOLLERR) &&
4741 !(display->display_fd_events & EPOLLHUP))
4742 wl_display_flush(display->display);
4743
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004744 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004745 free(display);
4746}
4747
4748void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004749display_set_user_data(struct display *display, void *data)
4750{
4751 display->user_data = data;
4752}
4753
4754void *
4755display_get_user_data(struct display *display)
4756{
4757 return display->user_data;
4758}
4759
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004760struct wl_display *
4761display_get_display(struct display *display)
4762{
4763 return display->display;
4764}
4765
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004766struct output *
4767display_get_output(struct display *display)
4768{
4769 return container_of(display->output_list.next, struct output, link);
4770}
4771
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004772struct wl_compositor *
4773display_get_compositor(struct display *display)
4774{
4775 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004776}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004777
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004778uint32_t
4779display_get_serial(struct display *display)
4780{
4781 return display->serial;
4782}
4783
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004784EGLDisplay
4785display_get_egl_display(struct display *d)
4786{
4787 return d->dpy;
4788}
4789
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004790struct wl_data_source *
4791display_create_data_source(struct display *display)
4792{
4793 return wl_data_device_manager_create_data_source(display->data_device_manager);
4794}
4795
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004796EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004797display_get_argb_egl_config(struct display *d)
4798{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004799 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004800}
4801
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004802struct wl_shell *
4803display_get_shell(struct display *display)
4804{
4805 return display->shell;
4806}
4807
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004808int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004809display_acquire_window_surface(struct display *display,
4810 struct window *window,
4811 EGLContext ctx)
4812{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004813 struct surface *surface = window->main_surface;
4814
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004815 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004816 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004817
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004818 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004819 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004820}
4821
4822void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004823display_release_window_surface(struct display *display,
4824 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004825{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004826 struct surface *surface = window->main_surface;
4827
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004828 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004829 return;
4830
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004831 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004832}
4833
4834void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004835display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004836{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004837 wl_list_insert(&display->deferred_list, &task->link);
4838}
4839
4840void
4841display_watch_fd(struct display *display,
4842 int fd, uint32_t events, struct task *task)
4843{
4844 struct epoll_event ep;
4845
4846 ep.events = events;
4847 ep.data.ptr = task;
4848 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4849}
4850
4851void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004852display_unwatch_fd(struct display *display, int fd)
4853{
4854 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4855}
4856
4857void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004858display_run(struct display *display)
4859{
4860 struct task *task;
4861 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004862 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004863
Pekka Paalanen826d7952011-12-15 10:14:07 +02004864 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004865 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004866 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004867 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004868 struct task, link);
4869 wl_list_remove(&task->link);
4870 task->run(task, 0);
4871 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004872
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004873 wl_display_dispatch_pending(display->display);
4874
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004875 if (!display->running)
4876 break;
4877
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004878 ret = wl_display_flush(display->display);
4879 if (ret < 0 && errno == EAGAIN) {
4880 ep[0].events =
4881 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4882 ep[0].data.ptr = &display->display_task;
4883
4884 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4885 display->display_fd, &ep[0]);
4886 } else if (ret < 0) {
4887 break;
4888 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004889
4890 count = epoll_wait(display->epoll_fd,
4891 ep, ARRAY_LENGTH(ep), -1);
4892 for (i = 0; i < count; i++) {
4893 task = ep[i].data.ptr;
4894 task->run(task, ep[i].events);
4895 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004896 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004897}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004898
4899void
4900display_exit(struct display *display)
4901{
4902 display->running = 0;
4903}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004904
4905void
4906keysym_modifiers_add(struct wl_array *modifiers_map,
4907 const char *name)
4908{
4909 size_t len = strlen(name) + 1;
4910 char *p;
4911
4912 p = wl_array_add(modifiers_map, len);
4913
4914 if (p == NULL)
4915 return;
4916
4917 strncpy(p, name, len);
4918}
4919
4920static xkb_mod_index_t
4921keysym_modifiers_get_index(struct wl_array *modifiers_map,
4922 const char *name)
4923{
4924 xkb_mod_index_t index = 0;
4925 char *p = modifiers_map->data;
4926
4927 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4928 if (strcmp(p, name) == 0)
4929 return index;
4930
4931 index++;
4932 p += strlen(p) + 1;
4933 }
4934
4935 return XKB_MOD_INVALID;
4936}
4937
4938xkb_mod_mask_t
4939keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4940 const char *name)
4941{
4942 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4943
4944 if (index == XKB_MOD_INVALID)
4945 return XKB_MOD_INVALID;
4946
4947 return 1 << index;
4948}