]> git.sesse.net Git - vlc/blob - share/lua/intf/modules/httprequests.lua
Lua: httprequests.lua -- formatting fixes
[vlc] / share / lua / intf / modules / httprequests.lua
1 --[==========================================================================[
2  httprequests.lua: code for processing httprequests commands and output
3 --[==========================================================================[
4  Copyright (C) 2007 the VideoLAN team
5  $Id$
6
7  Authors: Antoine Cellerier <dionoea at videolan dot org>
8  Rob Jonson <rob at hobbyistsoftware.com>
9
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 --]==========================================================================]
24
25 module("httprequests",package.seeall)
26
27
28 --input utilities
29
30 local function stripslashes(s)
31   return string.gsub(s,"\\(.)","%1")
32 end
33
34 --Round the number to the specified precision
35 function round(what, precision)
36   if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
37 end
38
39 --split text where it matches the delimiter
40 function strsplit(text, delimiter)
41         local strfind = string.find
42         local strsub = string.sub
43         local tinsert = table.insert
44         local list = {}
45         local pos = 1
46         if strfind("", delimiter, 1) then -- this would result in endless loops
47                 error("delimiter matches empty string!")
48         end
49         local i=1
50         while 1 do
51                 i=i+1
52                 local first, last = strfind(text, delimiter, pos)
53                 if first then -- found?
54                         tinsert(list,i, strsub(text, pos, first-1))
55                         pos = last+1
56                 else
57                         tinsert(list,i, strsub(text, pos))
58                 break
59                 end
60         end
61         return list
62 end
63
64 function round(what, precision)
65 if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
66 end
67
68 --main function to process commands sent with the request
69
70 processcommands = function ()
71
72         local input = _GET['input']
73         local command = _GET['command']
74         local id = tonumber(_GET['id'] or -1)
75         local val = _GET['val']
76         local options = _GET['option']
77         local band = _GET['band']
78         if type(options) ~= "table" then -- Deal with the 0 or 1 option case
79           options = { options }
80         end
81
82         if command == "in_play" then
83           --[[
84           vlc.msg.err( "<options>" )
85           for a,b in ipairs(options) do
86                 vlc.msg.err(b)
87           end
88           vlc.msg.err( "</options>" )
89           --]]
90           vlc.playlist.add({{path=stripslashes(input),options=options}})
91         elseif command == "in_enqueue" then
92           vlc.playlist.enqueue({{path=stripslashes(input),options=options}})
93         elseif command == "pl_play" then
94           if id == -1 then
95                 vlc.playlist.play()
96           else
97                 vlc.playlist.goto(id)
98           end
99         elseif command == "pl_pause" then
100           if vlc.playlist.status() == "stopped" then
101                 if id == -1 then
102                   vlc.playlist.play()
103                 else
104                   vlc.playlist.goto(id)
105                 end
106           else
107                 vlc.playlist.pause()
108           end
109         elseif command == "pl_forcepause" then
110           if vlc.playlist.status() == "playing" then
111                 vlc.playlist.pause()
112           end
113         elseif command == "pl_forceresume" then
114           if vlc.playlist.status() == "paused" then
115                 vlc.playlist.pause()
116           end
117         elseif command == "pl_stop" then
118           vlc.playlist.stop()
119         elseif command == "pl_next" then
120           vlc.playlist.next()
121         elseif command == "pl_previous" then
122           vlc.playlist.prev()
123         elseif command == "pl_delete" then
124           vlc.playlist.delete(id)
125         elseif command == "pl_empty" then
126           vlc.playlist.clear()
127         elseif command == "pl_sort" then
128           vlc.playlist.sort( val, id > 0 )
129         elseif command == "pl_random" then
130           vlc.playlist.random()
131         elseif command == "pl_loop" then
132           vlc.playlist.loop()
133         elseif command == "pl_repeat" then
134           vlc.playlist.repeat_()
135         elseif command == "pl_sd" then
136           if vlc.sd.is_loaded(val) then
137                 vlc.sd.remove(val)
138           else
139                 vlc.sd.add(val)
140           end
141         elseif command == "fullscreen" then
142           vlc.video.fullscreen()
143         elseif command == "snapshot" then
144           common.snapshot()
145         elseif command == "volume" then
146           common.volume(val)
147         elseif command == "seek" then
148           common.seek(val)
149         elseif command == "key" then
150           common.hotkey("key-"..val)
151         elseif command == "audiodelay" then
152           if vlc.object.input() and val then
153            vlc.var.set(vlc.object.input(),"audio-delay",val)
154           end
155         elseif command == "rate" then
156           if vlc.object.input() and tonumber(val) >= 0 then
157            vlc.var.set(vlc.object.input(),"rate",val)
158           end
159         elseif command == "subdelay" then
160           if vlc.object.input() then
161            vlc.var.set(vlc.object.input(),"spu-delay",val)
162           end
163         elseif command == "aspectratio" then
164           if vlc.object.vout() then
165            vlc.var.set(vlc.object.vout(),"aspect-ratio",val)
166           end
167         elseif command == "preamp" then
168           vlc.equalizer.preampset(val)
169         elseif command == "equalizer" then
170           vlc.equalizer.equalizerset(band,val)
171         elseif command == "enableeq" then
172           if val == '0' then vlc.equalizer.enable(false) else vlc.equalizer.enable(true) end
173         end
174
175         local input = nil
176         local command = nil
177         local id = nil
178         local val = nil
179
180 end
181
182 --utilities for formatting output
183
184 local function xmlString(s)
185   if (type(s)=="string") then
186         return vlc.strings.convert_xml_special_chars(s)
187   else
188         return tostring(s)
189   end
190 end
191
192 local printJsonKeyValue = function (k,v,indent)
193         print("\n")
194         for i=1,indent do print(" ") end
195         if (k) then
196                 print("\""..k.."\":")
197         end
198
199         if (type(v)=="number") then
200                 print(xmlString(v))
201         elseif (type(v)=="table") then
202                  if (v._array==NULL) then
203                 print("{\n")
204                 printTableAsJson(v,indent+2)
205                 print("\n}")
206           else
207                 print("[")
208                 printArrayAsJson(v._array,indent+2)
209                 print("\n]")
210           end
211         else
212         print("\""..xmlString(v).."\"")
213     end
214 end
215
216
217 printArrayAsJson = function(array,indent)
218         first=true
219         for i,v in ipairs(array) do
220                 if not first then print(",") end
221                 printJsonKeyValue(NULL,v,indent)
222                 first=false
223         end
224 end
225
226 printTableAsJson = function (dict,indent)
227         first=true
228         for k,v in pairs(dict) do
229                 if not first then print(",") end
230                 printJsonKeyValue(k,v,indent)
231                 first=false
232     end
233 end
234
235 local printXmlKeyValue = function (k,v,indent)
236         print("\n")
237         for i=1,indent do print(" ") end
238         if (k) then
239                 print("<"..k..">")
240         end
241
242         if (type(v)=="table") then
243                 printTableAsXml(v,indent+2)
244         else
245         print(xmlString(v))
246     end
247
248     if (k) then
249                 print("</"..k..">")
250         end
251 end
252
253 printTableAsXml = function (dict,indent)
254         for k,v in pairs(dict) do
255         printXmlKeyValue(k,v,indent)
256     end
257 end
258
259 --[[
260 function logTable(t,pre)
261   local pre = pre or ""
262   for k,v in pairs(t) do
263     vlc.msg.err(pre..tostring(k).." : "..tostring(v))
264     if type(v) == "table" then
265       a(v,pre.."  ")
266     end
267   end
268 end
269 --]]
270
271 --main accessors
272
273 getplaylist = function ()
274         local p
275
276         if _GET["search"] then
277           if _GET["search"] ~= "" then
278                 _G.search_key = _GET["search"]
279           else
280                 _G.search_key = nil
281           end
282           local key = vlc.strings.decode_uri(_GET["search"])
283           p = vlc.playlist.search(key)
284         else
285           p = vlc.playlist.get()
286         end
287
288         --logTable(p) --Uncomment to debug
289
290         return p
291 end
292
293 parseplaylist = function (item)
294         if item.flags.disabled then return end
295
296         if (item.children) then
297                 local result={}
298                 local name = vlc.strings.convert_xml_special_chars(item.name or "")
299
300                 result["type"]="node"
301                 result.id=tostring(item.id)
302                 result.name=tostring(name)
303                 result.ro=item.flags.ro and "ro" or "rw"
304
305                 --store children in an array
306                 --we use _array as a proxy for arrays
307                 result.children={}
308                 result.children._array={}
309
310                 for _, child in ipairs(item.children) do
311                         local nextChild=parseplaylist(child)
312             table.insert(result.children._array,nextChild)
313         end
314
315                 return result
316         else
317                 local result={}
318                 local name, path = vlc.strings.convert_xml_special_chars(item.name or "", item.path or "")
319                 local current_item = vlc.input.item()
320
321                 -- Is the item the one currently played
322                 if(current_item ~= nil) then
323             if(vlc.input.item().uri(current_item) == path) then
324                 result.current = "current"
325             end
326         end
327
328                 result["type"]="leaf"
329                 result.id=tostring(item.id)
330                 result.uri=tostring(path)
331                 result.name=name
332                 result.ro=item.flags.ro and "ro" or "rw"
333                 result.duration=math.floor(item.duration)
334
335                 return result
336         end
337
338 end
339
340 playlisttable = function ()
341
342         local basePlaylist=getplaylist()
343
344         return parseplaylist(basePlaylist)
345 end
346
347
348 getstatus = function (includecategories)
349
350
351 local input = vlc.object.input()
352 local item = vlc.input.item()
353 local playlist = vlc.object.playlist()
354 local vout = vlc.object.vout()
355 local aout = vlc.object.aout()
356
357         local s ={}
358
359         --update api version when new data/commands added
360         s.apiversion=1
361         s.version=vlc.misc.version()
362         s.volume=vlc.volume.get()
363
364         if input then
365                 s.length=math.floor(vlc.var.get(input,"length"))
366                 s.time=math.floor(vlc.var.get(input,"time"))
367                 s.position=vlc.var.get(input,"position")
368                 s.audiodelay=vlc.var.get(input,"audio-delay")
369                 s.rate=vlc.var.get(input,"rate")
370                 s.subtitledelay=vlc.var.get(input,"spu-delay")
371         else
372                 s.length=0
373                 s.time=0
374                 s.position=0
375                 s.audiodelay=0
376                 s.rate=1
377                 s.subtitledelay=0
378         end
379
380         if vout then
381                 s.fullscreen=vlc.var.get(vout,"fullscreen")
382                 s.aspectratio=vlc.var.get(vout,"aspect-ratio");
383                 if s.aspectratio=="" then s.aspectratio = "default" end
384         else
385                 s.fullscreen=0
386         end
387
388         if aout then
389                 local filters=vlc.var.get(aout,"audio-filter")
390                 local temp=strsplit(filters,":")
391                 s.audiofilters={}
392                 local id=0
393                 for i,j in pairs(temp) do
394                         s.audiofilters['filter_'..id]=j
395                         id=id+1
396                 end
397         end
398
399         s.videoeffects={}
400         s.videoeffects.hue=round(vlc.config.get("hue"),2)
401         s.videoeffects.brightness=round(vlc.config.get("brightness"),2)
402         s.videoeffects.contrast=round(vlc.config.get("contrast"),2)
403         s.videoeffects.saturation=round(vlc.config.get("saturation"),2)
404         s.videoeffects.gamma=round(vlc.config.get("gamma"),2)
405
406         s.state=vlc.playlist.status()
407         s.random=vlc.var.get(playlist,"random")
408         s.loop=vlc.var.get(playlist,"loop")
409         s["repeat"]=vlc.var.get(playlist,"repeat")
410
411         s.equalizer={}
412                 s.equalizer.preamp=round(vlc.equalizer.preampget(),2)
413                 s.equalizer.bands=vlc.equalizer.equalizerget()
414                 if s.equalizer.bands ~= null then
415                         for k,i in pairs(s.equalizer.bands) do s.equalizer.bands[k]=round(i,2) end
416                         s.equalizer.presets=vlc.equalizer.presets()
417                 end
418
419         if (includecategories and item) then
420                 s.information={}
421                 s.information.category={}
422                 s.information.category.meta=item:metas()
423
424                 local info = item:info()
425                 for k, v in pairs(info) do
426                         local streamTable={}
427                         for k2, v2 in pairs(v) do
428                                 local tag = string.gsub(k2," ","_")
429                                 streamTable[xmlString(tag)]=xmlString(v2)
430                         end
431
432                         s.information.category[xmlString(k)]=streamTable
433                 end
434
435                 s.stats={}
436
437                 local statsdata = item:stats()
438         for k,v in pairs(statsdata) do
439                 local tag = string.gsub(k,"_","")
440         s.stats[tag]=xmlString(v)
441       end
442         end
443         return s
444 end
445