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
|
#include <string.h>
#include <stdio.h>
#include "wayland.h"
#include "drw.h"
#include "xdg-shell-client-protocol.h"
/* macro definitions */
#define match_then_bind(obj, inter, ver) \
if (strcmp(interface, inter.name) == 0) { \
obj = wl_registry_bind(registry, name, &inter, ver);
#define end_match }
#define or_match } else
/* struct definitions */
typedef struct {
unsigned width;
unsigned height;
struct {
struct wl_surface *wl;
struct xdg_surface *xdg;
struct xdg_toplevel *toplevel;
} surface;
Canvas *canvas;
Font *font;
} Window;
static void buffer_release(void *data, struct wl_buffer *buffer);
static void draw_frame();
static void surface_configure(void *data, struct xdg_surface *surface, uint32_t serial);
static void toplevel_configure(void *data, struct xdg_toplevel *toplevel, int32_t width, int32_t height, struct wl_array *states);
static void toplevel_close(void *data, struct xdg_toplevel *toplevel);
static void wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial);
static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version);
static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name);
/* global variables */
static const struct wl_registry_listener registry_listener = {
.global = registry_global,
.global_remove = registry_global_remove
};
static const struct wl_buffer_listener buffer_listener = {
.release = buffer_release
};
static const struct xdg_surface_listener surface_listener = {
.configure = surface_configure,
};
static const struct xdg_toplevel_listener toplevel_listener = {
.configure = toplevel_configure,
.close = toplevel_close
};
static const struct xdg_wm_base_listener wm_base_listener = {
.ping = wm_base_ping
};
struct client_state state = { 0 };
Window win = { 0 };
int running = 1;
#include "config.h"
/* function implementation */
void
buffer_release(void *data, struct wl_buffer *buffer)
{
wl_buffer_destroy(buffer);
}
void
draw_frame()
{
draw_rect(win.canvas, 0, 0, win.width, win.height, 0xff00ff00);
}
void
surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
{
xdg_surface_ack_configure(surface, serial);
fprintf(stderr, "configure\n");
if (win.canvas != 0) {
free_drw(win.canvas);
}
win.canvas = create_drw(state.shm, win.width, win.height);
draw_frame();
wl_surface_attach(win.surface.wl, win.canvas->buffer, 0, 0);
wl_surface_damage_buffer(win.surface.wl, 0, 0, win.width, win.height);
wl_surface_commit(win.surface.wl);
}
void
toplevel_configure(void *data, struct xdg_toplevel *toplevel, int32_t width, int32_t height, struct wl_array *states)
{
if (width == 0 || height == 0) {
win.width = 800;
win.height = 600;
} else {
win.width = width;
win.height = height;
}
}
void
toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
running = 0;
}
void
wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
{
xdg_wm_base_pong(wm_base, serial);
}
void
registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
{
match_then_bind(state.shm, wl_shm_interface, 1)
or_match match_then_bind(state.compositor, wl_compositor_interface, 4)
or_match match_then_bind(state.wm_base, xdg_wm_base_interface, 1)
/* TODO: or_match match_then_bind(state.seat, wl_seat_interface, 7)
wl_seat_add_listener(state.seat, &seat_listener, &state);*/
end_match
}
void
registry_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
/* TODO */
}
int main()
{
state.display = wl_display_connect(0);
state.registry = wl_display_get_registry(state.display);
wl_registry_add_listener(state.registry, ®istry_listener, &state);
wl_display_roundtrip(state.display);
xdg_wm_base_add_listener(state.wm_base, &wm_base_listener, &state);
wl_display_roundtrip(state.display);
win.surface.wl = wl_compositor_create_surface(state.compositor);
win.surface.xdg = xdg_wm_base_get_xdg_surface(state.wm_base, win.surface.wl);
xdg_surface_add_listener(win.surface.xdg, &surface_listener, &win);
win.surface.toplevel = xdg_surface_get_toplevel(win.surface.xdg);
xdg_toplevel_add_listener(win.surface.toplevel, &toplevel_listener, &win);
xdg_toplevel_set_title(win.surface.toplevel, "swt");
wl_surface_commit(win.surface.wl);
while (wl_display_dispatch(state.display) && running) {
}
wl_display_disconnect(state.display);
return 0;
}
|