blob: e74edc5e02af808da73cbdf585668e7a1992d069 [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øgsberg0ac16f02009-01-15 11:37:43 -050068 cairo_surface_t *cairo_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
91window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050093 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050094 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050095 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050096 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050097 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050098 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050099
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500100 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400101 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500102 CAIRO_CONTENT_COLOR_ALPHA,
103 window->allocation.width,
104 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500106 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500107 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500108 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
109
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500110 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500111
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500112 width = window->allocation.width - window->margin * 2;
113 height = window->allocation.height - window->margin * 2;
114
Kristian Høgsberg40979232008-11-25 22:40:39 -0500115 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500116 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500117 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500118 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500119 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500120
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500121#define SLOW_BUT_PWETTY
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500122#ifdef SLOW_BUT_PWETTY
123 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
124 * Ideally we should have convolution filters in cairo, but we
125 * can also fallback to compositing the shadow image a bunch
126 * of times according to the blur kernel. */
127 {
128 cairo_surface_t *map;
129
130 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500131 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500132 cairo_drm_surface_unmap(window->cairo_surface, map);
133 }
134#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500135
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500136 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500138 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500139 cairo_set_source(cr, outline);
140 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500141 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500142 cairo_set_source(cr, bright);
143 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500144 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500145 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500146 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500147
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500148 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500149 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500150 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
151 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500152 cairo_set_source(cr, gradient);
153 cairo_fill(cr);
154 cairo_pattern_destroy(gradient);
155
156 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
157 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500158 cairo_line_to(cr, width - 10, 50);
159 cairo_line_to(cr, width - 10, height - 10);
160 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500161 cairo_close_path(cr);
162 cairo_set_source(cr, dim);
163 cairo_stroke(cr);
164
165 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500166 cairo_line_to(cr, width - 10, 51);
167 cairo_line_to(cr, width - 10, height - 10);
168 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500169 cairo_close_path(cr);
170 cairo_set_source(cr, bright);
171 cairo_stroke(cr);
172
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500173 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
174 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500175 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500176 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500177 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
178 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
179 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500180 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500181 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
182 cairo_stroke_preserve(cr);
183 cairo_set_source_rgb(cr, 1, 1, 1);
184 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500185 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500186
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500187 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500188 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500189 cairo_drm_surface_get_name(window->cairo_surface),
190 window->allocation.width,
191 window->allocation.height,
192 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500193 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500194
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500195 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500196 window->allocation.x - window->margin,
197 window->allocation.y - window->margin,
198 window->allocation.width,
199 window->allocation.height);
200}
201
202static void
203window_draw_fullscreen(struct window *window)
204{
205 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500206
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500207 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400208 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500209 CAIRO_CONTENT_COLOR_ALPHA,
210 window->allocation.width,
211 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500213 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500214 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500215 cairo_drm_surface_get_name(window->cairo_surface),
216 window->allocation.width,
217 window->allocation.height,
218 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500219 visual);
220
221 wl_surface_map(window->surface,
222 window->allocation.x,
223 window->allocation.y,
224 window->allocation.width,
225 window->allocation.height);
226}
227
228void
229window_draw(struct window *window)
230{
231 if (window->fullscreen)
232 window_draw_fullscreen(window);
233 else
234 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500235}
236
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500237static void
238window_handle_acknowledge(void *data,
239 struct wl_compositor *compositor,
240 uint32_t key, uint32_t frame)
241{
242 struct window *window = data;
243
244 /* The acknowledge event means that the server
245 * processed our last commit request and we can now
246 * safely free the old window buffer if we resized and
247 * render the next frame into our back buffer.. */
248
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500249 if (key == 0 && window->cairo_surface != NULL) {
250 cairo_surface_destroy(window->cairo_surface);
251 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500252 }
253}
254
255static void
256window_handle_frame(void *data,
257 struct wl_compositor *compositor,
258 uint32_t frame, uint32_t timestamp)
259{
260}
261
262static const struct wl_compositor_listener compositor_listener = {
263 window_handle_acknowledge,
264 window_handle_frame,
265};
266
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500267enum window_state {
268 WINDOW_STABLE,
269 WINDOW_MOVING,
270 WINDOW_RESIZING_UPPER_LEFT,
271 WINDOW_RESIZING_UPPER_RIGHT,
272 WINDOW_RESIZING_LOWER_LEFT,
273 WINDOW_RESIZING_LOWER_RIGHT
274};
275
276enum location {
277 LOCATION_INTERIOR,
278 LOCATION_UPPER_LEFT,
279 LOCATION_UPPER_RIGHT,
280 LOCATION_LOWER_LEFT,
281 LOCATION_LOWER_RIGHT,
282 LOCATION_OUTSIDE
283};
284
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500285static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500286window_handle_motion(void *data, struct wl_input_device *input_device,
287 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500288{
289 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500290
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500291 switch (window->state) {
292 case WINDOW_MOVING:
293 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500294 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500295 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500296 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500297 window->allocation.x = window->drag_x + x;
298 window->allocation.y = window->drag_y + y;
299 wl_surface_map(window->surface,
300 window->allocation.x - window->margin,
301 window->allocation.y - window->margin,
302 window->allocation.width,
303 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500304 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500305 break;
306 case WINDOW_RESIZING_LOWER_RIGHT:
307 if (window->fullscreen)
308 break;
309 if (window->grab_device != input_device)
310 break;
311 window->allocation.width = window->drag_x + x;
312 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500313
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500314 if (window->resize_handler)
315 (*window->resize_handler)(window,
316 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500317
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500318 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500319 }
320}
321
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500322static void window_handle_button(void *data, struct wl_input_device *input_device,
323 uint32_t button, uint32_t state,
324 int32_t x, int32_t y, int32_t sx, int32_t sy)
325{
326 struct window *window = data;
327 int32_t left = window->allocation.x;
328 int32_t right = window->allocation.x +
329 window->allocation.width - window->margin * 2;
330 int32_t top = window->allocation.y;
331 int32_t bottom = window->allocation.y +
332 window->allocation.height - window->margin * 2;
333 int grip_size = 16, location;
334
335 if (right - grip_size <= x && x < right &&
336 bottom - grip_size <= y && y < bottom) {
337 location = LOCATION_LOWER_RIGHT;
338 } else if (left <= x && x < right && top <= y && y < bottom) {
339 location = LOCATION_INTERIOR;
340 } else {
341 location = LOCATION_OUTSIDE;
342 }
343
344 if (button == BTN_LEFT && state == 1) {
345 switch (location) {
346 case LOCATION_INTERIOR:
347 window->drag_x = window->allocation.x - x;
348 window->drag_y = window->allocation.y - y;
349 window->state = WINDOW_MOVING;
350 window->grab_device = input_device;
351 break;
352 case LOCATION_LOWER_RIGHT:
353 window->drag_x = window->allocation.width - x;
354 window->drag_y = window->allocation.height - y;
355 window->state = WINDOW_RESIZING_LOWER_RIGHT;
356 window->grab_device = input_device;
357 break;
358 default:
359 window->state = WINDOW_STABLE;
360 break;
361 }
362 } else if (button == BTN_LEFT &&
363 state == 0 && window->grab_device == input_device) {
364 window->state = WINDOW_STABLE;
365 }
366}
367
Kristian Høgsberg55444912009-02-21 14:31:09 -0500368
369struct key {
370 uint32_t code[4];
371} evdev_keymap[] = {
372 { { 0, 0 } }, /* 0 */
373 { { 0x1b, 0x1b } },
374 { { '1', '!' } },
375 { { '2', '@' } },
376 { { '3', '#' } },
377 { { '4', '$' } },
378 { { '5', '%' } },
379 { { '6', '^' } },
380 { { '7', '&' } },
381 { { '8', '*' } },
382 { { '9', '(' } },
383 { { '0', ')' } },
384 { { '-', '_' } },
385 { { '=', '+' } },
386 { { '\b', '\b' } },
387 { { '\t', '\t' } },
388
389 { { 'q', 'Q', 0x11 } }, /* 16 */
390 { { 'w', 'W', 0x17 } },
391 { { 'e', 'E', 0x05 } },
392 { { 'r', 'R', 0x12 } },
393 { { 't', 'T', 0x14 } },
394 { { 'y', 'Y', 0x19 } },
395 { { 'u', 'U', 0x15 } },
396 { { 'i', 'I', 0x09 } },
397 { { 'o', 'O', 0x0f } },
398 { { 'p', 'P', 0x10 } },
399 { { '[', '{', 0x1b } },
400 { { ']', '}', 0x1d } },
401 { { '\n', '\n' } },
402 { { 0, 0 } },
403 { { 'a', 'A', 0x01} },
404 { { 's', 'S', 0x13 } },
405
406 { { 'd', 'D', 0x04 } }, /* 32 */
407 { { 'f', 'F', 0x06 } },
408 { { 'g', 'G', 0x07 } },
409 { { 'h', 'H', 0x08 } },
410 { { 'j', 'J', 0x0a } },
411 { { 'k', 'K', 0x0b } },
412 { { 'l', 'L', 0x0c } },
413 { { ';', ':' } },
414 { { '\'', '"' } },
415 { { '`', '~' } },
416 { { 0, 0 } },
417 { { '\\', '|', 0x1c } },
418 { { 'z', 'Z', 0x1a } },
419 { { 'x', 'X', 0x18 } },
420 { { 'c', 'C', 0x03 } },
421 { { 'v', 'V', 0x16 } },
422
423 { { 'b', 'B', 0x02 } }, /* 48 */
424 { { 'n', 'N', 0x0e } },
425 { { 'm', 'M', 0x0d } },
426 { { ',', '<' } },
427 { { '.', '>' } },
428 { { '/', '?' } },
429 { { 0, 0 } },
430 { { '*', '*' } },
431 { { 0, 0 } },
432 { { ' ', ' ' } },
433 { { 0, 0 } }
434
435 /* 59 */
436};
437
438#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
439
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500440static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500441window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500442{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500443 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500444
Kristian Høgsberg55444912009-02-21 14:31:09 -0500445 switch (key) {
446 case KEY_LEFTSHIFT:
447 case KEY_RIGHTSHIFT:
448 mod = WINDOW_MODIFIER_SHIFT;
449 break;
450 case KEY_LEFTCTRL:
451 case KEY_RIGHTCTRL:
452 mod = WINDOW_MODIFIER_CONTROL;
453 break;
454 case KEY_LEFTALT:
455 case KEY_RIGHTALT:
456 mod = WINDOW_MODIFIER_ALT;
457 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500458 }
459
460 if (state)
461 window->modifiers |= mod;
462 else
463 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500464}
465
466static void
467window_handle_key(void *data, struct wl_input_device *input_device,
468 uint32_t key, uint32_t state)
469{
470 struct window *window = data;
471 uint32_t unicode = 0;
472
473 if (window->keyboard_device != input_device)
474 return;
475
476 window_update_modifiers(window, key, state);
477
478 if (key < ARRAY_LENGTH(evdev_keymap)) {
479 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
480 unicode = evdev_keymap[key].code[2];
481 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
482 unicode = evdev_keymap[key].code[1];
483 else
484 unicode = evdev_keymap[key].code[0];
485 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500486
487 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500488 (*window->key_handler)(window, key, unicode,
489 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500490}
491
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500492static void
493window_handle_pointer_focus(void *data,
494 struct wl_input_device *input_device,
495 struct wl_surface *surface)
496{
497}
498
499static void
500window_handle_keyboard_focus(void *data,
501 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500502 struct wl_surface *surface,
503 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500504{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500505 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500506 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500507
508 if (window->keyboard_device == input_device && surface != window->surface)
509 window->keyboard_device = NULL;
510 else if (window->keyboard_device == NULL && surface == window->surface)
511 window->keyboard_device = input_device;
512 else
513 return;
514
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500515 if (window->keyboard_device) {
516 end = keys->data + keys->size;
517 for (k = keys->data; k < end; k++)
518 window_update_modifiers(window, *k, 1);
519 } else {
520 window->modifiers = 0;
521 }
522
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500523 if (window->keyboard_focus_handler)
524 (*window->keyboard_focus_handler)(window,
525 window->keyboard_device,
526 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500527}
528
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500529static const struct wl_input_device_listener input_device_listener = {
530 window_handle_motion,
531 window_handle_button,
532 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500533 window_handle_pointer_focus,
534 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500535};
536
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500537void
538window_get_child_rectangle(struct window *window,
539 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500540{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500541 if (window->fullscreen) {
542 *rectangle = window->allocation;
543 } else {
544 rectangle->x = window->margin + 10;
545 rectangle->y = window->margin + 50;
546 rectangle->width = window->allocation.width - 20 - window->margin * 2;
547 rectangle->height = window->allocation.height - 60 - window->margin * 2;
548 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500549}
550
551void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500552window_set_child_size(struct window *window,
553 struct rectangle *rectangle)
554{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500555 if (!window->fullscreen) {
556 window->allocation.width = rectangle->width + 20 + window->margin * 2;
557 window->allocation.height = rectangle->height + 60 + window->margin * 2;
558 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500559}
560
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500561cairo_surface_t *
562window_create_surface(struct window *window,
563 struct rectangle *rectangle)
564{
Kristian Høgsberg26449102009-05-28 20:23:31 -0400565 return cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500566 CAIRO_CONTENT_COLOR_ALPHA,
567 rectangle->width,
568 rectangle->height);
569}
570
Kristian Høgsberg22106762008-12-08 13:50:07 -0500571void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500572window_copy(struct window *window,
573 struct rectangle *rectangle,
574 uint32_t name, uint32_t stride)
575{
576 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500577 rectangle->x,
578 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500579 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500580 0, 0,
581 rectangle->width,
582 rectangle->height);
583}
584
585void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500586window_copy_surface(struct window *window,
587 struct rectangle *rectangle,
588 cairo_surface_t *surface)
589{
590 wl_surface_copy(window->surface,
591 rectangle->x,
592 rectangle->y,
593 cairo_drm_surface_get_name(surface),
594 cairo_drm_surface_get_stride(surface),
595 0, 0,
596 rectangle->width,
597 rectangle->height);
598}
599
600void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500601window_set_fullscreen(struct window *window, int fullscreen)
602{
603 window->fullscreen = fullscreen;
604 if (window->fullscreen) {
605 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500606 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500607 } else {
608 window->allocation = window->saved_allocation;
609 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500610}
611
612void
613window_set_resize_handler(struct window *window,
614 window_resize_handler_t handler, void *data)
615{
616 window->resize_handler = handler;
617 window->user_data = data;
618}
619
620void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500621window_set_key_handler(struct window *window,
622 window_key_handler_t handler, void *data)
623{
624 window->key_handler = handler;
625 window->user_data = data;
626}
627
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500628void
629window_set_keyboard_focus_handler(struct window *window,
630 window_keyboard_focus_handler_t handler, void *data)
631{
632 window->keyboard_focus_handler = handler;
633 window->user_data = data;
634}
635
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500636struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500637window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500638 int32_t x, int32_t y, int32_t width, int32_t height)
639{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500640 struct window *window;
641
642 window = malloc(sizeof *window);
643 if (window == NULL)
644 return NULL;
645
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500646 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500647 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500648 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500649 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500650 window->allocation.x = x;
651 window->allocation.y = y;
652 window->allocation.width = width;
653 window->allocation.height = height;
654 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500655 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500656 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500657
658 wl_compositor_add_listener(display->compositor,
659 &compositor_listener, window);
660
661 wl_input_device_add_listener(display->input_device,
662 &input_device_listener, window);
663
664 return window;
665}
666
667static void
668display_handle_geometry(void *data,
669 struct wl_output *output,
670 int32_t width, int32_t height)
671{
672 struct display *display = data;
673
674 display->screen_allocation.x = 0;
675 display->screen_allocation.y = 0;
676 display->screen_allocation.width = width;
677 display->screen_allocation.height = height;
678}
679
680static const struct wl_output_listener output_listener = {
681 display_handle_geometry,
682};
683
684static void
685display_handle_global(struct wl_display *display,
686 struct wl_object *object, void *data)
687{
688 struct display *d = data;
689
690 if (wl_object_implements(object, "compositor", 1)) {
691 d->compositor = (struct wl_compositor *) object;
692 } else if (wl_object_implements(object, "output", 1)) {
693 d->output = (struct wl_output *) object;
694 wl_output_add_listener(d->output, &output_listener, d);
695 } else if (wl_object_implements(object, "input_device", 1)) {
696 d->input_device =(struct wl_input_device *) object;
697 }
698}
699
700struct display *
701display_create(struct wl_display *display, int fd)
702{
703 struct display *d;
704
705 d = malloc(sizeof *d);
706 if (d == NULL)
707 return NULL;
708
709 d->display = display;
Kristian Høgsberg26449102009-05-28 20:23:31 -0400710 d->device = cairo_drm_device_get_for_fd(fd);
711 if (d->device == NULL) {
712 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500713 return NULL;
714 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500715
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500716 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500717 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500718 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500719
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500720 /* Process connection events. */
721 wl_display_iterate(display, WL_DISPLAY_READABLE);
722
723 return d;
724}
725
726struct wl_compositor *
727display_get_compositor(struct display *display)
728{
729 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500730}