cleanup and comments
[wmiirc-lua.git] / wmii.lua
blob990e73b7037636e47162955ec77fdcd546f02fea
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 local base = _G
11 local io = require("io")
12 local os = require("os")
13 local posix = require("posix")
14 local string = require("string")
15 local print = print
16 local pairs = pairs
18 module("wmii")
20 local wmiir = "wmiir"
22 -- ------------------------------------------------------------------------
23 -- TODO I would like to be able to return an interator
24 function ls (dir, fmt)
25 local tmpfile = os.tmpname()
26 local fmt = fmt or ""
28 os.execute (wmiir .. " ls " .. fmt .. " " .. dir .. " > " .. tmpfile)
30 local fh = io.open (tmpfile, "rb")
31 os.remove (tmpfile)
33 local data = fh:read("*a") -- read everything
35 io.close (fh)
37 return data
38 end
40 -- ------------------------------------------------------------------------
41 -- read all contents of a wmii virtual file
42 function read (file)
43 local tmpfile = os.tmpname()
45 os.execute (wmiir .. " read " .. file .. " > " .. tmpfile)
47 local fh = io.open (tmpfile, "rb")
48 os.remove (tmpfile)
50 local data = fh:read("*a") -- read all
51 io.close (fh)
53 return data
54 end
56 -- ------------------------------------------------------------------------
57 -- return an iterator which walks all the lines in the file
59 -- example:
60 -- for event in wmii.iread("/event")
61 -- ...
62 -- end
63 function iread (file)
64 local tmpfile = os.tmpname()
65 os.remove (tmpfile)
67 io.write ("-- tmpname " .. tmpfile .. "\n")
69 local rc = posix.mkfifo (tmpfile)
70 io.write ("-- mkfifo " .. rc .. "\n")
72 rc = posix.fork ()
73 if rc < 0 then
74 io.write ("-- fork failed " .. rc .. "\n")
75 return function ()
76 return nil
77 end
78 end
79 if rc == 0 then -- child
80 os.execute (wmiir .. " read " .. file .. " > " .. tmpfile)
81 posix.exec ("/usr/bin/env", "cat", "/dev/null")
82 end
84 -- parent
86 local fh = io.open (tmpfile, "rb")
87 os.remove (tmpfile)
89 return function ()
90 local line = fh:read("*l") -- read a line
91 if not line then
92 io.write ("-- closing " .. file .. "\n")
93 io.close (fh)
94 end
95 return line
96 end
97 end
99 -- ------------------------------------------------------------------------
100 -- returns an events iterator
101 function ievents ()
102 local it = iread("/event")
104 return function ()
105 local line = it()
106 return string.match(line, "(%S+)%s(.+)")
110 -- ------------------------------------------------------------------------
111 -- write a value to a wmii virtual file system
112 function write (file, value)
113 local tmpfile = os.tmpname()
115 local fh = io.open (tmpfile, "wb")
116 fh:write(value)
117 io.close (fh)
119 os.execute (wmiir .. " write " .. file .. " < " .. tmpfile)
120 os.remove (tmpfile)
123 -- ------------------------------------------------------------------------
124 -- write a value to a wmii virtual file system
125 function configure (config)
126 local x, y
127 for x, y in pairs(config) do
128 write ("/ctl", x .. " " .. y)