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