blob: a867a8884cda6448dd3197ed028d18f30cdb292b [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øgsberga85fe3c2010-06-08 14:08:30 -040034
35#define EGL_EGLEXT_PROTOTYPES 1
36#define GL_GLEXT_PROTOTYPES 1
37#include <GL/gl.h>
38#include <EGL/egl.h>
39#include <EGL/eglext.h>
40#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050041
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050043#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050044#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050045#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040046#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050047
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050048#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050049
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050050struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050051 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050052 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050053 struct wl_output *output;
54 struct wl_input_device *input_device;
55 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056 EGLDisplay dpy;
57 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010058 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040060 GMainLoop *loop;
61 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040062 struct wl_list window_list;
63 char *device_name;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064};
65
66struct window {
67 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050068 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050070 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050071 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050073 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050074 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050075 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040076 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050077 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050078 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050080 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050081
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040082 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050083 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberga341fa02010-01-24 18:10:15 -050084 int new_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050085
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050086 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050087 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050088 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040089 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040090 window_frame_handler_t frame_handler;
91
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050092 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050094};
95
Kristian Høgsberge4feb562008-11-08 18:53:37 -050096static void
97rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
98{
99 cairo_move_to(cr, x0, y0 + radius);
100 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
101 cairo_line_to(cr, x1 - radius, y0);
102 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
103 cairo_line_to(cr, x1, y1 - radius);
104 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
105 cairo_line_to(cr, x0 + radius, y1);
106 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
107 cairo_close_path(cr);
108}
109
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110static const cairo_user_data_key_t surface_data_key;
111struct surface_data {
112 EGLImageKHR image;
113 GLuint texture;
114 EGLDisplay dpy;
115};
116
117static void
118surface_data_destroy(void *p)
119{
120 struct surface_data *data = p;
121
122 glDeleteTextures(1, &data->texture);
123 eglDestroyImageKHR(data->dpy, data->image);
124}
125
126cairo_surface_t *
127window_create_surface(struct window *window,
128 struct rectangle *rectangle)
129{
130 struct surface_data *data;
131 EGLDisplay dpy = window->display->dpy;
132 cairo_surface_t *surface;
133
134 EGLint image_attribs[] = {
135 EGL_WIDTH, 0,
136 EGL_HEIGHT, 0,
137 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
138 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
139 EGL_NONE
140 };
141
142 data = malloc(sizeof *data);
143 image_attribs[1] = rectangle->width;
144 image_attribs[3] = rectangle->height;
145 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
146 glGenTextures(1, &data->texture);
147 data->dpy = dpy;
148 glBindTexture(GL_TEXTURE_2D, data->texture);
149 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
150
151 surface = cairo_gl_surface_create_for_texture(window->display->device,
152 CAIRO_CONTENT_COLOR_ALPHA,
153 data->texture,
154 rectangle->width,
155 rectangle->height);
156
157 cairo_surface_set_user_data (surface, &surface_data_key,
158 data, surface_data_destroy);
159
160 return surface;
161}
162
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500163static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500164window_attach_surface(struct window *window)
165{
166 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400167 struct surface_data *data;
168 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500169
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500170 if (window->pending_surface != NULL)
171 return;
172
173 window->pending_surface =
174 cairo_surface_reference(window->cairo_surface);
175
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400176 data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key);
177 eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride);
178
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500179 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
180 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400181 name,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500182 window->allocation.width,
183 window->allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400184 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500185 visual);
186
187 wl_surface_map(window->surface,
188 window->allocation.x - window->margin,
189 window->allocation.y - window->margin,
190 window->allocation.width,
191 window->allocation.height);
192}
193
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500194void
195window_commit(struct window *window, uint32_t key)
196{
197 if (window->new_surface) {
198 window_attach_surface(window);
199 window->new_surface = 0;
200 }
201
202 wl_compositor_commit(window->display->compositor, key);
203}
204
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500205static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500206window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500207{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500208 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500209 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500210 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500211 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500212 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400213 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500214
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500215 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400216 window_create_surface(window, &window->allocation);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500217
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500218 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500219 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500220 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
221
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500222 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500223
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500224 width = window->allocation.width - window->margin * 2;
225 height = window->allocation.height - window->margin * 2;
226
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400227 cairo_translate(cr, window->margin + shadow_dx,
228 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500229 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500230 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400231 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500232 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500233
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400234#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500235#ifdef SLOW_BUT_PWETTY
236 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
237 * Ideally we should have convolution filters in cairo, but we
238 * can also fallback to compositing the shadow image a bunch
239 * of times according to the blur kernel. */
240 {
241 cairo_surface_t *map;
242
243 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500244 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500245 cairo_drm_surface_unmap(window->cairo_surface, map);
246 }
247#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500248
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400249 cairo_translate(cr, -shadow_dx, -shadow_dy);
250 if (window->keyboard_device) {
251 rounded_rect(cr, 0, 0, width, height, radius);
252 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
253 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
254 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
255 cairo_set_source(cr, gradient);
256 cairo_fill(cr);
257 cairo_pattern_destroy(gradient);
258 } else {
259 rounded_rect(cr, 0, 0, width, height, radius);
260 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
261 cairo_fill(cr);
262 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500263
264 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
265 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500266 cairo_line_to(cr, width - 10, 50);
267 cairo_line_to(cr, width - 10, height - 10);
268 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500269 cairo_close_path(cr);
270 cairo_set_source(cr, dim);
271 cairo_stroke(cr);
272
273 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500274 cairo_line_to(cr, width - 10, 51);
275 cairo_line_to(cr, width - 10, height - 10);
276 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500277 cairo_close_path(cr);
278 cairo_set_source(cr, bright);
279 cairo_stroke(cr);
280
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500281 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
282 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500283 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500284 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500285 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
286 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
287 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500288 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400289 if (window->keyboard_device) {
290 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
291 cairo_stroke_preserve(cr);
292 cairo_set_source_rgb(cr, 1, 1, 1);
293 cairo_fill(cr);
294 } else {
295 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
296 cairo_fill(cr);
297 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500299}
300
301static void
302window_draw_fullscreen(struct window *window)
303{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500304 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400305 window_create_surface(window, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500306}
307
308void
309window_draw(struct window *window)
310{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500311 if (window->cairo_surface != NULL)
312 cairo_surface_destroy(window->cairo_surface);
313
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400314 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500315 window_draw_fullscreen(window);
316 else
317 window_draw_decorations(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500318
319 window->new_surface = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500320}
321
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400322cairo_surface_t *
323window_get_surface(struct window *window)
324{
325 return window->cairo_surface;
326}
327
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500328enum window_state {
329 WINDOW_STABLE,
330 WINDOW_MOVING,
331 WINDOW_RESIZING_UPPER_LEFT,
332 WINDOW_RESIZING_UPPER_RIGHT,
333 WINDOW_RESIZING_LOWER_LEFT,
334 WINDOW_RESIZING_LOWER_RIGHT
335};
336
337enum location {
338 LOCATION_INTERIOR,
339 LOCATION_UPPER_LEFT,
340 LOCATION_UPPER_RIGHT,
341 LOCATION_LOWER_LEFT,
342 LOCATION_LOWER_RIGHT,
343 LOCATION_OUTSIDE
344};
345
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500346static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500347window_handle_motion(void *data, struct wl_input_device *input_device,
348 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500349{
350 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500351
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352 switch (window->state) {
353 case WINDOW_MOVING:
354 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500355 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500356 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500357 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500358 window->allocation.x = window->drag_x + x;
359 window->allocation.y = window->drag_y + y;
360 wl_surface_map(window->surface,
361 window->allocation.x - window->margin,
362 window->allocation.y - window->margin,
363 window->allocation.width,
364 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500365 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500366 break;
367 case WINDOW_RESIZING_LOWER_RIGHT:
368 if (window->fullscreen)
369 break;
370 if (window->grab_device != input_device)
371 break;
372 window->allocation.width = window->drag_x + x;
373 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500374
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500375 if (window->resize_handler)
376 (*window->resize_handler)(window,
377 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500378
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500379 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500380 }
381}
382
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500383static void window_handle_button(void *data, struct wl_input_device *input_device,
384 uint32_t button, uint32_t state,
385 int32_t x, int32_t y, int32_t sx, int32_t sy)
386{
387 struct window *window = data;
388 int32_t left = window->allocation.x;
389 int32_t right = window->allocation.x +
390 window->allocation.width - window->margin * 2;
391 int32_t top = window->allocation.y;
392 int32_t bottom = window->allocation.y +
393 window->allocation.height - window->margin * 2;
394 int grip_size = 16, location;
395
396 if (right - grip_size <= x && x < right &&
397 bottom - grip_size <= y && y < bottom) {
398 location = LOCATION_LOWER_RIGHT;
399 } else if (left <= x && x < right && top <= y && y < bottom) {
400 location = LOCATION_INTERIOR;
401 } else {
402 location = LOCATION_OUTSIDE;
403 }
404
405 if (button == BTN_LEFT && state == 1) {
406 switch (location) {
407 case LOCATION_INTERIOR:
408 window->drag_x = window->allocation.x - x;
409 window->drag_y = window->allocation.y - y;
410 window->state = WINDOW_MOVING;
411 window->grab_device = input_device;
412 break;
413 case LOCATION_LOWER_RIGHT:
414 window->drag_x = window->allocation.width - x;
415 window->drag_y = window->allocation.height - y;
416 window->state = WINDOW_RESIZING_LOWER_RIGHT;
417 window->grab_device = input_device;
418 break;
419 default:
420 window->state = WINDOW_STABLE;
421 break;
422 }
423 } else if (button == BTN_LEFT &&
424 state == 0 && window->grab_device == input_device) {
425 window->state = WINDOW_STABLE;
426 }
427}
428
Kristian Høgsberg55444912009-02-21 14:31:09 -0500429
430struct key {
431 uint32_t code[4];
432} evdev_keymap[] = {
433 { { 0, 0 } }, /* 0 */
434 { { 0x1b, 0x1b } },
435 { { '1', '!' } },
436 { { '2', '@' } },
437 { { '3', '#' } },
438 { { '4', '$' } },
439 { { '5', '%' } },
440 { { '6', '^' } },
441 { { '7', '&' } },
442 { { '8', '*' } },
443 { { '9', '(' } },
444 { { '0', ')' } },
445 { { '-', '_' } },
446 { { '=', '+' } },
447 { { '\b', '\b' } },
448 { { '\t', '\t' } },
449
450 { { 'q', 'Q', 0x11 } }, /* 16 */
451 { { 'w', 'W', 0x17 } },
452 { { 'e', 'E', 0x05 } },
453 { { 'r', 'R', 0x12 } },
454 { { 't', 'T', 0x14 } },
455 { { 'y', 'Y', 0x19 } },
456 { { 'u', 'U', 0x15 } },
457 { { 'i', 'I', 0x09 } },
458 { { 'o', 'O', 0x0f } },
459 { { 'p', 'P', 0x10 } },
460 { { '[', '{', 0x1b } },
461 { { ']', '}', 0x1d } },
462 { { '\n', '\n' } },
463 { { 0, 0 } },
464 { { 'a', 'A', 0x01} },
465 { { 's', 'S', 0x13 } },
466
467 { { 'd', 'D', 0x04 } }, /* 32 */
468 { { 'f', 'F', 0x06 } },
469 { { 'g', 'G', 0x07 } },
470 { { 'h', 'H', 0x08 } },
471 { { 'j', 'J', 0x0a } },
472 { { 'k', 'K', 0x0b } },
473 { { 'l', 'L', 0x0c } },
474 { { ';', ':' } },
475 { { '\'', '"' } },
476 { { '`', '~' } },
477 { { 0, 0 } },
478 { { '\\', '|', 0x1c } },
479 { { 'z', 'Z', 0x1a } },
480 { { 'x', 'X', 0x18 } },
481 { { 'c', 'C', 0x03 } },
482 { { 'v', 'V', 0x16 } },
483
484 { { 'b', 'B', 0x02 } }, /* 48 */
485 { { 'n', 'N', 0x0e } },
486 { { 'm', 'M', 0x0d } },
487 { { ',', '<' } },
488 { { '.', '>' } },
489 { { '/', '?' } },
490 { { 0, 0 } },
491 { { '*', '*' } },
492 { { 0, 0 } },
493 { { ' ', ' ' } },
494 { { 0, 0 } }
495
496 /* 59 */
497};
498
499#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
500
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500501static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500502window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500503{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500504 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500505
Kristian Høgsberg55444912009-02-21 14:31:09 -0500506 switch (key) {
507 case KEY_LEFTSHIFT:
508 case KEY_RIGHTSHIFT:
509 mod = WINDOW_MODIFIER_SHIFT;
510 break;
511 case KEY_LEFTCTRL:
512 case KEY_RIGHTCTRL:
513 mod = WINDOW_MODIFIER_CONTROL;
514 break;
515 case KEY_LEFTALT:
516 case KEY_RIGHTALT:
517 mod = WINDOW_MODIFIER_ALT;
518 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500519 }
520
521 if (state)
522 window->modifiers |= mod;
523 else
524 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500525}
526
527static void
528window_handle_key(void *data, struct wl_input_device *input_device,
529 uint32_t key, uint32_t state)
530{
531 struct window *window = data;
532 uint32_t unicode = 0;
533
534 if (window->keyboard_device != input_device)
535 return;
536
537 window_update_modifiers(window, key, state);
538
539 if (key < ARRAY_LENGTH(evdev_keymap)) {
540 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
541 unicode = evdev_keymap[key].code[2];
542 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
543 unicode = evdev_keymap[key].code[1];
544 else
545 unicode = evdev_keymap[key].code[0];
546 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500547
548 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500549 (*window->key_handler)(window, key, unicode,
550 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500551}
552
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500553static void
554window_handle_pointer_focus(void *data,
555 struct wl_input_device *input_device,
556 struct wl_surface *surface)
557{
558}
559
560static void
561window_handle_keyboard_focus(void *data,
562 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500563 struct wl_surface *surface,
564 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500565{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500566 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500567 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500568
569 if (window->keyboard_device == input_device && surface != window->surface)
570 window->keyboard_device = NULL;
571 else if (window->keyboard_device == NULL && surface == window->surface)
572 window->keyboard_device = input_device;
573 else
574 return;
575
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500576 if (window->keyboard_device) {
577 end = keys->data + keys->size;
578 for (k = keys->data; k < end; k++)
579 window_update_modifiers(window, *k, 1);
580 } else {
581 window->modifiers = 0;
582 }
583
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500584 if (window->keyboard_focus_handler)
585 (*window->keyboard_focus_handler)(window,
586 window->keyboard_device,
587 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500588}
589
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500590static const struct wl_input_device_listener input_device_listener = {
591 window_handle_motion,
592 window_handle_button,
593 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500594 window_handle_pointer_focus,
595 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500596};
597
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500598void
599window_get_child_rectangle(struct window *window,
600 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500601{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400602 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500603 *rectangle = window->allocation;
604 } else {
605 rectangle->x = window->margin + 10;
606 rectangle->y = window->margin + 50;
607 rectangle->width = window->allocation.width - 20 - window->margin * 2;
608 rectangle->height = window->allocation.height - 60 - window->margin * 2;
609 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500610}
611
612void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500613window_set_child_size(struct window *window,
614 struct rectangle *rectangle)
615{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500616 if (!window->fullscreen) {
617 window->allocation.width = rectangle->width + 20 + window->margin * 2;
618 window->allocation.height = rectangle->height + 60 + window->margin * 2;
619 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500620}
621
622void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400623window_copy_image(struct window *window,
624 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500625{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400626 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500627}
628
629void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500630window_copy_surface(struct window *window,
631 struct rectangle *rectangle,
632 cairo_surface_t *surface)
633{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500634 cairo_t *cr;
635
636 cr = cairo_create (window->cairo_surface);
637
638 cairo_set_source_surface (cr,
639 surface,
640 rectangle->x, rectangle->y);
641
642 cairo_paint (cr);
643 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500644}
645
646void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500647window_set_fullscreen(struct window *window, int fullscreen)
648{
649 window->fullscreen = fullscreen;
650 if (window->fullscreen) {
651 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500652 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500653 } else {
654 window->allocation = window->saved_allocation;
655 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500656}
657
658void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400659window_set_decoration(struct window *window, int decoration)
660{
661 window->decoration = decoration;
662}
663
664void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500665window_set_resize_handler(struct window *window,
666 window_resize_handler_t handler, void *data)
667{
668 window->resize_handler = handler;
669 window->user_data = data;
670}
671
672void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500673window_set_key_handler(struct window *window,
674 window_key_handler_t handler, void *data)
675{
676 window->key_handler = handler;
677 window->user_data = data;
678}
679
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500680void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400681window_set_acknowledge_handler(struct window *window,
682 window_acknowledge_handler_t handler, void *data)
683{
684 window->acknowledge_handler = handler;
685 window->user_data = data;
686}
687
688void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400689window_set_frame_handler(struct window *window,
690 window_frame_handler_t handler, void *data)
691{
692 window->frame_handler = handler;
693 window->user_data = data;
694}
695
696void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500697window_set_keyboard_focus_handler(struct window *window,
698 window_keyboard_focus_handler_t handler, void *data)
699{
700 window->keyboard_focus_handler = handler;
701 window->user_data = data;
702}
703
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400704void
705window_move(struct window *window, int32_t x, int32_t y)
706{
707 window->allocation.x = x;
708 window->allocation.y = y;
709
710 wl_surface_map(window->surface,
711 window->allocation.x - window->margin,
712 window->allocation.y - window->margin,
713 window->allocation.width,
714 window->allocation.height);
715}
716
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500717struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500718window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500719 int32_t x, int32_t y, int32_t width, int32_t height)
720{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500721 struct window *window;
722
723 window = malloc(sizeof *window);
724 if (window == NULL)
725 return NULL;
726
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500727 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500728 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500729 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500730 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500731 window->allocation.x = x;
732 window->allocation.y = y;
733 window->allocation.width = width;
734 window->allocation.height = height;
735 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500736 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500737 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400738 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500739
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400740 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500741
742 wl_input_device_add_listener(display->input_device,
743 &input_device_listener, window);
744
745 return window;
746}
747
748static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400749display_handle_device(void *data,
750 struct wl_compositor *compositor,
751 const char *device)
752{
753 struct display *d = data;
754
755 d->device_name = strdup(device);
756}
757
758static void
759display_handle_acknowledge(void *data,
760 struct wl_compositor *compositor,
761 uint32_t key, uint32_t frame)
762{
763 struct display *d = data;
764 struct window *window;
765 cairo_surface_t *pending;
766
767 /* The acknowledge event means that the server processed our
768 * last commit request and we can now safely free the old
769 * window buffer if we resized and render the next frame into
770 * our back buffer.. */
771 wl_list_for_each(window, &d->window_list, link) {
772 pending = window->pending_surface;
773 window->pending_surface = NULL;
774 if (pending != window->cairo_surface)
775 window_attach_surface(window);
776 cairo_surface_destroy(pending);
777 if (window->acknowledge_handler)
778 (*window->acknowledge_handler)(window, key, frame, window->user_data);
779 }
780}
781
782static void
783display_handle_frame(void *data,
784 struct wl_compositor *compositor,
785 uint32_t frame, uint32_t timestamp)
786{
787 struct display *d = data;
788 struct window *window;
789
790 wl_list_for_each(window, &d->window_list, link) {
791 if (window->frame_handler)
792 (*window->frame_handler)(window, frame,
793 timestamp, window->user_data);
794 }
795}
796
797static const struct wl_compositor_listener compositor_listener = {
798 display_handle_device,
799 display_handle_acknowledge,
800 display_handle_frame,
801};
802
803static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500804display_handle_geometry(void *data,
805 struct wl_output *output,
806 int32_t width, int32_t height)
807{
808 struct display *display = data;
809
810 display->screen_allocation.x = 0;
811 display->screen_allocation.y = 0;
812 display->screen_allocation.width = width;
813 display->screen_allocation.height = height;
814}
815
816static const struct wl_output_listener output_listener = {
817 display_handle_geometry,
818};
819
820static void
821display_handle_global(struct wl_display *display,
822 struct wl_object *object, void *data)
823{
824 struct display *d = data;
825
826 if (wl_object_implements(object, "compositor", 1)) {
827 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400828 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500829 } else if (wl_object_implements(object, "output", 1)) {
830 d->output = (struct wl_output *) object;
831 wl_output_add_listener(d->output, &output_listener, d);
832 } else if (wl_object_implements(object, "input_device", 1)) {
833 d->input_device =(struct wl_input_device *) object;
834 }
835}
836
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400837static const char socket_name[] = "\0wayland";
838
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500839struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400840display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500841{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400842 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500843 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400844 EGLint major, minor, count;
845 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400846 int fd;
847 GOptionContext *context;
848 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400849
850 static const EGLint config_attribs[] = {
851 EGL_SURFACE_TYPE, 0,
852 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
853 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
854 EGL_NONE
855 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500856
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400857 g_type_init();
858
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400859 context = g_option_context_new(NULL);
860 if (option_entries) {
861 g_option_context_add_main_entries(context, option_entries, "Wayland View");
862 if (!g_option_context_parse(context, argc, argv, &error)) {
863 fprintf(stderr, "option parsing failed: %s\n", error->message);
864 exit(EXIT_FAILURE);
865 }
866 }
867
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500868 d = malloc(sizeof *d);
869 if (d == NULL)
870 return NULL;
871
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400872 d->display = wl_display_create(socket_name, sizeof socket_name);
873 if (d->display == NULL) {
874 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400875 return NULL;
876 }
877
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400878 /* Set up listener so we'll catch all events. */
879 wl_display_add_global_listener(d->display,
880 display_handle_global, d);
881
882 /* Process connection events. */
883 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
884
885 fd = open(d->device_name, O_RDWR);
886 if (fd < 0) {
887 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400888 return NULL;
889 }
890
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400891 get_typed_display_mesa =
892 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
893 if (get_typed_display_mesa == NULL) {
894 fprintf(stderr, "eglGetDisplayMESA() not found\n");
895 return NULL;
896 }
897
898 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
899 (void *) fd);
900 if (!eglInitialize(d->dpy, &major, &minor)) {
901 fprintf(stderr, "failed to initialize display\n");
902 return NULL;
903 }
904
905 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
906 count == 0) {
907 fprintf(stderr, "eglChooseConfig() failed\n");
908 return NULL;
909 }
910
911 eglBindAPI(EGL_OPENGL_API);
912
913 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
914 if (d->ctx == NULL) {
915 fprintf(stderr, "failed to create context\n");
916 return NULL;
917 }
918
919 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
920 fprintf(stderr, "faile to make context current\n");
921 return NULL;
922 }
923
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400924 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400925 if (d->device == NULL) {
926 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500927 return NULL;
928 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500929
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400930 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400931 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400932 g_source_attach(d->source, NULL);
933
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400934 wl_list_init(&d->window_list);
935
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500936 return d;
937}
938
939struct wl_compositor *
940display_get_compositor(struct display *display)
941{
942 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500943}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400944
945EGLDisplay
946display_get_egl_display(struct display *d)
947{
948 return d->dpy;
949}
950
951void
952display_run(struct display *d)
953{
954 g_main_loop_run(d->loop);
955}