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