2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
21 // TODO: Need a uniform way of returning an error status
23 #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
26 #include "network/Network.h"
28 #include "ModuleXbmc.h"
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"
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"
46 #include "settings/AdvancedSettings.h"
47 #include "settings/Settings.h"
48 #include "guilib/TextureManager.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"
59 #include "LanguageHook.h"
61 #include "cores/VideoRenderers/RenderCapture.h"
63 #include "threads/SystemClock.h"
64 #include "Exception.h"
72 /*****************************************************************
73 * start of xbmc methods
74 *****************************************************************/
75 void log(const char* msg, int level)
77 // check for a valid loglevel
78 if (level < LOGDEBUG || level > LOGNONE)
80 CLog::Log(level, "%s", msg);
86 ThreadMessage tMsg = {TMSG_SHUTDOWN};
87 CApplicationMessenger::Get().SendMessage(tMsg);
93 ThreadMessage tMsg = {TMSG_RESTART};
94 CApplicationMessenger::Get().SendMessage(tMsg);
97 void executescript(const char* script)
103 ThreadMessage tMsg = {TMSG_EXECUTE_SCRIPT};
104 tMsg.strParam = script;
105 CApplicationMessenger::Get().SendMessage(tMsg);
108 void executebuiltin(const char* function, bool wait /* = false*/)
113 CApplicationMessenger::Get().ExecBuiltIn(function,wait);
116 String executehttpapi(const char* httpcommand)
119 THROW_UNIMP("executehttpapi");
122 String executeJSONRPC(const char* jsonrpccommand)
129 if (! jsonrpccommand)
132 // String method = jsonrpccommand;
134 CAddOnTransport transport;
135 CAddOnTransport::CAddOnClient client;
137 return JSONRPC::CJSONRPC::MethodCall(/*method*/ jsonrpccommand, &transport, &client);
139 THROW_UNIMP("executeJSONRPC");
143 void sleep(long timemillis)
147 XbmcThreads::EndTime endTime(timemillis);
148 while (!endTime.IsTimePast())
150 LanguageHook* lh = NULL;
152 DelayedCallGuard dcguard;
153 lh = dcguard.getLanguageHook(); // borrow this
154 long nextSleep = endTime.MillisLeft();
156 nextSleep = 100; // only sleep for 100 millis
160 lh->MakePendingCalls();
164 String getLocalizedString(int id)
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);
173 label = g_localizeStrings.Get(id);
181 return CSettings::Get().GetString("lookandfeel.skin");
184 String getLanguage(int format /* = CLangCodeExpander::ENGLISH_NAME */, bool region /*= false*/)
187 CStdString lang = CSettings::Get().GetString("locale.language");
191 case CLangCodeExpander::ENGLISH_NAME:
195 CStdString region = "-" + g_langInfo.GetCurrentRegion();
196 return (lang += region);
200 case CLangCodeExpander::ISO_639_1:
203 g_LangCodeExpander.ConvertToTwoCharCode(langCode, lang);
206 CStdString region = g_langInfo.GetRegionLocale();
207 CStdString region2Code;
208 g_LangCodeExpander.ConvertToTwoCharCode(region2Code, region);
209 region2Code = "-" + region2Code;
210 return (langCode += region2Code);
214 case CLangCodeExpander::ISO_639_2:
217 g_LangCodeExpander.ConvertToThreeCharCode(langCode, lang);
220 CStdString region = g_langInfo.GetRegionLocale();
221 CStdString region3Code;
222 g_LangCodeExpander.ConvertToThreeCharCode(region3Code, region);
223 region3Code = "-" + region3Code;
224 return (langCode += region3Code);
234 String getIPAddress()
238 sprintf(cTitleIP, "127.0.0.1");
239 CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
241 return iface->GetCurrentIPAddress();
249 return g_mediaManager.GetDriveStatus();
256 stat.dwLength = sizeof(MEMORYSTATUSEX);
257 GlobalMemoryStatusEx(&stat);
258 return (long)(stat.ullAvailPhys / ( 1024 * 1024 ));
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."
267 " - cputemp = xbmc.getCpuTemp()");
269 PyObject* XBMC_GetCpuTemp(PyObject *self, PyObject *args)
271 unsigned short cputemp;
272 unsigned short cpudec;
274 _outp(0xc004, (0x4c<<1)|0x01);
276 _outpw(0xc000, _inpw(0xc000));
277 _outp(0xc002, (0) ? 0x0b : 0x0a);
278 while ((_inp(0xc000) & 8));
279 cputemp = _inpw(0xc006);
281 _outp(0xc004, (0x4c<<1)|0x01);
283 _outpw(0xc000, _inpw(0xc000));
284 _outp(0xc002, (0) ? 0x0b : 0x0a);
285 while ((_inp(0xc000) & 8));
286 cpudec = _inpw(0xc006);
288 if (cpudec<10) cpudec = cpudec * 100;
289 if (cpudec<100) cpudec = cpudec *10;
291 return PyInt_FromLong((long)(cputemp + cpudec / 1000.0f));
294 String getInfoLabel(const char* cLine)
303 int ret = g_infoManager.TranslateString(cLine);
304 //doesn't seem to be a single InfoTag?
305 //try full blown GuiInfoLabel then
308 CGUIInfoLabel label(cLine);
309 return label.GetLabel(0);
312 return g_infoManager.GetLabel(ret);
315 String getInfoImage(const char * infotag)
324 int ret = g_infoManager.TranslateString(infotag);
325 return g_infoManager.GetImage(ret, WINDOW_INVALID);
328 void playSFX(const char* filename)
334 if (XFILE::CFile::Exists(filename))
336 g_audioManager.PlayPythonSound(filename);
340 void enableNavSounds(bool yesNo)
343 g_audioManager.Enable(yesNo);
346 bool getCondVisibility(const char *condition)
356 int id = g_windowManager.GetTopMostModalDialogID();
357 if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
358 ret = g_infoManager.EvaluateBool(condition,id);
364 int getGlobalIdleTime()
367 return g_application.GlobalIdleTime();
370 String getCacheThumbName(const String& path)
374 crc.ComputeFromLowerCase(path);
375 return StringUtils::Format("%08x.tbn", (unsigned __int32)crc);;
378 String makeLegalFilename(const String& filename, bool fatX)
381 return CUtil::MakeLegalPath(filename);
384 String translatePath(const String& path)
387 return CSpecialProtocol::TranslatePath(path);
390 Tuple<String,String> getCleanMovieTitle(const String& path, bool usefoldername)
393 CFileItem item(path, false);
394 CStdString strName = item.GetMovieName(usefoldername);
396 CStdString strTitleAndYear;
399 CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, usefoldername);
400 return Tuple<String,String>(strTitle,strYear);
403 String validatePath(const String& path)
406 return CUtil::ValidatePath(path, true);
409 String getRegion(const char* id)
414 if (strcmpi(id, "datelong") == 0)
416 result = g_langInfo.GetDateFormat(true);
417 result.Replace("DDDD", "%A");
418 result.Replace("MMMM", "%B");
419 result.Replace("D", "%d");
420 result.Replace("YYYY", "%Y");
422 else if (strcmpi(id, "dateshort") == 0)
424 result = g_langInfo.GetDateFormat(false);
425 result.Replace("MM", "%m");
426 result.Replace("DD", "%d");
427 result.Replace("YYYY", "%Y");
429 else if (strcmpi(id, "tempunit") == 0)
430 result = g_langInfo.GetTempUnitString();
431 else if (strcmpi(id, "speedunit") == 0)
432 result = g_langInfo.GetSpeedUnitString();
433 else if (strcmpi(id, "time") == 0)
435 result = g_langInfo.GetTimeFormat();
436 result.Replace("H", "%H");
437 result.Replace("h", "%I");
438 result.Replace("mm", "%M");
439 result.Replace("ss", "%S");
440 result.Replace("xx", "%p");
442 else if (strcmpi(id, "meridiem") == 0)
443 result = StringUtils::Format("%s/%s",
444 g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_AM).c_str(),
445 g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_PM).c_str());
450 // TODO: Add a mediaType enum
451 String getSupportedMedia(const char* mediaType)
455 if (strcmpi(mediaType, "video") == 0)
456 result = g_advancedSettings.m_videoExtensions;
457 else if (strcmpi(mediaType, "music") == 0)
458 result = g_advancedSettings.m_musicExtensions;
459 else if (strcmpi(mediaType, "picture") == 0)
460 result = g_advancedSettings.m_pictureExtensions;
469 bool skinHasImage(const char* image)
472 return g_TextureManager.HasTexture(image);
476 bool startServer(int iTyp, bool bStart, bool bWait)
480 return g_application.StartServer((CApplication::ESERVERS)iTyp, bStart != 0, bWait != 0);
485 CAEFactory::Suspend();
490 CAEFactory::Resume();
493 String convertLanguage(const char* language, int format)
495 CStdString convertedLanguage;
498 case CLangCodeExpander::ENGLISH_NAME:
500 g_LangCodeExpander.Lookup(convertedLanguage, language);
501 // maybe it's a check whether the language exists or not
502 if (convertedLanguage.empty())
504 g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
505 g_LangCodeExpander.Lookup(convertedLanguage, convertedLanguage);
509 case CLangCodeExpander::ISO_639_1:
510 g_LangCodeExpander.ConvertToTwoCharCode(convertedLanguage, language);
512 case CLangCodeExpander::ISO_639_2:
513 g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
518 return convertedLanguage;
521 int getSERVER_WEBSERVER() { return CApplication::ES_WEBSERVER; }
522 int getSERVER_AIRPLAYSERVER() { return CApplication::ES_AIRPLAYSERVER; }
523 int getSERVER_UPNPSERVER() { return CApplication::ES_UPNPSERVER; }
524 int getSERVER_UPNPRENDERER() { return CApplication::ES_UPNPRENDERER; }
525 int getSERVER_EVENTSERVER() { return CApplication::ES_EVENTSERVER; }
526 int getSERVER_JSONRPCSERVER() { return CApplication::ES_JSONRPCSERVER; }
527 int getSERVER_ZEROCONF() { return CApplication::ES_ZEROCONF; }
529 int getPLAYLIST_MUSIC() { return PLAYLIST_MUSIC; }
530 int getPLAYLIST_VIDEO() { return PLAYLIST_VIDEO; }
531 int getPLAYER_CORE_AUTO() { return EPC_NONE; }
532 int getPLAYER_CORE_DVDPLAYER() { return EPC_DVDPLAYER; }
533 int getPLAYER_CORE_MPLAYER() { return EPC_MPLAYER; }
534 int getPLAYER_CORE_PAPLAYER() { return EPC_PAPLAYER; }
535 int getTRAY_OPEN() { return TRAY_OPEN; }
536 int getDRIVE_NOT_READY() { return DRIVE_NOT_READY; }
537 int getTRAY_CLOSED_NO_MEDIA() { return TRAY_CLOSED_NO_MEDIA; }
538 int getTRAY_CLOSED_MEDIA_PRESENT() { return TRAY_CLOSED_MEDIA_PRESENT; }
539 int getLOGDEBUG() { return LOGDEBUG; }
540 int getLOGINFO() { return LOGINFO; }
541 int getLOGNOTICE() { return LOGNOTICE; }
542 int getLOGWARNING() { return LOGWARNING; }
543 int getLOGERROR() { return LOGERROR; }
544 int getLOGSEVERE() { return LOGSEVERE; }
545 int getLOGFATAL() { return LOGFATAL; }
546 int getLOGNONE() { return LOGNONE; }
548 // render capture user states
549 int getCAPTURE_STATE_WORKING() { return CAPTURESTATE_WORKING; }
550 int getCAPTURE_STATE_DONE(){ return CAPTURESTATE_DONE; }
551 int getCAPTURE_STATE_FAILED() { return CAPTURESTATE_FAILED; }
553 // render capture flags
554 int getCAPTURE_FLAG_CONTINUOUS() { return (int)CAPTUREFLAG_CONTINUOUS; }
555 int getCAPTURE_FLAG_IMMEDIATELY() { return (int)CAPTUREFLAG_IMMEDIATELY; }
557 // language string formats
558 int getISO_639_1() { return CLangCodeExpander::ISO_639_1; }
559 int getISO_639_2(){ return CLangCodeExpander::ISO_639_2; }
560 int getENGLISH_NAME() { return CLangCodeExpander::ENGLISH_NAME; }
562 const int lLOGNOTICE = LOGNOTICE;