49
/* Only one instance of the state per process. */
48
50
static PyObject* quitfunctions = NULL;
49
static PyObject* PyExc_SDLError;
51
static int sdl_was_init = 0;
50
53
static void install_parachute (void);
51
54
static void uninstall_parachute (void);
55
static void _quit (void);
52
56
static void atexit_quit (void);
53
57
static int PyGame_Video_AutoInit (void);
54
58
static void PyGame_Video_AutoQuit (void);
67
71
if (compiled.major != linked->major || compiled.minor != linked->minor)
70
sprintf (err, "SDL compiled with version %d.%d.%d, linked to %d.%d.%d",
73
PyErr_Format(PyExc_RuntimeError, "SDL compiled with version %d.%d.%d, linked to %d.%d.%d",
71
74
compiled.major, compiled.minor, compiled.patch,
72
75
linked->major, linked->minor, linked->patch);
73
PyErr_SetString (PyExc_RuntimeError, err);
98
register_quit (PyObject* self, PyObject* arg)
100
register_quit (PyObject* self, PyObject* value)
102
if (!PyArg_ParseTuple (arg, "O", &quitfunc))
105
102
if (!quitfunctions)
107
104
quitfunctions = PyList_New (0);
108
105
if (!quitfunctions)
111
PyList_Append (quitfunctions, quitfunc);
108
PyList_Append (quitfunctions, value);
127
124
/*nice to initialize timer, so startup time will reflec init() time*/
125
sdl_was_init = SDL_Init (
129
126
#if defined(WITH_THREAD) && !defined(MS_WIN32) && defined(SDL_INIT_EVENTTHREAD)
130
127
SDL_INIT_EVENTTHREAD |
133
SDL_INIT_NOPARACHUTE);
130
SDL_INIT_NOPARACHUTE) == 0;
136
133
/* initialize all pygame modules */
174
171
atexit_quit (void)
173
PyGame_Video_AutoQuit ();
175
/* Maybe it is safe to call SDL_quit more than once after an SDL_Init,
176
but this is undocumented. So play it safe and only call after a
186
get_sdl_version (PyObject* self)
188
const SDL_version *v;
190
v = SDL_Linked_Version ();
191
return Py_BuildValue ("iii", v->major, v->minor, v->patch);
195
get_sdl_byteorder (PyObject *self)
197
return PyLong_FromLong (SDL_BYTEORDER);
201
quit (PyObject* self)
177
211
PyObject* privatefuncs;
214
if (!quitfunctions) {
183
218
privatefuncs = quitfunctions;
184
219
quitfunctions = NULL;
200
235
Py_DECREF (privatefuncs);
202
PyGame_Video_AutoQuit ();
207
get_sdl_version (PyObject* self)
209
const SDL_version *v;
211
v = SDL_Linked_Version ();
212
return Py_BuildValue ("iii", v->major, v->minor, v->patch);
216
get_sdl_byteorder (PyObject *self)
218
return PyLong_FromLong (SDL_BYTEORDER);
222
quit (PyObject* self)
228
240
/* internal C API utility functions */
271
static int FloatFromObj (PyObject* obj, float* val)
284
FloatFromObj (PyObject* obj, float* val)
286
float f= (float)PyFloat_AsDouble (obj);
275
if (PyNumber_Check (obj))
277
if (!(floatobj = PyNumber_Float (obj)))
279
*val = (float) PyFloat_AsDouble (floatobj);
280
Py_DECREF (floatobj);
288
if (f==-1 && PyErr_Occurred()) {
316
327
UintFromObj (PyObject* obj, Uint32* val)
320
331
if (PyNumber_Check (obj))
322
if (!(intobj = PyNumber_Int (obj)))
333
if (!(longobj = PyNumber_Long (obj)))
324
*val = (Uint32) PyInt_AsLong (intobj);
335
*val = (Uint32) PyLong_AsUnsignedLong (longobj);
332
343
UintFromObjIndex (PyObject* obj, int _index, Uint32* val)
381
392
get_error (PyObject* self)
383
return PyString_FromString (SDL_GetError ());
394
return Text_FromUTF8 (SDL_GetError ());
398
set_error (PyObject *s, PyObject *args)
400
char *errstring = NULL;
402
if (!PyArg_ParseTuple (args, "s", &errstring))
405
SDL_SetError(errstring);
386
413
/*video init needs to be here, because of it's
387
414
*important init order priority
405
432
module = PyImport_ImportModule ("pygame.macosx");
435
printf("ERROR: pygame.macosx import FAILED\n");
409
rval = PyObject_CallMethod (module, "init", "");
439
rval = PyObject_CallMethod (module, "Video_AutoInit", "");
410
440
Py_DECREF (module);
443
printf("ERROR: pygame.macosx.Video_AutoInit() call FAILED\n");
414
447
status = PyObject_IsTrue (rval);
415
448
Py_DECREF (rval);
493
530
/* Set a handler for any fatal signal not already handled */
494
531
for (i = 0; fatal_signals[i]; ++i)
496
ohandler = signal (fatal_signals[i], pygame_parachute);
533
ohandler = (void(*)(int))signal (fatal_signals[i], pygame_parachute);
497
534
if (ohandler != SIG_DFL)
498
535
signal (fatal_signals[i], ohandler);
538
#if defined(SIGALRM) && defined(HAVE_SIGACTION)
501
539
{/* Set SIGALRM to be ignored -- necessary on Solaris */
502
540
struct sigaction action, oaction;
503
541
/* Set SIG_IGN action */
525
565
/* Remove a handler for any fatal signal handled */
526
566
for (i = 0; fatal_signals[i]; ++i)
528
ohandler = signal (fatal_signals[i], SIG_DFL);
568
ohandler = (void(*)(int))signal (fatal_signals[i], SIG_DFL);
529
569
if (ohandler != pygame_parachute)
530
570
signal (fatal_signals[i], ohandler);
534
575
/* bind functions to python */
545
static PyMethodDef init__builtins__[] =
586
static PyMethodDef _base_methods[] =
547
588
{ "init", (PyCFunction) init, METH_NOARGS, DOC_PYGAMEINIT },
548
589
{ "quit", (PyCFunction) quit, METH_NOARGS, DOC_PYGAMEQUIT },
549
{ "register_quit", register_quit, METH_VARARGS, DOC_PYGAMEREGISTERQUIT },
590
{ "register_quit", register_quit, METH_O, DOC_PYGAMEREGISTERQUIT },
550
591
{ "get_error", (PyCFunction) get_error, METH_NOARGS, DOC_PYGAMEGETERROR },
592
{ "set_error", (PyCFunction) set_error, METH_VARARGS, DOC_PYGAMESETERROR },
551
593
{ "get_sdl_version", (PyCFunction) get_sdl_version, METH_NOARGS,
552
594
DOC_PYGAMEGETSDLVERSION },
553
595
{ "get_sdl_byteorder", (PyCFunction) get_sdl_byteorder, METH_NOARGS,
557
599
{ NULL, NULL, 0, NULL }
604
static int is_loaded = 0;
563
605
PyObject *module, *dict, *apiobj;
606
PyObject *atexit, *atexit_register = NULL, *quit, *rval;
607
PyObject *PyExc_SDLError;
564
609
static void* c_api[PYGAMEAPI_BASE_NUMSLOTS];
612
static struct PyModuleDef _module = {
613
PyModuleDef_HEAD_INIT,
618
NULL, NULL, NULL, NULL
623
/* import need modules. Do this first so if there is an error
624
the module is not loaded.
626
atexit = PyImport_ImportModule ("atexit");
630
atexit_register = PyObject_GetAttrString (atexit, "register");
632
if (!atexit_register) {
566
637
/* create the module */
567
module = Py_InitModule3 ("base", init__builtins__, DOC_PYGAME);
639
module = PyModule_Create (&_module);
641
module = Py_InitModule3 (MODPREFIX "base", _base_methods, DOC_PYGAME);
643
if (module == NULL) {
568
646
dict = PyModule_GetDict (module);
570
648
/* create the exceptions */
571
649
PyExc_SDLError = PyErr_NewException ("pygame.error", PyExc_RuntimeError,
573
PyDict_SetItemString (dict, "error", PyExc_SDLError);
651
if (PyExc_SDLError == NULL) {
652
Py_XDECREF (atexit_register);
656
ecode = PyDict_SetItemString (dict, "error", PyExc_SDLError);
574
657
Py_DECREF (PyExc_SDLError);
659
Py_XDECREF (atexit_register);
576
664
/* export the c api */
577
665
c_api[0] = PyExc_SDLError;
588
676
c_api[11] = PyGame_Video_AutoInit;
589
677
c_api[12] = RGBAFromObj;
590
678
apiobj = PyCObject_FromVoidPtr (c_api, NULL);
591
PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
679
if (apiobj == NULL) {
680
Py_XDECREF (atexit_register);
684
ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
592
685
Py_DECREF (apiobj);
594
/*some intiialization*/
595
Py_AtExit (atexit_quit);
596
install_parachute ();
687
Py_XDECREF (atexit_register);
693
/*some intialization*/
694
quit = PyObject_GetAttrString (module, "quit");
695
if (quit == NULL) { /* assertion */
696
Py_DECREF (atexit_register);
700
rval = PyObject_CallFunctionObjArgs (atexit_register, quit, NULL);
701
Py_DECREF (atexit_register);
708
Py_AtExit (atexit_quit);
710
install_parachute ();
598
SDL_RegisterApp ("pygame", 0, GetModuleHandle (NULL));
715
SDL_RegisterApp ("pygame", 0, GetModuleHandle (NULL));
600
717
#if defined(macintosh)
601
718
#if(!defined(__MWERKS__) && !TARGET_API_MAC_CARBON)
602
SDL_InitQuickDraw (&qd);
719
SDL_InitQuickDraw (&qd);
724
MODINIT_RETURN (module);