-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.c
183 lines (138 loc) · 3.64 KB
/
window.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <wayland-client.h>
#include "xdg-shell-client-protocol.h"
#include "display.h"
#include "window.h"
#include "region.h"
static void xdg_surface_configure(void *data, struct xdg_surface *surface,
uint32_t serial)
{
xdg_surface_ack_configure(surface, serial);
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_configure,
};
static void xdg_toplevel_configure(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height,
struct wl_array *states)
{
struct window *window = data;
window->configured = true;
}
static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
{
struct window *window = data;
window->closed = true;
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_configure,
.close = xdg_toplevel_close,
};
struct window *window_create(struct display *display, char *app_id, char *title)
{
struct window *window = NULL;
int ret;
if (!display)
goto error;
window = calloc(1, sizeof(*window));
if (!window)
goto error;
window->display = display;
window->surface = wl_compositor_create_surface(display->compositor);
if (!window->surface)
goto error;
window->xdg_surface = xdg_wm_base_get_xdg_surface(display->xdg_shell,
window->surface);
if (!window->xdg_surface)
goto error;
xdg_surface_add_listener(window->xdg_surface, &xdg_surface_listener,
window);
window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
if (!window->xdg_toplevel)
goto error;
xdg_toplevel_add_listener(window->xdg_toplevel, &xdg_toplevel_listener,
window);
xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
xdg_toplevel_set_title(window->xdg_toplevel, title);
wl_surface_commit(window->surface);
ret = wl_display_flush(display->display);
if (ret < 0) {
printf("Failed to flush Wayland display\n");
goto error;
}
return window;
error:
if (window) {
if (window->xdg_toplevel)
xdg_toplevel_destroy(window->xdg_toplevel);
if (window->xdg_surface)
xdg_surface_destroy(window->xdg_surface);
if (window->surface)
wl_surface_destroy(window->surface);
free(window);
}
return NULL;
}
void window_destroy(struct window *window)
{
if (!window)
return;
xdg_toplevel_destroy(window->xdg_toplevel);
xdg_surface_destroy(window->xdg_surface);
wl_surface_destroy(window->surface);
free(window);
}
int window_draw(struct window *window, struct buffer *buffer, struct region *region)
{
struct region region_full = { 0 };
int ret;
if (!window || !buffer)
return -EINVAL;
if (!region) {
region_full.x = 0;
region_full.y = 0;
region_full.width = buffer->width;
region_full.height = buffer->height;
region = ®ion_full;
}
wl_surface_attach(window->surface, buffer->buffer, 0, 0);
wl_surface_damage(window->surface, region->x, region->y, region->width,
region->height);
wl_surface_commit(window->surface);
ret = wl_display_flush(buffer->display->display);
if (ret < 0) {
printf("Failed to flush Wayland display\n");
ret = -errno;
goto error;
}
return 0;
error:
return ret;
}
int window_configured_wait(struct window *window)
{
struct display *display = window->display;
int ret;
while (!window->configured) {
ret = wl_display_dispatch(display->display);
if (ret < 0)
return ret;
}
return 0;
}
int window_closed_wait(struct window *window)
{
struct display *display = window->display;
int ret;
while (!window->closed) {
ret = wl_display_dispatch(display->display);
if (ret < 0)
return ret;
}
return 0;
}