primitive action handling
[wmiirc-lua.git] / wmii.lua
blob2e7972dd49a871d77c04f01512df38fac9cca036
1 --
2 -- Copyrigh (c) 2007, Bart Trojanowski <bart@jukie.net>
3 --
4 -- Simple wmiir like interface.
5 --
6 -- The current intent is to wrap around the wmiir executable.
7 -- This is just a proof of concept, and eventually this will
8 -- be rewritten in C to use libixp.
9 --
10 -- git://www.jukie.net/wmiirc-lua.git/
12 local base = _G
13 local io = require("io")
14 local os = require("os")
15 local posix = require("posix")
16 local string = require("string")
17 local print = print
18 local pairs = pairs
20 module("wmii")
22 local wmiir = "wmiir"
24 -- ------------------------------------------------------------------------
25 -- TODO I would like to be able to return an interator
26 function ls (dir, fmt)
27 local tmpfile = os.tmpname()
28 local fmt = fmt or ""
30 os.execute (wmiir .. " ls " .. fmt .. " " .. dir .. " > " .. tmpfile)
32 local fh = io.open (tmpfile, "rb")
33 os.remove (tmpfile)
35 local data = fh:read("*a") -- read everything
37 io.close (fh)
39 return data
40 end
42 -- ------------------------------------------------------------------------
43 -- read all contents of a wmii virtual file
44 function read (file)
45 local tmpfile = os.tmpname()
47 os.execute (wmiir .. " read " .. file .. " > " .. tmpfile)
49 local fh = io.open (tmpfile, "rb")
50 os.remove (tmpfile)
52 local data = fh:read("*a") -- read all
53 io.close (fh)
55 return data
56 end
58 -- ------------------------------------------------------------------------
59 -- return an iterator which walks all the lines in the file
61 -- example:
62 -- for event in wmii.iread("/event")
63 -- ...
64 -- end
65 function iread (file)
66 local tmpfile = os.tmpname()
67 os.remove (tmpfile)
69 io.write ("-- tmpname " .. tmpfile .. "\n")
71 local rc = posix.mkfifo (tmpfile)
72 io.write ("-- mkfifo " .. rc .. "\n")
74 rc = posix.fork ()
75 if rc < 0 then
76 io.write ("-- fork failed " .. rc .. "\n")
77 return function ()
78 return nil
79 end
80 end
81 if rc == 0 then -- child
82 os.execute (wmiir .. " read " .. file .. " > " .. tmpfile)
83 posix.exec ("/usr/bin/env", "cat", "/dev/null")
84 end
86 -- parent
88 local fh = io.open (tmpfile, "rb")
89 os.remove (tmpfile)
91 return function ()
92 local line = fh:read("*l") -- read a line
93 if not line then
94 io.write ("-- closing " .. file .. "\n")
95 io.close (fh)
96 end
97 return line
98 end
99 end
101 -- ------------------------------------------------------------------------
102 -- returns an events iterator
103 function ievents ()
104 local it = iread("/event")
106 return function ()
107 local line = it()
108 return string.match(line, "(%S+)%s(.+)")
112 -- ------------------------------------------------------------------------
113 -- write a value to a wmii virtual file system
114 function write (file, value)
115 local tmpfile = os.tmpname()
117 local fh = io.open (tmpfile, "wb")
118 fh:write(value)
119 io.close (fh)
121 os.execute (wmiir .. " write " .. file .. " < " .. tmpfile)
122 os.remove (tmpfile)
125 -- ------------------------------------------------------------------------
126 -- write a value to a wmii virtual file system
127 function configure (config)
128 local x, y
129 for x, y in pairs(config) do
130 write ("/ctl", x .. " " .. y)
134 -- ------------------------------------------------------------------------
135 -- displays the menu given an table of entires, returns selected text
136 function menu (tbl)
138 local infile = os.tmpname()
139 local fh = io.open (infile, "w+")
141 for n in pairs(tbl) do
142 fh:write (n)
143 fh:write ("\n")
145 fh:close()
149 local outfile = os.tmpname()
151 os.execute ("dmenu < " .. infile .. " > " .. outfile)
153 fh = io.open (outfile, "r")
154 os.remove (outfile)
156 local sel = fh:read("*l")
157 fh:close()
159 return sel
162 -- ------------------------------------------------------------------------
163 -- displays the a tag selection menu, returns selected tag
164 function tagmenu ()
165 local tmpfile = os.tmpname()
167 os.execute ("wmiir ls /tag | sed 's|/||; /^sel$/d' | dmenu > " .. tmpfile)
169 local fh = io.open (tmpfile, "rb")
170 os.remove (tmpfile)
172 local tag = fh:read("*l")
173 io.close (fh)
175 return tag
178 -- ------------------------------------------------------------------------
179 -- displays the a program menu, returns selected program
180 function progmenu ()
181 local tmpfile = os.tmpname()
183 os.execute ("dmenu_path | dmenu > " .. tmpfile)
185 local fh = io.open (tmpfile, "rb")
186 os.remove (tmpfile)
188 local prog = fh:read("*l")
189 io.close (fh)
191 return prog