blob: 28fb97a9e33caa37e78e478385e5b50c3f0091e1 [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øgsberg2f2cfae2008-11-08 22:46:30 -050039
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050041
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050042struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050043 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050044 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050045 struct wl_output *output;
46 struct wl_input_device *input_device;
47 struct rectangle screen_allocation;
48 cairo_drm_context_t *ctx;
49 int fd;
50};
51
52struct window {
53 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050054 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050055 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050056 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050058 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050059 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050060 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050061 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050062 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050063 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050065 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050066
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050067 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050070 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050071 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050073};
74
Kristian Høgsberge4feb562008-11-08 18:53:37 -050075static void
76rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
77{
78 cairo_move_to(cr, x0, y0 + radius);
79 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
80 cairo_line_to(cr, x1 - radius, y0);
81 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
82 cairo_line_to(cr, x1, y1 - radius);
83 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
84 cairo_line_to(cr, x0 + radius, y1);
85 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
86 cairo_close_path(cr);
87}
88
Kristian Høgsberg0395f302008-12-22 12:14:50 -050089static void
90window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050093 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050094 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050095 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050096 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050097 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050098
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050099 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500100 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500101 CAIRO_CONTENT_COLOR_ALPHA,
102 window->allocation.width,
103 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500105 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500106 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500107 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
108
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500109 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500110
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500111 width = window->allocation.width - window->margin * 2;
112 height = window->allocation.height - window->margin * 2;
113
Kristian Høgsberg40979232008-11-25 22:40:39 -0500114 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500115 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500116 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500117 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500118 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500119
120#ifdef SLOW_BUT_PWETTY
121 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
122 * Ideally we should have convolution filters in cairo, but we
123 * can also fallback to compositing the shadow image a bunch
124 * of times according to the blur kernel. */
125 {
126 cairo_surface_t *map;
127
128 map = cairo_drm_surface_map(window->cairo_surface);
129 blur_surface(map);
130 cairo_drm_surface_unmap(window->cairo_surface, map);
131 }
132#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500133
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500134 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500135 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500136 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500137 cairo_set_source(cr, outline);
138 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500139 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500140 cairo_set_source(cr, bright);
141 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500142 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500143 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500144 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500145
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500146 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500147 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500148 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
149 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500150 cairo_set_source(cr, gradient);
151 cairo_fill(cr);
152 cairo_pattern_destroy(gradient);
153
154 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
155 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500156 cairo_line_to(cr, width - 10, 50);
157 cairo_line_to(cr, width - 10, height - 10);
158 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500159 cairo_close_path(cr);
160 cairo_set_source(cr, dim);
161 cairo_stroke(cr);
162
163 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500164 cairo_line_to(cr, width - 10, 51);
165 cairo_line_to(cr, width - 10, height - 10);
166 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500167 cairo_close_path(cr);
168 cairo_set_source(cr, bright);
169 cairo_stroke(cr);
170
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500171 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
172 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500173 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500174 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500175 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
176 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
177 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500178 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500179 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
180 cairo_stroke_preserve(cr);
181 cairo_set_source_rgb(cr, 1, 1, 1);
182 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500183 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500184
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500185 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500186 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500187 cairo_drm_surface_get_name(window->cairo_surface),
188 window->allocation.width,
189 window->allocation.height,
190 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500191 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500192
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500193 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500194 window->allocation.x - window->margin,
195 window->allocation.y - window->margin,
196 window->allocation.width,
197 window->allocation.height);
198}
199
200static void
201window_draw_fullscreen(struct window *window)
202{
203 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500204
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500205 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500206 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500207 CAIRO_CONTENT_COLOR_ALPHA,
208 window->allocation.width,
209 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500211 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500212 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500213 cairo_drm_surface_get_name(window->cairo_surface),
214 window->allocation.width,
215 window->allocation.height,
216 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500217 visual);
218
219 wl_surface_map(window->surface,
220 window->allocation.x,
221 window->allocation.y,
222 window->allocation.width,
223 window->allocation.height);
224}
225
226void
227window_draw(struct window *window)
228{
229 if (window->fullscreen)
230 window_draw_fullscreen(window);
231 else
232 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500233}
234
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500235static void
236window_handle_acknowledge(void *data,
237 struct wl_compositor *compositor,
238 uint32_t key, uint32_t frame)
239{
240 struct window *window = data;
241
242 /* The acknowledge event means that the server
243 * processed our last commit request and we can now
244 * safely free the old window buffer if we resized and
245 * render the next frame into our back buffer.. */
246
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500247 if (key == 0 && window->cairo_surface != NULL) {
248 cairo_surface_destroy(window->cairo_surface);
249 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500250 }
251}
252
253static void
254window_handle_frame(void *data,
255 struct wl_compositor *compositor,
256 uint32_t frame, uint32_t timestamp)
257{
258}
259
260static const struct wl_compositor_listener compositor_listener = {
261 window_handle_acknowledge,
262 window_handle_frame,
263};
264
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500265enum window_state {
266 WINDOW_STABLE,
267 WINDOW_MOVING,
268 WINDOW_RESIZING_UPPER_LEFT,
269 WINDOW_RESIZING_UPPER_RIGHT,
270 WINDOW_RESIZING_LOWER_LEFT,
271 WINDOW_RESIZING_LOWER_RIGHT
272};
273
274enum location {
275 LOCATION_INTERIOR,
276 LOCATION_UPPER_LEFT,
277 LOCATION_UPPER_RIGHT,
278 LOCATION_LOWER_LEFT,
279 LOCATION_LOWER_RIGHT,
280 LOCATION_OUTSIDE
281};
282
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500283static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500284window_handle_motion(void *data, struct wl_input_device *input_device,
285 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500286{
287 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500288
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500289 switch (window->state) {
290 case WINDOW_MOVING:
291 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500292 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500293 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500294 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500295 window->allocation.x = window->drag_x + x;
296 window->allocation.y = window->drag_y + y;
297 wl_surface_map(window->surface,
298 window->allocation.x - window->margin,
299 window->allocation.y - window->margin,
300 window->allocation.width,
301 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500302 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500303 break;
304 case WINDOW_RESIZING_LOWER_RIGHT:
305 if (window->fullscreen)
306 break;
307 if (window->grab_device != input_device)
308 break;
309 window->allocation.width = window->drag_x + x;
310 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500311
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500312 if (window->resize_handler)
313 (*window->resize_handler)(window,
314 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500315
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500316 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500317 }
318}
319
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500320static void window_handle_button(void *data, struct wl_input_device *input_device,
321 uint32_t button, uint32_t state,
322 int32_t x, int32_t y, int32_t sx, int32_t sy)
323{
324 struct window *window = data;
325 int32_t left = window->allocation.x;
326 int32_t right = window->allocation.x +
327 window->allocation.width - window->margin * 2;
328 int32_t top = window->allocation.y;
329 int32_t bottom = window->allocation.y +
330 window->allocation.height - window->margin * 2;
331 int grip_size = 16, location;
332
333 if (right - grip_size <= x && x < right &&
334 bottom - grip_size <= y && y < bottom) {
335 location = LOCATION_LOWER_RIGHT;
336 } else if (left <= x && x < right && top <= y && y < bottom) {
337 location = LOCATION_INTERIOR;
338 } else {
339 location = LOCATION_OUTSIDE;
340 }
341
342 if (button == BTN_LEFT && state == 1) {
343 switch (location) {
344 case LOCATION_INTERIOR:
345 window->drag_x = window->allocation.x - x;
346 window->drag_y = window->allocation.y - y;
347 window->state = WINDOW_MOVING;
348 window->grab_device = input_device;
349 break;
350 case LOCATION_LOWER_RIGHT:
351 window->drag_x = window->allocation.width - x;
352 window->drag_y = window->allocation.height - y;
353 window->state = WINDOW_RESIZING_LOWER_RIGHT;
354 window->grab_device = input_device;
355 break;
356 default:
357 window->state = WINDOW_STABLE;
358 break;
359 }
360 } else if (button == BTN_LEFT &&
361 state == 0 && window->grab_device == input_device) {
362 window->state = WINDOW_STABLE;
363 }
364}
365
Kristian Høgsberg55444912009-02-21 14:31:09 -0500366
367struct key {
368 uint32_t code[4];
369} evdev_keymap[] = {
370 { { 0, 0 } }, /* 0 */
371 { { 0x1b, 0x1b } },
372 { { '1', '!' } },
373 { { '2', '@' } },
374 { { '3', '#' } },
375 { { '4', '$' } },
376 { { '5', '%' } },
377 { { '6', '^' } },
378 { { '7', '&' } },
379 { { '8', '*' } },
380 { { '9', '(' } },
381 { { '0', ')' } },
382 { { '-', '_' } },
383 { { '=', '+' } },
384 { { '\b', '\b' } },
385 { { '\t', '\t' } },
386
387 { { 'q', 'Q', 0x11 } }, /* 16 */
388 { { 'w', 'W', 0x17 } },
389 { { 'e', 'E', 0x05 } },
390 { { 'r', 'R', 0x12 } },
391 { { 't', 'T', 0x14 } },
392 { { 'y', 'Y', 0x19 } },
393 { { 'u', 'U', 0x15 } },
394 { { 'i', 'I', 0x09 } },
395 { { 'o', 'O', 0x0f } },
396 { { 'p', 'P', 0x10 } },
397 { { '[', '{', 0x1b } },
398 { { ']', '}', 0x1d } },
399 { { '\n', '\n' } },
400 { { 0, 0 } },
401 { { 'a', 'A', 0x01} },
402 { { 's', 'S', 0x13 } },
403
404 { { 'd', 'D', 0x04 } }, /* 32 */
405 { { 'f', 'F', 0x06 } },
406 { { 'g', 'G', 0x07 } },
407 { { 'h', 'H', 0x08 } },
408 { { 'j', 'J', 0x0a } },
409 { { 'k', 'K', 0x0b } },
410 { { 'l', 'L', 0x0c } },
411 { { ';', ':' } },
412 { { '\'', '"' } },
413 { { '`', '~' } },
414 { { 0, 0 } },
415 { { '\\', '|', 0x1c } },
416 { { 'z', 'Z', 0x1a } },
417 { { 'x', 'X', 0x18 } },
418 { { 'c', 'C', 0x03 } },
419 { { 'v', 'V', 0x16 } },
420
421 { { 'b', 'B', 0x02 } }, /* 48 */
422 { { 'n', 'N', 0x0e } },
423 { { 'm', 'M', 0x0d } },
424 { { ',', '<' } },
425 { { '.', '>' } },
426 { { '/', '?' } },
427 { { 0, 0 } },
428 { { '*', '*' } },
429 { { 0, 0 } },
430 { { ' ', ' ' } },
431 { { 0, 0 } }
432
433 /* 59 */
434};
435
436#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
437
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500438static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500439window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500440{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500441 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500442
Kristian Høgsberg55444912009-02-21 14:31:09 -0500443 switch (key) {
444 case KEY_LEFTSHIFT:
445 case KEY_RIGHTSHIFT:
446 mod = WINDOW_MODIFIER_SHIFT;
447 break;
448 case KEY_LEFTCTRL:
449 case KEY_RIGHTCTRL:
450 mod = WINDOW_MODIFIER_CONTROL;
451 break;
452 case KEY_LEFTALT:
453 case KEY_RIGHTALT:
454 mod = WINDOW_MODIFIER_ALT;
455 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500456 }
457
458 if (state)
459 window->modifiers |= mod;
460 else
461 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500462}
463
464static void
465window_handle_key(void *data, struct wl_input_device *input_device,
466 uint32_t key, uint32_t state)
467{
468 struct window *window = data;
469 uint32_t unicode = 0;
470
471 if (window->keyboard_device != input_device)
472 return;
473
474 window_update_modifiers(window, key, state);
475
476 if (key < ARRAY_LENGTH(evdev_keymap)) {
477 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
478 unicode = evdev_keymap[key].code[2];
479 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
480 unicode = evdev_keymap[key].code[1];
481 else
482 unicode = evdev_keymap[key].code[0];
483 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500484
485 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500486 (*window->key_handler)(window, key, unicode,
487 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500488}
489
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500490static void
491window_handle_pointer_focus(void *data,
492 struct wl_input_device *input_device,
493 struct wl_surface *surface)
494{
495}
496
497static void
498window_handle_keyboard_focus(void *data,
499 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500500 struct wl_surface *surface,
501 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500502{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500503 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500504 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500505
506 if (window->keyboard_device == input_device && surface != window->surface)
507 window->keyboard_device = NULL;
508 else if (window->keyboard_device == NULL && surface == window->surface)
509 window->keyboard_device = input_device;
510 else
511 return;
512
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500513 if (window->keyboard_device) {
514 end = keys->data + keys->size;
515 for (k = keys->data; k < end; k++)
516 window_update_modifiers(window, *k, 1);
517 } else {
518 window->modifiers = 0;
519 }
520
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500521 if (window->keyboard_focus_handler)
522 (*window->keyboard_focus_handler)(window,
523 window->keyboard_device,
524 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500525}
526
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500527static const struct wl_input_device_listener input_device_listener = {
528 window_handle_motion,
529 window_handle_button,
530 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500531 window_handle_pointer_focus,
532 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500533};
534
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500535void
536window_get_child_rectangle(struct window *window,
537 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500538{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500539 if (window->fullscreen) {
540 *rectangle = window->allocation;
541 } else {
542 rectangle->x = window->margin + 10;
543 rectangle->y = window->margin + 50;
544 rectangle->width = window->allocation.width - 20 - window->margin * 2;
545 rectangle->height = window->allocation.height - 60 - window->margin * 2;
546 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500547}
548
549void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500550window_set_child_size(struct window *window,
551 struct rectangle *rectangle)
552{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500553 if (!window->fullscreen) {
554 window->allocation.width = rectangle->width + 20 + window->margin * 2;
555 window->allocation.height = rectangle->height + 60 + window->margin * 2;
556 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500557}
558
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500559cairo_surface_t *
560window_create_surface(struct window *window,
561 struct rectangle *rectangle)
562{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500563 return cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500564 CAIRO_CONTENT_COLOR_ALPHA,
565 rectangle->width,
566 rectangle->height);
567}
568
Kristian Høgsberg22106762008-12-08 13:50:07 -0500569void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500570window_copy(struct window *window,
571 struct rectangle *rectangle,
572 uint32_t name, uint32_t stride)
573{
574 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500575 rectangle->x,
576 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500577 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500578 0, 0,
579 rectangle->width,
580 rectangle->height);
581}
582
583void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500584window_copy_surface(struct window *window,
585 struct rectangle *rectangle,
586 cairo_surface_t *surface)
587{
588 wl_surface_copy(window->surface,
589 rectangle->x,
590 rectangle->y,
591 cairo_drm_surface_get_name(surface),
592 cairo_drm_surface_get_stride(surface),
593 0, 0,
594 rectangle->width,
595 rectangle->height);
596}
597
598void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500599window_set_fullscreen(struct window *window, int fullscreen)
600{
601 window->fullscreen = fullscreen;
602 if (window->fullscreen) {
603 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500604 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500605 } else {
606 window->allocation = window->saved_allocation;
607 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500608}
609
610void
611window_set_resize_handler(struct window *window,
612 window_resize_handler_t handler, void *data)
613{
614 window->resize_handler = handler;
615 window->user_data = data;
616}
617
618void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500619window_set_key_handler(struct window *window,
620 window_key_handler_t handler, void *data)
621{
622 window->key_handler = handler;
623 window->user_data = data;
624}
625
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500626void
627window_set_keyboard_focus_handler(struct window *window,
628 window_keyboard_focus_handler_t handler, void *data)
629{
630 window->keyboard_focus_handler = handler;
631 window->user_data = data;
632}
633
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500634struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500635window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500636 int32_t x, int32_t y, int32_t width, int32_t height)
637{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500638 struct window *window;
639
640 window = malloc(sizeof *window);
641 if (window == NULL)
642 return NULL;
643
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500644 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500645 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500646 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500647 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500648 window->allocation.x = x;
649 window->allocation.y = y;
650 window->allocation.width = width;
651 window->allocation.height = height;
652 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500653 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500654 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500655
656 wl_compositor_add_listener(display->compositor,
657 &compositor_listener, window);
658
659 wl_input_device_add_listener(display->input_device,
660 &input_device_listener, window);
661
662 return window;
663}
664
665static void
666display_handle_geometry(void *data,
667 struct wl_output *output,
668 int32_t width, int32_t height)
669{
670 struct display *display = data;
671
672 display->screen_allocation.x = 0;
673 display->screen_allocation.y = 0;
674 display->screen_allocation.width = width;
675 display->screen_allocation.height = height;
676}
677
678static const struct wl_output_listener output_listener = {
679 display_handle_geometry,
680};
681
682static void
683display_handle_global(struct wl_display *display,
684 struct wl_object *object, void *data)
685{
686 struct display *d = data;
687
688 if (wl_object_implements(object, "compositor", 1)) {
689 d->compositor = (struct wl_compositor *) object;
690 } else if (wl_object_implements(object, "output", 1)) {
691 d->output = (struct wl_output *) object;
692 wl_output_add_listener(d->output, &output_listener, d);
693 } else if (wl_object_implements(object, "input_device", 1)) {
694 d->input_device =(struct wl_input_device *) object;
695 }
696}
697
698struct display *
699display_create(struct wl_display *display, int fd)
700{
701 struct display *d;
702
703 d = malloc(sizeof *d);
704 if (d == NULL)
705 return NULL;
706
707 d->display = display;
708 d->ctx = cairo_drm_context_get_for_fd(fd);
709 if (d->ctx == NULL) {
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500710 fprintf(stderr, "failed to get cairo drm context\n");
711 return NULL;
712 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500713
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500714 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500715 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500716 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500717
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500718 /* Process connection events. */
719 wl_display_iterate(display, WL_DISPLAY_READABLE);
720
721 return d;
722}
723
724struct wl_compositor *
725display_get_compositor(struct display *display)
726{
727 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500728}