try to get term working
[xlua.git] / cwm.lua
blob1121e1ad33ce5666e193495926a292cd7b652b25
1 require "xconst"
3 -- windows table
4 cwm={
5 windows={}
6 winkeys={}
9 function cwm.init()
10 -- open root window
11 cwm.root=x.init()
13 -- intern useful atoms
14 for _,v in pairs { "_NET_SUPPORTED", "_NET_WM_NAME", "WM_STATE" } do if not atom[v] then atom[v] = x.intern_atom(v) end end
16 -- utility window calls
17 local rt = getmetatable(cwm.root)
18 rt.getwmstate = function(win)
19 local res = win:get_property(atom.WM_STATE)
20 if res then return res[0] end
21 end
22 rt.setwmstate = function(win,data)
23 assert(data)
24 win:set_property(atom.WM_STATE,nil, data, 0)
25 end
26 end
28 -------------------------------------------------------
29 -- steal windows
30 -------------------------------------------------------
31 function dt(t)
32 for k,v in pairs(t) do
33 print(k,v)
34 end
35 end
37 function cwm.add_window(win,attr)
38 -- shouldnt happen
39 assert(attr)
40 if (cwm.windows[win]) then return end
42 cwm.windows[win] = attr
43 cwm.windows[win].old_bw = attr.border_width
44 -- win:configure{border_width=5}
45 win:set_attributes{border_pixel=x.color('#ff0000')}
46 end
48 function cwm.setup()
49 cwm.root:set_attributes {
50 override_redirect=true,
51 background_pixmap=x.ParentRelative,
52 event_mask=
53 x.SubstructureRedirectMask|
54 x.SubstructureNotifyMask|
55 x.ButtonPressMask|
56 x.EnterWindowMask|
57 x.LeaveWindowMask|
58 x.StructureNotifyMask
60 end
62 function cwm.scan()
63 local wins = x.query_tree()
64 for _,win in ipairs(wins) do
65 local a=win:get_attributes()
67 -- ignore overriden windows
68 if a.override_redirect then
69 continue
70 end
71 if a.map_state == x.IsViewable or win:getwmstate() == atom.IconicState then
72 cwm.add_window(win,a)
73 end
74 end
75 end
77 function cwm.grabkeys(win,map)
78 for k,func in pairs(map) do
79 local mt={}
80 local modmask=0
81 for e in string.gmatch(k, "[^+-]*") do
82 if e == "" then continue end
83 table.insert(mt,e)
84 end
85 for i=1,#mt-1 do
86 modmask = $ | x[mt[i].."Mask"]
87 end
88 local key = x.keysym2keycode(x.string2keysym(mt[#mt]))
90 -- grab the keycode
91 win:grab_key(key, modmask)
93 -- and register handler
94 cwm.winkeys[win] = $ or {}
95 cwm.winkeys[win][modmask] = $ or {}
96 cwm.winkeys[win][modmask][key] = func
97 end
98 end
100 cwm.init()
101 cwm.grabkeys(cwm.root,keymap)
103 cwm.setup()
104 cwm.scan()
106 -- prepare input event mask
107 while true do
108 print("waiting for next event")
109 local ev=x.get_events()
110 if ev then
111 print("GOT EVENT", ev.type)
112 dt(ev)
113 else
114 print("nil event?")