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, bool useCached)
334 if (XFILE::CFile::Exists(filename))
336 g_audioManager.PlayPythonSound(filename,useCached);
344 g_audioManager.Stop();
347 void enableNavSounds(bool yesNo)
350 g_audioManager.Enable(yesNo);
353 bool getCondVisibility(const char *condition)
363 int id = g_windowManager.GetTopMostModalDialogID();
364 if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
365 ret = g_infoManager.EvaluateBool(condition,id);
371 int getGlobalIdleTime()
374 return g_application.GlobalIdleTime();
377 String getCacheThumbName(const String& path)
381 crc.ComputeFromLowerCase(path);
382 return StringUtils::Format("%08x.tbn", (unsigned __int32)crc);;
385 String makeLegalFilename(const String& filename, bool fatX)
388 return CUtil::MakeLegalPath(filename);
391 String translatePath(const String& path)
394 return CSpecialProtocol::TranslatePath(path);
397 Tuple<String,String> getCleanMovieTitle(const String& path, bool usefoldername)
400 CFileItem item(path, false);
401 CStdString strName = item.GetMovieName(usefoldername);
403 CStdString strTitleAndYear;
406 CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, usefoldername);
407 return Tuple<String,String>(strTitle,strYear);
410 String validatePath(const String& path)
413 return CUtil::ValidatePath(path, true);
416 String getRegion(const char* id)
421 if (strcmpi(id, "datelong") == 0)
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");
429 else if (strcmpi(id, "dateshort") == 0)
431 result = g_langInfo.GetDateFormat(false);
432 StringUtils::Replace(result, "MM", "%m");
433 StringUtils::Replace(result, "DD", "%d");
434 StringUtils::Replace(result, "YYYY", "%Y");
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)
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");
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());
457 // TODO: Add a mediaType enum
458 String getSupportedMedia(const char* mediaType)
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;
476 bool skinHasImage(const char* image)
479 return g_TextureManager.HasTexture(image);
483 bool startServer(int iTyp, bool bStart, bool bWait)
487 return g_application.StartServer((CApplication::ESERVERS)iTyp, bStart != 0, bWait != 0);
492 CAEFactory::Suspend();
497 CAEFactory::Resume();
500 String convertLanguage(const char* language, int format)
502 CStdString convertedLanguage;
505 case CLangCodeExpander::ENGLISH_NAME:
507 g_LangCodeExpander.Lookup(convertedLanguage, language);
508 // maybe it's a check whether the language exists or not
509 if (convertedLanguage.empty())
511 g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
512 g_LangCodeExpander.Lookup(convertedLanguage, convertedLanguage);
516 case CLangCodeExpander::ISO_639_1:
517 g_LangCodeExpander.ConvertToTwoCharCode(convertedLanguage, language);
519 case CLangCodeExpander::ISO_639_2:
520 g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
525 return convertedLanguage;
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; }
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; }
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; }
560 // render capture flags
561 int getCAPTURE_FLAG_CONTINUOUS() { return (int)CAPTUREFLAG_CONTINUOUS; }
562 int getCAPTURE_FLAG_IMMEDIATELY() { return (int)CAPTUREFLAG_IMMEDIATELY; }
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; }
569 const int lLOGNOTICE = LOGNOTICE;