~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to src/base.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#define NO_PYGAME_C_API
23
23
#define PYGAMEAPI_BASE_INTERNAL
24
24
#include "pygame.h"
 
25
#include "pgcompat.h"
25
26
#include "pygamedocs.h"
26
27
#include <signal.h>
27
28
 
45
46
#endif
46
47
#endif
47
48
 
 
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;
 
52
 
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);
66
70
 
67
71
    if (compiled.major != linked->major || compiled.minor != linked->minor)
68
72
    {
69
 
        char err[1024];
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);
74
76
        return 0;
75
77
    }
76
78
    return 1;
95
97
}
96
98
 
97
99
static PyObject*
98
 
register_quit (PyObject* self, PyObject* arg)
 
100
register_quit (PyObject* self, PyObject* value)
99
101
{
100
 
    PyObject* quitfunc;
101
 
 
102
 
    if (!PyArg_ParseTuple (arg, "O", &quitfunc))
103
 
        return NULL;
104
 
 
105
102
    if (!quitfunctions)
106
103
    {
107
104
        quitfunctions = PyList_New (0);
108
105
        if (!quitfunctions)
109
106
            return NULL;
110
107
    }
111
 
    PyList_Append (quitfunctions, quitfunc);
 
108
    PyList_Append (quitfunctions, value);
112
109
 
113
110
    Py_RETURN_NONE;
114
111
}
125
122
 
126
123
 
127
124
    /*nice to initialize timer, so startup time will reflec init() time*/
128
 
    SDL_Init (
 
125
    sdl_was_init = SDL_Init (
129
126
#if defined(WITH_THREAD) && !defined(MS_WIN32) && defined(SDL_INIT_EVENTTHREAD)
130
127
        SDL_INIT_EVENTTHREAD |
131
128
#endif
132
129
        SDL_INIT_TIMER |
133
 
        SDL_INIT_NOPARACHUTE);
 
130
        SDL_INIT_NOPARACHUTE) == 0;
134
131
 
135
132
 
136
133
    /* initialize all pygame modules */
173
170
static void
174
171
atexit_quit (void)
175
172
{
 
173
    PyGame_Video_AutoQuit ();
 
174
 
 
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
 
177
       successful SDL_Init.
 
178
    */
 
179
    if (sdl_was_init) {
 
180
        sdl_was_init = 0;
 
181
        SDL_Quit ();
 
182
    }
 
183
}
 
184
 
 
185
static PyObject*
 
186
get_sdl_version (PyObject* self)
 
187
{
 
188
    const SDL_version *v;
 
189
        
 
190
    v = SDL_Linked_Version ();
 
191
    return Py_BuildValue ("iii", v->major, v->minor, v->patch);
 
192
}
 
193
 
 
194
static PyObject*
 
195
get_sdl_byteorder (PyObject *self)
 
196
{
 
197
    return PyLong_FromLong (SDL_BYTEORDER);
 
198
}
 
199
 
 
200
static PyObject*
 
201
quit (PyObject* self)
 
202
{
 
203
    _quit ();
 
204
    Py_RETURN_NONE;
 
205
}
 
206
 
 
207
static void
 
208
_quit (void)
 
209
{
176
210
    PyObject* quit;
177
211
    PyObject* privatefuncs;
178
212
    int num;
179
213
 
180
 
    if (!quitfunctions)
 
214
    if (!quitfunctions) {
181
215
        return;
 
216
    }
182
217
 
183
218
    privatefuncs = quitfunctions;
184
219
    quitfunctions = NULL;
199
234
    }
200
235
    Py_DECREF (privatefuncs);
201
236
 
202
 
    PyGame_Video_AutoQuit ();
203
 
    SDL_Quit ();
204
 
}
205
 
 
206
 
static PyObject*
207
 
get_sdl_version (PyObject* self)
208
 
{
209
 
    const SDL_version *v;
210
 
        
211
 
    v = SDL_Linked_Version ();
212
 
    return Py_BuildValue ("iii", v->major, v->minor, v->patch);
213
 
}
214
 
 
215
 
static PyObject*
216
 
get_sdl_byteorder (PyObject *self)
217
 
{
218
 
    return PyLong_FromLong (SDL_BYTEORDER);
219
 
}
220
 
 
221
 
static PyObject*
222
 
quit (PyObject* self)
223
 
{
224
237
    atexit_quit ();
225
 
    Py_RETURN_NONE;
226
238
}
227
239
 
228
240
/* internal C API utility functions */
268
280
    return 1;
269
281
}
270
282
 
271
 
static int FloatFromObj (PyObject* obj, float* val)
 
283
static int
 
284
FloatFromObj (PyObject* obj, float* val)
272
285
{
273
 
    PyObject* floatobj;
 
286
    float f= (float)PyFloat_AsDouble (obj);
274
287
 
275
 
    if (PyNumber_Check (obj))
276
 
    {
277
 
        if (!(floatobj = PyNumber_Float (obj)))
278
 
            return 0;
279
 
        *val = (float) PyFloat_AsDouble (floatobj);
280
 
        Py_DECREF (floatobj);
281
 
        return 1;
282
 
    }
283
 
    return 0;
 
288
    if (f==-1 && PyErr_Occurred()) {
 
289
                PyErr_Clear ();
 
290
        return 0;
 
291
        }
 
292
    
 
293
    *val = f;
 
294
    return 1;
284
295
}
285
296
 
286
297
static int
315
326
static int
316
327
UintFromObj (PyObject* obj, Uint32* val)
317
328
{
318
 
    PyObject* intobj;
 
329
    PyObject* longobj;
319
330
 
320
331
    if (PyNumber_Check (obj))
321
332
    {
322
 
        if (!(intobj = PyNumber_Int (obj)))
 
333
        if (!(longobj = PyNumber_Long (obj)))
323
334
            return 0;
324
 
        *val = (Uint32) PyInt_AsLong (intobj);
325
 
        Py_DECREF (intobj);
 
335
        *val = (Uint32) PyLong_AsUnsignedLong (longobj);
 
336
        Py_DECREF (longobj);
326
337
        return 1;
327
338
    }
328
339
    return 0;
329
340
}
330
341
 
331
 
static Uint32
 
342
static int
332
343
UintFromObjIndex (PyObject* obj, int _index, Uint32* val)
333
344
{
334
345
    int result = 0;
380
391
static PyObject*
381
392
get_error (PyObject* self)
382
393
{
383
 
    return PyString_FromString (SDL_GetError ());
384
 
}
 
394
    return Text_FromUTF8 (SDL_GetError ());
 
395
}
 
396
 
 
397
static PyObject*
 
398
set_error (PyObject *s, PyObject *args)
 
399
{
 
400
    char *errstring = NULL;
 
401
 
 
402
    if (!PyArg_ParseTuple (args, "s", &errstring))
 
403
        return NULL;
 
404
 
 
405
    SDL_SetError(errstring);
 
406
 
 
407
    Py_RETURN_NONE;
 
408
}
 
409
 
 
410
 
 
411
 
385
412
 
386
413
/*video init needs to be here, because of it's
387
414
 *important init order priority
404
431
        PyObject *rval;
405
432
        module = PyImport_ImportModule ("pygame.macosx");
406
433
        if (!module)
 
434
        {
 
435
                printf("ERROR: pygame.macosx import FAILED\n");
407
436
            return -1;
 
437
        }
408
438
 
409
 
        rval = PyObject_CallMethod (module, "init", "");
 
439
        rval = PyObject_CallMethod (module, "Video_AutoInit", "");
410
440
        Py_DECREF (module);
411
441
        if (!rval)
 
442
        {
 
443
                printf("ERROR: pygame.macosx.Video_AutoInit() call FAILED\n");
412
444
            return -1;
 
445
        }
413
446
 
414
447
        status = PyObject_IsTrue (rval);
415
448
        Py_DECREF (rval);
430
463
static void
431
464
pygame_parachute (int sig)
432
465
{
 
466
#ifdef HAVE_SIGNAL_H
433
467
    char* signaltype;
434
468
    
435
469
    signal (sig, SIG_DFL);
460
494
        break;
461
495
    }
462
496
 
463
 
    atexit_quit ();
 
497
    _quit ();
464
498
    Py_FatalError (signaltype);
 
499
#endif    
465
500
}
466
501
 
 
502
 
467
503
static int fatal_signals[] =
468
504
{
469
505
    SIGSEGV,
483
519
static void
484
520
install_parachute (void)
485
521
{
 
522
#ifdef HAVE_SIGNAL_H
486
523
    int i;
487
524
    void (*ohandler)(int);
488
525
 
493
530
    /* Set a handler for any fatal signal not already handled */
494
531
    for (i = 0; fatal_signals[i]; ++i)
495
532
    {
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);
499
536
    }
500
 
#ifdef SIGALRM
 
537
    
 
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 */
509
547
            sigaction (SIGALRM, &oaction, NULL);
510
548
    }
511
549
#endif
 
550
#endif    
512
551
    return;
513
552
}
514
553
 
515
554
static void
516
555
uninstall_parachute (void)
517
556
{
 
557
#ifdef HAVE_SIGNAL_H
518
558
    int i;
519
559
    void (*ohandler)(int);
520
560
 
525
565
    /* Remove a handler for any fatal signal handled */
526
566
    for (i = 0; fatal_signals[i]; ++i)
527
567
    {
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);
531
571
    }
 
572
#endif    
532
573
}
533
574
 
534
575
/* bind functions to python */
542
583
    Py_RETURN_NONE;
543
584
}
544
585
 
545
 
static PyMethodDef init__builtins__[] =
 
586
static PyMethodDef _base_methods[] =
546
587
{
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 }
558
600
};
559
601
 
560
 
PYGAME_EXPORT
561
 
void initbase (void)
 
602
MODINIT_DEFINE(base)
562
603
{
 
604
    static int is_loaded = 0;
563
605
    PyObject *module, *dict, *apiobj;
 
606
    PyObject *atexit, *atexit_register = NULL, *quit, *rval;
 
607
    PyObject *PyExc_SDLError;
 
608
    int ecode;
564
609
    static void* c_api[PYGAMEAPI_BASE_NUMSLOTS];
565
610
 
 
611
#if PY3
 
612
    static struct PyModuleDef _module = {
 
613
        PyModuleDef_HEAD_INIT,
 
614
        "base",
 
615
        "",
 
616
        -1,
 
617
        _base_methods,
 
618
        NULL, NULL, NULL, NULL
 
619
    };
 
620
#endif
 
621
 
 
622
    if (!is_loaded) {
 
623
        /* import need modules. Do this first so if there is an error
 
624
           the module is not loaded.
 
625
        */
 
626
        atexit = PyImport_ImportModule ("atexit");
 
627
        if (!atexit) {
 
628
            MODINIT_ERROR;
 
629
        }
 
630
        atexit_register = PyObject_GetAttrString (atexit, "register");
 
631
        Py_DECREF (atexit);
 
632
        if (!atexit_register) {
 
633
            MODINIT_ERROR;
 
634
        }
 
635
    }
 
636
 
566
637
    /* create the module */
567
 
    module = Py_InitModule3 ("base", init__builtins__, DOC_PYGAME);
 
638
#if PY3
 
639
    module = PyModule_Create (&_module);
 
640
#else
 
641
    module = Py_InitModule3 (MODPREFIX "base", _base_methods, DOC_PYGAME);
 
642
#endif
 
643
    if (module == NULL) {
 
644
        MODINIT_ERROR;
 
645
    }
568
646
    dict = PyModule_GetDict (module);
569
647
 
570
648
    /* create the exceptions */
571
649
    PyExc_SDLError = PyErr_NewException ("pygame.error", PyExc_RuntimeError,
572
650
                                         NULL);
573
 
    PyDict_SetItemString (dict, "error", PyExc_SDLError);
 
651
    if (PyExc_SDLError == NULL) {
 
652
        Py_XDECREF (atexit_register);
 
653
        DECREF_MOD (module);
 
654
        MODINIT_ERROR;
 
655
    }
 
656
    ecode = PyDict_SetItemString (dict, "error", PyExc_SDLError);
574
657
    Py_DECREF (PyExc_SDLError);
 
658
    if (ecode) {
 
659
        Py_XDECREF (atexit_register);
 
660
        DECREF_MOD (module);
 
661
        MODINIT_ERROR;
 
662
    }
575
663
 
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);
 
681
        DECREF_MOD (module);
 
682
        MODINIT_ERROR;
 
683
    }
 
684
    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
592
685
    Py_DECREF (apiobj);
593
 
 
594
 
    /*some intiialization*/
595
 
    Py_AtExit (atexit_quit);
596
 
    install_parachute ();
 
686
    if (ecode) {
 
687
        Py_XDECREF (atexit_register);
 
688
        DECREF_MOD (module);
 
689
        MODINIT_ERROR;
 
690
    }
 
691
 
 
692
    if (!is_loaded) {
 
693
        /*some intialization*/
 
694
        quit = PyObject_GetAttrString (module, "quit");
 
695
        if (quit == NULL) {  /* assertion */
 
696
            Py_DECREF (atexit_register);
 
697
            DECREF_MOD (module);
 
698
            MODINIT_ERROR;
 
699
        }
 
700
        rval = PyObject_CallFunctionObjArgs (atexit_register, quit, NULL);
 
701
        Py_DECREF (atexit_register);
 
702
        Py_DECREF (quit);
 
703
        if (rval == NULL) {
 
704
            DECREF_MOD (module);
 
705
            MODINIT_ERROR;
 
706
        }
 
707
        Py_DECREF (rval);
 
708
        Py_AtExit (atexit_quit);
 
709
#ifdef HAVE_SIGNAL_H    
 
710
        install_parachute ();
 
711
#endif
 
712
 
 
713
 
597
714
#ifdef MS_WIN32
598
 
    SDL_RegisterApp ("pygame", 0, GetModuleHandle (NULL));
 
715
        SDL_RegisterApp ("pygame", 0, GetModuleHandle (NULL));
599
716
#endif
600
717
#if defined(macintosh)
601
718
#if(!defined(__MWERKS__) && !TARGET_API_MAC_CARBON)
602
 
    SDL_InitQuickDraw (&qd);
603
 
#endif
604
 
#endif
 
719
        SDL_InitQuickDraw (&qd);
 
720
#endif
 
721
#endif
 
722
        }
 
723
    is_loaded = 1;
 
724
    MODINIT_RETURN (module);
605
725
}