summaryrefslogtreecommitdiff
path: root/window.c
blob: e27ff32c53991b25852dda94b5db1e073cf86edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
 * Copyright © 2008 Kristian Høgsberg
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <glib.h>
#include <cairo-drm.h>

#include <linux/input.h>
#include "wayland-util.h"
#include "wayland-client.h"
#include "wayland-glib.h"
#include "cairo-util.h"

#include "window.h"

struct display {
	struct wl_display *display;
	struct wl_compositor *compositor;
	struct wl_output *output;
	struct wl_input_device *input_device;
	struct rectangle screen_allocation;
	cairo_drm_device_t *device;
	int fd;
};

struct window {
	struct display *display;
	struct wl_surface *surface;
	const char *title;
	struct rectangle allocation, saved_allocation;
	int minimum_width, minimum_height;
	int margin;
	int drag_x, drag_y;
	int state;
	int fullscreen;
	struct wl_input_device *grab_device;
	struct wl_input_device *keyboard_device;
	uint32_t name;
	uint32_t modifiers;

	cairo_surface_t *cairo_surface, *pending_surface;

	window_resize_handler_t resize_handler;
	window_key_handler_t key_handler;
	window_keyboard_focus_handler_t keyboard_focus_handler;
	void *user_data;
};

static void
rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
{
	cairo_move_to(cr, x0, y0 + radius);
	cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
	cairo_line_to(cr, x1 - radius, y0);
	cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
	cairo_line_to(cr, x1, y1 - radius);
	cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
	cairo_line_to(cr, x0 + radius, y1);
	cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
	cairo_close_path(cr);
}

static void
window_attach_surface(struct window *window)
{
	struct wl_visual *visual;

	if (window->pending_surface != NULL)
		return;

	window->pending_surface =
		cairo_surface_reference(window->cairo_surface);

	visual = wl_display_get_premultiplied_argb_visual(window->display->display);
	wl_surface_attach(window->surface,
			  cairo_drm_surface_get_name(window->cairo_surface),
			  window->allocation.width,
			  window->allocation.height,
			  cairo_drm_surface_get_stride(window->cairo_surface),
			  visual);

	wl_surface_map(window->surface,
		       window->allocation.x - window->margin,
		       window->allocation.y - window->margin,
		       window->allocation.width,
		       window->allocation.height);
}

static void
window_draw_decorations(struct window *window)
{
	cairo_t *cr;
	int border = 2, radius = 5;
	cairo_text_extents_t extents;
	cairo_pattern_t *gradient, *outline, *bright, *dim;
	int width, height;
	int shadow_dx = 4, shadow_dy = 4;

	window->cairo_surface =
		cairo_drm_surface_create(window->display->device,
					 CAIRO_CONTENT_COLOR_ALPHA,
					 window->allocation.width,
					 window->allocation.height);

	outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
	bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
	dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);

	cr = cairo_create(window->cairo_surface);

	width = window->allocation.width - window->margin * 2;
	height = window->allocation.height - window->margin * 2;

	cairo_translate(cr, window->margin + shadow_dx,
			window->margin + shadow_dy);
	cairo_set_line_width (cr, border);
	cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
	rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
	cairo_fill(cr);

#define SLOW_BUT_PWETTY
#ifdef SLOW_BUT_PWETTY
	/* FIXME: Aw, pretty drop shadows now have to fallback to sw.
	 * Ideally we should have convolution filters in cairo, but we
	 * can also fallback to compositing the shadow image a bunch
	 * of times according to the blur kernel. */
	{
		cairo_surface_t *map;

		map = cairo_drm_surface_map(window->cairo_surface);
		blur_surface(map, 32);
		cairo_drm_surface_unmap(window->cairo_surface, map);
	}
#endif

	cairo_translate(cr, -shadow_dx, -shadow_dy);
	if (window->keyboard_device) {
		rounded_rect(cr, 0, 0, width, height, radius);
		gradient = cairo_pattern_create_linear (0, 0, 0, 100);
		cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
		cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
		cairo_set_source(cr, gradient);
		cairo_fill(cr);
		cairo_pattern_destroy(gradient);
	} else {
		rounded_rect(cr, 0, 0, width, height, radius);
		cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
		cairo_fill(cr);
	}

	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_move_to(cr, 10, 50);
	cairo_line_to(cr, width - 10, 50);
	cairo_line_to(cr, width - 10, height - 10);
	cairo_line_to(cr, 10, height - 10);
	cairo_close_path(cr);
	cairo_set_source(cr, dim);
	cairo_stroke(cr);

	cairo_move_to(cr, 11, 51);
	cairo_line_to(cr, width - 10, 51);
	cairo_line_to(cr, width - 10, height - 10);
	cairo_line_to(cr, 11, height - 10);
	cairo_close_path(cr);
	cairo_set_source(cr, bright);
	cairo_stroke(cr);

	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	cairo_set_font_size(cr, 14);
	cairo_text_extents(cr, window->title, &extents);
	cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
	cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
	cairo_set_line_width (cr, 4);
	cairo_text_path(cr, window->title);
	if (window->keyboard_device) {
		cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
		cairo_stroke_preserve(cr);
		cairo_set_source_rgb(cr, 1, 1, 1);
		cairo_fill(cr);
	} else {
		cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
		cairo_fill(cr);
	}
	cairo_destroy(cr);

	window_attach_surface(window);
}

static void
window_draw_fullscreen(struct window *window)
{
	window->cairo_surface =
		cairo_drm_surface_create(window->display->device,
					 CAIRO_CONTENT_COLOR_ALPHA,
					 window->allocation.width,
					 window->allocation.height);

	window_attach_surface(window);
}

void
window_draw(struct window *window)
{
	if (window->cairo_surface != NULL)
		cairo_surface_destroy(window->cairo_surface);

	if (window->fullscreen)
		window_draw_fullscreen(window);
	else
		window_draw_decorations(window);
}

static void
window_handle_acknowledge(void *data,
			  struct wl_compositor *compositor,
			  uint32_t key, uint32_t frame)
{
	struct window *window = data;
	cairo_surface_t *pending;

	/* The acknowledge event means that the server
	 * processed our last commit request and we can now
	 * safely free the old window buffer if we resized and
	 * render the next frame into our back buffer.. */

	if (key == 0) {
		pending = window->pending_surface;
		window->pending_surface = NULL;
		if (pending != window->cairo_surface)
			window_attach_surface(window);
		cairo_surface_destroy(pending);
	}
}

static void
window_handle_frame(void *data,
		    struct wl_compositor *compositor,
		    uint32_t frame, uint32_t timestamp)
{
}

static const struct wl_compositor_listener compositor_listener = {
	window_handle_acknowledge,
	window_handle_frame,
};

enum window_state {
	WINDOW_STABLE,
	WINDOW_MOVING,
	WINDOW_RESIZING_UPPER_LEFT,
	WINDOW_RESIZING_UPPER_RIGHT,
	WINDOW_RESIZING_LOWER_LEFT,
	WINDOW_RESIZING_LOWER_RIGHT
};

enum location {
	LOCATION_INTERIOR,
	LOCATION_UPPER_LEFT,
	LOCATION_UPPER_RIGHT,
	LOCATION_LOWER_LEFT,
	LOCATION_LOWER_RIGHT,
	LOCATION_OUTSIDE
};

static void
window_handle_motion(void *data, struct wl_input_device *input_device,
		     int32_t x, int32_t y, int32_t sx, int32_t sy)
{
	struct window *window = data;

	switch (window->state) {
	case WINDOW_MOVING:
		if (window->fullscreen)
			break;
		if (window->grab_device != input_device)
			break;
		window->allocation.x = window->drag_x + x;
		window->allocation.y = window->drag_y + y;
		wl_surface_map(window->surface,
			       window->allocation.x - window->margin,
			       window->allocation.y - window->margin,
			       window->allocation.width,
			       window->allocation.height);
		wl_compositor_commit(window->display->compositor, 1);
		break;
	case WINDOW_RESIZING_LOWER_RIGHT:
		if (window->fullscreen)
			break;
		if (window->grab_device != input_device)
			break;
		window->allocation.width = window->drag_x + x;
		window->allocation.height = window->drag_y + y;

		if (window->resize_handler)
			(*window->resize_handler)(window,
						  window->user_data);

		break;
	}
}

static void window_handle_button(void *data, struct wl_input_device *input_device,
				 uint32_t button, uint32_t state,
				 int32_t x, int32_t y, int32_t sx, int32_t sy)
{
	struct window *window = data;
	int32_t left = window->allocation.x;
	int32_t right = window->allocation.x +
		window->allocation.width - window->margin * 2;
	int32_t top = window->allocation.y;
	int32_t bottom = window->allocation.y +
		window->allocation.height - window->margin * 2;
	int grip_size = 16, location;
	
	if (right - grip_size <= x && x < right &&
	    bottom - grip_size <= y && y < bottom) {
		location = LOCATION_LOWER_RIGHT;
	} else if (left <= x && x < right && top <= y && y < bottom) {
		location = LOCATION_INTERIOR;
	} else {
		location = LOCATION_OUTSIDE;
	}

	if (button == BTN_LEFT && state == 1) {
		switch (location) {
		case LOCATION_INTERIOR:
			window->drag_x = window->allocation.x - x;
			window->drag_y = window->allocation.y - y;
			window->state = WINDOW_MOVING;
			window->grab_device = input_device;
			break;
		case LOCATION_LOWER_RIGHT:
			window->drag_x = window->allocation.width - x;
			window->drag_y = window->allocation.height - y;
			window->state = WINDOW_RESIZING_LOWER_RIGHT;
			window->grab_device = input_device;
			break;
		default:
			window->state = WINDOW_STABLE;
			break;
		}
	} else if (button == BTN_LEFT &&
		   state == 0 && window->grab_device == input_device) {
		window->state = WINDOW_STABLE;
	}
}


struct key {
	uint32_t code[4];
} evdev_keymap[] = {
	{ { 0, 0 } },		/* 0 */
	{ { 0x1b, 0x1b } },
	{ { '1', '!' } },
	{ { '2', '@' } },
	{ { '3', '#' } },
	{ { '4', '$' } },
	{ { '5', '%' } },
	{ { '6', '^' } },
	{ { '7', '&' } },
	{ { '8', '*' } },
	{ { '9', '(' } },
	{ { '0', ')' } },
	{ { '-', '_' } },
	{ { '=', '+' } },
	{ { '\b', '\b' } },
	{ { '\t', '\t' } },

	{ { 'q', 'Q', 0x11 } },		/* 16 */
	{ { 'w', 'W', 0x17 } },
	{ { 'e', 'E', 0x05 } },
	{ { 'r', 'R', 0x12 } },
	{ { 't', 'T', 0x14 } },
	{ { 'y', 'Y', 0x19 } },
	{ { 'u', 'U', 0x15 } },
	{ { 'i', 'I', 0x09 } },
	{ { 'o', 'O', 0x0f } },
	{ { 'p', 'P', 0x10 } },
	{ { '[', '{', 0x1b } },
	{ { ']', '}', 0x1d } },
	{ { '\n', '\n' } },
	{ { 0, 0 } },
	{ { 'a', 'A', 0x01} },
	{ { 's', 'S', 0x13 } },

	{ { 'd', 'D', 0x04 } },		/* 32 */
	{ { 'f', 'F', 0x06 } },
	{ { 'g', 'G', 0x07 } },
	{ { 'h', 'H', 0x08 } },
	{ { 'j', 'J', 0x0a } },
	{ { 'k', 'K', 0x0b } },
	{ { 'l', 'L', 0x0c } },
	{ { ';', ':' } },
	{ { '\'', '"' } },
	{ { '`', '~' } },
	{ { 0, 0 } },
	{ { '\\', '|', 0x1c } },
	{ { 'z', 'Z', 0x1a } },
	{ { 'x', 'X', 0x18 } },
	{ { 'c', 'C', 0x03 } },
	{ { 'v', 'V', 0x16 } },

	{ { 'b', 'B', 0x02 } },		/* 48 */
	{ { 'n', 'N', 0x0e } },
	{ { 'm', 'M', 0x0d } },
	{ { ',', '<' } },
	{ { '.', '>' } },
	{ { '/', '?' } },
	{ { 0, 0 } },
	{ { '*', '*' } },
	{ { 0, 0 } },
	{ { ' ', ' ' } },
	{ { 0, 0 } }

	/* 59 */
};

#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])

static void
window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
{
	uint32_t mod = 0;

	switch (key) {
	case KEY_LEFTSHIFT:
	case KEY_RIGHTSHIFT:
		mod = WINDOW_MODIFIER_SHIFT;
		break;
	case KEY_LEFTCTRL:
	case KEY_RIGHTCTRL:
		mod = WINDOW_MODIFIER_CONTROL;
		break;
	case KEY_LEFTALT:
	case KEY_RIGHTALT:
		mod = WINDOW_MODIFIER_ALT;
		break;
	}

	if (state)
		window->modifiers |= mod;
	else
		window->modifiers &= ~mod;
}

static void
window_handle_key(void *data, struct wl_input_device *input_device,
		  uint32_t key, uint32_t state)
{
	struct window *window = data;
	uint32_t unicode = 0;

	if (window->keyboard_device != input_device)
		return;

	window_update_modifiers(window, key, state);

	if (key < ARRAY_LENGTH(evdev_keymap)) {
		if (window->modifiers & WINDOW_MODIFIER_CONTROL)
			unicode = evdev_keymap[key].code[2];
		else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
			unicode = evdev_keymap[key].code[1];
		else
			unicode = evdev_keymap[key].code[0];
	}

	if (window->key_handler)
		(*window->key_handler)(window, key, unicode,
				       state, window->modifiers, window->user_data);
}

static void
window_handle_pointer_focus(void *data,
			    struct wl_input_device *input_device,
			    struct wl_surface *surface)
{
}

static void
window_handle_keyboard_focus(void *data,
			     struct wl_input_device *input_device,
			     struct wl_surface *surface,
			     struct wl_array *keys)
{
	struct window *window = data;
	uint32_t *k, *end;

	if (window->keyboard_device == input_device && surface != window->surface)
		window->keyboard_device = NULL;
	else if (window->keyboard_device == NULL && surface == window->surface)
		window->keyboard_device = input_device;
	else
		return;

	if (window->keyboard_device) {
		end = keys->data + keys->size;
		for (k = keys->data; k < end; k++)
			window_update_modifiers(window, *k, 1);
	} else {
		window->modifiers = 0;
	}

	if (window->keyboard_focus_handler)
		(*window->keyboard_focus_handler)(window,
						  window->keyboard_device,
						  window->user_data);
}

static const struct wl_input_device_listener input_device_listener = {
	window_handle_motion,
	window_handle_button,
	window_handle_key,
	window_handle_pointer_focus,
	window_handle_keyboard_focus,
};

void
window_get_child_rectangle(struct window *window,
			   struct rectangle *rectangle)
{
	if (window->fullscreen) {
		*rectangle = window->allocation;
	} else {
		rectangle->x = window->margin + 10;
		rectangle->y = window->margin + 50;
		rectangle->width = window->allocation.width - 20 - window->margin * 2;
		rectangle->height = window->allocation.height - 60 - window->margin * 2;
	}
}

void
window_set_child_size(struct window *window,
		      struct rectangle *rectangle)
{
	if (!window->fullscreen) {
		window->allocation.width = rectangle->width + 20 + window->margin * 2;
		window->allocation.height = rectangle->height + 60 + window->margin * 2;
	}
}

cairo_surface_t *
window_create_surface(struct window *window,
		      struct rectangle *rectangle)
{
	return cairo_drm_surface_create(window->display->device,
					CAIRO_CONTENT_COLOR_ALPHA,
					rectangle->width,
					rectangle->height);
}

void
window_copy(struct window *window,
	    struct rectangle *rectangle,
	    uint32_t name, uint32_t stride)
{
	cairo_surface_t *surface;
	cairo_t *cr;

	surface = cairo_drm_surface_create_for_name (window->display->device,
						     name, CAIRO_CONTENT_COLOR_ALPHA,
						     rectangle->width, rectangle->height,
						     stride);

	cr = cairo_create (window->cairo_surface);

	cairo_set_source_surface (cr,
				  surface,
				  rectangle->x, rectangle->y);

	cairo_paint (cr);
	cairo_destroy (cr);
	cairo_surface_destroy (surface);
}

void
window_copy_surface(struct window *window,
		    struct rectangle *rectangle,
		    cairo_surface_t *surface)
{
	cairo_t *cr;

	cr = cairo_create (window->cairo_surface);

	cairo_set_source_surface (cr,
				  surface,
				  rectangle->x, rectangle->y);

	cairo_paint (cr);
	cairo_destroy (cr);
}

void
window_set_fullscreen(struct window *window, int fullscreen)
{
	window->fullscreen = fullscreen;
	if (window->fullscreen) {
		window->saved_allocation = window->allocation;
		window->allocation = window->display->screen_allocation;
	} else {
		window->allocation = window->saved_allocation;
	}
}

void
window_set_resize_handler(struct window *window,
			  window_resize_handler_t handler, void *data)
{
	window->resize_handler = handler;
	window->user_data = data;
}

void
window_set_key_handler(struct window *window,
		       window_key_handler_t handler, void *data)
{
	window->key_handler = handler;
	window->user_data = data;
}

void
window_set_keyboard_focus_handler(struct window *window,
				  window_keyboard_focus_handler_t handler, void *data)
{
	window->keyboard_focus_handler = handler;
	window->user_data = data;
}

struct window *
window_create(struct display *display, const char *title,
	      int32_t x, int32_t y, int32_t width, int32_t height)
{
	struct window *window;

	window = malloc(sizeof *window);
	if (window == NULL)
		return NULL;

	memset(window, 0, sizeof *window);
	window->display = display;
	window->title = strdup(title);
	window->surface = wl_compositor_create_surface(display->compositor);
	window->allocation.x = x;
	window->allocation.y = y;
	window->allocation.width = width;
	window->allocation.height = height;
	window->saved_allocation = window->allocation;
	window->margin = 16;
	window->state = WINDOW_STABLE;

	wl_compositor_add_listener(display->compositor,
				   &compositor_listener, window);

	wl_input_device_add_listener(display->input_device,
				     &input_device_listener, window);

	return window;
}

static void
display_handle_geometry(void *data,
			struct wl_output *output,
			int32_t width, int32_t height)
{
	struct display *display = data;

	display->screen_allocation.x = 0;
	display->screen_allocation.y = 0;
	display->screen_allocation.width = width;
	display->screen_allocation.height = height;
}

static const struct wl_output_listener output_listener = {
	display_handle_geometry,
};

static void
display_handle_global(struct wl_display *display,
		     struct wl_object *object, void *data)
{
	struct display *d = data;

	if (wl_object_implements(object, "compositor", 1)) { 
		d->compositor = (struct wl_compositor *) object;
	} else if (wl_object_implements(object, "output", 1)) {
		d->output = (struct wl_output *) object;
		wl_output_add_listener(d->output, &output_listener, d);
	} else if (wl_object_implements(object, "input_device", 1)) {
		d->input_device =(struct wl_input_device *) object;
	}
}

struct display *
display_create(struct wl_display *display, int fd)
{
	struct display *d;

	d = malloc(sizeof *d);
	if (d == NULL)
		return NULL;

	d->display = display;
	d->device = cairo_drm_device_get_for_fd(fd);
	if (d->device == NULL) {
		fprintf(stderr, "failed to get cairo drm device\n");
		return NULL;
	}

	/* Set up listener so we'll catch all events. */
	wl_display_add_global_listener(display,
				       display_handle_global, d);

	/* Process connection events. */
	wl_display_iterate(display, WL_DISPLAY_READABLE);

	return d;
}

struct wl_compositor *
display_get_compositor(struct display *display)
{
	return display->compositor;
}