New example: Dump colormap
[luaxcb.git] / examples / dumpcmap.lua
blobf8ff260925f4093c4b98574c4de926122f884049
1 require("lxcb")
3 do
4 options = {}
5 local optlist = {
6 { shortname = 'h', longname = 'help', desc = 'Show this help menu' },
7 { longname = 'display', desc = 'Set the display to connect to', arg = true },
9 local shorts = {}
10 local longs = {}
12 table.sort(optlist, function(a, b) return a.longname < b.longname end)
14 local function usage()
15 print("\nUsage: dumpcmap <colormap id>\n")
16 for _, o in ipairs(optlist) do
17 local str
18 if o.shortname then
19 str = '-' .. o.shortname
20 else
21 str = ' '
22 end
23 print(string.format(' %s --%-15s %s', str, o.longname, o.desc))
24 end
25 print('')
26 os.exit(1)
27 end
29 for _, i in ipairs(optlist) do
30 if i.shortname then
31 shorts[i.shortname] = i.longname
32 end
33 if i.longname then
34 longs[i.longname] = i
35 end
36 end
38 local nextname
40 for _, i in ipairs(arg) do
41 if nextname then
42 options[nextname] = i
43 nextname = nil
44 else
45 if string.sub(i, 1, 1) == '-' then
46 if string.sub(i, 2, 2) == '-' then
47 local opt = string.sub(i,3)
48 local val = longs[opt]
49 if val then
50 if val.arg then
51 nextname = opt
52 else
53 options[opt] = true
54 end
55 else
56 print("Unknown long option "..i)
57 usage()
58 end
59 else
60 for o in string.gmatch(string.sub(i, 2), '.') do
61 if shorts[o] then
62 options[shorts[o]] = true
63 else
64 print("Unknown option -"..o)
65 usage()
66 end
67 end
68 end
69 else
70 if options.colormap then
71 usage()
72 else
73 options.colormap = i
74 end
75 end
76 end
77 end
79 if not options.colormap then
80 usage()
81 end
83 if nextname then
84 usage()
85 end
87 if options.help then
88 usage()
89 end
90 end
92 local x = lxcb.connect(options.display)
93 if (not x) or x:has_error() then
94 print("Error: cannot open display")
95 os.exit(1)
96 end
98 local query = {}
99 for i = 1, 0x100 do
100 query[i] = i - 1
102 local cookie = x:query_colors(options.colormap, query)
104 query = x:query_colors_reply(cookie)
105 if not query then
106 print("Invalid colormap ID")
107 os.exit(1)
110 for i = 1, query.colors_len do
111 print(string.format("RGB[%03d] = %04X %04X %04X",
113 query.colors[i].red,
114 query.colors[i].green,
115 query.colors[i].blue))