blob: ae8792e867046b68187228f4e82223915a0a9493 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
99 struct {
100 struct xkb_rule_names names;
101 struct xkb_keymap *keymap;
102 struct xkb_state *state;
103 struct xkb_context *context;
104 xkb_mod_mask_t control_mask;
105 xkb_mod_mask_t alt_mask;
106 xkb_mod_mask_t shift_mask;
107 } xkb;
108
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300109 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300110 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400111
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200115
116 display_output_handler_t output_configure_handler;
117
118 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500119};
120
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400122 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400124 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500125 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400126 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500127 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128 TYPE_CUSTOM
129};
Rob Bradford7507b572012-05-15 17:55:34 +0100130
131struct window_output {
132 struct output *output;
133 struct wl_list link;
134};
135
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500136struct window {
137 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500138 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100139 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500140 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200141 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500142 struct wl_region *input_region;
143 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500144 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500145 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500146 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500147 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400148 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400149 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400150 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400151 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400152 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400153 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400154 int transparent;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600155 int send_cursor_position;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400156 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400157 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500158
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400159 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500160
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700161 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400162
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500163 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500164 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400165 window_data_handler_t data_handler;
166 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400168
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200169 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500171
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500172 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400173 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500174};
175
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500176struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500177 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300178 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500179 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400180 struct wl_list link;
181 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500182 widget_resize_handler_t resize_handler;
183 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500184 widget_enter_handler_t enter_handler;
185 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500186 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500187 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400188 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500189 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300190 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400191};
192
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193struct input {
194 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100195 struct wl_seat *seat;
196 struct wl_pointer *pointer;
197 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct window *pointer_focus;
199 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300200 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400202 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400203 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400204 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400205
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500206 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500207 struct widget *grab;
208 uint32_t grab_button;
209
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400210 struct wl_data_device *data_device;
211 struct data_offer *drag_offer;
212 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400213};
214
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215struct output {
216 struct display *display;
217 struct wl_output *output;
218 struct rectangle allocation;
219 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200220
221 display_output_handler_t destroy_handler;
222 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500223};
224
Martin Minarik1998b152012-05-10 02:04:35 +0200225enum frame_button_action {
226 FRAME_BUTTON_NULL = 0,
227 FRAME_BUTTON_ICON = 1,
228 FRAME_BUTTON_CLOSE = 2,
229 FRAME_BUTTON_MINIMIZE = 3,
230 FRAME_BUTTON_MAXIMIZE = 4,
231};
232
233enum frame_button_pointer {
234 FRAME_BUTTON_DEFAULT = 0,
235 FRAME_BUTTON_OVER = 1,
236 FRAME_BUTTON_ACTIVE = 2,
237};
238
239enum frame_button_align {
240 FRAME_BUTTON_RIGHT = 0,
241 FRAME_BUTTON_LEFT = 1,
242};
243
244enum frame_button_decoration {
245 FRAME_BUTTON_NONE = 0,
246 FRAME_BUTTON_FANCY = 1,
247};
248
249struct frame_button {
250 struct widget *widget;
251 struct frame *frame;
252 cairo_surface_t *icon;
253 enum frame_button_action type;
254 enum frame_button_pointer state;
255 struct wl_list link; /* buttons_list */
256 enum frame_button_align align;
257 enum frame_button_decoration decoration;
258};
259
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500260struct frame {
261 struct widget *widget;
262 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200263 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500264};
265
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500266struct menu {
267 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500268 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500269 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500270 const char **entries;
271 uint32_t time;
272 int current;
273 int count;
274 menu_func_t func;
275};
276
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300277struct tooltip {
278 struct widget *parent;
279 struct window *window;
280 struct widget *widget;
281 char *entry;
282 struct task tooltip_task;
283 int tooltip_fd;
284 float x, y;
285};
286
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700287struct shm_pool {
288 struct wl_shm_pool *pool;
289 size_t size;
290 size_t used;
291 void *data;
292};
293
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400294enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300295 CURSOR_DEFAULT = 100,
296 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400297};
298
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500299enum window_location {
300 WINDOW_INTERIOR = 0,
301 WINDOW_RESIZING_TOP = 1,
302 WINDOW_RESIZING_BOTTOM = 2,
303 WINDOW_RESIZING_LEFT = 4,
304 WINDOW_RESIZING_TOP_LEFT = 5,
305 WINDOW_RESIZING_BOTTOM_LEFT = 6,
306 WINDOW_RESIZING_RIGHT = 8,
307 WINDOW_RESIZING_TOP_RIGHT = 9,
308 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
309 WINDOW_RESIZING_MASK = 15,
310 WINDOW_EXTERIOR = 16,
311 WINDOW_TITLEBAR = 17,
312 WINDOW_CLIENT_AREA = 18,
313};
314
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400315const char *option_xkb_layout = "us";
316const char *option_xkb_variant = "";
317const char *option_xkb_options = "";
318
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400319static const struct weston_option xkb_options[] = {
320 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
321 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
322 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400323};
324
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325static const cairo_user_data_key_t surface_data_key;
326struct surface_data {
327 struct wl_buffer *buffer;
328};
329
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500330#define MULT(_d,c,a,t) \
331 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
332
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500333#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100335struct egl_window_surface_data {
336 struct display *display;
337 struct wl_surface *surface;
338 struct wl_egl_window *window;
339 EGLSurface surf;
340};
341
342static void
343egl_window_surface_data_destroy(void *p)
344{
345 struct egl_window_surface_data *data = p;
346 struct display *d = data->display;
347
348 eglDestroySurface(d->dpy, data->surf);
349 wl_egl_window_destroy(data->window);
350 data->surface = NULL;
351
352 free(p);
353}
354
355static cairo_surface_t *
356display_create_egl_window_surface(struct display *display,
357 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400358 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359 struct rectangle *rectangle)
360{
361 cairo_surface_t *cairo_surface;
362 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200364 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100365
366 data = malloc(sizeof *data);
367 if (data == NULL)
368 return NULL;
369
370 data->display = display;
371 data->surface = surface;
372
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500373 config = display->argb_config;
374 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400376 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500381 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100382
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384 data->surf,
385 rectangle->width,
386 rectangle->height);
387
388 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
389 data, egl_window_surface_data_destroy);
390
391 return cairo_surface;
392}
393
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394#endif
395
396struct wl_buffer *
397display_get_buffer_for_surface(struct display *display,
398 cairo_surface_t *surface)
399{
400 struct surface_data *data;
401
402 data = cairo_surface_get_user_data (surface, &surface_data_key);
403
404 return data->buffer;
405}
406
407struct shm_surface_data {
408 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410};
411
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500412static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700413shm_pool_destroy(struct shm_pool *pool);
414
415static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416shm_surface_data_destroy(void *p)
417{
418 struct shm_surface_data *data = p;
419
420 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700421 if (data->pool)
422 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423}
424
Kristian Høgsberg16626282012-04-03 11:21:27 -0400425static struct wl_shm_pool *
426make_shm_pool(struct display *display, int size, void **data)
427{
428 char filename[] = "/tmp/wayland-shm-XXXXXX";
429 struct wl_shm_pool *pool;
430 int fd;
431
432 fd = mkstemp(filename);
433 if (fd < 0) {
434 fprintf(stderr, "open %s failed: %m\n", filename);
435 return NULL;
436 }
437 if (ftruncate(fd, size) < 0) {
438 fprintf(stderr, "ftruncate failed: %m\n");
439 close(fd);
440 return NULL;
441 }
442
443 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
444 unlink(filename);
445
446 if (*data == MAP_FAILED) {
447 fprintf(stderr, "mmap failed: %m\n");
448 close(fd);
449 return NULL;
450 }
451
452 pool = wl_shm_create_pool(display->shm, fd, size);
453
454 close(fd);
455
456 return pool;
457}
458
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700459static struct shm_pool *
460shm_pool_create(struct display *display, size_t size)
461{
462 struct shm_pool *pool = malloc(sizeof *pool);
463
464 if (!pool)
465 return NULL;
466
467 pool->pool = make_shm_pool(display, size, &pool->data);
468 if (!pool->pool) {
469 free(pool);
470 return NULL;
471 }
472
473 pool->size = size;
474 pool->used = 0;
475
476 return pool;
477}
478
479static void *
480shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
481{
482 if (pool->used + size > pool->size)
483 return NULL;
484
485 *offset = pool->used;
486 pool->used += size;
487
488 return (char *) pool->data + *offset;
489}
490
491/* destroy the pool. this does not unmap the memory though */
492static void
493shm_pool_destroy(struct shm_pool *pool)
494{
495 munmap(pool->data, pool->size);
496 wl_shm_pool_destroy(pool->pool);
497 free(pool);
498}
499
500/* Start allocating from the beginning of the pool again */
501static void
502shm_pool_reset(struct shm_pool *pool)
503{
504 pool->used = 0;
505}
506
507static int
508data_length_for_shm_surface(struct rectangle *rect)
509{
510 int stride;
511
512 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
513 rect->width);
514 return stride * rect->height;
515}
516
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500517static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700518display_create_shm_surface_from_pool(struct display *display,
519 struct rectangle *rectangle,
520 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521{
522 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400523 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400524 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700525 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400526 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527
528 data = malloc(sizeof *data);
529 if (data == NULL)
530 return NULL;
531
532 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
533 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700534 length = stride * rectangle->height;
535 data->pool = NULL;
536 map = shm_pool_allocate(pool, length, &offset);
537
538 if (!map) {
539 free(data);
540 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400541 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400543 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544 CAIRO_FORMAT_ARGB32,
545 rectangle->width,
546 rectangle->height,
547 stride);
548
549 cairo_surface_set_user_data (surface, &surface_data_key,
550 data, shm_surface_data_destroy);
551
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500553 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400554 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500555 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700557 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400558 rectangle->width,
559 rectangle->height,
560 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700562 return surface;
563}
564
565static cairo_surface_t *
566display_create_shm_surface(struct display *display,
567 struct rectangle *rectangle, uint32_t flags,
568 struct window *window)
569{
570 struct shm_surface_data *data;
571 struct shm_pool *pool;
572 cairo_surface_t *surface;
573
574 if (window && window->pool) {
575 shm_pool_reset(window->pool);
576 surface = display_create_shm_surface_from_pool(display,
577 rectangle,
578 flags,
579 window->pool);
580 if (surface)
581 return surface;
582 }
583
584 pool = shm_pool_create(display,
585 data_length_for_shm_surface(rectangle));
586 if (!pool)
587 return NULL;
588
589 surface =
590 display_create_shm_surface_from_pool(display, rectangle,
591 flags, pool);
592
593 if (!surface) {
594 shm_pool_destroy(pool);
595 return NULL;
596 }
597
598 /* make sure we destroy the pool when the surface is destroyed */
599 data = cairo_surface_get_user_data(surface, &surface_data_key);
600 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601
602 return surface;
603}
604
nobled7b87cb02011-02-01 18:51:47 +0000605static int
606check_size(struct rectangle *rect)
607{
608 if (rect->width && rect->height)
609 return 0;
610
611 fprintf(stderr, "tried to create surface of "
612 "width: %d, height: %d\n", rect->width, rect->height);
613 return -1;
614}
615
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616cairo_surface_t *
617display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100618 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400619 struct rectangle *rectangle,
620 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621{
nobled7b87cb02011-02-01 18:51:47 +0000622 if (check_size(rectangle) < 0)
623 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500624#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300625 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400626 return display_create_egl_window_surface(display,
627 surface,
628 flags,
629 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400631 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632}
633
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300634static const char *cursors[] = {
635 "bottom_left_corner",
636 "bottom_right_corner",
637 "bottom_side",
638 "grabbing",
639 "left_ptr",
640 "left_side",
641 "right_side",
642 "top_left_corner",
643 "top_right_corner",
644 "top_side",
645 "xterm",
646 "hand1",
647};
648
649static void
650create_cursors(struct display *display)
651{
652 unsigned int i;
653
654 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
655 display->cursors =
656 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
657
658 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
659 display->cursors[i] =
660 wl_cursor_theme_get_cursor(display->cursor_theme,
661 cursors[i]);
662}
663
664static void
665destroy_cursors(struct display *display)
666{
667 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800668 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300669}
670
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300671struct wl_cursor_image *
672display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300674 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300675 wl_cursor_theme_get_cursor(display->cursor_theme,
676 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300678 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400679}
680
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400681static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200682window_get_resize_dx_dy(struct window *window, int *x, int *y)
683{
684 if (window->resize_edges & WINDOW_RESIZING_LEFT)
685 *x = window->server_allocation.width - window->allocation.width;
686 else
687 *x = 0;
688
689 if (window->resize_edges & WINDOW_RESIZING_TOP)
690 *y = window->server_allocation.height -
691 window->allocation.height;
692 else
693 *y = 0;
694
695 window->resize_edges = 0;
696}
697
698static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500699window_attach_surface(struct window *window)
700{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400701 struct display *display = window->display;
702 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000703#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100704 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000705#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500706 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100707
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400708 if (window->type == TYPE_NONE) {
709 window->type = TYPE_TOPLEVEL;
710 if (display->shell)
711 wl_shell_surface_set_toplevel(window->shell_surface);
712 }
713
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000715#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
717 data = cairo_surface_get_user_data(window->cairo_surface,
718 &surface_data_key);
719
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 cairo_gl_surface_swapbuffers(window->cairo_surface);
721 wl_egl_window_get_attached_size(data->window,
722 &window->server_allocation.width,
723 &window->server_allocation.height);
724 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000725#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 buffer =
728 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400729 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200731 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 wl_surface_damage(window->surface, 0, 0,
734 window->allocation.width,
735 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400737 cairo_surface_destroy(window->cairo_surface);
738 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000740 default:
741 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500743
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500744 if (window->input_region) {
745 wl_surface_set_input_region(window->surface,
746 window->input_region);
747 wl_region_destroy(window->input_region);
748 window->input_region = NULL;
749 }
750
751 if (window->opaque_region) {
752 wl_surface_set_opaque_region(window->surface,
753 window->opaque_region);
754 wl_region_destroy(window->opaque_region);
755 window->opaque_region = NULL;
756 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500757}
758
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500759void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400760window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500761{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400762 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100763 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500764}
765
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400766void
767window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400768{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500769 cairo_surface_reference(surface);
770
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771 if (window->cairo_surface != NULL)
772 cairo_surface_destroy(window->cairo_surface);
773
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500774 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400775}
776
Benjamin Franzke22d54812011-07-16 19:50:32 +0000777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778static void
779window_resize_cairo_window_surface(struct window *window)
780{
781 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200782 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783
784 data = cairo_surface_get_user_data(window->cairo_surface,
785 &surface_data_key);
786
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 wl_egl_window_resize(data->window,
789 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200790 window->allocation.height,
791 x,y);
792
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793 cairo_gl_surface_set_size(window->cairo_surface,
794 window->allocation.width,
795 window->allocation.height);
796}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400799struct display *
800window_get_display(struct window *window)
801{
802 return window->display;
803}
804
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400805void
806window_create_surface(struct window *window)
807{
808 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400809 uint32_t flags = 0;
810
811 if (!window->transparent)
812 flags = SURFACE_OPAQUE;
813
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500815#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
817 if (window->cairo_surface) {
818 window_resize_cairo_window_surface(window);
819 return;
820 }
821 surface = display_create_surface(window->display,
822 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400823 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825#endif
826 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400827 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400828 &window->allocation,
829 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800831 default:
832 surface = NULL;
833 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400835
836 window_set_surface(window, surface);
837 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838}
839
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200840static void frame_destroy(struct frame *frame);
841
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400842void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500843window_destroy(struct window *window)
844{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200845 struct display *display = window->display;
846 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100847 struct window_output *window_output;
848 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200849
850 if (window->redraw_scheduled)
851 wl_list_remove(&window->redraw_task.link);
852
853 wl_list_for_each(input, &display->input_list, link) {
854 if (input->pointer_focus == window)
855 input->pointer_focus = NULL;
856 if (input->keyboard_focus == window)
857 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500858 if (input->focus_widget &&
859 input->focus_widget->window == window)
860 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200861 }
862
Rob Bradford7507b572012-05-15 17:55:34 +0100863 wl_list_for_each_safe(window_output, window_output_tmp,
864 &window->window_output_list, link) {
865 free (window_output);
866 }
867
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500868 if (window->input_region)
869 wl_region_destroy(window->input_region);
870 if (window->opaque_region)
871 wl_region_destroy(window->opaque_region);
872
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200873 if (window->frame)
874 frame_destroy(window->frame);
875
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200876 if (window->shell_surface)
877 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500878 wl_surface_destroy(window->surface);
879 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200880
881 if (window->cairo_surface != NULL)
882 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200883
884 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500885 free(window);
886}
887
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500888static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400890{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500891 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400892
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500893 wl_list_for_each(child, &widget->child_list, link) {
894 target = widget_find_widget(child, x, y);
895 if (target)
896 return target;
897 }
898
899 if (widget->allocation.x <= x &&
900 x < widget->allocation.x + widget->allocation.width &&
901 widget->allocation.y <= y &&
902 y < widget->allocation.y + widget->allocation.height) {
903 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400904 }
905
906 return NULL;
907}
908
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500909static struct widget *
910widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400911{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500912 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400913
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500914 widget = malloc(sizeof *widget);
915 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500916 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500917 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500918 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500919 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500920 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300921 widget->tooltip = NULL;
922 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500923
924 return widget;
925}
926
927struct widget *
928window_add_widget(struct window *window, void *data)
929{
930 window->widget = widget_create(window, data);
931 wl_list_init(&window->widget->link);
932
933 return window->widget;
934}
935
936struct widget *
937widget_add_widget(struct widget *parent, void *data)
938{
939 struct widget *widget;
940
941 widget = widget_create(parent->window, data);
942 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400943
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500944 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400945}
946
947void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500948widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200950 struct display *display = widget->window->display;
951 struct input *input;
952
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300953 if (widget->tooltip) {
954 free(widget->tooltip);
955 widget->tooltip = NULL;
956 }
957
Pekka Paalanene156fb62012-01-19 13:51:38 +0200958 wl_list_for_each(input, &display->input_list, link) {
959 if (input->focus_widget == widget)
960 input->focus_widget = NULL;
961 }
962
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500963 wl_list_remove(&widget->link);
964 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965}
966
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500968widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500970 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400971}
972
973void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974widget_set_size(struct widget *widget, int32_t width, int32_t height)
975{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500976 widget->allocation.width = width;
977 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500978}
979
980void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981widget_set_allocation(struct widget *widget,
982 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500984 widget->allocation.x = x;
985 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200986 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987}
988
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500989void
990widget_set_transparent(struct widget *widget, int transparent)
991{
992 widget->opaque = !transparent;
993}
994
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999}
1000
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001001void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001002widget_set_resize_handler(struct widget *widget,
1003 widget_resize_handler_t handler)
1004{
1005 widget->resize_handler = handler;
1006}
1007
1008void
1009widget_set_redraw_handler(struct widget *widget,
1010 widget_redraw_handler_t handler)
1011{
1012 widget->redraw_handler = handler;
1013}
1014
1015void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001016widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001017{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001018 widget->enter_handler = handler;
1019}
1020
1021void
1022widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1023{
1024 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001025}
1026
1027void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001028widget_set_motion_handler(struct widget *widget,
1029 widget_motion_handler_t handler)
1030{
1031 widget->motion_handler = handler;
1032}
1033
1034void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001035widget_set_button_handler(struct widget *widget,
1036 widget_button_handler_t handler)
1037{
1038 widget->button_handler = handler;
1039}
1040
1041void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001042widget_schedule_redraw(struct widget *widget)
1043{
1044 window_schedule_redraw(widget->window);
1045}
1046
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047cairo_surface_t *
1048window_get_surface(struct window *window)
1049{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001050 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001051}
1052
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001053struct wl_surface *
1054window_get_wl_surface(struct window *window)
1055{
1056 return window->surface;
1057}
1058
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001059struct wl_shell_surface *
1060window_get_wl_shell_surface(struct window *window)
1061{
1062 return window->shell_surface;
1063}
1064
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001065static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001066tooltip_redraw_handler(struct widget *widget, void *data)
1067{
1068 cairo_t *cr;
1069 const int32_t r = 3;
1070 struct tooltip *tooltip = data;
1071 int32_t width, height;
1072 struct window *window = widget->window;
1073
1074 cr = cairo_create(window->cairo_surface);
1075 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1076 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1077 cairo_paint(cr);
1078
1079 width = window->allocation.width;
1080 height = window->allocation.height;
1081 rounded_rect(cr, 0, 0, width, height, r);
1082
1083 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1084 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1085 cairo_fill(cr);
1086
1087 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1088 cairo_move_to(cr, 10, 16);
1089 cairo_show_text(cr, tooltip->entry);
1090 cairo_destroy(cr);
1091}
1092
1093static cairo_text_extents_t
1094get_text_extents(struct tooltip *tooltip)
1095{
1096 struct window *window;
1097 cairo_t *cr;
1098 cairo_text_extents_t extents;
1099
1100 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1101 * created yet */
1102 window = tooltip->widget->window->parent;
1103 cr = cairo_create(window->cairo_surface);
1104 cairo_text_extents(cr, tooltip->entry, &extents);
1105 cairo_destroy(cr);
1106
1107 return extents;
1108}
1109
1110static int
1111window_create_tooltip(struct tooltip *tooltip)
1112{
1113 struct widget *parent = tooltip->parent;
1114 struct display *display = parent->window->display;
1115 struct window *window;
1116 const int offset_y = 27;
1117 const int margin = 3;
1118 cairo_text_extents_t extents;
1119
1120 if (tooltip->widget)
1121 return 0;
1122
1123 window = window_create_transient(display, parent->window, tooltip->x,
1124 tooltip->y + offset_y,
1125 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1126 if (!window)
1127 return -1;
1128
1129 tooltip->window = window;
1130 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1131
1132 extents = get_text_extents(tooltip);
1133 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1134 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1135
1136 return 0;
1137}
1138
1139void
1140widget_destroy_tooltip(struct widget *parent)
1141{
1142 struct tooltip *tooltip = parent->tooltip;
1143
1144 parent->tooltip_count = 0;
1145 if (!tooltip)
1146 return;
1147
1148 if (tooltip->widget) {
1149 widget_destroy(tooltip->widget);
1150 window_destroy(tooltip->window);
1151 tooltip->widget = NULL;
1152 tooltip->window = NULL;
1153 }
1154
1155 close(tooltip->tooltip_fd);
1156 free(tooltip->entry);
1157 free(tooltip);
1158 parent->tooltip = NULL;
1159}
1160
1161static void
1162tooltip_func(struct task *task, uint32_t events)
1163{
1164 struct tooltip *tooltip =
1165 container_of(task, struct tooltip, tooltip_task);
1166 uint64_t exp;
1167
1168 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1169 window_create_tooltip(tooltip);
1170}
1171
1172#define TOOLTIP_TIMEOUT 500
1173static int
1174tooltip_timer_reset(struct tooltip *tooltip)
1175{
1176 struct itimerspec its;
1177
1178 its.it_interval.tv_sec = 0;
1179 its.it_interval.tv_nsec = 0;
1180 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1181 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1182 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1183 fprintf(stderr, "could not set timerfd\n: %m");
1184 return -1;
1185 }
1186
1187 return 0;
1188}
1189
1190int
1191widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1192{
1193 struct tooltip *tooltip = parent->tooltip;
1194
1195 parent->tooltip_count++;
1196 if (tooltip) {
1197 tooltip->x = x;
1198 tooltip->y = y;
1199 tooltip_timer_reset(tooltip);
1200 return 0;
1201 }
1202
1203 /* the handler might be triggered too fast via input device motion, so
1204 * we need this check here to make sure tooltip is fully initialized */
1205 if (parent->tooltip_count > 1)
1206 return 0;
1207
1208 tooltip = malloc(sizeof *tooltip);
1209 if (!tooltip)
1210 return -1;
1211
1212 parent->tooltip = tooltip;
1213 tooltip->parent = parent;
1214 tooltip->widget = NULL;
1215 tooltip->window = NULL;
1216 tooltip->x = x;
1217 tooltip->y = y;
1218 tooltip->entry = strdup(entry);
1219 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1220 if (tooltip->tooltip_fd < 0) {
1221 fprintf(stderr, "could not create timerfd\n: %m");
1222 return -1;
1223 }
1224
1225 tooltip->tooltip_task.run = tooltip_func;
1226 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1227 EPOLLIN, &tooltip->tooltip_task);
1228 tooltip_timer_reset(tooltip);
1229
1230 return 0;
1231}
1232
1233static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001234frame_resize_handler(struct widget *widget,
1235 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 struct frame *frame = data;
1238 struct widget *child = frame->child;
1239 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001240 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001242 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001243 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001245 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001247 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 decoration_width = (t->width + t->margin) * 2;
1249 decoration_height = t->width +
1250 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001252 allocation.x = t->width + t->margin;
1253 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001254 allocation.width = width - decoration_width;
1255 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001256
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001257 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001258
1259 wl_list_for_each(button, &frame->buttons_list, link)
1260 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001261 } else {
1262 decoration_width = 0;
1263 decoration_height = 0;
1264
1265 allocation.x = 0;
1266 allocation.y = 0;
1267 allocation.width = width;
1268 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001270
1271 wl_list_for_each(button, &frame->buttons_list, link)
1272 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001273 }
1274
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275 widget_set_allocation(child, allocation.x, allocation.y,
1276 allocation.width, allocation.height);
1277
1278 if (child->resize_handler)
1279 child->resize_handler(child,
1280 allocation.width,
1281 allocation.height,
1282 child->user_data);
1283
Scott Moreauf7e498c2012-05-14 11:39:29 -06001284 width = child->allocation.width + decoration_width;
1285 height = child->allocation.height + decoration_height;
1286
1287 widget->window->input_region =
1288 wl_compositor_create_region(display->compositor);
1289 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001290 t->margin, t->margin,
1291 width - 2 * t->margin,
1292 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001293
1294 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001295
1296 if (child->opaque) {
1297 widget->window->opaque_region =
1298 wl_compositor_create_region(display->compositor);
1299 wl_region_add(widget->window->opaque_region,
1300 opaque_margin, opaque_margin,
1301 widget->allocation.width - 2 * opaque_margin,
1302 widget->allocation.height - 2 * opaque_margin);
1303 }
Martin Minarik1998b152012-05-10 02:04:35 +02001304
1305 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001306 x_r = frame->widget->allocation.width - t->width - t->margin;
1307 x_l = t->width + t->margin;
1308 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001309 wl_list_for_each(button, &frame->buttons_list, link) {
1310 const int button_padding = 4;
1311 w = cairo_image_surface_get_width(button->icon);
1312 h = cairo_image_surface_get_height(button->icon);
1313
1314 if (button->decoration == FRAME_BUTTON_FANCY)
1315 w += 10;
1316
1317 if (button->align == FRAME_BUTTON_LEFT) {
1318 widget_set_allocation(button->widget,
1319 x_l, y , w + 1, h + 1);
1320 x_l += w;
1321 x_l += button_padding;
1322 } else {
1323 x_r -= w;
1324 widget_set_allocation(button->widget,
1325 x_r, y , w + 1, h + 1);
1326 x_r -= button_padding;
1327 }
1328 }
1329}
1330
1331static int
1332frame_button_enter_handler(struct widget *widget,
1333 struct input *input, float x, float y, void *data)
1334{
1335 struct frame_button *frame_button = data;
1336
1337 widget_schedule_redraw(frame_button->widget);
1338 frame_button->state = FRAME_BUTTON_OVER;
1339
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001340 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001341}
1342
1343static void
1344frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1345{
1346 struct frame_button *frame_button = data;
1347
1348 widget_schedule_redraw(frame_button->widget);
1349 frame_button->state = FRAME_BUTTON_DEFAULT;
1350}
1351
1352static void
1353frame_button_button_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
1355 uint32_t button, uint32_t state, void *data)
1356{
1357 struct frame_button *frame_button = data;
1358 struct window *window = widget->window;
1359
1360 if (button != BTN_LEFT)
1361 return;
1362
1363 switch (state) {
1364 case 1:
1365 frame_button->state = FRAME_BUTTON_ACTIVE;
1366 widget_schedule_redraw(frame_button->widget);
1367
1368 if (frame_button->type == FRAME_BUTTON_ICON)
1369 window_show_frame_menu(window, input, time);
1370 return;
1371 case 0:
1372 frame_button->state = FRAME_BUTTON_DEFAULT;
1373 widget_schedule_redraw(frame_button->widget);
1374 break;
1375 }
1376
1377 switch (frame_button->type) {
1378 case FRAME_BUTTON_CLOSE:
1379 if (window->close_handler)
1380 window->close_handler(window->parent,
1381 window->user_data);
1382 else
1383 display_exit(window->display);
1384 break;
1385 case FRAME_BUTTON_MINIMIZE:
1386 fprintf(stderr,"Minimize stub\n");
1387 break;
1388 case FRAME_BUTTON_MAXIMIZE:
1389 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1390 break;
1391 default:
1392 /* Unknown operation */
1393 break;
1394 }
1395}
1396
1397static void
1398frame_button_redraw_handler(struct widget *widget, void *data)
1399{
1400 struct frame_button *frame_button = data;
1401 cairo_t *cr;
1402 int width, height, x, y;
1403 struct window *window = widget->window;
1404
1405 x = widget->allocation.x;
1406 y = widget->allocation.y;
1407 width = widget->allocation.width;
1408 height = widget->allocation.height;
1409
1410 if (!width)
1411 return;
1412 if (!height)
1413 return;
1414 if (widget->opaque)
1415 return;
1416
1417 cr = cairo_create(window->cairo_surface);
1418
1419 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1420 cairo_set_line_width(cr, 1);
1421
1422 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1423 cairo_rectangle (cr, x, y, 25, 16);
1424
1425 cairo_stroke_preserve(cr);
1426
1427 switch (frame_button->state) {
1428 case FRAME_BUTTON_DEFAULT:
1429 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1430 break;
1431 case FRAME_BUTTON_OVER:
1432 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1433 break;
1434 case FRAME_BUTTON_ACTIVE:
1435 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1436 break;
1437 }
1438
1439 cairo_fill (cr);
1440
1441 x += 4;
1442 }
1443
1444 cairo_set_source_surface(cr, frame_button->icon, x, y);
1445 cairo_paint(cr);
1446
1447 cairo_destroy(cr);
1448}
1449
1450static struct widget *
1451frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1452 enum frame_button_align align, enum frame_button_decoration style)
1453{
1454 struct frame_button *frame_button;
1455 const char *icon = data;
1456
1457 frame_button = malloc (sizeof *frame_button);
1458 memset(frame_button, 0, sizeof *frame_button);
1459
1460 frame_button->icon = cairo_image_surface_create_from_png(icon);
1461 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1462 frame_button->frame = frame;
1463 frame_button->type = type;
1464 frame_button->align = align;
1465 frame_button->decoration = style;
1466
1467 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1468
1469 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1470 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1471 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1472 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1473 return frame_button->widget;
1474}
1475
1476static void
1477frame_button_destroy(struct frame_button *frame_button)
1478{
1479 widget_destroy(frame_button->widget);
1480 wl_list_remove(&frame_button->link);
1481 cairo_surface_destroy(frame_button->icon);
1482 free(frame_button);
1483
1484 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485}
1486
1487static void
1488frame_redraw_handler(struct widget *widget, void *data)
1489{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001492 struct theme *t = window->display->theme;
1493 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001495 if (window->type == TYPE_FULLSCREEN)
1496 return;
1497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001498 cr = cairo_create(window->cairo_surface);
1499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001501 flags |= THEME_FRAME_ACTIVE;
1502 theme_render_frame(t, cr, widget->allocation.width,
1503 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504
1505 cairo_destroy(cr);
1506}
1507
1508static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001511 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001512 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001514 location = theme_get_location(t, input->sx, input->sy,
1515 frame->widget->allocation.width,
1516 frame->widget->allocation.height);
1517
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001520 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001533 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 case THEME_LOCATION_EXTERIOR:
1536 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001537 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001538 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001539 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001540}
1541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542static void
1543frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001544{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001545 switch (index) {
1546 case 0: /* close */
1547 if (window->close_handler)
1548 window->close_handler(window->parent,
1549 window->user_data);
1550 else
1551 display_exit(window->display);
1552 break;
1553 case 1: /* fullscreen */
1554 /* we don't have a way to get out of fullscreen for now */
1555 window_set_fullscreen(window, 1);
1556 break;
1557 case 2: /* rotate */
1558 case 3: /* scale */
1559 break;
1560 }
1561}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001562
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001563void
1564window_show_frame_menu(struct window *window,
1565 struct input *input, uint32_t time)
1566{
1567 int32_t x, y;
1568
1569 static const char *entries[] = {
1570 "Close", "Fullscreen", "Rotate", "Scale"
1571 };
1572
1573 input_get_position(input, &x, &y);
1574 window_show_menu(window->display, input, time, window,
1575 x - 10, y - 10, frame_menu_func, entries, 4);
1576}
1577
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578static int
1579frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001580 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581{
1582 return frame_get_pointer_image_for_location(data, input);
1583}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001584
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585static int
1586frame_motion_handler(struct widget *widget,
1587 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001588 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589{
1590 return frame_get_pointer_image_for_location(data, input);
1591}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594frame_button_handler(struct widget *widget,
1595 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001596 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597
1598{
1599 struct frame *frame = data;
1600 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001601 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001603
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001604 location = theme_get_location(display->theme, input->sx, input->sy,
1605 frame->widget->allocation.width,
1606 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607
1608 if (window->display->shell && button == BTN_LEFT && state == 1) {
1609 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001610 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 if (!window->shell_surface)
1612 break;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001613 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001614 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001616 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001617 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001619 case THEME_LOCATION_RESIZING_TOP:
1620 case THEME_LOCATION_RESIZING_BOTTOM:
1621 case THEME_LOCATION_RESIZING_LEFT:
1622 case THEME_LOCATION_RESIZING_RIGHT:
1623 case THEME_LOCATION_RESIZING_TOP_LEFT:
1624 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1625 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1626 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001627 if (!window->shell_surface)
1628 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001629 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001630
1631 if (!display->dpy) {
1632 /* If we're using shm, allocate a big
1633 pool to create buffers out of while
1634 we resize. We should probably base
1635 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001636 window->pool =
1637 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001638 }
1639
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001640 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001641 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001642 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001643 break;
1644 }
1645 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001646 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 }
1648}
1649
1650struct widget *
1651frame_create(struct window *window, void *data)
1652{
1653 struct frame *frame;
1654
1655 frame = malloc(sizeof *frame);
1656 memset(frame, 0, sizeof *frame);
1657
1658 frame->widget = window_add_widget(window, frame);
1659 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001660
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001661 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1662 widget_set_resize_handler(frame->widget, frame_resize_handler);
1663 widget_set_enter_handler(frame->widget, frame_enter_handler);
1664 widget_set_motion_handler(frame->widget, frame_motion_handler);
1665 widget_set_button_handler(frame->widget, frame_button_handler);
1666
Martin Minarik1998b152012-05-10 02:04:35 +02001667 /* Create empty list for frame buttons */
1668 wl_list_init(&frame->buttons_list);
1669
1670 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1671 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1672
1673 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1674 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1677 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1680 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001682 window->frame = frame;
1683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685}
1686
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001687static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001688frame_destroy(struct frame *frame)
1689{
Martin Minarik1998b152012-05-10 02:04:35 +02001690 struct frame_button *button, *tmp;
1691
1692 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1693 frame_button_destroy(button);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 /* frame->child must be destroyed by the application */
1696 widget_destroy(frame->widget);
1697 free(frame);
1698}
1699
1700static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001701input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001702 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001704 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001705 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708 return;
1709
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001710 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001711 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001712 widget = old;
1713 if (input->grab)
1714 widget = input->grab;
1715 if (widget->leave_handler)
1716 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001718 }
1719
1720 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001721 widget = focus;
1722 if (input->grab)
1723 widget = input->grab;
1724 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001726 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001727 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001728
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001729 input_set_pointer_image(input, input->pointer_enter_serial,
1730 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001731 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001732}
1733
1734static void
Daniel Stone37816df2012-05-16 18:45:18 +01001735pointer_handle_motion(void *data, struct wl_pointer *pointer,
1736 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001737{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001738 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001739 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001740 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001741 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001742 float sx = wl_fixed_to_double(sx_w);
1743 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001745 input->sx = sx;
1746 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001748 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001749 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001750 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001751 }
1752
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 if (input->grab)
1754 widget = input->grab;
1755 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001756 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001757 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001758 cursor = widget->motion_handler(input->focus_widget,
1759 input, time, sx, sy,
1760 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001761
Daniel Stone37816df2012-05-16 18:45:18 +01001762 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001763}
1764
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001765void
1766input_grab(struct input *input, struct widget *widget, uint32_t button)
1767{
1768 input->grab = widget;
1769 input->grab_button = button;
1770}
1771
1772void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001774{
1775 struct widget *widget;
1776
1777 input->grab = NULL;
1778 if (input->pointer_focus) {
1779 widget = widget_find_widget(input->pointer_focus->widget,
1780 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001781 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001782 }
1783}
1784
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001785static void
Daniel Stone37816df2012-05-16 18:45:18 +01001786pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1787 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001788{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001789 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001790 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001791
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001792 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001793 if (input->focus_widget && input->grab == NULL && state)
1794 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001795
Neil Roberts6b28aad2012-01-23 19:11:18 +00001796 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001797 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001798 (*widget->button_handler)(widget,
1799 input, time,
1800 button, state,
1801 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001802
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001803 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001805}
1806
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001807static void
Daniel Stone37816df2012-05-16 18:45:18 +01001808pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001809 uint32_t time, uint32_t axis, int32_t value)
1810{
1811}
1812
1813static void
Daniel Stone37816df2012-05-16 18:45:18 +01001814keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1815 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001816{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001817 struct input *input = data;
1818 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001819 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001820 uint32_t code, num_syms;
1821 const xkb_keysym_t *syms;
1822 xkb_keysym_t sym;
1823 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001824
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001825 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001826 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001827 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001828 return;
1829
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001830 if (state)
1831 window->send_cursor_position = 1;
1832
Kristian Høgsberg70163132012-05-08 15:55:39 -04001833 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1834 xkb_state_update_key(d->xkb.state, code,
1835 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001836
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001837 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001838 XKB_STATE_DEPRESSED |
1839 XKB_STATE_LATCHED);
1840 input->modifiers = 0;
1841 if (mask & input->display->xkb.control_mask)
1842 input->modifiers |= MOD_CONTROL_MASK;
1843 if (mask & input->display->xkb.alt_mask)
1844 input->modifiers |= MOD_ALT_MASK;
1845 if (mask & input->display->xkb.shift_mask)
1846 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001847
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001848 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001849 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001850 if (state)
1851 window_set_maximized(window,
1852 window->type != TYPE_MAXIMIZED);
1853 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001854 if (num_syms == 1)
1855 sym = syms[0];
1856 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001857 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001858
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001859 (*window->key_handler)(window, input, time, key,
1860 sym, state, window->user_data);
1861 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001862}
1863
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001864static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001866{
1867 struct window *window = input->pointer_focus;
1868
1869 if (!window)
1870 return;
1871
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001872 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001873
Pekka Paalanene1207c72011-12-16 12:02:09 +02001874 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001875 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001876}
1877
1878static void
Daniel Stone37816df2012-05-16 18:45:18 +01001879pointer_handle_enter(void *data, struct wl_pointer *pointer,
1880 uint32_t serial, struct wl_surface *surface,
1881 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001882{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001883 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001884 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001885 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001886 float sx = wl_fixed_to_double(sx_w);
1887 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001888
Martin Minarik1998b152012-05-10 02:04:35 +02001889 if (!surface) {
1890 /* enter event for a window we've just destroyed */
1891 return;
1892 }
1893
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001894 input->display->serial = serial;
1895 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001896 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001897 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001898
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001899 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001900 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001901 window->pool = NULL;
1902 /* Schedule a redraw to free the pool */
1903 window_schedule_redraw(window);
1904 }
1905
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906 input->sx = sx;
1907 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001908
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001910 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001911}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001912
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001913static void
Daniel Stone37816df2012-05-16 18:45:18 +01001914pointer_handle_leave(void *data, struct wl_pointer *pointer,
1915 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001916{
1917 struct input *input = data;
1918
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001919 input->display->serial = serial;
1920 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001921}
1922
1923static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001924input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001925{
1926 struct window *window = input->keyboard_focus;
1927
1928 if (!window)
1929 return;
1930
1931 window->keyboard_device = NULL;
1932 if (window->keyboard_focus_handler)
1933 (*window->keyboard_focus_handler)(window, NULL,
1934 window->user_data);
1935
1936 input->keyboard_focus = NULL;
1937}
1938
1939static void
Daniel Stone37816df2012-05-16 18:45:18 +01001940keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1941 uint32_t serial, struct wl_surface *surface,
1942 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001943{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001944 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001945 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001946
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001947 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001948 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001949
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001950 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001951 window->keyboard_device = input;
1952 if (window->keyboard_focus_handler)
1953 (*window->keyboard_focus_handler)(window,
1954 window->keyboard_device,
1955 window->user_data);
1956}
1957
1958static void
Daniel Stone37816df2012-05-16 18:45:18 +01001959keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1960 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001961{
1962 struct input *input = data;
1963
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001964 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001965 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001966}
1967
Daniel Stone37816df2012-05-16 18:45:18 +01001968static const struct wl_pointer_listener pointer_listener = {
1969 pointer_handle_enter,
1970 pointer_handle_leave,
1971 pointer_handle_motion,
1972 pointer_handle_button,
1973 pointer_handle_axis,
1974};
1975
1976static const struct wl_keyboard_listener keyboard_listener = {
1977 keyboard_handle_enter,
1978 keyboard_handle_leave,
1979 keyboard_handle_key,
1980};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001981
1982static void
Daniel Stone37816df2012-05-16 18:45:18 +01001983seat_handle_capabilities(void *data, struct wl_seat *seat,
1984 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001985{
Daniel Stone37816df2012-05-16 18:45:18 +01001986 struct input *input = data;
1987
1988 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1989 input->pointer = wl_seat_get_pointer(seat);
1990 wl_pointer_set_user_data(input->pointer, input);
1991 wl_pointer_add_listener(input->pointer, &pointer_listener,
1992 input);
1993 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1994 wl_pointer_destroy(input->pointer);
1995 input->pointer = NULL;
1996 }
1997
1998 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1999 input->keyboard = wl_seat_get_keyboard(seat);
2000 wl_keyboard_set_user_data(input->keyboard, input);
2001 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2002 input);
2003 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2004 wl_keyboard_destroy(input->keyboard);
2005 input->keyboard = NULL;
2006 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002007}
2008
Daniel Stone37816df2012-05-16 18:45:18 +01002009static const struct wl_seat_listener seat_listener = {
2010 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002011};
2012
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002013void
2014input_get_position(struct input *input, int32_t *x, int32_t *y)
2015{
2016 *x = input->sx;
2017 *y = input->sy;
2018}
2019
Daniel Stone37816df2012-05-16 18:45:18 +01002020struct wl_seat *
2021input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002022{
Daniel Stone37816df2012-05-16 18:45:18 +01002023 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002024}
2025
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002026uint32_t
2027input_get_modifiers(struct input *input)
2028{
2029 return input->modifiers;
2030}
2031
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002032struct widget *
2033input_get_focus_widget(struct input *input)
2034{
2035 return input->focus_widget;
2036}
2037
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002038struct data_offer {
2039 struct wl_data_offer *offer;
2040 struct input *input;
2041 struct wl_array types;
2042 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002043
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002044 struct task io_task;
2045 int fd;
2046 data_func_t func;
2047 int32_t x, y;
2048 void *user_data;
2049};
2050
2051static void
2052data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2053{
2054 struct data_offer *offer = data;
2055 char **p;
2056
2057 p = wl_array_add(&offer->types, sizeof *p);
2058 *p = strdup(type);
2059}
2060
2061static const struct wl_data_offer_listener data_offer_listener = {
2062 data_offer_offer,
2063};
2064
2065static void
2066data_offer_destroy(struct data_offer *offer)
2067{
2068 char **p;
2069
2070 offer->refcount--;
2071 if (offer->refcount == 0) {
2072 wl_data_offer_destroy(offer->offer);
2073 for (p = offer->types.data; *p; p++)
2074 free(*p);
2075 wl_array_release(&offer->types);
2076 free(offer);
2077 }
2078}
2079
2080static void
2081data_device_data_offer(void *data,
2082 struct wl_data_device *data_device, uint32_t id)
2083{
2084 struct data_offer *offer;
2085
2086 offer = malloc(sizeof *offer);
2087
2088 wl_array_init(&offer->types);
2089 offer->refcount = 1;
2090 offer->input = data;
2091
2092 /* FIXME: Generate typesafe wrappers for this */
2093 offer->offer = (struct wl_data_offer *)
2094 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2095 id, &wl_data_offer_interface);
2096
2097 wl_data_offer_add_listener(offer->offer,
2098 &data_offer_listener, offer);
2099}
2100
2101static void
2102data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002103 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002104 wl_fixed_t x_w, wl_fixed_t y_w,
2105 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106{
2107 struct input *input = data;
2108 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002109 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002110 float x = wl_fixed_to_double(x_w);
2111 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002112 char **p;
2113
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002115 window = wl_surface_get_user_data(surface);
2116 input->pointer_focus = window;
2117
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002118 if (offer) {
2119 input->drag_offer = wl_data_offer_get_user_data(offer);
2120
2121 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2122 *p = NULL;
2123
2124 types_data = input->drag_offer->types.data;
2125 } else {
2126 input->drag_offer = NULL;
2127 types_data = NULL;
2128 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002129
2130 window = input->pointer_focus;
2131 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002132 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002133 window->user_data);
2134}
2135
2136static void
2137data_device_leave(void *data, struct wl_data_device *data_device)
2138{
2139 struct input *input = data;
2140
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002141 if (input->drag_offer) {
2142 data_offer_destroy(input->drag_offer);
2143 input->drag_offer = NULL;
2144 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002145}
2146
2147static void
2148data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002149 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002150{
2151 struct input *input = data;
2152 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002153 float x = wl_fixed_to_double(x_w);
2154 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002155 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002156
2157 input->sx = x;
2158 input->sy = y;
2159
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002160 if (input->drag_offer)
2161 types_data = input->drag_offer->types.data;
2162 else
2163 types_data = NULL;
2164
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002165 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002166 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002167 window->user_data);
2168}
2169
2170static void
2171data_device_drop(void *data, struct wl_data_device *data_device)
2172{
2173 struct input *input = data;
2174 struct window *window = input->pointer_focus;
2175
2176 if (window->drop_handler)
2177 window->drop_handler(window, input,
2178 input->sx, input->sy, window->user_data);
2179}
2180
2181static void
2182data_device_selection(void *data,
2183 struct wl_data_device *wl_data_device,
2184 struct wl_data_offer *offer)
2185{
2186 struct input *input = data;
2187 char **p;
2188
2189 if (input->selection_offer)
2190 data_offer_destroy(input->selection_offer);
2191
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002192 if (offer) {
2193 input->selection_offer = wl_data_offer_get_user_data(offer);
2194 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2195 *p = NULL;
2196 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197}
2198
2199static const struct wl_data_device_listener data_device_listener = {
2200 data_device_data_offer,
2201 data_device_enter,
2202 data_device_leave,
2203 data_device_motion,
2204 data_device_drop,
2205 data_device_selection
2206};
2207
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208void
2209input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2210{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002212 struct wl_cursor *cursor;
2213 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002215 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 return;
2217
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002218 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002219 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002220 return;
2221
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002222 image = cursor->images[0];
2223 buffer = wl_cursor_image_get_buffer(image);
2224 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 return;
2226
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002227 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002228 wl_pointer_attach(input->pointer, time, buffer,
2229 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230}
2231
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002232struct wl_data_device *
2233input_get_data_device(struct input *input)
2234{
2235 return input->data_device;
2236}
2237
2238void
2239input_set_selection(struct input *input,
2240 struct wl_data_source *source, uint32_t time)
2241{
2242 wl_data_device_set_selection(input->data_device, source, time);
2243}
2244
2245void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002246input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002247{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002248 wl_data_offer_accept(input->drag_offer->offer,
2249 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002250}
2251
2252static void
2253offer_io_func(struct task *task, uint32_t events)
2254{
2255 struct data_offer *offer =
2256 container_of(task, struct data_offer, io_task);
2257 unsigned int len;
2258 char buffer[4096];
2259
2260 len = read(offer->fd, buffer, sizeof buffer);
2261 offer->func(buffer, len,
2262 offer->x, offer->y, offer->user_data);
2263
2264 if (len == 0) {
2265 close(offer->fd);
2266 data_offer_destroy(offer);
2267 }
2268}
2269
2270static void
2271data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2272 data_func_t func, void *user_data)
2273{
2274 int p[2];
2275
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002276 if (pipe2(p, O_CLOEXEC) == -1)
2277 return;
2278
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002279 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2280 close(p[1]);
2281
2282 offer->io_task.run = offer_io_func;
2283 offer->fd = p[0];
2284 offer->func = func;
2285 offer->refcount++;
2286 offer->user_data = user_data;
2287
2288 display_watch_fd(offer->input->display,
2289 offer->fd, EPOLLIN, &offer->io_task);
2290}
2291
2292void
2293input_receive_drag_data(struct input *input, const char *mime_type,
2294 data_func_t func, void *data)
2295{
2296 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2297 input->drag_offer->x = input->sx;
2298 input->drag_offer->y = input->sy;
2299}
2300
2301int
2302input_receive_selection_data(struct input *input, const char *mime_type,
2303 data_func_t func, void *data)
2304{
2305 char **p;
2306
2307 if (input->selection_offer == NULL)
2308 return -1;
2309
2310 for (p = input->selection_offer->types.data; *p; p++)
2311 if (strcmp(mime_type, *p) == 0)
2312 break;
2313
2314 if (*p == NULL)
2315 return -1;
2316
2317 data_offer_receive_data(input->selection_offer,
2318 mime_type, func, data);
2319 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002320}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002321
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002322int
2323input_receive_selection_data_to_fd(struct input *input,
2324 const char *mime_type, int fd)
2325{
2326 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2327
2328 return 0;
2329}
2330
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002331void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002332window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002333{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002334 if (!window->shell_surface)
2335 return;
2336
Daniel Stone37816df2012-05-16 18:45:18 +01002337 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002338}
2339
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002340static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002341idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002342{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002343 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002344
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002345 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002346 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002347 widget_set_allocation(widget,
2348 window->pending_allocation.x,
2349 window->pending_allocation.y,
2350 window->pending_allocation.width,
2351 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002352
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002353 if (window->input_region) {
2354 wl_region_destroy(window->input_region);
2355 window->input_region = NULL;
2356 }
2357
2358 if (window->opaque_region) {
2359 wl_region_destroy(window->opaque_region);
2360 window->opaque_region = NULL;
2361 }
2362
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002363 if (widget->resize_handler)
2364 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002365 widget->allocation.width,
2366 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002367 widget->user_data);
2368
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002369 if (window->allocation.width != widget->allocation.width ||
2370 window->allocation.height != widget->allocation.height) {
2371 window->allocation = widget->allocation;
2372 window_schedule_redraw(window);
2373 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002374}
2375
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002376void
2377window_schedule_resize(struct window *window, int width, int height)
2378{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002379 window->pending_allocation.x = 0;
2380 window->pending_allocation.y = 0;
2381 window->pending_allocation.width = width;
2382 window->pending_allocation.height = height;
2383
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002384 window->resize_needed = 1;
2385 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002386}
2387
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002388void
2389widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2390{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002391 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002392}
2393
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002394static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002395handle_ping(void *data, struct wl_shell_surface *shell_surface,
2396 uint32_t serial)
2397{
2398 wl_shell_surface_pong(shell_surface, serial);
2399}
2400
2401static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002402handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002403 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002404{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002405 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002406
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002407 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002408 return;
2409
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002410 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002411 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002412}
2413
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002414static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002415menu_destroy(struct menu *menu)
2416{
2417 widget_destroy(menu->widget);
2418 window_destroy(menu->window);
2419 free(menu);
2420}
2421
2422static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002423handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2424{
2425 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002426 struct menu *menu = window->widget->user_data;
2427
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002428 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002429 * device. Or just use wl_callback. And this really needs to
2430 * be a window vfunc that the menu can set. And we need the
2431 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002432
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002433 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002434 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002435 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002436}
2437
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002438static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002439 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002440 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002441 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002442};
2443
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002444void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002445window_get_allocation(struct window *window,
2446 struct rectangle *allocation)
2447{
2448 *allocation = window->allocation;
2449}
2450
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002451static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002452widget_redraw(struct widget *widget)
2453{
2454 struct widget *child;
2455
2456 if (widget->redraw_handler)
2457 widget->redraw_handler(widget, widget->user_data);
2458 wl_list_for_each(child, &widget->child_list, link)
2459 widget_redraw(child);
2460}
2461
2462static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002463frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2464{
2465 struct window *window = data;
2466
2467 wl_callback_destroy(callback);
2468 window->redraw_scheduled = 0;
2469 if (window->redraw_needed)
2470 window_schedule_redraw(window);
2471}
2472
2473static const struct wl_callback_listener listener = {
2474 frame_callback
2475};
2476
2477static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002478idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002479{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002480 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002481 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002482
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002483 if (window->resize_needed)
2484 idle_resize(window);
2485
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002486 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002487 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002488 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002489 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002490 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002491
2492 callback = wl_surface_frame(window->surface);
2493 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002494}
2495
2496void
2497window_schedule_redraw(struct window *window)
2498{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002499 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002500 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002501 window->redraw_task.run = idle_redraw;
2502 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002503 window->redraw_scheduled = 1;
2504 }
2505}
2506
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002507void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002508window_set_custom(struct window *window)
2509{
2510 window->type = TYPE_CUSTOM;
2511}
2512
2513void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002514window_set_fullscreen(struct window *window, int fullscreen)
2515{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002516 if (!window->display->shell)
2517 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002518
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002519 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002520 return;
2521
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002522 if (fullscreen) {
2523 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002524 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002525 wl_shell_surface_set_fullscreen(window->shell_surface,
2526 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2527 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002528 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002529 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002530 wl_shell_surface_set_toplevel(window->shell_surface);
2531 window_schedule_resize(window,
2532 window->saved_allocation.width,
2533 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002534 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002535}
2536
2537void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002538window_set_maximized(struct window *window, int maximized)
2539{
2540 if (!window->display->shell)
2541 return;
2542
2543 if ((window->type == TYPE_MAXIMIZED) == maximized)
2544 return;
2545
2546 if (window->type == TYPE_TOPLEVEL) {
2547 window->saved_allocation = window->allocation;
2548 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2549 window->type = TYPE_MAXIMIZED;
2550 } else {
2551 wl_shell_surface_set_toplevel(window->shell_surface);
2552 window->type = TYPE_TOPLEVEL;
2553 window_schedule_resize(window,
2554 window->saved_allocation.width,
2555 window->saved_allocation.height);
2556 }
2557}
2558
2559void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002560window_set_user_data(struct window *window, void *data)
2561{
2562 window->user_data = data;
2563}
2564
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002565void *
2566window_get_user_data(struct window *window)
2567{
2568 return window->user_data;
2569}
2570
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002571void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002572window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002573 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002574{
2575 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002576}
2577
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002578void
2579window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002580 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002581{
2582 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002583}
2584
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002585void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002586window_set_data_handler(struct window *window, window_data_handler_t handler)
2587{
2588 window->data_handler = handler;
2589}
2590
2591void
2592window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2593{
2594 window->drop_handler = handler;
2595}
2596
2597void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002598window_set_close_handler(struct window *window,
2599 window_close_handler_t handler)
2600{
2601 window->close_handler = handler;
2602}
2603
2604void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002605window_set_title(struct window *window, const char *title)
2606{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002607 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002608 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002609 if (window->shell_surface)
2610 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002611}
2612
2613const char *
2614window_get_title(struct window *window)
2615{
2616 return window->title;
2617}
2618
2619void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002620window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2621{
2622 struct text_cursor_position *text_cursor_position =
2623 window->display->text_cursor_position;
2624
2625 if (!window->send_cursor_position || !text_cursor_position)
2626 return;
2627
2628 text_cursor_position_notify(text_cursor_position,
2629 window->surface, x, y);
2630
2631 window->send_cursor_position = 0;
2632}
2633
2634void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002635window_damage(struct window *window, int32_t x, int32_t y,
2636 int32_t width, int32_t height)
2637{
2638 wl_surface_damage(window->surface, x, y, width, height);
2639}
2640
Casey Dahlin9074db52012-04-19 22:50:09 -04002641static void
2642surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002643 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002644{
Rob Bradford7507b572012-05-15 17:55:34 +01002645 struct window *window = data;
2646 struct output *output;
2647 struct output *output_found = NULL;
2648 struct window_output *window_output;
2649
2650 wl_list_for_each(output, &window->display->output_list, link) {
2651 if (output->output == wl_output) {
2652 output_found = output;
2653 break;
2654 }
2655 }
2656
2657 if (!output_found)
2658 return;
2659
2660 window_output = malloc (sizeof *window_output);
2661 window_output->output = output_found;
2662
2663 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002664}
2665
2666static void
2667surface_leave(void *data,
2668 struct wl_surface *wl_surface, struct wl_output *output)
2669{
Rob Bradford7507b572012-05-15 17:55:34 +01002670 struct window *window = data;
2671 struct window_output *window_output;
2672 struct window_output *window_output_found = NULL;
2673
2674 wl_list_for_each(window_output, &window->window_output_list, link) {
2675 if (window_output->output->output == output) {
2676 window_output_found = window_output;
2677 break;
2678 }
2679 }
2680
2681 if (window_output_found) {
2682 wl_list_remove(&window_output_found->link);
2683 free(window_output_found);
2684 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002685}
2686
2687static const struct wl_surface_listener surface_listener = {
2688 surface_enter,
2689 surface_leave
2690};
2691
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002692static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002693window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002694{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002695 struct window *window;
2696
2697 window = malloc(sizeof *window);
2698 if (window == NULL)
2699 return NULL;
2700
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002701 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002702 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002703 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002704 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002705 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002706 if (display->shell) {
2707 window->shell_surface =
2708 wl_shell_get_shell_surface(display->shell,
2709 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002710 if (window->title)
2711 wl_shell_surface_set_title(window->shell_surface,
2712 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002713 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002714 window->allocation.x = 0;
2715 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002716 window->allocation.width = 0;
2717 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002718 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002719 window->transparent = 1;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002720 window->send_cursor_position = 0;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002721 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002722 window->input_region = NULL;
2723 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002724
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002725 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002726#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002727 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002728#else
2729 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2730#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002731 else
2732 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002733
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002734 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002735 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002736 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002737
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002738 if (window->shell_surface) {
2739 wl_shell_surface_set_user_data(window->shell_surface, window);
2740 wl_shell_surface_add_listener(window->shell_surface,
2741 &shell_surface_listener, window);
2742 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002743
Rob Bradford7507b572012-05-15 17:55:34 +01002744 wl_list_init (&window->window_output_list);
2745
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002746 return window;
2747}
2748
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002749struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002750window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002751{
2752 struct window *window;
2753
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002754 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002755 if (!window)
2756 return NULL;
2757
2758 return window;
2759}
2760
2761struct window *
2762window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002763 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002764{
2765 struct window *window;
2766
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002767 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002768 if (!window)
2769 return NULL;
2770
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002771 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002772 window->x = x;
2773 window->y = y;
2774
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002775 if (display->shell)
2776 wl_shell_surface_set_transient(window->shell_surface,
2777 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002778 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002779
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002780 return window;
2781}
2782
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002783static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002784menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002785{
2786 int next;
2787
2788 next = (sy - 8) / 20;
2789 if (menu->current != next) {
2790 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002791 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002792 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002793}
2794
2795static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002796menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002797 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002798 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002799{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002800 struct menu *menu = data;
2801
2802 if (widget == menu->widget)
2803 menu_set_item(data, y);
2804
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002805 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002806}
2807
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002808static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002809menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002810 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002811{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002812 struct menu *menu = data;
2813
2814 if (widget == menu->widget)
2815 menu_set_item(data, y);
2816
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002817 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002818}
2819
2820static void
2821menu_leave_handler(struct widget *widget, struct input *input, void *data)
2822{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002823 struct menu *menu = data;
2824
2825 if (widget == menu->widget)
2826 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002827}
2828
2829static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002830menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002831 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002832 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002833
2834{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002835 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002836
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002837 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002838 /* Either relase after press-drag-release or
2839 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002840 menu->func(menu->window->parent,
2841 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002842 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002843 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002844 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002845}
2846
2847static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002848menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002849{
2850 cairo_t *cr;
2851 const int32_t r = 3, margin = 3;
2852 struct menu *menu = data;
2853 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002854 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002855
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002856 cr = cairo_create(window->cairo_surface);
2857 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2858 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2859 cairo_paint(cr);
2860
2861 width = window->allocation.width;
2862 height = window->allocation.height;
2863 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002864
2865 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002866 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2867 cairo_fill(cr);
2868
2869 for (i = 0; i < menu->count; i++) {
2870 if (i == menu->current) {
2871 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2872 cairo_rectangle(cr, margin, i * 20 + margin,
2873 width - 2 * margin, 20);
2874 cairo_fill(cr);
2875 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2876 cairo_move_to(cr, 10, i * 20 + 16);
2877 cairo_show_text(cr, menu->entries[i]);
2878 } else {
2879 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2880 cairo_move_to(cr, 10, i * 20 + 16);
2881 cairo_show_text(cr, menu->entries[i]);
2882 }
2883 }
2884
2885 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002886}
2887
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002888void
2889window_show_menu(struct display *display,
2890 struct input *input, uint32_t time, struct window *parent,
2891 int32_t x, int32_t y,
2892 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002893{
2894 struct window *window;
2895 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002896 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002897
2898 menu = malloc(sizeof *menu);
2899 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002900 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002901
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002902 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002903 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002904 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002905
2906 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002907 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002908 menu->entries = entries;
2909 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002910 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002912 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002913 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002914 window->type = TYPE_MENU;
2915 window->x = x;
2916 window->y = y;
2917
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002918 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002919 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002920 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921 window->parent->shell_surface,
2922 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002923
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002924 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002925 widget_set_enter_handler(menu->widget, menu_enter_handler);
2926 widget_set_leave_handler(menu->widget, menu_leave_handler);
2927 widget_set_motion_handler(menu->widget, menu_motion_handler);
2928 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002929
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002930 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002931 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002932}
2933
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002934void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002935window_set_buffer_type(struct window *window, enum window_buffer_type type)
2936{
2937 window->buffer_type = type;
2938}
2939
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002940
2941static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002942display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002943 struct wl_output *wl_output,
2944 int x, int y,
2945 int physical_width,
2946 int physical_height,
2947 int subpixel,
2948 const char *make,
2949 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002950{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002951 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002952
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002953 output->allocation.x = x;
2954 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002955}
2956
2957static void
2958display_handle_mode(void *data,
2959 struct wl_output *wl_output,
2960 uint32_t flags,
2961 int width,
2962 int height,
2963 int refresh)
2964{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002965 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002966 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002967
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002968 if (flags & WL_OUTPUT_MODE_CURRENT) {
2969 output->allocation.width = width;
2970 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002971 if (display->output_configure_handler)
2972 (*display->output_configure_handler)(
2973 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002974 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002975}
2976
2977static const struct wl_output_listener output_listener = {
2978 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002979 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002980};
2981
2982static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002983display_add_output(struct display *d, uint32_t id)
2984{
2985 struct output *output;
2986
2987 output = malloc(sizeof *output);
2988 if (output == NULL)
2989 return;
2990
2991 memset(output, 0, sizeof *output);
2992 output->display = d;
2993 output->output =
2994 wl_display_bind(d->display, id, &wl_output_interface);
2995 wl_list_insert(d->output_list.prev, &output->link);
2996
2997 wl_output_add_listener(output->output, &output_listener, output);
2998}
2999
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003000static void
3001output_destroy(struct output *output)
3002{
3003 if (output->destroy_handler)
3004 (*output->destroy_handler)(output, output->user_data);
3005
3006 wl_output_destroy(output->output);
3007 wl_list_remove(&output->link);
3008 free(output);
3009}
3010
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003011void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003012display_set_output_configure_handler(struct display *display,
3013 display_output_handler_t handler)
3014{
3015 struct output *output;
3016
3017 display->output_configure_handler = handler;
3018 if (!handler)
3019 return;
3020
3021 wl_list_for_each(output, &display->output_list, link)
3022 (*display->output_configure_handler)(output,
3023 display->user_data);
3024}
3025
3026void
3027output_set_user_data(struct output *output, void *data)
3028{
3029 output->user_data = data;
3030}
3031
3032void *
3033output_get_user_data(struct output *output)
3034{
3035 return output->user_data;
3036}
3037
3038void
3039output_set_destroy_handler(struct output *output,
3040 display_output_handler_t handler)
3041{
3042 output->destroy_handler = handler;
3043 /* FIXME: implement this, once we have way to remove outputs */
3044}
3045
3046void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003047output_get_allocation(struct output *output, struct rectangle *allocation)
3048{
3049 *allocation = output->allocation;
3050}
3051
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003052struct wl_output *
3053output_get_wl_output(struct output *output)
3054{
3055 return output->output;
3056}
3057
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003058static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003059display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003060{
3061 struct input *input;
3062
3063 input = malloc(sizeof *input);
3064 if (input == NULL)
3065 return;
3066
3067 memset(input, 0, sizeof *input);
3068 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003069 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003070 input->pointer_focus = NULL;
3071 input->keyboard_focus = NULL;
3072 wl_list_insert(d->input_list.prev, &input->link);
3073
Daniel Stone37816df2012-05-16 18:45:18 +01003074 wl_seat_add_listener(input->seat, &seat_listener, input);
3075 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003076
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003077 input->data_device =
3078 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003079 input->seat);
3080 wl_data_device_add_listener(input->data_device, &data_device_listener,
3081 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003082}
3083
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003084static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003085input_destroy(struct input *input)
3086{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003087 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003088 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003089
3090 if (input->drag_offer)
3091 data_offer_destroy(input->drag_offer);
3092
3093 if (input->selection_offer)
3094 data_offer_destroy(input->selection_offer);
3095
3096 wl_data_device_destroy(input->data_device);
3097 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003098 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003099 free(input);
3100}
3101
3102static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003103display_handle_global(struct wl_display *display, uint32_t id,
3104 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003105{
3106 struct display *d = data;
3107
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003108 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003109 d->compositor =
3110 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003111 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003112 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003113 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003114 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003115 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003116 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003117 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003118 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003119 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3120 d->data_device_manager =
3121 wl_display_bind(display, id,
3122 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003123 } else if (strcmp(interface, "text_cursor_position") == 0) {
3124 d->text_cursor_position =
3125 wl_display_bind(display, id,
3126 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003127 }
3128}
3129
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003130static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003131init_xkb(struct display *d)
3132{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003133 d->xkb.names.rules = "evdev";
3134 d->xkb.names.model = "pc105";
3135 d->xkb.names.layout = (char *) option_xkb_layout;
3136 d->xkb.names.variant = (char *) option_xkb_variant;
3137 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003138
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003139 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003140 if (!d->xkb.context) {
3141 fprintf(stderr, "Failed to create XKB context\n");
3142 exit(1);
3143 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003144
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003145 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003146 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003147 fprintf(stderr, "Failed to compile keymap\n");
3148 exit(1);
3149 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003150
3151 d->xkb.state = xkb_state_new(d->xkb.keymap);
3152 if (!d->xkb.state) {
3153 fprintf(stderr, "Failed to create XKB state\n");
3154 exit(1);
3155 }
3156
3157 d->xkb.control_mask =
3158 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3159 d->xkb.alt_mask =
3160 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3161 d->xkb.shift_mask =
3162 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3163
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003164}
3165
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003166static void
3167fini_xkb(struct display *display)
3168{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003169 xkb_state_unref(display->xkb.state);
3170 xkb_map_unref(display->xkb.keymap);
3171 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003172}
3173
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003174#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003175static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003176init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003177{
3178 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003179 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003180
Rob Clark6396ed32012-03-11 19:48:41 -05003181#ifdef USE_CAIRO_GLESV2
3182# define GL_BIT EGL_OPENGL_ES2_BIT
3183#else
3184# define GL_BIT EGL_OPENGL_BIT
3185#endif
3186
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003187 static const EGLint argb_cfg_attribs[] = {
3188 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003189 EGL_RED_SIZE, 1,
3190 EGL_GREEN_SIZE, 1,
3191 EGL_BLUE_SIZE, 1,
3192 EGL_ALPHA_SIZE, 1,
3193 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003194 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003195 EGL_NONE
3196 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003197
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003198#ifdef USE_CAIRO_GLESV2
3199 static const EGLint context_attribs[] = {
3200 EGL_CONTEXT_CLIENT_VERSION, 2,
3201 EGL_NONE
3202 };
3203 EGLint api = EGL_OPENGL_ES_API;
3204#else
3205 EGLint *context_attribs = NULL;
3206 EGLint api = EGL_OPENGL_API;
3207#endif
3208
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003209 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003210 if (!eglInitialize(d->dpy, &major, &minor)) {
3211 fprintf(stderr, "failed to initialize display\n");
3212 return -1;
3213 }
3214
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003215 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003216 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3217 return -1;
3218 }
3219
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003220 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3221 &d->argb_config, 1, &n) || n != 1) {
3222 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003223 return -1;
3224 }
3225
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003226 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003227 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003228 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003229 fprintf(stderr, "failed to create context\n");
3230 return -1;
3231 }
3232
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003233 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003234 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003235 return -1;
3236 }
3237
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003238#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003239 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3240 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3241 fprintf(stderr, "failed to get cairo egl argb device\n");
3242 return -1;
3243 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003244#endif
3245
3246 return 0;
3247}
3248
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003249static void
3250fini_egl(struct display *display)
3251{
3252#ifdef HAVE_CAIRO_EGL
3253 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003254#endif
3255
3256 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3257 EGL_NO_CONTEXT);
3258
3259 eglTerminate(display->dpy);
3260 eglReleaseThread();
3261}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003262#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003263
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003264static int
3265event_mask_update(uint32_t mask, void *data)
3266{
3267 struct display *d = data;
3268
3269 d->mask = mask;
3270
3271 return 0;
3272}
3273
3274static void
3275handle_display_data(struct task *task, uint32_t events)
3276{
3277 struct display *display =
3278 container_of(task, struct display, display_task);
3279
3280 wl_display_iterate(display->display, display->mask);
3281}
3282
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003283struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003284display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003285{
3286 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003287
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003288 argc = parse_options(xkb_options,
3289 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003290
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003291 d = malloc(sizeof *d);
3292 if (d == NULL)
3293 return NULL;
3294
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003295 memset(d, 0, sizeof *d);
3296
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003297 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003298 if (d->display == NULL) {
3299 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003300 return NULL;
3301 }
3302
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003303 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003304 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3305 d->display_task.run = handle_display_data;
3306 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3307
3308 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003309 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003310 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003311
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003312 /* Set up listener so we'll catch all events. */
3313 wl_display_add_global_listener(d->display,
3314 display_handle_global, d);
3315
3316 /* Process connection events. */
3317 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003318#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003319 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003320 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003321#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003322
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003323 d->image_target_texture_2d =
3324 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3325 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3326 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3327
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003328 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003329
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003330 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003331
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003332 wl_list_init(&d->window_list);
3333
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003334 init_xkb(d);
3335
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003336 return d;
3337}
3338
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003339static void
3340display_destroy_outputs(struct display *display)
3341{
3342 struct output *tmp;
3343 struct output *output;
3344
3345 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3346 output_destroy(output);
3347}
3348
Pekka Paalanene1207c72011-12-16 12:02:09 +02003349static void
3350display_destroy_inputs(struct display *display)
3351{
3352 struct input *tmp;
3353 struct input *input;
3354
3355 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3356 input_destroy(input);
3357}
3358
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003359void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003360display_destroy(struct display *display)
3361{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003362 if (!wl_list_empty(&display->window_list))
3363 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3364
3365 if (!wl_list_empty(&display->deferred_list))
3366 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3367
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003368 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003369 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003370
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003371 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003372
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003373 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003374 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003375
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003376#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003377 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003378#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003379
Pekka Paalanenc2052982011-12-16 11:41:32 +02003380 if (display->shell)
3381 wl_shell_destroy(display->shell);
3382
3383 if (display->shm)
3384 wl_shm_destroy(display->shm);
3385
3386 if (display->data_device_manager)
3387 wl_data_device_manager_destroy(display->data_device_manager);
3388
3389 wl_compositor_destroy(display->compositor);
3390
3391 close(display->epoll_fd);
3392
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003393 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003394 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003395 free(display);
3396}
3397
3398void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003399display_set_user_data(struct display *display, void *data)
3400{
3401 display->user_data = data;
3402}
3403
3404void *
3405display_get_user_data(struct display *display)
3406{
3407 return display->user_data;
3408}
3409
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003410struct wl_display *
3411display_get_display(struct display *display)
3412{
3413 return display->display;
3414}
3415
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003416struct output *
3417display_get_output(struct display *display)
3418{
3419 return container_of(display->output_list.next, struct output, link);
3420}
3421
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003422struct wl_compositor *
3423display_get_compositor(struct display *display)
3424{
3425 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003426}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003427
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003428uint32_t
3429display_get_serial(struct display *display)
3430{
3431 return display->serial;
3432}
3433
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003434EGLDisplay
3435display_get_egl_display(struct display *d)
3436{
3437 return d->dpy;
3438}
3439
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003440struct wl_data_source *
3441display_create_data_source(struct display *display)
3442{
3443 return wl_data_device_manager_create_data_source(display->data_device_manager);
3444}
3445
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003446EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003447display_get_argb_egl_config(struct display *d)
3448{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003449 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003450}
3451
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003452struct wl_shell *
3453display_get_shell(struct display *display)
3454{
3455 return display->shell;
3456}
3457
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003458int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003459display_acquire_window_surface(struct display *display,
3460 struct window *window,
3461 EGLContext ctx)
3462{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003463#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003464 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003465 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003466
3467 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003468 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003469 device = cairo_surface_get_device(window->cairo_surface);
3470 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003471 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003472
Benjamin Franzke0c991632011-09-27 21:57:31 +02003473 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003474 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003475 ctx = display->argb_ctx;
3476 else
3477 assert(0);
3478 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003479
3480 data = cairo_surface_get_user_data(window->cairo_surface,
3481 &surface_data_key);
3482
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003483 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003484 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003485 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3486 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003487
3488 return 0;
3489#else
3490 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003491#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003492}
3493
3494void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003495display_release_window_surface(struct display *display,
3496 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003497{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003498#ifdef HAVE_CAIRO_EGL
3499 cairo_device_t *device;
3500
3501 device = cairo_surface_get_device(window->cairo_surface);
3502 if (!device)
3503 return;
3504
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003505 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003506 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003507 cairo_device_release(device);
3508#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003509}
3510
3511void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003512display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003513{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003514 wl_list_insert(&display->deferred_list, &task->link);
3515}
3516
3517void
3518display_watch_fd(struct display *display,
3519 int fd, uint32_t events, struct task *task)
3520{
3521 struct epoll_event ep;
3522
3523 ep.events = events;
3524 ep.data.ptr = task;
3525 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3526}
3527
3528void
3529display_run(struct display *display)
3530{
3531 struct task *task;
3532 struct epoll_event ep[16];
3533 int i, count;
3534
Pekka Paalanen826d7952011-12-15 10:14:07 +02003535 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003536 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003537 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003538
3539 while (!wl_list_empty(&display->deferred_list)) {
3540 task = container_of(display->deferred_list.next,
3541 struct task, link);
3542 wl_list_remove(&task->link);
3543 task->run(task, 0);
3544 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003545
3546 if (!display->running)
3547 break;
3548
3549 wl_display_flush(display->display);
3550
3551 count = epoll_wait(display->epoll_fd,
3552 ep, ARRAY_LENGTH(ep), -1);
3553 for (i = 0; i < count; i++) {
3554 task = ep[i].data.ptr;
3555 task->run(task, ep[i].events);
3556 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003557 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003558}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003559
3560void
3561display_exit(struct display *display)
3562{
3563 display->running = 0;
3564}