blob: 994c8a8f5bf47f25770055c2bd84da6bd6aedca8 [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øgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040033#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040034#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg640609a2010-08-09 22:11:47 -040035#include <xf86drm.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040036
37#define EGL_EGLEXT_PROTOTYPES 1
38#define GL_GLEXT_PROTOTYPES 1
39#include <GL/gl.h>
40#include <EGL/egl.h>
41#include <EGL/eglext.h>
42#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040044#include <X11/extensions/XKBcommon.h>
45
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050047#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050049#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040050#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050053
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050054struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050055 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050056 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040057 struct wl_shell *shell;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -040058 struct wl_drm *drm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 struct wl_output *output;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040060 struct wl_drag *drag;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040062 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040063 EGLDisplay dpy;
64 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010065 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040067 GMainLoop *loop;
68 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040069 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040070 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040071 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040072 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040073 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040074 cairo_surface_t **pointer_surfaces;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050075};
76
77struct window {
78 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050080 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040081 struct rectangle allocation, saved_allocation, pending_allocation;
82 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040083 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050084 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050086 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040087 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct input *grab_device;
89 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050090 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050091
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040092 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050093 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050094
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050095 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040096 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050097 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040098 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050099 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400100 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 window_frame_handler_t frame_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400102 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400103
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500104 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400105 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500106};
107
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400108struct input {
109 struct display *display;
110 struct wl_input_device *input_device;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400111 struct wl_drag *drag;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct window *pointer_focus;
113 struct window *keyboard_focus;
114 uint32_t modifiers;
115 int32_t x, y, sx, sy;
116 struct wl_list link;
117};
118
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500119static void
120rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
121{
122 cairo_move_to(cr, x0, y0 + radius);
123 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
124 cairo_line_to(cr, x1 - radius, y0);
125 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
126 cairo_line_to(cr, x1, y1 - radius);
127 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
128 cairo_line_to(cr, x0 + radius, y1);
129 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
130 cairo_close_path(cr);
131}
132
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400133static int
134texture_from_png(const char *filename, int width, int height)
135{
136 GdkPixbuf *pixbuf;
137 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400138 int stride, i;
139 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400140
141 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
142 width, height,
143 FALSE, &error);
144 if (error != NULL)
145 return -1;
146
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400147 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
148 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
149 gdk_pixbuf_unref(pixbuf);
150 return -1;
151 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400152
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400153
154 stride = gdk_pixbuf_get_rowstride(pixbuf);
155 pixels = gdk_pixbuf_get_pixels(pixbuf);
156
157 for (i = 0; i < height; i++) {
158 p = pixels + i * stride;
159 end = p + width * 4;
160 while (p < end) {
161 unsigned int t;
162
163#define MULT(d,c,a,t) \
164 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
165
166 MULT(p[0], p[0], p[3], t);
167 MULT(p[1], p[1], p[3], t);
168 MULT(p[2], p[2], p[3], t);
169 p += 4;
170
171 }
172 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400173
174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400175 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400176
177 gdk_pixbuf_unref(pixbuf);
178
179 return 0;
180}
181
182static const struct {
183 const char *filename;
184 int hotspot_x, hotspot_y;
185} pointer_images[] = {
186 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
187 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
188 { DATADIR "/wayland/bottom_side.png", 16, 20 },
189 { DATADIR "/wayland/grabbing.png", 20, 17 },
190 { DATADIR "/wayland/left_ptr.png", 10, 5 },
191 { DATADIR "/wayland/left_side.png", 10, 20 },
192 { DATADIR "/wayland/right_side.png", 30, 19 },
193 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
194 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
195 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400196 { DATADIR "/wayland/xterm.png", 15, 15 },
197 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400198};
199
200static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400201create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400202{
203 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400204 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400205 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400206
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400207 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400208 display->pointer_surfaces =
209 malloc(count * sizeof *display->pointer_surfaces);
210 rect.width = width;
211 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400212 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400213 display->pointer_surfaces[i] =
214 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400215 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400216 }
217
218}
219
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400220static const cairo_user_data_key_t surface_data_key;
221struct surface_data {
222 EGLImageKHR image;
223 GLuint texture;
224 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400225 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400226};
227
228static void
229surface_data_destroy(void *p)
230{
231 struct surface_data *data = p;
232
233 glDeleteTextures(1, &data->texture);
234 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400235 if (data->buffer)
236 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400237}
238
239cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240display_create_surface(struct display *display,
241 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400242{
243 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400244 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400245 cairo_surface_t *surface;
246
247 EGLint image_attribs[] = {
248 EGL_WIDTH, 0,
249 EGL_HEIGHT, 0,
250 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
251 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
252 EGL_NONE
253 };
254
255 data = malloc(sizeof *data);
256 image_attribs[1] = rectangle->width;
257 image_attribs[3] = rectangle->height;
258 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
259 glGenTextures(1, &data->texture);
260 data->dpy = dpy;
261 glBindTexture(GL_TEXTURE_2D, data->texture);
262 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400263 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400264
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400266 CAIRO_CONTENT_COLOR_ALPHA,
267 data->texture,
268 rectangle->width,
269 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400270
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400271 cairo_surface_set_user_data (surface, &surface_data_key,
272 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400273
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400274 return surface;
275}
276
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400277struct wl_buffer *
278display_get_buffer_for_surface(struct display *display,
279 cairo_surface_t *surface)
280{
281 struct surface_data *data;
282 struct wl_visual *visual;
283 struct wl_buffer *buffer;
284 EGLint name, stride;
285 int width, height;
286
287 data = cairo_surface_get_user_data (surface, &surface_data_key);
288 if (data->buffer)
289 return data->buffer;
290
291 width = cairo_gl_surface_get_width (surface);
292 height = cairo_gl_surface_get_height (surface);
293
294 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
295
296 visual = wl_display_get_premultiplied_argb_visual(display->display);
297 buffer = wl_drm_create_buffer(display->drm,
298 name, width, height, stride, visual);
299 data->buffer = buffer;
300
301 return buffer;
302}
303
304cairo_surface_t *
305display_get_pointer_surface(struct display *display, int pointer,
306 int *width, int *height,
307 int *hotspot_x, int *hotspot_y)
308{
309 cairo_surface_t *surface;
310
311 surface = display->pointer_surfaces[pointer];
312 *width = cairo_gl_surface_get_width(surface);
313 *height = cairo_gl_surface_get_height(surface);
314 *hotspot_x = pointer_images[pointer].hotspot_x;
315 *hotspot_y = pointer_images[pointer].hotspot_y;
316
317 return cairo_surface_reference(surface);
318}
319
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500320static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500321window_attach_surface(struct window *window)
322{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400323 struct display *display = window->display;
324 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500325
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500326 if (window->pending_surface != NULL)
327 return;
328
Kristian Høgsberg09531622010-06-14 23:22:15 -0400329 window->pending_surface = window->cairo_surface;
330 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500331
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400332 buffer = display_get_buffer_for_surface(display,
333 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400334 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500335
336 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400337 window->allocation.x,
338 window->allocation.y,
339 window->allocation.width,
340 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400341
342 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500343}
344
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500345void
346window_commit(struct window *window, uint32_t key)
347{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400348 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500349 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400350 } else {
351 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500352 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500353}
354
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500355static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500356window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500357{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500358 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500359 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400360 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400361 cairo_surface_t *frame;
362 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500363
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500364 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400365 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400366 width = window->allocation.width;
367 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500368
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500369 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500370 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500371 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
372
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500373 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500374
Kristian Høgsberg09531622010-06-14 23:22:15 -0400375 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400376 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400377 cairo_paint(cr);
378
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400379 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400380 tile_mask(cr, window->display->shadow,
381 shadow_dx, shadow_dy, width, height,
382 window->margin + 10 - shadow_dx,
383 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500384
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400385 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400386 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400387 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400388 frame = window->display->inactive_frame;
389
390 tile_source(cr, frame, 0, 0, width, height,
391 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500392
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500393 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
394 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500395 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400396 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500397 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
398 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
399 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400400 if (window->keyboard_device)
401 cairo_set_source_rgb(cr, 0, 0, 0);
402 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400403 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400404 cairo_show_text(cr, window->title);
405
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500406 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400407
408 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500409}
410
411static void
412window_draw_fullscreen(struct window *window)
413{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500414 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400415 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500416}
417
418void
419window_draw(struct window *window)
420{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500421 if (window->cairo_surface != NULL)
422 cairo_surface_destroy(window->cairo_surface);
423
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400424 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500425 window_draw_fullscreen(window);
426 else
427 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500428}
429
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400430cairo_surface_t *
431window_get_surface(struct window *window)
432{
433 return window->cairo_surface;
434}
435
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436enum window_location {
437 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400438 WINDOW_RESIZING_TOP = 1,
439 WINDOW_RESIZING_BOTTOM = 2,
440 WINDOW_RESIZING_LEFT = 4,
441 WINDOW_RESIZING_TOP_LEFT = 5,
442 WINDOW_RESIZING_BOTTOM_LEFT = 6,
443 WINDOW_RESIZING_RIGHT = 8,
444 WINDOW_RESIZING_TOP_RIGHT = 9,
445 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
446 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400447 WINDOW_EXTERIOR = 16,
448 WINDOW_TITLEBAR = 17,
449 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400450};
451
452static int
453get_pointer_location(struct window *window, int32_t x, int32_t y)
454{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400455 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400456 const int grip_size = 8;
457
458 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400459 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400460 else if (window->margin <= x && x < window->margin + grip_size)
461 hlocation = WINDOW_RESIZING_LEFT;
462 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400463 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464 else if (x < window->allocation.width - window->margin)
465 hlocation = WINDOW_RESIZING_RIGHT;
466 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400467 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400468
469 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400470 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400471 else if (window->margin <= y && y < window->margin + grip_size)
472 vlocation = WINDOW_RESIZING_TOP;
473 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400474 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400475 else if (y < window->allocation.height - window->margin)
476 vlocation = WINDOW_RESIZING_BOTTOM;
477 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400478 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400479
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400480 location = vlocation | hlocation;
481 if (location & WINDOW_EXTERIOR)
482 location = WINDOW_EXTERIOR;
483 if (location == WINDOW_INTERIOR && y < window->margin + 50)
484 location = WINDOW_TITLEBAR;
485 else if (location == WINDOW_INTERIOR)
486 location = WINDOW_CLIENT_AREA;
487
488 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400489}
490
491static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400492set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400493{
494 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400495 struct wl_buffer *buffer;
496 cairo_surface_t *surface;
497 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498
499 location = get_pointer_location(input->pointer_focus,
500 input->sx, input->sy);
501 switch (location) {
502 case WINDOW_RESIZING_TOP:
503 pointer = POINTER_TOP;
504 break;
505 case WINDOW_RESIZING_BOTTOM:
506 pointer = POINTER_BOTTOM;
507 break;
508 case WINDOW_RESIZING_LEFT:
509 pointer = POINTER_LEFT;
510 break;
511 case WINDOW_RESIZING_RIGHT:
512 pointer = POINTER_RIGHT;
513 break;
514 case WINDOW_RESIZING_TOP_LEFT:
515 pointer = POINTER_TOP_LEFT;
516 break;
517 case WINDOW_RESIZING_TOP_RIGHT:
518 pointer = POINTER_TOP_RIGHT;
519 break;
520 case WINDOW_RESIZING_BOTTOM_LEFT:
521 pointer = POINTER_BOTTOM_LEFT;
522 break;
523 case WINDOW_RESIZING_BOTTOM_RIGHT:
524 pointer = POINTER_BOTTOM_RIGHT;
525 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400526 case WINDOW_EXTERIOR:
527 case WINDOW_TITLEBAR:
528 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400529 return;
530 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400531 break;
532 }
533
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400534 surface = display->pointer_surfaces[pointer];
535 buffer = display_get_buffer_for_surface(display, surface);
536 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400537 pointer_images[pointer].hotspot_x,
538 pointer_images[pointer].hotspot_y);
539}
540
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500541static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500542window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400543 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500544 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500545{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400546 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400547 struct window *window = input->pointer_focus;
548 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400549
550 input->x = x;
551 input->y = y;
552 input->sx = sx;
553 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400554
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400555 location = get_pointer_location(window, input->sx, input->sy);
556
557 if (window->motion_handler)
558 pointer = (*window->motion_handler)(window, input, time,
559 x, y, sx, sy,
560 window->user_data);
561
562 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500563}
564
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400565static void window_handle_button(void *data,
566 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400567 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500568{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400569 struct input *input = data;
570 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400571 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400572
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400573 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500574
575 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400576 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400577 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400578 wl_shell_move(window->display->shell,
579 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500580 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400581 case WINDOW_RESIZING_TOP:
582 case WINDOW_RESIZING_BOTTOM:
583 case WINDOW_RESIZING_LEFT:
584 case WINDOW_RESIZING_RIGHT:
585 case WINDOW_RESIZING_TOP_LEFT:
586 case WINDOW_RESIZING_TOP_RIGHT:
587 case WINDOW_RESIZING_BOTTOM_LEFT:
588 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400589 wl_shell_resize(window->display->shell,
590 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400591 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500592 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400593 case WINDOW_CLIENT_AREA:
594 if (window->button_handler)
595 (*window->button_handler)(window,
596 input, time,
597 button, state,
598 window->user_data);
599 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500600 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400601 } else {
602 if (window->button_handler)
603 (*window->button_handler)(window,
604 input, time,
605 button, state,
606 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500607 }
608}
609
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500610static void
611window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400612 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500613{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400614 struct input *input = data;
615 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400616 struct display *d = window->display;
617 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500618
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400619 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400620 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500621 return;
622
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400623 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400624 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400625 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
626 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500627
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400628 sym = XkbKeySymEntry(d->xkb, code, level, 0);
629
630 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400631 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400632 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400633 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500634
635 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400636 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400637 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500638}
639
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500640static void
641window_handle_pointer_focus(void *data,
642 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400643 uint32_t time, struct wl_surface *surface,
644 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500645{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400646 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647 struct window *window;
648 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400649
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400651 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400652 window = input->pointer_focus;
653
654 pointer = POINTER_LEFT_PTR;
655 if (window->motion_handler)
656 pointer = (*window->motion_handler)(window,
657 input, time,
658 x, y, sx, sy,
659 window->user_data);
660
661 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400663 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500665}
666
667static void
668window_handle_keyboard_focus(void *data,
669 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400670 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500671 struct wl_surface *surface,
672 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500673{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400674 struct input *input = data;
675 struct window *window = input->keyboard_focus;
676 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500677 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500678
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400679 window = input->keyboard_focus;
680 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500681 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400682 if (window->keyboard_focus_handler)
683 (*window->keyboard_focus_handler)(window, NULL,
684 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500685 }
686
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400687 if (surface)
688 input->keyboard_focus = wl_surface_get_user_data(surface);
689 else
690 input->keyboard_focus = NULL;
691
692 end = keys->data + keys->size;
693 for (k = keys->data; k < end; k++)
694 input->modifiers |= d->xkb->map->modmap[*k];
695
696 window = input->keyboard_focus;
697 if (window) {
698 window->keyboard_device = input;
699 if (window->keyboard_focus_handler)
700 (*window->keyboard_focus_handler)(window,
701 window->keyboard_device,
702 window->user_data);
703 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500704}
705
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500706static const struct wl_input_device_listener input_device_listener = {
707 window_handle_motion,
708 window_handle_button,
709 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500710 window_handle_pointer_focus,
711 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500712};
713
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400714void
715input_get_position(struct input *input, int32_t *x, int32_t *y)
716{
717 *x = input->sx;
718 *y = input->sy;
719}
720
721void
722window_start_drag(struct window *window, struct input *input, uint32_t time,
723 struct wl_buffer *buffer, int32_t x, int32_t y)
724{
725 cairo_device_flush (window->display->device);
726
727 wl_drag_prepare(input->drag, window->surface, time, buffer, x, y);
728 wl_drag_offer(input->drag, "text/plain");
729 wl_drag_activate(input->drag);
730}
731
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400732static void
733handle_configure(void *data, struct wl_shell *shell,
734 uint32_t time, uint32_t edges,
735 struct wl_surface *surface,
736 int32_t x, int32_t y, int32_t width, int32_t height)
737{
738 struct window *window = wl_surface_get_user_data(surface);
739
740 window->resize_edges = edges;
741 window->pending_allocation.x = x;
742 window->pending_allocation.y = y;
743 window->pending_allocation.width = width;
744 window->pending_allocation.height = height;
745
746 if (!(edges & 15))
747 return;
748
749 if (window->resize_handler)
750 (*window->resize_handler)(window,
751 window->user_data);
752 else if (window->redraw_handler)
753 window_schedule_redraw(window);
754}
755
756static const struct wl_shell_listener shell_listener = {
757 handle_configure,
758};
759
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500760void
761window_get_child_rectangle(struct window *window,
762 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500763{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400764 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500765 *rectangle = window->allocation;
766 } else {
767 rectangle->x = window->margin + 10;
768 rectangle->y = window->margin + 50;
769 rectangle->width = window->allocation.width - 20 - window->margin * 2;
770 rectangle->height = window->allocation.height - 60 - window->margin * 2;
771 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500772}
773
774void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500775window_set_child_size(struct window *window,
776 struct rectangle *rectangle)
777{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400778 int32_t width, height;
779
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500780 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400781 width = rectangle->width + 20 + window->margin * 2;
782 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400783
784 if (window->resize_edges & WINDOW_RESIZING_LEFT)
785 window->allocation.x +=
786 window->allocation.width - width;
787 if (window->resize_edges & WINDOW_RESIZING_TOP)
788 window->allocation.y +=
789 window->allocation.height - height;
790
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400791 window->allocation.width = width;
792 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500793 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500794}
795
796void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400797window_copy_image(struct window *window,
798 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500799{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400800 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500801}
802
803void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500804window_copy_surface(struct window *window,
805 struct rectangle *rectangle,
806 cairo_surface_t *surface)
807{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500808 cairo_t *cr;
809
810 cr = cairo_create (window->cairo_surface);
811
812 cairo_set_source_surface (cr,
813 surface,
814 rectangle->x, rectangle->y);
815
816 cairo_paint (cr);
817 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500818}
819
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400820static gboolean
821idle_redraw(void *data)
822{
823 struct window *window = data;
824
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400825 if (window->resize_edges)
826 window->allocation = window->pending_allocation;
827
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400828 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400829
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400830 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400831 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400832
833 return FALSE;
834}
835
836void
837window_schedule_redraw(struct window *window)
838{
839 if (!window->redraw_scheduled) {
840 g_idle_add(idle_redraw, window);
841 window->redraw_scheduled = 1;
842 }
843}
844
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500845void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500846window_set_fullscreen(struct window *window, int fullscreen)
847{
848 window->fullscreen = fullscreen;
849 if (window->fullscreen) {
850 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500851 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500852 } else {
853 window->allocation = window->saved_allocation;
854 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500855}
856
857void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400858window_set_decoration(struct window *window, int decoration)
859{
860 window->decoration = decoration;
861}
862
863void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400864window_set_user_data(struct window *window, void *data)
865{
866 window->user_data = data;
867}
868
869void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500870window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400871 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500872{
873 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500874}
875
876void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400877window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400878 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400879{
880 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400881}
882
883void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500884window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400885 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500886{
887 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500888}
889
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500890void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400891window_set_button_handler(struct window *window,
892 window_button_handler_t handler)
893{
894 window->button_handler = handler;
895}
896
897void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400898window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400899 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400900{
901 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400902}
903
904void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400905window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400906 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400907{
908 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400909}
910
911void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400912window_set_motion_handler(struct window *window,
913 window_motion_handler_t handler)
914{
915 window->motion_handler = handler;
916}
917
918void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500919window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400920 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500921{
922 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500923}
924
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400925void
926window_move(struct window *window, int32_t x, int32_t y)
927{
928 window->allocation.x = x;
929 window->allocation.y = y;
930
931 wl_surface_map(window->surface,
932 window->allocation.x - window->margin,
933 window->allocation.y - window->margin,
934 window->allocation.width,
935 window->allocation.height);
936}
937
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500938struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500939window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500940 int32_t x, int32_t y, int32_t width, int32_t height)
941{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500942 struct window *window;
943
944 window = malloc(sizeof *window);
945 if (window == NULL)
946 return NULL;
947
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500948 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500949 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500950 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500951 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500952 window->allocation.x = x;
953 window->allocation.y = y;
954 window->allocation.width = width;
955 window->allocation.height = height;
956 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500957 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400958 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500959
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400960 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400961 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500962
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500963 return window;
964}
965
966static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400967drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400968{
969 struct display *d = data;
970
971 d->device_name = strdup(device);
972}
973
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400974static void drm_handle_authenticated(void *data, struct wl_drm *drm)
975{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400976 struct display *d = data;
977
978 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400979}
980
981static const struct wl_drm_listener drm_listener = {
982 drm_handle_device,
983 drm_handle_authenticated
984};
985
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400986static void
987display_handle_acknowledge(void *data,
988 struct wl_compositor *compositor,
989 uint32_t key, uint32_t frame)
990{
991 struct display *d = data;
992 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400993
994 /* The acknowledge event means that the server processed our
995 * last commit request and we can now safely free the old
996 * window buffer if we resized and render the next frame into
997 * our back buffer.. */
998 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400999 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001000 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001001 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001002 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001003 if (window->acknowledge_handler)
1004 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1005 }
1006}
1007
1008static void
1009display_handle_frame(void *data,
1010 struct wl_compositor *compositor,
1011 uint32_t frame, uint32_t timestamp)
1012{
1013 struct display *d = data;
1014 struct window *window;
1015
1016 wl_list_for_each(window, &d->window_list, link) {
1017 if (window->frame_handler)
1018 (*window->frame_handler)(window, frame,
1019 timestamp, window->user_data);
1020 }
1021}
1022
1023static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001024 display_handle_acknowledge,
1025 display_handle_frame,
1026};
1027
1028static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001029display_handle_geometry(void *data,
1030 struct wl_output *output,
1031 int32_t width, int32_t height)
1032{
1033 struct display *display = data;
1034
1035 display->screen_allocation.x = 0;
1036 display->screen_allocation.y = 0;
1037 display->screen_allocation.width = width;
1038 display->screen_allocation.height = height;
1039}
1040
1041static const struct wl_output_listener output_listener = {
1042 display_handle_geometry,
1043};
1044
1045static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001046display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001047{
1048 struct input *input;
1049
1050 input = malloc(sizeof *input);
1051 if (input == NULL)
1052 return;
1053
1054 memset(input, 0, sizeof *input);
1055 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001056 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001057 input->pointer_focus = NULL;
1058 input->keyboard_focus = NULL;
1059 wl_list_insert(d->input_list.prev, &input->link);
1060
1061 wl_input_device_add_listener(input->input_device,
1062 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001063 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001064}
1065
1066static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001067drag_handle_device(void *data,
1068 struct wl_drag *drag, struct wl_input_device *device)
1069{
1070 struct input *input;
1071 fprintf(stderr, "device for drag %p: %p\n", drag, device);
1072
1073 input = wl_input_device_get_user_data(device);
1074 input->drag = drag;
1075
1076}
1077
1078static void
1079drag_pointer_focus(void *data,
1080 struct wl_drag *drag,
1081 uint32_t time, struct wl_surface *surface,
1082 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1083{
1084 fprintf(stderr, "drag pointer focus %p\n", surface);
1085}
1086
1087static void
1088drag_offer(void *data,
1089 struct wl_drag *drag, const char *type)
1090{
1091 fprintf(stderr, "drag offer %s\n", type);
1092}
1093
1094static void
1095drag_motion(void *data,
1096 struct wl_drag *drag,
1097 uint32_t time,
1098 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1099{
1100 fprintf(stderr, "drag motion %d,%d\n", surface_x, surface_y);
1101}
1102
1103static void
1104drag_target(void *data,
1105 struct wl_drag *drag, const char *mime_type)
1106{
1107}
1108
1109static void
1110drag_finish(void *data, struct wl_drag *drag)
1111{
1112 fprintf(stderr, "drag finish\n");
1113}
1114
1115static void
1116drag_data(void *data,
1117 struct wl_drag *drag, struct wl_array *contents)
1118{
1119}
1120
1121static const struct wl_drag_listener drag_listener = {
1122 drag_handle_device,
1123 drag_pointer_focus,
1124 drag_offer,
1125 drag_motion,
1126 drag_target,
1127 drag_finish,
1128 drag_data
1129};
1130
1131static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001132display_handle_global(struct wl_display *display, uint32_t id,
1133 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001134{
1135 struct display *d = data;
1136
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001137 if (strcmp(interface, "compositor") == 0) {
1138 d->compositor = wl_compositor_create(display, id);
1139 wl_compositor_add_listener(d->compositor,
1140 &compositor_listener, d);
1141 } else if (strcmp(interface, "output") == 0) {
1142 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001143 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001144 } else if (strcmp(interface, "input_device") == 0) {
1145 display_add_input(d, id);
1146 } else if (strcmp(interface, "shell") == 0) {
1147 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001148 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001149 } else if (strcmp(interface, "drm") == 0) {
1150 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001151 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001152 } else if (strcmp(interface, "drag") == 0) {
1153 d->drag = wl_drag_create(display, id);
1154 wl_drag_add_listener(d->drag, &drag_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001155 }
1156}
1157
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001158static const char socket_name[] = "\0wayland";
1159
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001160static void
1161display_render_frame(struct display *d)
1162{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001163 int radius = 8;
1164 cairo_t *cr;
1165
1166 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1167 cr = cairo_create(d->shadow);
1168 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1169 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1170 rounded_rect(cr, 16, 16, 112, 112, radius);
1171 cairo_fill(cr);
1172 cairo_destroy(cr);
1173 blur_surface(d->shadow, 64);
1174
1175 d->active_frame =
1176 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1177 cr = cairo_create(d->active_frame);
1178 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1179 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1180 rounded_rect(cr, 16, 16, 112, 112, radius);
1181 cairo_fill(cr);
1182 cairo_destroy(cr);
1183
1184 d->inactive_frame =
1185 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1186 cr = cairo_create(d->inactive_frame);
1187 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1188 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1189 rounded_rect(cr, 16, 16, 112, 112, radius);
1190 cairo_fill(cr);
1191 cairo_destroy(cr);
1192}
1193
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001194static void
1195init_xkb(struct display *d)
1196{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001197 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001198
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001199 names.rules = "evdev";
1200 names.model = "pc105";
1201 names.layout = "us";
1202 names.variant = "";
1203 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001204
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001205 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001206 if (!d->xkb) {
1207 fprintf(stderr, "Failed to compile keymap\n");
1208 exit(1);
1209 }
1210}
1211
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001212struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001213display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001214{
1215 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001216 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001217 int fd;
1218 GOptionContext *context;
1219 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001220 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001221
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001222 g_type_init();
1223
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001224 context = g_option_context_new(NULL);
1225 if (option_entries) {
1226 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1227 if (!g_option_context_parse(context, argc, argv, &error)) {
1228 fprintf(stderr, "option parsing failed: %s\n", error->message);
1229 exit(EXIT_FAILURE);
1230 }
1231 }
1232
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001233 d = malloc(sizeof *d);
1234 if (d == NULL)
1235 return NULL;
1236
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001237 d->display = wl_display_create(socket_name, sizeof socket_name);
1238 if (d->display == NULL) {
1239 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001240 return NULL;
1241 }
1242
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001243 wl_list_init(&d->input_list);
1244
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001245 /* Set up listener so we'll catch all events. */
1246 wl_display_add_global_listener(d->display,
1247 display_handle_global, d);
1248
1249 /* Process connection events. */
1250 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1251
1252 fd = open(d->device_name, O_RDWR);
1253 if (fd < 0) {
1254 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001255 return NULL;
1256 }
1257
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001258 if (drmGetMagic(fd, &magic)) {
1259 fprintf(stderr, "DRI2: failed to get drm magic");
1260 return NULL;
1261 }
1262
1263 /* Wait for authenticated event */
1264 wl_drm_authenticate(d->drm, magic);
1265 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1266 while (!d->authenticated)
1267 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1268
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001269 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001270 if (!eglInitialize(d->dpy, &major, &minor)) {
1271 fprintf(stderr, "failed to initialize display\n");
1272 return NULL;
1273 }
1274
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001275 eglBindAPI(EGL_OPENGL_API);
1276
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001277 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001278 if (d->ctx == NULL) {
1279 fprintf(stderr, "failed to create context\n");
1280 return NULL;
1281 }
1282
1283 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1284 fprintf(stderr, "faile to make context current\n");
1285 return NULL;
1286 }
1287
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001288 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001289 if (d->device == NULL) {
1290 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001291 return NULL;
1292 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001293
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001294 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001296 display_render_frame(d);
1297
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001298 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001299 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001300 g_source_attach(d->source, NULL);
1301
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001302 wl_list_init(&d->window_list);
1303
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001304 init_xkb(d);
1305
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001306 return d;
1307}
1308
1309struct wl_compositor *
1310display_get_compositor(struct display *display)
1311{
1312 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001313}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001314
1315EGLDisplay
1316display_get_egl_display(struct display *d)
1317{
1318 return d->dpy;
1319}
1320
1321void
1322display_run(struct display *d)
1323{
1324 g_main_loop_run(d->loop);
1325}