annotate dep/animia/src/win/x11.cc @ 234:8ccf0302afb1

dep/animia: convert xlib code to xcb I'm not *entirely* sure if this will work correctly for everything, but it works fine enough
author Paper <mrpapersonic@gmail.com>
date Tue, 16 Jan 2024 08:08:42 -0500
parents 8d35061e7505
children 593108b3d555
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
1 #include "animia/win/x11.h"
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
2 #include "animia/win.h"
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
3 #include "animia.h"
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
4
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
5 #include <xcb/xcb.h>
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
6 #include <xcb/res.h>
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 157
diff changeset
7
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
8 #include <cstdint>
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
9 #include <climits>
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
10 #include <cstring>
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
11 #include <string>
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 157
diff changeset
12 #include <set>
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
14 #include <iostream>
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
15
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
16 static size_t str_nlen(const char* s, size_t len) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
17 size_t i = 0;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
18 for (; i < len && s[i]; i++);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
19 return i;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
20 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
21
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 namespace animia::internal::x11 {
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
24 static void GetWindowPID(xcb_connection_t* connection, const std::vector<xcb_window_t>& windows, std::unordered_map<xcb_window_t, pid_t>& result) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
25 std::vector<xcb_res_query_client_ids_cookie_t> cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
26 cookies.reserve(windows.size());
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
27
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
28 for (const auto& window : windows) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
29 xcb_res_client_id_spec_t spec = {
199
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
30 .client = window,
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
31 .mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
199
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
32 };
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
33
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
34 cookies.push_back(::xcb_res_query_client_ids(connection, 1, &spec));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
35 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
36
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
37 for (size_t i = 0; i < cookies.size(); i++) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
38 xcb_res_query_client_ids_reply_t* reply = ::xcb_res_query_client_ids_reply(connection, cookies.at(i), NULL);
199
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
39
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
40 xcb_res_client_id_value_iterator_t it = xcb_res_query_client_ids_ids_iterator(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
41 for (; it.rem; xcb_res_client_id_value_next(&it)) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
42 if (it.data->spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
43 result[windows.at(i)] = *::xcb_res_client_id_value_value(it.data);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
44 continue;
199
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
45 }
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
46 }
9f3534f6b8c4 dep/animia: initial Wayland support, drop non-working kvm fd plugin
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
47 }
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 }
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
50 static bool GetAllTopLevelWindowsEWMH(xcb_connection_t* connection, const std::vector<xcb_window_t>& roots, std::set<xcb_window_t>& result) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
51 const xcb_atom_t Atom__NET_CLIENT_LIST = [connection]{
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
52 static constexpr std::string_view name = "_NET_CLIENT_LIST";
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
53 xcb_intern_atom_cookie_t cookie = ::xcb_intern_atom(connection, true, name.size(), name.data());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
54 xcb_intern_atom_reply_t* reply = ::xcb_intern_atom_reply(connection, cookie, NULL);
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
55
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
56 xcb_atom_t atom = reply->atom;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
57 free(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
58 return atom;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
59 }();
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
60 if (Atom__NET_CLIENT_LIST == XCB_ATOM_NONE)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
61 return false; // BTFO
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
62
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
63 bool success = false;
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
64
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
65 std::vector<xcb_get_property_cookie_t> cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
66 cookies.reserve(roots.size());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
67
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
68 for (const auto& root : roots)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
69 cookies.push_back(::xcb_get_property(connection, 0, root, Atom__NET_CLIENT_LIST, XCB_ATOM_ANY, 0L, UINT_MAX));
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
70
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
71 for (const auto& cookie : cookies) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
72 xcb_get_property_reply_t* reply = ::xcb_get_property_reply(connection, cookie, NULL);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
73 if (reply) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
74 xcb_window_t* value = reinterpret_cast<xcb_window_t*>(::xcb_get_property_value(reply));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
75 int len = ::xcb_get_property_value_length(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
76
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
77 for (size_t i = 0; i < len; i++)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
78 result.insert(value[i]);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
79 success = true;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
80 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
81 free(reply);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
82 }
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
83
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
84 return success;
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
85 }
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
86
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
87 /* I have no idea why this works. */
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
88 static bool WalkWindows(xcb_connection_t* connection, const std::vector<xcb_window_t>& roots, std::set<xcb_window_t>& result) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
89 /* move this somewhere */
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
90 xcb_atom_t Atom_WM_STATE = [connection]{
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
91 static constexpr std::string_view name = "WM_STATE";
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
92 xcb_intern_atom_cookie_t cookie = ::xcb_intern_atom(connection, true, name.size(), name.data());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
93 xcb_intern_atom_reply_t* reply = ::xcb_intern_atom_reply(connection, cookie, NULL);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
94
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
95 xcb_atom_t atom = reply->atom;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
96 free(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
97 return atom;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
98 }();
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
99 if (Atom_WM_STATE == XCB_ATOM_NONE)
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
100 return false;
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
101
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
102 std::vector<xcb_query_tree_cookie_t> cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
103 cookies.reserve(roots.size());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
104
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
105 for (const auto& root : roots)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
106 cookies.push_back(::xcb_query_tree(connection, root));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
107
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
108 for (const auto& cookie : cookies) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
109 xcb_query_tree_reply_t* reply = ::xcb_query_tree_reply(connection, cookie, NULL);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
110
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
111 std::vector<xcb_window_t> windows = [reply]{
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
112 xcb_window_t* windows = ::xcb_query_tree_children(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
113 int len = ::xcb_query_tree_children_length(reply);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
114
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
115 std::vector<xcb_window_t> w;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
116 w.reserve(len);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
117
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
118 for (int i = 0; i < len; i++)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
119 w.push_back(windows[i]);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
120
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
121 return w;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
122 }();
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
123
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
124 std::vector<xcb_get_property_cookie_t> state_property_cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
125 state_property_cookies.reserve(windows.size());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
126
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
127 for (int i = 0; i < windows.size(); i++)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
128 state_property_cookies.push_back(::xcb_get_property(connection, 0, windows[i], Atom_WM_STATE, Atom_WM_STATE, 0, 0));
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
129
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
130 for (size_t i = 0; i < state_property_cookies.size(); i++) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
131 xcb_generic_error_t* err = NULL;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
132 xcb_get_property_reply_t* reply = ::xcb_get_property_reply(connection, state_property_cookies.at(i), &err);
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
133
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
134 if (reply->format || reply->type || reply->length) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
135 result.insert(windows[i]);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
136 free(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
137 continue;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
138 }
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
139
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
140 free(reply);
213
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
141 }
58e81b42a0d6 dep/animia: win/x11: find toplevels WAY faster
Paper <mrpapersonic@gmail.com>
parents: 199
diff changeset
142
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
143 if (WalkWindows(connection, windows, result))
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
144 continue;
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
145 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
146
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
147 return false;
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
148 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
149
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 191
diff changeset
150 bool EnumerateWindows(window_proc_t window_proc) {
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 157
diff changeset
151 if (!window_proc)
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 157
diff changeset
152 return false;
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
153
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
154 xcb_connection_t* connection = ::xcb_connect(NULL, NULL);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
155 if (!connection)
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
156 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
157
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
158 std::vector<xcb_screen_t> screens;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
159
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
160 {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
161 xcb_screen_iterator_t iter = ::xcb_setup_roots_iterator(xcb_get_setup(connection));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
162 for (; iter.rem; ::xcb_screen_next(&iter))
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
163 screens.push_back(*iter.data);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
164 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
165
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
166 std::vector<xcb_window_t> roots;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
167 roots.reserve(screens.size());
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
168
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
169 for (const auto& screen : screens)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
170 roots.push_back(screen.root);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
171
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
172 std::set<xcb_window_t> windows;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
173 //if (!GetAllTopLevelWindowsEWMH(connection, roots, windows))
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
174 WalkWindows(connection, roots, windows);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
175
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
176 std::vector<xcb_get_property_cookie_t> class_property_cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
177 std::vector<xcb_get_property_cookie_t> name_property_cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
178 std::vector<xcb_get_property_cookie_t> pid_property_cookies;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
179 class_property_cookies.reserve(windows.size());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
180 name_property_cookies.reserve(windows.size());
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
181 pid_property_cookies.reserve(windows.size());
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
182
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 158
diff changeset
183 for (const auto& window : windows) {
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
184 class_property_cookies.push_back(::xcb_get_property(connection, 0, window, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 0L, 2048L));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
185 name_property_cookies.push_back(::xcb_get_property(connection, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0L, UINT_MAX));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
186 pid_property_cookies.push_back(::xcb_get_property(connection, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_CARDINAL, 0L, 1L));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
187 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
188
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
189 size_t i = 0;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
190 for (const auto& window : windows) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
191 Window win = {0};
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 157
diff changeset
192 win.id = window;
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
193 {
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
194 xcb_get_property_reply_t* reply = ::xcb_get_property_reply(connection, class_property_cookies.at(i), NULL);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
195 if (reply && reply->format == 8) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
196 const char* data = reinterpret_cast<char*>(::xcb_get_property_value(reply));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
197 const int data_len = ::xcb_get_property_value_length(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
198
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
199 int instance_len = str_nlen(data, data_len);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
200 const char* class_name = data + instance_len + 1;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
201
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
202 win.class_name = std::string(class_name, str_nlen(class_name, data_len - (instance_len + 1)));
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
203 }
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
204 free(reply);
157
18c8eb5d1edc x11: make it work
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
205 }
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
206 {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
207 xcb_get_property_reply_t* reply = ::xcb_get_property_reply(connection, name_property_cookies.at(i), NULL);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
208 if (reply) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
209 const char* data = reinterpret_cast<char*>(::xcb_get_property_value(reply));
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
210 int len = ::xcb_get_property_value_length(reply);
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
211
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
212 win.text = std::string((char*)data, len);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
213 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
214 free(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
215 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
216 std::cout << win.class_name << ": " << win.text << std::endl;
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
217 Process proc = {0};
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
218 {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
219 xcb_get_property_reply_t* reply = ::xcb_get_property_reply(connection, pid_property_cookies.at(i), NULL);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
220 if (reply)
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
221 proc.pid = *reinterpret_cast<uint32_t*>(::xcb_get_property_value(reply));
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
222
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
223 free(reply);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
224 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
225 if (!window_proc(proc, win)) {
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
226 ::xcb_disconnect(connection);
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
227 return false;
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
228 }
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
229 i++;
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
230 }
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
231
234
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
232 ::xcb_disconnect(connection);
8ccf0302afb1 dep/animia: convert xlib code to xcb
Paper <mrpapersonic@gmail.com>
parents: 214
diff changeset
233
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
234 return true;
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
235 }
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
236
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
237 }