awesome-client: use readline and no backslash
[awesome.git] / lib / telak.lua.in
blob10988199d0616ea404bfba05792059431b29f11f
1 ----------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
7 -- Grab environment
8 local os = os
9 local io = io
10 local http = require("socket.http")
11 local ltn12 = require("ltn12")
12 local setmetatable = setmetatable
13 local util = require("awful.util")
14 local hooks = require("awful.hooks")
15 local capi =
17 wibox = wibox,
18 widget = widget,
19 image = image
22 --- Root window image display library
23 module("telak")
25 local data = setmetatable({}, { __mode = 'k' })
27 -- Update a telak wibox.
28 -- @param w The wibox to update.
29 local function update(w)
30 local tmp = os.tmpname()
31 http.request{url = data[w].image, sink = ltn12.sink.file(io.open(tmp, "w"))}
32 local img = capi.image(tmp)
33 if img then
34 w:geometry({ width = img.width, height = img.height })
35 w.widgets.image = img
36 else
37 w.visible = false
38 end
39 os.remove(tmp)
40 end
42 --- Create a new telak wibox. This will be automagically update to show a
43 -- local or remote image.
44 -- @param args A table with arguments: image is the local or remote image.
45 -- Timer can be specified to set the time in seconds between two update.
46 -- Default is 300 seconds.
47 -- @return The wibox. You need to attach it to a screen and to set its
48 -- coordinates as you want.
49 local function new(_, args)
50 if not args or not args.image then return end
52 -- Create wibox
53 local w = capi.wibox({ position = "floating" })
54 data[w] = { image = args.image }
55 local wimg = capi.widget({ type = "imagebox" })
56 w.widgets = wimg
58 data[w].cb = function () update(w) end
59 hooks.timer.register(args.timer or 300, data[w].cb)
61 update(w)
63 return w
64 end
66 --- Delete a telak wibox.
67 -- @param w The wibox.
68 function delete(w)
69 if data[w] then
70 hooks.timer.unregister(data[w].cb)
71 data[w] = nil
72 end
73 end
75 setmetatable(_M, { __call = new })
77 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80