blob: b306a23cc53c0fa6b7154d35595ac354f765e071 [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øgsberg61017b12008-11-02 18:51:48 -050036#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050038
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050040
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050041struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050042 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050043 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050044 struct wl_output *output;
45 struct wl_input_device *input_device;
46 struct rectangle screen_allocation;
47 cairo_drm_context_t *ctx;
48 int fd;
49};
50
51struct window {
52 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050055 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050057 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050058 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050059 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050060 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050061 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050062 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050064 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050065
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050066 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050069 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050070 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050071 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050072};
73
Kristian Høgsberge4feb562008-11-08 18:53:37 -050074static void
75rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
76{
77 cairo_move_to(cr, x0, y0 + radius);
78 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
79 cairo_line_to(cr, x1 - radius, y0);
80 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
81 cairo_line_to(cr, x1, y1 - radius);
82 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
83 cairo_line_to(cr, x0 + radius, y1);
84 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
85 cairo_close_path(cr);
86}
87
Kristian Høgsberg0395f302008-12-22 12:14:50 -050088static void
89window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050090{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050092 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050093 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050094 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050095 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050096 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050097
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050098 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050099 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500100 CAIRO_CONTENT_COLOR_ALPHA,
101 window->allocation.width,
102 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500104 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500105 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500106 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
107
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500108 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500109
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500110 width = window->allocation.width - window->margin * 2;
111 height = window->allocation.height - window->margin * 2;
112
Kristian Høgsberg40979232008-11-25 22:40:39 -0500113 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500114 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500115 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500116 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500117 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500118
119#ifdef SLOW_BUT_PWETTY
120 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
121 * Ideally we should have convolution filters in cairo, but we
122 * can also fallback to compositing the shadow image a bunch
123 * of times according to the blur kernel. */
124 {
125 cairo_surface_t *map;
126
127 map = cairo_drm_surface_map(window->cairo_surface);
128 blur_surface(map);
129 cairo_drm_surface_unmap(window->cairo_surface, map);
130 }
131#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500132
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500133 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500134 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500135 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500136 cairo_set_source(cr, outline);
137 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500138 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500139 cairo_set_source(cr, bright);
140 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500141 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500142 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500143 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500144
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500145 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500146 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500147 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
148 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500149 cairo_set_source(cr, gradient);
150 cairo_fill(cr);
151 cairo_pattern_destroy(gradient);
152
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øgsberge4feb562008-11-08 18:53:37 -0500178 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
179 cairo_stroke_preserve(cr);
180 cairo_set_source_rgb(cr, 1, 1, 1);
181 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500182 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500183
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500184 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500185 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500186 cairo_drm_surface_get_name(window->cairo_surface),
187 window->allocation.width,
188 window->allocation.height,
189 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500190 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500191
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500192 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500193 window->allocation.x - window->margin,
194 window->allocation.y - window->margin,
195 window->allocation.width,
196 window->allocation.height);
197}
198
199static void
200window_draw_fullscreen(struct window *window)
201{
202 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500203
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500204 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500205 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500206 CAIRO_CONTENT_COLOR_ALPHA,
207 window->allocation.width,
208 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500209
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500210 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500211 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500212 cairo_drm_surface_get_name(window->cairo_surface),
213 window->allocation.width,
214 window->allocation.height,
215 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500216 visual);
217
218 wl_surface_map(window->surface,
219 window->allocation.x,
220 window->allocation.y,
221 window->allocation.width,
222 window->allocation.height);
223}
224
225void
226window_draw(struct window *window)
227{
228 if (window->fullscreen)
229 window_draw_fullscreen(window);
230 else
231 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500232}
233
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500234static void
235window_handle_acknowledge(void *data,
236 struct wl_compositor *compositor,
237 uint32_t key, uint32_t frame)
238{
239 struct window *window = data;
240
241 /* The acknowledge event means that the server
242 * processed our last commit request and we can now
243 * safely free the old window buffer if we resized and
244 * render the next frame into our back buffer.. */
245
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500246 if (key == 0 && window->cairo_surface != NULL) {
247 cairo_surface_destroy(window->cairo_surface);
248 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500249 }
250}
251
252static void
253window_handle_frame(void *data,
254 struct wl_compositor *compositor,
255 uint32_t frame, uint32_t timestamp)
256{
257}
258
259static const struct wl_compositor_listener compositor_listener = {
260 window_handle_acknowledge,
261 window_handle_frame,
262};
263
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500264enum window_state {
265 WINDOW_STABLE,
266 WINDOW_MOVING,
267 WINDOW_RESIZING_UPPER_LEFT,
268 WINDOW_RESIZING_UPPER_RIGHT,
269 WINDOW_RESIZING_LOWER_LEFT,
270 WINDOW_RESIZING_LOWER_RIGHT
271};
272
273enum location {
274 LOCATION_INTERIOR,
275 LOCATION_UPPER_LEFT,
276 LOCATION_UPPER_RIGHT,
277 LOCATION_LOWER_LEFT,
278 LOCATION_LOWER_RIGHT,
279 LOCATION_OUTSIDE
280};
281
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500282static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500283window_handle_motion(void *data, struct wl_input_device *input_device,
284 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500285{
286 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500287
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500288 switch (window->state) {
289 case WINDOW_MOVING:
290 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500291 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500292 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500293 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500294 window->allocation.x = window->drag_x + x;
295 window->allocation.y = window->drag_y + y;
296 wl_surface_map(window->surface,
297 window->allocation.x - window->margin,
298 window->allocation.y - window->margin,
299 window->allocation.width,
300 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500301 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500302 break;
303 case WINDOW_RESIZING_LOWER_RIGHT:
304 if (window->fullscreen)
305 break;
306 if (window->grab_device != input_device)
307 break;
308 window->allocation.width = window->drag_x + x;
309 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500310
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500311 if (window->resize_handler)
312 (*window->resize_handler)(window,
313 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500314
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500315 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500316 }
317}
318
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500319static void window_handle_button(void *data, struct wl_input_device *input_device,
320 uint32_t button, uint32_t state,
321 int32_t x, int32_t y, int32_t sx, int32_t sy)
322{
323 struct window *window = data;
324 int32_t left = window->allocation.x;
325 int32_t right = window->allocation.x +
326 window->allocation.width - window->margin * 2;
327 int32_t top = window->allocation.y;
328 int32_t bottom = window->allocation.y +
329 window->allocation.height - window->margin * 2;
330 int grip_size = 16, location;
331
332 if (right - grip_size <= x && x < right &&
333 bottom - grip_size <= y && y < bottom) {
334 location = LOCATION_LOWER_RIGHT;
335 } else if (left <= x && x < right && top <= y && y < bottom) {
336 location = LOCATION_INTERIOR;
337 } else {
338 location = LOCATION_OUTSIDE;
339 }
340
341 if (button == BTN_LEFT && state == 1) {
342 switch (location) {
343 case LOCATION_INTERIOR:
344 window->drag_x = window->allocation.x - x;
345 window->drag_y = window->allocation.y - y;
346 window->state = WINDOW_MOVING;
347 window->grab_device = input_device;
348 break;
349 case LOCATION_LOWER_RIGHT:
350 window->drag_x = window->allocation.width - x;
351 window->drag_y = window->allocation.height - y;
352 window->state = WINDOW_RESIZING_LOWER_RIGHT;
353 window->grab_device = input_device;
354 break;
355 default:
356 window->state = WINDOW_STABLE;
357 break;
358 }
359 } else if (button == BTN_LEFT &&
360 state == 0 && window->grab_device == input_device) {
361 window->state = WINDOW_STABLE;
362 }
363}
364
Kristian Høgsberg55444912009-02-21 14:31:09 -0500365
366struct key {
367 uint32_t code[4];
368} evdev_keymap[] = {
369 { { 0, 0 } }, /* 0 */
370 { { 0x1b, 0x1b } },
371 { { '1', '!' } },
372 { { '2', '@' } },
373 { { '3', '#' } },
374 { { '4', '$' } },
375 { { '5', '%' } },
376 { { '6', '^' } },
377 { { '7', '&' } },
378 { { '8', '*' } },
379 { { '9', '(' } },
380 { { '0', ')' } },
381 { { '-', '_' } },
382 { { '=', '+' } },
383 { { '\b', '\b' } },
384 { { '\t', '\t' } },
385
386 { { 'q', 'Q', 0x11 } }, /* 16 */
387 { { 'w', 'W', 0x17 } },
388 { { 'e', 'E', 0x05 } },
389 { { 'r', 'R', 0x12 } },
390 { { 't', 'T', 0x14 } },
391 { { 'y', 'Y', 0x19 } },
392 { { 'u', 'U', 0x15 } },
393 { { 'i', 'I', 0x09 } },
394 { { 'o', 'O', 0x0f } },
395 { { 'p', 'P', 0x10 } },
396 { { '[', '{', 0x1b } },
397 { { ']', '}', 0x1d } },
398 { { '\n', '\n' } },
399 { { 0, 0 } },
400 { { 'a', 'A', 0x01} },
401 { { 's', 'S', 0x13 } },
402
403 { { 'd', 'D', 0x04 } }, /* 32 */
404 { { 'f', 'F', 0x06 } },
405 { { 'g', 'G', 0x07 } },
406 { { 'h', 'H', 0x08 } },
407 { { 'j', 'J', 0x0a } },
408 { { 'k', 'K', 0x0b } },
409 { { 'l', 'L', 0x0c } },
410 { { ';', ':' } },
411 { { '\'', '"' } },
412 { { '`', '~' } },
413 { { 0, 0 } },
414 { { '\\', '|', 0x1c } },
415 { { 'z', 'Z', 0x1a } },
416 { { 'x', 'X', 0x18 } },
417 { { 'c', 'C', 0x03 } },
418 { { 'v', 'V', 0x16 } },
419
420 { { 'b', 'B', 0x02 } }, /* 48 */
421 { { 'n', 'N', 0x0e } },
422 { { 'm', 'M', 0x0d } },
423 { { ',', '<' } },
424 { { '.', '>' } },
425 { { '/', '?' } },
426 { { 0, 0 } },
427 { { '*', '*' } },
428 { { 0, 0 } },
429 { { ' ', ' ' } },
430 { { 0, 0 } }
431
432 /* 59 */
433};
434
435#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
436
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500437static void
438window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500439 uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500440{
441 struct window *window = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500442 uint32_t mod = 0;
443 uint32_t unicode = 0;
444
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500445 if (window->keyboard_device != input_device)
446 return;
447
Kristian Høgsberg55444912009-02-21 14:31:09 -0500448 switch (key) {
449 case KEY_LEFTSHIFT:
450 case KEY_RIGHTSHIFT:
451 mod = WINDOW_MODIFIER_SHIFT;
452 break;
453 case KEY_LEFTCTRL:
454 case KEY_RIGHTCTRL:
455 mod = WINDOW_MODIFIER_CONTROL;
456 break;
457 case KEY_LEFTALT:
458 case KEY_RIGHTALT:
459 mod = WINDOW_MODIFIER_ALT;
460 break;
461 default:
462 if (key < ARRAY_LENGTH(evdev_keymap)) {
463 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
464 unicode = evdev_keymap[key].code[2];
465 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
466 unicode = evdev_keymap[key].code[1];
467 else
468 unicode = evdev_keymap[key].code[0];
469 }
470 break;
471 }
472
473 if (state)
474 window->modifiers |= mod;
475 else
476 window->modifiers &= ~mod;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500477
478 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500479 (*window->key_handler)(window, key, unicode,
480 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500481}
482
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500483static void
484window_handle_pointer_focus(void *data,
485 struct wl_input_device *input_device,
486 struct wl_surface *surface)
487{
488}
489
490static void
491window_handle_keyboard_focus(void *data,
492 struct wl_input_device *input_device,
493 struct wl_surface *surface)
494{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500495 struct window *window = data;
496
497 if (window->keyboard_device == input_device && surface != window->surface)
498 window->keyboard_device = NULL;
499 else if (window->keyboard_device == NULL && surface == window->surface)
500 window->keyboard_device = input_device;
501 else
502 return;
503
504 if (window->keyboard_focus_handler)
505 (*window->keyboard_focus_handler)(window,
506 window->keyboard_device,
507 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500508}
509
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500510static const struct wl_input_device_listener input_device_listener = {
511 window_handle_motion,
512 window_handle_button,
513 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500514 window_handle_pointer_focus,
515 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500516};
517
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500518void
519window_get_child_rectangle(struct window *window,
520 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500521{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500522 if (window->fullscreen) {
523 *rectangle = window->allocation;
524 } else {
525 rectangle->x = window->margin + 10;
526 rectangle->y = window->margin + 50;
527 rectangle->width = window->allocation.width - 20 - window->margin * 2;
528 rectangle->height = window->allocation.height - 60 - window->margin * 2;
529 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500530}
531
532void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500533window_set_child_size(struct window *window,
534 struct rectangle *rectangle)
535{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500536 if (!window->fullscreen) {
537 window->allocation.width = rectangle->width + 20 + window->margin * 2;
538 window->allocation.height = rectangle->height + 60 + window->margin * 2;
539 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500540}
541
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500542cairo_surface_t *
543window_create_surface(struct window *window,
544 struct rectangle *rectangle)
545{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500546 return cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500547 CAIRO_CONTENT_COLOR_ALPHA,
548 rectangle->width,
549 rectangle->height);
550}
551
Kristian Høgsberg22106762008-12-08 13:50:07 -0500552void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500553window_copy(struct window *window,
554 struct rectangle *rectangle,
555 uint32_t name, uint32_t stride)
556{
557 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500558 rectangle->x,
559 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500560 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500561 0, 0,
562 rectangle->width,
563 rectangle->height);
564}
565
566void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500567window_copy_surface(struct window *window,
568 struct rectangle *rectangle,
569 cairo_surface_t *surface)
570{
571 wl_surface_copy(window->surface,
572 rectangle->x,
573 rectangle->y,
574 cairo_drm_surface_get_name(surface),
575 cairo_drm_surface_get_stride(surface),
576 0, 0,
577 rectangle->width,
578 rectangle->height);
579}
580
581void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500582window_set_fullscreen(struct window *window, int fullscreen)
583{
584 window->fullscreen = fullscreen;
585 if (window->fullscreen) {
586 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500587 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500588 } else {
589 window->allocation = window->saved_allocation;
590 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500591}
592
593void
594window_set_resize_handler(struct window *window,
595 window_resize_handler_t handler, void *data)
596{
597 window->resize_handler = handler;
598 window->user_data = data;
599}
600
601void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500602window_set_key_handler(struct window *window,
603 window_key_handler_t handler, void *data)
604{
605 window->key_handler = handler;
606 window->user_data = data;
607}
608
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500609void
610window_set_keyboard_focus_handler(struct window *window,
611 window_keyboard_focus_handler_t handler, void *data)
612{
613 window->keyboard_focus_handler = handler;
614 window->user_data = data;
615}
616
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500617struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500618window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500619 int32_t x, int32_t y, int32_t width, int32_t height)
620{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500621 struct window *window;
622
623 window = malloc(sizeof *window);
624 if (window == NULL)
625 return NULL;
626
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500627 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500628 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500629 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500630 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500631 window->allocation.x = x;
632 window->allocation.y = y;
633 window->allocation.width = width;
634 window->allocation.height = height;
635 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500636 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500637 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500638
639 wl_compositor_add_listener(display->compositor,
640 &compositor_listener, window);
641
642 wl_input_device_add_listener(display->input_device,
643 &input_device_listener, window);
644
645 return window;
646}
647
648static void
649display_handle_geometry(void *data,
650 struct wl_output *output,
651 int32_t width, int32_t height)
652{
653 struct display *display = data;
654
655 display->screen_allocation.x = 0;
656 display->screen_allocation.y = 0;
657 display->screen_allocation.width = width;
658 display->screen_allocation.height = height;
659}
660
661static const struct wl_output_listener output_listener = {
662 display_handle_geometry,
663};
664
665static void
666display_handle_global(struct wl_display *display,
667 struct wl_object *object, void *data)
668{
669 struct display *d = data;
670
671 if (wl_object_implements(object, "compositor", 1)) {
672 d->compositor = (struct wl_compositor *) object;
673 } else if (wl_object_implements(object, "output", 1)) {
674 d->output = (struct wl_output *) object;
675 wl_output_add_listener(d->output, &output_listener, d);
676 } else if (wl_object_implements(object, "input_device", 1)) {
677 d->input_device =(struct wl_input_device *) object;
678 }
679}
680
681struct display *
682display_create(struct wl_display *display, int fd)
683{
684 struct display *d;
685
686 d = malloc(sizeof *d);
687 if (d == NULL)
688 return NULL;
689
690 d->display = display;
691 d->ctx = cairo_drm_context_get_for_fd(fd);
692 if (d->ctx == NULL) {
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500693 fprintf(stderr, "failed to get cairo drm context\n");
694 return NULL;
695 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500696
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500697 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500698 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500699 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500700
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500701 /* Process connection events. */
702 wl_display_iterate(display, WL_DISPLAY_READABLE);
703
704 return d;
705}
706
707struct wl_compositor *
708display_get_compositor(struct display *display)
709{
710 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500711}