blob: 976d415aa11be1026b4c83883efb92db0848cec5 [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øgsberg7d7b5db2009-09-21 13:43:46 -040099 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500101 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400102 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500103 CAIRO_CONTENT_COLOR_ALPHA,
104 window->allocation.width,
105 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500106
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500107 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500108 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500109 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
110
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500111 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500112
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500113 width = window->allocation.width - window->margin * 2;
114 height = window->allocation.height - window->margin * 2;
115
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400116 cairo_translate(cr, window->margin + shadow_dx,
117 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500118 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500119 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400120 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500121 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500122
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500123#define SLOW_BUT_PWETTY
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500124#ifdef SLOW_BUT_PWETTY
125 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
126 * Ideally we should have convolution filters in cairo, but we
127 * can also fallback to compositing the shadow image a bunch
128 * of times according to the blur kernel. */
129 {
130 cairo_surface_t *map;
131
132 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500133 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500134 cairo_drm_surface_unmap(window->cairo_surface, map);
135 }
136#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500137
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400138 cairo_translate(cr, -shadow_dx, -shadow_dy);
139 if (window->keyboard_device) {
140 rounded_rect(cr, 0, 0, width, height, radius);
141 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
142 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
143 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
144 cairo_set_source(cr, gradient);
145 cairo_fill(cr);
146 cairo_pattern_destroy(gradient);
147 } else {
148 rounded_rect(cr, 0, 0, width, height, radius);
149 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
150 cairo_fill(cr);
151 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500152
153 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
154 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500155 cairo_line_to(cr, width - 10, 50);
156 cairo_line_to(cr, width - 10, height - 10);
157 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500158 cairo_close_path(cr);
159 cairo_set_source(cr, dim);
160 cairo_stroke(cr);
161
162 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500163 cairo_line_to(cr, width - 10, 51);
164 cairo_line_to(cr, width - 10, height - 10);
165 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500166 cairo_close_path(cr);
167 cairo_set_source(cr, bright);
168 cairo_stroke(cr);
169
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500170 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
171 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500172 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500173 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500174 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
175 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
176 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500177 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400178 if (window->keyboard_device) {
179 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
180 cairo_stroke_preserve(cr);
181 cairo_set_source_rgb(cr, 1, 1, 1);
182 cairo_fill(cr);
183 } else {
184 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
185 cairo_fill(cr);
186 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500189 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500190 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500191 cairo_drm_surface_get_name(window->cairo_surface),
192 window->allocation.width,
193 window->allocation.height,
194 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500195 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500196
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500197 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500198 window->allocation.x - window->margin,
199 window->allocation.y - window->margin,
200 window->allocation.width,
201 window->allocation.height);
202}
203
204static void
205window_draw_fullscreen(struct window *window)
206{
207 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500208
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500209 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400210 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500211 CAIRO_CONTENT_COLOR_ALPHA,
212 window->allocation.width,
213 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500214
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500215 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500216 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500217 cairo_drm_surface_get_name(window->cairo_surface),
218 window->allocation.width,
219 window->allocation.height,
220 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500221 visual);
222
223 wl_surface_map(window->surface,
224 window->allocation.x,
225 window->allocation.y,
226 window->allocation.width,
227 window->allocation.height);
228}
229
230void
231window_draw(struct window *window)
232{
233 if (window->fullscreen)
234 window_draw_fullscreen(window);
235 else
236 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500237}
238
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500239static void
240window_handle_acknowledge(void *data,
241 struct wl_compositor *compositor,
242 uint32_t key, uint32_t frame)
243{
244 struct window *window = data;
245
246 /* The acknowledge event means that the server
247 * processed our last commit request and we can now
248 * safely free the old window buffer if we resized and
249 * render the next frame into our back buffer.. */
250
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500251 if (key == 0 && window->cairo_surface != NULL) {
252 cairo_surface_destroy(window->cairo_surface);
253 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500254 }
255}
256
257static void
258window_handle_frame(void *data,
259 struct wl_compositor *compositor,
260 uint32_t frame, uint32_t timestamp)
261{
262}
263
264static const struct wl_compositor_listener compositor_listener = {
265 window_handle_acknowledge,
266 window_handle_frame,
267};
268
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500269enum window_state {
270 WINDOW_STABLE,
271 WINDOW_MOVING,
272 WINDOW_RESIZING_UPPER_LEFT,
273 WINDOW_RESIZING_UPPER_RIGHT,
274 WINDOW_RESIZING_LOWER_LEFT,
275 WINDOW_RESIZING_LOWER_RIGHT
276};
277
278enum location {
279 LOCATION_INTERIOR,
280 LOCATION_UPPER_LEFT,
281 LOCATION_UPPER_RIGHT,
282 LOCATION_LOWER_LEFT,
283 LOCATION_LOWER_RIGHT,
284 LOCATION_OUTSIDE
285};
286
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500287static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500288window_handle_motion(void *data, struct wl_input_device *input_device,
289 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500290{
291 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500292
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500293 switch (window->state) {
294 case WINDOW_MOVING:
295 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500296 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500297 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500299 window->allocation.x = window->drag_x + x;
300 window->allocation.y = window->drag_y + y;
301 wl_surface_map(window->surface,
302 window->allocation.x - window->margin,
303 window->allocation.y - window->margin,
304 window->allocation.width,
305 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500306 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500307 break;
308 case WINDOW_RESIZING_LOWER_RIGHT:
309 if (window->fullscreen)
310 break;
311 if (window->grab_device != input_device)
312 break;
313 window->allocation.width = window->drag_x + x;
314 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500315
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500316 if (window->resize_handler)
317 (*window->resize_handler)(window,
318 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500319
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500320 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500321 }
322}
323
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500324static void window_handle_button(void *data, struct wl_input_device *input_device,
325 uint32_t button, uint32_t state,
326 int32_t x, int32_t y, int32_t sx, int32_t sy)
327{
328 struct window *window = data;
329 int32_t left = window->allocation.x;
330 int32_t right = window->allocation.x +
331 window->allocation.width - window->margin * 2;
332 int32_t top = window->allocation.y;
333 int32_t bottom = window->allocation.y +
334 window->allocation.height - window->margin * 2;
335 int grip_size = 16, location;
336
337 if (right - grip_size <= x && x < right &&
338 bottom - grip_size <= y && y < bottom) {
339 location = LOCATION_LOWER_RIGHT;
340 } else if (left <= x && x < right && top <= y && y < bottom) {
341 location = LOCATION_INTERIOR;
342 } else {
343 location = LOCATION_OUTSIDE;
344 }
345
346 if (button == BTN_LEFT && state == 1) {
347 switch (location) {
348 case LOCATION_INTERIOR:
349 window->drag_x = window->allocation.x - x;
350 window->drag_y = window->allocation.y - y;
351 window->state = WINDOW_MOVING;
352 window->grab_device = input_device;
353 break;
354 case LOCATION_LOWER_RIGHT:
355 window->drag_x = window->allocation.width - x;
356 window->drag_y = window->allocation.height - y;
357 window->state = WINDOW_RESIZING_LOWER_RIGHT;
358 window->grab_device = input_device;
359 break;
360 default:
361 window->state = WINDOW_STABLE;
362 break;
363 }
364 } else if (button == BTN_LEFT &&
365 state == 0 && window->grab_device == input_device) {
366 window->state = WINDOW_STABLE;
367 }
368}
369
Kristian Høgsberg55444912009-02-21 14:31:09 -0500370
371struct key {
372 uint32_t code[4];
373} evdev_keymap[] = {
374 { { 0, 0 } }, /* 0 */
375 { { 0x1b, 0x1b } },
376 { { '1', '!' } },
377 { { '2', '@' } },
378 { { '3', '#' } },
379 { { '4', '$' } },
380 { { '5', '%' } },
381 { { '6', '^' } },
382 { { '7', '&' } },
383 { { '8', '*' } },
384 { { '9', '(' } },
385 { { '0', ')' } },
386 { { '-', '_' } },
387 { { '=', '+' } },
388 { { '\b', '\b' } },
389 { { '\t', '\t' } },
390
391 { { 'q', 'Q', 0x11 } }, /* 16 */
392 { { 'w', 'W', 0x17 } },
393 { { 'e', 'E', 0x05 } },
394 { { 'r', 'R', 0x12 } },
395 { { 't', 'T', 0x14 } },
396 { { 'y', 'Y', 0x19 } },
397 { { 'u', 'U', 0x15 } },
398 { { 'i', 'I', 0x09 } },
399 { { 'o', 'O', 0x0f } },
400 { { 'p', 'P', 0x10 } },
401 { { '[', '{', 0x1b } },
402 { { ']', '}', 0x1d } },
403 { { '\n', '\n' } },
404 { { 0, 0 } },
405 { { 'a', 'A', 0x01} },
406 { { 's', 'S', 0x13 } },
407
408 { { 'd', 'D', 0x04 } }, /* 32 */
409 { { 'f', 'F', 0x06 } },
410 { { 'g', 'G', 0x07 } },
411 { { 'h', 'H', 0x08 } },
412 { { 'j', 'J', 0x0a } },
413 { { 'k', 'K', 0x0b } },
414 { { 'l', 'L', 0x0c } },
415 { { ';', ':' } },
416 { { '\'', '"' } },
417 { { '`', '~' } },
418 { { 0, 0 } },
419 { { '\\', '|', 0x1c } },
420 { { 'z', 'Z', 0x1a } },
421 { { 'x', 'X', 0x18 } },
422 { { 'c', 'C', 0x03 } },
423 { { 'v', 'V', 0x16 } },
424
425 { { 'b', 'B', 0x02 } }, /* 48 */
426 { { 'n', 'N', 0x0e } },
427 { { 'm', 'M', 0x0d } },
428 { { ',', '<' } },
429 { { '.', '>' } },
430 { { '/', '?' } },
431 { { 0, 0 } },
432 { { '*', '*' } },
433 { { 0, 0 } },
434 { { ' ', ' ' } },
435 { { 0, 0 } }
436
437 /* 59 */
438};
439
440#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
441
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500442static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500443window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500444{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500445 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500446
Kristian Høgsberg55444912009-02-21 14:31:09 -0500447 switch (key) {
448 case KEY_LEFTSHIFT:
449 case KEY_RIGHTSHIFT:
450 mod = WINDOW_MODIFIER_SHIFT;
451 break;
452 case KEY_LEFTCTRL:
453 case KEY_RIGHTCTRL:
454 mod = WINDOW_MODIFIER_CONTROL;
455 break;
456 case KEY_LEFTALT:
457 case KEY_RIGHTALT:
458 mod = WINDOW_MODIFIER_ALT;
459 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500460 }
461
462 if (state)
463 window->modifiers |= mod;
464 else
465 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500466}
467
468static void
469window_handle_key(void *data, struct wl_input_device *input_device,
470 uint32_t key, uint32_t state)
471{
472 struct window *window = data;
473 uint32_t unicode = 0;
474
475 if (window->keyboard_device != input_device)
476 return;
477
478 window_update_modifiers(window, key, state);
479
480 if (key < ARRAY_LENGTH(evdev_keymap)) {
481 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
482 unicode = evdev_keymap[key].code[2];
483 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
484 unicode = evdev_keymap[key].code[1];
485 else
486 unicode = evdev_keymap[key].code[0];
487 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500488
489 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500490 (*window->key_handler)(window, key, unicode,
491 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500492}
493
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500494static void
495window_handle_pointer_focus(void *data,
496 struct wl_input_device *input_device,
497 struct wl_surface *surface)
498{
499}
500
501static void
502window_handle_keyboard_focus(void *data,
503 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500504 struct wl_surface *surface,
505 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500506{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500507 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500508 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500509
510 if (window->keyboard_device == input_device && surface != window->surface)
511 window->keyboard_device = NULL;
512 else if (window->keyboard_device == NULL && surface == window->surface)
513 window->keyboard_device = input_device;
514 else
515 return;
516
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500517 if (window->keyboard_device) {
518 end = keys->data + keys->size;
519 for (k = keys->data; k < end; k++)
520 window_update_modifiers(window, *k, 1);
521 } else {
522 window->modifiers = 0;
523 }
524
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500525 if (window->keyboard_focus_handler)
526 (*window->keyboard_focus_handler)(window,
527 window->keyboard_device,
528 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500529}
530
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500531static const struct wl_input_device_listener input_device_listener = {
532 window_handle_motion,
533 window_handle_button,
534 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500535 window_handle_pointer_focus,
536 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500537};
538
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500539void
540window_get_child_rectangle(struct window *window,
541 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500542{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500543 if (window->fullscreen) {
544 *rectangle = window->allocation;
545 } else {
546 rectangle->x = window->margin + 10;
547 rectangle->y = window->margin + 50;
548 rectangle->width = window->allocation.width - 20 - window->margin * 2;
549 rectangle->height = window->allocation.height - 60 - window->margin * 2;
550 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500551}
552
553void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500554window_set_child_size(struct window *window,
555 struct rectangle *rectangle)
556{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500557 if (!window->fullscreen) {
558 window->allocation.width = rectangle->width + 20 + window->margin * 2;
559 window->allocation.height = rectangle->height + 60 + window->margin * 2;
560 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500561}
562
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500563cairo_surface_t *
564window_create_surface(struct window *window,
565 struct rectangle *rectangle)
566{
Kristian Høgsberg26449102009-05-28 20:23:31 -0400567 return cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500568 CAIRO_CONTENT_COLOR_ALPHA,
569 rectangle->width,
570 rectangle->height);
571}
572
Kristian Høgsberg22106762008-12-08 13:50:07 -0500573void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500574window_copy(struct window *window,
575 struct rectangle *rectangle,
576 uint32_t name, uint32_t stride)
577{
578 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500579 rectangle->x,
580 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500581 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500582 0, 0,
583 rectangle->width,
584 rectangle->height);
585}
586
587void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500588window_copy_surface(struct window *window,
589 struct rectangle *rectangle,
590 cairo_surface_t *surface)
591{
592 wl_surface_copy(window->surface,
593 rectangle->x,
594 rectangle->y,
595 cairo_drm_surface_get_name(surface),
596 cairo_drm_surface_get_stride(surface),
597 0, 0,
598 rectangle->width,
599 rectangle->height);
600}
601
602void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500603window_set_fullscreen(struct window *window, int fullscreen)
604{
605 window->fullscreen = fullscreen;
606 if (window->fullscreen) {
607 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500608 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500609 } else {
610 window->allocation = window->saved_allocation;
611 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500612}
613
614void
615window_set_resize_handler(struct window *window,
616 window_resize_handler_t handler, void *data)
617{
618 window->resize_handler = handler;
619 window->user_data = data;
620}
621
622void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500623window_set_key_handler(struct window *window,
624 window_key_handler_t handler, void *data)
625{
626 window->key_handler = handler;
627 window->user_data = data;
628}
629
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500630void
631window_set_keyboard_focus_handler(struct window *window,
632 window_keyboard_focus_handler_t handler, void *data)
633{
634 window->keyboard_focus_handler = handler;
635 window->user_data = data;
636}
637
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500638struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500639window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500640 int32_t x, int32_t y, int32_t width, int32_t height)
641{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500642 struct window *window;
643
644 window = malloc(sizeof *window);
645 if (window == NULL)
646 return NULL;
647
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500648 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500649 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500650 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500651 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500652 window->allocation.x = x;
653 window->allocation.y = y;
654 window->allocation.width = width;
655 window->allocation.height = height;
656 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500657 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500658 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500659
660 wl_compositor_add_listener(display->compositor,
661 &compositor_listener, window);
662
663 wl_input_device_add_listener(display->input_device,
664 &input_device_listener, window);
665
666 return window;
667}
668
669static void
670display_handle_geometry(void *data,
671 struct wl_output *output,
672 int32_t width, int32_t height)
673{
674 struct display *display = data;
675
676 display->screen_allocation.x = 0;
677 display->screen_allocation.y = 0;
678 display->screen_allocation.width = width;
679 display->screen_allocation.height = height;
680}
681
682static const struct wl_output_listener output_listener = {
683 display_handle_geometry,
684};
685
686static void
687display_handle_global(struct wl_display *display,
688 struct wl_object *object, void *data)
689{
690 struct display *d = data;
691
692 if (wl_object_implements(object, "compositor", 1)) {
693 d->compositor = (struct wl_compositor *) object;
694 } else if (wl_object_implements(object, "output", 1)) {
695 d->output = (struct wl_output *) object;
696 wl_output_add_listener(d->output, &output_listener, d);
697 } else if (wl_object_implements(object, "input_device", 1)) {
698 d->input_device =(struct wl_input_device *) object;
699 }
700}
701
702struct display *
703display_create(struct wl_display *display, int fd)
704{
705 struct display *d;
706
707 d = malloc(sizeof *d);
708 if (d == NULL)
709 return NULL;
710
711 d->display = display;
Kristian Høgsberg26449102009-05-28 20:23:31 -0400712 d->device = cairo_drm_device_get_for_fd(fd);
713 if (d->device == NULL) {
714 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500715 return NULL;
716 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500717
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500718 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500719 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500720 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500721
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500722 /* Process connection events. */
723 wl_display_iterate(display, WL_DISPLAY_READABLE);
724
725 return d;
726}
727
728struct wl_compositor *
729display_get_compositor(struct display *display)
730{
731 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500732}