New example: Dump colormap
[luaxcb.git] / examples / DumpTree.lua
blob3c8877b2b9ea43b87ec431d913a0c72fc537d84a
1 require("lxcb")
3 local INDENT_PER_LEVEL=2
4 local no_name = ""
6 local atom = {}
8 function internatom(this, name)
9 this[name] = dpy:intern_atom_reply(dpy:intern_atom(false, string.len(name), name)).atom
10 return this[name]
11 end
13 setmetatable(atom, { __index = internatom })
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 mapped
23 local attrcookie = dpy:get_window_attributes(id)
24 local geomcookie = dpy:get_geometry(id)
25 local namecookie = dpy:get_property(false, id, atom.WM_NAME, dpy.GET_PROPERTY_TYPE.ANY, 0, 80)
26 local treecookie = dpy:query_tree(id)
28 local xwa = dpy:get_window_attributes_reply(attrcookie)
29 if (xwa) then
30 mapped = (xwa.map_state == dpy.MAP_STATE.VIEWABLE)
31 end
33 if (mapped) then
34 printf("%s0x%08X ", string.rep(" ", indent), id);
35 else
36 printf("%s(0x%08X)", string.rep(" ", indent), id);
37 end
39 local geom = dpy:get_geometry_reply(geomcookie)
40 printf(" %d: %d,%d [%dx%d]", geom.depth, geom.x, geom.y, geom.width, geom.height)
42 local name = ''
43 local prop = dpy:get_property_reply(namecookie)
44 if (prop) then
45 name = prop.value
46 end
48 printf(" %s\n", name);
50 -- Recursively show the information for the rest of the tree
51 local tree = dpy:query_tree_reply(treecookie)
52 for k, v in ipairs(tree.children) do
53 DumpTree(v, indent + INDENT_PER_LEVEL)
54 end
55 end
58 local root
59 dpy, screen = lxcb.connect("")
61 if (not dpy) then
62 print("Error: Cannot open default display.")
63 return 1;
64 end
66 if (#arg >= 1) then
67 root = tonumber(arg[1])
68 if (not root) then
69 print("Usage: DumpTree [starting window]")
70 return 1;
71 end
72 else
73 root = dpy:get_setup().roots[screen+1].root
74 end
76 DumpTree(root, 0)
78 dpy = nil
80 return 0