Skip to content

Commit b4c2e5d

Browse files
committed
wayland member: a module-only consumer alongside the header one
1 parent 496f26e commit b4c2e5d

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// The module wrappers, used the way a consumer would.
2+
//
3+
// The claim they make is narrow and testable: `import wayland.client;` gives
4+
// you the same entities `#include <wayland-client.h>` does, spelled the same
5+
// way. So this file includes NOTHING from wayland — no header — and still
6+
// calls the stock API. If an export were missing, this would not compile; if a
7+
// name had been renamed or wrapped, it would not compile either.
8+
//
9+
// tests/wayland.cpp is the header-based sibling. Both must pass: the module
10+
// layer is an addition, not a replacement.
11+
12+
#ifdef __linux__
13+
14+
#include <cstdio>
15+
#include <string>
16+
#include <vector>
17+
18+
import wayland.client;
19+
import wayland.server;
20+
import wayland.util;
21+
22+
namespace {
23+
24+
int failures = 0;
25+
26+
void check(bool ok, const char *what)
27+
{
28+
std::printf("%-58s %s\n", what, ok ? "ok" : "FAILED");
29+
if (!ok) ++failures;
30+
}
31+
32+
// A node linked through wl_list, the shape every wayland consumer writes.
33+
struct listener_node {
34+
int id;
35+
wl_list link;
36+
};
37+
38+
} // namespace
39+
40+
int main()
41+
{
42+
// ── 1. Client entities, with no #include anywhere ────────────────────
43+
{
44+
wl_display *d = wl_display_connect("mcpp-no-such-compositor");
45+
check(d == nullptr, "wl_display_connect through the module returns NULL");
46+
47+
// A protocol interface object: generated code, exported by the module.
48+
check(std::string(wl_registry_interface.name) == "wl_registry",
49+
"wl_registry_interface arrives through wayland.client");
50+
check(std::string(wl_compositor_interface.name) == "wl_compositor",
51+
"so does wl_compositor_interface");
52+
}
53+
54+
// ── 2. Server entities ───────────────────────────────────────────────
55+
{
56+
wl_display *s = wl_display_create();
57+
check(s != nullptr, "wl_display_create through wayland.server");
58+
if (s != nullptr) {
59+
wl_event_loop *loop = wl_display_get_event_loop(s);
60+
check(loop != nullptr, "wl_display_get_event_loop returns a loop");
61+
wl_display_destroy(s);
62+
}
63+
}
64+
65+
// ── 3. The macros, as entities ───────────────────────────────────────
66+
// These are the names a module cannot export as macros; wayland.util
67+
// carries them as a constant, a template and ranges. Exercised here in a
68+
// consumer rather than only in the package's own test.
69+
{
70+
wl_list head;
71+
head.prev = &head;
72+
head.next = &head;
73+
74+
listener_node a{1, {}}, b{2, {}};
75+
for (listener_node *n : {&a, &b}) {
76+
wl_list *prev = head.prev;
77+
n->link.prev = prev; n->link.next = &head;
78+
prev->next = &n->link; head.prev = &n->link;
79+
}
80+
81+
std::vector<int> seen;
82+
for (listener_node *n : wl_list_each<&listener_node::link>(&head)) {
83+
seen.push_back(n->id);
84+
}
85+
check(seen.size() == 2 && seen[0] == 1 && seen[1] == 2,
86+
"wl_list_each walks a consumer's own list type");
87+
88+
check(wl_container_of<&listener_node::link>(&b.link) == &b,
89+
"wl_container_of recovers the containing object");
90+
91+
check(WL_MARSHAL_FLAG_DESTROY == 1u,
92+
"WL_MARSHAL_FLAG_DESTROY keeps upstream's value");
93+
}
94+
95+
std::printf("\n%d check(s) failed\n", failures);
96+
return failures == 0 ? 0 : 1;
97+
}
98+
99+
#else
100+
101+
int main() { return 0; }
102+
103+
#endif

0 commit comments

Comments
 (0)