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