]> git.sesse.net Git - vlc/blob - share/luaintf/modules/common.lua
Port the http interface to the Lua Interface Module framework.
[vlc] / share / luaintf / modules / common.lua
1 --[[ This code is public domain (since it really isn't very interesting) ]]--
2
3 module("common",package.seeall)
4
5 -- Iterate over a table in the keys' alphabetical order
6 function pairs_sorted(t)
7     local s = {}
8     for k,_ in pairs(t) do table.insert(s,k) end
9     table.sort(s)
10     local i = 0
11     return function () i = i + 1; return s[i], t[s[i]] end
12 end
13
14 -- Return a function such as skip(foo)(a,b,c) = foo(b,c)
15 function skip(foo)
16     return function(discard,...) return foo(...) end
17 end
18
19 -- Return a function such as setarg(foo,a)(b,c) = foo(a,b,c)
20 function setarg(foo,a)
21     return function(...) return foo(a,...) end
22 end
23
24 -- Trigger a hotkey
25 function hotkey(arg)
26     vlc.var.set( vlc.object.libvlc(), "key-pressed", vlc.config.get( arg ) )
27 end
28
29 -- Take a video snapshot
30 function snapshot()
31     local vout = vlc.object.find(nil,"vout","anywhere")
32     if not vout then return end
33     vlc.var.set(vout,"video-snapshot",nil)
34 end
35
36 -- Naive (non recursive) table copy
37 function table_copy(t)
38     c = {}
39     for i,v in pairs(t) do c[i]=v end
40     return c
41 end
42
43 -- strip leading and trailing spaces
44 function strip(str)
45     return string.gsub(str, "^%s*(.-)%s*$", "%1")
46 end
47
48 -- print a table (recursively)
49 function table_print(t,prefix)
50     local prefix = prefix or ""
51     for a,b in pairs_sorted(t) do
52         print(prefix..tostring(a),b)
53         if type(b)==type({}) then
54             table_print(b,prefix.."\t")
55         end
56     end
57 end
58
59 -- print the list of callbacks registered in lua
60 -- usefull for debug purposes
61 function print_callbacks()
62     print "callbacks:"
63     table_print(vlc.callbacks)
64 end 
65
66 -- convert a duration (in seconds) to a string
67 function durationtostring(duration)
68     return string.format("%02d:%02d:%02d",
69                          math.floor(duration/3600),
70                          math.floor(duration/60)%60,
71                          math.floor(duration%60))
72 end
73
74 -- realpath
75 function realpath(path)
76     return string.gsub(string.gsub(string.gsub(string.gsub(path,"/%.%./[^/]+","/"),"/[^/]+/%.%./","/"),"/%./","/"),"//","/")
77 end
78
79 -- seek
80 function seek(value)
81     local input = vlc.object.input()
82     if string.sub(value,#value)=="%" then
83         vlc.var.set(input,"position",tonumber(string.sub(value,1,#value-1))/100.)
84     else
85         vlc.var.set(input,"time",tonumber(value))
86     end
87 end