blob: 0aa319d93866595ebfd2aa0f0fffc6a7269b7b1b [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øgsberg43c28ee2009-01-26 23:42:46 -050060 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040061 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040062 EGLDisplay dpy;
63 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010064 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040066 GMainLoop *loop;
67 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040068 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040069 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040070 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040071 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040072 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040073 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040074
75 display_drag_offer_handler_t drag_offer_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050076};
77
78struct window {
79 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050080 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050081 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040082 struct rectangle allocation, saved_allocation, pending_allocation;
83 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040084 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050085 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050087 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040088 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040089 struct input *grab_device;
90 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050092
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050094 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050095
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050096 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050098 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040099 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500100 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400101 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500103 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400104 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105};
106
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400107struct input {
108 struct display *display;
109 struct wl_input_device *input_device;
110 struct window *pointer_focus;
111 struct window *keyboard_focus;
112 uint32_t modifiers;
113 int32_t x, y, sx, sy;
114 struct wl_list link;
115};
116
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400117const char *option_xkb_layout = "us";
118const char *option_xkb_variant = "";
119const char *option_xkb_options = "";
120
121static const GOptionEntry xkb_option_entries[] = {
122 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
123 &option_xkb_layout, "XKB Layout" },
124 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
125 &option_xkb_variant, "XKB Variant" },
126 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
127 &option_xkb_options, "XKB Options" },
128 { NULL }
129};
130
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500131static void
132rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
133{
134 cairo_move_to(cr, x0, y0 + radius);
135 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
136 cairo_line_to(cr, x1 - radius, y0);
137 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
138 cairo_line_to(cr, x1, y1 - radius);
139 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
140 cairo_line_to(cr, x0 + radius, y1);
141 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
142 cairo_close_path(cr);
143}
144
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400145static int
146texture_from_png(const char *filename, int width, int height)
147{
148 GdkPixbuf *pixbuf;
149 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400150 int stride, i;
151 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400152
153 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
154 width, height,
155 FALSE, &error);
156 if (error != NULL)
157 return -1;
158
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400159 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
160 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
161 gdk_pixbuf_unref(pixbuf);
162 return -1;
163 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400164
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400165
166 stride = gdk_pixbuf_get_rowstride(pixbuf);
167 pixels = gdk_pixbuf_get_pixels(pixbuf);
168
169 for (i = 0; i < height; i++) {
170 p = pixels + i * stride;
171 end = p + width * 4;
172 while (p < end) {
173 unsigned int t;
174
175#define MULT(d,c,a,t) \
176 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
177
178 MULT(p[0], p[0], p[3], t);
179 MULT(p[1], p[1], p[3], t);
180 MULT(p[2], p[2], p[3], t);
181 p += 4;
182
183 }
184 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400185
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400187 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400188
189 gdk_pixbuf_unref(pixbuf);
190
191 return 0;
192}
193
194static const struct {
195 const char *filename;
196 int hotspot_x, hotspot_y;
197} pointer_images[] = {
198 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
199 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
200 { DATADIR "/wayland/bottom_side.png", 16, 20 },
201 { DATADIR "/wayland/grabbing.png", 20, 17 },
202 { DATADIR "/wayland/left_ptr.png", 10, 5 },
203 { DATADIR "/wayland/left_side.png", 10, 20 },
204 { DATADIR "/wayland/right_side.png", 30, 19 },
205 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
206 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
207 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400208 { DATADIR "/wayland/xterm.png", 15, 15 },
209 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400210};
211
212static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400213create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400214{
215 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400216 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400217 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400218
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400219 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400220 display->pointer_surfaces =
221 malloc(count * sizeof *display->pointer_surfaces);
222 rect.width = width;
223 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400224 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400225 display->pointer_surfaces[i] =
226 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400227 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400228 }
229
230}
231
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400232static const cairo_user_data_key_t surface_data_key;
233struct surface_data {
234 EGLImageKHR image;
235 GLuint texture;
236 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400237 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400238};
239
240static void
241surface_data_destroy(void *p)
242{
243 struct surface_data *data = p;
244
245 glDeleteTextures(1, &data->texture);
246 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400247 if (data->buffer)
248 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400249}
250
251cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400252display_create_surface(struct display *display,
253 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400254{
255 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400256 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400257 cairo_surface_t *surface;
258
259 EGLint image_attribs[] = {
260 EGL_WIDTH, 0,
261 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400262 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
263 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400264 EGL_NONE
265 };
266
267 data = malloc(sizeof *data);
268 image_attribs[1] = rectangle->width;
269 image_attribs[3] = rectangle->height;
270 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
271 glGenTextures(1, &data->texture);
272 data->dpy = dpy;
273 glBindTexture(GL_TEXTURE_2D, data->texture);
274 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400275 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400276
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400277 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400278 CAIRO_CONTENT_COLOR_ALPHA,
279 data->texture,
280 rectangle->width,
281 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400282
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400283 cairo_surface_set_user_data (surface, &surface_data_key,
284 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400285
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400286 return surface;
287}
288
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400289struct wl_buffer *
290display_get_buffer_for_surface(struct display *display,
291 cairo_surface_t *surface)
292{
293 struct surface_data *data;
294 struct wl_visual *visual;
295 struct wl_buffer *buffer;
296 EGLint name, stride;
297 int width, height;
298
299 data = cairo_surface_get_user_data (surface, &surface_data_key);
300 if (data->buffer)
301 return data->buffer;
302
303 width = cairo_gl_surface_get_width (surface);
304 height = cairo_gl_surface_get_height (surface);
305
306 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
307
308 visual = wl_display_get_premultiplied_argb_visual(display->display);
309 buffer = wl_drm_create_buffer(display->drm,
310 name, width, height, stride, visual);
311 data->buffer = buffer;
312
313 return buffer;
314}
315
316cairo_surface_t *
317display_get_pointer_surface(struct display *display, int pointer,
318 int *width, int *height,
319 int *hotspot_x, int *hotspot_y)
320{
321 cairo_surface_t *surface;
322
323 surface = display->pointer_surfaces[pointer];
324 *width = cairo_gl_surface_get_width(surface);
325 *height = cairo_gl_surface_get_height(surface);
326 *hotspot_x = pointer_images[pointer].hotspot_x;
327 *hotspot_y = pointer_images[pointer].hotspot_y;
328
329 return cairo_surface_reference(surface);
330}
331
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400332
333static void
334window_attach_surface(struct window *window);
335
336static void
337free_surface(void *data)
338{
339 struct window *window = data;
340
341 cairo_surface_destroy(window->pending_surface);
342 window->pending_surface = NULL;
343 if (window->cairo_surface)
344 window_attach_surface(window);
345}
346
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500347static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500348window_attach_surface(struct window *window)
349{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400350 struct display *display = window->display;
351 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500352
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500353 if (window->pending_surface != NULL)
354 return;
355
Kristian Høgsberg09531622010-06-14 23:22:15 -0400356 window->pending_surface = window->cairo_surface;
357 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500358
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400359 buffer = display_get_buffer_for_surface(display,
360 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400361 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500362
363 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400364 window->allocation.x,
365 window->allocation.y,
366 window->allocation.width,
367 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400368
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400369 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500370}
371
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500372void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400373window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500374{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400375 if (window->cairo_surface)
376 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500377}
378
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500379static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500380window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500381{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500382 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500383 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400384 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 cairo_surface_t *frame;
386 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500387
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500388 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400389 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400390 width = window->allocation.width;
391 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500392
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500393 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500394 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500395 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
396
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500397 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500398
Kristian Høgsberg09531622010-06-14 23:22:15 -0400399 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400400 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400401 cairo_paint(cr);
402
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400403 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400404 tile_mask(cr, window->display->shadow,
405 shadow_dx, shadow_dy, width, height,
406 window->margin + 10 - shadow_dx,
407 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500408
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400409 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400410 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400411 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400412 frame = window->display->inactive_frame;
413
414 tile_source(cr, frame, 0, 0, width, height,
415 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500416
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500417 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
418 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500419 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400420 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500421 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
422 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
423 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400424 if (window->keyboard_device)
425 cairo_set_source_rgb(cr, 0, 0, 0);
426 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400427 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400428 cairo_show_text(cr, window->title);
429
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500430 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400431
432 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500433}
434
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400435void
436display_flush_cairo_device(struct display *display)
437{
438 cairo_device_flush (display->device);
439}
440
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500441static void
442window_draw_fullscreen(struct window *window)
443{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500444 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400445 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500446}
447
448void
449window_draw(struct window *window)
450{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500451 if (window->cairo_surface != NULL)
452 cairo_surface_destroy(window->cairo_surface);
453
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400454 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500455 window_draw_fullscreen(window);
456 else
457 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500458}
459
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400460cairo_surface_t *
461window_get_surface(struct window *window)
462{
463 return window->cairo_surface;
464}
465
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400466enum window_location {
467 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400468 WINDOW_RESIZING_TOP = 1,
469 WINDOW_RESIZING_BOTTOM = 2,
470 WINDOW_RESIZING_LEFT = 4,
471 WINDOW_RESIZING_TOP_LEFT = 5,
472 WINDOW_RESIZING_BOTTOM_LEFT = 6,
473 WINDOW_RESIZING_RIGHT = 8,
474 WINDOW_RESIZING_TOP_RIGHT = 9,
475 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
476 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400477 WINDOW_EXTERIOR = 16,
478 WINDOW_TITLEBAR = 17,
479 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400480};
481
482static int
483get_pointer_location(struct window *window, int32_t x, int32_t y)
484{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400485 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400486 const int grip_size = 8;
487
488 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400489 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400490 else if (window->margin <= x && x < window->margin + grip_size)
491 hlocation = WINDOW_RESIZING_LEFT;
492 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400493 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494 else if (x < window->allocation.width - window->margin)
495 hlocation = WINDOW_RESIZING_RIGHT;
496 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400497 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498
499 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400500 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400501 else if (window->margin <= y && y < window->margin + grip_size)
502 vlocation = WINDOW_RESIZING_TOP;
503 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400504 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400505 else if (y < window->allocation.height - window->margin)
506 vlocation = WINDOW_RESIZING_BOTTOM;
507 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400508 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400510 location = vlocation | hlocation;
511 if (location & WINDOW_EXTERIOR)
512 location = WINDOW_EXTERIOR;
513 if (location == WINDOW_INTERIOR && y < window->margin + 50)
514 location = WINDOW_TITLEBAR;
515 else if (location == WINDOW_INTERIOR)
516 location = WINDOW_CLIENT_AREA;
517
518 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400519}
520
521static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400522set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400523{
524 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400525 struct wl_buffer *buffer;
526 cairo_surface_t *surface;
527 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400528
529 location = get_pointer_location(input->pointer_focus,
530 input->sx, input->sy);
531 switch (location) {
532 case WINDOW_RESIZING_TOP:
533 pointer = POINTER_TOP;
534 break;
535 case WINDOW_RESIZING_BOTTOM:
536 pointer = POINTER_BOTTOM;
537 break;
538 case WINDOW_RESIZING_LEFT:
539 pointer = POINTER_LEFT;
540 break;
541 case WINDOW_RESIZING_RIGHT:
542 pointer = POINTER_RIGHT;
543 break;
544 case WINDOW_RESIZING_TOP_LEFT:
545 pointer = POINTER_TOP_LEFT;
546 break;
547 case WINDOW_RESIZING_TOP_RIGHT:
548 pointer = POINTER_TOP_RIGHT;
549 break;
550 case WINDOW_RESIZING_BOTTOM_LEFT:
551 pointer = POINTER_BOTTOM_LEFT;
552 break;
553 case WINDOW_RESIZING_BOTTOM_RIGHT:
554 pointer = POINTER_BOTTOM_RIGHT;
555 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400556 case WINDOW_EXTERIOR:
557 case WINDOW_TITLEBAR:
558 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400559 return;
560 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400561 break;
562 }
563
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400564 surface = display->pointer_surfaces[pointer];
565 buffer = display_get_buffer_for_surface(display, surface);
566 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400567 pointer_images[pointer].hotspot_x,
568 pointer_images[pointer].hotspot_y);
569}
570
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500571static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500572window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400573 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500574 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500575{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400576 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400577 struct window *window = input->pointer_focus;
578 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400579
580 input->x = x;
581 input->y = y;
582 input->sx = sx;
583 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400584
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400585 location = get_pointer_location(window, input->sx, input->sy);
586
587 if (window->motion_handler)
588 pointer = (*window->motion_handler)(window, input, time,
589 x, y, sx, sy,
590 window->user_data);
591
592 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500593}
594
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400595static void
596window_handle_button(void *data,
597 struct wl_input_device *input_device,
598 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500599{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400600 struct input *input = data;
601 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400602 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400603
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400604 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500605
606 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400607 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400608 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400609 wl_shell_move(window->display->shell,
610 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500611 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400612 case WINDOW_RESIZING_TOP:
613 case WINDOW_RESIZING_BOTTOM:
614 case WINDOW_RESIZING_LEFT:
615 case WINDOW_RESIZING_RIGHT:
616 case WINDOW_RESIZING_TOP_LEFT:
617 case WINDOW_RESIZING_TOP_RIGHT:
618 case WINDOW_RESIZING_BOTTOM_LEFT:
619 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400620 wl_shell_resize(window->display->shell,
621 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400622 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500623 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400624 case WINDOW_CLIENT_AREA:
625 if (window->button_handler)
626 (*window->button_handler)(window,
627 input, time,
628 button, state,
629 window->user_data);
630 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500631 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400632 } else {
633 if (window->button_handler)
634 (*window->button_handler)(window,
635 input, time,
636 button, state,
637 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500638 }
639}
640
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500641static void
642window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400643 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500644{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400645 struct input *input = data;
646 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400647 struct display *d = window->display;
648 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500649
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400650 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400651 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500652 return;
653
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400654 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400655 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400656 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
657 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500658
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400659 sym = XkbKeySymEntry(d->xkb, code, level, 0);
660
661 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400662 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400663 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400664 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500665
666 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400667 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400668 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500669}
670
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500671static void
672window_handle_pointer_focus(void *data,
673 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400674 uint32_t time, struct wl_surface *surface,
675 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500676{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400677 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678 struct window *window;
679 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400680
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400682 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683 window = input->pointer_focus;
684
685 pointer = POINTER_LEFT_PTR;
686 if (window->motion_handler)
687 pointer = (*window->motion_handler)(window,
688 input, time,
689 x, y, sx, sy,
690 window->user_data);
691
692 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400694 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500696}
697
698static void
699window_handle_keyboard_focus(void *data,
700 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400701 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500702 struct wl_surface *surface,
703 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500704{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400705 struct input *input = data;
706 struct window *window = input->keyboard_focus;
707 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500708 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500709
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400710 window = input->keyboard_focus;
711 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500712 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400713 if (window->keyboard_focus_handler)
714 (*window->keyboard_focus_handler)(window, NULL,
715 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500716 }
717
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400718 if (surface)
719 input->keyboard_focus = wl_surface_get_user_data(surface);
720 else
721 input->keyboard_focus = NULL;
722
723 end = keys->data + keys->size;
724 for (k = keys->data; k < end; k++)
725 input->modifiers |= d->xkb->map->modmap[*k];
726
727 window = input->keyboard_focus;
728 if (window) {
729 window->keyboard_device = input;
730 if (window->keyboard_focus_handler)
731 (*window->keyboard_focus_handler)(window,
732 window->keyboard_device,
733 window->user_data);
734 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500735}
736
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500737static const struct wl_input_device_listener input_device_listener = {
738 window_handle_motion,
739 window_handle_button,
740 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500741 window_handle_pointer_focus,
742 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500743};
744
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400745void
746input_get_position(struct input *input, int32_t *x, int32_t *y)
747{
748 *x = input->sx;
749 *y = input->sy;
750}
751
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400752struct wl_input_device *
753input_get_input_device(struct input *input)
754{
755 return input->input_device;
756}
757
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400758struct wl_drag *
759window_start_drag(struct window *window, struct input *input, uint32_t time,
760 const struct wl_drag_listener *listener, void *data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400761{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400762 struct wl_drag *drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400763
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400764 cairo_device_flush (window->display->device);
765
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400766 drag = wl_shell_create_drag(window->display->shell);
767 wl_drag_offer(drag, "text/plain");
768 wl_drag_offer(drag, "text/html");
769 wl_drag_activate(drag, window->surface, input->input_device, time);
770 wl_drag_add_listener(drag, listener, data);
771
772 return drag;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400773}
774
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400775static void
776handle_configure(void *data, struct wl_shell *shell,
777 uint32_t time, uint32_t edges,
778 struct wl_surface *surface,
779 int32_t x, int32_t y, int32_t width, int32_t height)
780{
781 struct window *window = wl_surface_get_user_data(surface);
782
783 window->resize_edges = edges;
784 window->pending_allocation.x = x;
785 window->pending_allocation.y = y;
786 window->pending_allocation.width = width;
787 window->pending_allocation.height = height;
788
789 if (!(edges & 15))
790 return;
791
792 if (window->resize_handler)
793 (*window->resize_handler)(window,
794 window->user_data);
795 else if (window->redraw_handler)
796 window_schedule_redraw(window);
797}
798
799static const struct wl_shell_listener shell_listener = {
800 handle_configure,
801};
802
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500803void
804window_get_child_rectangle(struct window *window,
805 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500806{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400807 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500808 *rectangle = window->allocation;
809 } else {
810 rectangle->x = window->margin + 10;
811 rectangle->y = window->margin + 50;
812 rectangle->width = window->allocation.width - 20 - window->margin * 2;
813 rectangle->height = window->allocation.height - 60 - window->margin * 2;
814 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500815}
816
817void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500818window_set_child_size(struct window *window,
819 struct rectangle *rectangle)
820{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400821 int32_t width, height;
822
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500823 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400824 width = rectangle->width + 20 + window->margin * 2;
825 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400826
827 if (window->resize_edges & WINDOW_RESIZING_LEFT)
828 window->allocation.x +=
829 window->allocation.width - width;
830 if (window->resize_edges & WINDOW_RESIZING_TOP)
831 window->allocation.y +=
832 window->allocation.height - height;
833
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400834 window->allocation.width = width;
835 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500836 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500837}
838
839void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400840window_copy_image(struct window *window,
841 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500842{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400843 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500844}
845
846void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500847window_copy_surface(struct window *window,
848 struct rectangle *rectangle,
849 cairo_surface_t *surface)
850{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500851 cairo_t *cr;
852
853 cr = cairo_create (window->cairo_surface);
854
855 cairo_set_source_surface (cr,
856 surface,
857 rectangle->x, rectangle->y);
858
859 cairo_paint (cr);
860 cairo_destroy (cr);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400861
862 wl_surface_damage(window->surface,
863 rectangle->x, rectangle->y,
864 rectangle->width, rectangle->height);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500865}
866
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400867static gboolean
868idle_redraw(void *data)
869{
870 struct window *window = data;
871
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400872 if (window->resize_edges)
873 window->allocation = window->pending_allocation;
874
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400875 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400876
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400877 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400878 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400879
880 return FALSE;
881}
882
883void
884window_schedule_redraw(struct window *window)
885{
886 if (!window->redraw_scheduled) {
887 g_idle_add(idle_redraw, window);
888 window->redraw_scheduled = 1;
889 }
890}
891
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500892void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500893window_set_fullscreen(struct window *window, int fullscreen)
894{
895 window->fullscreen = fullscreen;
896 if (window->fullscreen) {
897 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500898 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500899 } else {
900 window->allocation = window->saved_allocation;
901 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500902}
903
904void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400905window_set_decoration(struct window *window, int decoration)
906{
907 window->decoration = decoration;
908}
909
910void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400911window_set_user_data(struct window *window, void *data)
912{
913 window->user_data = data;
914}
915
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400916void *
917window_get_user_data(struct window *window)
918{
919 return window->user_data;
920}
921
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400922void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500923window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400924 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500925{
926 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500927}
928
929void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400930window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400931 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400932{
933 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400934}
935
936void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500937window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400938 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500939{
940 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500941}
942
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500943void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400944window_set_button_handler(struct window *window,
945 window_button_handler_t handler)
946{
947 window->button_handler = handler;
948}
949
950void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400951window_set_motion_handler(struct window *window,
952 window_motion_handler_t handler)
953{
954 window->motion_handler = handler;
955}
956
957void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500958window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400959 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500960{
961 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500962}
963
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400964void
965window_move(struct window *window, int32_t x, int32_t y)
966{
967 window->allocation.x = x;
968 window->allocation.y = y;
969
970 wl_surface_map(window->surface,
971 window->allocation.x - window->margin,
972 window->allocation.y - window->margin,
973 window->allocation.width,
974 window->allocation.height);
975}
976
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500977struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500978window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500979 int32_t x, int32_t y, int32_t width, int32_t height)
980{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500981 struct window *window;
982
983 window = malloc(sizeof *window);
984 if (window == NULL)
985 return NULL;
986
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500987 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500988 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500989 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500990 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500991 window->allocation.x = x;
992 window->allocation.y = y;
993 window->allocation.width = width;
994 window->allocation.height = height;
995 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500996 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400997 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500998
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001000 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001001
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001002 return window;
1003}
1004
1005static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001006drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001007{
1008 struct display *d = data;
1009
1010 d->device_name = strdup(device);
1011}
1012
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001013static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1014{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001015 struct display *d = data;
1016
1017 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001018}
1019
1020static const struct wl_drm_listener drm_listener = {
1021 drm_handle_device,
1022 drm_handle_authenticated
1023};
1024
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001025static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001026display_handle_geometry(void *data,
1027 struct wl_output *output,
1028 int32_t width, int32_t height)
1029{
1030 struct display *display = data;
1031
1032 display->screen_allocation.x = 0;
1033 display->screen_allocation.y = 0;
1034 display->screen_allocation.width = width;
1035 display->screen_allocation.height = height;
1036}
1037
1038static const struct wl_output_listener output_listener = {
1039 display_handle_geometry,
1040};
1041
1042static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001043display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001044{
1045 struct input *input;
1046
1047 input = malloc(sizeof *input);
1048 if (input == NULL)
1049 return;
1050
1051 memset(input, 0, sizeof *input);
1052 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001053 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001054 input->pointer_focus = NULL;
1055 input->keyboard_focus = NULL;
1056 wl_list_insert(d->input_list.prev, &input->link);
1057
1058 wl_input_device_add_listener(input->input_device,
1059 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001060 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001061}
1062
1063static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001064display_handle_global(struct wl_display *display, uint32_t id,
1065 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001066{
1067 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001068 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001069
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001070 if (strcmp(interface, "compositor") == 0) {
1071 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001072 } else if (strcmp(interface, "output") == 0) {
1073 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001074 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001075 } else if (strcmp(interface, "input_device") == 0) {
1076 display_add_input(d, id);
1077 } else if (strcmp(interface, "shell") == 0) {
1078 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001079 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001080 } else if (strcmp(interface, "drm") == 0) {
1081 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001082 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001083 } else if (strcmp(interface, "drag_offer") == 0) {
1084 offer = wl_drag_offer_create(display, id);
1085 d->drag_offer_handler(offer, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001086 }
1087}
1088
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001089static const char socket_name[] = "\0wayland";
1090
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001091static void
1092display_render_frame(struct display *d)
1093{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001094 int radius = 8;
1095 cairo_t *cr;
1096
1097 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1098 cr = cairo_create(d->shadow);
1099 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1100 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1101 rounded_rect(cr, 16, 16, 112, 112, radius);
1102 cairo_fill(cr);
1103 cairo_destroy(cr);
1104 blur_surface(d->shadow, 64);
1105
1106 d->active_frame =
1107 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1108 cr = cairo_create(d->active_frame);
1109 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1110 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1111 rounded_rect(cr, 16, 16, 112, 112, radius);
1112 cairo_fill(cr);
1113 cairo_destroy(cr);
1114
1115 d->inactive_frame =
1116 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1117 cr = cairo_create(d->inactive_frame);
1118 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1119 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1120 rounded_rect(cr, 16, 16, 112, 112, radius);
1121 cairo_fill(cr);
1122 cairo_destroy(cr);
1123}
1124
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001125static void
1126init_xkb(struct display *d)
1127{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001128 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001129
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001130 names.rules = "evdev";
1131 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001132 names.layout = option_xkb_layout;
1133 names.variant = option_xkb_variant;
1134 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001135
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001136 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001137 if (!d->xkb) {
1138 fprintf(stderr, "Failed to compile keymap\n");
1139 exit(1);
1140 }
1141}
1142
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001143struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001144display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001145{
1146 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001147 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001148 int fd;
1149 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001150 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001151 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001152 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001153
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001154 g_type_init();
1155
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001156 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001157 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001158 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001159
1160 xkb_option_group = g_option_group_new("xkb",
1161 "Keyboard options",
1162 "Show all XKB options",
1163 NULL, NULL);
1164 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1165 g_option_context_add_group (context, xkb_option_group);
1166
1167 if (!g_option_context_parse(context, argc, argv, &error)) {
1168 fprintf(stderr, "option parsing failed: %s\n", error->message);
1169 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001170 }
1171
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001172
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001173 d = malloc(sizeof *d);
1174 if (d == NULL)
1175 return NULL;
1176
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001177 d->display = wl_display_create(socket_name, sizeof socket_name);
1178 if (d->display == NULL) {
1179 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001180 return NULL;
1181 }
1182
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001183 wl_list_init(&d->input_list);
1184
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001185 /* Set up listener so we'll catch all events. */
1186 wl_display_add_global_listener(d->display,
1187 display_handle_global, d);
1188
1189 /* Process connection events. */
1190 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1191
1192 fd = open(d->device_name, O_RDWR);
1193 if (fd < 0) {
1194 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001195 return NULL;
1196 }
1197
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001198 if (drmGetMagic(fd, &magic)) {
1199 fprintf(stderr, "DRI2: failed to get drm magic");
1200 return NULL;
1201 }
1202
1203 /* Wait for authenticated event */
1204 wl_drm_authenticate(d->drm, magic);
1205 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1206 while (!d->authenticated)
1207 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1208
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001209 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001210 if (!eglInitialize(d->dpy, &major, &minor)) {
1211 fprintf(stderr, "failed to initialize display\n");
1212 return NULL;
1213 }
1214
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001215 eglBindAPI(EGL_OPENGL_API);
1216
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001217 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001218 if (d->ctx == NULL) {
1219 fprintf(stderr, "failed to create context\n");
1220 return NULL;
1221 }
1222
1223 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1224 fprintf(stderr, "faile to make context current\n");
1225 return NULL;
1226 }
1227
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001228 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001229 if (d->device == NULL) {
1230 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001231 return NULL;
1232 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001233
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001236 display_render_frame(d);
1237
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001238 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001239 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001240 g_source_attach(d->source, NULL);
1241
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001242 wl_list_init(&d->window_list);
1243
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001244 init_xkb(d);
1245
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001246 return d;
1247}
1248
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001249struct wl_display *
1250display_get_display(struct display *display)
1251{
1252 return display->display;
1253}
1254
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001255struct wl_compositor *
1256display_get_compositor(struct display *display)
1257{
1258 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001259}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001260
1261EGLDisplay
1262display_get_egl_display(struct display *d)
1263{
1264 return d->dpy;
1265}
1266
1267void
1268display_run(struct display *d)
1269{
1270 g_main_loop_run(d->loop);
1271}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001272
1273void
1274display_set_drag_offer_handler(struct display *display,
1275 display_drag_offer_handler_t handler)
1276{
1277 display->drag_offer_handler = handler;
1278}