]> git.sesse.net Git - vlc/blob - share/lua/intf/modules/common.lua
LUA HTTP Interface: Implementation of missing seeking funtionality when using status...
[vlc] / share / lua / intf / 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     local id = vlc.misc.action_id( arg )
27     if id ~= nil then
28         vlc.var.set( vlc.object.libvlc(), "key-action", id )
29         return true
30     else
31         return false
32     end
33 end
34
35 -- Take a video snapshot
36 function snapshot()
37     local vout = vlc.object.find(nil,"vout","anywhere")
38     if not vout then return end
39     vlc.var.set(vout,"video-snapshot",nil)
40 end
41
42 -- Naive (non recursive) table copy
43 function table_copy(t)
44     c = {}
45     for i,v in pairs(t) do c[i]=v end
46     return c
47 end
48
49 -- strip leading and trailing spaces
50 function strip(str)
51     return string.gsub(str, "^%s*(.-)%s*$", "%1")
52 end
53
54 -- print a table (recursively)
55 function table_print(t,prefix)
56     local prefix = prefix or ""
57     if not t then
58         print(prefix.."/!\\ nil")
59         return
60     end
61     for a,b in pairs_sorted(t) do
62         print(prefix..tostring(a),b)
63         if type(b)==type({}) then
64             table_print(b,prefix.."\t")
65         end
66     end
67 end
68
69 -- print the list of callbacks registered in lua
70 -- useful for debug purposes
71 function print_callbacks()
72     print "callbacks:"
73     table_print(vlc.callbacks)
74 end 
75
76 -- convert a duration (in seconds) to a string
77 function durationtostring(duration)
78     return string.format("%02d:%02d:%02d",
79                          math.floor(duration/3600),
80                          math.floor(duration/60)%60,
81                          math.floor(duration%60))
82 end
83
84 -- realpath
85 function realpath(path)
86     return string.gsub(string.gsub(string.gsub(string.gsub(path,"/%.%./[^/]+","/"),"/[^/]+/%.%./","/"),"/%./","/"),"//","/")
87 end
88
89 -- parse the time from a string and return the seconds
90 -- time format: [+ or -][<int><H or h>:][<int><M or m or '>:][<int><nothing or S or s or ">]
91 function parsetime(timestring)
92     local seconds = 0
93     local hourspattern = "(%d+)[hH]"
94     local minutespattern = "(%d+)[mM']"
95     local secondspattern = "(%d+)[sS\"]?$"
96
97     local _, _, hoursmatch = string.find(timestring, hourspattern)
98     if hoursmatch ~= nil then
99         seconds = seconds + tonumber(hoursmatch) * 3600
100     end
101     local _, _, minutesmatch = string.find(timestring, minutespattern)
102     if minutesmatch ~= nil then
103         seconds = seconds + tonumber(minutesmatch) * 60
104     end
105     local _, _, secondsmatch = string.find(timestring, secondspattern)
106     if secondsmatch ~= nil then
107         seconds = seconds + tonumber(secondsmatch)
108     end
109
110     if string.sub(timestring,1,1) == "-" then
111         seconds = seconds * -1
112     end
113
114     return seconds
115 end
116
117 -- seek
118 function seek(value)
119     local input = vlc.object.input()
120     if input ~= nil and value ~= nil then
121         if string.sub(value,-1) == "%" then
122             local number = tonumber(string.sub(value,1,-2))
123             if number ~= nil then
124                 local posPercent = tonumber( string.sub(value,1,-2))/100.
125                 if string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
126                     vlc.var.set(input,"position",vlc.var.get(input,"position") + posPercent)
127                 else
128                     vlc.var.set(input,"position",posPercent)
129                 end
130             end
131         else
132             local posTime = parsetime(value)
133             if string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
134                 vlc.var.set(input,"time",vlc.var.get(input,"time") + posTime)
135             else
136                 vlc.var.set(input,"time",posTime)
137             end
138         end
139     end
140 end
141
142 function volume(value)
143     if type(value)=="string" and string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
144         vlc.volume.set(vlc.volume.get()+tonumber(value))
145     else
146         vlc.volume.set(tostring(value))
147     end
148 end