2 -- Copyrigh (c) 2007, Bart Trojanowski <bart@jukie.net>
4 -- Simple wmiir like interface.
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.
11 local io
= require("io")
12 local os
= require("os")
13 local posix
= require("posix")
14 local string = require("string")
22 -- ------------------------------------------------------------------------
23 -- TODO I would like to be able to return an interator
24 function ls (dir
, fmt
)
25 local tmpfile
= os
.tmpname()
28 os
.execute (wmiir
.. " ls " .. fmt
.. " " .. dir
.. " > " .. tmpfile
)
30 local fh
= io
.open (tmpfile
, "rb")
33 local data
= fh
:read("*a") -- read everything
40 -- ------------------------------------------------------------------------
41 -- read all contents of a wmii virtual file
43 local tmpfile
= os
.tmpname()
45 os
.execute (wmiir
.. " read " .. file
.. " > " .. tmpfile
)
47 local fh
= io
.open (tmpfile
, "rb")
50 local data
= fh
:read("*a") -- read all
56 -- ------------------------------------------------------------------------
57 -- return an iterator which walks all the lines in the file
60 -- for event in wmii.iread("/event")
64 local tmpfile
= os
.tmpname()
67 io
.write ("-- tmpname " .. tmpfile
.. "\n")
69 local rc
= posix
.mkfifo (tmpfile
)
70 io
.write ("-- mkfifo " .. rc
.. "\n")
74 io
.write ("-- fork failed " .. rc
.. "\n")
79 if rc
== 0 then -- child
80 os
.execute (wmiir
.. " read " .. file
.. " > " .. tmpfile
)
81 posix
.exec ("/usr/bin/env", "cat", "/dev/null")
86 local fh
= io
.open (tmpfile
, "rb")
90 local line
= fh
:read("*l") -- read a line
92 io
.write ("-- closing " .. file
.. "\n")
99 -- ------------------------------------------------------------------------
100 -- returns an events iterator
102 local it
= iread("/event")
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")
119 os
.execute (wmiir
.. " write " .. file
.. " < " .. tmpfile
)
123 -- ------------------------------------------------------------------------
124 -- write a value to a wmii virtual file system
125 function configure (config
)
127 for x
, y
in pairs(config
) do
128 write ("/ctl", x
.. " " .. y
)