window.c 94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 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.
 */

23
24
#define _GNU_SOURCE

25
26
#include "../config.h"

27
28
29
30
31
32
33
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
34
#include <assert.h>
35
36
#include <time.h>
#include <cairo.h>
37
#include <sys/mman.h>
38
#include <sys/epoll.h>
39
#include <sys/timerfd.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
40

41
42
#include <pixman.h>

43
44
#include <wayland-egl.h>

45
46
47
48
#ifdef USE_CAIRO_GLESV2
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
49
#include <GL/gl.h>
50
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
51
52
#include <EGL/egl.h>
#include <EGL/eglext.h>
53

54
#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg's avatar
Kristian Høgsberg committed
55
#include <cairo-gl.h>
56
#endif
57

58
#include <xkbcommon/xkbcommon.h>
59
#include <wayland-cursor.h>
60

61
#include <linux/input.h>
62
#include <wayland-client.h>
63
#include "../shared/cairo-util.h"
64
#include "text-cursor-position-client-protocol.h"
65
#include "workspaces-client-protocol.h"
66
#include "../shared/os-compatibility.h"
67

68
#include "window.h"
69

70
struct shm_pool;
71

72
struct display {
73
	struct wl_display *display;
74
	struct wl_compositor *compositor;
75
	struct wl_shell *shell;
76
	struct wl_shm *shm;
77
	struct wl_data_device_manager *data_device_manager;
78
	struct text_cursor_position *text_cursor_position;
79
	struct workspace_manager *workspace_manager;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
80
	EGLDisplay dpy;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
81
	EGLConfig argb_config;
82
83
	EGLContext argb_ctx;
	cairo_device_t *argb_device;
84
	uint32_t serial;
85
86

	int display_fd;
87
	uint32_t display_fd_events;
88
89
90
91
92
93
	uint32_t mask;
	struct task display_task;

	int epoll_fd;
	struct wl_list deferred_list;

Pekka Paalanen's avatar
Pekka Paalanen committed
94
95
	int running;

96
	struct wl_list window_list;
97
	struct wl_list input_list;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
98
	struct wl_list output_list;
99

100
	struct theme *theme;
101

102
	struct wl_cursor_theme *cursor_theme;
103
	struct wl_cursor **cursors;
104

105
106
107
	PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
	PFNEGLCREATEIMAGEKHRPROC create_image;
	PFNEGLDESTROYIMAGEKHRPROC destroy_image;
108
109
110
111

	display_output_handler_t output_configure_handler;

	void *user_data;
112
113

	struct xkb_context *xkb_context;
114
115
116

	uint32_t workspace;
	uint32_t workspace_count;
117
118
};

119
enum {
120
	TYPE_NONE,
121
	TYPE_TOPLEVEL,
122
	TYPE_FULLSCREEN,
123
	TYPE_MAXIMIZED,
124
	TYPE_TRANSIENT,
125
	TYPE_MENU,
126
127
	TYPE_CUSTOM
};
128
129
130
131
132
133

struct window_output {
	struct output *output;
	struct wl_list link;
};

134
135
struct window {
	struct display *display;
136
	struct window *parent;
137
	struct wl_list window_output_list;
138
	struct wl_surface *surface;
Pekka Paalanen's avatar
Pekka Paalanen committed
139
	struct wl_shell_surface *shell_surface;
140
141
	struct wl_region *input_region;
	struct wl_region *opaque_region;
142
	char *title;
143
	struct rectangle allocation, saved_allocation, server_allocation;
144
	struct rectangle min_allocation;
145
	struct rectangle pending_allocation;
146
	int x, y;
147
	int resize_edges;
148
	int redraw_scheduled;
149
	int redraw_needed;
150
	struct task redraw_task;
151
	int resize_needed;
152
	int type;
153
	int transparent;
154
155
	int focus_count;

156
	enum window_buffer_type buffer_type;
157

158
	cairo_surface_t *cairo_surface;
159

160
	struct shm_pool *pool;
161

162
	window_key_handler_t key_handler;
163
	window_keyboard_focus_handler_t keyboard_focus_handler;
164
165
	window_data_handler_t data_handler;
	window_drop_handler_t drop_handler;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
166
	window_close_handler_t close_handler;
167
	window_fullscreen_handler_t fullscreen_handler;
168

169
170
	struct wl_callback *frame_cb;

171
	struct frame *frame;
172
	struct widget *widget;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
173

174
	void *user_data;
175
	struct wl_list link;
176
177
};

178
struct widget {
179
	struct window *window;
180
	struct tooltip *tooltip;
181
	struct wl_list child_list;
182
183
	struct wl_list link;
	struct rectangle allocation;
184
185
	widget_resize_handler_t resize_handler;
	widget_redraw_handler_t redraw_handler;
186
187
	widget_enter_handler_t enter_handler;
	widget_leave_handler_t leave_handler;
188
	widget_motion_handler_t motion_handler;
189
	widget_button_handler_t button_handler;
190
	widget_axis_handler_t axis_handler;
191
	void *user_data;
192
	int opaque;
193
	int tooltip_count;
194
195
};

196
197
struct input {
	struct display *display;
198
199
200
	struct wl_seat *seat;
	struct wl_pointer *pointer;
	struct wl_keyboard *keyboard;
201
202
	struct window *pointer_focus;
	struct window *keyboard_focus;
203
	int current_cursor;
204
205
	uint32_t cursor_anim_start;
	struct wl_callback *cursor_frame_cb;
206
	struct wl_surface *pointer_surface;
207
	uint32_t modifiers;
208
	uint32_t pointer_enter_serial;
209
	uint32_t cursor_serial;
210
	float sx, sy;
211
	struct wl_list link;
212

213
	struct widget *focus_widget;
214
215
216
	struct widget *grab;
	uint32_t grab_button;

217
218
219
	struct wl_data_device *data_device;
	struct data_offer *drag_offer;
	struct data_offer *selection_offer;
220
221
222
223
224
225
226
227

	struct {
		struct xkb_keymap *keymap;
		struct xkb_state *state;
		xkb_mod_mask_t control_mask;
		xkb_mod_mask_t alt_mask;
		xkb_mod_mask_t shift_mask;
	} xkb;
228
229
230
231
232
233

	struct task repeat_task;
	int repeat_timer_fd;
	uint32_t repeat_sym;
	uint32_t repeat_key;
	uint32_t repeat_time;
234
235
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
236
237
238
239
240
struct output {
	struct display *display;
	struct wl_output *output;
	struct rectangle allocation;
	struct wl_list link;
241
	int transform;
242
243
244

	display_output_handler_t destroy_handler;
	void *user_data;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
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
enum frame_button_action {
	FRAME_BUTTON_NULL = 0,
	FRAME_BUTTON_ICON = 1,
	FRAME_BUTTON_CLOSE = 2,
	FRAME_BUTTON_MINIMIZE = 3,
	FRAME_BUTTON_MAXIMIZE = 4,
};

enum frame_button_pointer {
	FRAME_BUTTON_DEFAULT = 0,
	FRAME_BUTTON_OVER = 1,
	FRAME_BUTTON_ACTIVE = 2,
};

enum frame_button_align {
	FRAME_BUTTON_RIGHT = 0,
	FRAME_BUTTON_LEFT = 1,
};

enum frame_button_decoration {
	FRAME_BUTTON_NONE = 0,
	FRAME_BUTTON_FANCY = 1,
};

struct frame_button {
	struct widget *widget;
	struct frame *frame;
	cairo_surface_t *icon;
	enum frame_button_action type;
	enum frame_button_pointer state;
	struct wl_list link;	/* buttons_list */
	enum frame_button_align align;
	enum frame_button_decoration decoration;
};

282
283
284
struct frame {
	struct widget *widget;
	struct widget *child;
285
	struct wl_list buttons_list;
286
287
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
288
289
struct menu {
	struct window *window;
290
	struct widget *widget;
291
	struct input *input;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
292
293
294
295
296
297
298
	const char **entries;
	uint32_t time;
	int current;
	int count;
	menu_func_t func;
};

299
300
301
302
303
304
305
306
307
308
struct tooltip {
	struct widget *parent;
	struct window *window;
	struct widget *widget;
	char *entry;
	struct task tooltip_task;
	int tooltip_fd;
	float x, y;
};

309
310
311
312
313
314
315
struct shm_pool {
	struct wl_shm_pool *pool;
	size_t size;
	size_t used;
	void *data;
};

316
enum {
317
318
	CURSOR_DEFAULT = 100,
	CURSOR_UNSET
319
320
};

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
enum window_location {
	WINDOW_INTERIOR = 0,
	WINDOW_RESIZING_TOP = 1,
	WINDOW_RESIZING_BOTTOM = 2,
	WINDOW_RESIZING_LEFT = 4,
	WINDOW_RESIZING_TOP_LEFT = 5,
	WINDOW_RESIZING_BOTTOM_LEFT = 6,
	WINDOW_RESIZING_RIGHT = 8,
	WINDOW_RESIZING_TOP_RIGHT = 9,
	WINDOW_RESIZING_BOTTOM_RIGHT = 10,
	WINDOW_RESIZING_MASK = 15,
	WINDOW_EXTERIOR = 16,
	WINDOW_TITLEBAR = 17,
	WINDOW_CLIENT_AREA = 18,
};

337
338
339
340
341
static const cairo_user_data_key_t surface_data_key;
struct surface_data {
	struct wl_buffer *buffer;
};

342
343
344
#define MULT(_d,c,a,t) \
	do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)

345
#ifdef HAVE_CAIRO_EGL
346

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
struct egl_window_surface_data {
	struct display *display;
	struct wl_surface *surface;
	struct wl_egl_window *window;
	EGLSurface surf;
};

static void
egl_window_surface_data_destroy(void *p)
{
	struct egl_window_surface_data *data = p;
	struct display *d = data->display;

	eglDestroySurface(d->dpy, data->surf);
	wl_egl_window_destroy(data->window);
	data->surface = NULL;

	free(p);
}

static cairo_surface_t *
display_create_egl_window_surface(struct display *display,
				  struct wl_surface *surface,
370
				  uint32_t flags,
371
372
373
374
				  struct rectangle *rectangle)
{
	cairo_surface_t *cairo_surface;
	struct egl_window_surface_data *data;
375
	EGLConfig config;
376
	cairo_device_t *device;
377
378
379
380
381
382
383
384

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

	data->display = display;
	data->surface = surface;

385
386
	config = display->argb_config;
	device = display->argb_device;
387

388
	data->window = wl_egl_window_create(surface,
389
					    rectangle->width,
390
					    rectangle->height);
391

392
	data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
393
					    data->window, NULL);
394

395
	cairo_surface = cairo_gl_surface_create_for_egl(device,
396
397
398
399
400
401
402
403
404
405
							data->surf,
							rectangle->width,
							rectangle->height);

	cairo_surface_set_user_data(cairo_surface, &surface_data_key,
				    data, egl_window_surface_data_destroy);

	return cairo_surface;
}

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#endif

struct wl_buffer *
display_get_buffer_for_surface(struct display *display,
			       cairo_surface_t *surface)
{
	struct surface_data *data;

	data = cairo_surface_get_user_data (surface, &surface_data_key);

	return data->buffer;
}

struct shm_surface_data {
	struct surface_data data;
421
	struct shm_pool *pool;
422
423
};

424
425
426
static void
shm_pool_destroy(struct shm_pool *pool);

427
static void
428
429
430
431
432
shm_surface_data_destroy(void *p)
{
	struct shm_surface_data *data = p;

	wl_buffer_destroy(data->data.buffer);
433
434
	if (data->pool)
		shm_pool_destroy(data->pool);
435
436

	free(data);
437
438
}

439
440
static struct wl_shm_pool *
make_shm_pool(struct display *display, int size, void **data)
441
{
442
443
	struct wl_shm_pool *pool;
	int fd;
444

445
	fd = os_create_anonymous_file(size);
446
	if (fd < 0) {
447
448
		fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
			size);
449
450
451
		return NULL;
	}

452
453
	*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (*data == MAP_FAILED) {
454
		fprintf(stderr, "mmap failed: %m\n");
455
456
457
458
		close(fd);
		return NULL;
	}

459
460
461
462
463
464
465
	pool = wl_shm_create_pool(display->shm, fd, size);

	close(fd);

	return pool;
}

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
static struct shm_pool *
shm_pool_create(struct display *display, size_t size)
{
	struct shm_pool *pool = malloc(sizeof *pool);

	if (!pool)
		return NULL;

	pool->pool = make_shm_pool(display, size, &pool->data);
	if (!pool->pool) {
		free(pool);
		return NULL;
	}

	pool->size = size;
	pool->used = 0;

	return pool;
}

static void *
shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
{
	if (pool->used + size > pool->size)
		return NULL;

	*offset = pool->used;
	pool->used += size;

	return (char *) pool->data + *offset;
}

/* destroy the pool. this does not unmap the memory though */
static void
shm_pool_destroy(struct shm_pool *pool)
{
	munmap(pool->data, pool->size);
	wl_shm_pool_destroy(pool->pool);
	free(pool);
}

/* Start allocating from the beginning of the pool again */
static void
shm_pool_reset(struct shm_pool *pool)
{
	pool->used = 0;
}

static int
data_length_for_shm_surface(struct rectangle *rect)
{
	int stride;

	stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
						rect->width);
	return stride * rect->height;
}

524
static cairo_surface_t *
525
526
527
display_create_shm_surface_from_pool(struct display *display,
				     struct rectangle *rectangle,
				     uint32_t flags, struct shm_pool *pool)
528
529
530
531
{
	struct shm_surface_data *data;
	uint32_t format;
	cairo_surface_t *surface;
532
	int stride, length, offset;
533
	void *map;
534
535
536
537
538
539
540

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

	stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
						rectangle->width);
541
542
543
544
545
546
547
	length = stride * rectangle->height;
	data->pool = NULL;
	map = shm_pool_allocate(pool, length, &offset);

	if (!map) {
		free(data);
		return NULL;
548
	}
549

550
	surface = cairo_image_surface_create_for_data (map,
551
552
553
554
555
556
557
558
						       CAIRO_FORMAT_ARGB32,
						       rectangle->width,
						       rectangle->height,
						       stride);

	cairo_surface_set_user_data (surface, &surface_data_key,
				     data, shm_surface_data_destroy);

559
	if (flags & SURFACE_OPAQUE)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
560
		format = WL_SHM_FORMAT_XRGB8888;
561
	else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
562
		format = WL_SHM_FORMAT_ARGB8888;
563

564
	data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
565
566
567
						      rectangle->width,
						      rectangle->height,
						      stride, format);
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
	return surface;
}

static cairo_surface_t *
display_create_shm_surface(struct display *display,
			   struct rectangle *rectangle, uint32_t flags,
			   struct window *window)
{
	struct shm_surface_data *data;
	struct shm_pool *pool;
	cairo_surface_t *surface;

	if (window && window->pool) {
		shm_pool_reset(window->pool);
		surface = display_create_shm_surface_from_pool(display,
							       rectangle,
							       flags,
							       window->pool);
		if (surface)
			return surface;
	}

	pool = shm_pool_create(display,
			       data_length_for_shm_surface(rectangle));
	if (!pool)
		return NULL;

	surface =
		display_create_shm_surface_from_pool(display, rectangle,
						     flags, pool);

	if (!surface) {
		shm_pool_destroy(pool);
		return NULL;
	}

	/* make sure we destroy the pool when the surface is destroyed */
	data = cairo_surface_get_user_data(surface, &surface_data_key);
	data->pool = pool;
608
609
610
611

	return surface;
}

Dylan Noblesmith's avatar
Dylan Noblesmith committed
612
613
614
615
616
617
618
619
620
621
622
static int
check_size(struct rectangle *rect)
{
	if (rect->width && rect->height)
		return 0;

	fprintf(stderr, "tried to create surface of "
		"width: %d, height: %d\n", rect->width, rect->height);
	return -1;
}

623
624
cairo_surface_t *
display_create_surface(struct display *display,
625
		       struct wl_surface *surface,
626
627
		       struct rectangle *rectangle,
		       uint32_t flags)
628
{
Dylan Noblesmith's avatar
Dylan Noblesmith committed
629
630
	if (check_size(rectangle) < 0)
		return NULL;
631
#ifdef HAVE_CAIRO_EGL
632
	if (display->dpy && !(flags & SURFACE_SHM))
633
634
635
636
		return display_create_egl_window_surface(display,
							 surface,
							 flags,
							 rectangle);
637
#endif
638
	return display_create_shm_surface(display, rectangle, flags, NULL);
639
640
}

641
642
643
644
645
646
/*
 * The following correspondences between file names and cursors was copied
 * from: https://bugs.kde.org/attachment.cgi?id=67313
 */

static const char *bottom_left_corners[] = {
647
	"bottom_left_corner",
648
649
650
651
	"sw-resize"
};

static const char *bottom_right_corners[] = {
652
	"bottom_right_corner",
653
654
655
656
	"se-resize"
};

static const char *bottom_sides[] = {
657
	"bottom_side",
658
659
660
661
	"s-resize"
};

static const char *grabbings[] = {
662
	"grabbing",
663
664
665
666
667
	"closedhand",
	"208530c400c041818281048008011002"
};

static const char *left_ptrs[] = {
668
	"left_ptr",
669
670
671
672
673
674
	"default",
	"top_left_arrow",
	"left-arrow"
};

static const char *left_sides[] = {
675
	"left_side",
676
677
678
679
	"w-resize"
};

static const char *right_sides[] = {
680
	"right_side",
681
682
683
684
	"e-resize"
};

static const char *top_left_corners[] = {
685
	"top_left_corner",
686
687
688
689
	"nw-resize"
};

static const char *top_right_corners[] = {
690
	"top_right_corner",
691
692
693
694
	"ne-resize"
};

static const char *top_sides[] = {
695
	"top_side",
696
697
698
699
	"n-resize"
};

static const char *xterms[] = {
700
	"xterm",
701
702
703
704
705
	"ibeam",
	"text"
};

static const char *hand1s[] = {
706
	"hand1",
707
708
709
710
711
712
	"pointer",
	"pointing_hand",
	"e29285e634086352946a0e7090d73106"
};

static const char *watches[] = {
713
	"watch",
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
	"wait",
	"0426c94ea35c87780ff01dc239897213"
};

struct cursor_alternatives {
	const char **names;
	size_t count;
};

static const struct cursor_alternatives cursors[] = {
	{bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
	{bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
	{bottom_sides, ARRAY_LENGTH(bottom_sides)},
	{grabbings, ARRAY_LENGTH(grabbings)},
	{left_ptrs, ARRAY_LENGTH(left_ptrs)},
	{left_sides, ARRAY_LENGTH(left_sides)},
	{right_sides, ARRAY_LENGTH(right_sides)},
	{top_left_corners, ARRAY_LENGTH(top_left_corners)},
	{top_right_corners, ARRAY_LENGTH(top_right_corners)},
	{top_sides, ARRAY_LENGTH(top_sides)},
	{xterms, ARRAY_LENGTH(xterms)},
	{hand1s, ARRAY_LENGTH(hand1s)},
	{watches, ARRAY_LENGTH(watches)},
737
738
739
740
741
};

static void
create_cursors(struct display *display)
{
742
743
	char *config_file;
	char *theme = NULL;
744
745
	unsigned int i, j;
	struct wl_cursor *cursor;
746
747
748
749
750
751
752
753
754
755
756
757
	struct config_key shell_keys[] = {
		{ "cursor-theme", CONFIG_KEY_STRING, &theme },
	};
	struct config_section cs[] = {
		{ "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
	};

	config_file = config_file_path("weston.ini");
	parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
	free(config_file);

	display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
758
759
760
	display->cursors =
		malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);

761
	for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
762
763
764
765
766
767
		cursor = NULL;
		for (j = 0; !cursor && j < cursors[i].count; ++j)
			cursor = wl_cursor_theme_get_cursor(
			    display->cursor_theme, cursors[i].names[j]);

		if (!cursor)
768
			fprintf(stderr, "could not load cursor '%s'\n",
769
770
771
				cursors[i].names[0]);

		display->cursors[i] = cursor;
772
	}
773
774
775
776
777
778
}

static void
destroy_cursors(struct display *display)
{
	wl_cursor_theme_destroy(display->cursor_theme);
779
	free(display->cursors);
780
781
}

782
783
struct wl_cursor_image *
display_get_pointer_image(struct display *display, int pointer)
784
{
785
	struct wl_cursor *cursor = display->cursors[pointer];
786

787
	return cursor ? cursor->images[0] : NULL;
788
789
}

790
static void
791
window_get_resize_dx_dy(struct window *window, int *x, int *y)
792
{
793
	if (window->resize_edges & WINDOW_RESIZING_LEFT)
794
		*x = window->server_allocation.width - window->allocation.width;
795
	else
796
		*x = 0;
797

798
	if (window->resize_edges & WINDOW_RESIZING_TOP)
799
800
		*y = window->server_allocation.height -
			window->allocation.height;
801
	else
802
		*y = 0;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
803

804
	window->resize_edges = 0;
805
806
807
808
809
810
811
}

static void
window_attach_surface(struct window *window)
{
	struct display *display = window->display;
	struct wl_buffer *buffer;
812
#ifdef HAVE_CAIRO_EGL
813
	struct egl_window_surface_data *data;
814
#endif
815
	int32_t x, y;
816

817
818
819
820
821
822
	if (window->type == TYPE_NONE) {
		window->type = TYPE_TOPLEVEL;
		if (display->shell)
			wl_shell_surface_set_toplevel(window->shell_surface);
	}

823
	switch (window->buffer_type) {
824
#ifdef HAVE_CAIRO_EGL
825
826
827
828
829
830
831
832
833
	case WINDOW_BUFFER_TYPE_EGL_WINDOW:
		data = cairo_surface_get_user_data(window->cairo_surface,
						   &surface_data_key);

		cairo_gl_surface_swapbuffers(window->cairo_surface);
		wl_egl_window_get_attached_size(data->window,
				&window->server_allocation.width,
				&window->server_allocation.height);
		break;
834
#endif
835
836
837
	case WINDOW_BUFFER_TYPE_SHM:
		buffer =
			display_get_buffer_for_surface(display,
838
						       window->cairo_surface);
839

840
		window_get_resize_dx_dy(window, &x, &y);
841
		wl_surface_attach(window->surface, buffer, x, y);
842
843
844
		wl_surface_damage(window->surface, 0, 0,
				  window->allocation.width,
				  window->allocation.height);
845
		window->server_allocation = window->allocation;
846
847
		cairo_surface_destroy(window->cairo_surface);
		window->cairo_surface = NULL;
848
		break;
849
850
	default:
		return;
851
	}
852

853
854
855
856
857
858
859
860
861
862
863
864
865
	if (window->input_region) {
		wl_surface_set_input_region(window->surface,
					    window->input_region);
		wl_region_destroy(window->input_region);
		window->input_region = NULL;
	}

	if (window->opaque_region) {
		wl_surface_set_opaque_region(window->surface,
					     window->opaque_region);
		wl_region_destroy(window->opaque_region);
		window->opaque_region = NULL;
	}
866
867
}

868
869
870
871
872
873
int
window_has_focus(struct window *window)
{
	return window->focus_count > 0;
}

874
void
875
window_flush(struct window *window)
876
{
877
	if (window->cairo_surface)
878
		window_attach_surface(window);
879
880
}

881
882
void
window_set_surface(struct window *window, cairo_surface_t *surface)
883
{
884
885
	cairo_surface_reference(surface);

886
887
888
	if (window->cairo_surface != NULL)
		cairo_surface_destroy(window->cairo_surface);

889
	window->cairo_surface = surface;
890
891
}

892
#ifdef HAVE_CAIRO_EGL
893
894
895
896
static void
window_resize_cairo_window_surface(struct window *window)
{
	struct egl_window_surface_data *data;
897
	int x, y;
898
899
900
901

	data = cairo_surface_get_user_data(window->cairo_surface,
					   &surface_data_key);

902
	window_get_resize_dx_dy(window, &x, &y),
903
904
	wl_egl_window_resize(data->window,
			     window->allocation.width,
905
906
907
			     window->allocation.height,
			     x,y);

908
909
910
911
	cairo_gl_surface_set_size(window->cairo_surface,
				  window->allocation.width,
				  window->allocation.height);
}
912
#endif
913

914
915
916
917
918
919
struct display *
window_get_display(struct window *window)
{
	return window->display;
}

920
921
922
923
void
window_create_surface(struct window *window)
{
	cairo_surface_t *surface;
924
925
926
927
928
	uint32_t flags = 0;
	
	if (!window->transparent)
		flags = SURFACE_OPAQUE;
	
929
	switch (window->buffer_type) {
930
#ifdef HAVE_CAIRO_EGL
931
932
933
934
935
936
937
	case WINDOW_BUFFER_TYPE_EGL_WINDOW:
		if (window->cairo_surface) {
			window_resize_cairo_window_surface(window);
			return;
		}
		surface = display_create_surface(window->display,
						 window->surface,
938
						 &window->allocation, flags);
939
		break;
940
941
#endif
	case WINDOW_BUFFER_TYPE_SHM:
942
		surface = display_create_shm_surface(window->display,
943
944
						     &window->allocation,
						     flags, window);
945
		break;
946
947
948
        default:
		surface = NULL;
		break;
949
	}
950
951
952

	window_set_surface(window, surface);
	cairo_surface_destroy(surface);
953
954
}

955
956
static void frame_destroy(struct frame *frame);

957
958
959
void
window_destroy(struct window *window)
{
960
961
	struct display *display = window->display;
	struct input *input;
962
963
	struct window_output *window_output;
	struct window_output *window_output_tmp;
964
965
966
967
968
969
970
971
972

	if (window->redraw_scheduled)
		wl_list_remove(&window->redraw_task.link);

	wl_list_for_each(input, &display->input_list, link) {
		if (input->pointer_focus == window)
			input->pointer_focus = NULL;
		if (input->keyboard_focus == window)
			input->keyboard_focus = NULL;
973
974
975
		if (input->focus_widget &&
		    input->focus_widget->window == window)
			input->focus_widget = NULL;
976
977
	}

978
979
980
981
982
	wl_list_for_each_safe(window_output, window_output_tmp,
			      &window->window_output_list, link) {
		free (window_output);
	}

983
984
985
986
987
	if (window->input_region)
		wl_region_destroy(window->input_region);
	if (window->opaque_region)
		wl_region_destroy(window->opaque_region);

988
989
990
	if (window->frame)
		frame_destroy(window->frame);

991
992
	if (window->shell_surface)
		wl_shell_surface_destroy(window->shell_surface);
993
994
	wl_surface_destroy(window->surface);
	wl_list_remove(&window->link);
995
996
997
998

	if (window->cairo_surface != NULL)
		cairo_surface_destroy(window->cairo_surface);

999
1000
	if (window->frame_cb)
		wl_callback_destroy(window->frame_cb);