New example: Dump colormap
[luaxcb.git] / examples / DumpTreeSlow.lua
blob4ba0aa2e2ded0534153c0b7a39c8b4d7b94b8faa
1 require("lxcb")
3 local INDENT_PER_LEVEL=2
4 local no_name = ""
6 local WM_NAME
8 function GetWMName(id)
9 if (WM_NAME == nil) then
10 local name = 'WM_NAME'
11 WM_NAME = dpy:intern_atom_reply(dpy:intern_atom(false, string.len(name), name)).atom
12 end
13 local prop, propreq
14 propreq = dpy:get_property(false, id, WM_NAME, dpy.GET_PROPERTY_TYPE.ANY, 0, 80)
15 prop = dpy:get_property_reply(propreq)
16 if (prop) then
17 return string.char(unpack(prop.value))
18 else
19 return nil
20 end
21 end
23 function printf(...)
24 io.output():write(string.format(...))
25 end
27 function DumpTree(id, indent)
28 local root, parent, children
30 -- Show the information for this window
31 local mapped = true
32 local name
34 local xwa = dpy:get_window_attributes_reply(dpy:get_window_attributes(id))
35 if (xwa) then
36 mapped = (xwa.map_state == dpy.MAP_STATE.VIEWABLE)
37 end
39 local tp = GetWMName(id)
40 name = tp or ""
42 if (mapped) then
43 printf("%s0x%08X ", string.rep(" ", indent), id);
44 else
45 printf("%s(0x%08X)", string.rep(" ", indent), id);
46 end
48 local geom = dpy:get_geometry_reply(dpy:get_geometry(id))
49 printf(" %d: %d,%d [%dx%d]", geom.depth, geom.x, geom.y, geom.width, geom.height)
51 printf(" %s\n", name);
53 -- Recursively show the information for the rest of the tree
54 local tree = dpy:query_tree_reply(dpy:query_tree(id))
55 for k, v in ipairs(tree.children) do
56 DumpTree(v, indent + INDENT_PER_LEVEL)
57 end
58 end
61 local root
62 dpy, screen = lxcb.connect("")
64 if (not dpy) then
65 print("Error: Cannot open default display.")
66 return 1;
67 end
69 if (#arg >= 1) then
70 root = tonumber(arg[1])
71 if (not root) then
72 print("Usage: DumpTree [starting window]")
73 return 1;
74 end
75 else
76 root = dpy:get_setup().roots[screen+1].root
77 end
79 DumpTree(root, 0)
81 dpy = nil
83 return 0