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