blob: 43fa3cf36a13d36fc871ccfb15935d842207590f [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øgsberg61017b12008-11-02 18:51:48 -050062 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050063 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050064
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050065 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050068 window_key_handler_t key_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050070};
71
Kristian Høgsberge4feb562008-11-08 18:53:37 -050072static void
73rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
74{
75 cairo_move_to(cr, x0, y0 + radius);
76 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
77 cairo_line_to(cr, x1 - radius, y0);
78 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
79 cairo_line_to(cr, x1, y1 - radius);
80 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
81 cairo_line_to(cr, x0 + radius, y1);
82 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
83 cairo_close_path(cr);
84}
85
Kristian Høgsberg0395f302008-12-22 12:14:50 -050086static void
87window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050089 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050090 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050091 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050092 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050093 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050094 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050095
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050096 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050098 CAIRO_CONTENT_COLOR_ALPHA,
99 window->allocation.width,
100 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500101
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500102 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500103 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500104 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
105
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500106 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500107
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500108 width = window->allocation.width - window->margin * 2;
109 height = window->allocation.height - window->margin * 2;
110
Kristian Høgsberg40979232008-11-25 22:40:39 -0500111 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500112 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500113 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500114 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500115 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500116
117#ifdef SLOW_BUT_PWETTY
118 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
119 * Ideally we should have convolution filters in cairo, but we
120 * can also fallback to compositing the shadow image a bunch
121 * of times according to the blur kernel. */
122 {
123 cairo_surface_t *map;
124
125 map = cairo_drm_surface_map(window->cairo_surface);
126 blur_surface(map);
127 cairo_drm_surface_unmap(window->cairo_surface, map);
128 }
129#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500130
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500131 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500133 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500134 cairo_set_source(cr, outline);
135 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500136 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500137 cairo_set_source(cr, bright);
138 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500139 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500140 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500141 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500142
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500143 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500144 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500145 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
146 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500147 cairo_set_source(cr, gradient);
148 cairo_fill(cr);
149 cairo_pattern_destroy(gradient);
150
151 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
152 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500153 cairo_line_to(cr, width - 10, 50);
154 cairo_line_to(cr, width - 10, height - 10);
155 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500156 cairo_close_path(cr);
157 cairo_set_source(cr, dim);
158 cairo_stroke(cr);
159
160 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500161 cairo_line_to(cr, width - 10, 51);
162 cairo_line_to(cr, width - 10, height - 10);
163 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500164 cairo_close_path(cr);
165 cairo_set_source(cr, bright);
166 cairo_stroke(cr);
167
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500168 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
169 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500170 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500171 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500172 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
173 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
174 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500175 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500176 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
177 cairo_stroke_preserve(cr);
178 cairo_set_source_rgb(cr, 1, 1, 1);
179 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500180 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500181
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500182 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500183 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500184 cairo_drm_surface_get_name(window->cairo_surface),
185 window->allocation.width,
186 window->allocation.height,
187 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500188 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500189
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500190 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500191 window->allocation.x - window->margin,
192 window->allocation.y - window->margin,
193 window->allocation.width,
194 window->allocation.height);
195}
196
197static void
198window_draw_fullscreen(struct window *window)
199{
200 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500201
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500202 window->cairo_surface =
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500203 cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500204 CAIRO_CONTENT_COLOR_ALPHA,
205 window->allocation.width,
206 window->allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500207
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500208 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500209 wl_surface_attach(window->surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500210 cairo_drm_surface_get_name(window->cairo_surface),
211 window->allocation.width,
212 window->allocation.height,
213 cairo_drm_surface_get_stride(window->cairo_surface),
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500214 visual);
215
216 wl_surface_map(window->surface,
217 window->allocation.x,
218 window->allocation.y,
219 window->allocation.width,
220 window->allocation.height);
221}
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
436window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500437 uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500438{
439 struct window *window = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500440 uint32_t mod = 0;
441 uint32_t unicode = 0;
442
443 switch (key) {
444 case KEY_LEFTSHIFT:
445 case KEY_RIGHTSHIFT:
446 mod = WINDOW_MODIFIER_SHIFT;
447 break;
448 case KEY_LEFTCTRL:
449 case KEY_RIGHTCTRL:
450 mod = WINDOW_MODIFIER_CONTROL;
451 break;
452 case KEY_LEFTALT:
453 case KEY_RIGHTALT:
454 mod = WINDOW_MODIFIER_ALT;
455 break;
456 default:
457 if (key < ARRAY_LENGTH(evdev_keymap)) {
458 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
459 unicode = evdev_keymap[key].code[2];
460 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
461 unicode = evdev_keymap[key].code[1];
462 else
463 unicode = evdev_keymap[key].code[0];
464 }
465 break;
466 }
467
468 if (state)
469 window->modifiers |= mod;
470 else
471 window->modifiers &= ~mod;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500472
473 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500474 (*window->key_handler)(window, key, unicode,
475 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500476}
477
478static const struct wl_input_device_listener input_device_listener = {
479 window_handle_motion,
480 window_handle_button,
481 window_handle_key,
482};
483
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500484void
485window_get_child_rectangle(struct window *window,
486 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500487{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500488 if (window->fullscreen) {
489 *rectangle = window->allocation;
490 } else {
491 rectangle->x = window->margin + 10;
492 rectangle->y = window->margin + 50;
493 rectangle->width = window->allocation.width - 20 - window->margin * 2;
494 rectangle->height = window->allocation.height - 60 - window->margin * 2;
495 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500496}
497
498void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500499window_set_child_size(struct window *window,
500 struct rectangle *rectangle)
501{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500502 if (!window->fullscreen) {
503 window->allocation.width = rectangle->width + 20 + window->margin * 2;
504 window->allocation.height = rectangle->height + 60 + window->margin * 2;
505 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500506}
507
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500508cairo_surface_t *
509window_create_surface(struct window *window,
510 struct rectangle *rectangle)
511{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500512 return cairo_drm_surface_create(window->display->ctx,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500513 CAIRO_CONTENT_COLOR_ALPHA,
514 rectangle->width,
515 rectangle->height);
516}
517
Kristian Høgsberg22106762008-12-08 13:50:07 -0500518void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500519window_copy(struct window *window,
520 struct rectangle *rectangle,
521 uint32_t name, uint32_t stride)
522{
523 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500524 rectangle->x,
525 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500526 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500527 0, 0,
528 rectangle->width,
529 rectangle->height);
530}
531
532void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500533window_copy_surface(struct window *window,
534 struct rectangle *rectangle,
535 cairo_surface_t *surface)
536{
537 wl_surface_copy(window->surface,
538 rectangle->x,
539 rectangle->y,
540 cairo_drm_surface_get_name(surface),
541 cairo_drm_surface_get_stride(surface),
542 0, 0,
543 rectangle->width,
544 rectangle->height);
545}
546
547void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500548window_set_fullscreen(struct window *window, int fullscreen)
549{
550 window->fullscreen = fullscreen;
551 if (window->fullscreen) {
552 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500553 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500554 } else {
555 window->allocation = window->saved_allocation;
556 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500557}
558
559void
560window_set_resize_handler(struct window *window,
561 window_resize_handler_t handler, void *data)
562{
563 window->resize_handler = handler;
564 window->user_data = data;
565}
566
567void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500568window_set_key_handler(struct window *window,
569 window_key_handler_t handler, void *data)
570{
571 window->key_handler = handler;
572 window->user_data = data;
573}
574
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500575struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500576window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500577 int32_t x, int32_t y, int32_t width, int32_t height)
578{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500579 struct window *window;
580
581 window = malloc(sizeof *window);
582 if (window == NULL)
583 return NULL;
584
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500585 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500586 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500587 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500588 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500589 window->allocation.x = x;
590 window->allocation.y = y;
591 window->allocation.width = width;
592 window->allocation.height = height;
593 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500594 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500595 window->state = WINDOW_STABLE;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500596
597 wl_compositor_add_listener(display->compositor,
598 &compositor_listener, window);
599
600 wl_input_device_add_listener(display->input_device,
601 &input_device_listener, window);
602
603 return window;
604}
605
606static void
607display_handle_geometry(void *data,
608 struct wl_output *output,
609 int32_t width, int32_t height)
610{
611 struct display *display = data;
612
613 display->screen_allocation.x = 0;
614 display->screen_allocation.y = 0;
615 display->screen_allocation.width = width;
616 display->screen_allocation.height = height;
617}
618
619static const struct wl_output_listener output_listener = {
620 display_handle_geometry,
621};
622
623static void
624display_handle_global(struct wl_display *display,
625 struct wl_object *object, void *data)
626{
627 struct display *d = data;
628
629 if (wl_object_implements(object, "compositor", 1)) {
630 d->compositor = (struct wl_compositor *) object;
631 } else if (wl_object_implements(object, "output", 1)) {
632 d->output = (struct wl_output *) object;
633 wl_output_add_listener(d->output, &output_listener, d);
634 } else if (wl_object_implements(object, "input_device", 1)) {
635 d->input_device =(struct wl_input_device *) object;
636 }
637}
638
639struct display *
640display_create(struct wl_display *display, int fd)
641{
642 struct display *d;
643
644 d = malloc(sizeof *d);
645 if (d == NULL)
646 return NULL;
647
648 d->display = display;
649 d->ctx = cairo_drm_context_get_for_fd(fd);
650 if (d->ctx == NULL) {
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500651 fprintf(stderr, "failed to get cairo drm context\n");
652 return NULL;
653 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500654
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500655 /* Set up listener so we'll catch all events. */
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500656 wl_display_add_global_listener(display,
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500657 display_handle_global, d);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500658
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500659 /* Process connection events. */
660 wl_display_iterate(display, WL_DISPLAY_READABLE);
661
662 return d;
663}
664
665struct wl_compositor *
666display_get_compositor(struct display *display)
667{
668 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500669}