Skip to content

Commit bc7a4c2

Browse files
authored
wayland: Notes for future readers on use of get_window_with_id and window->terminated. (#59)
1 parent b2cad0a commit bc7a4c2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Headers/wayland/WaylandServer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ struct window
169169
CairoSurface *wcs;
170170
};
171171

172+
/* get_window_with_id returns the known window with the passed ID, or NULL.
173+
*
174+
* NULL is returned when the passed window is not known; for example, the
175+
* window has been '->terminated' and was removed from the window_list already,
176+
* but something has referred to its ID. It is the responsibility of the caller
177+
* to handle the case where NULL is used.
178+
*/
172179
struct window *get_window_with_id(WaylandConfig *wlconfig, int winid);
173180

174181
@interface WaylandServer : GSDisplayServer

Source/wayland/WaylandServer+Xdgshell.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@
4040

4141
if (window->terminated == YES)
4242
{
43+
// 'struct window' is defined in Headers/wayland/WaylandServer.
44+
//
45+
// window->terminated should only be true after
46+
// -[WaylandServer(WindowOps) termwindow:(int)win] sets this to true
47+
// after invoking -[WaylandServer destroyWindowShell:window] and
48+
// using wl_list_remove(&window->link);.
49+
//
50+
// We do not expect 'window' to be referenced again, since
51+
// -destroyWindowShell: invokes wl_display_dispatch_pending(...) and
52+
// wl_display_flush(...);. On the off chance it does, first, we may want
53+
// to patch every single invocation of get_window_with_id() that may
54+
// currently be ignoring the case where NULL may be returned, and
55+
// possibly crashing for that reason.
56+
//
57+
// But, a free here should on its own be fine as long as everyone
58+
// passes around the window ID and does not store a ptr to window itself.
4359
NSDebugLog(@"deleting window win=%d", window->window_id);
4460
free(window);
4561
return;

Source/wayland/WaylandServer.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ static void handle_global_remove(void *data, struct wl_registry *registry,
137137
handle_global, handle_global_remove};
138138

139139
struct window *get_window_with_id(WaylandConfig *wlconfig, int winid) {
140+
/* This can return NULL. A relevant note has been added to the docstring
141+
* in the header. Callers should be handling this. */
140142
struct window *window;
141143

142144
wl_list_for_each(window, &wlconfig->window_list, link) {

0 commit comments

Comments
 (0)