blob: 6d423abd52b38f13610a6d299ac24ada24bfc292 [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øgsberga85fe3c2010-06-08 14:08:30 -040033
34#define EGL_EGLEXT_PROTOTYPES 1
35#define GL_GLEXT_PROTOTYPES 1
36#include <GL/gl.h>
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050040
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050041#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050042#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050044#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040045#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050046
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050048
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050049struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050050 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050051 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050052 struct wl_output *output;
53 struct wl_input_device *input_device;
54 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055 EGLDisplay dpy;
56 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010057 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040059 GMainLoop *loop;
60 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061};
62
63struct window {
64 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050065 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050069 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050070 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050071 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050072 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050074 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050075 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050076 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050077 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050078
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040079 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050080 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberga341fa02010-01-24 18:10:15 -050081 int new_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050082
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050083 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050084 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050085 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040086 window_frame_handler_t frame_handler;
87
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050088 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050089};
90
Kristian Høgsberge4feb562008-11-08 18:53:37 -050091static void
92rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
93{
94 cairo_move_to(cr, x0, y0 + radius);
95 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
96 cairo_line_to(cr, x1 - radius, y0);
97 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
98 cairo_line_to(cr, x1, y1 - radius);
99 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
100 cairo_line_to(cr, x0 + radius, y1);
101 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
102 cairo_close_path(cr);
103}
104
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400105static const cairo_user_data_key_t surface_data_key;
106struct surface_data {
107 EGLImageKHR image;
108 GLuint texture;
109 EGLDisplay dpy;
110};
111
112static void
113surface_data_destroy(void *p)
114{
115 struct surface_data *data = p;
116
117 glDeleteTextures(1, &data->texture);
118 eglDestroyImageKHR(data->dpy, data->image);
119}
120
121cairo_surface_t *
122window_create_surface(struct window *window,
123 struct rectangle *rectangle)
124{
125 struct surface_data *data;
126 EGLDisplay dpy = window->display->dpy;
127 cairo_surface_t *surface;
128
129 EGLint image_attribs[] = {
130 EGL_WIDTH, 0,
131 EGL_HEIGHT, 0,
132 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
133 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
134 EGL_NONE
135 };
136
137 data = malloc(sizeof *data);
138 image_attribs[1] = rectangle->width;
139 image_attribs[3] = rectangle->height;
140 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
141 glGenTextures(1, &data->texture);
142 data->dpy = dpy;
143 glBindTexture(GL_TEXTURE_2D, data->texture);
144 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
145
146 surface = cairo_gl_surface_create_for_texture(window->display->device,
147 CAIRO_CONTENT_COLOR_ALPHA,
148 data->texture,
149 rectangle->width,
150 rectangle->height);
151
152 cairo_surface_set_user_data (surface, &surface_data_key,
153 data, surface_data_destroy);
154
155 return surface;
156}
157
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500158static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500159window_attach_surface(struct window *window)
160{
161 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400162 struct surface_data *data;
163 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500164
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500165 if (window->pending_surface != NULL)
166 return;
167
168 window->pending_surface =
169 cairo_surface_reference(window->cairo_surface);
170
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400171 data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key);
172 eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride);
173
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500174 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
175 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400176 name,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500177 window->allocation.width,
178 window->allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400179 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500180 visual);
181
182 wl_surface_map(window->surface,
183 window->allocation.x - window->margin,
184 window->allocation.y - window->margin,
185 window->allocation.width,
186 window->allocation.height);
187}
188
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500189void
190window_commit(struct window *window, uint32_t key)
191{
192 if (window->new_surface) {
193 window_attach_surface(window);
194 window->new_surface = 0;
195 }
196
197 wl_compositor_commit(window->display->compositor, key);
198}
199
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500200static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500201window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500202{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500203 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500204 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500205 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500206 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500207 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400208 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500209
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500210 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400211 window_create_surface(window, &window->allocation);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500212
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500213 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500214 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500215 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
216
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500217 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500218
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500219 width = window->allocation.width - window->margin * 2;
220 height = window->allocation.height - window->margin * 2;
221
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400222 cairo_translate(cr, window->margin + shadow_dx,
223 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500224 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500225 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400226 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500227 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500228
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400229#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500230#ifdef SLOW_BUT_PWETTY
231 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
232 * Ideally we should have convolution filters in cairo, but we
233 * can also fallback to compositing the shadow image a bunch
234 * of times according to the blur kernel. */
235 {
236 cairo_surface_t *map;
237
238 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500239 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500240 cairo_drm_surface_unmap(window->cairo_surface, map);
241 }
242#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500243
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400244 cairo_translate(cr, -shadow_dx, -shadow_dy);
245 if (window->keyboard_device) {
246 rounded_rect(cr, 0, 0, width, height, radius);
247 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
248 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
249 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
250 cairo_set_source(cr, gradient);
251 cairo_fill(cr);
252 cairo_pattern_destroy(gradient);
253 } else {
254 rounded_rect(cr, 0, 0, width, height, radius);
255 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
256 cairo_fill(cr);
257 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500258
259 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
260 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500261 cairo_line_to(cr, width - 10, 50);
262 cairo_line_to(cr, width - 10, height - 10);
263 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500264 cairo_close_path(cr);
265 cairo_set_source(cr, dim);
266 cairo_stroke(cr);
267
268 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500269 cairo_line_to(cr, width - 10, 51);
270 cairo_line_to(cr, width - 10, height - 10);
271 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500272 cairo_close_path(cr);
273 cairo_set_source(cr, bright);
274 cairo_stroke(cr);
275
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500276 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
277 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500278 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500279 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500280 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
281 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
282 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500283 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400284 if (window->keyboard_device) {
285 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
286 cairo_stroke_preserve(cr);
287 cairo_set_source_rgb(cr, 1, 1, 1);
288 cairo_fill(cr);
289 } else {
290 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
291 cairo_fill(cr);
292 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500293 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500294}
295
296static void
297window_draw_fullscreen(struct window *window)
298{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500299 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400300 window_create_surface(window, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500301}
302
303void
304window_draw(struct window *window)
305{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500306 if (window->cairo_surface != NULL)
307 cairo_surface_destroy(window->cairo_surface);
308
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400309 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500310 window_draw_fullscreen(window);
311 else
312 window_draw_decorations(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500313
314 window->new_surface = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500315}
316
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400317cairo_surface_t *
318window_get_surface(struct window *window)
319{
320 return window->cairo_surface;
321}
322
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500323static void
324window_handle_acknowledge(void *data,
325 struct wl_compositor *compositor,
326 uint32_t key, uint32_t frame)
327{
328 struct window *window = data;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500329 cairo_surface_t *pending;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500330
331 /* The acknowledge event means that the server
332 * processed our last commit request and we can now
333 * safely free the old window buffer if we resized and
334 * render the next frame into our back buffer.. */
335
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500336 pending = window->pending_surface;
337 window->pending_surface = NULL;
338 if (pending != window->cairo_surface)
339 window_attach_surface(window);
340 cairo_surface_destroy(pending);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500341}
342
343static void
344window_handle_frame(void *data,
345 struct wl_compositor *compositor,
346 uint32_t frame, uint32_t timestamp)
347{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400348 struct window *window = data;
349
350 if (window->frame_handler)
351 (*window->frame_handler)(window, frame, timestamp, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352}
353
354static const struct wl_compositor_listener compositor_listener = {
355 window_handle_acknowledge,
356 window_handle_frame,
357};
358
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500359enum window_state {
360 WINDOW_STABLE,
361 WINDOW_MOVING,
362 WINDOW_RESIZING_UPPER_LEFT,
363 WINDOW_RESIZING_UPPER_RIGHT,
364 WINDOW_RESIZING_LOWER_LEFT,
365 WINDOW_RESIZING_LOWER_RIGHT
366};
367
368enum location {
369 LOCATION_INTERIOR,
370 LOCATION_UPPER_LEFT,
371 LOCATION_UPPER_RIGHT,
372 LOCATION_LOWER_LEFT,
373 LOCATION_LOWER_RIGHT,
374 LOCATION_OUTSIDE
375};
376
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500377static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500378window_handle_motion(void *data, struct wl_input_device *input_device,
379 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500380{
381 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500382
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500383 switch (window->state) {
384 case WINDOW_MOVING:
385 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500386 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500387 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500388 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500389 window->allocation.x = window->drag_x + x;
390 window->allocation.y = window->drag_y + y;
391 wl_surface_map(window->surface,
392 window->allocation.x - window->margin,
393 window->allocation.y - window->margin,
394 window->allocation.width,
395 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500396 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500397 break;
398 case WINDOW_RESIZING_LOWER_RIGHT:
399 if (window->fullscreen)
400 break;
401 if (window->grab_device != input_device)
402 break;
403 window->allocation.width = window->drag_x + x;
404 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500405
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500406 if (window->resize_handler)
407 (*window->resize_handler)(window,
408 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500409
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500410 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500411 }
412}
413
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500414static void window_handle_button(void *data, struct wl_input_device *input_device,
415 uint32_t button, uint32_t state,
416 int32_t x, int32_t y, int32_t sx, int32_t sy)
417{
418 struct window *window = data;
419 int32_t left = window->allocation.x;
420 int32_t right = window->allocation.x +
421 window->allocation.width - window->margin * 2;
422 int32_t top = window->allocation.y;
423 int32_t bottom = window->allocation.y +
424 window->allocation.height - window->margin * 2;
425 int grip_size = 16, location;
426
427 if (right - grip_size <= x && x < right &&
428 bottom - grip_size <= y && y < bottom) {
429 location = LOCATION_LOWER_RIGHT;
430 } else if (left <= x && x < right && top <= y && y < bottom) {
431 location = LOCATION_INTERIOR;
432 } else {
433 location = LOCATION_OUTSIDE;
434 }
435
436 if (button == BTN_LEFT && state == 1) {
437 switch (location) {
438 case LOCATION_INTERIOR:
439 window->drag_x = window->allocation.x - x;
440 window->drag_y = window->allocation.y - y;
441 window->state = WINDOW_MOVING;
442 window->grab_device = input_device;
443 break;
444 case LOCATION_LOWER_RIGHT:
445 window->drag_x = window->allocation.width - x;
446 window->drag_y = window->allocation.height - y;
447 window->state = WINDOW_RESIZING_LOWER_RIGHT;
448 window->grab_device = input_device;
449 break;
450 default:
451 window->state = WINDOW_STABLE;
452 break;
453 }
454 } else if (button == BTN_LEFT &&
455 state == 0 && window->grab_device == input_device) {
456 window->state = WINDOW_STABLE;
457 }
458}
459
Kristian Høgsberg55444912009-02-21 14:31:09 -0500460
461struct key {
462 uint32_t code[4];
463} evdev_keymap[] = {
464 { { 0, 0 } }, /* 0 */
465 { { 0x1b, 0x1b } },
466 { { '1', '!' } },
467 { { '2', '@' } },
468 { { '3', '#' } },
469 { { '4', '$' } },
470 { { '5', '%' } },
471 { { '6', '^' } },
472 { { '7', '&' } },
473 { { '8', '*' } },
474 { { '9', '(' } },
475 { { '0', ')' } },
476 { { '-', '_' } },
477 { { '=', '+' } },
478 { { '\b', '\b' } },
479 { { '\t', '\t' } },
480
481 { { 'q', 'Q', 0x11 } }, /* 16 */
482 { { 'w', 'W', 0x17 } },
483 { { 'e', 'E', 0x05 } },
484 { { 'r', 'R', 0x12 } },
485 { { 't', 'T', 0x14 } },
486 { { 'y', 'Y', 0x19 } },
487 { { 'u', 'U', 0x15 } },
488 { { 'i', 'I', 0x09 } },
489 { { 'o', 'O', 0x0f } },
490 { { 'p', 'P', 0x10 } },
491 { { '[', '{', 0x1b } },
492 { { ']', '}', 0x1d } },
493 { { '\n', '\n' } },
494 { { 0, 0 } },
495 { { 'a', 'A', 0x01} },
496 { { 's', 'S', 0x13 } },
497
498 { { 'd', 'D', 0x04 } }, /* 32 */
499 { { 'f', 'F', 0x06 } },
500 { { 'g', 'G', 0x07 } },
501 { { 'h', 'H', 0x08 } },
502 { { 'j', 'J', 0x0a } },
503 { { 'k', 'K', 0x0b } },
504 { { 'l', 'L', 0x0c } },
505 { { ';', ':' } },
506 { { '\'', '"' } },
507 { { '`', '~' } },
508 { { 0, 0 } },
509 { { '\\', '|', 0x1c } },
510 { { 'z', 'Z', 0x1a } },
511 { { 'x', 'X', 0x18 } },
512 { { 'c', 'C', 0x03 } },
513 { { 'v', 'V', 0x16 } },
514
515 { { 'b', 'B', 0x02 } }, /* 48 */
516 { { 'n', 'N', 0x0e } },
517 { { 'm', 'M', 0x0d } },
518 { { ',', '<' } },
519 { { '.', '>' } },
520 { { '/', '?' } },
521 { { 0, 0 } },
522 { { '*', '*' } },
523 { { 0, 0 } },
524 { { ' ', ' ' } },
525 { { 0, 0 } }
526
527 /* 59 */
528};
529
530#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
531
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500532static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500533window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500534{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500535 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500536
Kristian Høgsberg55444912009-02-21 14:31:09 -0500537 switch (key) {
538 case KEY_LEFTSHIFT:
539 case KEY_RIGHTSHIFT:
540 mod = WINDOW_MODIFIER_SHIFT;
541 break;
542 case KEY_LEFTCTRL:
543 case KEY_RIGHTCTRL:
544 mod = WINDOW_MODIFIER_CONTROL;
545 break;
546 case KEY_LEFTALT:
547 case KEY_RIGHTALT:
548 mod = WINDOW_MODIFIER_ALT;
549 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500550 }
551
552 if (state)
553 window->modifiers |= mod;
554 else
555 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500556}
557
558static void
559window_handle_key(void *data, struct wl_input_device *input_device,
560 uint32_t key, uint32_t state)
561{
562 struct window *window = data;
563 uint32_t unicode = 0;
564
565 if (window->keyboard_device != input_device)
566 return;
567
568 window_update_modifiers(window, key, state);
569
570 if (key < ARRAY_LENGTH(evdev_keymap)) {
571 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
572 unicode = evdev_keymap[key].code[2];
573 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
574 unicode = evdev_keymap[key].code[1];
575 else
576 unicode = evdev_keymap[key].code[0];
577 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500578
579 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500580 (*window->key_handler)(window, key, unicode,
581 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500582}
583
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500584static void
585window_handle_pointer_focus(void *data,
586 struct wl_input_device *input_device,
587 struct wl_surface *surface)
588{
589}
590
591static void
592window_handle_keyboard_focus(void *data,
593 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500594 struct wl_surface *surface,
595 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500596{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500597 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500598 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500599
600 if (window->keyboard_device == input_device && surface != window->surface)
601 window->keyboard_device = NULL;
602 else if (window->keyboard_device == NULL && surface == window->surface)
603 window->keyboard_device = input_device;
604 else
605 return;
606
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500607 if (window->keyboard_device) {
608 end = keys->data + keys->size;
609 for (k = keys->data; k < end; k++)
610 window_update_modifiers(window, *k, 1);
611 } else {
612 window->modifiers = 0;
613 }
614
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500615 if (window->keyboard_focus_handler)
616 (*window->keyboard_focus_handler)(window,
617 window->keyboard_device,
618 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500619}
620
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500621static const struct wl_input_device_listener input_device_listener = {
622 window_handle_motion,
623 window_handle_button,
624 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500625 window_handle_pointer_focus,
626 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500627};
628
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500629void
630window_get_child_rectangle(struct window *window,
631 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500632{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400633 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500634 *rectangle = window->allocation;
635 } else {
636 rectangle->x = window->margin + 10;
637 rectangle->y = window->margin + 50;
638 rectangle->width = window->allocation.width - 20 - window->margin * 2;
639 rectangle->height = window->allocation.height - 60 - window->margin * 2;
640 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500641}
642
643void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500644window_set_child_size(struct window *window,
645 struct rectangle *rectangle)
646{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500647 if (!window->fullscreen) {
648 window->allocation.width = rectangle->width + 20 + window->margin * 2;
649 window->allocation.height = rectangle->height + 60 + window->margin * 2;
650 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500651}
652
653void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400654window_copy_image(struct window *window,
655 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500656{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400657 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500658}
659
660void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500661window_copy_surface(struct window *window,
662 struct rectangle *rectangle,
663 cairo_surface_t *surface)
664{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500665 cairo_t *cr;
666
667 cr = cairo_create (window->cairo_surface);
668
669 cairo_set_source_surface (cr,
670 surface,
671 rectangle->x, rectangle->y);
672
673 cairo_paint (cr);
674 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500675}
676
677void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500678window_set_fullscreen(struct window *window, int fullscreen)
679{
680 window->fullscreen = fullscreen;
681 if (window->fullscreen) {
682 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500683 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500684 } else {
685 window->allocation = window->saved_allocation;
686 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500687}
688
689void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400690window_set_decoration(struct window *window, int decoration)
691{
692 window->decoration = decoration;
693}
694
695void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500696window_set_resize_handler(struct window *window,
697 window_resize_handler_t handler, void *data)
698{
699 window->resize_handler = handler;
700 window->user_data = data;
701}
702
703void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500704window_set_key_handler(struct window *window,
705 window_key_handler_t handler, void *data)
706{
707 window->key_handler = handler;
708 window->user_data = data;
709}
710
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500711void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400712window_set_frame_handler(struct window *window,
713 window_frame_handler_t handler, void *data)
714{
715 window->frame_handler = handler;
716 window->user_data = data;
717}
718
719void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500720window_set_keyboard_focus_handler(struct window *window,
721 window_keyboard_focus_handler_t handler, void *data)
722{
723 window->keyboard_focus_handler = handler;
724 window->user_data = data;
725}
726
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400727void
728window_move(struct window *window, int32_t x, int32_t y)
729{
730 window->allocation.x = x;
731 window->allocation.y = y;
732
733 wl_surface_map(window->surface,
734 window->allocation.x - window->margin,
735 window->allocation.y - window->margin,
736 window->allocation.width,
737 window->allocation.height);
738}
739
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500740struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500741window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500742 int32_t x, int32_t y, int32_t width, int32_t height)
743{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500744 struct window *window;
745
746 window = malloc(sizeof *window);
747 if (window == NULL)
748 return NULL;
749
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500750 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500751 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500752 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500753 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500754 window->allocation.x = x;
755 window->allocation.y = y;
756 window->allocation.width = width;
757 window->allocation.height = height;
758 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500759 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500760 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400761 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500762
763 wl_compositor_add_listener(display->compositor,
764 &compositor_listener, window);
765
766 wl_input_device_add_listener(display->input_device,
767 &input_device_listener, window);
768
769 return window;
770}
771
772static void
773display_handle_geometry(void *data,
774 struct wl_output *output,
775 int32_t width, int32_t height)
776{
777 struct display *display = data;
778
779 display->screen_allocation.x = 0;
780 display->screen_allocation.y = 0;
781 display->screen_allocation.width = width;
782 display->screen_allocation.height = height;
783}
784
785static const struct wl_output_listener output_listener = {
786 display_handle_geometry,
787};
788
789static void
790display_handle_global(struct wl_display *display,
791 struct wl_object *object, void *data)
792{
793 struct display *d = data;
794
795 if (wl_object_implements(object, "compositor", 1)) {
796 d->compositor = (struct wl_compositor *) object;
797 } else if (wl_object_implements(object, "output", 1)) {
798 d->output = (struct wl_output *) object;
799 wl_output_add_listener(d->output, &output_listener, d);
800 } else if (wl_object_implements(object, "input_device", 1)) {
801 d->input_device =(struct wl_input_device *) object;
802 }
803}
804
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400805static const char gem_device[] = "/dev/dri/card0";
806static const char socket_name[] = "\0wayland";
807
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500808struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400809display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500810{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400811 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500812 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400813 EGLint major, minor, count;
814 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400815 struct wl_display *display;
816 int fd;
817 GOptionContext *context;
818 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400819
820 static const EGLint config_attribs[] = {
821 EGL_SURFACE_TYPE, 0,
822 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
823 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
824 EGL_NONE
825 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500826
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400827 context = g_option_context_new(NULL);
828 if (option_entries) {
829 g_option_context_add_main_entries(context, option_entries, "Wayland View");
830 if (!g_option_context_parse(context, argc, argv, &error)) {
831 fprintf(stderr, "option parsing failed: %s\n", error->message);
832 exit(EXIT_FAILURE);
833 }
834 }
835
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500836 d = malloc(sizeof *d);
837 if (d == NULL)
838 return NULL;
839
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400840 fd = open(gem_device, O_RDWR);
841 if (fd < 0) {
842 fprintf(stderr, "drm open failed: %m\n");
843 return NULL;
844 }
845
846 display = wl_display_create(socket_name, sizeof socket_name);
847 if (display == NULL) {
848 fprintf(stderr, "failed to create display: %m\n");
849 return NULL;
850 }
851
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400852 get_typed_display_mesa =
853 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
854 if (get_typed_display_mesa == NULL) {
855 fprintf(stderr, "eglGetDisplayMESA() not found\n");
856 return NULL;
857 }
858
859 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
860 (void *) fd);
861 if (!eglInitialize(d->dpy, &major, &minor)) {
862 fprintf(stderr, "failed to initialize display\n");
863 return NULL;
864 }
865
866 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
867 count == 0) {
868 fprintf(stderr, "eglChooseConfig() failed\n");
869 return NULL;
870 }
871
872 eglBindAPI(EGL_OPENGL_API);
873
874 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
875 if (d->ctx == NULL) {
876 fprintf(stderr, "failed to create context\n");
877 return NULL;
878 }
879
880 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
881 fprintf(stderr, "faile to make context current\n");
882 return NULL;
883 }
884
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500885 d->display = display;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400886 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400887 if (d->device == NULL) {
888 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500889 return NULL;
890 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500891
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500892 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500893 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500894 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500895
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500896 /* Process connection events. */
897 wl_display_iterate(display, WL_DISPLAY_READABLE);
898
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400899 d->loop = g_main_loop_new(NULL, FALSE);
900 d->source = wl_glib_source_new(display);
901 g_source_attach(d->source, NULL);
902
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500903 return d;
904}
905
906struct wl_compositor *
907display_get_compositor(struct display *display)
908{
909 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500910}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400911
912EGLDisplay
913display_get_egl_display(struct display *d)
914{
915 return d->dpy;
916}
917
918void
919display_run(struct display *d)
920{
921 g_main_loop_run(d->loop);
922}