2 -- Copyright (c) 2007, Bart Trojanowski <bart@jukie.net>
4 -- WMII event loop, in lua
6 -- http://www.jukie.net/~bart/blog/tag/wmiirc-lua
7 -- git://www.jukie.net/wmiirc-lua.git/
10 -- ========================================================================
12 -- ========================================================================
18 wmii.lua - WMII event-loop methods in lua
24 -- Write something to the wmii filesystem, in this case a key event.
25 wmii.write ("/event", "Key Mod1-j")
27 -- Set your wmii /ctl parameters
32 -- Configure wmii.lua parameters
34 xterm = 'x-terminal-emulator'
37 -- Now start the event loop
42 wmii.lua provides methods for replacing the stock sh-based wmiirc shipped with
43 wmii 3.6 and newer with a lua-based event loop.
45 It should be used by your wmiirc
54 -- ========================================================================
56 -- ========================================================================
58 local wmiidir
= ("%HOME_WMII%"):gsub("^~", os
.getenv("HOME"))
59 local wmiirc
= wmiidir
.. "/wmiirc"
61 package
.path
= wmiidir
.. "/core/?.lua;" ..
62 wmiidir
.. "/plugins/?.lua;" ..
64 package
.cpath
= wmiidir
.. "/core/?.so;" ..
65 wmiidir
.. "/plugins/?.so;" ..
68 local ixp
= require
"ixp"
69 local eventloop
= require
"eventloop"
70 local history
= require
"history"
72 local io
= require("io")
73 local os
= require("os")
74 local string = require("string")
75 local table = require("table")
76 local math
= require("math")
82 local package
= package
83 local require
= require
84 local tostring = tostring
85 local tonumber = tonumber
86 local setmetatable
= setmetatable
88 -- kinda silly, but there is no working liblua5.1-posix0 in ubuntu
89 -- so we make it optional
90 local have_posix
, posix
= pcall(require
,"posix")
97 -- but having posix is not enough as the API changes, so we try each one
98 if posix
.getprocessid
then
99 local stat
,rc
= pcall (posix
.getprocessid
, "pid")
104 if not myid
and posix
.getpid
then
105 local stat
,rc
= pcall (posix
.getpid
, "pid")
112 -- we were not able to get the PID, but we can create a random number
113 local now
= tonumber(os
.date("%s"))
115 myid
= math
.random(10000)
118 -- ========================================================================
120 -- ========================================================================
122 -- wmiir points to the wmiir executable
123 -- TODO: need to make sure that wmiir is in path, and if not find it
124 local wmiir
= "wmiir"
126 -- wmii_adr is the address we use when connecting using ixp
127 local wmii_adr
= os
.getenv("WMII_ADDRESS")
128 or ("unix!/tmp/ns." .. os
.getenv("USER") .. "."
129 .. os
.getenv("DISPLAY"):match("(:%d+)") .. "/wmii")
131 -- wmixp is the ixp context we use to talk to wmii
132 local wmixp
= ixp
.new(wmii_adr
)
134 -- history of previous views, view_hist[#view_hist] is the last one
135 local view_hist
= {} -- sorted with 1 being the oldest
136 local view_hist_max
= 50 -- max number to keep track of
138 -- allow for a client to be forced to a tag
139 local next_client_goes_to_tag
= nil
141 -- program and action histories
142 local prog_hist
= history
.new (20)
143 local action_hist
= history
.new(10)
145 -- where to find plugins
146 plugin_path
= wmiidir
.. "/plugins/?.so;"
147 .. wmiidir
.. "/plugins/?.lua;"
148 .. "/usr/local/lib/lua/5.1/wmii/?.so;"
149 .. "/usr/local/share/lua/5.1/wmii/?.lua;"
150 .. "/usr/lib/lua/5.1/wmii/?.so;"
151 .. "/usr/share/lua/5.1/wmii/?.lua"
153 -- where to find wmiirc (see find_wmiirc())
154 wmiirc_path
= wmiidir
.. "/wmiirc.lua;"
155 .. wmiidir
.. "/wmiirc;"
156 .. "%RC_DIR%/wmiirc.lua;"
159 -- ========================================================================
161 -- ========================================================================
168 Log the message provided in C<str>
170 Currently just writes to io.stderr
175 if get_conf("debug") then
176 io
.stderr
:write (str
.. "\n")
183 =item find_wmiirc ( )
185 Locates the wmiirc script. It looks in %HOME_WMII% and %RC_DIR%
186 for the first lua script bearing the name wmiirc.lua or wmiirc. Returns
191 function find_wmiirc()
193 for fn
in string.gmatch(wmiirc_path
, "[^;]+") do
194 -- try to locate the files locally
195 local file
= io
.open(fn
, "r")
197 local txt
= file
:read("*line")
199 if type(txt
) == 'string' and txt
:match("lua") then
208 -- ========================================================================
209 -- MAIN ACCESS FUNCTIONS
210 -- ========================================================================
215 =item ls ( dir, fmt )
217 List the wmii filesystem directory provided in C<dir>, in the format specified
220 Returns an iterator of TODO
224 function ls (dir
, fmt
)
225 local verbose
= fmt
and fmt
:match("l")
227 local s
= wmixp
:stat(dir
)
229 return function () return nil end
231 if s
.modestr
:match("^[^d]") then
233 return stat2str(verbose
, s
)
237 local itr
= wmixp
:idir (dir
)
248 return stat2str(verbose
, s
)
254 local function stat2str(verbose
, stat
)
256 return string.format("%s %s %s %5d %s %s", stat
.modestr
, stat
.uid
, stat
.gid
, stat
.length
, stat
.timestr
, stat
.name
)
258 if stat
.modestr
:match("^d") then
259 return stat
.name
.. "/"
266 -- ------------------------------------------------------------------------
267 -- read all contents of a wmii virtual file
269 return wmixp
:read (file
)
272 -- ------------------------------------------------------------------------
273 -- return an iterator which walks all the lines in the file
276 -- for event in wmii.iread("/ctl")
280 -- NOTE: don't use iread for files that could block, as this will interfere
281 -- with timer processing and event delivery. Instead fork off a process to
282 -- execute wmiir and read back the responses via callback.
283 function iread (file
)
284 return wmixp
:iread(file
)
287 -- ------------------------------------------------------------------------
288 -- create a wmii file, optionally write data to it
289 function create (file
, data
)
290 wmixp
:create(file
, data
)
293 -- ------------------------------------------------------------------------
294 -- remove a wmii file
295 function remove (file
)
299 -- ------------------------------------------------------------------------
300 -- write a value to a wmii virtual file system
301 function write (file
, value
)
302 wmixp
:write (file
, value
)
305 -- ------------------------------------------------------------------------
306 -- setup a table describing the menu command
307 local function menu_cmd (prompt
)
308 local cmdt
= { wmiir
, "setsid", "dmenu", "-b" }
309 local fn
= get_ctl("font")
311 cmdt
[#cmdt
+1] = "-fn"
314 local normcolors
= get_ctl("normcolors")
316 local nf
, nb
= normcolors
:match("(#%x+)%s+(#%x+)%s#%x+")
318 cmdt
[#cmdt
+1] = "-nf"
319 cmdt
[#cmdt
+1] = "'" .. nf
.. "'"
322 cmdt
[#cmdt
+1] = "-nb"
323 cmdt
[#cmdt
+1] = "'" .. nb
.. "'"
326 local focuscolors
= get_ctl("focuscolors")
328 local sf
, sb
= focuscolors
:match("(#%x+)%s+(#%x+)%s#%x+")
330 cmdt
[#cmdt
+1] = "-sf"
331 cmdt
[#cmdt
+1] = "'" .. sf
.. "'"
334 cmdt
[#cmdt
+1] = "-sb"
335 cmdt
[#cmdt
+1] = "'" .. sb
.. "'"
340 cmdt
[#cmdt
+1] = "'" .. prompt
.. "'"
346 -- ------------------------------------------------------------------------
347 -- displays the menu given an table of entires, returns selected text
348 function menu (tbl
, prompt
)
349 local menu
= menu_cmd(prompt
)
351 local infile
= os
.tmpname()
352 local fh
= io
.open (infile
, "w+")
355 for i
,v
in pairs(tbl
) do
356 if type(i
) == 'number' and type(v
) == 'string' then
365 local outfile
= os
.tmpname()
368 menu
[#menu
+1] = infile
370 menu
[#menu
+1] = outfile
372 local cmd
= table.concat(menu
," ")
375 fh
= io
.open (outfile
, "r")
378 local sel
= fh
:read("*l")
384 -- ------------------------------------------------------------------------
385 -- displays the a tag selection menu, returns selected tag
387 local tags
= get_tags()
389 return menu(tags
, "tag:")
392 -- ------------------------------------------------------------------------
393 -- displays the a program menu, returns selected program
394 function prog_menu ()
395 local menu
= menu_cmd("cmd:")
397 local outfile
= os
.tmpname()
400 menu
[#menu
+1] = outfile
403 for n
in prog_hist
:walk_reverse_unique() do
404 hstt
[#hstt
+1] = "echo '" .. n
.. "' ; "
407 local cmd
= "(" .. table.concat(hstt
)
409 .. table.concat(menu
," ")
412 local fh
= io
.open (outfile
, "rb")
415 local prog
= fh
:read("*l")
421 -- ------------------------------------------------------------------------
422 -- returns a table of sorted tags names
426 for s
in wmixp
:idir ("/tag") do
427 if s
.name
and not (s
.name
== "sel") then
435 -- ------------------------------------------------------------------------
436 -- returns a table of sorted screen names
437 function get_screens()
441 for s
in wmixp
:idir ("/screen") do
442 if s
.name
and not (s
.name
== "sel") then
454 -- ------------------------------------------------------------------------
455 -- returns current view, on current screen or specified screen
456 function get_view(screen
)
457 return get_screen_ctl(screen
, "view") or get_ctl("view")
460 -- ------------------------------------------------------------------------
461 -- changes the current view to the name given
462 function set_view(sel
)
463 local cur
= get_view()
464 local all
= get_tags()
466 if #all
< 2 or sel
== cur
then
467 -- nothing to do if we have less then 2 tags
471 if not (type(sel
) == "string") then
472 error ("string argument expected")
476 write ("/ctl", "view " .. sel
)
479 -- ------------------------------------------------------------------------
480 -- changes the current view to the index given
481 function set_view_index(sel
)
482 local cur
= get_view()
483 local all
= get_tags()
486 -- nothing to do if we have less then 2 tags
490 local num
= tonumber (sel
)
492 error ("number argument expected")
495 local name
= all
[sel
]
496 if not name
or name
== cur
then
501 write ("/ctl", "view " .. name
)
504 -- ------------------------------------------------------------------------
505 -- chnages to current view by offset given
506 function set_view_ofs(jump
)
507 local cur
= get_view()
508 local all
= get_tags()
511 -- nothing to do if we have less then 2 tags
516 if (jump
< - #all
) or (jump
> #all
) then
517 error ("view selector is out of range")
520 -- find the one that's selected index
523 for i
,v
in pairs (all
) do
524 if v
== cur
then curi
= i
end
528 local newi
= math
.fmod(#all
+ curi
+ jump
- 1, #all
) + 1
529 if (newi
< - #all
) or (newi
> #all
) then
530 error ("error computng new view")
533 write ("/ctl", "view " .. all
[newi
])
536 -- ------------------------------------------------------------------------
537 -- toggle between last view and current view
538 function toggle_view()
539 local last
= view_hist
[#view_hist
]
545 -- ========================================================================
547 -- ========================================================================
549 local action_handlers
= {
550 man
= function (act
, args
)
551 local xterm
= get_conf("xterm") or "xterm"
553 if (not page
) or (not page
:match("%S")) then
554 page
= wmiidir
.. "/wmii.3lua"
556 local cmd
= xterm
.. " -e man " .. page
.. " &"
557 log (" executing: " .. cmd
)
558 os
.execute (wmiir
.. " setsid " .. cmd
)
562 write ("/ctl", "quit")
565 exec
= function (act
, args
)
566 local what
= args
or "wmii"
567 log (" asking wmii to exec " .. tostring(what
))
569 write ("/ctl", "exec " .. what
)
572 xlock
= function (act
)
573 local cmd
= get_conf("xlock") or "xscreensaver-command --lock"
574 os
.execute (wmiir
.. " setsid " .. cmd
)
579 local wmiirc
= find_wmiirc()
581 log (" executing: lua " .. wmiirc
)
584 posix
.exec ("/bin/sh", "-c", "exec lua wmiirc")
585 posix
.exec ("%LUA_BIN%", wmiirc
)
586 posix
.exec ("/usr/bin/lua", wmiirc
)
589 log("sorry cannot restart; you don't have lua's posix library.")
594 wmixp
:write ("/client/sel/ctl", "Urgent toggle")
599 -- TODO: consider storing list of executables around, and
600 -- this will then reinitialize that list
601 log (" TODO: rehash")
605 -- TODO: this should eventually update something on the /rbar
606 log (" TODO: status")
614 =item add_action_handler (action, fn)
616 Add an Alt-a action handler callback function, I<fn>, for the given action string I<action>.
620 function add_action_handler (action
, fn
)
622 if type(action
) ~= "string" or type(fn
) ~= "function" then
623 error ("expecting a string and a function")
626 if action_handlers
[action
] then
627 error ("action handler already exists for '" .. action
.. "'")
630 action_handlers
[action
] = fn
636 =item remove_action_handler (action)
638 Remove an action handler callback function for the given action string I<action>.
642 function remove_action_handler (action
)
644 action_handlers
[action
] = nil
647 -- ========================================================================
649 -- ========================================================================
651 function ke_fullscreen_toggle()
652 wmixp
:write ("/client/sel/ctl", "Fullscreen toggle")
655 function ke_view_starting_with_letter (letter
)
658 -- find the view name in history in reverse order
659 for i
=#view_hist
,1,-1 do
661 if letter
== v
:sub(1,1) then
667 -- otherwise just pick the first view that matches
668 local all
= get_tags()
669 for i
,v
in pairs(all
) do
670 if letter
== v
:sub(1,1) then
679 function ke_handle_action()
684 for n
in action_hist
:walk_reverse() do
686 actions
[#actions
+1] = n
692 for n
,v
in pairs(action_handlers
) do
694 actions
[#actions
+1] = n
699 local text
= menu(actions
, "action:")
701 log ("Action: " .. text
)
704 local si
= text
:find("%s")
706 act
,args
= string.match(text
.. " ", "(%w+)%s(.+)")
709 local fn
= action_handlers
[act
]
711 action_hist
:add (act
)
712 local r
, err
= pcall (fn
, act
, args
)
714 log ("WARNING: " .. tostring(err
))
722 local key_handlers
= {
723 ["*"] = function (key
)
727 -- execution and actions
728 ["Mod1-Return"] = function (key
)
729 local xterm
= get_conf("xterm") or "xterm"
730 log (" executing: " .. xterm
)
731 os
.execute (wmiir
.. " setsid " .. xterm
.. " &")
733 ["Mod1-Shift-Return"] = function (key
)
734 local tag = tag_menu()
736 local xterm
= get_conf("xterm") or "xterm"
737 log (" executing: " .. xterm
.. " on: " .. tag)
738 next_client_goes_to_tag
= tag
739 os
.execute (wmiir
.. " setsid " .. xterm
.. " &")
742 ["Mod1-a"] = function (key
)
745 ["Mod1-p"] = function (key
)
746 local prog
= prog_menu()
748 prog_hist
:add(prog
:match("([^ ]+)"))
749 log (" executing: " .. prog
)
750 os
.execute (wmiir
.. " setsid " .. prog
.. " &")
753 ["Mod1-Shift-p"] = function (key
)
754 local tag = tag_menu()
756 local prog
= prog_menu()
758 log (" executing: " .. prog
.. " on: " .. tag)
759 next_client_goes_to_tag
= tag
760 os
.execute (wmiir
.. " setsid " .. prog
.. " &")
764 ["Mod1-Shift-c"] = function (key
)
765 write ("/client/sel/ctl", "kill")
768 -- HJKL active selection
769 ["Mod1-h"] = function (key
)
770 write ("/tag/sel/ctl", "select left")
772 ["Mod1-l"] = function (key
)
773 write ("/tag/sel/ctl", "select right")
775 ["Mod1-j"] = function (key
)
776 write ("/tag/sel/ctl", "select down")
778 ["Mod1-k"] = function (key
)
779 write ("/tag/sel/ctl", "select up")
783 ["Mod1-Shift-h"] = function (key
)
784 write ("/tag/sel/ctl", "send sel left")
786 ["Mod1-Shift-l"] = function (key
)
787 write ("/tag/sel/ctl", "send sel right")
789 ["Mod1-Shift-j"] = function (key
)
790 write ("/tag/sel/ctl", "send sel down")
792 ["Mod1-Shift-k"] = function (key
)
793 write ("/tag/sel/ctl", "send sel up")
797 ["Mod1-space"] = function (key
)
798 write ("/tag/sel/ctl", "select toggle")
800 ["Mod1-Shift-space"] = function (key
)
801 write ("/tag/sel/ctl", "send sel toggle")
804 -- work spaces (# and @ are wildcards for numbers and letters)
805 ["Mod4-#"] = function (key
, num
)
806 -- first attempt to find a view that starts with the number requested
807 local num_str
= tostring(num
)
808 if not ke_view_starting_with_letter (num_str
) then
809 -- if we fail, then set it to the index requested
813 ["Mod4-Shift-#"] = function (key
, num
)
814 write ("/client/sel/tags", tostring(num
))
816 ["Mod4-@"] = function (key
, letter
)
817 ke_view_starting_with_letter (letter
)
819 ["Mod4-Shift-@"] = function (key
, letter
)
820 local all
= get_tags()
822 for i
,v
in pairs(all
) do
823 if letter
== v
:sub(1,1) then
824 write ("/client/sel/tags", v
)
829 ["Mod1-comma"] = function (key
)
832 ["Mod1-period"] = function (key
)
835 ["Mod1-r"] = function (key
)
836 -- got to the last view
840 -- switching views and retagging
841 ["Mod1-t"] = function (key
)
843 local tag = tag_menu()
848 ["Mod1-Shift-t"] = function (key
)
849 -- move selected client to a tag
850 local tag = tag_menu()
852 write ("/client/sel/tags", tag)
855 ["Mod1-Shift-r"] = function (key
)
856 -- move selected client to a tag, and follow
857 local tag = tag_menu()
859 -- get the current window id
860 local xid
= wmixp
:read("/client/sel/ctl") or ""
863 write("/client/sel/tags", tag)
865 -- if the client is still in this tag, then
866 -- it might have been a regexp tag... check
867 local test
= wmixp
:read("/client/sel/ctl")
868 if not test
or test
~= xid
then
869 -- if the window moved, follow it
874 ["Mod1-Control-t"] = function (key
)
875 log (" TODO: Mod1-Control-t: " .. key
)
879 ["Mod1-d"] = function (key
)
880 write("/tag/sel/ctl", "colmode sel default-max")
882 ["Mod1-s"] = function (key
)
883 write("/tag/sel/ctl", "colmode sel stack-max")
885 ["Mod1-m"] = function (key
)
886 write("/tag/sel/ctl", "colmode sel stack+max")
888 ["Mod1-f"] = function (key
)
889 ke_fullscreen_toggle()
892 -- changing client flags
893 ["Shift-Mod1-f"] = function (key
)
894 log ("setting flags")
896 local cli
= get_client ()
898 local flags
= { "suspend", "raw" }
899 local current_flags
= cli
:flags_string()
901 local what
= menu(flags
, "current flags: " .. current_flags
.. " toggle:")
905 ["Mod4-space"] = function (key
)
906 local cli
= get_client ()
914 =item add_key_handler (key, fn)
916 Add a keypress handler callback function, I<fn>, for the given key sequence I<key>.
920 function add_key_handler (key
, fn
)
922 if type(key
) ~= "string" or type(fn
) ~= "function" then
923 error ("expecting a string and a function")
926 if key_handlers
[key
] then
927 -- TODO: we may wish to allow multiple handlers for one keypress
928 error ("key handler already exists for '" .. key
.. "'")
931 key_handlers
[key
] = fn
937 =item remove_key_handler (key)
939 Remove an key handler callback function for the given key I<key>.
941 Returns the handler callback function.
945 function remove_key_handler (key
)
947 local fn
= key_handlers
[key
]
948 key_handlers
[key
] = nil
955 =item remap_key_handler (old_key, new_key)
957 Remove a key handler callback function from the given key I<old_key>,
958 and assign it to a new key I<new_key>.
962 function remap_key_handler (old_key
, new_key
)
964 local fn
= remove_key_handler(old_key
)
966 return add_key_handler (new_key
, fn
)
970 -- ------------------------------------------------------------------------
971 -- update the /keys wmii file with the list of all handlers
972 local alphabet
="abcdefghijklmnopqrstuvwxyz"
973 function update_active_keys ()
976 for x
,y
in pairs(key_handlers
) do
978 local i
= x
:find("#$")
982 t
[#t
+ 1] = x
:sub(1,i
-1) .. j
988 for j
=1,alphabet
:len() do
989 local a
= alphabet
:sub(j
,j
)
990 t
[#t
+ 1] = x
:sub(1,i
-1) .. a
993 t
[#t
+ 1] = tostring(x
)
998 local all_keys
= table.concat(t
, "\n")
999 --log ("setting /keys to...\n" .. all_keys .. "\n");
1000 write ("/keys", all_keys
)
1003 -- ------------------------------------------------------------------------
1004 -- update the /lbar wmii file with the current tags
1005 function update_displayed_tags ()
1006 -- list of all screens
1007 local screens
= get_screens()
1009 update_displayed_tags_on_screen()
1014 for i
,s
in pairs(screens
) do
1015 update_displayed_tags_on_screen(s
)
1019 function update_displayed_tags_on_screen(s
)
1020 local lbar
= "/lbar"
1022 lbar
= "/screen/" .. s
.. "/lbar"
1025 -- colours for screen
1026 local fc
= get_screen_ctl(s
, "focuscolors") or get_ctl("focuscolors") or ""
1027 local nc
= get_screen_ctl(s
, "normcolors") or get_ctl("normcolors") or ""
1029 -- build up a table of existing tags in the /lbar
1032 for ent
in wmixp
:idir (lbar
) do
1036 -- for all actual tags in use create any entries in /lbar we don't have
1037 -- clear the old table entries if we have them
1038 local cur
= get_view(s
)
1039 local all
= get_tags()
1041 for i
,v
in pairs(all
) do
1047 create (lbar
.. "/" .. v
, color
.. " " .. v
)
1049 write (lbar
.. "/" .. v
, color
.. " " .. v
)
1053 -- anything left in the old table should be removed now
1054 for i
,v
in pairs(old
) do
1056 remove(lbar
.."/"..i
)
1060 -- this is a hack, and should brobably be rethought
1061 -- the intent is to distinguish the multiple screens
1063 create ("/screen/"..s
.."/lbar/000000000000000000", '-'..s
..'-')
1067 function create_tag_widget(name
)
1068 local nc
= get_ctl("normcolors") or ""
1069 local screens
= get_screens()
1071 create ("/lbar/" .. name
, nc
.. " " .. name
)
1075 for i
,s
in pairs(screens
) do
1076 create ("/screen/"..s
.."/lbar/" .. name
, nc
.. " " .. name
)
1080 function destroy_tag_widget(name
)
1081 local screens
= get_screens()
1083 remove ("/lbar/" .. name
)
1087 for i
,s
in pairs(screens
) do
1088 remove ("/screen/"..s
.."/lbar/" .. name
)
1093 -- ========================================================================
1095 -- ========================================================================
1097 local widget_ev_handlers
= {
1103 =item _handle_widget_event (ev, arg)
1105 Top-level event handler for redispatching events to widgets. This event
1106 handler is added for any widget event that currently has a widget registered
1109 Valid widget events are currently
1111 RightBarMouseDown <buttonnumber> <widgetname>
1112 RightBarClick <buttonnumber> <widgetname>
1114 the "Click" event is sent on mouseup.
1116 The callbacks are given only the button number as their argument, to avoid the
1122 local function _handle_widget_event (ev
, arg
)
1124 log("_handle_widget_event: " .. tostring(ev
) .. " - " .. tostring(arg
))
1126 -- parse arg to strip out our widget name
1127 local number,wname
= string.match(arg
, "(%d+)%s+(.+)")
1129 -- check our dispatch table for that widget
1131 log("Didn't find wname")
1135 local wtable
= widget_ev_handlers
[wname
]
1137 log("No widget cares about" .. wname
)
1141 local fn
= wtable
[ev
] or wtable
["*"]
1143 success
, err
= pcall( fn
, ev
, tonumber(number) )
1145 log("Callback had an error in _handle_widget_event: " .. tostring(err
) )
1149 log("no function found for " .. ev
)
1153 local ev_handlers
= {
1154 ["*"] = function (ev
, arg
)
1155 log ("ev: " .. tostring(ev
) .. " - " .. tostring(arg
))
1158 RightBarClick
= _handle_widget_event
,
1160 -- process timer events
1161 ProcessTimerEvents
= function (ev
, arg
)
1165 -- exit if another wmiirc started up
1166 Start
= function (ev
, arg
)
1168 if arg
== "wmiirc" then
1169 -- backwards compatibility with bash version
1170 log (" exiting; pid=" .. tostring(myid
))
1174 -- ignore if it came from us
1175 local pid
= string.match(arg
, "wmiirc (%d+)")
1177 local pid
= tonumber (pid
)
1178 if not (pid
== myid
) then
1179 log (" exiting; pid=" .. tostring(myid
))
1189 CreateTag
= function (ev
, arg
)
1190 log ("CreateTag: " .. arg
)
1191 create_tag_widget(arg
)
1193 DestroyTag
= function (ev
, arg
)
1194 log ("DestroyTag: " .. arg
)
1195 destroy_tag_widget(arg
)
1197 -- remove the tag from history
1199 for i
=#view_hist
,1,-1 do
1202 table.remove(view_hist
,i
)
1207 FocusTag
= function (ev
, arg
)
1208 log ("FocusTag: " .. arg
)
1210 local tag,scrn
= arg
:match("(%w+)%s*(%w*)")
1215 local file
= "/lbar/" .. tag
1216 if scrn
and scrn
:len() > 0 then
1217 file
= "/screen/" .. scrn
.. file
1220 local fc
= get_screen_ctl(scrn
, "focuscolors") or get_ctl("focuscolors") or ""
1221 log ("# echo " .. fc
.. " " .. tag .. " | wmiir write " .. file
)
1223 create (file
, fc
.. " " .. tag)
1224 write (file
, fc
.. " " .. tag)
1226 UnfocusTag
= function (ev
, arg
)
1227 log ("UnfocusTag: " .. arg
)
1229 local tag,scrn
= arg
:match("(%w+)%s*(%w*)")
1234 local file
= "/lbar/" .. tag
1235 if scrn
and scrn
:len() > 0 then
1236 file
= "/screen/" .. scrn
.. file
1239 local nc
= get_screen_ctl(scrn
, "normcolors") or get_ctl("normcolors") or ""
1240 log ("# echo " .. nc
.. " " .. tag .. " | wmiir write " .. file
)
1242 create (file
, nc
.. " " .. tag)
1243 write (file
, nc
.. " " .. tag)
1245 -- don't duplicate the last entry
1246 if not (tag == view_hist
[#view_hist
]) then
1247 view_hist
[#view_hist
+1] = tag
1249 -- limit to view_hist_max
1250 if #view_hist
> view_hist_max
then
1251 table.remove(view_hist
, 1)
1256 -- key event handling
1257 Key
= function (ev
, arg
)
1258 log ("Key: " .. arg
)
1260 -- can we find an exact match?
1261 local fn
= key_handlers
[arg
]
1263 local key
= arg
:gsub("-%d$", "-#")
1264 -- can we find a match with a # wild card for the number
1265 fn
= key_handlers
[key
]
1267 -- convert the trailing number to a number
1268 magic
= tonumber(arg
:match("-(%d)$"))
1272 local key
= arg
:gsub("-%a$", "-@")
1273 -- can we find a match with a @ wild card for a letter
1274 fn
= key_handlers
[key
]
1276 -- split off the trailing letter
1277 magic
= arg
:match("-(%a)$")
1281 -- everything else failed, try default match
1282 fn
= key_handlers
["*"]
1285 local r
, err
= pcall (fn
, arg
, magic
)
1287 log ("WARNING: " .. tostring(err
))
1292 -- mouse handling on the lbar
1293 LeftBarClick
= function (ev
, arg
)
1294 local button
,tag = string.match(arg
, "(%w+)%s+(%S+)")
1299 ClientFocus
= function (ev
, arg
)
1300 log ("ClientFocus: " .. arg
)
1301 client_focused (arg
)
1303 ColumnFocus
= function (ev
, arg
)
1304 log ("ColumnFocus: " .. arg
)
1308 CreateClient
= function (ev
, arg
)
1309 if next_client_goes_to_tag
then
1310 local tag = next_client_goes_to_tag
1312 next_client_goes_to_tag
= nil
1313 write ("/client/" .. cli
.. "/tags", tag)
1316 client_created (arg
)
1318 DestroyClient
= function (ev
, arg
)
1319 client_destoryed (arg
)
1323 UrgentTag
= function (ev
, arg
)
1324 log ("UrgentTag: " .. arg
)
1325 write ("/lbar/" .. arg
, "*" .. arg
);
1327 NotUrgentTag
= function (ev
, arg
)
1328 log ("NotUrgentTag: " .. arg
)
1329 write ("/lbar/" .. arg
, arg
);
1333 Unresponsive
= function (ev
, arg
)
1334 log ("Unresponsive: " .. arg
)
1335 -- TODO ask the user if it shoudl be killed off
1338 Notice
= function (ev
, arg
)
1339 log ("Notice: " .. arg
)
1340 -- TODO send to the message plugin (or implement there)
1349 =item add_widget_event_handler (wname, ev, fn)
1351 Add an event handler callback for the I<ev> event on the widget named I<wname>
1356 function add_widget_event_handler (wname
, ev
, fn
)
1357 if type(wname
) ~= "string" or type(ev
) ~= "string" or type(fn
) ~= "function" then
1358 error ("expecting string for widget name, string for event name and a function callback")
1361 -- Make sure the widget event handler is present
1362 if not ev_handlers
[ev
] then
1363 ev_handlers
[ev
] = _handle_widget_event
1366 if not widget_ev_handlers
[wname
] then
1367 widget_ev_handlers
[wname
] = { }
1370 if widget_ev_handlers
[wname
][ev
] then
1371 -- TODO: we may wish to allow multiple handlers for one event
1372 error ("event handler already exists on widget '" .. wname
.. "' for '" .. ev
.. "'")
1375 widget_ev_handlers
[wname
][ev
] = fn
1381 =item remove_widget_event_handler (wname, ev)
1383 Remove an event handler callback function for the I<ev> on the widget named I<wname>.
1387 function remove_event_handler (wname
, ev
)
1389 if not widget_ev_handlers
[wname
] then
1393 widget_ev_handlers
[wname
][ev
] = nil
1399 =item add_event_handler (ev, fn)
1401 Add an event handler callback function, I<fn>, for the given event I<ev>.
1405 -- TODO: Need to allow registering widgets for RightBar* events. Should probably be done with its own event table, though
1406 function add_event_handler (ev
, fn
)
1407 if type(ev
) ~= "string" or type(fn
) ~= "function" then
1408 error ("expecting a string and a function")
1411 if ev_handlers
[ev
] then
1412 -- TODO: we may wish to allow multiple handlers for one event
1413 error ("event handler already exists for '" .. ev
.. "'")
1417 ev_handlers
[ev
] = fn
1423 =item remove_event_handler (ev)
1425 Remove an event handler callback function for the given event I<ev>.
1429 function remove_event_handler (ev
)
1431 ev_handlers
[ev
] = nil
1435 -- ========================================================================
1436 -- MAIN INTERFACE FUNCTIONS
1437 -- ========================================================================
1440 xterm
= 'x-terminal-emulator',
1441 xlock
= "xscreensaver-command --lock",
1445 -- ------------------------------------------------------------------------
1446 -- write configuration to /ctl wmii file
1447 -- wmii.set_ctl({ "var" = "val", ...})
1448 -- wmii.set_ctl("var, "val")
1449 function set_ctl (first
,second
)
1450 if type(first
) == "table" and second
== nil then
1452 for x
, y
in pairs(first
) do
1453 write ("/ctl", x
.. " " .. y
)
1456 elseif type(first
) == "string" and type(second
) == "string" then
1457 write ("/ctl", first
.. " " .. second
)
1460 error ("expecting a table or two string arguments")
1464 -- ------------------------------------------------------------------------
1465 -- read a value from /ctl wmii file
1466 -- table = wmii.get_ctl()
1467 -- value = wmii.get_ctl("variable")
1468 function get_ctl (name
)
1471 for s
in iread("/ctl") do
1472 local var
,val
= s
:match("(%w+)%s+(.+)")
1484 -- ------------------------------------------------------------------------
1485 -- write configuration to /screen/*/ctl wmii file
1486 -- wmii.set_screen_ctl("screen", { "var" = "val", ...})
1487 -- wmii.set_screen_ctl("screen", "var, "val")
1488 function set_screen_ctl (screen
, first
, second
)
1489 local ctl
= "/screen/" .. tostring(screen
) .. "/ctl"
1491 error ("screen is not set")
1492 elseif type(first
) == "table" and second
== nil then
1494 for x
, y
in pairs(first
) do
1495 write (ctl
, x
.. " " .. y
)
1498 elseif type(first
) == "string" and type(second
) == "string" then
1499 write (ctl
, first
.. " " .. second
)
1502 error ("expecting a screen name, followed by a table or two string arguments")
1506 -- ------------------------------------------------------------------------
1507 -- read a value from /screen/*/ctl wmii file
1508 -- table = wmii.get_screen_ctl("screen")
1509 -- value = wmii.get_screen_ctl("screen", "variable")
1510 function get_screen_ctl (screen
, name
)
1516 local ctl
= "/screen/" .. tostring(screen
) .. "/ctl"
1517 for s
in iread(ctl
) do
1518 local var
,val
= s
:match("(%w+)%s+(.+)")
1522 -- sometimes first line is the name of the entry
1523 -- in which case there will be no space
1532 -- ------------------------------------------------------------------------
1533 -- set an internal wmiirc.lua variable
1534 -- wmii.set_conf({ "var" = "val", ...})
1535 -- wmii.set_conf("var, "val")
1536 function set_conf (first
,second
)
1537 if type(first
) == "table" and second
== nil then
1539 for x
, y
in pairs(first
) do
1543 elseif type(first
) == "string"
1544 and (type(second
) == "string"
1545 or type(second
) == "number"
1546 or type(second
) == "boolean") then
1547 config
[first
] = second
1550 error ("expecting a table, or string and string/number as arguments")
1554 -- ------------------------------------------------------------------------
1555 -- read an internal wmiirc.lua variable
1556 function get_conf (name
)
1563 -- ========================================================================
1565 -- ========================================================================
1567 -- the event loop instance
1568 local el
= eventloop
.new()
1569 local event_read_fd
= -1
1570 local wmiirc_running
= false
1571 local event_read_start
= 0
1573 -- ------------------------------------------------------------------------
1574 -- start/restart the core event reading process
1575 local function start_event_reader ()
1576 -- prevent adding two readers
1577 if event_read_fd
~= -1 then
1578 if el
:check_exec(event_read_fd
) then
1582 -- prevert rapid restarts
1583 local now
= os
.time()
1584 if os
.difftime(now
, event_read_start
) < 5 then
1585 log("wmii: detected rapid restart of /event reader")
1586 local cmd
= "wmiir ls /ctl"
1587 if os
.execute(cmd
) ~= 0 then
1588 log("wmii: cannot confirm communication with wmii, shutting down!")
1589 wmiirc_running
= false
1592 log("wmii: but things look ok, so we will restart it")
1594 event_read_start
= now
1596 -- start a new event reader
1597 log("wmii: starting /event reading process")
1598 event_read_fd
= el
:add_exec (wmiir
.. " read /event",
1600 local line
= line
or "nil"
1602 -- try to split off the argument(s)
1603 local ev
,arg
= string.match(line
, "(%S+)%s+(.+)")
1608 -- now locate the handler function and call it
1609 local fn
= ev_handlers
[ev
] or ev_handlers
["*"]
1611 local r
, err
= pcall (fn
, ev
, arg
)
1613 log ("WARNING: " .. tostring(err
))
1618 log("wmii: ... fd=" .. tostring(event_read_fd
))
1621 -- ------------------------------------------------------------------------
1622 -- run the event loop and process events, this function does not exit
1623 function run_event_loop ()
1624 -- stop any other instance of wmiirc
1625 wmixp
:write ("/event", "Start wmiirc " .. tostring(myid
))
1627 log("wmii: updating lbar")
1629 update_displayed_tags ()
1631 log("wmii: updating rbar")
1633 update_displayed_widgets ()
1635 log("wmii: updating active keys")
1637 update_active_keys ()
1639 log("wmii: starting event loop")
1640 wmiirc_running
= true
1641 while wmiirc_running
do
1642 start_event_reader()
1643 local sleep_for
= process_timers()
1644 el
:run_loop(sleep_for
)
1646 log ("wmii: exiting")
1649 -- ========================================================================
1651 -- ========================================================================
1653 api_version
= 0.1 -- the API version we export
1655 plugins
= {} -- all plugins that were loaded
1657 -- ------------------------------------------------------------------------
1658 -- plugin loader which also verifies the version of the api the plugin needs
1660 -- here is what it does
1661 -- - does a manual locate on the file using package.path
1662 -- - reads in the file w/o using the lua interpreter
1663 -- - locates api_version=X.Y string
1664 -- - makes sure that api_version requested can be satisfied
1665 -- - if the plugins is available it will set variables passed in
1666 -- - it then loads the plugin
1668 -- TODO: currently the api_version must be in an X.Y format, but we may want
1669 -- to expend this so plugins can say they want '0.1 | 1.3 | 2.0' etc
1671 function load_plugin(name
, vars
)
1672 local backup_path
= package
.path
or "./?.lua"
1674 log ("loading " .. name
)
1676 -- this is the version we want to find
1677 local api_major
, api_minor
= tostring(api_version
):match("(%d+)%.0*(%d+)")
1678 if (not api_major
) or (not api_minor
) then
1679 log ("WARNING: could not parse api_version in core/wmii.lua")
1683 -- first find the plugin file
1684 local s
, path_match
, full_name
, file
1685 for s
in string.gmatch(plugin_path
, "[^;]+") do
1686 -- try to locate the files locally
1687 local fn
= s
:gsub("%?", name
)
1688 file
= io
.open(fn
, "r")
1699 txt
= file
:read("*all")
1704 log ("WARNING: could not load plugin '" .. name
.. "'")
1708 -- find the api_version line
1709 local line
, plugin_version
1710 for line
in string.gmatch(txt
, "%s*api_version%s*=%s*%d+%.%d+%s*") do
1711 plugin_version
= line
:match("api_version%s*=%s*(%d+%.%d+)%s*")
1712 if plugin_version
then
1717 if not plugin_version
then
1718 log ("WARNING: could not find api_version string in plugin '" .. name
.. "'")
1722 -- decompose the version string
1723 local plugin_major
, plugin_minor
= plugin_version
:match("(%d+)%.0*(%d+)")
1724 if (not plugin_major
) or (not plugin_minor
) then
1725 log ("WARNING: could not parse api_version for '" .. name
.. "' plugin")
1729 -- make a version test
1730 if plugin_major
~= api_major
then
1731 log ("WARNING: " .. name
.. " plugin major version missmatch, is " .. plugin_version
1732 .. " (api " .. tonumber(api_version
) .. ")")
1736 if plugin_minor
> api_minor
then
1737 log ("WARNING: '" .. name
.. "' plugin minor version missmatch, is " .. plugin_version
1738 .. " (api " .. tonumber(api_version
) .. ")")
1742 -- the configuration parameters before loading
1743 if type(vars
) == "table" then
1745 for var
,val
in pairs(vars
) do
1746 local success
= pcall (set_conf
, name
.. "." .. var
, val
)
1748 log ("WARNING: bad variable {" .. tostring(var
) .. ", " .. tostring(val
) .. "} "
1749 .. "given; loading '" .. name
.. "' plugin failed.")
1755 -- actually load the module, but use only the path where we though it should be
1756 package
.path
= path_match
1757 local success
,what
= pcall (require
, name
)
1758 package
.path
= backup_path
1760 log ("WARNING: failed to load '" .. name
.. "' plugin")
1761 log (" - path: " .. tostring(path_match
))
1762 log (" - file: " .. tostring(full_name
))
1763 log (" - plugin's api_version: " .. tostring(plugin_version
))
1764 log (" - reason: " .. tostring(what
))
1769 log ("OK, plugin " .. name
.. " loaded, requested api v" .. plugin_version
)
1770 plugins
[name
] = what
1774 -- ------------------------------------------------------------------------
1779 -- ------------------------------------------------------------------------
1780 -- create a widget object and add it to the wmii /rbar
1783 -- widget = wmii.widget:new ("999_clock")
1784 -- widget = wmii.widget:new ("999_clock", clock_event_handler)
1785 function widget
:new (name
, fn
)
1788 if type(name
) == "string" then
1790 if type(fn
) == "function" then
1794 error ("expected name followed by an optional function as arguments")
1797 setmetatable (o
,self
)
1799 self
.__gc
= function (o
) o
:hide() end
1807 -- ------------------------------------------------------------------------
1808 -- stop and destroy the timer
1809 function widget
:delete ()
1810 widgets
[self
.name
] = nil
1814 -- ------------------------------------------------------------------------
1815 -- displays or updates the widget text
1819 -- w:show("foo", "#888888 #222222 #333333")
1820 -- w:show("foo", cell_fg .. " " .. cell_bg .. " " .. border)
1822 function widget
:show (txt
, colors
)
1823 local colors
= colors
or get_ctl("normcolors") or ""
1824 local txt
= txt
or self
.txt
or ""
1827 towrite
= colors
.. " " .. towrite
1829 if not self
.txt
then
1830 create ("/rbar/" .. self
.name
, towrite
)
1832 write ("/rbar/" .. self
.name
, towrite
)
1837 -- ------------------------------------------------------------------------
1838 -- hides a widget and removes it from the bar
1839 function widget
:hide ()
1841 remove ("/lbar/" .. self
.name
)
1849 =item widget:add_event_handler (ev, fn)
1851 Add an event handler callback for this widget, using I<fn> for event I<ev>
1856 function widget
:add_event_handler (ev
, fn
)
1857 add_widget_event_handler( self
.name
, ev
, fn
)
1861 -- ------------------------------------------------------------------------
1862 -- remove all /rbar entries that we don't have widget objects for
1863 function update_displayed_widgets ()
1864 -- colours for /rbar
1865 local nc
= get_ctl("normcolors") or ""
1867 -- build up a table of existing tags in the /lbar
1870 for s
in wmixp
:idir ("/rbar") do
1874 -- for all actual widgets in use we want to remove them from the old list
1876 for i
,v
in pairs(widgets
) do
1880 -- anything left in the old table should be removed now
1881 for i
,v
in pairs(old
) do
1888 -- ------------------------------------------------------------------------
1889 -- create a new program and for each line it generates call the callback function
1890 -- returns fd which can be passed to kill_exec()
1891 function add_exec (command
, callback
)
1892 return el
:add_exec (command
, callback
)
1895 -- ------------------------------------------------------------------------
1896 -- terminates a program spawned off by add_exec()
1897 function kill_exec (fd
)
1898 return el
:kill_exec (fd
)
1901 -- ------------------------------------------------------------------------
1906 -- ------------------------------------------------------------------------
1907 -- create a timer object and add it to the event loop
1910 -- timer:new (my_timer_fn)
1911 -- timer:new (my_timer_fn, 15)
1912 function timer
:new (fn
, seconds
)
1915 if type(fn
) == "function" then
1918 error ("expected function followed by an optional number as arguments")
1921 setmetatable (o
,self
)
1923 self
.__gc
= function (o
) o
:stop() end
1926 timers
[#timers
+1] = o
1934 -- ------------------------------------------------------------------------
1935 -- stop and destroy the timer
1936 function timer
:delete ()
1939 for i
,t
in pairs(timers
) do
1941 table.remove (timers
,i
)
1947 -- ------------------------------------------------------------------------
1948 -- run the timer given new interval
1949 function timer
:resched (seconds
)
1950 local seconds
= seconds
or self
.interval
1951 if not (type(seconds
) == "number") then
1952 error ("timer:resched expected number as argument")
1955 local now
= tonumber(os
.date("%s"))
1957 self
.interval
= seconds
1958 self
.next_time
= now
+ seconds
1960 -- resort the timer list
1961 table.sort (timers
, timer
.is_less_then
)
1964 -- helper for sorting timers
1965 function timer
:is_less_then(another
)
1966 if not self
.next_time
then
1967 return false -- another is smaller, nil means infinity
1969 elseif not another
.next_time
then
1970 return true -- self is smaller, nil means infinity
1972 elseif self
.next_time
< another
.next_time
then
1973 return true -- self is smaller than another
1976 return false -- another is smaller then self
1979 -- ------------------------------------------------------------------------
1981 function timer
:stop ()
1982 self
.next_time
= nil
1984 -- resort the timer list
1985 table.sort (timers
, timer
.is_less_then
)
1988 -- ------------------------------------------------------------------------
1989 -- figure out how long before the next event
1990 function time_before_next_timer_event()
1991 local tmr
= timers
[1]
1992 if tmr
and tmr
.next_time
then
1993 local now
= tonumber(os
.date("%s"))
1994 local seconds
= tmr
.next_time
- now
1999 return 0 -- sleep for ever
2002 -- ------------------------------------------------------------------------
2003 -- handle outstanding events
2004 function process_timers ()
2005 local now
= tonumber(os
.date("%s"))
2009 for i
,tmr
in pairs (timers
) do
2011 -- prune out removed timers
2012 table.remove(timers
,i
)
2015 elseif not tmr
.next_time
then
2016 -- break out once we find a timer that is stopped
2019 elseif tmr
.next_time
> now
then
2020 -- break out once we get to the future
2024 -- this one is good to go
2025 torun
[#torun
+1] = tmr
2028 for i
,tmr
in pairs (torun
) do
2030 local status
,new_interval
= pcall (tmr
.fn
, tmr
)
2032 new_interval
= new_interval
or self
.interval
2033 if new_interval
and (new_interval
~= -1) then
2034 tmr
:resched(new_interval
)
2037 log ("ERROR: " .. tostring(new_interval
))
2041 local sleep_for
= time_before_next_timer_event()
2045 -- ------------------------------------------------------------------------
2046 -- cleanup everything in preparation for exit() or exec()
2051 log ("wmii: stopping timer events")
2053 for i
,tmr
in pairs (timers
) do
2054 pcall (tmr
.delete
, tmr
)
2058 log ("wmii: terminating eventloop")
2060 pcall(el
.kill_all
,el
)
2062 log ("wmii: disposing of widgets")
2064 -- dispose of all widgets
2065 for i
,v
in pairs(widgets
) do
2070 -- FIXME: it doesn't seem to do what I want
2072 log ("wmii: releasing plugins")
2074 for i,p in pairs(plugins) do
2076 pcall (p.cleanup, p)
2082 log ("wmii: dormant")
2083 wmiirc_running
= false
2086 -- ========================================================================
2088 -- ========================================================================
2091 -- Notes on client tracking
2093 -- When a client is created wmii sends us a CreateClient message, and
2094 -- we in turn create a 'client' object and store it in the 'clients'
2095 -- table indexed by the client's ID.
2097 -- Each client object stores the following:
2098 -- .xid - the X client ID
2099 -- .pid - the process ID
2100 -- .prog - program object representing the process
2102 -- The client and program objects track the following modes for each program:
2105 -- - for each client window
2106 -- - Mod4-space toggles the state between normal and raw
2107 -- - Mod1-f raw also toggles the state
2108 -- - in raw mode all input goes to the client, except for Mod4-space
2109 -- - a focused client with raw mode enabled is put into raw mode
2112 -- - for each program
2113 -- - Mod1-f suspend toggles the state for current client's program
2114 -- - a focused client, whose program was previous suspended is resumed
2115 -- - an unfocused client, with suspend enabled, will be suspended
2116 -- - suspend/resume is done by sending the STOP/CONT signals to the PID
2119 function xid_to_pid (xid
)
2120 local cmd
= "xprop -id " .. tostring(xid
) .. " _NET_WM_PID"
2121 local file
= io
.popen (cmd
)
2122 local out
= file
:read("*a")
2124 local pid
= out
:match("^_NET_WM_PID.*%s+=%s+(%d+)%s+$")
2125 return tonumber(pid
)
2128 local focused_xid
= nil
2129 local clients
= {} -- table of client objects indexed by xid
2130 local programs
= {} -- table of program objects indexed by pid
2131 local mode_widget
= widget
:new ("999_client_mode")
2133 -- make programs table have weak values
2134 -- programs go away as soon as no clients point to it
2135 local programs_mt
= {}
2136 setmetatable(programs
, programs_mt
)
2137 programs_mt
.__mode
= 'v'
2141 function program
:new (pid
)
2144 setmetatable (o
,self
)
2146 self
.__gc
= function (old
) old
:cont() end
2147 -- initialize the new object
2151 o
.suspend
.toggle
= function (prog
)
2152 prog
.suspend
.enabled
= not prog
.suspend
.enabled
2154 o
.suspend
.enabled
= false -- if true, defocusing suspends (SIGSTOP)
2155 o
.suspend
.active
= true -- if true, focusing resumes (SIGCONT)
2159 function program
:stop ()
2160 if not self
.suspend
.active
then
2161 local cmd
= "kill -STOP " .. tostring(self
.pid
)
2162 log (" executing: " .. cmd
)
2164 self
.suspend
.active
= true
2168 function program
:cont ()
2169 if self
.suspend
.active
then
2170 local cmd
= "kill -CONT " .. tostring(self
.pid
)
2171 log (" executing: " .. cmd
)
2173 self
.suspend
.active
= false
2177 function get_program (pid
)
2178 local prog
= programs
[pid
]
2179 if pid
and not prog
then
2180 prog
= program
:new (pid
)
2181 programs
[pid
] = prog
2188 function client
:new (xid
)
2189 local pid
= xid_to_pid(xid
)
2191 log ("WARNING: failed to convert XID " .. tostring(xid
) .. " to a PID")
2196 setmetatable (o
,self
)
2197 self
.__index
= function (t
,k
)
2198 if k
== 'suspend' then -- suspend mode is tracked per program
2199 return t
.prog
.suspend
2203 self
.__gc
= function (old
) old
.prog
=nil end
2204 -- initialize the new object
2207 o
.prog
= get_program (pid
)
2210 o
.raw
.toggle
= function (cli
)
2211 cli
.raw
.enabled
= not cli
.raw
.enabled
2214 o
.raw
.enabled
= false -- if true, raw mode enabled when client is focused
2218 function client
:stop ()
2219 if self
.suspend
.enabled
then
2224 function client
:cont ()
2228 function client
:set_raw_mode()
2229 if not self
or not self
.raw
.enabled
then -- normal mode
2230 update_active_keys ()
2232 write ("/keys", "Mod4-space")
2236 function client
:toggle(what
)
2237 if what
and self
[what
] then
2238 local ctl
= self
[what
]
2242 log ("xid=" .. tostring (xid
)
2243 .. " pid=" .. tostring (self
.pid
) .. " (" .. tostring (self
.prog
.pid
) .. ")"
2244 .. " what=" .. tostring (what
)
2245 .. " enabled=" .. tostring(ctl
["enabled"]))
2247 mode_widget
:show (self
:flags_string())
2250 function client
:flags_string()
2252 if self
.suspend
.enabled
then ret
= ret
.. "s" else ret
= ret
.. "-" end
2253 if self
.raw
.enabled
then ret
= ret
.. "r" else ret
= ret
.. "-" end
2257 function get_client (xid
)
2258 local xid
= xid
or wmixp
:read("/client/sel/ctl")
2259 local cli
= clients
[xid
]
2261 cli
= client
:new (xid
)
2267 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
2268 function client_created (xid
)
2269 log ("-client_created " .. tostring(xid
))
2270 return get_client(xid
)
2273 function client_destoryed (xid
)
2274 log ("-client_destoryed " .. tostring(xid
))
2275 if clients
[xid
] then
2276 local cli
= clients
[xid
]
2278 log (" del pid: " .. tostring(cli
.pid
))
2281 if focused_xid
== xid
then
2286 function client_focused (xid
)
2287 log ("-client_focused " .. tostring(xid
))
2288 -- return the current focused xid if nil is passed
2289 if type(xid
) ~= 'string' or not xid
:match("0x%x*$") then
2292 -- do nothing if the same xid
2293 if focused_xid
== xid
then
2297 local old
= clients
[focused_xid
]
2298 local new
= get_client(xid
)
2300 -- handle raw mode switch
2301 if not old
or ( old
and new
and old
.raw
.enabled
~= new
.raw
.enabled
) then
2305 -- do nothing if the same pid
2306 if old
and new
and old
.pid
== new
.pid
then
2307 mode_widget
:show (new
:flags_string())
2313 log (" old pid: " .. tostring(old.pid)
2314 .. " xid: " .. tostring(old.xid)
2315 .. " flags: " .. old:flags_string())
2322 log (" new pid: " .. tostring(new.pid)
2323 .. " xid: " .. tostring(new.xid)
2324 .. " flags: " .. new:flags_string())
2329 mode_widget
:show (new
:flags_string())
2335 -- ========================================================================
2337 -- ========================================================================
2350 Used to determine location of wmii's listen socket.
2356 L<wmii(1)>, L<lua(1)>
2360 Bart Trojanowski B<< <bart@jukie.net> >>
2362 =head1 COPYRIGHT AND LICENSE
2364 Copyright (c) 2007, Bart Trojanowski <bart@jukie.net>
2366 This is free software. You may redistribute copies of it under the terms of
2367 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
2368 is NO WARRANTY, to the extent permitted by law.