blob: a566c9fb1bfe6d0004d8efd1ff6eb6bfd02e10f7 [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;
59};
60
61struct window {
62 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050067 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050068 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050069 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050070 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040071 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050072 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050073 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050074 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050075 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050076
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040077 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050078 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberga341fa02010-01-24 18:10:15 -050079 int new_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050080
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050081 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050082 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050083 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040084 window_frame_handler_t frame_handler;
85
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050086 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050087};
88
Kristian Høgsberge4feb562008-11-08 18:53:37 -050089static void
90rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
91{
92 cairo_move_to(cr, x0, y0 + radius);
93 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
94 cairo_line_to(cr, x1 - radius, y0);
95 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
96 cairo_line_to(cr, x1, y1 - radius);
97 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
98 cairo_line_to(cr, x0 + radius, y1);
99 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
100 cairo_close_path(cr);
101}
102
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400103static const cairo_user_data_key_t surface_data_key;
104struct surface_data {
105 EGLImageKHR image;
106 GLuint texture;
107 EGLDisplay dpy;
108};
109
110static void
111surface_data_destroy(void *p)
112{
113 struct surface_data *data = p;
114
115 glDeleteTextures(1, &data->texture);
116 eglDestroyImageKHR(data->dpy, data->image);
117}
118
119cairo_surface_t *
120window_create_surface(struct window *window,
121 struct rectangle *rectangle)
122{
123 struct surface_data *data;
124 EGLDisplay dpy = window->display->dpy;
125 cairo_surface_t *surface;
126
127 EGLint image_attribs[] = {
128 EGL_WIDTH, 0,
129 EGL_HEIGHT, 0,
130 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
131 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
132 EGL_NONE
133 };
134
135 data = malloc(sizeof *data);
136 image_attribs[1] = rectangle->width;
137 image_attribs[3] = rectangle->height;
138 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
139 glGenTextures(1, &data->texture);
140 data->dpy = dpy;
141 glBindTexture(GL_TEXTURE_2D, data->texture);
142 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
143
144 surface = cairo_gl_surface_create_for_texture(window->display->device,
145 CAIRO_CONTENT_COLOR_ALPHA,
146 data->texture,
147 rectangle->width,
148 rectangle->height);
149
150 cairo_surface_set_user_data (surface, &surface_data_key,
151 data, surface_data_destroy);
152
153 return surface;
154}
155
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500156static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500157window_attach_surface(struct window *window)
158{
159 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400160 struct surface_data *data;
161 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500162
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500163 if (window->pending_surface != NULL)
164 return;
165
166 window->pending_surface =
167 cairo_surface_reference(window->cairo_surface);
168
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400169 data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key);
170 eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride);
171
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500172 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
173 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400174 name,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500175 window->allocation.width,
176 window->allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400177 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500178 visual);
179
180 wl_surface_map(window->surface,
181 window->allocation.x - window->margin,
182 window->allocation.y - window->margin,
183 window->allocation.width,
184 window->allocation.height);
185}
186
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500187void
188window_commit(struct window *window, uint32_t key)
189{
190 if (window->new_surface) {
191 window_attach_surface(window);
192 window->new_surface = 0;
193 }
194
195 wl_compositor_commit(window->display->compositor, key);
196}
197
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500198static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500199window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500200{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500201 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500202 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500203 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500204 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500205 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400206 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500207
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500208 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400209 window_create_surface(window, &window->allocation);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500210
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500211 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500212 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500213 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
214
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500215 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500216
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500217 width = window->allocation.width - window->margin * 2;
218 height = window->allocation.height - window->margin * 2;
219
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400220 cairo_translate(cr, window->margin + shadow_dx,
221 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500222 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500223 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400224 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500225 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500226
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400227#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500228#ifdef SLOW_BUT_PWETTY
229 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
230 * Ideally we should have convolution filters in cairo, but we
231 * can also fallback to compositing the shadow image a bunch
232 * of times according to the blur kernel. */
233 {
234 cairo_surface_t *map;
235
236 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500237 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500238 cairo_drm_surface_unmap(window->cairo_surface, map);
239 }
240#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500241
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400242 cairo_translate(cr, -shadow_dx, -shadow_dy);
243 if (window->keyboard_device) {
244 rounded_rect(cr, 0, 0, width, height, radius);
245 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
246 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
247 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
248 cairo_set_source(cr, gradient);
249 cairo_fill(cr);
250 cairo_pattern_destroy(gradient);
251 } else {
252 rounded_rect(cr, 0, 0, width, height, radius);
253 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
254 cairo_fill(cr);
255 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500256
257 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
258 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500259 cairo_line_to(cr, width - 10, 50);
260 cairo_line_to(cr, width - 10, height - 10);
261 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500262 cairo_close_path(cr);
263 cairo_set_source(cr, dim);
264 cairo_stroke(cr);
265
266 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500267 cairo_line_to(cr, width - 10, 51);
268 cairo_line_to(cr, width - 10, height - 10);
269 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500270 cairo_close_path(cr);
271 cairo_set_source(cr, bright);
272 cairo_stroke(cr);
273
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500274 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
275 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500276 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500277 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500278 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
279 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
280 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500281 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400282 if (window->keyboard_device) {
283 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
284 cairo_stroke_preserve(cr);
285 cairo_set_source_rgb(cr, 1, 1, 1);
286 cairo_fill(cr);
287 } else {
288 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
289 cairo_fill(cr);
290 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500291 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500292}
293
294static void
295window_draw_fullscreen(struct window *window)
296{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500297 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400298 window_create_surface(window, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500299}
300
301void
302window_draw(struct window *window)
303{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500304 if (window->cairo_surface != NULL)
305 cairo_surface_destroy(window->cairo_surface);
306
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400307 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500308 window_draw_fullscreen(window);
309 else
310 window_draw_decorations(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500311
312 window->new_surface = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500313}
314
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400315cairo_surface_t *
316window_get_surface(struct window *window)
317{
318 return window->cairo_surface;
319}
320
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500321static void
322window_handle_acknowledge(void *data,
323 struct wl_compositor *compositor,
324 uint32_t key, uint32_t frame)
325{
326 struct window *window = data;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500327 cairo_surface_t *pending;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500328
329 /* The acknowledge event means that the server
330 * processed our last commit request and we can now
331 * safely free the old window buffer if we resized and
332 * render the next frame into our back buffer.. */
333
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500334 pending = window->pending_surface;
335 window->pending_surface = NULL;
336 if (pending != window->cairo_surface)
337 window_attach_surface(window);
338 cairo_surface_destroy(pending);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500339}
340
341static void
342window_handle_frame(void *data,
343 struct wl_compositor *compositor,
344 uint32_t frame, uint32_t timestamp)
345{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400346 struct window *window = data;
347
348 if (window->frame_handler)
349 (*window->frame_handler)(window, frame, timestamp, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500350}
351
352static const struct wl_compositor_listener compositor_listener = {
353 window_handle_acknowledge,
354 window_handle_frame,
355};
356
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500357enum window_state {
358 WINDOW_STABLE,
359 WINDOW_MOVING,
360 WINDOW_RESIZING_UPPER_LEFT,
361 WINDOW_RESIZING_UPPER_RIGHT,
362 WINDOW_RESIZING_LOWER_LEFT,
363 WINDOW_RESIZING_LOWER_RIGHT
364};
365
366enum location {
367 LOCATION_INTERIOR,
368 LOCATION_UPPER_LEFT,
369 LOCATION_UPPER_RIGHT,
370 LOCATION_LOWER_LEFT,
371 LOCATION_LOWER_RIGHT,
372 LOCATION_OUTSIDE
373};
374
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500375static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500376window_handle_motion(void *data, struct wl_input_device *input_device,
377 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500378{
379 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500380
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500381 switch (window->state) {
382 case WINDOW_MOVING:
383 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500384 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500385 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500386 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500387 window->allocation.x = window->drag_x + x;
388 window->allocation.y = window->drag_y + y;
389 wl_surface_map(window->surface,
390 window->allocation.x - window->margin,
391 window->allocation.y - window->margin,
392 window->allocation.width,
393 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500394 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500395 break;
396 case WINDOW_RESIZING_LOWER_RIGHT:
397 if (window->fullscreen)
398 break;
399 if (window->grab_device != input_device)
400 break;
401 window->allocation.width = window->drag_x + x;
402 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500403
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500404 if (window->resize_handler)
405 (*window->resize_handler)(window,
406 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500407
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500408 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500409 }
410}
411
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500412static void window_handle_button(void *data, struct wl_input_device *input_device,
413 uint32_t button, uint32_t state,
414 int32_t x, int32_t y, int32_t sx, int32_t sy)
415{
416 struct window *window = data;
417 int32_t left = window->allocation.x;
418 int32_t right = window->allocation.x +
419 window->allocation.width - window->margin * 2;
420 int32_t top = window->allocation.y;
421 int32_t bottom = window->allocation.y +
422 window->allocation.height - window->margin * 2;
423 int grip_size = 16, location;
424
425 if (right - grip_size <= x && x < right &&
426 bottom - grip_size <= y && y < bottom) {
427 location = LOCATION_LOWER_RIGHT;
428 } else if (left <= x && x < right && top <= y && y < bottom) {
429 location = LOCATION_INTERIOR;
430 } else {
431 location = LOCATION_OUTSIDE;
432 }
433
434 if (button == BTN_LEFT && state == 1) {
435 switch (location) {
436 case LOCATION_INTERIOR:
437 window->drag_x = window->allocation.x - x;
438 window->drag_y = window->allocation.y - y;
439 window->state = WINDOW_MOVING;
440 window->grab_device = input_device;
441 break;
442 case LOCATION_LOWER_RIGHT:
443 window->drag_x = window->allocation.width - x;
444 window->drag_y = window->allocation.height - y;
445 window->state = WINDOW_RESIZING_LOWER_RIGHT;
446 window->grab_device = input_device;
447 break;
448 default:
449 window->state = WINDOW_STABLE;
450 break;
451 }
452 } else if (button == BTN_LEFT &&
453 state == 0 && window->grab_device == input_device) {
454 window->state = WINDOW_STABLE;
455 }
456}
457
Kristian Høgsberg55444912009-02-21 14:31:09 -0500458
459struct key {
460 uint32_t code[4];
461} evdev_keymap[] = {
462 { { 0, 0 } }, /* 0 */
463 { { 0x1b, 0x1b } },
464 { { '1', '!' } },
465 { { '2', '@' } },
466 { { '3', '#' } },
467 { { '4', '$' } },
468 { { '5', '%' } },
469 { { '6', '^' } },
470 { { '7', '&' } },
471 { { '8', '*' } },
472 { { '9', '(' } },
473 { { '0', ')' } },
474 { { '-', '_' } },
475 { { '=', '+' } },
476 { { '\b', '\b' } },
477 { { '\t', '\t' } },
478
479 { { 'q', 'Q', 0x11 } }, /* 16 */
480 { { 'w', 'W', 0x17 } },
481 { { 'e', 'E', 0x05 } },
482 { { 'r', 'R', 0x12 } },
483 { { 't', 'T', 0x14 } },
484 { { 'y', 'Y', 0x19 } },
485 { { 'u', 'U', 0x15 } },
486 { { 'i', 'I', 0x09 } },
487 { { 'o', 'O', 0x0f } },
488 { { 'p', 'P', 0x10 } },
489 { { '[', '{', 0x1b } },
490 { { ']', '}', 0x1d } },
491 { { '\n', '\n' } },
492 { { 0, 0 } },
493 { { 'a', 'A', 0x01} },
494 { { 's', 'S', 0x13 } },
495
496 { { 'd', 'D', 0x04 } }, /* 32 */
497 { { 'f', 'F', 0x06 } },
498 { { 'g', 'G', 0x07 } },
499 { { 'h', 'H', 0x08 } },
500 { { 'j', 'J', 0x0a } },
501 { { 'k', 'K', 0x0b } },
502 { { 'l', 'L', 0x0c } },
503 { { ';', ':' } },
504 { { '\'', '"' } },
505 { { '`', '~' } },
506 { { 0, 0 } },
507 { { '\\', '|', 0x1c } },
508 { { 'z', 'Z', 0x1a } },
509 { { 'x', 'X', 0x18 } },
510 { { 'c', 'C', 0x03 } },
511 { { 'v', 'V', 0x16 } },
512
513 { { 'b', 'B', 0x02 } }, /* 48 */
514 { { 'n', 'N', 0x0e } },
515 { { 'm', 'M', 0x0d } },
516 { { ',', '<' } },
517 { { '.', '>' } },
518 { { '/', '?' } },
519 { { 0, 0 } },
520 { { '*', '*' } },
521 { { 0, 0 } },
522 { { ' ', ' ' } },
523 { { 0, 0 } }
524
525 /* 59 */
526};
527
528#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
529
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500530static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500531window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500532{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500533 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500534
Kristian Høgsberg55444912009-02-21 14:31:09 -0500535 switch (key) {
536 case KEY_LEFTSHIFT:
537 case KEY_RIGHTSHIFT:
538 mod = WINDOW_MODIFIER_SHIFT;
539 break;
540 case KEY_LEFTCTRL:
541 case KEY_RIGHTCTRL:
542 mod = WINDOW_MODIFIER_CONTROL;
543 break;
544 case KEY_LEFTALT:
545 case KEY_RIGHTALT:
546 mod = WINDOW_MODIFIER_ALT;
547 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500548 }
549
550 if (state)
551 window->modifiers |= mod;
552 else
553 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500554}
555
556static void
557window_handle_key(void *data, struct wl_input_device *input_device,
558 uint32_t key, uint32_t state)
559{
560 struct window *window = data;
561 uint32_t unicode = 0;
562
563 if (window->keyboard_device != input_device)
564 return;
565
566 window_update_modifiers(window, key, state);
567
568 if (key < ARRAY_LENGTH(evdev_keymap)) {
569 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
570 unicode = evdev_keymap[key].code[2];
571 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
572 unicode = evdev_keymap[key].code[1];
573 else
574 unicode = evdev_keymap[key].code[0];
575 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500576
577 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500578 (*window->key_handler)(window, key, unicode,
579 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500580}
581
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500582static void
583window_handle_pointer_focus(void *data,
584 struct wl_input_device *input_device,
585 struct wl_surface *surface)
586{
587}
588
589static void
590window_handle_keyboard_focus(void *data,
591 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500592 struct wl_surface *surface,
593 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500594{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500595 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500596 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500597
598 if (window->keyboard_device == input_device && surface != window->surface)
599 window->keyboard_device = NULL;
600 else if (window->keyboard_device == NULL && surface == window->surface)
601 window->keyboard_device = input_device;
602 else
603 return;
604
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500605 if (window->keyboard_device) {
606 end = keys->data + keys->size;
607 for (k = keys->data; k < end; k++)
608 window_update_modifiers(window, *k, 1);
609 } else {
610 window->modifiers = 0;
611 }
612
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500613 if (window->keyboard_focus_handler)
614 (*window->keyboard_focus_handler)(window,
615 window->keyboard_device,
616 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500617}
618
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500619static const struct wl_input_device_listener input_device_listener = {
620 window_handle_motion,
621 window_handle_button,
622 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500623 window_handle_pointer_focus,
624 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500625};
626
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500627void
628window_get_child_rectangle(struct window *window,
629 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500630{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400631 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500632 *rectangle = window->allocation;
633 } else {
634 rectangle->x = window->margin + 10;
635 rectangle->y = window->margin + 50;
636 rectangle->width = window->allocation.width - 20 - window->margin * 2;
637 rectangle->height = window->allocation.height - 60 - window->margin * 2;
638 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500639}
640
641void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500642window_set_child_size(struct window *window,
643 struct rectangle *rectangle)
644{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500645 if (!window->fullscreen) {
646 window->allocation.width = rectangle->width + 20 + window->margin * 2;
647 window->allocation.height = rectangle->height + 60 + window->margin * 2;
648 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500649}
650
651void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400652window_copy_image(struct window *window,
653 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500654{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400655 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500656}
657
658void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500659window_copy_surface(struct window *window,
660 struct rectangle *rectangle,
661 cairo_surface_t *surface)
662{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500663 cairo_t *cr;
664
665 cr = cairo_create (window->cairo_surface);
666
667 cairo_set_source_surface (cr,
668 surface,
669 rectangle->x, rectangle->y);
670
671 cairo_paint (cr);
672 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500673}
674
675void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500676window_set_fullscreen(struct window *window, int fullscreen)
677{
678 window->fullscreen = fullscreen;
679 if (window->fullscreen) {
680 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500681 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500682 } else {
683 window->allocation = window->saved_allocation;
684 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500685}
686
687void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400688window_set_decoration(struct window *window, int decoration)
689{
690 window->decoration = decoration;
691}
692
693void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500694window_set_resize_handler(struct window *window,
695 window_resize_handler_t handler, void *data)
696{
697 window->resize_handler = handler;
698 window->user_data = data;
699}
700
701void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500702window_set_key_handler(struct window *window,
703 window_key_handler_t handler, void *data)
704{
705 window->key_handler = handler;
706 window->user_data = data;
707}
708
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500709void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400710window_set_frame_handler(struct window *window,
711 window_frame_handler_t handler, void *data)
712{
713 window->frame_handler = handler;
714 window->user_data = data;
715}
716
717void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500718window_set_keyboard_focus_handler(struct window *window,
719 window_keyboard_focus_handler_t handler, void *data)
720{
721 window->keyboard_focus_handler = handler;
722 window->user_data = data;
723}
724
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400725void
726window_move(struct window *window, int32_t x, int32_t y)
727{
728 window->allocation.x = x;
729 window->allocation.y = y;
730
731 wl_surface_map(window->surface,
732 window->allocation.x - window->margin,
733 window->allocation.y - window->margin,
734 window->allocation.width,
735 window->allocation.height);
736}
737
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500738struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500739window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500740 int32_t x, int32_t y, int32_t width, int32_t height)
741{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500742 struct window *window;
743
744 window = malloc(sizeof *window);
745 if (window == NULL)
746 return NULL;
747
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500748 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500749 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500750 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500751 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500752 window->allocation.x = x;
753 window->allocation.y = y;
754 window->allocation.width = width;
755 window->allocation.height = height;
756 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500757 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500758 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500759
760 wl_compositor_add_listener(display->compositor,
761 &compositor_listener, window);
762
763 wl_input_device_add_listener(display->input_device,
764 &input_device_listener, window);
765
766 return window;
767}
768
769static void
770display_handle_geometry(void *data,
771 struct wl_output *output,
772 int32_t width, int32_t height)
773{
774 struct display *display = data;
775
776 display->screen_allocation.x = 0;
777 display->screen_allocation.y = 0;
778 display->screen_allocation.width = width;
779 display->screen_allocation.height = height;
780}
781
782static const struct wl_output_listener output_listener = {
783 display_handle_geometry,
784};
785
786static void
787display_handle_global(struct wl_display *display,
788 struct wl_object *object, void *data)
789{
790 struct display *d = data;
791
792 if (wl_object_implements(object, "compositor", 1)) {
793 d->compositor = (struct wl_compositor *) object;
794 } else if (wl_object_implements(object, "output", 1)) {
795 d->output = (struct wl_output *) object;
796 wl_output_add_listener(d->output, &output_listener, d);
797 } else if (wl_object_implements(object, "input_device", 1)) {
798 d->input_device =(struct wl_input_device *) object;
799 }
800}
801
802struct display *
803display_create(struct wl_display *display, int fd)
804{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400805 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500806 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400807 EGLint major, minor, count;
808 EGLConfig config;
809
810 static const EGLint config_attribs[] = {
811 EGL_SURFACE_TYPE, 0,
812 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
813 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
814 EGL_NONE
815 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500816
817 d = malloc(sizeof *d);
818 if (d == NULL)
819 return NULL;
820
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400821 get_typed_display_mesa =
822 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
823 if (get_typed_display_mesa == NULL) {
824 fprintf(stderr, "eglGetDisplayMESA() not found\n");
825 return NULL;
826 }
827
828 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
829 (void *) fd);
830 if (!eglInitialize(d->dpy, &major, &minor)) {
831 fprintf(stderr, "failed to initialize display\n");
832 return NULL;
833 }
834
835 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
836 count == 0) {
837 fprintf(stderr, "eglChooseConfig() failed\n");
838 return NULL;
839 }
840
841 eglBindAPI(EGL_OPENGL_API);
842
843 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
844 if (d->ctx == NULL) {
845 fprintf(stderr, "failed to create context\n");
846 return NULL;
847 }
848
849 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
850 fprintf(stderr, "faile to make context current\n");
851 return NULL;
852 }
853
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500854 d->display = display;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400855 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400856 if (d->device == NULL) {
857 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500858 return NULL;
859 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500860
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500861 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500863 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500865 /* Process connection events. */
866 wl_display_iterate(display, WL_DISPLAY_READABLE);
867
868 return d;
869}
870
871struct wl_compositor *
872display_get_compositor(struct display *display)
873{
874 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500875}