blob: 376664e940ca4e7eb100b75cfa3a70669be1c761 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050033#include <cairo-drm.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050035#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050036#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050038#include "wayland-glib.h"
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -050039#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050040
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050042
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050043struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050044 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050045 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050046 struct wl_output *output;
47 struct wl_input_device *input_device;
48 struct rectangle screen_allocation;
Kristian Høgsberg26449102009-05-28 20:23:31 -040049 cairo_drm_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050050 int fd;
51};
52
53struct window {
54 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050055 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050057 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050060 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050062 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050063 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050064 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050065 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050066 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050067
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050068 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050069
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050071 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050072 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050074};
75
Kristian Høgsberge4feb562008-11-08 18:53:37 -050076static void
77rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
78{
79 cairo_move_to(cr, x0, y0 + radius);
80 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
81 cairo_line_to(cr, x1 - radius, y0);
82 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
83 cairo_line_to(cr, x1, y1 - radius);
84 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
85 cairo_line_to(cr, x0 + radius, y1);
86 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
87 cairo_close_path(cr);
88}
89
Kristian Høgsberg0395f302008-12-22 12:14:50 -050090static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050091window_attach_surface(struct window *window)
92{
93 struct wl_visual *visual;
94
95 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
96 wl_surface_attach(window->surface,
97 cairo_drm_surface_get_name(window->cairo_surface),
98 window->allocation.width,
99 window->allocation.height,
100 cairo_drm_surface_get_stride(window->cairo_surface),
101 visual);
102
103 wl_surface_map(window->surface,
104 window->allocation.x - window->margin,
105 window->allocation.y - window->margin,
106 window->allocation.width,
107 window->allocation.height);
108}
109
110static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500111window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500112{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500113 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500114 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500115 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500116 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500117 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400118 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500120 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400121 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500122 CAIRO_CONTENT_COLOR_ALPHA,
123 window->allocation.width,
124 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500125
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500126 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500127 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500128 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
129
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500130 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500131
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500132 width = window->allocation.width - window->margin * 2;
133 height = window->allocation.height - window->margin * 2;
134
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400135 cairo_translate(cr, window->margin + shadow_dx,
136 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500137 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500138 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400139 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500140 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500141
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500142#define SLOW_BUT_PWETTY
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500143#ifdef SLOW_BUT_PWETTY
144 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
145 * Ideally we should have convolution filters in cairo, but we
146 * can also fallback to compositing the shadow image a bunch
147 * of times according to the blur kernel. */
148 {
149 cairo_surface_t *map;
150
151 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500152 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500153 cairo_drm_surface_unmap(window->cairo_surface, map);
154 }
155#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500156
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400157 cairo_translate(cr, -shadow_dx, -shadow_dy);
158 if (window->keyboard_device) {
159 rounded_rect(cr, 0, 0, width, height, radius);
160 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
161 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
162 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
163 cairo_set_source(cr, gradient);
164 cairo_fill(cr);
165 cairo_pattern_destroy(gradient);
166 } else {
167 rounded_rect(cr, 0, 0, width, height, radius);
168 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
169 cairo_fill(cr);
170 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500171
172 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
173 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500174 cairo_line_to(cr, width - 10, 50);
175 cairo_line_to(cr, width - 10, height - 10);
176 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500177 cairo_close_path(cr);
178 cairo_set_source(cr, dim);
179 cairo_stroke(cr);
180
181 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500182 cairo_line_to(cr, width - 10, 51);
183 cairo_line_to(cr, width - 10, height - 10);
184 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500185 cairo_close_path(cr);
186 cairo_set_source(cr, bright);
187 cairo_stroke(cr);
188
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500189 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
190 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500191 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500192 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500193 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
194 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
195 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500196 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400197 if (window->keyboard_device) {
198 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
199 cairo_stroke_preserve(cr);
200 cairo_set_source_rgb(cr, 1, 1, 1);
201 cairo_fill(cr);
202 } else {
203 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
204 cairo_fill(cr);
205 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500206 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500207
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500208 window_attach_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500209}
210
211static void
212window_draw_fullscreen(struct window *window)
213{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500214 window->cairo_surface =
Kristian Høgsberg26449102009-05-28 20:23:31 -0400215 cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500216 CAIRO_CONTENT_COLOR_ALPHA,
217 window->allocation.width,
218 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500219
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500220 window_attach_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500221}
222
223void
224window_draw(struct window *window)
225{
226 if (window->fullscreen)
227 window_draw_fullscreen(window);
228 else
229 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500230}
231
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500232static void
233window_handle_acknowledge(void *data,
234 struct wl_compositor *compositor,
235 uint32_t key, uint32_t frame)
236{
237 struct window *window = data;
238
239 /* The acknowledge event means that the server
240 * processed our last commit request and we can now
241 * safely free the old window buffer if we resized and
242 * render the next frame into our back buffer.. */
243
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500244 if (key == 0 && window->cairo_surface != NULL) {
245 cairo_surface_destroy(window->cairo_surface);
246 window->cairo_surface = NULL;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500247 }
248}
249
250static void
251window_handle_frame(void *data,
252 struct wl_compositor *compositor,
253 uint32_t frame, uint32_t timestamp)
254{
255}
256
257static const struct wl_compositor_listener compositor_listener = {
258 window_handle_acknowledge,
259 window_handle_frame,
260};
261
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500262enum window_state {
263 WINDOW_STABLE,
264 WINDOW_MOVING,
265 WINDOW_RESIZING_UPPER_LEFT,
266 WINDOW_RESIZING_UPPER_RIGHT,
267 WINDOW_RESIZING_LOWER_LEFT,
268 WINDOW_RESIZING_LOWER_RIGHT
269};
270
271enum location {
272 LOCATION_INTERIOR,
273 LOCATION_UPPER_LEFT,
274 LOCATION_UPPER_RIGHT,
275 LOCATION_LOWER_LEFT,
276 LOCATION_LOWER_RIGHT,
277 LOCATION_OUTSIDE
278};
279
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500280static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500281window_handle_motion(void *data, struct wl_input_device *input_device,
282 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500283{
284 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500285
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500286 switch (window->state) {
287 case WINDOW_MOVING:
288 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500290 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500291 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500292 window->allocation.x = window->drag_x + x;
293 window->allocation.y = window->drag_y + y;
294 wl_surface_map(window->surface,
295 window->allocation.x - window->margin,
296 window->allocation.y - window->margin,
297 window->allocation.width,
298 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500299 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500300 break;
301 case WINDOW_RESIZING_LOWER_RIGHT:
302 if (window->fullscreen)
303 break;
304 if (window->grab_device != input_device)
305 break;
306 window->allocation.width = window->drag_x + x;
307 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500308
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500309 if (window->resize_handler)
310 (*window->resize_handler)(window,
311 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500312
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500314 }
315}
316
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500317static void window_handle_button(void *data, struct wl_input_device *input_device,
318 uint32_t button, uint32_t state,
319 int32_t x, int32_t y, int32_t sx, int32_t sy)
320{
321 struct window *window = data;
322 int32_t left = window->allocation.x;
323 int32_t right = window->allocation.x +
324 window->allocation.width - window->margin * 2;
325 int32_t top = window->allocation.y;
326 int32_t bottom = window->allocation.y +
327 window->allocation.height - window->margin * 2;
328 int grip_size = 16, location;
329
330 if (right - grip_size <= x && x < right &&
331 bottom - grip_size <= y && y < bottom) {
332 location = LOCATION_LOWER_RIGHT;
333 } else if (left <= x && x < right && top <= y && y < bottom) {
334 location = LOCATION_INTERIOR;
335 } else {
336 location = LOCATION_OUTSIDE;
337 }
338
339 if (button == BTN_LEFT && state == 1) {
340 switch (location) {
341 case LOCATION_INTERIOR:
342 window->drag_x = window->allocation.x - x;
343 window->drag_y = window->allocation.y - y;
344 window->state = WINDOW_MOVING;
345 window->grab_device = input_device;
346 break;
347 case LOCATION_LOWER_RIGHT:
348 window->drag_x = window->allocation.width - x;
349 window->drag_y = window->allocation.height - y;
350 window->state = WINDOW_RESIZING_LOWER_RIGHT;
351 window->grab_device = input_device;
352 break;
353 default:
354 window->state = WINDOW_STABLE;
355 break;
356 }
357 } else if (button == BTN_LEFT &&
358 state == 0 && window->grab_device == input_device) {
359 window->state = WINDOW_STABLE;
360 }
361}
362
Kristian Høgsberg55444912009-02-21 14:31:09 -0500363
364struct key {
365 uint32_t code[4];
366} evdev_keymap[] = {
367 { { 0, 0 } }, /* 0 */
368 { { 0x1b, 0x1b } },
369 { { '1', '!' } },
370 { { '2', '@' } },
371 { { '3', '#' } },
372 { { '4', '$' } },
373 { { '5', '%' } },
374 { { '6', '^' } },
375 { { '7', '&' } },
376 { { '8', '*' } },
377 { { '9', '(' } },
378 { { '0', ')' } },
379 { { '-', '_' } },
380 { { '=', '+' } },
381 { { '\b', '\b' } },
382 { { '\t', '\t' } },
383
384 { { 'q', 'Q', 0x11 } }, /* 16 */
385 { { 'w', 'W', 0x17 } },
386 { { 'e', 'E', 0x05 } },
387 { { 'r', 'R', 0x12 } },
388 { { 't', 'T', 0x14 } },
389 { { 'y', 'Y', 0x19 } },
390 { { 'u', 'U', 0x15 } },
391 { { 'i', 'I', 0x09 } },
392 { { 'o', 'O', 0x0f } },
393 { { 'p', 'P', 0x10 } },
394 { { '[', '{', 0x1b } },
395 { { ']', '}', 0x1d } },
396 { { '\n', '\n' } },
397 { { 0, 0 } },
398 { { 'a', 'A', 0x01} },
399 { { 's', 'S', 0x13 } },
400
401 { { 'd', 'D', 0x04 } }, /* 32 */
402 { { 'f', 'F', 0x06 } },
403 { { 'g', 'G', 0x07 } },
404 { { 'h', 'H', 0x08 } },
405 { { 'j', 'J', 0x0a } },
406 { { 'k', 'K', 0x0b } },
407 { { 'l', 'L', 0x0c } },
408 { { ';', ':' } },
409 { { '\'', '"' } },
410 { { '`', '~' } },
411 { { 0, 0 } },
412 { { '\\', '|', 0x1c } },
413 { { 'z', 'Z', 0x1a } },
414 { { 'x', 'X', 0x18 } },
415 { { 'c', 'C', 0x03 } },
416 { { 'v', 'V', 0x16 } },
417
418 { { 'b', 'B', 0x02 } }, /* 48 */
419 { { 'n', 'N', 0x0e } },
420 { { 'm', 'M', 0x0d } },
421 { { ',', '<' } },
422 { { '.', '>' } },
423 { { '/', '?' } },
424 { { 0, 0 } },
425 { { '*', '*' } },
426 { { 0, 0 } },
427 { { ' ', ' ' } },
428 { { 0, 0 } }
429
430 /* 59 */
431};
432
433#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
434
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500435static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500436window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500437{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500438 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500439
Kristian Høgsberg55444912009-02-21 14:31:09 -0500440 switch (key) {
441 case KEY_LEFTSHIFT:
442 case KEY_RIGHTSHIFT:
443 mod = WINDOW_MODIFIER_SHIFT;
444 break;
445 case KEY_LEFTCTRL:
446 case KEY_RIGHTCTRL:
447 mod = WINDOW_MODIFIER_CONTROL;
448 break;
449 case KEY_LEFTALT:
450 case KEY_RIGHTALT:
451 mod = WINDOW_MODIFIER_ALT;
452 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500453 }
454
455 if (state)
456 window->modifiers |= mod;
457 else
458 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500459}
460
461static void
462window_handle_key(void *data, struct wl_input_device *input_device,
463 uint32_t key, uint32_t state)
464{
465 struct window *window = data;
466 uint32_t unicode = 0;
467
468 if (window->keyboard_device != input_device)
469 return;
470
471 window_update_modifiers(window, key, state);
472
473 if (key < ARRAY_LENGTH(evdev_keymap)) {
474 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
475 unicode = evdev_keymap[key].code[2];
476 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
477 unicode = evdev_keymap[key].code[1];
478 else
479 unicode = evdev_keymap[key].code[0];
480 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500481
482 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500483 (*window->key_handler)(window, key, unicode,
484 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500485}
486
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500487static void
488window_handle_pointer_focus(void *data,
489 struct wl_input_device *input_device,
490 struct wl_surface *surface)
491{
492}
493
494static void
495window_handle_keyboard_focus(void *data,
496 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500497 struct wl_surface *surface,
498 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500499{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500500 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500501 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500502
503 if (window->keyboard_device == input_device && surface != window->surface)
504 window->keyboard_device = NULL;
505 else if (window->keyboard_device == NULL && surface == window->surface)
506 window->keyboard_device = input_device;
507 else
508 return;
509
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500510 if (window->keyboard_device) {
511 end = keys->data + keys->size;
512 for (k = keys->data; k < end; k++)
513 window_update_modifiers(window, *k, 1);
514 } else {
515 window->modifiers = 0;
516 }
517
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500518 if (window->keyboard_focus_handler)
519 (*window->keyboard_focus_handler)(window,
520 window->keyboard_device,
521 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500522}
523
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500524static const struct wl_input_device_listener input_device_listener = {
525 window_handle_motion,
526 window_handle_button,
527 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500528 window_handle_pointer_focus,
529 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500530};
531
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500532void
533window_get_child_rectangle(struct window *window,
534 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500535{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500536 if (window->fullscreen) {
537 *rectangle = window->allocation;
538 } else {
539 rectangle->x = window->margin + 10;
540 rectangle->y = window->margin + 50;
541 rectangle->width = window->allocation.width - 20 - window->margin * 2;
542 rectangle->height = window->allocation.height - 60 - window->margin * 2;
543 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500544}
545
546void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500547window_set_child_size(struct window *window,
548 struct rectangle *rectangle)
549{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500550 if (!window->fullscreen) {
551 window->allocation.width = rectangle->width + 20 + window->margin * 2;
552 window->allocation.height = rectangle->height + 60 + window->margin * 2;
553 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500554}
555
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500556cairo_surface_t *
557window_create_surface(struct window *window,
558 struct rectangle *rectangle)
559{
Kristian Høgsberg26449102009-05-28 20:23:31 -0400560 return cairo_drm_surface_create(window->display->device,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500561 CAIRO_CONTENT_COLOR_ALPHA,
562 rectangle->width,
563 rectangle->height);
564}
565
Kristian Høgsberg22106762008-12-08 13:50:07 -0500566void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500567window_copy(struct window *window,
568 struct rectangle *rectangle,
569 uint32_t name, uint32_t stride)
570{
571 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500572 rectangle->x,
573 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500574 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500575 0, 0,
576 rectangle->width,
577 rectangle->height);
578}
579
580void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500581window_copy_surface(struct window *window,
582 struct rectangle *rectangle,
583 cairo_surface_t *surface)
584{
585 wl_surface_copy(window->surface,
586 rectangle->x,
587 rectangle->y,
588 cairo_drm_surface_get_name(surface),
589 cairo_drm_surface_get_stride(surface),
590 0, 0,
591 rectangle->width,
592 rectangle->height);
593}
594
595void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500596window_set_fullscreen(struct window *window, int fullscreen)
597{
598 window->fullscreen = fullscreen;
599 if (window->fullscreen) {
600 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500601 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500602 } else {
603 window->allocation = window->saved_allocation;
604 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500605}
606
607void
608window_set_resize_handler(struct window *window,
609 window_resize_handler_t handler, void *data)
610{
611 window->resize_handler = handler;
612 window->user_data = data;
613}
614
615void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500616window_set_key_handler(struct window *window,
617 window_key_handler_t handler, void *data)
618{
619 window->key_handler = handler;
620 window->user_data = data;
621}
622
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500623void
624window_set_keyboard_focus_handler(struct window *window,
625 window_keyboard_focus_handler_t handler, void *data)
626{
627 window->keyboard_focus_handler = handler;
628 window->user_data = data;
629}
630
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500631struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500632window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500633 int32_t x, int32_t y, int32_t width, int32_t height)
634{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500635 struct window *window;
636
637 window = malloc(sizeof *window);
638 if (window == NULL)
639 return NULL;
640
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500641 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500642 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500643 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500644 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500645 window->allocation.x = x;
646 window->allocation.y = y;
647 window->allocation.width = width;
648 window->allocation.height = height;
649 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500650 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500651 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500652
653 wl_compositor_add_listener(display->compositor,
654 &compositor_listener, window);
655
656 wl_input_device_add_listener(display->input_device,
657 &input_device_listener, window);
658
659 return window;
660}
661
662static void
663display_handle_geometry(void *data,
664 struct wl_output *output,
665 int32_t width, int32_t height)
666{
667 struct display *display = data;
668
669 display->screen_allocation.x = 0;
670 display->screen_allocation.y = 0;
671 display->screen_allocation.width = width;
672 display->screen_allocation.height = height;
673}
674
675static const struct wl_output_listener output_listener = {
676 display_handle_geometry,
677};
678
679static void
680display_handle_global(struct wl_display *display,
681 struct wl_object *object, void *data)
682{
683 struct display *d = data;
684
685 if (wl_object_implements(object, "compositor", 1)) {
686 d->compositor = (struct wl_compositor *) object;
687 } else if (wl_object_implements(object, "output", 1)) {
688 d->output = (struct wl_output *) object;
689 wl_output_add_listener(d->output, &output_listener, d);
690 } else if (wl_object_implements(object, "input_device", 1)) {
691 d->input_device =(struct wl_input_device *) object;
692 }
693}
694
695struct display *
696display_create(struct wl_display *display, int fd)
697{
698 struct display *d;
699
700 d = malloc(sizeof *d);
701 if (d == NULL)
702 return NULL;
703
704 d->display = display;
Kristian Høgsberg26449102009-05-28 20:23:31 -0400705 d->device = cairo_drm_device_get_for_fd(fd);
706 if (d->device == NULL) {
707 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500708 return NULL;
709 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500710
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500711 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500712 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500713 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500714
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500715 /* Process connection events. */
716 wl_display_iterate(display, WL_DISPLAY_READABLE);
717
718 return d;
719}
720
721struct wl_compositor *
722display_get_compositor(struct display *display)
723{
724 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500725}