1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
--
-- ion/mod_statusbar/mod_statusbar.lua
--
-- Copyright (c) Tuomo Valkonen 2004-2007.
--
-- See the included file LICENSE for details.
--
-- This is a slight abuse of the package.loaded variable perhaps, but
-- library-like packages should handle checking if they're loaded instead of
-- confusing the user with require/include differences.
if package.loaded["mod_statusbar"] then return end
if not ioncore.load_module("mod_statusbar") then
return
end
local mod_statusbar=_G["mod_statusbar"]
assert(mod_statusbar)
-- Meter list {{{
local meters={}
--DOC
-- Inform of a value.
function mod_statusbar.inform(name, value)
meters[name]=value
end
-- }}}
-- Template processing {{{
local function process_template(template, meter_f, text_f, stretch_f)
local st, en, b, c, r, p, tmp
while template~="" do
-- Find '%something'
st, en, b, r=string.find(template, '^(.-)%%(.*)')
if not b then
-- Not found
text_f(template)
break
else
if b~="" then
-- Add preciding text as normal text element
text_f(b)
end
template=r
-- Check for '% ' and '%%'
st, en, c, r=string.find(template, '^([ %%])(.*)')
if c then
if c==' ' then
stretch_f(c)
else
text_f(c)
end
template=r
else
-- Extract [alignment][zero padding]<meter name>
local pat='([<|>]?)(0*[0-9]*)([a-zA-Z0-9_]+)'
-- First within {...}
st, en, c, p, b, r=string.find(template, '^{'..pat..'}(.*)')
if not st then
-- And then without
st, en, c, p, b, r=string.find(template, '^'..pat..'(.*)')
end
if b then
meter_f(b, c, tonumber(p))
template=r
end
end
end
end
end
function mod_statusbar.template_to_table(template)
local res={}
local m=meters --set_date(stng, meters)
local aligns={["<"]=0, ["|"]=1, [">"]=2}
process_template(template,
-- meter
function(s, c, p)
if s=="filler" then
table.insert(res, {type=4})
elseif (string.find(s, "^systray$") or
string.find(s, "^systray_")) then
table.insert(res, {
type=5,
meter=s,
align=aligns[c],
})
else
table.insert(res, {
type=2,
meter=s,
align=aligns[c],
tmpl=meters[s.."_template"],
zeropad=p,
})
end
end,
-- text
function(t)
table.insert(res, {
type=1,
text=t,
})
end,
-- stretch
function(t)
table.insert(res, {
type=3,
text=t,
})
end)
return res
end
mod_statusbar._set_template_parser(mod_statusbar.template_to_table)
-- }}}
-- Update {{{
--DOC
-- Update statusbar contents. To be called after series
-- of \fnref{mod_statusbar.inform} calls.
function mod_statusbar.update(update_templates)
for _, sb in pairs(mod_statusbar.statusbars()) do
if update_templates then
local t=sb:get_template_table()
for _, v in pairs(t) do
if v.meter then
v.tmpl=meters[v.meter.."_template"]
end
end
sb:set_template_table(t)
end
sb:update(meters)
end
end
-- }}}
-- ion-statusd support {{{
local statusd_pid=0
function mod_statusbar.rcv_statusd(str)
local data=""
local updatenw=false
local updated=false
local function doline(i)
if i=="." then
mod_statusbar.update(updatenw)
updated=true
else
local _, _, m, v=string.find(i, "^([^:]+):%s*(.*)")
if m and v then
mod_statusbar.inform(m, v)
updatenw=updatenw or string.find(m, "_template")
end
end
return ""
end
while str do
updated=false
data=string.gsub(data..str, "([^\n]*)\n", doline)
str=coroutine.yield(updated)
end
ioncore.warn(TR("ion-statusd quit."))
statusd_pid=0
meters={}
mod_statusbar.update(updatenw)
end
local function get_modules()
local mods={}
local specials={["filler"]=true, ["systray"]=true}
for _, sb in pairs(mod_statusbar.statusbars()) do
for _, item in pairs(sb:get_template_table()) do
if item.type==2 and not specials[item.meter] then
local _, _, m=string.find(item.meter, "^([^_]*)");
if m and m~="" then
mods[m]=true
end
end
end
end
return mods
end
function mod_statusbar.cfg_statusd(cfg)
if date_format_backcompat_kludge then
if not cfg.date then
cfg=table.copy(cfg, false)
cfg.date={date_format=date_format_backcompat_kludge}
elseif not cfg.date.date_format then
cfg=table.copy(cfg, true)
cfg.date.date_format=date_format_backcompat_kludge
end
end
--TODO: don't construct file name twice.
ioncore.write_savefile("cfg_statusd", cfg)
return ioncore.get_savefile("cfg_statusd")
end
function mod_statusbar.rcv_statusd_err(str)
if str then
io.stderr:write(str)
end
end
--DOC
-- Load modules and launch \file{ion-statusd} with configuration
-- table \var{cfg}. The options for each \file{ion-statusd} monitor
-- script should be contained in the corresponding sub-table of \var{cfg}.
function mod_statusbar.launch_statusd(cfg)
if statusd_pid>0 then
return
end
local mods=get_modules()
-- Load modules
for m in pairs(mods) do
if dopath("statusbar_"..m, true) then
mods[m]=nil
end
end
-- Lookup ion-statusd
local statusd=ioncore.lookup_script("ion-statusd")
if not statusd then
ioncore.warn(TR("Could not find %s", script))
return
end
local statusd_errors
local function initrcverr(str)
statusd_errors=(statusd_errors or "")..str
end
local cfg=mod_statusbar.cfg_statusd(cfg or {})
local params=""
for m in pairs(mods) do
params=params.." -m "..m
end
local cmd=statusd.." -q -c "..cfg..params
local rcv=coroutine.wrap(mod_statusbar.rcv_statusd)
local rcverr=mod_statusbar.rcv_statusd_err
statusd_pid=mod_statusbar._launch_statusd(cmd,
rcv, initrcverr,
rcv, rcverr)
if statusd_errors then
warn(TR("Errors starting ion-statusd:\n")..statusd_errors)
end
if statusd_pid<=0 then
warn(TR("Failed to start ion-statusd."))
end
end
--}}}
-- Initialisation and default settings {{{
--DOC
-- Create a statusbar. The possible parameters in the
-- table \var{param} are:
--
-- \begin{tabularx}{\linewidth}{llX}
-- Variable & Type & Description \\
-- \var{template} & string & The template; see
-- Section \ref{sec:statusbar}. \\
-- \var{pos} & string & Position: \code{"tl"}, \code{"tr"},
-- \code{"bl"} or \code{"br"}
-- (for the obvious combinations of
-- top/left/bottom/right). \\
-- \var{screen} & integer & Screen number to create the statusbar on. \\
-- \var{fullsize} & boolean & If set, the statusbar will waste
-- space instead of adapting to layout. \\
-- \var{systray} & boolaen & Swallow (KDE protocol) systray icons. \\
-- \end{tabularx}
--
function mod_statusbar.create(param)
local scr=ioncore.find_screen_id(param.screen or 0)
if not scr then
error(TR("Screen not found."))
end
if not param.force then
local stdisp=scr:get_stdisp()
if stdisp and stdisp.reg then
error(TR("Screen already has an stdisp. Refusing to create a "..
"statusbar."))
end
end
local sb=scr:set_stdisp({
type="WStatusBar",
pos=(param.pos or "bl"),
fullsize=param.fullsize,
name="*statusbar*",
template=param.template,
template_table=param.template_table,
systray=param.systray,
})
if not sb then
error(TR("Failed to create statusbar."))
end
return sb
end
--DOC
-- Function to terminate \file{ion-statusd} on exit or reload. Should
-- be called from hook \var{deinit}.
function mod_statusbar.terminate_statusd()
if statusd_pid==0 then
return
end
mod_statusbar._terminate_statusd(statusd_pid)
statusd_pid=0
end
-- Establish hook
ioncore.get_hook("ioncore_deinit_hook"):add(mod_statusbar.terminate_statusd)
-- }}}
-- Mark ourselves loaded.
package.loaded["mod_statusbar"]=true
-- Load user configuration file
dopath('cfg_statusbar', true)
-- Launch statusd if the user didn't launch it.
if statusd_pid<=0 then
mod_statusbar.launch_statusd()
end
|