blob: 8008017daf2c6dbaff64eebae694d0e0ef0f11ef [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øgsberg0ac16f02009-01-15 11:37:43 -050033#include <cairo-drm.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050035#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050036#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050038#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040039#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050040
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050042
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050043struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050044 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050045 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050046 struct wl_output *output;
47 struct wl_input_device *input_device;
48 struct rectangle screen_allocation;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010049 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050050 int fd;
51};
52
53struct window {
54 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050055 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050057 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050060 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050062 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050063 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050064 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050065 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050066 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050067
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050068 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberga341fa02010-01-24 18:10:15 -050069 int new_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050070
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050071 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050072 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050073 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050075};
76
Kristian Høgsberge4feb562008-11-08 18:53:37 -050077static void
78rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
79{
80 cairo_move_to(cr, x0, y0 + radius);
81 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
82 cairo_line_to(cr, x1 - radius, y0);
83 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
84 cairo_line_to(cr, x1, y1 - radius);
85 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
86 cairo_line_to(cr, x0 + radius, y1);
87 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
88 cairo_close_path(cr);
89}
90
Kristian Høgsberg0395f302008-12-22 12:14:50 -050091static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050092window_attach_surface(struct window *window)
93{
94 struct wl_visual *visual;
95
Kristian Høgsberg2aac3022009-12-21 10:04:53 -050096 if (window->pending_surface != NULL)
97 return;
98
99 window->pending_surface =
100 cairo_surface_reference(window->cairo_surface);
101
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500102 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
103 wl_surface_attach(window->surface,
104 cairo_drm_surface_get_name(window->cairo_surface),
105 window->allocation.width,
106 window->allocation.height,
107 cairo_drm_surface_get_stride(window->cairo_surface),
108 visual);
109
110 wl_surface_map(window->surface,
111 window->allocation.x - window->margin,
112 window->allocation.y - window->margin,
113 window->allocation.width,
114 window->allocation.height);
115}
116
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500117void
118window_commit(struct window *window, uint32_t key)
119{
120 if (window->new_surface) {
121 window_attach_surface(window);
122 window->new_surface = 0;
123 }
124
125 wl_compositor_commit(window->display->compositor, key);
126}
127
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500129window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500130{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500131 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500132 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500133 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500134 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500135 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400136 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500138 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400139 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500140 CAIRO_CONTENT_COLOR_ALPHA,
141 window->allocation.width,
142 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500143
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500144 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500145 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500146 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
147
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500148 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500149
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500150 width = window->allocation.width - window->margin * 2;
151 height = window->allocation.height - window->margin * 2;
152
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400153 cairo_translate(cr, window->margin + shadow_dx,
154 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500155 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500156 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400157 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500158 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500159
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400160#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500161#ifdef SLOW_BUT_PWETTY
162 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
163 * Ideally we should have convolution filters in cairo, but we
164 * can also fallback to compositing the shadow image a bunch
165 * of times according to the blur kernel. */
166 {
167 cairo_surface_t *map;
168
169 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500170 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500171 cairo_drm_surface_unmap(window->cairo_surface, map);
172 }
173#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500174
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400175 cairo_translate(cr, -shadow_dx, -shadow_dy);
176 if (window->keyboard_device) {
177 rounded_rect(cr, 0, 0, width, height, radius);
178 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
179 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
180 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
181 cairo_set_source(cr, gradient);
182 cairo_fill(cr);
183 cairo_pattern_destroy(gradient);
184 } else {
185 rounded_rect(cr, 0, 0, width, height, radius);
186 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
187 cairo_fill(cr);
188 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500189
190 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
191 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500192 cairo_line_to(cr, width - 10, 50);
193 cairo_line_to(cr, width - 10, height - 10);
194 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500195 cairo_close_path(cr);
196 cairo_set_source(cr, dim);
197 cairo_stroke(cr);
198
199 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500200 cairo_line_to(cr, width - 10, 51);
201 cairo_line_to(cr, width - 10, height - 10);
202 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500203 cairo_close_path(cr);
204 cairo_set_source(cr, bright);
205 cairo_stroke(cr);
206
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500207 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
208 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500209 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500210 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500211 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
212 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
213 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500214 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400215 if (window->keyboard_device) {
216 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
217 cairo_stroke_preserve(cr);
218 cairo_set_source_rgb(cr, 1, 1, 1);
219 cairo_fill(cr);
220 } else {
221 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
222 cairo_fill(cr);
223 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500224 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500225}
226
227static void
228window_draw_fullscreen(struct window *window)
229{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500230 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400231 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500232 CAIRO_CONTENT_COLOR_ALPHA,
233 window->allocation.width,
234 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500235}
236
237void
238window_draw(struct window *window)
239{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500240 if (window->cairo_surface != NULL)
241 cairo_surface_destroy(window->cairo_surface);
242
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500243 if (window->fullscreen)
244 window_draw_fullscreen(window);
245 else
246 window_draw_decorations(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500247
248 window->new_surface = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500249}
250
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500251static void
252window_handle_acknowledge(void *data,
253 struct wl_compositor *compositor,
254 uint32_t key, uint32_t frame)
255{
256 struct window *window = data;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500257 cairo_surface_t *pending;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500258
259 /* The acknowledge event means that the server
260 * processed our last commit request and we can now
261 * safely free the old window buffer if we resized and
262 * render the next frame into our back buffer.. */
263
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500264 pending = window->pending_surface;
265 window->pending_surface = NULL;
266 if (pending != window->cairo_surface)
267 window_attach_surface(window);
268 cairo_surface_destroy(pending);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500269}
270
271static void
272window_handle_frame(void *data,
273 struct wl_compositor *compositor,
274 uint32_t frame, uint32_t timestamp)
275{
276}
277
278static const struct wl_compositor_listener compositor_listener = {
279 window_handle_acknowledge,
280 window_handle_frame,
281};
282
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500283enum window_state {
284 WINDOW_STABLE,
285 WINDOW_MOVING,
286 WINDOW_RESIZING_UPPER_LEFT,
287 WINDOW_RESIZING_UPPER_RIGHT,
288 WINDOW_RESIZING_LOWER_LEFT,
289 WINDOW_RESIZING_LOWER_RIGHT
290};
291
292enum location {
293 LOCATION_INTERIOR,
294 LOCATION_UPPER_LEFT,
295 LOCATION_UPPER_RIGHT,
296 LOCATION_LOWER_LEFT,
297 LOCATION_LOWER_RIGHT,
298 LOCATION_OUTSIDE
299};
300
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500301static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500302window_handle_motion(void *data, struct wl_input_device *input_device,
303 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500304{
305 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500306
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500307 switch (window->state) {
308 case WINDOW_MOVING:
309 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500310 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500311 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500312 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313 window->allocation.x = window->drag_x + x;
314 window->allocation.y = window->drag_y + y;
315 wl_surface_map(window->surface,
316 window->allocation.x - window->margin,
317 window->allocation.y - window->margin,
318 window->allocation.width,
319 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500320 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500321 break;
322 case WINDOW_RESIZING_LOWER_RIGHT:
323 if (window->fullscreen)
324 break;
325 if (window->grab_device != input_device)
326 break;
327 window->allocation.width = window->drag_x + x;
328 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500329
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500330 if (window->resize_handler)
331 (*window->resize_handler)(window,
332 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500333
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500334 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500335 }
336}
337
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500338static void window_handle_button(void *data, struct wl_input_device *input_device,
339 uint32_t button, uint32_t state,
340 int32_t x, int32_t y, int32_t sx, int32_t sy)
341{
342 struct window *window = data;
343 int32_t left = window->allocation.x;
344 int32_t right = window->allocation.x +
345 window->allocation.width - window->margin * 2;
346 int32_t top = window->allocation.y;
347 int32_t bottom = window->allocation.y +
348 window->allocation.height - window->margin * 2;
349 int grip_size = 16, location;
350
351 if (right - grip_size <= x && x < right &&
352 bottom - grip_size <= y && y < bottom) {
353 location = LOCATION_LOWER_RIGHT;
354 } else if (left <= x && x < right && top <= y && y < bottom) {
355 location = LOCATION_INTERIOR;
356 } else {
357 location = LOCATION_OUTSIDE;
358 }
359
360 if (button == BTN_LEFT && state == 1) {
361 switch (location) {
362 case LOCATION_INTERIOR:
363 window->drag_x = window->allocation.x - x;
364 window->drag_y = window->allocation.y - y;
365 window->state = WINDOW_MOVING;
366 window->grab_device = input_device;
367 break;
368 case LOCATION_LOWER_RIGHT:
369 window->drag_x = window->allocation.width - x;
370 window->drag_y = window->allocation.height - y;
371 window->state = WINDOW_RESIZING_LOWER_RIGHT;
372 window->grab_device = input_device;
373 break;
374 default:
375 window->state = WINDOW_STABLE;
376 break;
377 }
378 } else if (button == BTN_LEFT &&
379 state == 0 && window->grab_device == input_device) {
380 window->state = WINDOW_STABLE;
381 }
382}
383
Kristian Høgsberg55444912009-02-21 14:31:09 -0500384
385struct key {
386 uint32_t code[4];
387} evdev_keymap[] = {
388 { { 0, 0 } }, /* 0 */
389 { { 0x1b, 0x1b } },
390 { { '1', '!' } },
391 { { '2', '@' } },
392 { { '3', '#' } },
393 { { '4', '$' } },
394 { { '5', '%' } },
395 { { '6', '^' } },
396 { { '7', '&' } },
397 { { '8', '*' } },
398 { { '9', '(' } },
399 { { '0', ')' } },
400 { { '-', '_' } },
401 { { '=', '+' } },
402 { { '\b', '\b' } },
403 { { '\t', '\t' } },
404
405 { { 'q', 'Q', 0x11 } }, /* 16 */
406 { { 'w', 'W', 0x17 } },
407 { { 'e', 'E', 0x05 } },
408 { { 'r', 'R', 0x12 } },
409 { { 't', 'T', 0x14 } },
410 { { 'y', 'Y', 0x19 } },
411 { { 'u', 'U', 0x15 } },
412 { { 'i', 'I', 0x09 } },
413 { { 'o', 'O', 0x0f } },
414 { { 'p', 'P', 0x10 } },
415 { { '[', '{', 0x1b } },
416 { { ']', '}', 0x1d } },
417 { { '\n', '\n' } },
418 { { 0, 0 } },
419 { { 'a', 'A', 0x01} },
420 { { 's', 'S', 0x13 } },
421
422 { { 'd', 'D', 0x04 } }, /* 32 */
423 { { 'f', 'F', 0x06 } },
424 { { 'g', 'G', 0x07 } },
425 { { 'h', 'H', 0x08 } },
426 { { 'j', 'J', 0x0a } },
427 { { 'k', 'K', 0x0b } },
428 { { 'l', 'L', 0x0c } },
429 { { ';', ':' } },
430 { { '\'', '"' } },
431 { { '`', '~' } },
432 { { 0, 0 } },
433 { { '\\', '|', 0x1c } },
434 { { 'z', 'Z', 0x1a } },
435 { { 'x', 'X', 0x18 } },
436 { { 'c', 'C', 0x03 } },
437 { { 'v', 'V', 0x16 } },
438
439 { { 'b', 'B', 0x02 } }, /* 48 */
440 { { 'n', 'N', 0x0e } },
441 { { 'm', 'M', 0x0d } },
442 { { ',', '<' } },
443 { { '.', '>' } },
444 { { '/', '?' } },
445 { { 0, 0 } },
446 { { '*', '*' } },
447 { { 0, 0 } },
448 { { ' ', ' ' } },
449 { { 0, 0 } }
450
451 /* 59 */
452};
453
454#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
455
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500456static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500457window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500458{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500459 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500460
Kristian Høgsberg55444912009-02-21 14:31:09 -0500461 switch (key) {
462 case KEY_LEFTSHIFT:
463 case KEY_RIGHTSHIFT:
464 mod = WINDOW_MODIFIER_SHIFT;
465 break;
466 case KEY_LEFTCTRL:
467 case KEY_RIGHTCTRL:
468 mod = WINDOW_MODIFIER_CONTROL;
469 break;
470 case KEY_LEFTALT:
471 case KEY_RIGHTALT:
472 mod = WINDOW_MODIFIER_ALT;
473 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500474 }
475
476 if (state)
477 window->modifiers |= mod;
478 else
479 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500480}
481
482static void
483window_handle_key(void *data, struct wl_input_device *input_device,
484 uint32_t key, uint32_t state)
485{
486 struct window *window = data;
487 uint32_t unicode = 0;
488
489 if (window->keyboard_device != input_device)
490 return;
491
492 window_update_modifiers(window, key, state);
493
494 if (key < ARRAY_LENGTH(evdev_keymap)) {
495 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
496 unicode = evdev_keymap[key].code[2];
497 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
498 unicode = evdev_keymap[key].code[1];
499 else
500 unicode = evdev_keymap[key].code[0];
501 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500502
503 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500504 (*window->key_handler)(window, key, unicode,
505 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500506}
507
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500508static void
509window_handle_pointer_focus(void *data,
510 struct wl_input_device *input_device,
511 struct wl_surface *surface)
512{
513}
514
515static void
516window_handle_keyboard_focus(void *data,
517 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500518 struct wl_surface *surface,
519 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500520{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500521 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500522 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500523
524 if (window->keyboard_device == input_device && surface != window->surface)
525 window->keyboard_device = NULL;
526 else if (window->keyboard_device == NULL && surface == window->surface)
527 window->keyboard_device = input_device;
528 else
529 return;
530
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500531 if (window->keyboard_device) {
532 end = keys->data + keys->size;
533 for (k = keys->data; k < end; k++)
534 window_update_modifiers(window, *k, 1);
535 } else {
536 window->modifiers = 0;
537 }
538
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500539 if (window->keyboard_focus_handler)
540 (*window->keyboard_focus_handler)(window,
541 window->keyboard_device,
542 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500543}
544
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500545static const struct wl_input_device_listener input_device_listener = {
546 window_handle_motion,
547 window_handle_button,
548 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500549 window_handle_pointer_focus,
550 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500551};
552
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500553void
554window_get_child_rectangle(struct window *window,
555 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500556{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500557 if (window->fullscreen) {
558 *rectangle = window->allocation;
559 } else {
560 rectangle->x = window->margin + 10;
561 rectangle->y = window->margin + 50;
562 rectangle->width = window->allocation.width - 20 - window->margin * 2;
563 rectangle->height = window->allocation.height - 60 - window->margin * 2;
564 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500565}
566
567void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500568window_set_child_size(struct window *window,
569 struct rectangle *rectangle)
570{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500571 if (!window->fullscreen) {
572 window->allocation.width = rectangle->width + 20 + window->margin * 2;
573 window->allocation.height = rectangle->height + 60 + window->margin * 2;
574 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500575}
576
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500577cairo_surface_t *
578window_create_surface(struct window *window,
579 struct rectangle *rectangle)
580{
Kristian Høgsberg26449102009-05-28 20:23:31 -0400581 return cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500582 CAIRO_CONTENT_COLOR_ALPHA,
583 rectangle->width,
584 rectangle->height);
585}
586
Kristian Høgsberg22106762008-12-08 13:50:07 -0500587void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500588window_copy(struct window *window,
589 struct rectangle *rectangle,
590 uint32_t name, uint32_t stride)
591{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500592 cairo_surface_t *surface;
593 cairo_t *cr;
594
595 surface = cairo_drm_surface_create_for_name (window->display->device,
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500596 name, CAIRO_FORMAT_ARGB32,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500597 rectangle->width, rectangle->height,
598 stride);
599
600 cr = cairo_create (window->cairo_surface);
601
602 cairo_set_source_surface (cr,
603 surface,
604 rectangle->x, rectangle->y);
605
606 cairo_paint (cr);
607 cairo_destroy (cr);
608 cairo_surface_destroy (surface);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500609}
610
611void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500612window_copy_surface(struct window *window,
613 struct rectangle *rectangle,
614 cairo_surface_t *surface)
615{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500616 cairo_t *cr;
617
618 cr = cairo_create (window->cairo_surface);
619
620 cairo_set_source_surface (cr,
621 surface,
622 rectangle->x, rectangle->y);
623
624 cairo_paint (cr);
625 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500626}
627
628void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500629window_set_fullscreen(struct window *window, int fullscreen)
630{
631 window->fullscreen = fullscreen;
632 if (window->fullscreen) {
633 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500634 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500635 } else {
636 window->allocation = window->saved_allocation;
637 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500638}
639
640void
641window_set_resize_handler(struct window *window,
642 window_resize_handler_t handler, void *data)
643{
644 window->resize_handler = handler;
645 window->user_data = data;
646}
647
648void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500649window_set_key_handler(struct window *window,
650 window_key_handler_t handler, void *data)
651{
652 window->key_handler = handler;
653 window->user_data = data;
654}
655
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500656void
657window_set_keyboard_focus_handler(struct window *window,
658 window_keyboard_focus_handler_t handler, void *data)
659{
660 window->keyboard_focus_handler = handler;
661 window->user_data = data;
662}
663
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500664struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500665window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500666 int32_t x, int32_t y, int32_t width, int32_t height)
667{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500668 struct window *window;
669
670 window = malloc(sizeof *window);
671 if (window == NULL)
672 return NULL;
673
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500674 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500675 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500676 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500677 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500678 window->allocation.x = x;
679 window->allocation.y = y;
680 window->allocation.width = width;
681 window->allocation.height = height;
682 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500683 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500684 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500685
686 wl_compositor_add_listener(display->compositor,
687 &compositor_listener, window);
688
689 wl_input_device_add_listener(display->input_device,
690 &input_device_listener, window);
691
692 return window;
693}
694
695static void
696display_handle_geometry(void *data,
697 struct wl_output *output,
698 int32_t width, int32_t height)
699{
700 struct display *display = data;
701
702 display->screen_allocation.x = 0;
703 display->screen_allocation.y = 0;
704 display->screen_allocation.width = width;
705 display->screen_allocation.height = height;
706}
707
708static const struct wl_output_listener output_listener = {
709 display_handle_geometry,
710};
711
712static void
713display_handle_global(struct wl_display *display,
714 struct wl_object *object, void *data)
715{
716 struct display *d = data;
717
718 if (wl_object_implements(object, "compositor", 1)) {
719 d->compositor = (struct wl_compositor *) object;
720 } else if (wl_object_implements(object, "output", 1)) {
721 d->output = (struct wl_output *) object;
722 wl_output_add_listener(d->output, &output_listener, d);
723 } else if (wl_object_implements(object, "input_device", 1)) {
724 d->input_device =(struct wl_input_device *) object;
725 }
726}
727
728struct display *
729display_create(struct wl_display *display, int fd)
730{
731 struct display *d;
732
733 d = malloc(sizeof *d);
734 if (d == NULL)
735 return NULL;
736
737 d->display = display;
Kristian Høgsberg26449102009-05-28 20:23:31 -0400738 d->device = cairo_drm_device_get_for_fd(fd);
739 if (d->device == NULL) {
740 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500741 return NULL;
742 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500743
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500744 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500745 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500746 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500747
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500748 /* Process connection events. */
749 wl_display_iterate(display, WL_DISPLAY_READABLE);
750
751 return d;
752}
753
754struct wl_compositor *
755display_get_compositor(struct display *display)
756{
757 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500758}