New example: Dump colormap
[luaxcb.git] / examples / FullDumpTree.lua
blobdd211d2ee7c3d19127e4d571bef8ecad898a5dd0
1 require("lxcb")
3 local INDENT_PER_LEVEL=2
4 local no_name = ""
6 local queue = {};
7 local window = {};
8 local atom = {};
10 function internatom(this, name)
11 this[name] = dpy:intern_atom_reply(dpy:intern_atom(false, string.len(name), name)).atom
12 return this[name]
13 end
15 function printf(...)
16 io.output():write(string.format(...))
17 end
19 function DumpTree(id, indent)
20 -- Show the information for this window
21 local win = window[id]
23 if (win.mapped) then
24 printf("%s0x%08X ", string.rep(" ", indent), id);
25 else
26 printf("%s(0x%08X)", string.rep(" ", indent), id);
27 end
29 local geom = win.geom
30 printf(" %d: %d,%d [%dx%d]", geom.depth, geom.x, geom.y, geom.width, geom.height)
32 printf(" %s\n", win.name or "");
34 -- Recursively show the information for the rest of the tree
35 for k, v in ipairs(win.children) do
36 DumpTree(v, indent + INDENT_PER_LEVEL)
37 end
38 end
40 -- Queueing functions
41 function QueueInternAtom(name)
42 local cookie = dpy:intern_atom(false, string.len(name), name)
43 table.insert(queue, function()
44 atom[name] = dpy:intern_atom_reply(cookie).atom
45 end)
46 end
48 function QueueWindowName(id)
49 local cookie = dpy:get_property(false, id, atom.WM_NAME, dpy.GET_PROPERTY_TYPE.ANY, 0, 80)
50 table.insert(queue, function()
51 local prop = dpy:get_property_reply(cookie)
52 if (prop) then
53 window[id].name = string.char(unpack(prop.value))
54 end
55 end)
56 end
58 function QueueWindowGeometry(id)
59 local cookie = dpy:get_geometry(id)
60 table.insert(queue, function()
61 window[id].geom = dpy:get_geometry_reply(cookie)
62 end)
63 end
65 function QueueWindowMapped(id)
66 local cookie = dpy:get_window_attributes(id)
67 table.insert(queue, function()
68 local xwa = dpy:get_window_attributes_reply(cookie)
69 window[id].mapped = (xwa.map_state == dpy.MAP_STATE.VIEWABLE)
70 end)
71 end
73 function QueueWindowChildren(id)
74 local cookie = dpy:query_tree(id)
75 table.insert(queue, function()
76 local tree = dpy:query_tree_reply(cookie)
77 window[id].children = tree.children
79 -- For each window, query the children first
80 -- (since that's the one that'll cause recursive
81 -- queries), and then query incidental per-window data
82 -- (hopefully overlapping with the child queries)
84 for k, v in ipairs(tree.children) do
85 window[v] = {}
86 QueueWindowChildren(v)
87 end
89 for k, v in ipairs(tree.children) do
90 QueueWindowName(v)
91 QueueWindowGeometry(v)
92 QueueWindowMapped(v)
93 end
94 end)
95 end
97 function ProcessQueue()
98 local next = 1
99 while (#queue >= next) do
100 queue[next]()
101 next = next + 1
105 -- Atom cache setup
106 setmetatable(atom, { __index = internatom })
108 -- Main
109 local root
110 dpy, screen = lxcb.connect("")
112 if (not dpy) then
113 print("Error: Cannot open default display.")
114 return 1;
117 if (#arg >= 1) then
118 root = tonumber(arg[1])
119 if (not root) then
120 print("Usage: DumpTree [starting window]")
121 return 1;
123 else
124 root = dpy:get_setup().roots[screen+1].root
127 window[root] = { mapped = true }
128 QueueWindowChildren(root)
129 QueueInternAtom('WM_NAME')
130 QueueWindowGeometry(root)
131 ProcessQueue()
133 DumpTree(root, 0)
135 dpy = nil
137 return 0