Python updates:
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Initialize Python thingies.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #pragma warning (disable : 4786)
40 #endif //WIN32
41
42 #include "KX_PythonInit.h"
43
44 #include "SCA_IInputDevice.h"
45 #include "SCA_PropertySensor.h"
46 #include "SCA_RandomActuator.h"
47 #include "KX_ConstraintActuator.h"
48 #include "KX_IpoActuator.h"
49 #include "RAS_IRasterizer.h"
50 #include "RAS_ICanvas.h"
51 #include "MT_Vector3.h"
52 #include "MT_Point3.h"
53 #include "ListValue.h"
54 #include "KX_Scene.h"
55 #include "SND_DeviceManager.h"
56
57 #include "KX_PyMath.h"
58
59 // FIXME: Enable for access to blender python modules.  This is disabled because
60 // python has dependencies on a lot of other modules and is a pain to link.
61 //#define USE_BLENDER_PYTHON
62 #ifdef USE_BLENDER_PYTHON
63 //#include "BPY_extern.h"
64 #endif 
65
66 static void setSandbox(TPythonSecurityLevel level);
67
68
69 // 'local' copy of canvas ptr, for window height/width python scripts
70 static RAS_ICanvas* gp_Canvas = NULL;
71 static KX_Scene*        gp_KetsjiScene = NULL;
72 static RAS_IRasterizer* gp_Rasterizer = NULL;
73
74 /* Macro for building the keyboard translation */
75 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(SCA_IInputDevice::KX_##name))
76 #define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
77 /* For the defines for types from logic bricks, we do stuff explicitly... */
78 #define KX_MACRO_addTypesToDict(dict, name, name2) PyDict_SetItemString(dict, #name, PyInt_FromLong(name2))
79
80
81 // temporarily python stuff, will be put in another place later !
82 #include "KX_Python.h"
83 #include "SCA_PythonController.h"
84 // List of methods defined in the module
85
86 static PyObject* ErrorObject;
87 STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)";
88
89 static PyObject* gPyGetRandomFloat(PyObject* self,
90                                                                                  PyObject* args, 
91                                                                                  PyObject* kwds)
92 {
93         return PyFloat_FromDouble(MT_random());
94 }
95
96
97
98
99
100 void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector3 &pos)
101 {
102         PyObject* pylist;
103         PyArg_ParseTuple(args,"O",&pylist);
104
105         pos = MT_Vector3FromPyList(pylist);
106 }
107
108 void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector4 &vec)
109 {
110         PyObject* pylist;
111         PyArg_ParseTuple(args,"O",&pylist);
112
113         vec = MT_Vector4FromPyList(pylist);
114 }
115
116
117 static PyObject* gPySetGravity(PyObject* self,
118                                                                                  PyObject* args, 
119                                                                                  PyObject* kwds)
120 {
121         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
122         GlobalConvertPythonVectorArg(args, vec);
123
124         if (gp_KetsjiScene)
125                 gp_KetsjiScene->SetGravity(vec);
126         
127         Py_Return;
128 }
129
130
131 static bool usedsp = false;
132
133 // this gets a pointer to an array filled with floats
134 static PyObject* gPyGetSpectrum(PyObject* self,
135                                                                 PyObject* args, 
136                                                                 PyObject* kwds)
137 {
138         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
139
140         PyObject* resultlist = PyList_New(512);
141
142         if (audiodevice)
143         {
144                 if (!usedsp)
145                 {
146                         audiodevice->StartUsingDSP();
147                         usedsp = true;
148                 }
149                         
150                 float* spectrum = audiodevice->GetSpectrum();
151
152                 for (int index = 0; index < 512; index++)
153                 {
154                         PyList_SetItem(resultlist, index, PyFloat_FromDouble(spectrum[index]));
155                 }
156         }
157
158         return resultlist;
159 }
160
161
162
163 static PyObject* gPyStartDSP(PyObject* self,
164                                                 PyObject* args, 
165                                                 PyObject* kwds)
166 {
167         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
168
169         if (audiodevice)
170         {
171                 if (!usedsp)
172                 {
173                         audiodevice->StartUsingDSP();
174                         usedsp = true;
175                         Py_Return;
176                 }
177         }
178         return NULL;
179 }
180
181
182
183 static PyObject* gPyStopDSP(PyObject* self,
184                                            PyObject* args, 
185                                            PyObject* kwds)
186 {
187         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
188
189         if (audiodevice)
190         {
191                 if (usedsp)
192                 {
193                         audiodevice->StopUsingDSP();
194                         usedsp = false;
195                         Py_Return;
196                 }
197         }
198         return NULL;
199 }
200
201
202 static STR_String gPyGetCurrentScene_doc =  
203 "getCurrentScene()\n"
204 "Gets a reference to the current scene.\n";
205 static PyObject* gPyGetCurrentScene(PyObject* self,
206                                            PyObject* args, 
207                                            PyObject* kwds)
208 {
209         Py_INCREF(gp_KetsjiScene);
210         return (PyObject*) gp_KetsjiScene;
211 }
212                                            
213
214
215 static struct PyMethodDef game_methods[] = {
216         {"getCurrentController",
217         (PyCFunction) SCA_PythonController::sPyGetCurrentController,
218         METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__},
219         {"getCurrentScene", (PyCFunction) gPyGetCurrentScene,
220         METH_VARARGS, gPyGetCurrentScene_doc.Ptr()},
221         {"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator,
222         METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__},
223         {"getRandomFloat",(PyCFunction) gPyGetRandomFloat,
224         METH_VARARGS,gPyGetRandomFloat_doc.Ptr()},
225         {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"},
226         {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_VARARGS,"get audio spectrum"},
227         {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"},
228         {NULL, (PyCFunction) NULL, 0, NULL }
229 };
230
231
232 static PyObject* gPyGetWindowHeight(PyObject* self, 
233                                                                                  PyObject* args, 
234                                                                                  PyObject* kwds)
235 {
236         int height = (gp_Canvas ? gp_Canvas->GetHeight() : 0);
237
238                 PyObject* heightval = PyInt_FromLong(height);
239                 return heightval;
240 }
241
242
243
244 static PyObject* gPyGetWindowWidth(PyObject* self, 
245                                                                                  PyObject* args, 
246                                                                                  PyObject* kwds)
247 {
248                 
249
250         int width = (gp_Canvas ? gp_Canvas->GetWidth() : 0);
251         
252                 PyObject* widthval = PyInt_FromLong(width);
253                 return widthval;
254 }
255
256
257
258 // temporarility visibility thing, will be moved to rasterizer/renderer later
259 bool gUseVisibilityTemp = false;
260
261 static PyObject* gPyEnableVisibility(PyObject* self, 
262                                                                                  PyObject* args, 
263                                                                                  PyObject* kwds)
264 {
265         int visible;
266         if (PyArg_ParseTuple(args,"i",&visible))
267         {
268             gUseVisibilityTemp = (visible != 0);
269         }
270         else
271         {
272           Py_Return;         
273         }
274    Py_Return;
275 }
276
277
278
279 static PyObject* gPyShowMouse(PyObject* self, 
280                                                                                  PyObject* args, 
281                                                                                  PyObject* kwds)
282 {
283         int visible;
284         if (PyArg_ParseTuple(args,"i",&visible))
285         {
286             if (visible)
287                 {
288                         if (gp_Canvas)
289                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
290                 } else
291                 {
292                         if (gp_Canvas)
293                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
294                 }
295         }
296         
297    Py_Return;
298 }
299
300
301
302 static PyObject* gPySetMousePosition(PyObject* self, 
303                                                                                  PyObject* args, 
304                                                                                  PyObject* kwds)
305 {
306         int x,y;
307         if (PyArg_ParseTuple(args,"ii",&x,&y))
308         {
309             if (gp_Canvas)
310                         gp_Canvas->SetMousePosition(x,y);
311         }
312         
313    Py_Return;
314 }
315
316
317
318 static PyObject* gPySetBackgroundColor(PyObject* self, 
319                                                                                  PyObject* args, 
320                                                                                  PyObject* kwds)
321 {
322         
323         MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.);
324         GlobalConvertPythonVectorArg(args, vec);
325
326         if (gp_Canvas)
327         {
328                 gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
329         }
330    Py_Return;
331 }
332
333
334
335 static PyObject* gPySetMistColor(PyObject* self, 
336                                                                                  PyObject* args, 
337                                                                                  PyObject* kwds)
338 {
339         
340         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
341         GlobalConvertPythonVectorArg(args, vec);
342
343         if (gp_Rasterizer)
344         {
345                 gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]);
346         }
347    Py_Return;
348 }
349
350
351
352 static PyObject* gPySetMistStart(PyObject* self, 
353                                                                                  PyObject* args, 
354                                                                                  PyObject* kwds)
355 {
356
357         float miststart;
358         if (PyArg_ParseTuple(args,"f",&miststart))
359         {
360                 if (gp_Rasterizer)
361                 {
362                         gp_Rasterizer->SetFogStart(miststart);
363                 }
364         }
365    Py_Return;
366 }
367
368
369
370 static PyObject* gPySetMistEnd(PyObject* self, 
371                                                                                  PyObject* args, 
372                                                                                  PyObject* kwds)
373 {
374
375         float mistend;
376         if (PyArg_ParseTuple(args,"f",&mistend))
377         {
378                 if (gp_Rasterizer)
379                 {
380                         gp_Rasterizer->SetFogEnd(mistend);
381                 }
382         }
383    Py_Return;
384 }
385
386
387
388 static PyObject* gPyMakeScreenshot(PyObject* self,
389                                                                         PyObject* args,
390                                                                         PyObject* kwds)
391 {
392         char* filename;
393         if (PyArg_ParseTuple(args,"s",&filename))
394         {
395                 if (gp_Canvas)
396                 {
397                         gp_Canvas->MakeScreenShot(filename);
398                 }
399         }
400         Py_Return;
401 }
402
403
404
405 STR_String      gPyGetWindowHeight__doc__="getWindowHeight doc";
406 STR_String      gPyGetWindowWidth__doc__="getWindowWidth doc";
407 STR_String      gPyEnableVisibility__doc__="enableVisibility doc";
408 STR_String      gPyMakeScreenshot__doc__="make Screenshot doc";
409 STR_String      gPyShowMouse__doc__="showMouse(bool visible)";
410 STR_String      gPySetMousePosition__doc__="setMousePosition(int x,int y)";
411
412 static struct PyMethodDef rasterizer_methods[] = {
413   {"getWindowWidth",(PyCFunction) gPyGetWindowWidth,
414    METH_VARARGS, gPyGetWindowWidth__doc__.Ptr()},
415    {"getWindowHeight",(PyCFunction) gPyGetWindowHeight,
416    METH_VARARGS, gPyGetWindowHeight__doc__.Ptr()},
417   {"makeScreenshot",(PyCFunction)gPyMakeScreenshot,
418         METH_VARARGS, gPyMakeScreenshot__doc__.Ptr()},
419    {"enableVisibility",(PyCFunction) gPyEnableVisibility,
420    METH_VARARGS, gPyEnableVisibility__doc__.Ptr()},
421         {"showMouse",(PyCFunction) gPyShowMouse,
422    METH_VARARGS, gPyShowMouse__doc__.Ptr()},
423    {"setMousePosition",(PyCFunction) gPySetMousePosition,
424    METH_VARARGS, gPySetMousePosition__doc__.Ptr()},
425   {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_VARARGS,"set Background Color (rgb)"},
426   {"setMistColor",(PyCFunction)gPySetMistColor,METH_VARARGS,"set Mist Color (rgb)"},
427   {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
428   {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
429   
430   { NULL, (PyCFunction) NULL, 0, NULL }
431 };
432
433
434
435 // Initialization function for the module (*must* be called initGameLogic)
436
437 static char GameLogic_module_documentation[] =
438 "This is the Python API for the game engine of GameLogic"
439 ;
440
441 static char Rasterizer_module_documentation[] =
442 "This is the Python API for the game engine of Rasterizer"
443 ;
444
445
446
447 PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook
448 {
449         PyObject* m;
450         PyObject* d;
451
452         gp_KetsjiScene = scene;
453
454         gUseVisibilityTemp=false;
455
456         // Create the module and add the functions
457         m = Py_InitModule4("GameLogic", game_methods,
458                                            GameLogic_module_documentation,
459                                            (PyObject*)NULL,PYTHON_API_VERSION);
460
461         // Add some symbolic constants to the module
462         d = PyModule_GetDict(m);
463
464         ErrorObject = PyString_FromString("GameLogic.error");
465         PyDict_SetItemString(d, "error", ErrorObject);
466
467         // XXXX Add constants here
468         /* To use logic bricks, we need some sort of constants. Here, we associate */
469         /* constants and sumbolic names. Add them to dictionary d.                 */
470
471         /* 1. true and false: needed for everyone                                  */
472         KX_MACRO_addTypesToDict(d, KX_TRUE,  SCA_ILogicBrick::KX_TRUE);
473         KX_MACRO_addTypesToDict(d, KX_FALSE, SCA_ILogicBrick::KX_FALSE);
474
475         /* 2. Property sensor                                                      */
476         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EQUAL,      SCA_PropertySensor::KX_PROPSENSOR_EQUAL);
477         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_NOTEQUAL,   SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL);
478         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_INTERVAL,   SCA_PropertySensor::KX_PROPSENSOR_INTERVAL);
479         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_CHANGED,    SCA_PropertySensor::KX_PROPSENSOR_CHANGED);
480         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EXPRESSION, SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION);
481
482         /* 3. Constraint actuator                                                  */
483         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX);
484         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY);
485         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ);
486         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX);
487         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY);
488         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ);
489
490         /* 4. Ipo actuator, simple part                                            */
491         KX_MACRO_addTypesToDict(d, KX_IPOACT_PLAY,     KX_IpoActuator::KX_ACT_IPO_PLAY);
492         KX_MACRO_addTypesToDict(d, KX_IPOACT_PINGPONG, KX_IpoActuator::KX_ACT_IPO_PINGPONG);
493         KX_MACRO_addTypesToDict(d, KX_IPOACT_FLIPPER,  KX_IpoActuator::KX_ACT_IPO_FLIPPER);
494         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPSTOP, KX_IpoActuator::KX_ACT_IPO_LOOPSTOP);
495         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPEND,  KX_IpoActuator::KX_ACT_IPO_LOOPEND);
496
497         /* 5. Random distribution types                                            */
498         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_CONST,      SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST);
499         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_UNIFORM,    SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM);
500         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_BERNOUILLI, SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI);
501         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_CONST,       SCA_RandomActuator::KX_RANDOMACT_INT_CONST);
502         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_UNIFORM,     SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM);
503         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_POISSON,     SCA_RandomActuator::KX_RANDOMACT_INT_POISSON);
504         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_CONST,     SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST);
505         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_UNIFORM,   SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM);
506         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL,    SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
507         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
508
509         // Check for errors
510         if (PyErr_Occurred())
511     {
512                 Py_FatalError("can't initialize module GameLogic");
513     }
514
515         return d;
516 }
517
518
519
520 // Python Sandbox code
521 // override builtin functions import() and open()
522
523
524 PyObject *KXpy_open(PyObject *self, PyObject *args)
525 {
526         PyErr_SetString(PyExc_RuntimeError, "Sandbox: open() function disabled!\nGame Scripts should not use this function.");
527         return NULL;
528 }
529
530
531
532 PyObject *KXpy_import(PyObject *self, PyObject *args)
533 {
534         char *name;
535         PyObject *globals = NULL;
536         PyObject *locals = NULL;
537         PyObject *fromlist = NULL;
538         PyObject *l, *m, *n;
539
540         if (!PyArg_ParseTuple(args, "s|OOO:m_import",
541                 &name, &globals, &locals, &fromlist))
542             return NULL;
543
544         /* check for builtin modules */
545         m = PyImport_AddModule("sys");
546         l = PyObject_GetAttrString(m, "builtin_module_names");
547         n = PyString_FromString(name);
548         
549         if (PySequence_Contains(l, n)) {
550                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
551         }
552
553         /* quick hack for GamePython modules 
554                 TODO: register builtin modules properly by ExtendInittab */
555         if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") ||
556                 !strcmp(name, "Rasterizer")) {
557                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
558         }
559                 
560         PyErr_Format(PyExc_ImportError,
561                  "Import of external Module %.20s not allowed.", name);
562         return NULL;
563
564 }
565
566
567
568 static PyMethodDef meth_open[] = {
569         { "open", KXpy_open, METH_VARARGS,
570                 "(disabled)"}
571 };
572
573
574 static PyMethodDef meth_import[] = {
575         { "import", KXpy_import, METH_VARARGS,
576                 "our own import"}
577 };
578
579
580
581 //static PyObject *g_oldopen = 0;
582 //static PyObject *g_oldimport = 0;
583 //static int g_security = 0;
584
585
586 void setSandbox(TPythonSecurityLevel level)
587 {
588     PyObject *m = PyImport_AddModule("__builtin__");
589     PyObject *d = PyModule_GetDict(m);
590         PyObject *meth = PyCFunction_New(meth_open, NULL);
591
592         switch (level) {
593         case psl_Highest:
594                 //if (!g_security) {
595                         //g_oldopen = PyDict_GetItemString(d, "open");
596                         PyDict_SetItemString(d, "open", meth);
597                         meth = PyCFunction_New(meth_import, NULL);
598                         PyDict_SetItemString(d, "__import__", meth);
599                         //g_security = level;
600                 //}
601                 break;
602         /*
603         case psl_Lowest:
604                 if (g_security) {
605                         PyDict_SetItemString(d, "open", g_oldopen);
606                         PyDict_SetItemString(d, "__import__", g_oldimport);
607                         g_security = level;
608                 }
609         */
610         default:
611                 break;
612         }
613 }
614
615 /**
616  * Python is not initialised.
617  */
618 PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level)
619 {
620         STR_String pname = progname;
621         Py_SetProgramName(pname.Ptr());
622         Py_NoSiteFlag=1;
623         Py_FrozenFlag=1;
624         Py_Initialize();
625
626         //importBlenderModules()
627         
628         setSandbox(level);
629
630         PyObject* moduleobj = PyImport_AddModule("__main__");
631         return PyModule_GetDict(moduleobj);
632 }
633
634 void exitGamePlayerPythonScripting()
635 {
636         Py_Finalize();
637 }
638
639 /**
640  * Python is already initialized.
641  */
642 PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level)
643 {
644         STR_String pname = progname;
645         Py_SetProgramName(pname.Ptr());
646         Py_NoSiteFlag=1;
647         Py_FrozenFlag=1;
648
649         setSandbox(level);
650
651         PyObject* moduleobj = PyImport_AddModule("__main__");
652         return PyModule_GetDict(moduleobj);
653 }
654
655
656
657 void exitGamePythonScripting()
658 {
659 }
660
661
662
663 PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas)
664 {
665         gp_Canvas = canvas;
666         gp_Rasterizer = rasty;
667
668
669   PyObject* m;
670   PyObject* d;
671
672   // Create the module and add the functions
673   m = Py_InitModule4("Rasterizer", rasterizer_methods,
674                      Rasterizer_module_documentation,
675                      (PyObject*)NULL,PYTHON_API_VERSION);
676
677   // Add some symbolic constants to the module
678   d = PyModule_GetDict(m);
679   ErrorObject = PyString_FromString("Rasterizer.error");
680   PyDict_SetItemString(d, "error", ErrorObject);
681
682   // XXXX Add constants here
683
684   // Check for errors
685   if (PyErr_Occurred())
686     {
687       Py_FatalError("can't initialize module Rasterizer");
688     }
689
690   return d;
691 }
692
693
694
695 /* ------------------------------------------------------------------------- */
696 /* GameKeys: symbolic constants for key mapping                              */
697 /* ------------------------------------------------------------------------- */
698
699 static char GameKeys_module_documentation[] =
700 "This modules provides defines for key-codes"
701 ;
702
703
704
705 static struct PyMethodDef gamekeys_methods[] = {
706         { NULL, (PyCFunction) NULL, 0, NULL }
707 };
708
709
710
711 PyObject* initGameKeys()
712 {
713         PyObject* m;
714         PyObject* d;
715
716         // Create the module and add the functions
717         m = Py_InitModule4("GameKeys", gamekeys_methods,
718                                            GameKeys_module_documentation,
719                                            (PyObject*)NULL,PYTHON_API_VERSION);
720
721         // Add some symbolic constants to the module
722         d = PyModule_GetDict(m);
723
724         // XXXX Add constants here
725
726         KX_MACRO_addTypesToDict(d, AKEY, SCA_IInputDevice::KX_AKEY);
727         KX_MACRO_addTypesToDict(d, BKEY, SCA_IInputDevice::KX_BKEY);
728         KX_MACRO_addTypesToDict(d, CKEY, SCA_IInputDevice::KX_CKEY);
729         KX_MACRO_addTypesToDict(d, DKEY, SCA_IInputDevice::KX_DKEY);
730         KX_MACRO_addTypesToDict(d, EKEY, SCA_IInputDevice::KX_EKEY);
731         KX_MACRO_addTypesToDict(d, FKEY, SCA_IInputDevice::KX_FKEY);
732         KX_MACRO_addTypesToDict(d, GKEY, SCA_IInputDevice::KX_GKEY);
733         KX_MACRO_addTypesToDict(d, HKEY, SCA_IInputDevice::KX_HKEY);
734         KX_MACRO_addTypesToDict(d, IKEY, SCA_IInputDevice::KX_IKEY);
735         KX_MACRO_addTypesToDict(d, JKEY, SCA_IInputDevice::KX_JKEY);
736         KX_MACRO_addTypesToDict(d, KKEY, SCA_IInputDevice::KX_KKEY);
737         KX_MACRO_addTypesToDict(d, LKEY, SCA_IInputDevice::KX_LKEY);
738         KX_MACRO_addTypesToDict(d, MKEY, SCA_IInputDevice::KX_MKEY);
739         KX_MACRO_addTypesToDict(d, NKEY, SCA_IInputDevice::KX_NKEY);
740         KX_MACRO_addTypesToDict(d, OKEY, SCA_IInputDevice::KX_OKEY);
741         KX_MACRO_addTypesToDict(d, PKEY, SCA_IInputDevice::KX_PKEY);
742         KX_MACRO_addTypesToDict(d, QKEY, SCA_IInputDevice::KX_QKEY);
743         KX_MACRO_addTypesToDict(d, RKEY, SCA_IInputDevice::KX_RKEY);
744         KX_MACRO_addTypesToDict(d, SKEY, SCA_IInputDevice::KX_SKEY);
745         KX_MACRO_addTypesToDict(d, TKEY, SCA_IInputDevice::KX_TKEY);
746         KX_MACRO_addTypesToDict(d, UKEY, SCA_IInputDevice::KX_UKEY);
747         KX_MACRO_addTypesToDict(d, VKEY, SCA_IInputDevice::KX_VKEY);
748         KX_MACRO_addTypesToDict(d, WKEY, SCA_IInputDevice::KX_WKEY);
749         KX_MACRO_addTypesToDict(d, XKEY, SCA_IInputDevice::KX_XKEY);
750         KX_MACRO_addTypesToDict(d, YKEY, SCA_IInputDevice::KX_YKEY);
751         KX_MACRO_addTypesToDict(d, ZKEY, SCA_IInputDevice::KX_ZKEY);
752         
753         KX_MACRO_addTypesToDict(d, ZEROKEY, SCA_IInputDevice::KX_ZEROKEY);              
754         KX_MACRO_addTypesToDict(d, ONEKEY, SCA_IInputDevice::KX_ONEKEY);                
755         KX_MACRO_addTypesToDict(d, TWOKEY, SCA_IInputDevice::KX_TWOKEY);                
756         KX_MACRO_addTypesToDict(d, THREEKEY, SCA_IInputDevice::KX_THREEKEY);
757         KX_MACRO_addTypesToDict(d, FOURKEY, SCA_IInputDevice::KX_FOURKEY);              
758         KX_MACRO_addTypesToDict(d, FIVEKEY, SCA_IInputDevice::KX_FIVEKEY);              
759         KX_MACRO_addTypesToDict(d, SIXKEY, SCA_IInputDevice::KX_SIXKEY);                
760         KX_MACRO_addTypesToDict(d, SEVENKEY, SCA_IInputDevice::KX_SEVENKEY);
761         KX_MACRO_addTypesToDict(d, EIGHTKEY, SCA_IInputDevice::KX_EIGHTKEY);
762         KX_MACRO_addTypesToDict(d, NINEKEY, SCA_IInputDevice::KX_NINEKEY);              
763                 
764         KX_MACRO_addTypesToDict(d, CAPSLOCKKEY, SCA_IInputDevice::KX_CAPSLOCKKEY);
765                 
766         KX_MACRO_addTypesToDict(d, LEFTCTRLKEY, SCA_IInputDevice::KX_LEFTCTRLKEY);      
767         KX_MACRO_addTypesToDict(d, LEFTALTKEY, SCA_IInputDevice::KX_LEFTALTKEY);                
768         KX_MACRO_addTypesToDict(d, RIGHTALTKEY, SCA_IInputDevice::KX_RIGHTALTKEY);      
769         KX_MACRO_addTypesToDict(d, RIGHTCTRLKEY, SCA_IInputDevice::KX_RIGHTCTRLKEY);    
770         KX_MACRO_addTypesToDict(d, RIGHTSHIFTKEY, SCA_IInputDevice::KX_RIGHTSHIFTKEY);  
771         KX_MACRO_addTypesToDict(d, LEFTSHIFTKEY, SCA_IInputDevice::KX_LEFTSHIFTKEY);
772                 
773         KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
774         KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
775         KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
776         KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
777         KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);              
778         KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
779         KX_MACRO_addTypesToDict(d, DELKEY, SCA_IInputDevice::KX_DELKEY);
780         KX_MACRO_addTypesToDict(d, SEMICOLONKEY, SCA_IInputDevice::KX_SEMICOLONKEY);
781         KX_MACRO_addTypesToDict(d, PERIODKEY, SCA_IInputDevice::KX_PERIODKEY);          
782         KX_MACRO_addTypesToDict(d, COMMAKEY, SCA_IInputDevice::KX_COMMAKEY);            
783         KX_MACRO_addTypesToDict(d, QUOTEKEY, SCA_IInputDevice::KX_QUOTEKEY);            
784         KX_MACRO_addTypesToDict(d, ACCENTGRAVEKEY, SCA_IInputDevice::KX_ACCENTGRAVEKEY);        
785         KX_MACRO_addTypesToDict(d, MINUSKEY, SCA_IInputDevice::KX_MINUSKEY);            
786         KX_MACRO_addTypesToDict(d, SLASHKEY, SCA_IInputDevice::KX_SLASHKEY);            
787         KX_MACRO_addTypesToDict(d, BACKSLASHKEY, SCA_IInputDevice::KX_BACKSLASHKEY);
788         KX_MACRO_addTypesToDict(d, EQUALKEY, SCA_IInputDevice::KX_EQUALKEY);            
789         KX_MACRO_addTypesToDict(d, LEFTBRACKETKEY, SCA_IInputDevice::KX_LEFTBRACKETKEY);        
790         KX_MACRO_addTypesToDict(d, RIGHTBRACKETKEY, SCA_IInputDevice::KX_RIGHTBRACKETKEY);      
791                 
792         KX_MACRO_addTypesToDict(d, LEFTARROWKEY, SCA_IInputDevice::KX_LEFTARROWKEY);
793         KX_MACRO_addTypesToDict(d, DOWNARROWKEY, SCA_IInputDevice::KX_DOWNARROWKEY);
794         KX_MACRO_addTypesToDict(d, RIGHTARROWKEY, SCA_IInputDevice::KX_RIGHTARROWKEY);  
795         KX_MACRO_addTypesToDict(d, UPARROWKEY, SCA_IInputDevice::KX_UPARROWKEY);                
796         
797         KX_MACRO_addTypesToDict(d, PAD2 , SCA_IInputDevice::KX_PAD2);
798         KX_MACRO_addTypesToDict(d, PAD4 , SCA_IInputDevice::KX_PAD4);
799         KX_MACRO_addTypesToDict(d, PAD6 , SCA_IInputDevice::KX_PAD6);
800         KX_MACRO_addTypesToDict(d, PAD8 , SCA_IInputDevice::KX_PAD8);
801                 
802         KX_MACRO_addTypesToDict(d, PAD1 , SCA_IInputDevice::KX_PAD1);
803         KX_MACRO_addTypesToDict(d, PAD3 , SCA_IInputDevice::KX_PAD3);
804         KX_MACRO_addTypesToDict(d, PAD5 , SCA_IInputDevice::KX_PAD5);
805         KX_MACRO_addTypesToDict(d, PAD7 , SCA_IInputDevice::KX_PAD7);
806         KX_MACRO_addTypesToDict(d, PAD9 , SCA_IInputDevice::KX_PAD9);
807                 
808         KX_MACRO_addTypesToDict(d, PADPERIOD, SCA_IInputDevice::KX_PADPERIOD);
809         KX_MACRO_addTypesToDict(d, PADSLASHKEY, SCA_IInputDevice::KX_PADSLASHKEY);
810         KX_MACRO_addTypesToDict(d, PADASTERKEY, SCA_IInputDevice::KX_PADASTERKEY);
811                 
812                 
813         KX_MACRO_addTypesToDict(d, PAD0, SCA_IInputDevice::KX_PAD0);
814         KX_MACRO_addTypesToDict(d, PADMINUS, SCA_IInputDevice::KX_PADMINUS);
815         KX_MACRO_addTypesToDict(d, PADENTER, SCA_IInputDevice::KX_PADENTER);
816         KX_MACRO_addTypesToDict(d, PADPLUSKEY, SCA_IInputDevice::KX_PADPLUSKEY);
817                 
818                 
819         KX_MACRO_addTypesToDict(d, F1KEY , SCA_IInputDevice::KX_F1KEY);
820         KX_MACRO_addTypesToDict(d, F2KEY , SCA_IInputDevice::KX_F2KEY);
821         KX_MACRO_addTypesToDict(d, F3KEY , SCA_IInputDevice::KX_F3KEY);
822         KX_MACRO_addTypesToDict(d, F4KEY , SCA_IInputDevice::KX_F4KEY);
823         KX_MACRO_addTypesToDict(d, F5KEY , SCA_IInputDevice::KX_F5KEY);
824         KX_MACRO_addTypesToDict(d, F6KEY , SCA_IInputDevice::KX_F6KEY);
825         KX_MACRO_addTypesToDict(d, F7KEY , SCA_IInputDevice::KX_F7KEY);
826         KX_MACRO_addTypesToDict(d, F8KEY , SCA_IInputDevice::KX_F8KEY);
827         KX_MACRO_addTypesToDict(d, F9KEY , SCA_IInputDevice::KX_F9KEY);
828         KX_MACRO_addTypesToDict(d, F10KEY, SCA_IInputDevice::KX_F10KEY);
829         KX_MACRO_addTypesToDict(d, F11KEY, SCA_IInputDevice::KX_F11KEY);
830         KX_MACRO_addTypesToDict(d, F12KEY, SCA_IInputDevice::KX_F12KEY);
831                 
832         KX_MACRO_addTypesToDict(d, PAUSEKEY, SCA_IInputDevice::KX_PAUSEKEY);
833         KX_MACRO_addTypesToDict(d, INSERTKEY, SCA_IInputDevice::KX_INSERTKEY);
834         KX_MACRO_addTypesToDict(d, HOMEKEY , SCA_IInputDevice::KX_HOMEKEY);
835         KX_MACRO_addTypesToDict(d, PAGEUPKEY, SCA_IInputDevice::KX_PAGEUPKEY);
836         KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
837         KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
838
839
840         // Check for errors
841         if (PyErr_Occurred())
842     {
843                 Py_FatalError("can't initialize module GameKeys");
844     }
845
846         return d;
847 }
848
849 void PHY_SetActiveScene(class KX_Scene* scene)
850 {
851         gp_KetsjiScene = scene;
852 }