Released version 3-2015061300
[notion.git] / mod_xkbevents / xkbbell.lua
blobc2e076f995ce68f8593f10604ed50cf774853392
1 --[[
2 Author: Etan Reisner
3 Email: deryni@unreliablesource.net
4 Summary: Displays an WInfoWin on XkbBell events, also sets xkbbell grattr:s on the frame containing the window that triggered the event.
5 License: MIT
6 Last Updated: 2011-05-05
8 Copyright (c) Etan Reisner 2011
9 --]]
11 local bell_hook = ioncore.get_hook("xkb_bell_event")
12 if not bell_hook then
13 pcall(dopath, "mod_xkbevents")
14 bell_hook = ioncore.get_hook("xkb_bell_event")
15 if not bell_hook then
16 warn("Could not load mod_xkbevents.")
17 return
18 end
19 end
21 xkbbell = xkbbell or {}
22 if not xkbbell.timeout then
23 xkbbell.timeout = 10000
24 end
25 if not xkbbell.low_threshold then
26 xkbbell.low_threshold = 50
27 end
28 if not xkbbell.medium_threshold then
29 xkbbell.medium_threshold = 75
30 end
31 if not xkbbell.high_threshold then
32 xkbbell.high_threshold = 100
33 end
35 local timer = ioncore.create_timer()
36 local screen_iws = setmetatable({}, {__mode="kv"})
37 local iw_hide_funs = setmetatable({}, {__mode="kv"})
38 local active_frames = setmetatable({}, {__mode="kv"})
40 local function set_hidden(iw, scr, state)
41 scr = scr or ioncore.find_manager(iw, "WScreen")
42 scr:set_hidden(iw, state or "set")
43 end
45 local function unset_grattrs(frame)
46 frame:set_grattr("xkbbell", "unset")
47 frame:set_grattr("xkbbell-low", "unset")
48 frame:set_grattr("xkbbell-medium", "unset")
49 frame:set_grattr("xkbbell-high", "unset")
50 end
52 local function hide_fun(iw, scr)
53 return function()
54 set_hidden(iw, scr)
55 end
56 end
58 local function region_notify(reg, how)
59 if how ~= "activated" then
60 return
61 end
63 local frame = ioncore.find_manager(reg, "WFrame")
64 if frame and active_frames[frame] then
65 active_frames[frame] = nil
66 unset_grattrs(frame)
67 end
68 end
70 local function bell_info(tab)
71 local style = "xkbbell"
72 if tab.percent <= xkbbell.low_threshold then
73 style = "xkbbell-low"
74 elseif tab.percent <= xkbbell.medium_threshold then
75 style = "xkbbell-medium"
76 elseif tab.percent <= xkbbell.high_threshold then
77 style = "xkbbell-high"
78 end
80 if tab.window then
81 local frame = ioncore.find_manager(tab.window, "WFrame")
82 if frame then
83 local cframe = ioncore.find_manager(ioncore.current(), "WFrame")
84 if frame ~= cframe then
85 active_frames[frame] = true
86 unset_grattrs(frame)
87 frame:set_grattr(style, "set")
88 end
89 return
90 end
91 -- Fall through to setting a screen iw for this bell.
92 end
94 local scr = ioncore.current():screen_of()
95 local iw = screen_iws[scr]
96 ioncore.defer(function()
97 if not iw then
98 iw = scr:attach_new{
99 name="xkbbell"..scr:id(),
100 style=style,
102 type="WInfoWin",
103 hidden=true,
104 unnumbered=true,
105 sizepolicy="free",
106 geom={x=0,y=0,w=1,h=1},
108 screen_iws[scr] = iw
109 iw:set_text("Bell!", -1)
111 iw_hide_funs[scr] = hide_fun(iw, scr)
114 set_hidden(iw, scr, "unset")
115 timer:set(xkbbell.timeout, iw_hide_funs[scr])
116 end)
119 bell_hook:add(bell_info)
121 local hook = ioncore.get_hook("region_notify")
122 if hook then
123 hook:add()