| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
local awful = require("awful") |
| 15 |
local string = string |
| 16 |
local io = io |
| 17 |
local table = table |
| 18 |
local pairs = pairs |
| 19 |
local timer = timer |
| 20 |
local type = type |
| 21 |
local image = image |
| 22 |
local capi = {oocairo = oocairo, timer = timer, dbus = dbus} |
| 23 |
local tonumber = tonumber |
| 24 |
local print = print |
| 25 |
local error = error |
| 26 |
|
| 27 |
|
| 28 |
local bashets = {} |
| 29 |
|
| 30 |
|
| 31 |
local script_path = "/usr/share/awesome/bashets/" |
| 32 |
local tmp_folder = "/tmp/" |
| 33 |
|
| 34 |
|
| 35 |
local util = {} |
| 36 |
|
| 37 |
|
| 38 |
local timerdata = {} |
| 39 |
local timers = {} |
| 40 |
|
| 41 |
|
| 42 |
local ewidgets = {} |
| 43 |
|
| 44 |
|
| 45 |
local defaults = {} |
| 46 |
defaults.update_time = 1 |
| 47 |
defaults.file_update_time = 2 |
| 48 |
defaults.format_string = "$1" |
| 49 |
defaults.separator = " " |
| 50 |
defaults.updater = "runner.sh" |
| 51 |
|
| 52 |
|
| 53 |
local is_running = false |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
function util.split(str, sep) |
| 61 |
if sep == nil then |
| 62 |
return {str} |
| 63 |
end |
| 64 |
|
| 65 |
local parts = {} |
| 66 |
local first = 1 |
| 67 |
local ostart, oend = string.find(str, sep, first, true) |
| 68 |
|
| 69 |
while ostart do |
| 70 |
local part = string.sub(str, first, ostart - 1) |
| 71 |
table.insert(parts, part) |
| 72 |
first = oend + 1 |
| 73 |
ostart, oend = string.find(str, sep, first, true) |
| 74 |
end |
| 75 |
|
| 76 |
local part = string.sub(str, first) |
| 77 |
table.insert(parts, part) |
| 78 |
|
| 79 |
return parts |
| 80 |
end |
| 81 |
|
| 82 |
function util.tmpkey(script) |
| 83 |
|
| 84 |
|
| 85 |
local tmpname = string.gsub(script, '/', '') |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
tmpname = string.gsub(tmpname, '%s+', '.') |
| 90 |
|
| 91 |
return tmpname |
| 92 |
end |
| 93 |
|
| 94 |
function util.tmpname(script) |
| 95 |
|
| 96 |
local file = tmp_folder .. util.tmpkey(script) .. '.bashets.out' |
| 97 |
|
| 98 |
return file |
| 99 |
end |
| 100 |
|
| 101 |
function util.fullpath(script) |
| 102 |
if string.find(script, '^/') == nil then |
| 103 |
script = script_path .. script |
| 104 |
end |
| 105 |
|
| 106 |
return script |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
function util.execfile(script, file) |
| 113 |
|
| 114 |
awful.util.spawn_with_shell(script .. " > " .. file) |
| 115 |
end |
| 116 |
|
| 117 |
|
| 118 |
function util.readstring(str, sep) |
| 119 |
if sep == nil then |
| 120 |
return str |
| 121 |
else |
| 122 |
parts = util.split(str, sep); |
| 123 |
return parts |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function util.readfile(file, sep) |
| 131 |
local fh = io.input(file) |
| 132 |
local str = fh:read("*all"); |
| 133 |
io.close(fh) |
| 134 |
|
| 135 |
return util.readstring(str, sep) |
| 136 |
end |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
function util.readshell(script, sep) |
| 142 |
local str = awful.util.pread(script) |
| 143 |
|
| 144 |
return util.readstring(str, sep) |
| 145 |
end |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
function util.format(parts, format, sep) |
| 152 |
|
| 153 |
for k,part in pairs(parts) do |
| 154 |
local part = string.gsub(part, "%%", "%1%1") |
| 155 |
part = awful.util.escape(part) |
| 156 |
format = string.gsub(format, "$" .. k, part) |
| 157 |
end |
| 158 |
|
| 159 |
return format |
| 160 |
end |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
function util.add_to_timings(updtime, func, force_new) |
| 166 |
local found = false |
| 167 |
|
| 168 |
|
| 169 |
for k,tmr in pairs(timerdata) do |
| 170 |
if tmr[1] == updtime and (force_new == nil or force_new == false) then |
| 171 |
table.insert(timerdata[k][2], func) |
| 172 |
found = true |
| 173 |
end |
| 174 |
end |
| 175 |
|
| 176 |
|
| 177 |
if not found then |
| 178 |
table.insert(timerdata, {updtime, {func}}) |
| 179 |
end |
| 180 |
end |
| 181 |
|
| 182 |
|
| 183 |
function util.create_timers_table() |
| 184 |
|
| 185 |
for _,tmr in pairs(timerdata) do |
| 186 |
|
| 187 |
local t |
| 188 |
if capi.timer ~= nil then |
| 189 |
t = capi.timer {timeout = tmr[1]} |
| 190 |
else |
| 191 |
t = timer {timeout = tmr[1]} |
| 192 |
end |
| 193 |
|
| 194 |
local f = function() |
| 195 |
for _, func in pairs(tmr[2]) do |
| 196 |
func() |
| 197 |
end |
| 198 |
end |
| 199 |
|
| 200 |
if t.add_signal ~= nil then |
| 201 |
t:add_signal("timeout", f) |
| 202 |
else |
| 203 |
t:connect_signal("timeout", f) |
| 204 |
end |
| 205 |
table.insert(timers, t) |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
function util.update_widget_field(widget, valuess) |
| 210 |
|
| 211 |
if widget.type == "imagebox" then |
| 212 |
widget["image"] = image(valuess) |
| 213 |
elseif widget.set_image ~= nil then |
| 214 |
|
| 215 |
|
| 216 |
widget:set_image(valuess) |
| 217 |
elseif widget.type == "textbox" then |
| 218 |
widget["text"] = valuess |
| 219 |
elseif widget.set_markup ~= nil then |
| 220 |
widget:set_markup(valuess) |
| 221 |
|
| 222 |
elseif widget.set_value ~= nil then |
| 223 |
widget:set_value(tonumber(valuess)) |
| 224 |
elseif widget.add_value ~= nil then |
| 225 |
widget:add_value(tonumber(valuess)) |
| 226 |
end |
| 227 |
|
| 228 |
end |
| 229 |
|
| 230 |
|
| 231 |
function util.update_widget(widget, values, format) |
| 232 |
if widget ~= nil then |
| 233 |
if type(values) == "table" then |
| 234 |
util.update_widget_field(widget, util.format(values, format)) |
| 235 |
else |
| 236 |
util.update_widget_field(widget, values) |
| 237 |
end |
| 238 |
end |
| 239 |
end |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
function bashets.set_script_path(path) |
| 246 |
script_path = path |
| 247 |
end |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
function bashets.set_temporary_path(path) |
| 252 |
tmp_folder = path |
| 253 |
end |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
function bashets.set_defaults(defs) |
| 258 |
if type(defs) == "table" then |
| 259 |
if defs.update_time ~= nil then |
| 260 |
defaults.update_time = defs.update_time |
| 261 |
end |
| 262 |
if defs.file_update_time ~= nil then |
| 263 |
defaults.file_update_time = defs.file_update_time |
| 264 |
end |
| 265 |
if defs.format_string ~= nil then |
| 266 |
defaults.format_string = defs.format_string |
| 267 |
end |
| 268 |
if defs.updater ~= nil then |
| 269 |
defaults.updater = defs.updater |
| 270 |
end |
| 271 |
|
| 272 |
defaults.separator = defs.separator |
| 273 |
end |
| 274 |
end |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
function bashets.start() |
| 281 |
|
| 282 |
if (not timers) or table.maxn(timers) == 0 then |
| 283 |
util.create_timers_table() |
| 284 |
end |
| 285 |
|
| 286 |
for _, tmr in pairs(timers) do |
| 287 |
tmr:start() |
| 288 |
end |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
for _, wgt in pairs(ewidgets) do |
| 295 |
awful.util.spawn_with_shell(wgt.cmd) |
| 296 |
end |
| 297 |
is_running = true |
| 298 |
end |
| 299 |
|
| 300 |
|
| 301 |
function bashets.stop() |
| 302 |
|
| 303 |
for _, tmr in pairs(timers) do |
| 304 |
tmr:stop() |
| 305 |
end |
| 306 |
|
| 307 |
|
| 308 |
awful.util.spawn_with_shell('killall ' .. defaults.updater) |
| 309 |
|
| 310 |
is_running = false |
| 311 |
end |
| 312 |
|
| 313 |
|
| 314 |
function bashets.get_running() |
| 315 |
return is_running |
| 316 |
end |
| 317 |
|
| 318 |
|
| 319 |
function bashets.toggle() |
| 320 |
if is_running then |
| 321 |
bashets.start() |
| 322 |
else |
| 323 |
bashets.stop() |
| 324 |
end |
| 325 |
end |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
function bashets.schedule(func, updtime) |
| 331 |
updtime = updtime or defaults.update_time |
| 332 |
if func ~= nil then |
| 333 |
util.add_to_timings(updtime, func) |
| 334 |
end |
| 335 |
end |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
function bashets.schedule_w(data_provider, widget, callback, format, updtime) |
| 345 |
if callback ~= nil and type(callback) == "function" then |
| 346 |
if widget ~= nil then |
| 347 |
bashets.schedule(function() |
| 348 |
local data = data_provider() |
| 349 |
util.update_widget(widget, data, format) |
| 350 |
callback(data) |
| 351 |
end, updtime) |
| 352 |
else |
| 353 |
bashets.schedule(function() |
| 354 |
local data = data_provider() |
| 355 |
callback(data) |
| 356 |
end, updtime) |
| 357 |
end |
| 358 |
elseif widget ~= nil then |
| 359 |
bashets.schedule(function() |
| 360 |
local data = data_provider() |
| 361 |
util.update_widget(widget, data, format) |
| 362 |
end, updtime) |
| 363 |
end |
| 364 |
end |
| 365 |
|
| 366 |
|
| 367 |
function bashets.schedule_e(script, widget, callback, format, updtime, sep) |
| 368 |
local ascript = util.fullpath(script) |
| 369 |
local key = util.tmpkey(ascript) |
| 370 |
|
| 371 |
ewidgets[key] = {} |
| 372 |
ewidgets[key].widget = widget |
| 373 |
ewidgets[key].callback = callback |
| 374 |
ewidgets[key].format = format |
| 375 |
ewidgets[key].separator = sep |
| 376 |
ewidgets[key].cmd = util.fullpath(defaults.updater) .. " \"" .. ascript .. "\" " .. key .. " " ..updtime |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
end |
| 381 |
|
| 382 |
function bashets.external_w(raw_data, key) |
| 383 |
|
| 384 |
local callback = ewidgets[key].callback or nil |
| 385 |
local widget = ewidgets[key].widget or nil |
| 386 |
local format = ewidgets[key].format or defaults.format_string |
| 387 |
local data = util.readstring(raw_data, ewidgets[key].separator) |
| 388 |
|
| 389 |
if callback ~= nil and type(callback) == "function" then |
| 390 |
if widget ~= nil then |
| 391 |
util.update_widget(widget, data, format) |
| 392 |
callback(data) |
| 393 |
else |
| 394 |
callback(data) |
| 395 |
end |
| 396 |
elseif widget ~= nil then |
| 397 |
util.update_widget(widget, data, format) |
| 398 |
end |
| 399 |
end |
| 400 |
|
| 401 |
function bashets.register_d(bus, iface, widget, callback, format) |
| 402 |
if capi.dbus then |
| 403 |
bus = bus or "session" |
| 404 |
capi.dbus.add_match(bus, "interface='" .. iface .. "'") |
| 405 |
|
| 406 |
capi.dbus.connect_signal(iface, function (...) |
| 407 |
local argums = {...} |
| 408 |
local data = {} |
| 409 |
for _,v in pairs(argums) do |
| 410 |
if type(v) == "table" then |
| 411 |
for _,iv in pairs(v) do |
| 412 |
table.insert(data, iv) |
| 413 |
end |
| 414 |
else |
| 415 |
table.insert(data, v) |
| 416 |
end |
| 417 |
end |
| 418 |
|
| 419 |
if callback ~= nil and type(callback) == "function" then |
| 420 |
if widget ~= nil then |
| 421 |
util.update_widget(widget, data, format) |
| 422 |
callback(data) |
| 423 |
else |
| 424 |
callback(data) |
| 425 |
end |
| 426 |
elseif widget ~= nil then |
| 427 |
util.update_widget(widget, data, format) |
| 428 |
end |
| 429 |
end) |
| 430 |
else |
| 431 |
error("bashets: You are trying to employ dbus, but no dbus api detected in your build of awesome") |
| 432 |
end |
| 433 |
end |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
function bashets.register(object, options) |
| 454 |
|
| 455 |
options = options or {} |
| 456 |
local updtime = options.update_time or defaults.update_time |
| 457 |
local fltime = options.file_update_time or defaults.file_update_time |
| 458 |
local format = options.format or defaults.format_string |
| 459 |
local sep = options.separator or defaults.separator |
| 460 |
local callback = options.callback |
| 461 |
local async = options.async or false |
| 462 |
local isdbus = options.dbus or false |
| 463 |
local busname = options.busname or "session" |
| 464 |
local readfile = options.read_file or false |
| 465 |
local widget = options.widget |
| 466 |
local external = options.external or false |
| 467 |
|
| 468 |
if type(object) == "function" then |
| 469 |
|
| 470 |
local data = func() |
| 471 |
util.update_widget(widget, data, format) |
| 472 |
|
| 473 |
|
| 474 |
bashets.schedule_w(func, widget, callback, format, updtime) |
| 475 |
|
| 476 |
elseif readfile then |
| 477 |
|
| 478 |
local data = util.readfile(object, sep) |
| 479 |
util.update_widget(widget, data, format) |
| 480 |
|
| 481 |
|
| 482 |
bashets.schedule_w(function() return util.readfile(object, sep) end, widget, callback, format, updtime) |
| 483 |
|
| 484 |
elseif async then |
| 485 |
local script = util.fullpath(object) |
| 486 |
local tmpfile = util.tmpname(script) |
| 487 |
|
| 488 |
|
| 489 |
fl = io.open(tmpfile, "w") |
| 490 |
io.close(fl) |
| 491 |
|
| 492 |
|
| 493 |
util.execfile(script, tmpfile) |
| 494 |
local data = util.readfile(tmpfile, sep) |
| 495 |
util.update_widget(widget, data, format) |
| 496 |
|
| 497 |
|
| 498 |
bashets.schedule(function() util.execfile(script, tmpfile) end, fltime) |
| 499 |
bashets.schedule_w(function() return util.readfile(tmpfile, sep) end, widget, callback, format, updtime) |
| 500 |
|
| 501 |
elseif external then |
| 502 |
local script = util.fullpath(object) |
| 503 |
bashets.schedule_e(script, widget, callback, format, updtime, sep) |
| 504 |
elseif isdbus then |
| 505 |
bashets.register_d(busname, object, widget, callback, format) |
| 506 |
|
| 507 |
else |
| 508 |
local script = util.fullpath(object) |
| 509 |
|
| 510 |
|
| 511 |
local data = util.readshell(script, sep) |
| 512 |
util.update_widget(widget, data, format) |
| 513 |
|
| 514 |
|
| 515 |
bashets.schedule_w(function() return util.readshell(script, sep) end, widget, callback, format, updtime) |
| 516 |
end |
| 517 |
|
| 518 |
end |
| 519 |
|
| 520 |
return bashets |