Merge pull request #4539 from Matricom/amcodec
[vuplus_xbmc] / xbmc / interfaces / legacy / ModuleXbmc.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 // TODO: Need a uniform way of returning an error status
22
23 #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
24   #include "config.h"
25 #endif
26 #include "network/Network.h"
27
28 #include "ModuleXbmc.h"
29
30 #include "Application.h"
31 #include "ApplicationMessenger.h"
32 #include "utils/URIUtils.h"
33 #include "aojsonrpc.h"
34 #ifndef TARGET_WINDOWS
35 #include "XTimeUtils.h"
36 #endif
37 #include "guilib/LocalizeStrings.h"
38 #include "GUIInfoManager.h"
39 #include "guilib/GUIAudioManager.h"
40 #include "guilib/GUIWindowManager.h"
41 #include "filesystem/File.h"
42 #include "filesystem/SpecialProtocol.h"
43 #include "utils/Crc32.h"
44 #include "FileItem.h"
45 #include "LangInfo.h"
46 #include "settings/AdvancedSettings.h"
47 #include "settings/Settings.h"
48 #include "guilib/TextureManager.h"
49 #include "Util.h"
50 #include "URL.h"
51 #include "cores/AudioEngine/AEFactory.h"
52 #include "storage/MediaManager.h"
53 #include "utils/FileUtils.h"
54 #include "utils/LangCodeExpander.h"
55 #include "utils/StringUtils.h"
56 #include "CallbackHandler.h"
57 #include "AddonUtils.h"
58
59 #include "LanguageHook.h"
60
61 #include "cores/VideoRenderers/RenderCapture.h"
62
63 #include "threads/SystemClock.h"
64 #include "Exception.h"
65 #include <vector>
66
67 namespace XBMCAddon
68 {
69
70   namespace xbmc
71   {
72     /*****************************************************************
73      * start of xbmc methods
74      *****************************************************************/
75     void log(const char* msg, int level)
76     {
77       // check for a valid loglevel
78       if (level < LOGDEBUG || level > LOGNONE)
79         level = LOGNOTICE;
80       CLog::Log(level, "%s", msg);
81     }
82
83     void shutdown()
84     {
85       XBMC_TRACE;
86       ThreadMessage tMsg = {TMSG_SHUTDOWN};
87       CApplicationMessenger::Get().SendMessage(tMsg);
88     }
89
90     void restart()
91     {
92       XBMC_TRACE;
93       ThreadMessage tMsg = {TMSG_RESTART};
94       CApplicationMessenger::Get().SendMessage(tMsg);
95     }
96
97     void executescript(const char* script)
98     {
99       XBMC_TRACE;
100       if (! script)
101         return;
102
103       ThreadMessage tMsg = {TMSG_EXECUTE_SCRIPT};
104       tMsg.strParam = script;
105       CApplicationMessenger::Get().SendMessage(tMsg);
106     }
107
108     void executebuiltin(const char* function, bool wait /* = false*/)
109     {
110       XBMC_TRACE;
111       if (! function)
112         return;
113       CApplicationMessenger::Get().ExecBuiltIn(function,wait);
114     }
115
116     String executehttpapi(const char* httpcommand) 
117     {
118       XBMC_TRACE;
119       THROW_UNIMP("executehttpapi");
120     }
121
122     String executeJSONRPC(const char* jsonrpccommand)
123     {
124       XBMC_TRACE;
125       DelayedCallGuard dg;
126 #ifdef HAS_JSONRPC
127       String ret;
128
129       if (! jsonrpccommand)
130         return ret;
131
132       //    String method = jsonrpccommand;
133
134       CAddOnTransport transport;
135       CAddOnTransport::CAddOnClient client;
136
137       return JSONRPC::CJSONRPC::MethodCall(/*method*/ jsonrpccommand, &transport, &client);
138 #else
139       THROW_UNIMP("executeJSONRPC");
140 #endif
141     }
142
143     void sleep(long timemillis)
144     {
145       XBMC_TRACE;
146
147       XbmcThreads::EndTime endTime(timemillis);
148       while (!endTime.IsTimePast())
149       {
150         LanguageHook* lh = NULL;
151         {
152           DelayedCallGuard dcguard;
153           lh = dcguard.getLanguageHook(); // borrow this
154           long nextSleep = endTime.MillisLeft();
155           if (nextSleep > 100)
156             nextSleep = 100; // only sleep for 100 millis
157           ::Sleep(nextSleep);
158         }
159         if (lh != NULL)
160           lh->MakePendingCalls();
161       }
162     }
163
164     String getLocalizedString(int id)
165     {
166       XBMC_TRACE;
167       String label;
168       if (id >= 30000 && id <= 30999)
169         label = g_localizeStringsTemp.Get(id);
170       else if (id >= 32000 && id <= 32999)
171         label = g_localizeStringsTemp.Get(id);
172       else
173         label = g_localizeStrings.Get(id);
174
175       return label;
176     }
177
178     String getSkinDir()
179     {
180       XBMC_TRACE;
181       return CSettings::Get().GetString("lookandfeel.skin");
182     }
183
184     String getLanguage(int format /* = CLangCodeExpander::ENGLISH_NAME */, bool region /*= false*/)
185     {
186       XBMC_TRACE;
187       CStdString lang = CSettings::Get().GetString("locale.language");
188
189       switch (format)
190       {
191       case CLangCodeExpander::ENGLISH_NAME:
192         {
193           if (region)
194           {
195             CStdString region = "-" + g_langInfo.GetCurrentRegion();
196             return (lang += region);
197           }
198           return lang;
199         }
200       case CLangCodeExpander::ISO_639_1:
201         {
202           CStdString langCode;
203           g_LangCodeExpander.ConvertToTwoCharCode(langCode, lang);
204           if (region)
205           {
206             CStdString region = g_langInfo.GetRegionLocale();
207             CStdString region2Code;
208             g_LangCodeExpander.ConvertToTwoCharCode(region2Code, region);
209             region2Code = "-" + region2Code;
210             return (langCode += region2Code);
211           }
212           return langCode;
213         }
214       case CLangCodeExpander::ISO_639_2:
215         {
216           CStdString langCode;
217           g_LangCodeExpander.ConvertToThreeCharCode(langCode, lang);
218           if (region)
219           {
220             CStdString region = g_langInfo.GetRegionLocale();
221             CStdString region3Code;
222             g_LangCodeExpander.ConvertToThreeCharCode(region3Code, region);
223             region3Code = "-" + region3Code;
224             return (langCode += region3Code);
225           }
226
227           return langCode;
228         }
229       default:
230         return "";
231       }
232     }
233
234     String getIPAddress()
235     {
236       XBMC_TRACE;
237       char cTitleIP[32];
238       sprintf(cTitleIP, "127.0.0.1");
239       CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
240       if (iface)
241         return iface->GetCurrentIPAddress();
242
243       return cTitleIP;
244     }
245
246     long getDVDState()
247     {
248       XBMC_TRACE;
249       return g_mediaManager.GetDriveStatus();
250     }
251
252     long getFreeMem()
253     {
254       XBMC_TRACE;
255       MEMORYSTATUSEX stat;
256       stat.dwLength = sizeof(MEMORYSTATUSEX);
257       GlobalMemoryStatusEx(&stat);
258       return (long)(stat.ullAvailPhys  / ( 1024 * 1024 ));
259     }
260
261     // getCpuTemp() method
262     // ## Doesn't work right, use getInfoLabel('System.CPUTemperature') instead.
263     /*PyDoc_STRVAR(getCpuTemp__doc__,
264       "getCpuTemp() -- Returns the current cpu temperature as an integer."
265       ""
266       "example:"
267       "  - cputemp = xbmc.getCpuTemp()");
268
269       PyObject* XBMC_GetCpuTemp(PyObject *self, PyObject *args)
270       {
271       unsigned short cputemp;
272       unsigned short cpudec;
273
274       _outp(0xc004, (0x4c<<1)|0x01);
275       _outp(0xc008, 0x01);
276       _outpw(0xc000, _inpw(0xc000));
277       _outp(0xc002, (0) ? 0x0b : 0x0a);
278       while ((_inp(0xc000) & 8));
279       cputemp = _inpw(0xc006);
280
281       _outp(0xc004, (0x4c<<1)|0x01);
282       _outp(0xc008, 0x10);
283       _outpw(0xc000, _inpw(0xc000));
284       _outp(0xc002, (0) ? 0x0b : 0x0a);
285       while ((_inp(0xc000) & 8));
286       cpudec = _inpw(0xc006);
287
288       if (cpudec<10) cpudec = cpudec * 100;
289       if (cpudec<100) cpudec = cpudec *10;
290
291       return PyInt_FromLong((long)(cputemp + cpudec / 1000.0f));
292       }*/
293
294     String getInfoLabel(const char* cLine)
295     {
296       XBMC_TRACE;
297       if (!cLine)
298       {
299         String ret;
300         return ret;
301       }
302
303       int ret = g_infoManager.TranslateString(cLine);
304       //doesn't seem to be a single InfoTag?
305       //try full blown GuiInfoLabel then
306       if (ret == 0)
307       {
308         CGUIInfoLabel label(cLine);
309         return label.GetLabel(0);
310       }
311       else
312         return g_infoManager.GetLabel(ret);
313     }
314
315     String getInfoImage(const char * infotag)
316     {
317       XBMC_TRACE;
318       if (!infotag)
319         {
320           String ret;
321           return ret;
322         }
323
324       int ret = g_infoManager.TranslateString(infotag);
325       return g_infoManager.GetImage(ret, WINDOW_INVALID);
326     }
327
328     void playSFX(const char* filename, bool useCached)
329     {
330       XBMC_TRACE;
331       if (!filename)
332         return;
333
334       if (XFILE::CFile::Exists(filename))
335       {
336         g_audioManager.PlayPythonSound(filename,useCached);
337       }
338     }
339
340     void stopSFX()
341     {
342       XBMC_TRACE;
343       DelayedCallGuard dg;
344       g_audioManager.Stop();
345     }
346     
347     void enableNavSounds(bool yesNo)
348     {
349       XBMC_TRACE;
350       g_audioManager.Enable(yesNo);
351     }
352
353     bool getCondVisibility(const char *condition)
354     {
355       XBMC_TRACE;
356       if (!condition)
357         return false;
358
359       bool ret;
360       {
361         LOCKGUI;
362
363         int id = g_windowManager.GetTopMostModalDialogID();
364         if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
365         ret = g_infoManager.EvaluateBool(condition,id);
366       }
367
368       return ret;
369     }
370
371     int getGlobalIdleTime()
372     {
373       XBMC_TRACE;
374       return g_application.GlobalIdleTime();
375     }
376
377     String getCacheThumbName(const String& path)
378     {
379       XBMC_TRACE;
380       Crc32 crc;
381       crc.ComputeFromLowerCase(path);
382       return StringUtils::Format("%08x.tbn", (unsigned __int32)crc);;
383     }
384
385     String makeLegalFilename(const String& filename, bool fatX)
386     {
387       XBMC_TRACE;
388       return CUtil::MakeLegalPath(filename);
389     }
390
391     String translatePath(const String& path)
392     {
393       XBMC_TRACE;
394       return CSpecialProtocol::TranslatePath(path);
395     }
396
397     Tuple<String,String> getCleanMovieTitle(const String& path, bool usefoldername)
398     {
399       XBMC_TRACE;
400       CFileItem item(path, false);
401       CStdString strName = item.GetMovieName(usefoldername);
402
403       CStdString strTitleAndYear;
404       CStdString strTitle;
405       CStdString strYear;
406       CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, usefoldername);
407       return Tuple<String,String>(strTitle,strYear);
408     }
409
410     String validatePath(const String& path)
411     {
412       XBMC_TRACE;
413       return CUtil::ValidatePath(path, true);
414     }
415
416     String getRegion(const char* id)
417     {
418       XBMC_TRACE;
419       CStdString result;
420
421       if (strcmpi(id, "datelong") == 0)
422         {
423           result = g_langInfo.GetDateFormat(true);
424           StringUtils::Replace(result, "DDDD", "%A");
425           StringUtils::Replace(result, "MMMM", "%B");
426           StringUtils::Replace(result, "D", "%d");
427           StringUtils::Replace(result, "YYYY", "%Y");
428         }
429       else if (strcmpi(id, "dateshort") == 0)
430         {
431           result = g_langInfo.GetDateFormat(false);
432           StringUtils::Replace(result, "MM", "%m");
433           StringUtils::Replace(result, "DD", "%d");
434           StringUtils::Replace(result, "YYYY", "%Y");
435         }
436       else if (strcmpi(id, "tempunit") == 0)
437         result = g_langInfo.GetTempUnitString();
438       else if (strcmpi(id, "speedunit") == 0)
439         result = g_langInfo.GetSpeedUnitString();
440       else if (strcmpi(id, "time") == 0)
441         {
442           result = g_langInfo.GetTimeFormat();
443           StringUtils::Replace(result, "H", "%H");
444           StringUtils::Replace(result, "h", "%I");
445           StringUtils::Replace(result, "mm", "%M");
446           StringUtils::Replace(result, "ss", "%S");
447           StringUtils::Replace(result, "xx", "%p");
448         }
449       else if (strcmpi(id, "meridiem") == 0)
450         result = StringUtils::Format("%s/%s",
451                                      g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_AM).c_str(),
452                                      g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_PM).c_str());
453
454       return result;
455     }
456
457     // TODO: Add a mediaType enum
458     String getSupportedMedia(const char* mediaType)
459     {
460       XBMC_TRACE;
461       String result;
462       if (strcmpi(mediaType, "video") == 0)
463         result = g_advancedSettings.m_videoExtensions;
464       else if (strcmpi(mediaType, "music") == 0)
465         result = g_advancedSettings.m_musicExtensions;
466       else if (strcmpi(mediaType, "picture") == 0)
467         result = g_advancedSettings.m_pictureExtensions;
468
469       // TODO:
470       //    else
471       //      return an error
472
473       return result;
474     }
475
476     bool skinHasImage(const char* image)
477     {
478       XBMC_TRACE;
479       return g_TextureManager.HasTexture(image);
480     }
481
482
483     bool startServer(int iTyp, bool bStart, bool bWait)
484     {
485       XBMC_TRACE;
486       DelayedCallGuard dg;
487       return g_application.StartServer((CApplication::ESERVERS)iTyp, bStart != 0, bWait != 0);
488     }
489
490     void audioSuspend()
491     {  
492       CAEFactory::Suspend();
493     }
494
495     void audioResume()
496     { 
497       CAEFactory::Resume();
498     }
499
500     String convertLanguage(const char* language, int format)
501     {
502       CStdString convertedLanguage;
503       switch (format)
504       {
505       case CLangCodeExpander::ENGLISH_NAME:
506         {
507           g_LangCodeExpander.Lookup(convertedLanguage, language);
508           // maybe it's a check whether the language exists or not
509           if (convertedLanguage.empty())
510           {
511             g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
512             g_LangCodeExpander.Lookup(convertedLanguage, convertedLanguage);
513           }
514           break;
515         }
516       case CLangCodeExpander::ISO_639_1:
517         g_LangCodeExpander.ConvertToTwoCharCode(convertedLanguage, language);
518         break;
519       case CLangCodeExpander::ISO_639_2:
520         g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
521         break;
522       default:
523         return "";
524       }
525       return convertedLanguage;
526     }
527
528     int getSERVER_WEBSERVER() { return CApplication::ES_WEBSERVER; }
529     int getSERVER_AIRPLAYSERVER() { return CApplication::ES_AIRPLAYSERVER; }
530     int getSERVER_UPNPSERVER() { return CApplication::ES_UPNPSERVER; }
531     int getSERVER_UPNPRENDERER() { return CApplication::ES_UPNPRENDERER; }
532     int getSERVER_EVENTSERVER() { return CApplication::ES_EVENTSERVER; }
533     int getSERVER_JSONRPCSERVER() { return CApplication::ES_JSONRPCSERVER; }
534     int getSERVER_ZEROCONF() { return CApplication::ES_ZEROCONF; }
535
536     int getPLAYLIST_MUSIC() { return PLAYLIST_MUSIC; }
537     int getPLAYLIST_VIDEO() { return PLAYLIST_VIDEO; }
538     int getPLAYER_CORE_AUTO() { return EPC_NONE; }
539     int getPLAYER_CORE_DVDPLAYER() { return EPC_DVDPLAYER; }
540     int getPLAYER_CORE_MPLAYER() { return EPC_MPLAYER; }
541     int getPLAYER_CORE_PAPLAYER() { return EPC_PAPLAYER; }
542     int getTRAY_OPEN() { return TRAY_OPEN; }
543     int getDRIVE_NOT_READY() { return DRIVE_NOT_READY; }
544     int getTRAY_CLOSED_NO_MEDIA() { return TRAY_CLOSED_NO_MEDIA; }
545     int getTRAY_CLOSED_MEDIA_PRESENT() { return TRAY_CLOSED_MEDIA_PRESENT; }
546     int getLOGDEBUG() { return LOGDEBUG; }
547     int getLOGINFO() { return LOGINFO; }
548     int getLOGNOTICE() { return LOGNOTICE; }
549     int getLOGWARNING() { return LOGWARNING; }
550     int getLOGERROR() { return LOGERROR; }
551     int getLOGSEVERE() { return LOGSEVERE; }
552     int getLOGFATAL() { return LOGFATAL; }
553     int getLOGNONE() { return LOGNONE; }
554
555     // render capture user states
556     int getCAPTURE_STATE_WORKING() { return CAPTURESTATE_WORKING; }
557     int getCAPTURE_STATE_DONE(){ return CAPTURESTATE_DONE; }
558     int getCAPTURE_STATE_FAILED() { return CAPTURESTATE_FAILED; }
559
560     // render capture flags
561     int getCAPTURE_FLAG_CONTINUOUS() { return (int)CAPTUREFLAG_CONTINUOUS; }
562     int getCAPTURE_FLAG_IMMEDIATELY() { return (int)CAPTUREFLAG_IMMEDIATELY; }
563
564     // language string formats
565     int getISO_639_1() { return CLangCodeExpander::ISO_639_1; } 
566     int getISO_639_2(){ return CLangCodeExpander::ISO_639_2; }
567     int getENGLISH_NAME() { return CLangCodeExpander::ENGLISH_NAME; }
568
569     const int lLOGNOTICE = LOGNOTICE;
570   }
571 }