Skip to content

Commit 5f3a35d

Browse files
committed
Add comments to explain what's up
1 parent 08e5b42 commit 5f3a35d

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

main.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ static void noop() {
3232

3333
static void xdg_wm_base_handle_ping(void *data,
3434
struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
35+
// The compositor will send us a ping event to check that we're responsive.
36+
// We need to send back a pong request immediately.
3537
xdg_wm_base_pong(xdg_wm_base, serial);
3638
}
3739

@@ -41,12 +43,17 @@ static const struct xdg_wm_base_listener xdg_wm_base_listener = {
4143

4244
static void xdg_surface_handle_configure(void *data,
4345
struct xdg_surface *xdg_surface, uint32_t serial) {
46+
// The compositor configures our surface, acknowledge the configure event
4447
xdg_surface_ack_configure(xdg_surface, serial);
48+
4549
if (configured) {
50+
// If this isn't the first configure event we've received, we already
51+
// have a buffer attached, so no need to do anything. Commit the
52+
// surface to apply the configure acknowledgement.
4653
wl_surface_commit(surface);
47-
} else {
48-
configured = true;
4954
}
55+
56+
configured = true;
5057
}
5158

5259
static const struct xdg_surface_listener xdg_surface_listener = {
@@ -55,6 +62,7 @@ static const struct xdg_surface_listener xdg_surface_listener = {
5562

5663
static void xdg_toplevel_handle_close(void *data,
5764
struct xdg_toplevel *xdg_toplevel) {
65+
// Stop running if the user requests to close the toplevel
5866
running = false;
5967
}
6068

@@ -67,6 +75,8 @@ static void pointer_handle_button(void *data, struct wl_pointer *pointer,
6775
uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
6876
struct wl_seat *seat = data;
6977

78+
// If the user presses the left pointer button, start an interactive move
79+
// of the toplevel
7080
if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
7181
xdg_toplevel_move(xdg_toplevel, seat, serial);
7282
}
@@ -82,6 +92,8 @@ static const struct wl_pointer_listener pointer_listener = {
8292

8393
static void seat_handle_capabilities(void *data, struct wl_seat *seat,
8494
uint32_t capabilities) {
95+
// If the wl_seat has the pointer capability, start listening to pointer
96+
// events
8597
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
8698
struct wl_pointer *pointer = wl_seat_get_pointer(seat);
8799
wl_pointer_add_listener(pointer, &pointer_listener, seat);
@@ -123,52 +135,59 @@ static struct wl_buffer *create_buffer(void) {
123135
int stride = width * 4;
124136
int size = stride * height;
125137

138+
// Allocate a shared memory file with the right size
126139
int fd = create_shm_file(size);
127140
if (fd < 0) {
128141
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
129142
return NULL;
130143
}
131144

145+
// Map the shared memory file
132146
shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
133147
if (shm_data == MAP_FAILED) {
134148
fprintf(stderr, "mmap failed: %m\n");
135149
close(fd);
136150
return NULL;
137151
}
138152

153+
// Create a wl_buffer from our shared memory file descriptor
139154
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
140155
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height,
141156
stride, WL_SHM_FORMAT_ARGB8888);
142157
wl_shm_pool_destroy(pool);
143158

144-
// MagickImage is from cat.h
159+
// Now that we've mapped the file and created the wl_buffer, we no longer
160+
// need to keep file descriptor opened
161+
close(fd);
162+
163+
// Copy pixels into our shared memory file (MagickImage is from cat.h)
145164
memcpy(shm_data, MagickImage, size);
165+
146166
return buffer;
147167
}
148168

149169
int main(int argc, char *argv[]) {
170+
// Connect to the Wayland compositor
150171
struct wl_display *display = wl_display_connect(NULL);
151172
if (display == NULL) {
152173
fprintf(stderr, "failed to create display\n");
153174
return EXIT_FAILURE;
154175
}
155176

177+
// Obtain the wl_registry and fetch the list of globals
156178
struct wl_registry *registry = wl_display_get_registry(display);
157179
wl_registry_add_listener(registry, &registry_listener, NULL);
158180
if (wl_display_roundtrip(display) == -1) {
159181
return EXIT_FAILURE;
160182
}
161183

184+
// Check that all globals we require are available
162185
if (shm == NULL || compositor == NULL || xdg_wm_base == NULL) {
163186
fprintf(stderr, "no wl_shm, wl_compositor or xdg_wm_base support\n");
164187
return EXIT_FAILURE;
165188
}
166189

167-
struct wl_buffer *buffer = create_buffer();
168-
if (buffer == NULL) {
169-
return EXIT_FAILURE;
170-
}
171-
190+
// Create a wl_surface, a xdg_surface and a xdg_toplevel
172191
surface = wl_compositor_create_surface(compositor);
173192
struct xdg_surface *xdg_surface =
174193
xdg_wm_base_get_xdg_surface(xdg_wm_base, surface);
@@ -177,14 +196,22 @@ int main(int argc, char *argv[]) {
177196
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
178197
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
179198

199+
// Perform the initial commit and wait for the first configure event
180200
wl_surface_commit(surface);
181201
while (wl_display_dispatch(display) != -1 && !configured) {
182202
// This space intentionally left blank
183203
}
184204

205+
// Create a wl_buffer, attach it to the surface and commit the surface
206+
struct wl_buffer *buffer = create_buffer();
207+
if (buffer == NULL) {
208+
return EXIT_FAILURE;
209+
}
210+
185211
wl_surface_attach(surface, buffer, 0, 0);
186212
wl_surface_commit(surface);
187213

214+
// Continue dispatching events until the user closes the toplevel
188215
while (wl_display_dispatch(display) != -1 && running) {
189216
// This space intentionally left blank
190217
}

0 commit comments

Comments
 (0)