2 * Copyright (C) 2005-2008 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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 #if (defined USE_EXTERNAL_PYTHON)
24 #if (defined HAVE_LIBPYTHON2_6)
25 #include <python2.6/Python.h>
26 #elif (defined HAVE_LIBPYTHON2_5)
27 #include <python2.5/Python.h>
28 #elif (defined HAVE_LIBPYTHON2_4)
29 #include <python2.4/Python.h>
31 #error "Could not determine version of Python to use."
34 #include "python/Include/Python.h"
36 #include "../XBPythonDll.h"
38 #include "pyplaylist.h"
40 #include "storage/IoSupport.h"
44 #include "infotagvideo.h"
45 #include "infotagmusic.h"
47 #include "interfaces/http-api/XBMChttp.h"
48 #include "interfaces/http-api/HttpApi.h"
50 #include "pyjsonrpc.h"
51 #include "GUIInfoManager.h"
52 #include "guilib/GUIWindowManager.h"
53 #include "guilib/GUIAudioManager.h"
54 #include "Application.h"
55 #include "utils/Crc32.h"
56 #include "utils/URIUtils.h"
58 #include "filesystem/File.h"
59 #include "filesystem/SpecialProtocol.h"
60 #include "settings/GUISettings.h"
61 #include "guilib/TextureManager.h"
63 #include "SectionLoader.h"
64 #include "settings/Settings.h"
65 #include "guilib/LocalizeStrings.h"
66 #include "utils/FileUtils.h"
67 #include "pythreadstate.h"
69 // include for constants
71 #include "PlayListPlayer.h"
74 using namespace XFILE;
77 #pragma code_seg("PY_TEXT")
78 #pragma data_seg("PY_DATA")
79 #pragma bss_seg("PY_BSS")
80 #pragma const_seg("PY_RDATA")
83 #if defined(__GNUG__) && (__GNUC__>4) || (__GNUC__==4 && __GNUC_MINOR__>=2)
84 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
93 /*****************************************************************
94 * start of xbmc methods
95 *****************************************************************/
98 PyDoc_STRVAR(output__doc__,
99 "output(msg[, level]) -- Write a string to XBMC's log file and the debug window.\n"
101 "msg : string - text to output.\n"
102 "level : [opt] integer - log level to ouput at. (default=LOGNOTICE)\n"
104 "*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
105 " Once you use a keyword, all following arguments require the keyword.\n"
107 " Text is written to the log for the following conditions.\n"
108 " XBMC loglevel == -1 (NONE, nothing at all is logged)"
109 " XBMC loglevel == 0 (NORMAL, shows LOGNOTICE, LOGERROR, LOGSEVERE and LOGFATAL)"
110 " XBMC loglevel == 1 (DEBUG, shows all)"
111 " See pydocs for valid values for level.\n"
114 " - xbmc.output(msg='This is a test string.', level=xbmc.LOGDEBUG)\n");
116 PyObject* XBMC_Output(PyObject *self, PyObject *args, PyObject *kwds)
118 static const char *keywords[] = {
124 int iLevel = LOGNOTICE;
125 if (!PyArg_ParseTupleAndKeywords(
135 // check for a valid loglevel
136 if (iLevel < LOGDEBUG || iLevel > LOGNONE)
138 CLog::Log(iLevel, "%s", s_line);
145 PyDoc_STRVAR(log__doc__,
146 "log(msg[, level]) -- Write a string to XBMC's log file.\n"
148 "msg : string - text to output.\n"
149 "level : [opt] integer - log level to ouput at. (default=LOGNOTICE)\n"
152 "*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
153 " Once you use a keyword, all following arguments require the keyword.\n"
155 " Text is written to the log for the following conditions.\n"
156 " XBMC loglevel == -1 (NONE, nothing at all is logged)"
157 " XBMC loglevel == 0 (NORMAL, shows LOGNOTICE, LOGERROR, LOGSEVERE and LOGFATAL)"
158 " XBMC loglevel == 1 (DEBUG, shows all)"
159 " See pydocs for valid values for level.\n"
162 " - xbmc.log(msg='This is a test string.', level=xbmc.LOGDEBUG)\n");
164 PyObject* XBMC_Log(PyObject *self, PyObject *args, PyObject *kwds)
166 static const char *keywords[] = {
172 int iLevel = LOGNOTICE;
173 if (!PyArg_ParseTupleAndKeywords(
183 // check for a valid loglevel
184 if (iLevel < LOGDEBUG || iLevel > LOGNONE)
186 CLog::Log(iLevel, "%s", s_line);
193 PyDoc_STRVAR(shutdown__doc__,
194 "shutdown() -- Shutdown the xbox.\n"
197 " - xbmc.shutdown()\n");
199 PyObject* XBMC_Shutdown(PyObject *self, PyObject *args)
201 ThreadMessage tMsg = {TMSG_SHUTDOWN};
202 g_application.getApplicationMessenger().SendMessage(tMsg);
208 // dashboard() method
209 PyDoc_STRVAR(dashboard__doc__,
210 "dashboard() -- Boot to dashboard as set in My Pograms/General.\n"
213 " - xbmc.dashboard()\n");
215 PyObject* XBMC_Dashboard(PyObject *self, PyObject *args)
217 ThreadMessage tMsg = {TMSG_DASHBOARD};
218 g_application.getApplicationMessenger().SendMessage(tMsg);
225 PyDoc_STRVAR(restart__doc__,
226 "restart() -- Restart the xbox.\n"
229 " - xbmc.restart()\n");
231 PyObject* XBMC_Restart(PyObject *self, PyObject *args)
233 ThreadMessage tMsg = {TMSG_RESTART};
234 g_application.getApplicationMessenger().SendMessage(tMsg);
240 // executescript() method
241 PyDoc_STRVAR(executeScript__doc__,
242 "executescript(script) -- Execute a python script.\n"
244 "script : string - script filename to execute.\n"
247 " - xbmc.executescript('special://home/scripts/update.py')\n");
249 PyObject* XBMC_ExecuteScript(PyObject *self, PyObject *args)
252 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
254 ThreadMessage tMsg = {TMSG_EXECUTE_SCRIPT};
255 tMsg.strParam = cLine;
256 g_application.getApplicationMessenger().SendMessage(tMsg);
262 // executebuiltin() method
263 PyDoc_STRVAR(executeBuiltIn__doc__,
264 "executebuiltin(function) -- Execute a built in XBMC function.\n"
266 "function : string - builtin function to execute.\n"
268 "List of functions - http://wiki.xbmc.org/?title=List_of_Built_In_Functions \n"
271 " - xbmc.executebuiltin('XBMC.RunXBE(c:\\\\avalaunch.xbe)')\n");
273 PyObject* XBMC_ExecuteBuiltIn(PyObject *self, PyObject *args)
276 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
278 g_application.getApplicationMessenger().ExecBuiltIn(cLine);
285 // executehttpapi() method
286 PyDoc_STRVAR(executeHttpApi__doc__,
287 "executehttpapi(httpcommand) -- Execute an HTTP API command.\n"
289 "httpcommand : string - http command to execute.\n"
291 "List of commands - http://wiki.xbmc.org/?title=WebServerHTTP-API#The_Commands \n"
294 " - response = xbmc.executehttpapi('TakeScreenShot(special://temp/test.jpg,0,false,200,-1,90)')\n");
296 PyObject* XBMC_ExecuteHttpApi(PyObject *self, PyObject *args)
299 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
301 m_pXbmcHttp = new CXbmcHttp();
302 CStdString method = cLine;
305 CStdString parameter="", cmd=cLine, execute;
306 open = cmd.Find("(");
310 while (close>open && cmd.Mid(close,1)!=")")
314 parameter = cmd.Mid(open + 1, close - open - 1);
315 parameter.Replace(",",";");
316 execute = cmd.Left(open);
318 else //open bracket but no close
319 return PyString_FromString("");
324 CURL::Decode(parameter);
325 return PyString_FromString(CHttpApi::MethodCall(execute, parameter).c_str());
330 // executehttpapi() method
331 PyDoc_STRVAR(executeJSONRPC__doc__,
332 "executeJSONRPC(jsonrpccommand) -- Execute an JSONRPC command.\n"
334 "jsonrpccommand : string - jsonrpc command to execute.\n"
336 "List of commands - \n"
339 " - response = xbmc.executeJSONRPC('{ \"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Introspect\", \"id\": 1 }')\n");
341 PyObject* XBMC_ExecuteJSONRPC(PyObject *self, PyObject *args)
344 if (!PyArg_ParseTuple(args, (char*)"s", &cLine))
347 CStdString method = cLine;
349 CPythonTransport transport;
350 CPythonTransport::CPythonClient client;
352 return PyString_FromString(JSONRPC::CJSONRPC::MethodCall(method, &transport, &client).c_str());
357 PyDoc_STRVAR(sleep__doc__,
358 "sleep(time) -- Sleeps for 'time' msec.\n"
360 "time : integer - number of msec to sleep.\n"
362 "*Note, This is useful if you have for example a Player class that is waiting\n"
363 " for onPlayBackEnded() calls.\n"
365 "Throws: PyExc_TypeError, if time is not an integer.\n"
368 " - xbmc.sleep(2000) # sleeps for 2 seconds\n");
370 PyObject* XBMC_Sleep(PyObject *self, PyObject *args)
373 if (!PyArg_ParseTuple(args, (char*)"O", &pObject)) return NULL;
374 if (!PyInt_Check(pObject))
376 PyErr_Format(PyExc_TypeError, "argument must be a bool(integer) value");
380 long i = PyInt_AsLong(pObject);
383 CPyThreadState pyState;
387 PyXBMC_MakePendingCalls();
388 //i = PyInt_AsLong(pObject);
395 // getLocalizedString() method
396 PyDoc_STRVAR(getLocalizedString__doc__,
397 "getLocalizedString(id) -- Returns a localized 'unicode string'.\n"
399 "id : integer - id# for string you want to localize.\n"
401 "*Note, See strings.xml in \\language\\{yourlanguage}\\ for which id\n"
402 " you need for a string.\n"
405 " - locstr = xbmc.getLocalizedString(6)\n");
407 PyObject* XBMC_GetLocalizedString(PyObject *self, PyObject *args)
410 if (!PyArg_ParseTuple(args, (char*)"i", &iString)) return NULL;
413 if (iString >= 30000 && iString <= 30999)
414 label = g_localizeStringsTemp.Get(iString);
415 else if (iString >= 32000 && iString <= 32999)
416 label = g_localizeStringsTemp.Get(iString);
418 label = g_localizeStrings.Get(iString);
420 return PyUnicode_DecodeUTF8(label.c_str(), label.size(), "replace");
423 // getSkinDir() method
424 PyDoc_STRVAR(getSkinDir__doc__,
425 "getSkinDir() -- Returns the active skin directory as a string.\n"
427 "*Note, This is not the full path like 'special://home/addons/MediaCenter', but only 'MediaCenter'.\n"
430 " - skindir = xbmc.getSkinDir()\n");
432 PyObject* XBMC_GetSkinDir(PyObject *self, PyObject *args)
434 return PyString_FromString(g_guiSettings.GetString("lookandfeel.skin"));
437 // getLanguage() method
438 PyDoc_STRVAR(getLanguage__doc__,
439 "getLanguage() -- Returns the active language as a string.\n"
442 " - language = xbmc.getLanguage()\n");
444 PyObject* XBMC_GetLanguage(PyObject *self, PyObject *args)
446 return PyString_FromString(g_guiSettings.GetString("locale.language"));
449 // getIPAddress() method
450 PyDoc_STRVAR(getIPAddress__doc__,
451 "getIPAddress() -- Returns the current ip address as a string.\n"
454 " - ip = xbmc.getIPAddress()\n");
456 PyObject* XBMC_GetIPAddress(PyObject *self, PyObject *args)
459 sprintf(cTitleIP, "127.0.0.1");
460 CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
462 return PyString_FromString(iface->GetCurrentIPAddress().c_str());
464 return PyString_FromString(cTitleIP);
467 // getDVDState() method
468 PyDoc_STRVAR(getDVDState__doc__,
469 "getDVDState() -- Returns the dvd state as an integer.\n"
471 "return values are:\n"
472 " 1 : xbmc.DRIVE_NOT_READY\n"
473 " 16 : xbmc.TRAY_OPEN\n"
474 " 64 : xbmc.TRAY_CLOSED_NO_MEDIA\n"
475 " 96 : xbmc.TRAY_CLOSED_MEDIA_PRESENT\n"
478 " - dvdstate = xbmc.getDVDState()\n");
480 PyObject* XBMC_GetDVDState(PyObject *self, PyObject *args)
482 return PyInt_FromLong(CIoSupport::GetTrayState());
485 // getFreeMem() method
486 PyDoc_STRVAR(getFreeMem__doc__,
487 "getFreeMem() -- Returns the amount of free memory in MB as an integer.\n"
490 " - freemem = xbmc.getFreeMem()\n");
492 PyObject* XBMC_GetFreeMem(PyObject *self, PyObject *args)
495 GlobalMemoryStatus(&stat);
496 return PyInt_FromLong( stat.dwAvailPhys / ( 1024 * 1024 ) );
499 // getCpuTemp() method
500 // ## Doesn't work right, use getInfoLabel('System.CPUTemperature') instead.
501 /*PyDoc_STRVAR(getCpuTemp__doc__,
502 "getCpuTemp() -- Returns the current cpu temperature as an integer.\n"
505 " - cputemp = xbmc.getCpuTemp()\n");
507 PyObject* XBMC_GetCpuTemp(PyObject *self, PyObject *args)
509 unsigned short cputemp;
510 unsigned short cpudec;
512 _outp(0xc004, (0x4c<<1)|0x01);
514 _outpw(0xc000, _inpw(0xc000));
515 _outp(0xc002, (0) ? 0x0b : 0x0a);
516 while ((_inp(0xc000) & 8));
517 cputemp = _inpw(0xc006);
519 _outp(0xc004, (0x4c<<1)|0x01);
521 _outpw(0xc000, _inpw(0xc000));
522 _outp(0xc002, (0) ? 0x0b : 0x0a);
523 while ((_inp(0xc000) & 8));
524 cpudec = _inpw(0xc006);
526 if (cpudec<10) cpudec = cpudec * 100;
527 if (cpudec<100) cpudec = cpudec *10;
529 return PyInt_FromLong((long)(cputemp + cpudec / 1000.0f));
532 // getInfolabel() method
533 PyDoc_STRVAR(getInfoLabel__doc__,
534 "getInfoLabel(infotag) -- Returns an InfoLabel as a string.\n"
536 "infotag : string - infoTag for value you want returned.\n"
538 "List of InfoTags - http://wiki.xbmc.org/?title=InfoLabels \n"
541 " - label = xbmc.getInfoLabel('Weather.Conditions')\n");
543 PyObject* XBMC_GetInfoLabel(PyObject *self, PyObject *args)
546 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
548 int ret = g_infoManager.TranslateString(cLine);
549 return Py_BuildValue((char*)"s", g_infoManager.GetLabel(ret).c_str());
552 // getInfoImage() method
553 PyDoc_STRVAR(getInfoImage__doc__,
554 "getInfoImage(infotag) -- Returns a filename including path to the InfoImage's\n"
555 " thumbnail as a string.\n"
557 "infotag : string - infotag for value you want returned.\n"
559 "List of InfoTags - http://wiki.xbmc.org/?title=InfoLabels \n"
562 " - filename = xbmc.getInfoImage('Weather.Conditions')\n");
564 PyObject* XBMC_GetInfoImage(PyObject *self, PyObject *args)
567 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
569 int ret = g_infoManager.TranslateString(cLine);
570 return Py_BuildValue((char*)"s", g_infoManager.GetImage(ret, WINDOW_INVALID).c_str());
574 PyDoc_STRVAR(playSFX__doc__,
575 "playSFX(filename) -- Plays a wav file by filename\n"
577 "filename : string - filename of the wav file to play.\n"
580 " - xbmc.playSFX('special://xbmc/scripts/dingdong.wav')\n");
582 PyObject* XBMC_PlaySFX(PyObject *self, PyObject *args)
584 const char *cFile = NULL;
586 if (!PyArg_ParseTuple(args, (char*)"s", &cFile)) return NULL;
588 if (CFile::Exists(cFile))
590 g_audioManager.PlayPythonSound(cFile);
597 // enableNavSounds() method
598 PyDoc_STRVAR(enableNavSounds__doc__,
599 "enableNavSounds(yesNo) -- Enables/Disables nav sounds\n"
601 "yesNo : integer - enable (True) or disable (False) nav sounds\n"
604 " - xbmc.enableNavSounds(True)\n");
606 PyObject* XBMC_EnableNavSounds(PyObject *self, PyObject *args)
610 if (!PyArg_ParseTuple(args, (char*)"i", &yesNo)) return NULL;
612 g_audioManager.Enable(yesNo==1);
618 // getCondVisibility() method
619 PyDoc_STRVAR(getCondVisibility__doc__,
620 "getCondVisibility(condition) -- Returns True (1) or False (0) as a bool.\n"
622 "condition : string - condition to check.\n"
624 "List of Conditions - http://wiki.xbmc.org/?title=List_of_Boolean_Conditions \n"
626 "*Note, You can combine two (or more) of the above settings by using \"+\" as an AND operator,\n"
627 "\"|\" as an OR operator, \"!\" as a NOT operator, and \"[\" and \"]\" to bracket expressions.\n"
630 " - visible = xbmc.getCondVisibility('[Control.IsVisible(41) + !Control.IsVisible(12)]')\n");
632 PyObject* XBMC_GetCondVisibility(PyObject *self, PyObject *args)
635 if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
638 int id = g_windowManager.GetTopMostModalDialogID();
639 if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
642 int ret = g_infoManager.TranslateString(cLine);
643 return Py_BuildValue((char*)"b", g_infoManager.GetBool(ret,id));
646 // getGlobalIdleTime() method
647 PyDoc_STRVAR(getGlobalIdleTime__doc__,
648 "getGlobalIdleTime() -- Returns the elapsed idle time in seconds as an integer.\n"
651 " - t = xbmc.getGlobalIdleTime()");
653 PyObject* XBMC_GetGlobalIdleTime(PyObject *self)
655 return Py_BuildValue((char*)"i", g_application.GlobalIdleTime());
658 // getCacheThumbName function
659 PyDoc_STRVAR(getCacheThumbName__doc__,
660 "getCacheThumbName(path) -- Returns a thumb cache filename.\n"
662 "path : string or unicode - path to file\n"
665 " - thumb = xbmc.getCacheThumbName('f:\\\\videos\\\\movie.avi')\n");
667 PyObject* XBMC_GetCacheThumbName(PyObject *self, PyObject *args)
669 PyObject *pObjectText;
670 if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
673 if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
677 crc.ComputeFromLowerCase(strText);
678 strPath.Format("%08x.tbn", (unsigned __int32)crc);
679 return Py_BuildValue((char*)"s", strPath.c_str());
682 // makeLegalFilename function
683 PyDoc_STRVAR(makeLegalFilename__doc__,
684 "makeLegalFilename(filename[, fatX]) -- Returns a legal filename or path as a string.\n"
686 "filename : string or unicode - filename/path to make legal\n"
687 "fatX : [opt] bool - True=Xbox file system(Default)\n"
689 "*Note, If fatX is true you should pass a full path. If fatX is false only pass\n"
690 " the basename of the path.\n"
692 " You can use the above as keywords for arguments and skip certain optional arguments.\n"
693 " Once you use a keyword, all following arguments require the keyword.\n"
696 " - filename = xbmc.makeLegalFilename('F:\\Trailers\\Ice Age: The Meltdown.avi')\n");
698 PyObject* XBMC_MakeLegalFilename(PyObject *self, PyObject *args, PyObject *kwds)
700 static const char *keywords[] = { "filename", "fatX", NULL };
701 PyObject *pObjectText;
703 // parse arguments to constructor
704 if (!PyArg_ParseTupleAndKeywords(
717 if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
719 CStdString strFilename;
720 strFilename = CUtil::MakeLegalPath(strText);
721 return Py_BuildValue((char*)"s", strFilename.c_str());
724 // translatePath function
725 PyDoc_STRVAR(translatePath__doc__,
726 "translatePath(path) -- Returns the translated path.\n"
728 "path : string or unicode - Path to format\n"
730 "*Note, Only useful if you are coding for both Linux and Windows/Xbox.\n"
731 " e.g. Converts 'special://masterprofile/script_data' -> '/home/user/XBMC/UserData/script_data'\n"
732 " on Linux. Would return 'special://masterprofile/script_data' on the Xbox.\n"
735 " - fpath = xbmc.translatePath('special://masterprofile/script_data')\n");
737 PyObject* XBMC_TranslatePath(PyObject *self, PyObject *args)
739 PyObject *pObjectText;
740 if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
743 if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
746 if (URIUtils::IsDOSPath(strText))
747 strText = CSpecialProtocol::ReplaceOldPath(strText, 0);
749 strPath = CSpecialProtocol::TranslatePath(strText);
751 return Py_BuildValue((char*)"s", strPath.c_str());
754 // getcleanmovietitle function
755 PyDoc_STRVAR(getCleanMovieTitle__doc__,
756 "getCleanMovieTitle(path[, usefoldername]) -- Returns a clean movie title and year string if available.\n"
758 "path : string or unicode - String to clean\n"
759 "bool : [opt] bool - use folder names (defaults to false)\n"
762 " - title, year = xbmc.getCleanMovieTitle('/path/to/moviefolder/test.avi', True)\n");
764 PyObject* XBMC_GetCleanMovieTitle(PyObject *self, PyObject *args, PyObject *kwds)
766 static const char *keywords[] = { "path", "usefoldername", NULL };
767 PyObject *pObjectText;
768 bool bUseFolderName = false;
769 // parse arguments to constructor
770 if (!PyArg_ParseTupleAndKeywords(
783 if (!PyXBMCGetUnicodeString(strPath, pObjectText, 1)) return NULL;
785 CFileItem item(strPath, false);
786 CStdString strName = item.GetMovieName(bUseFolderName);
788 CStdString strTitle, strTitleAndYear, strYear;
789 CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, bUseFolderName);
791 return Py_BuildValue((char*)"s,s", strTitle.c_str(), strYear.c_str());
794 // validatePath function
795 PyDoc_STRVAR(validatePath__doc__,
796 "validatePath(path) -- Returns the validated path.\n"
798 "path : string or unicode - Path to format\n"
800 "*Note, Only useful if you are coding for both Linux and Windows/Xbox for fixing slash problems.\n"
801 " e.g. Corrects 'Z://something' -> 'Z:\\something'\n"
804 " - fpath = xbmc.validatePath(somepath)\n");
806 PyObject* XBMC_ValidatePath(PyObject *self, PyObject *args)
808 PyObject *pObjectText;
809 if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
812 if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
814 return Py_BuildValue((char*)"s", CUtil::ValidatePath(strText, true).c_str());
817 // getRegion function
818 PyDoc_STRVAR(getRegion__doc__,
819 "getRegion(id) -- Returns your regions setting as a string for the specified id.\n"
821 "id : string - id of setting to return\n"
823 "*Note, choices are (dateshort, datelong, time, meridiem, tempunit, speedunit)\n"
825 " You can use the above as keywords for arguments.\n"
828 " - date_long_format = xbmc.getRegion('datelong')\n");
830 PyObject* XBMC_GetRegion(PyObject *self, PyObject *args, PyObject *kwds)
832 static const char *keywords[] = { "id", NULL };
834 // parse arguments to constructor
835 if (!PyArg_ParseTupleAndKeywords(
848 if (strcmpi(id, "datelong") == 0)
850 result = g_langInfo.GetDateFormat(true);
851 result.Replace("DDDD", "%A");
852 result.Replace("MMMM", "%B");
853 result.Replace("D", "%d");
854 result.Replace("YYYY", "%Y");
856 else if (strcmpi(id, "dateshort") == 0)
858 result = g_langInfo.GetDateFormat(false);
859 result.Replace("MM", "%m");
860 result.Replace("DD", "%d");
861 result.Replace("YYYY", "%Y");
863 else if (strcmpi(id, "tempunit") == 0)
864 result = g_langInfo.GetTempUnitString();
865 else if (strcmpi(id, "speedunit") == 0)
866 result = g_langInfo.GetSpeedUnitString();
867 else if (strcmpi(id, "time") == 0)
869 result = g_langInfo.GetTimeFormat();
870 result.Replace("H", "%H");
871 result.Replace("h", "%I");
872 result.Replace("mm", "%M");
873 result.Replace("ss", "%S");
874 result.Replace("xx", "%p");
876 else if (strcmpi(id, "meridiem") == 0)
877 result.Format("%s/%s", g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_AM), g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_PM));
879 return Py_BuildValue((char*)"s", result.c_str());
882 // getSupportedMedia function
883 PyDoc_STRVAR(getSupportedMedia__doc__,
884 "getSupportedMedia(media) -- Returns the supported file types for the specific media as a string.\n"
886 "media : string - media type\n"
888 "*Note, media type can be (video, music, picture).\n"
890 " The return value is a pipe separated string of filetypes (eg. '.mov|.avi').\n"
892 " You can use the above as keywords for arguments.\n"
895 " - mTypes = xbmc.getSupportedMedia('video')\n");
897 PyObject* XBMC_GetSupportedMedia(PyObject *self, PyObject *args, PyObject *kwds)
899 static const char *keywords[] = { "media", NULL };
901 // parse arguments to constructor
902 if (!PyArg_ParseTupleAndKeywords(
914 if (strcmpi(media, "video") == 0)
915 result = g_settings.m_videoExtensions;
916 else if (strcmpi(media, "music") == 0)
917 result = g_settings.m_musicExtensions;
918 else if (strcmpi(media, "picture") == 0)
919 result = g_settings.m_pictureExtensions;
922 PyErr_SetString(PyExc_ValueError, "media = (video, music, picture)");
926 return Py_BuildValue((char*)"s", result.c_str());
929 // skinHasImage function
930 PyDoc_STRVAR(skinHasImage__doc__,
931 "skinHasImage(image) -- Returns True if the image file exists in the skin.\n"
933 "image : string - image filename\n"
935 "*Note, If the media resides in a subfolder include it. (eg. home-myfiles\\\\home-myfiles2.png)\n"
937 " You can use the above as keywords for arguments.\n"
940 " - exists = xbmc.skinHasImage('ButtonFocusedTexture.png')\n");
942 PyObject* XBMC_SkinHasImage(PyObject *self, PyObject *args, PyObject *kwds)
944 static const char *keywords[] = { "image", NULL };
946 // parse arguments to constructor
947 if (!PyArg_ParseTupleAndKeywords(
958 bool exists = g_TextureManager.HasTexture(image);
960 return Py_BuildValue((char*)"b", exists);
963 PyDoc_STRVAR(subHashAndFileSize__doc__,
964 "subHashAndFileSize(file)\n"
966 "file : file to calculate subtitle hash and size for"
969 " size,hash = xbmcvfs.subHashAndFileSize(file)\n");
970 PyObject* XBMC_subHashAndFileSize(PyObject *self, PyObject *args, PyObject *kwds)
973 if (!PyArg_ParseTuple(
980 CStdString strSource;
981 if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
986 CPyThreadState pyState;
987 CFileUtils::SubtitleFileSizeAndHash(strSource, strSize, strHash);
990 return Py_BuildValue((char*)"ss",strSize.c_str(), strHash.c_str());
993 // define c functions to be used in python here
994 PyMethodDef xbmcMethods[] = {
995 {(char*)"output", (PyCFunction)XBMC_Output, METH_VARARGS|METH_KEYWORDS, output__doc__},
996 {(char*)"log", (PyCFunction)XBMC_Log, METH_VARARGS|METH_KEYWORDS, log__doc__},
997 {(char*)"executescript", (PyCFunction)XBMC_ExecuteScript, METH_VARARGS, executeScript__doc__},
998 {(char*)"executebuiltin", (PyCFunction)XBMC_ExecuteBuiltIn, METH_VARARGS, executeBuiltIn__doc__},
1000 {(char*)"sleep", (PyCFunction)XBMC_Sleep, METH_VARARGS, sleep__doc__},
1001 {(char*)"shutdown", (PyCFunction)XBMC_Shutdown, METH_VARARGS, shutdown__doc__},
1002 {(char*)"dashboard", (PyCFunction)XBMC_Dashboard, METH_VARARGS, dashboard__doc__},
1003 {(char*)"restart", (PyCFunction)XBMC_Restart, METH_VARARGS, restart__doc__},
1004 {(char*)"getSkinDir", (PyCFunction)XBMC_GetSkinDir, METH_VARARGS, getSkinDir__doc__},
1005 {(char*)"getLocalizedString", (PyCFunction)XBMC_GetLocalizedString, METH_VARARGS, getLocalizedString__doc__},
1007 {(char*)"getLanguage", (PyCFunction)XBMC_GetLanguage, METH_VARARGS, getLanguage__doc__},
1008 {(char*)"getIPAddress", (PyCFunction)XBMC_GetIPAddress, METH_VARARGS, getIPAddress__doc__},
1009 {(char*)"getDVDState", (PyCFunction)XBMC_GetDVDState, METH_VARARGS, getDVDState__doc__},
1010 {(char*)"getFreeMem", (PyCFunction)XBMC_GetFreeMem, METH_VARARGS, getFreeMem__doc__},
1011 //{(char*)"getCpuTemp", (PyCFunction)XBMC_GetCpuTemp, METH_VARARGS, getCpuTemp__doc__},
1014 {(char*)"executehttpapi", (PyCFunction)XBMC_ExecuteHttpApi, METH_VARARGS, executeHttpApi__doc__},
1017 {(char*)"executeJSONRPC", (PyCFunction)XBMC_ExecuteJSONRPC, METH_VARARGS, executeJSONRPC__doc__},
1019 {(char*)"getInfoLabel", (PyCFunction)XBMC_GetInfoLabel, METH_VARARGS, getInfoLabel__doc__},
1020 {(char*)"getInfoImage", (PyCFunction)XBMC_GetInfoImage, METH_VARARGS, getInfoImage__doc__},
1021 {(char*)"getCondVisibility", (PyCFunction)XBMC_GetCondVisibility, METH_VARARGS, getCondVisibility__doc__},
1022 {(char*)"getGlobalIdleTime", (PyCFunction)XBMC_GetGlobalIdleTime, METH_VARARGS, getGlobalIdleTime__doc__},
1024 {(char*)"playSFX", (PyCFunction)XBMC_PlaySFX, METH_VARARGS, playSFX__doc__},
1025 {(char*)"enableNavSounds", (PyCFunction)XBMC_EnableNavSounds, METH_VARARGS, enableNavSounds__doc__},
1027 {(char*)"getCacheThumbName", (PyCFunction)XBMC_GetCacheThumbName, METH_VARARGS, getCacheThumbName__doc__},
1029 {(char*)"makeLegalFilename", (PyCFunction)XBMC_MakeLegalFilename, METH_VARARGS|METH_KEYWORDS, makeLegalFilename__doc__},
1030 {(char*)"translatePath", (PyCFunction)XBMC_TranslatePath, METH_VARARGS, translatePath__doc__},
1031 {(char*)"validatePath", (PyCFunction)XBMC_ValidatePath, METH_VARARGS, validatePath__doc__},
1033 {(char*)"getRegion", (PyCFunction)XBMC_GetRegion, METH_VARARGS|METH_KEYWORDS, getRegion__doc__},
1034 {(char*)"getSupportedMedia", (PyCFunction)XBMC_GetSupportedMedia, METH_VARARGS|METH_KEYWORDS, getSupportedMedia__doc__},
1036 {(char*)"getCleanMovieTitle", (PyCFunction)XBMC_GetCleanMovieTitle, METH_VARARGS|METH_KEYWORDS, getCleanMovieTitle__doc__},
1038 {(char*)"skinHasImage", (PyCFunction)XBMC_SkinHasImage, METH_VARARGS|METH_KEYWORDS, skinHasImage__doc__},
1039 {(char*)"subHashAndFileSize", (PyCFunction)XBMC_subHashAndFileSize, METH_VARARGS, subHashAndFileSize__doc__},
1041 {NULL, NULL, 0, NULL}
1044 /*****************************************************************
1045 * end of methods and python objects
1047 *****************************************************************/
1049 InitXBMCTypes(bool bInitTypes)
1051 initKeyboard_Type();
1053 initPlayList_Type();
1054 initPlayListItem_Type();
1055 initInfoTagMusic_Type();
1056 initInfoTagVideo_Type();
1058 if (PyType_Ready(&Keyboard_Type) < 0 ||
1059 PyType_Ready(&Player_Type) < 0 ||
1060 PyType_Ready(&PlayList_Type) < 0 ||
1061 PyType_Ready(&PlayListItem_Type) < 0 ||
1062 PyType_Ready(&InfoTagMusic_Type) < 0 ||
1063 PyType_Ready(&InfoTagVideo_Type) < 0) return;
1069 // no need to Py_DECREF our objects (see InitXBMCModule()) as they were created only
1070 // so that they could be added to the module, which steals a reference.
1076 // init general xbmc modules
1077 PyObject* pXbmcModule;
1079 Py_INCREF(&Keyboard_Type);
1080 Py_INCREF(&Player_Type);
1081 Py_INCREF(&PlayList_Type);
1082 Py_INCREF(&PlayListItem_Type);
1083 Py_INCREF(&InfoTagMusic_Type);
1084 Py_INCREF(&InfoTagVideo_Type);
1086 pXbmcModule = Py_InitModule((char*)"xbmc", xbmcMethods);
1087 if (pXbmcModule == NULL) return;
1089 PyModule_AddObject(pXbmcModule, (char*)"Keyboard", (PyObject*)&Keyboard_Type);
1090 PyModule_AddObject(pXbmcModule, (char*)"Player", (PyObject*)&Player_Type);
1091 PyModule_AddObject(pXbmcModule, (char*)"PlayList", (PyObject*)&PlayList_Type);
1092 PyModule_AddObject(pXbmcModule, (char*)"PlayListItem", (PyObject*)&PlayListItem_Type);
1093 PyModule_AddObject(pXbmcModule, (char*)"InfoTagMusic", (PyObject*)&InfoTagMusic_Type);
1094 PyModule_AddObject(pXbmcModule, (char*)"InfoTagVideo", (PyObject*)&InfoTagVideo_Type);
1097 PyModule_AddStringConstant(pXbmcModule, (char*)"__author__", (char*)PY_XBMC_AUTHOR);
1098 PyModule_AddStringConstant(pXbmcModule, (char*)"__date__", (char*)"15 November 2005");
1099 PyModule_AddStringConstant(pXbmcModule, (char*)"__version__", (char*)"1.3");
1100 PyModule_AddStringConstant(pXbmcModule, (char*)"__credits__", (char*)PY_XBMC_CREDITS);
1101 PyModule_AddStringConstant(pXbmcModule, (char*)"__platform__", (char*)PY_XBMC_PLATFORM);
1103 // playlist constants
1104 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYLIST_MUSIC", PLAYLIST_MUSIC);
1105 //PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYLIST_MUSIC_TEMP", (char*)PLAYLIST_MUSIC_TEMP);
1106 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYLIST_VIDEO", PLAYLIST_VIDEO);
1107 //PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYLIST_VIDEO_TEMP", PLAYLIST_VIDEO_TEMP);
1110 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYER_CORE_AUTO", EPC_NONE);
1111 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYER_CORE_DVDPLAYER", EPC_DVDPLAYER);
1112 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYER_CORE_MPLAYER", EPC_MPLAYER);
1113 PyModule_AddIntConstant(pXbmcModule, (char*)"PLAYER_CORE_PAPLAYER", EPC_PAPLAYER);
1115 // dvd state constants
1116 PyModule_AddIntConstant(pXbmcModule, (char*)"TRAY_OPEN", TRAY_OPEN);
1117 PyModule_AddIntConstant(pXbmcModule, (char*)"DRIVE_NOT_READY", DRIVE_NOT_READY);
1118 PyModule_AddIntConstant(pXbmcModule, (char*)"TRAY_CLOSED_NO_MEDIA", TRAY_CLOSED_NO_MEDIA);
1119 PyModule_AddIntConstant(pXbmcModule, (char*)"TRAY_CLOSED_MEDIA_PRESENT", TRAY_CLOSED_MEDIA_PRESENT);
1122 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGDEBUG", LOGDEBUG);
1123 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGINFO", LOGINFO);
1124 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGNOTICE", LOGNOTICE);
1125 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGWARNING", LOGWARNING);
1126 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGERROR", LOGERROR);
1127 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGSEVERE", LOGSEVERE);
1128 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGFATAL", LOGFATAL);
1129 PyModule_AddIntConstant(pXbmcModule, (char*)"LOGNONE", LOGNONE);
1130 PyModule_AddObject(pXbmcModule, (char*)"abortRequested", PyBool_FromLong(0));