3 local p = require("posix")
9 {"port", "required", 'p'},
13 {'type:find("toolbar") or type:find("utility")', 'focus=off'},
14 {'type:find("dialog")', 'floating=on'},
15 {'type:find("dock") or type:find("desktop") or type:find("notification")', 'manage=off'},
16 {'type:find("desktop")', 'lower=on'},
17 {'state:find("fullscreen")', 'fullscreen=on'},
18 {'state:find("sticky")', 'sticky=on'}
21 for opt, optarg, optind, longind in p.getopt(arg, short, long) do
23 print("Unrecognized option")
25 elseif opt == 'h' then
26 print("Usage: ruld [-h|-p PORT]")
28 elseif opt == 'p' then
33 function eval(exp, env)
35 local value = "return " .. exp
37 f = load(value, nil, nil, env)
46 for index = #rules, 1, -1 do
47 local entry = rules[index]
48 if eval(entry[1], env) then
51 delay = entry[3].delay
52 duration = entry[3].duration
53 if delay and delay > 0 then
54 entry[3].delay = delay - 1
56 if ((not delay) or delay == 0) and duration and duration > 0 then
57 entry[3].duration = duration - 1
60 if (((not delay) or delay == 0) and ((not duration) or duration > 0)) then
62 rsp = rsp .. " " .. entry[2]
67 if duration and duration <= 1 then
68 table.remove(rules, index)
75 local fd = p.socket(p.AF_INET, p.SOCK_STREAM, 0)
76 p.bind(fd, {family=p.AF_INET, addr="127.0.0.1", port=port})
77 p.listen(fd, p.SOMAXCONN)
83 local msg = p.recv(ret_fd, p.BUFSIZ)
88 cmd = msg:sub(1, sep - 1)
89 data = msg:sub(sep + 1)
94 local env = eval(data)
99 elseif cmd == "add" then
100 local value = eval(data)
102 table.insert(rules, value)
104 elseif cmd == "remove" then
105 if data == "tail" then
106 table.remove(rules, #rules)
107 elseif data == "head" then
108 table.remove(rules, 1)
110 for index = #rules, 1, -1 do
111 if rules[index][1]:find(data) then
112 table.remove(rules, index)
116 elseif cmd == "quit" then
118 elseif cmd == "list" then
120 for index, entry in pairs(rules) do
121 rsp = rsp .. string.format("%s => %s", entry[1], entry[2])
123 if entry[3].delay then
124 rsp = rsp .. string.format(" @%i", entry[3].delay)
126 if entry[3].duration then
127 rsp = rsp .. string.format(" +%i", entry[3].duration)
130 if index < #rules then