/
xbmcmodule.cpp
1172 lines (1002 loc) · 37.9 KB
/
xbmcmodule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <Python.h>
#include "player.h"
#include "pyplaylist.h"
#include "keyboard.h"
#include "storage/IoSupport.h"
#ifndef _LINUX
#include <ConIo.h>
#endif
#include "infotagvideo.h"
#include "infotagmusic.h"
#ifdef HAS_HTTPAPI
#include "interfaces/http-api/XBMChttp.h"
#include "interfaces/http-api/HttpApi.h"
#endif
#include "pyjsonrpc.h"
#include "GUIInfoManager.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/GUIAudioManager.h"
#include "Application.h"
#include "ApplicationMessenger.h"
#include "utils/Crc32.h"
#include "utils/URIUtils.h"
#include "Util.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "settings/GUISettings.h"
#include "guilib/TextureManager.h"
#include "LangInfo.h"
#include "SectionLoader.h"
#include "settings/Settings.h"
#include "guilib/LocalizeStrings.h"
#include "utils/FileUtils.h"
#include "pythreadstate.h"
#include "utils/log.h"
#include "pyrendercapture.h"
#include "monitor.h"
#include "URL.h"
// include for constants
#include "pyutil.h"
#include "PlayListPlayer.h"
using namespace std;
using namespace XFILE;
#if defined(__GNUG__) && (__GNUC__>4) || (__GNUC__==4 && __GNUC_MINOR__>=2)
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
#ifdef __cplusplus
extern "C" {
#endif
namespace PYXBMC
{
/*****************************************************************
* start of xbmc methods
*****************************************************************/
// log() method
PyDoc_STRVAR(log__doc__,
"log(msg[, level]) -- Write a string to XBMC's log file.\n"
"\n"
"msg : string - text to output.\n"
"level : [opt] integer - log level to ouput at. (default=LOGNOTICE)\n"
"\n"
"\n"
"*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
" Once you use a keyword, all following arguments require the keyword.\n"
"\n"
" Text is written to the log for the following conditions.\n"
" XBMC loglevel == -1 (NONE, nothing at all is logged)"
" XBMC loglevel == 0 (NORMAL, shows LOGNOTICE, LOGERROR, LOGSEVERE and LOGFATAL)"
" XBMC loglevel == 1 (DEBUG, shows all)"
" See pydocs for valid values for level.\n"
"\n"
"example:\n"
" - xbmc.log(msg='This is a test string.', level=xbmc.LOGDEBUG)\n");
PyObject* XBMC_Log(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = {
"msg",
"level",
NULL};
char *s_line = NULL;
int iLevel = LOGNOTICE;
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"s|i",
(char**)keywords,
&s_line,
&iLevel))
{
return NULL;
}
// check for a valid loglevel
if (iLevel < LOGDEBUG || iLevel > LOGNONE)
iLevel = LOGNOTICE;
CLog::Log(iLevel, "%s", s_line);
Py_INCREF(Py_None);
return Py_None;
}
// output() method
PyDoc_STRVAR(output__doc__,
"'xbmc.output()' is depreciated and will be removed in future releases,\n"
"please use 'xbmc.log()' instead");
PyObject* XBMC_Output(PyObject *self, PyObject *args, PyObject *kwds)
{
CLog::Log(LOGWARNING,"'xbmc.output()' is depreciated and will be removed in future releases, please use 'xbmc.log()' instead");
return XBMC_Log(self, args, kwds);
}
// shutdown() method
PyDoc_STRVAR(shutdown__doc__,
"shutdown() -- Shutdown the xbox.\n"
"\n"
"example:\n"
" - xbmc.shutdown()\n");
PyObject* XBMC_Shutdown(PyObject *self, PyObject *args)
{
ThreadMessage tMsg = {TMSG_SHUTDOWN};
CApplicationMessenger::Get().SendMessage(tMsg);
Py_INCREF(Py_None);
return Py_None;
}
// restart() method
PyDoc_STRVAR(restart__doc__,
"restart() -- Restart the xbox.\n"
"\n"
"example:\n"
" - xbmc.restart()\n");
PyObject* XBMC_Restart(PyObject *self, PyObject *args)
{
ThreadMessage tMsg = {TMSG_RESTART};
CApplicationMessenger::Get().SendMessage(tMsg);
Py_INCREF(Py_None);
return Py_None;
}
// executescript() method
PyDoc_STRVAR(executeScript__doc__,
"executescript(script) -- Execute a python script.\n"
"\n"
"script : string - script filename to execute.\n"
"\n"
"example:\n"
" - xbmc.executescript('special://home/scripts/update.py')\n");
PyObject* XBMC_ExecuteScript(PyObject *self, PyObject *args)
{
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
ThreadMessage tMsg = {TMSG_EXECUTE_SCRIPT};
tMsg.strParam = cLine;
CApplicationMessenger::Get().SendMessage(tMsg);
Py_INCREF(Py_None);
return Py_None;
}
// executebuiltin() method
PyDoc_STRVAR(executeBuiltIn__doc__,
"executebuiltin(function) -- Execute a built in XBMC function.\n"
"\n"
"function : string - builtin function to execute.\n"
"wait : [opt] bool - True=Wait for end of execution, False=don't wait (Default)\n"
"\n"
"List of functions - http://wiki.xbmc.org/?title=List_of_Built_In_Functions \n"
"\n"
"NOTE: This function is executed asynchronously, so do not rely on it being done immediately\n"
"\n"
"example:\n"
" - xbmc.executebuiltin('XBMC.RunXBE(c:\\\\avalaunch.xbe)')\n");
PyObject* XBMC_ExecuteBuiltIn(PyObject *self, PyObject *args)
{
char *cLine = NULL;
bool bWait = false;
if (!PyArg_ParseTuple(args, (char*)"s|b", &cLine, &bWait)) return NULL;
CApplicationMessenger::Get().ExecBuiltIn(cLine, bWait);
Py_INCREF(Py_None);
return Py_None;
}
#ifdef HAS_HTTPAPI
// executehttpapi() method
PyDoc_STRVAR(executeHttpApi__doc__,
"executehttpapi(httpcommand) -- Execute an HTTP API command.\n"
"\n"
"httpcommand : string - http command to execute.\n"
"\n"
"List of commands - http://wiki.xbmc.org/?title=WebServerHTTP-API#The_Commands \n"
"\n"
"example:\n"
" - response = xbmc.executehttpapi('TakeScreenShot(special://temp/test.jpg,0,false,200,-1,90)')\n");
PyObject* XBMC_ExecuteHttpApi(PyObject *self, PyObject *args)
{
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
CPyThreadState pyLock;
if (!m_pXbmcHttp)
m_pXbmcHttp = new CXbmcHttp();
CStdString method = cLine;
int open, close;
CStdString parameter="", cmd=cLine, execute;
open = cmd.Find("(");
if (open>0)
{
close=cmd.length();
while (close>open && cmd.Mid(close,1)!=")")
close--;
if (close>open)
{
parameter = cmd.Mid(open + 1, close - open - 1);
parameter.Replace(",",";");
execute = cmd.Left(open);
}
else //open bracket but no close
{
pyLock.Restore();
return PyString_FromString("");
}
}
else //no parameters
execute = cmd;
CURL::Decode(parameter);
pyLock.Restore();
return PyString_FromString(CHttpApi::MethodCall(execute, parameter).c_str());
}
#endif
#ifdef HAS_JSONRPC
// executehttpapi() method
PyDoc_STRVAR(executeJSONRPC__doc__,
"executeJSONRPC(jsonrpccommand) -- Execute an JSONRPC command.\n"
"\n"
"jsonrpccommand : string - jsonrpc command to execute.\n"
"\n"
"List of commands - \n"
"\n"
"example:\n"
" - response = xbmc.executeJSONRPC('{ \"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Introspect\", \"id\": 1 }')\n");
PyObject* XBMC_ExecuteJSONRPC(PyObject *self, PyObject *args)
{
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine))
return NULL;
CStdString method = cLine;
CPythonTransport transport;
CPythonTransport::CPythonClient client;
return PyString_FromString(JSONRPC::CJSONRPC::MethodCall(method, &transport, &client).c_str());
}
#endif
// sleep() method
PyDoc_STRVAR(sleep__doc__,
"sleep(time) -- Sleeps for 'time' msec.\n"
"\n"
"time : integer - number of msec to sleep.\n"
"\n"
"*Note, This is useful if you have for example a Player class that is waiting\n"
" for onPlayBackEnded() calls.\n"
"\n"
"Throws: PyExc_TypeError, if time is not an integer.\n"
"\n"
"example:\n"
" - xbmc.sleep(2000) # sleeps for 2 seconds\n");
PyObject* XBMC_Sleep(PyObject *self, PyObject *args)
{
PyObject *pObject;
if (!PyArg_ParseTuple(args, (char*)"O", &pObject)) return NULL;
if (!PyInt_Check(pObject))
{
PyErr_Format(PyExc_TypeError, "argument must be a bool(integer) value");
return NULL;
}
long i = PyInt_AsLong(pObject);
//while(i != 0)
//{
CPyThreadState pyState;
Sleep(i);//(500);
pyState.Restore();
PyXBMC_MakePendingCalls();
//i = PyInt_AsLong(pObject);
//}
Py_INCREF(Py_None);
return Py_None;
}
// getLocalizedString() method
PyDoc_STRVAR(getLocalizedString__doc__,
"getLocalizedString(id) -- Returns a localized 'unicode string'.\n"
"\n"
"id : integer - id# for string you want to localize.\n"
"\n"
"*Note, See strings.xml in \\language\\{yourlanguage}\\ for which id\n"
" you need for a string.\n"
"\n"
"example:\n"
" - locstr = xbmc.getLocalizedString(6)\n");
PyObject* XBMC_GetLocalizedString(PyObject *self, PyObject *args)
{
int iString;
if (!PyArg_ParseTuple(args, (char*)"i", &iString)) return NULL;
CStdString label;
if (iString >= 30000 && iString <= 30999)
label = g_localizeStringsTemp.Get(iString);
else if (iString >= 32000 && iString <= 32999)
label = g_localizeStringsTemp.Get(iString);
else
label = g_localizeStrings.Get(iString);
return PyUnicode_DecodeUTF8(label.c_str(), label.size(), "replace");
}
// getSkinDir() method
PyDoc_STRVAR(getSkinDir__doc__,
"getSkinDir() -- Returns the active skin directory as a string.\n"
"\n"
"*Note, This is not the full path like 'special://home/addons/MediaCenter', but only 'MediaCenter'.\n"
"\n"
"example:\n"
" - skindir = xbmc.getSkinDir()\n");
PyObject* XBMC_GetSkinDir(PyObject *self, PyObject *args)
{
return PyString_FromString(g_guiSettings.GetString("lookandfeel.skin"));
}
// getLanguage() method
PyDoc_STRVAR(getLanguage__doc__,
"getLanguage() -- Returns the active language as a string.\n"
"\n"
"example:\n"
" - language = xbmc.getLanguage()\n");
PyObject* XBMC_GetLanguage(PyObject *self, PyObject *args)
{
return PyString_FromString(g_guiSettings.GetString("locale.language"));
}
// getIPAddress() method
PyDoc_STRVAR(getIPAddress__doc__,
"getIPAddress() -- Returns the current ip address as a string.\n"
"\n"
"example:\n"
" - ip = xbmc.getIPAddress()\n");
PyObject* XBMC_GetIPAddress(PyObject *self, PyObject *args)
{
char cTitleIP[32];
sprintf(cTitleIP, "127.0.0.1");
CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
if (iface)
return PyString_FromString(iface->GetCurrentIPAddress().c_str());
return PyString_FromString(cTitleIP);
}
// getDVDState() method
PyDoc_STRVAR(getDVDState__doc__,
"getDVDState() -- Returns the dvd state as an integer.\n"
"\n"
"return values are:\n"
" 1 : xbmc.DRIVE_NOT_READY\n"
" 16 : xbmc.TRAY_OPEN\n"
" 64 : xbmc.TRAY_CLOSED_NO_MEDIA\n"
" 96 : xbmc.TRAY_CLOSED_MEDIA_PRESENT\n"
"\n"
"example:\n"
" - dvdstate = xbmc.getDVDState()\n");
PyObject* XBMC_GetDVDState(PyObject *self, PyObject *args)
{
return PyInt_FromLong(CIoSupport::GetTrayState());
}
// getFreeMem() method
PyDoc_STRVAR(getFreeMem__doc__,
"getFreeMem() -- Returns the amount of free memory in MB as an integer.\n"
"\n"
"example:\n"
" - freemem = xbmc.getFreeMem()\n");
PyObject* XBMC_GetFreeMem(PyObject *self, PyObject *args)
{
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&stat);
return PyInt_FromLong( (long)(stat.ullAvailPhys / ( 1024 * 1024 )) );
}
// getCpuTemp() method
// ## Doesn't work right, use getInfoLabel('System.CPUTemperature') instead.
/*PyDoc_STRVAR(getCpuTemp__doc__,
"getCpuTemp() -- Returns the current cpu temperature as an integer.\n"
"\n"
"example:\n"
" - cputemp = xbmc.getCpuTemp()\n");
PyObject* XBMC_GetCpuTemp(PyObject *self, PyObject *args)
{
unsigned short cputemp;
unsigned short cpudec;
_outp(0xc004, (0x4c<<1)|0x01);
_outp(0xc008, 0x01);
_outpw(0xc000, _inpw(0xc000));
_outp(0xc002, (0) ? 0x0b : 0x0a);
while ((_inp(0xc000) & 8));
cputemp = _inpw(0xc006);
_outp(0xc004, (0x4c<<1)|0x01);
_outp(0xc008, 0x10);
_outpw(0xc000, _inpw(0xc000));
_outp(0xc002, (0) ? 0x0b : 0x0a);
while ((_inp(0xc000) & 8));
cpudec = _inpw(0xc006);
if (cpudec<10) cpudec = cpudec * 100;
if (cpudec<100) cpudec = cpudec *10;
return PyInt_FromLong((long)(cputemp + cpudec / 1000.0f));
}*/
// getInfolabel() method
PyDoc_STRVAR(getInfoLabel__doc__,
"getInfoLabel(infotag) -- Returns an InfoLabel as a string.\n"
"\n"
"infotag : string - infoTag for value you want returned.\n"
" Also multiple InfoLabels are possible e.x.:\n"
" label = xbmc.getInfoLabel('$INFO[Weather.Conditions] - thats the weather')\n"
"\n"
"List of InfoTags - http://wiki.xbmc.org/?title=InfoLabels \n"
"\n"
"example:\n"
" - label = xbmc.getInfoLabel('Weather.Conditions')\n");
PyObject* XBMC_GetInfoLabel(PyObject *self, PyObject *args)
{
std::string cret;
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
{
CPyThreadState gilRelease;
int ret = g_infoManager.TranslateString(cLine);
//doesn't seem to be a single InfoTag?
//try full blown GuiInfoLabel then
if (ret == 0)
{
CGUIInfoLabel label(cLine);
cret = label.GetLabel(0);
}
else
{
cret = g_infoManager.GetLabel(ret);
}
}
return Py_BuildValue((char*)"s", cret.c_str());
}
// getInfoImage() method
PyDoc_STRVAR(getInfoImage__doc__,
"getInfoImage(infotag) -- Returns a filename including path to the InfoImage's\n"
" thumbnail as a string.\n"
"\n"
"infotag : string - infotag for value you want returned.\n"
"\n"
"List of InfoTags - http://wiki.xbmc.org/?title=InfoLabels \n"
"\n"
"example:\n"
" - filename = xbmc.getInfoImage('Weather.Conditions')\n");
PyObject* XBMC_GetInfoImage(PyObject *self, PyObject *args)
{
std::string cret;
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
{
CPyThreadState gilRelease;
int ret = g_infoManager.TranslateString(cLine);
cret = g_infoManager.GetImage(ret, WINDOW_INVALID);
}
return Py_BuildValue((char*)"s", cret.c_str());
}
// playSFX() method
PyDoc_STRVAR(playSFX__doc__,
"playSFX(filename) -- Plays a wav file by filename\n"
"\n"
"filename : string - filename of the wav file to play.\n"
"\n"
"example:\n"
" - xbmc.playSFX('special://xbmc/scripts/dingdong.wav')\n");
PyObject* XBMC_PlaySFX(PyObject *self, PyObject *args)
{
const char *cFile = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cFile)) return NULL;
{
CPyThreadState gilRelease;
if (CFile::Exists(cFile))
{
g_audioManager.PlayPythonSound(cFile);
}
}
Py_INCREF(Py_None);
return Py_None;
}
// enableNavSounds() method
PyDoc_STRVAR(enableNavSounds__doc__,
"enableNavSounds(yesNo) -- Enables/Disables nav sounds\n"
"\n"
"yesNo : integer - enable (True) or disable (False) nav sounds\n"
"\n"
"example:\n"
" - xbmc.enableNavSounds(True)\n");
PyObject* XBMC_EnableNavSounds(PyObject *self, PyObject *args)
{
int yesNo = 1;
if (!PyArg_ParseTuple(args, (char*)"i", &yesNo)) return NULL;
g_audioManager.Enable(yesNo==1);
Py_INCREF(Py_None);
return Py_None;
}
// getCondVisibility() method
PyDoc_STRVAR(getCondVisibility__doc__,
"getCondVisibility(condition) -- Returns True (1) or False (0) as a bool.\n"
"\n"
"condition : string - condition to check.\n"
"\n"
"List of Conditions - http://wiki.xbmc.org/?title=List_of_Boolean_Conditions \n"
"\n"
"*Note, You can combine two (or more) of the above settings by using \"+\" as an AND operator,\n"
"\"|\" as an OR operator, \"!\" as a NOT operator, and \"[\" and \"]\" to bracket expressions.\n"
"\n"
"example:\n"
" - visible = xbmc.getCondVisibility('[Control.IsVisible(41) + !Control.IsVisible(12)]')\n");
PyObject* XBMC_GetCondVisibility(PyObject *self, PyObject *args)
{
char *cLine = NULL;
if (!PyArg_ParseTuple(args, (char*)"s", &cLine)) return NULL;
bool ret;
{
CPyThreadState gilRelease;
CSingleLock gc(g_graphicsContext);
int id = g_windowManager.GetTopMostModalDialogID();
if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
ret = g_infoManager.EvaluateBool(cLine,id);
}
return Py_BuildValue((char*)"b", ret);
}
// getGlobalIdleTime() method
PyDoc_STRVAR(getGlobalIdleTime__doc__,
"getGlobalIdleTime() -- Returns the elapsed idle time in seconds as an integer.\n"
"\n"
"example:\n"
" - t = xbmc.getGlobalIdleTime()");
PyObject* XBMC_GetGlobalIdleTime(PyObject *self)
{
return Py_BuildValue((char*)"i", g_application.GlobalIdleTime());
}
// getCacheThumbName function
PyDoc_STRVAR(getCacheThumbName__doc__,
"getCacheThumbName(path) -- Returns a thumb cache filename.\n"
"\n"
"path : string or unicode - path to file\n"
"\n"
"example:\n"
" - thumb = xbmc.getCacheThumbName('f:\\\\videos\\\\movie.avi')\n");
PyObject* XBMC_GetCacheThumbName(PyObject *self, PyObject *args)
{
PyObject *pObjectText;
if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
string strText;
if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
Crc32 crc;
CStdString strPath;
crc.ComputeFromLowerCase(strText);
strPath.Format("%08x.tbn", (unsigned __int32)crc);
return Py_BuildValue((char*)"s", strPath.c_str());
}
// makeLegalFilename function
PyDoc_STRVAR(makeLegalFilename__doc__,
"makeLegalFilename(filename[, fatX]) -- Returns a legal filename or path as a string.\n"
"\n"
"filename : string or unicode - filename/path to make legal\n"
"fatX : [opt] bool - True=Xbox file system(Default)\n"
"\n"
"*Note, If fatX is true you should pass a full path. If fatX is false only pass\n"
" the basename of the path.\n"
"\n"
" You can use the above as keywords for arguments and skip certain optional arguments.\n"
" Once you use a keyword, all following arguments require the keyword.\n"
"\n"
"example:\n"
" - filename = xbmc.makeLegalFilename('F:\\Trailers\\Ice Age: The Meltdown.avi')\n");
PyObject* XBMC_MakeLegalFilename(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = { "filename", "fatX", NULL };
PyObject *pObjectText;
char bIsFatX = true;
// parse arguments to constructor
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"O|b",
(char**)keywords,
&pObjectText,
&bIsFatX
))
{
return NULL;
};
CStdString strText;
if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
CStdString strFilename;
strFilename = CUtil::MakeLegalPath(strText);
return Py_BuildValue((char*)"s", strFilename.c_str());
}
// translatePath function
PyDoc_STRVAR(translatePath__doc__,
"translatePath(path) -- Returns the translated path.\n"
"\n"
"path : string or unicode - Path to format\n"
"\n"
"*Note, Only useful if you are coding for both Linux and Windows/Xbox.\n"
" e.g. Converts 'special://masterprofile/script_data' -> '/home/user/XBMC/UserData/script_data'\n"
" on Linux. Would return 'special://masterprofile/script_data' on the Xbox.\n"
"\n"
"example:\n"
" - fpath = xbmc.translatePath('special://masterprofile/script_data')\n");
PyObject* XBMC_TranslatePath(PyObject *self, PyObject *args)
{
PyObject *pObjectText;
if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
CStdString strText;
if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
CStdString strPath;
strPath = CSpecialProtocol::TranslatePath(strText);
return Py_BuildValue((char*)"s", strPath.c_str());
}
// getcleanmovietitle function
PyDoc_STRVAR(getCleanMovieTitle__doc__,
"getCleanMovieTitle(path[, usefoldername]) -- Returns a clean movie title and year string if available.\n"
"\n"
"path : string or unicode - String to clean\n"
"bool : [opt] bool - use folder names (defaults to false)\n"
"\n"
"example:\n"
" - title, year = xbmc.getCleanMovieTitle('/path/to/moviefolder/test.avi', True)\n");
PyObject* XBMC_GetCleanMovieTitle(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = { "path", "usefoldername", NULL };
PyObject *pObjectText;
bool bUseFolderName = false;
// parse arguments to constructor
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"O|b",
(char**)keywords,
&pObjectText,
&bUseFolderName
))
{
return NULL;
};
CStdString strPath;
if (!PyXBMCGetUnicodeString(strPath, pObjectText, 1)) return NULL;
CFileItem item(strPath, false);
CStdString strName = item.GetMovieName(bUseFolderName);
CStdString strTitle, strTitleAndYear, strYear;
CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, bUseFolderName);
return Py_BuildValue((char*)"s,s", strTitle.c_str(), strYear.c_str());
}
// validatePath function
PyDoc_STRVAR(validatePath__doc__,
"validatePath(path) -- Returns the validated path.\n"
"\n"
"path : string or unicode - Path to format\n"
"\n"
"*Note, Only useful if you are coding for both Linux and Windows/Xbox for fixing slash problems.\n"
" e.g. Corrects 'Z://something' -> 'Z:\\something'\n"
"\n"
"example:\n"
" - fpath = xbmc.validatePath(somepath)\n");
PyObject* XBMC_ValidatePath(PyObject *self, PyObject *args)
{
PyObject *pObjectText;
if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
CStdString strText;
if (!PyXBMCGetUnicodeString(strText, pObjectText, 1)) return NULL;
return Py_BuildValue((char*)"s", CUtil::ValidatePath(strText, true).c_str());
}
// getRegion function
PyDoc_STRVAR(getRegion__doc__,
"getRegion(id) -- Returns your regions setting as a string for the specified id.\n"
"\n"
"id : string - id of setting to return\n"
"\n"
"*Note, choices are (dateshort, datelong, time, meridiem, tempunit, speedunit)\n"
"\n"
" You can use the above as keywords for arguments.\n"
"\n"
"example:\n"
" - date_long_format = xbmc.getRegion('datelong')\n");
PyObject* XBMC_GetRegion(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = { "id", NULL };
char *id = NULL;
// parse arguments to constructor
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"s",
(char**)keywords,
&id
))
{
return NULL;
};
CStdString result;
if (strcmpi(id, "datelong") == 0)
{
result = g_langInfo.GetDateFormat(true);
result.Replace("DDDD", "%A");
result.Replace("MMMM", "%B");
result.Replace("D", "%d");
result.Replace("YYYY", "%Y");
}
else if (strcmpi(id, "dateshort") == 0)
{
result = g_langInfo.GetDateFormat(false);
result.Replace("MM", "%m");
result.Replace("DD", "%d");
result.Replace("YYYY", "%Y");
}
else if (strcmpi(id, "tempunit") == 0)
result = g_langInfo.GetTempUnitString();
else if (strcmpi(id, "speedunit") == 0)
result = g_langInfo.GetSpeedUnitString();
else if (strcmpi(id, "time") == 0)
{
result = g_langInfo.GetTimeFormat();
result.Replace("H", "%H");
result.Replace("h", "%I");
result.Replace("mm", "%M");
result.Replace("ss", "%S");
result.Replace("xx", "%p");
}
else if (strcmpi(id, "meridiem") == 0)
result.Format("%s/%s", g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_AM), g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_PM));
return Py_BuildValue((char*)"s", result.c_str());
}
// getSupportedMedia function
PyDoc_STRVAR(getSupportedMedia__doc__,
"getSupportedMedia(media) -- Returns the supported file types for the specific media as a string.\n"
"\n"
"media : string - media type\n"
"\n"
"*Note, media type can be (video, music, picture).\n"
"\n"
" The return value is a pipe separated string of filetypes (eg. '.mov|.avi').\n"
"\n"
" You can use the above as keywords for arguments.\n"
"\n"
"example:\n"
" - mTypes = xbmc.getSupportedMedia('video')\n");
PyObject* XBMC_GetSupportedMedia(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = { "media", NULL };
char *media = NULL;
// parse arguments to constructor
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"s",
(char**)keywords,
&media
))
{
return NULL;
};
CStdString result;
if (strcmpi(media, "video") == 0)
result = g_settings.m_videoExtensions;
else if (strcmpi(media, "music") == 0)
result = g_settings.m_musicExtensions;
else if (strcmpi(media, "picture") == 0)
result = g_settings.m_pictureExtensions;
else
{
PyErr_SetString(PyExc_ValueError, "media = (video, music, picture)");
return NULL;
}
return Py_BuildValue((char*)"s", result.c_str());
}
// skinHasImage function
PyDoc_STRVAR(skinHasImage__doc__,
"skinHasImage(image) -- Returns True if the image file exists in the skin.\n"
"\n"
"image : string - image filename\n"
"\n"
"*Note, If the media resides in a subfolder include it. (eg. home-myfiles\\\\home-myfiles2.png)\n"
"\n"
" You can use the above as keywords for arguments.\n"
"\n"
"example:\n"
" - exists = xbmc.skinHasImage('ButtonFocusedTexture.png')\n");
PyObject* XBMC_SkinHasImage(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = { "image", NULL };
char *image = NULL;
// parse arguments to constructor
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"s",
(char**)keywords,
&image
))
{
return NULL;
};
bool exists = g_TextureManager.HasTexture(image);
return Py_BuildValue((char*)"b", exists);
}
// startServer() method
PyDoc_STRVAR(startServer__doc__,
"startServer(typ, bStart, bWait) -- start or stop a server.\n"
"\n"
"typ : integer - use SERVER_* constants\n"
"\n"
"bStart : bool - start (True) or stop (False) a server\n"
"\n"
"bWait : [opt] bool - wait on stop before returning (not supported by all servers)\n"
"\n"
"returnValue : bool - True or False\n"
"example:\n"
" - xbmc.startServer(xbmc.SERVER_AIRPLAYSERVER, False)\n");
PyObject* XBMC_StartServer(PyObject *self, PyObject *args, PyObject *kwds)
{
static const char *keywords[] = {
"typ",
"bStart",
"bWait",
NULL};
int iTyp = 0;
char bStart = false;
char bWait = false;
bool ret = false;
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
(char*)"ib|b",
(char**)keywords,
&iTyp,
&bStart,
&bWait))
{
return NULL;
}
{
CPyThreadState save;
ret = g_application.StartServer((CApplication::ESERVERS)iTyp, bStart != 0, bWait != 0);
}
return Py_BuildValue((char*)"b", ret);
}
// define c functions to be used in python here
PyMethodDef xbmcMethods[] = {
{(char*)"output", (PyCFunction)XBMC_Output, METH_VARARGS|METH_KEYWORDS, output__doc__},
{(char*)"log", (PyCFunction)XBMC_Log, METH_VARARGS|METH_KEYWORDS, log__doc__},
{(char*)"executescript", (PyCFunction)XBMC_ExecuteScript, METH_VARARGS, executeScript__doc__},
{(char*)"executebuiltin", (PyCFunction)XBMC_ExecuteBuiltIn, METH_VARARGS, executeBuiltIn__doc__},
{(char*)"sleep", (PyCFunction)XBMC_Sleep, METH_VARARGS, sleep__doc__},
{(char*)"shutdown", (PyCFunction)XBMC_Shutdown, METH_VARARGS, shutdown__doc__},
{(char*)"restart", (PyCFunction)XBMC_Restart, METH_VARARGS, restart__doc__},
{(char*)"getSkinDir", (PyCFunction)XBMC_GetSkinDir, METH_VARARGS, getSkinDir__doc__},
{(char*)"getLocalizedString", (PyCFunction)XBMC_GetLocalizedString, METH_VARARGS, getLocalizedString__doc__},