blob: e27ff32c53991b25852dda94b5db1e073cf86edd [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øgsberg0acc6c42009-03-05 07:49:42 -050039#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;
Kristian Høgsberg26449102009-05-28 20:23:31 -040049 cairo_drm_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øgsberg8a9cda82008-11-03 15:31:30 -050069
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050071 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050072 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050074};
75
Kristian Høgsberge4feb562008-11-08 18:53:37 -050076static void
77rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
78{
79 cairo_move_to(cr, x0, y0 + radius);
80 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
81 cairo_line_to(cr, x1 - radius, y0);
82 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
83 cairo_line_to(cr, x1, y1 - radius);
84 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
85 cairo_line_to(cr, x0 + radius, y1);
86 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
87 cairo_close_path(cr);
88}
89
Kristian Høgsberg0395f302008-12-22 12:14:50 -050090static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050091window_attach_surface(struct window *window)
92{
93 struct wl_visual *visual;
94
Kristian Høgsberg2aac3022009-12-21 10:04:53 -050095 if (window->pending_surface != NULL)
96 return;
97
98 window->pending_surface =
99 cairo_surface_reference(window->cairo_surface);
100
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500101 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
102 wl_surface_attach(window->surface,
103 cairo_drm_surface_get_name(window->cairo_surface),
104 window->allocation.width,
105 window->allocation.height,
106 cairo_drm_surface_get_stride(window->cairo_surface),
107 visual);
108
109 wl_surface_map(window->surface,
110 window->allocation.x - window->margin,
111 window->allocation.y - window->margin,
112 window->allocation.width,
113 window->allocation.height);
114}
115
116static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500117window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500120 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500121 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500122 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500123 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400124 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500125
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500126 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400127 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500128 CAIRO_CONTENT_COLOR_ALPHA,
129 window->allocation.width,
130 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500131
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500132 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500133 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500134 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
135
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500136 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500137
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500138 width = window->allocation.width - window->margin * 2;
139 height = window->allocation.height - window->margin * 2;
140
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400141 cairo_translate(cr, window->margin + shadow_dx,
142 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500143 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500144 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400145 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500146 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500147
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500148#define SLOW_BUT_PWETTY
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500149#ifdef SLOW_BUT_PWETTY
150 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
151 * Ideally we should have convolution filters in cairo, but we
152 * can also fallback to compositing the shadow image a bunch
153 * of times according to the blur kernel. */
154 {
155 cairo_surface_t *map;
156
157 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500158 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500159 cairo_drm_surface_unmap(window->cairo_surface, map);
160 }
161#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500162
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400163 cairo_translate(cr, -shadow_dx, -shadow_dy);
164 if (window->keyboard_device) {
165 rounded_rect(cr, 0, 0, width, height, radius);
166 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
167 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
168 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
169 cairo_set_source(cr, gradient);
170 cairo_fill(cr);
171 cairo_pattern_destroy(gradient);
172 } else {
173 rounded_rect(cr, 0, 0, width, height, radius);
174 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
175 cairo_fill(cr);
176 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500177
178 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
179 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500180 cairo_line_to(cr, width - 10, 50);
181 cairo_line_to(cr, width - 10, height - 10);
182 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500183 cairo_close_path(cr);
184 cairo_set_source(cr, dim);
185 cairo_stroke(cr);
186
187 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500188 cairo_line_to(cr, width - 10, 51);
189 cairo_line_to(cr, width - 10, height - 10);
190 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500191 cairo_close_path(cr);
192 cairo_set_source(cr, bright);
193 cairo_stroke(cr);
194
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500195 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
196 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500197 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500198 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500199 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
200 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
201 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500202 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400203 if (window->keyboard_device) {
204 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
205 cairo_stroke_preserve(cr);
206 cairo_set_source_rgb(cr, 1, 1, 1);
207 cairo_fill(cr);
208 } else {
209 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
210 cairo_fill(cr);
211 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500212 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500213
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500214 window_attach_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500215}
216
217static void
218window_draw_fullscreen(struct window *window)
219{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500220 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400221 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500222 CAIRO_CONTENT_COLOR_ALPHA,
223 window->allocation.width,
224 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500225
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500226 window_attach_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500227}
228
229void
230window_draw(struct window *window)
231{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500232 if (window->cairo_surface != NULL)
233 cairo_surface_destroy(window->cairo_surface);
234
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500235 if (window->fullscreen)
236 window_draw_fullscreen(window);
237 else
238 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500239}
240
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500241static void
242window_handle_acknowledge(void *data,
243 struct wl_compositor *compositor,
244 uint32_t key, uint32_t frame)
245{
246 struct window *window = data;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500247 cairo_surface_t *pending;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500248
249 /* The acknowledge event means that the server
250 * processed our last commit request and we can now
251 * safely free the old window buffer if we resized and
252 * render the next frame into our back buffer.. */
253
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500254 if (key == 0) {
255 pending = window->pending_surface;
256 window->pending_surface = NULL;
257 if (pending != window->cairo_surface)
258 window_attach_surface(window);
259 cairo_surface_destroy(pending);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500260 }
261}
262
263static void
264window_handle_frame(void *data,
265 struct wl_compositor *compositor,
266 uint32_t frame, uint32_t timestamp)
267{
268}
269
270static const struct wl_compositor_listener compositor_listener = {
271 window_handle_acknowledge,
272 window_handle_frame,
273};
274
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500275enum window_state {
276 WINDOW_STABLE,
277 WINDOW_MOVING,
278 WINDOW_RESIZING_UPPER_LEFT,
279 WINDOW_RESIZING_UPPER_RIGHT,
280 WINDOW_RESIZING_LOWER_LEFT,
281 WINDOW_RESIZING_LOWER_RIGHT
282};
283
284enum location {
285 LOCATION_INTERIOR,
286 LOCATION_UPPER_LEFT,
287 LOCATION_UPPER_RIGHT,
288 LOCATION_LOWER_LEFT,
289 LOCATION_LOWER_RIGHT,
290 LOCATION_OUTSIDE
291};
292
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500293static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500294window_handle_motion(void *data, struct wl_input_device *input_device,
295 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500296{
297 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500299 switch (window->state) {
300 case WINDOW_MOVING:
301 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500302 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500303 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500304 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500305 window->allocation.x = window->drag_x + x;
306 window->allocation.y = window->drag_y + y;
307 wl_surface_map(window->surface,
308 window->allocation.x - window->margin,
309 window->allocation.y - window->margin,
310 window->allocation.width,
311 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500312 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313 break;
314 case WINDOW_RESIZING_LOWER_RIGHT:
315 if (window->fullscreen)
316 break;
317 if (window->grab_device != input_device)
318 break;
319 window->allocation.width = window->drag_x + x;
320 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500321
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500322 if (window->resize_handler)
323 (*window->resize_handler)(window,
324 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500325
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500326 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500327 }
328}
329
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500330static void window_handle_button(void *data, struct wl_input_device *input_device,
331 uint32_t button, uint32_t state,
332 int32_t x, int32_t y, int32_t sx, int32_t sy)
333{
334 struct window *window = data;
335 int32_t left = window->allocation.x;
336 int32_t right = window->allocation.x +
337 window->allocation.width - window->margin * 2;
338 int32_t top = window->allocation.y;
339 int32_t bottom = window->allocation.y +
340 window->allocation.height - window->margin * 2;
341 int grip_size = 16, location;
342
343 if (right - grip_size <= x && x < right &&
344 bottom - grip_size <= y && y < bottom) {
345 location = LOCATION_LOWER_RIGHT;
346 } else if (left <= x && x < right && top <= y && y < bottom) {
347 location = LOCATION_INTERIOR;
348 } else {
349 location = LOCATION_OUTSIDE;
350 }
351
352 if (button == BTN_LEFT && state == 1) {
353 switch (location) {
354 case LOCATION_INTERIOR:
355 window->drag_x = window->allocation.x - x;
356 window->drag_y = window->allocation.y - y;
357 window->state = WINDOW_MOVING;
358 window->grab_device = input_device;
359 break;
360 case LOCATION_LOWER_RIGHT:
361 window->drag_x = window->allocation.width - x;
362 window->drag_y = window->allocation.height - y;
363 window->state = WINDOW_RESIZING_LOWER_RIGHT;
364 window->grab_device = input_device;
365 break;
366 default:
367 window->state = WINDOW_STABLE;
368 break;
369 }
370 } else if (button == BTN_LEFT &&
371 state == 0 && window->grab_device == input_device) {
372 window->state = WINDOW_STABLE;
373 }
374}
375
Kristian Høgsberg55444912009-02-21 14:31:09 -0500376
377struct key {
378 uint32_t code[4];
379} evdev_keymap[] = {
380 { { 0, 0 } }, /* 0 */
381 { { 0x1b, 0x1b } },
382 { { '1', '!' } },
383 { { '2', '@' } },
384 { { '3', '#' } },
385 { { '4', '$' } },
386 { { '5', '%' } },
387 { { '6', '^' } },
388 { { '7', '&' } },
389 { { '8', '*' } },
390 { { '9', '(' } },
391 { { '0', ')' } },
392 { { '-', '_' } },
393 { { '=', '+' } },
394 { { '\b', '\b' } },
395 { { '\t', '\t' } },
396
397 { { 'q', 'Q', 0x11 } }, /* 16 */
398 { { 'w', 'W', 0x17 } },
399 { { 'e', 'E', 0x05 } },
400 { { 'r', 'R', 0x12 } },
401 { { 't', 'T', 0x14 } },
402 { { 'y', 'Y', 0x19 } },
403 { { 'u', 'U', 0x15 } },
404 { { 'i', 'I', 0x09 } },
405 { { 'o', 'O', 0x0f } },
406 { { 'p', 'P', 0x10 } },
407 { { '[', '{', 0x1b } },
408 { { ']', '}', 0x1d } },
409 { { '\n', '\n' } },
410 { { 0, 0 } },
411 { { 'a', 'A', 0x01} },
412 { { 's', 'S', 0x13 } },
413
414 { { 'd', 'D', 0x04 } }, /* 32 */
415 { { 'f', 'F', 0x06 } },
416 { { 'g', 'G', 0x07 } },
417 { { 'h', 'H', 0x08 } },
418 { { 'j', 'J', 0x0a } },
419 { { 'k', 'K', 0x0b } },
420 { { 'l', 'L', 0x0c } },
421 { { ';', ':' } },
422 { { '\'', '"' } },
423 { { '`', '~' } },
424 { { 0, 0 } },
425 { { '\\', '|', 0x1c } },
426 { { 'z', 'Z', 0x1a } },
427 { { 'x', 'X', 0x18 } },
428 { { 'c', 'C', 0x03 } },
429 { { 'v', 'V', 0x16 } },
430
431 { { 'b', 'B', 0x02 } }, /* 48 */
432 { { 'n', 'N', 0x0e } },
433 { { 'm', 'M', 0x0d } },
434 { { ',', '<' } },
435 { { '.', '>' } },
436 { { '/', '?' } },
437 { { 0, 0 } },
438 { { '*', '*' } },
439 { { 0, 0 } },
440 { { ' ', ' ' } },
441 { { 0, 0 } }
442
443 /* 59 */
444};
445
446#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
447
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500448static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500449window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500450{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500451 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500452
Kristian Høgsberg55444912009-02-21 14:31:09 -0500453 switch (key) {
454 case KEY_LEFTSHIFT:
455 case KEY_RIGHTSHIFT:
456 mod = WINDOW_MODIFIER_SHIFT;
457 break;
458 case KEY_LEFTCTRL:
459 case KEY_RIGHTCTRL:
460 mod = WINDOW_MODIFIER_CONTROL;
461 break;
462 case KEY_LEFTALT:
463 case KEY_RIGHTALT:
464 mod = WINDOW_MODIFIER_ALT;
465 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500466 }
467
468 if (state)
469 window->modifiers |= mod;
470 else
471 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500472}
473
474static void
475window_handle_key(void *data, struct wl_input_device *input_device,
476 uint32_t key, uint32_t state)
477{
478 struct window *window = data;
479 uint32_t unicode = 0;
480
481 if (window->keyboard_device != input_device)
482 return;
483
484 window_update_modifiers(window, key, state);
485
486 if (key < ARRAY_LENGTH(evdev_keymap)) {
487 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
488 unicode = evdev_keymap[key].code[2];
489 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
490 unicode = evdev_keymap[key].code[1];
491 else
492 unicode = evdev_keymap[key].code[0];
493 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500494
495 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500496 (*window->key_handler)(window, key, unicode,
497 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500498}
499
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500500static void
501window_handle_pointer_focus(void *data,
502 struct wl_input_device *input_device,
503 struct wl_surface *surface)
504{
505}
506
507static void
508window_handle_keyboard_focus(void *data,
509 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500510 struct wl_surface *surface,
511 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500512{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500513 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500514 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500515
516 if (window->keyboard_device == input_device && surface != window->surface)
517 window->keyboard_device = NULL;
518 else if (window->keyboard_device == NULL && surface == window->surface)
519 window->keyboard_device = input_device;
520 else
521 return;
522
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500523 if (window->keyboard_device) {
524 end = keys->data + keys->size;
525 for (k = keys->data; k < end; k++)
526 window_update_modifiers(window, *k, 1);
527 } else {
528 window->modifiers = 0;
529 }
530
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500531 if (window->keyboard_focus_handler)
532 (*window->keyboard_focus_handler)(window,
533 window->keyboard_device,
534 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500535}
536
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500537static const struct wl_input_device_listener input_device_listener = {
538 window_handle_motion,
539 window_handle_button,
540 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500541 window_handle_pointer_focus,
542 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500543};
544
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500545void
546window_get_child_rectangle(struct window *window,
547 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500548{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500549 if (window->fullscreen) {
550 *rectangle = window->allocation;
551 } else {
552 rectangle->x = window->margin + 10;
553 rectangle->y = window->margin + 50;
554 rectangle->width = window->allocation.width - 20 - window->margin * 2;
555 rectangle->height = window->allocation.height - 60 - window->margin * 2;
556 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500557}
558
559void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500560window_set_child_size(struct window *window,
561 struct rectangle *rectangle)
562{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500563 if (!window->fullscreen) {
564 window->allocation.width = rectangle->width + 20 + window->margin * 2;
565 window->allocation.height = rectangle->height + 60 + window->margin * 2;
566 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500567}
568
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500569cairo_surface_t *
570window_create_surface(struct window *window,
571 struct rectangle *rectangle)
572{
Kristian Høgsberg26449102009-05-28 20:23:31 -0400573 return cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500574 CAIRO_CONTENT_COLOR_ALPHA,
575 rectangle->width,
576 rectangle->height);
577}
578
Kristian Høgsberg22106762008-12-08 13:50:07 -0500579void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500580window_copy(struct window *window,
581 struct rectangle *rectangle,
582 uint32_t name, uint32_t stride)
583{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500584 cairo_surface_t *surface;
585 cairo_t *cr;
586
587 surface = cairo_drm_surface_create_for_name (window->display->device,
588 name, CAIRO_CONTENT_COLOR_ALPHA,
589 rectangle->width, rectangle->height,
590 stride);
591
592 cr = cairo_create (window->cairo_surface);
593
594 cairo_set_source_surface (cr,
595 surface,
596 rectangle->x, rectangle->y);
597
598 cairo_paint (cr);
599 cairo_destroy (cr);
600 cairo_surface_destroy (surface);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500601}
602
603void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500604window_copy_surface(struct window *window,
605 struct rectangle *rectangle,
606 cairo_surface_t *surface)
607{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500608 cairo_t *cr;
609
610 cr = cairo_create (window->cairo_surface);
611
612 cairo_set_source_surface (cr,
613 surface,
614 rectangle->x, rectangle->y);
615
616 cairo_paint (cr);
617 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500618}
619
620void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500621window_set_fullscreen(struct window *window, int fullscreen)
622{
623 window->fullscreen = fullscreen;
624 if (window->fullscreen) {
625 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500626 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500627 } else {
628 window->allocation = window->saved_allocation;
629 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500630}
631
632void
633window_set_resize_handler(struct window *window,
634 window_resize_handler_t handler, void *data)
635{
636 window->resize_handler = handler;
637 window->user_data = data;
638}
639
640void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500641window_set_key_handler(struct window *window,
642 window_key_handler_t handler, void *data)
643{
644 window->key_handler = handler;
645 window->user_data = data;
646}
647
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500648void
649window_set_keyboard_focus_handler(struct window *window,
650 window_keyboard_focus_handler_t handler, void *data)
651{
652 window->keyboard_focus_handler = handler;
653 window->user_data = data;
654}
655
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500656struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500657window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500658 int32_t x, int32_t y, int32_t width, int32_t height)
659{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500660 struct window *window;
661
662 window = malloc(sizeof *window);
663 if (window == NULL)
664 return NULL;
665
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500666 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500667 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500668 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500669 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500670 window->allocation.x = x;
671 window->allocation.y = y;
672 window->allocation.width = width;
673 window->allocation.height = height;
674 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500675 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500676 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500677
678 wl_compositor_add_listener(display->compositor,
679 &compositor_listener, window);
680
681 wl_input_device_add_listener(display->input_device,
682 &input_device_listener, window);
683
684 return window;
685}
686
687static void
688display_handle_geometry(void *data,
689 struct wl_output *output,
690 int32_t width, int32_t height)
691{
692 struct display *display = data;
693
694 display->screen_allocation.x = 0;
695 display->screen_allocation.y = 0;
696 display->screen_allocation.width = width;
697 display->screen_allocation.height = height;
698}
699
700static const struct wl_output_listener output_listener = {
701 display_handle_geometry,
702};
703
704static void
705display_handle_global(struct wl_display *display,
706 struct wl_object *object, void *data)
707{
708 struct display *d = data;
709
710 if (wl_object_implements(object, "compositor", 1)) {
711 d->compositor = (struct wl_compositor *) object;
712 } else if (wl_object_implements(object, "output", 1)) {
713 d->output = (struct wl_output *) object;
714 wl_output_add_listener(d->output, &output_listener, d);
715 } else if (wl_object_implements(object, "input_device", 1)) {
716 d->input_device =(struct wl_input_device *) object;
717 }
718}
719
720struct display *
721display_create(struct wl_display *display, int fd)
722{
723 struct display *d;
724
725 d = malloc(sizeof *d);
726 if (d == NULL)
727 return NULL;
728
729 d->display = display;
Kristian Høgsberg26449102009-05-28 20:23:31 -0400730 d->device = cairo_drm_device_get_for_fd(fd);
731 if (d->device == NULL) {
732 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500733 return NULL;
734 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500735
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500736 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500737 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500738 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500739
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500740 /* Process connection events. */
741 wl_display_iterate(display, WL_DISPLAY_READABLE);
742
743 return d;
744}
745
746struct wl_compositor *
747display_get_compositor(struct display *display)
748{
749 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500750}