~ubuntu-branches/ubuntu/warty/pygame/warty

« back to all changes in this revision

Viewing changes to src/event.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-17 17:09:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040917170953-caomeukd8awvvpwv
Tags: 1.6-0.2ubuntu1
Add missing build-depends: python

Show diffs side-by-side

added added

removed removed

Lines of Context:
152
152
        }
153
153
}
154
154
 
 
155
#ifdef Py_USING_UNICODE
 
156
 
 
157
static PyObject* our_unichr(long uni)
 
158
{
 
159
        static PyObject* bltin_unichr = NULL;
 
160
 
 
161
        if (bltin_unichr == NULL) {
 
162
                PyObject* bltins;
 
163
 
 
164
                bltins = PyImport_ImportModule("__builtin__");
 
165
                bltin_unichr = PyObject_GetAttrString(bltins, "unichr");
 
166
 
 
167
                Py_DECREF(bltins);
 
168
        }
 
169
 
 
170
        return PyEval_CallFunction(bltin_unichr, "(l)", uni);
 
171
}
 
172
 
 
173
static PyObject* our_empty_ustr(void)
 
174
{
 
175
        static PyObject* empty_ustr = NULL;
 
176
 
 
177
        if (empty_ustr == NULL) {
 
178
                PyObject* bltins;
 
179
                PyObject* bltin_unicode;
 
180
 
 
181
                bltins = PyImport_ImportModule("__builtin__");
 
182
                bltin_unicode = PyObject_GetAttrString(bltins, "unicode");
 
183
                empty_ustr = PyEval_CallFunction(bltin_unicode, "(s)", "");
 
184
 
 
185
                Py_DECREF(bltin_unicode);
 
186
                Py_DECREF(bltins);
 
187
        }
 
188
 
 
189
        Py_INCREF(empty_ustr);
 
190
 
 
191
        return empty_ustr;
 
192
}
 
193
 
 
194
#else
 
195
 
 
196
static PyObject* our_unichr(long uni)
 
197
{
 
198
        return PyInt_FromLong(uni);
 
199
}
 
200
 
 
201
static PyObject* our_empty_ustr(void)
 
202
{
 
203
        return PyInt_FromLong(0);
 
204
}
 
205
 
 
206
#endif /* Py_USING_UNICODE */
155
207
 
156
208
static PyObject* dict_from_event(SDL_Event* event)
157
209
{
166
218
                        return dict;
167
219
        }
168
220
 
169
 
 
170
221
        if(!(dict = PyDict_New()))
171
222
                return NULL;
172
223
        switch(event->type)
177
228
                break;
178
229
        case SDL_KEYDOWN:
179
230
                if(event->key.keysym.unicode)
180
 
                        insobj(dict, "unicode", PyUnicode_FromUnicode(
181
 
                                                        (Py_UNICODE*)&event->key.keysym.unicode,
182
 
                                                        event->key.keysym.unicode > 0));
 
231
                        insobj(dict, "unicode", our_unichr(event->key.keysym.unicode));
183
232
                else
184
 
                        insobj(dict, "unicode",
185
 
                                                        PyUnicode_FromObject(PyString_FromString("")));
 
233
                        insobj(dict, "unicode", our_empty_ustr());
186
234
        case SDL_KEYUP:
187
235
                insobj(dict, "key", PyInt_FromLong(event->key.keysym.sym));
188
236
                insobj(dict, "mod", PyInt_FromLong(event->key.keysym.mod));
223
271
                hx = hy = 0;
224
272
                if(event->jhat.value&SDL_HAT_UP) hy = 1;
225
273
                else if(event->jhat.value&SDL_HAT_DOWN) hy = -1;
226
 
                if(event->jhat.value&SDL_HAT_LEFT) hx = 1;
 
274
                if(event->jhat.value&SDL_HAT_RIGHT) hx = 1;
227
275
                else if(event->jhat.value&SDL_HAT_LEFT) hx = -1;
228
276
                insobj(dict, "value", Py_BuildValue("(ii)", hx, hy));
229
277
                break;
255
303
{
256
304
        PyEventObject* e = (PyEventObject*)self;
257
305
        Py_XDECREF(e->dict);
258
 
        PyObject_DEL(self);     
 
306
        PyObject_DEL(self);
259
307
}
260
308
 
261
309
 
313
361
    /*DOC*/    "lookups will be passed through to the Event's dictionary values.\n"
314
362
    /*DOC*/    "\n"
315
363
    /*DOC*/    "While debugging and experimenting, you can print the Event\n"
316
 
    /*DOC*/    "objects for a quick display of its type and members.\n"    __SECRET_COLON__ 
 
364
    /*DOC*/    "objects for a quick display of its type and members.\n" __SECRET_COLON__
317
365
    /*DOC*/    "Events that come from the system will have a guaranteed set of\n"
318
366
    /*DOC*/    "member items based on the type. Here is a list of the Event members\n"
319
367
    /*DOC*/    "that are defined with each type.<br><table align=center>"
334
382
    /*DOC*/    "<tr><td><b>USEREVENT</b></td><td>code</td></tr></table>\n"
335
383
    /*DOC*/ ;
336
384
 
 
385
static int event_nonzero(PyEventObject *self)
 
386
{
 
387
        return self->type != SDL_NOEVENT;
 
388
}
337
389
 
 
390
static PyNumberMethods event_as_number = {
 
391
        (binaryfunc)NULL,               /*add*/
 
392
        (binaryfunc)NULL,               /*subtract*/
 
393
        (binaryfunc)NULL,               /*multiply*/
 
394
        (binaryfunc)NULL,               /*divide*/
 
395
        (binaryfunc)NULL,               /*remainder*/
 
396
        (binaryfunc)NULL,               /*divmod*/
 
397
        (ternaryfunc)NULL,              /*power*/
 
398
        (unaryfunc)NULL,                /*negative*/
 
399
        (unaryfunc)NULL,                /*pos*/
 
400
        (unaryfunc)NULL,                /*abs*/
 
401
        (inquiry)event_nonzero, /*nonzero*/
 
402
        (unaryfunc)NULL,                /*invert*/
 
403
        (binaryfunc)NULL,               /*lshift*/
 
404
        (binaryfunc)NULL,               /*rshift*/
 
405
        (binaryfunc)NULL,               /*and*/
 
406
        (binaryfunc)NULL,               /*xor*/
 
407
        (binaryfunc)NULL,               /*or*/
 
408
        (coercion)NULL,                 /*coerce*/
 
409
        (unaryfunc)NULL,                /*int*/
 
410
        (unaryfunc)NULL,                /*long*/
 
411
        (unaryfunc)NULL,                /*float*/
 
412
        (unaryfunc)NULL,                /*oct*/
 
413
        (unaryfunc)NULL,                /*hex*/
 
414
};
338
415
 
339
416
 
340
417
static PyTypeObject PyEvent_Type =
350
427
        NULL,                                   /*setattr*/
351
428
        NULL,                                   /*compare*/
352
429
        event_str,                              /*repr*/
353
 
        NULL,                                   /*as_number*/
 
430
        &event_as_number,               /*as_number*/
354
431
        NULL,                                   /*as_sequence*/
355
432
        NULL,                                   /*as_mapping*/
356
433
        (hashfunc)NULL,                 /*hash*/
366
443
{
367
444
        PyEventObject* e;
368
445
        e = PyObject_NEW(PyEventObject, &PyEvent_Type);
 
446
        if(!e) return NULL;
369
447
 
370
 
        if(e)
 
448
        if(event)
371
449
        {
372
450
                e->type = event->type;
373
451
                e->dict = dict_from_event(event);
374
452
        }
 
453
        else
 
454
        {
 
455
                e->type = SDL_NOEVENT;
 
456
                e->dict = PyDict_New();
 
457
        }
375
458
        return (PyObject*)e;
376
459
}
377
460
 
397
480
 
398
481
 
399
482
    /*DOC*/ static char doc_Event[] =
400
 
    /*DOC*/    "pygame.event.Event(type, dict, [keyword_args]) -> Event\n"
 
483
    /*DOC*/    "pygame.event.Event(type, [dict], [keyword_args]) -> Event\n"
401
484
    /*DOC*/    "create new event object\n"
402
485
    /*DOC*/    "\n"
403
486
    /*DOC*/    "Creates a new event object. The type should be one of SDL's\n"
405
488
    /*DOC*/    "the keys that will be members of the new event.\n"
406
489
    /*DOC*/    "\n"
407
490
    /*DOC*/    "Also, instead of passing a dictionary to create the event\n"
408
 
    /*DOC*/    "members, you also/or pass a list of keyword arguments that\n"
409
 
    /*DOC*/    "will become data members in the new event.\n"
 
491
    /*DOC*/    "members, you can pass keyword arguments that will become the\n"
 
492
    /*DOC*/    "attributes of the new event.\n"
410
493
    /*DOC*/ ;
411
494
 
412
495
static PyObject* Event(PyObject* self, PyObject* arg, PyObject* keywords)
514
597
    /*DOC*/    "pygame.event.pump() -> None\n"
515
598
    /*DOC*/    "update the internal messages\n"
516
599
    /*DOC*/    "\n"
517
 
    /*DOC*/    "Pumping the message queue is important if you are not getting\n"
518
 
    /*DOC*/    "events off the message queue. The pump will allow pygame to\n"
519
 
    /*DOC*/    "communicate with the window manager, which helps keep your\n"
520
 
    /*DOC*/    "application responsive, as well as updating the state for various\n"
521
 
    /*DOC*/    "input devices.\n"
 
600
    /*DOC*/    "For each frame of your game, you will need to make some sort\n"
 
601
    /*DOC*/    "of call to the event queue. This ensures your program can internally\n"
 
602
    /*DOC*/    "interact with the rest of the operating system. If you are not using\n"
 
603
    /*DOC*/    "other event functions in your game, you should call pump() to allow\n"
 
604
    /*DOC*/    "pygame to handle internal actions.\n"
 
605
    /*DOC*/    "\n"
 
606
    /*DOC*/    "There are important things that must be dealt with internally in the\n"
 
607
    /*DOC*/    "event queue. The main window may need to be repainted. Certain joysticks\n"
 
608
    /*DOC*/    "must be polled for their values. If you fail to make a call to the event\n"
 
609
    /*DOC*/    "queue for too long, the system may decide your program has locked up.\n"
522
610
    /*DOC*/ ;
523
611
 
524
612
static PyObject* pump(PyObject* self, PyObject* args)
579
667
static PyObject* poll(PyObject* self, PyObject* args)
580
668
{
581
669
        SDL_Event event;
582
 
        
 
670
 
583
671
        if(!PyArg_ParseTuple(args, ""))
584
672
                return NULL;
585
673
 
586
674
        VIDEO_INIT_CHECK();
587
675
 
588
 
        if(!SDL_PollEvent(&event))
589
 
                return PyEvent_New2(SDL_NOEVENT, NULL);
590
 
 
591
 
        return PyEvent_New(&event);
592
 
}
593
 
 
594
 
 
595
 
    /*DOC*/ static char doc_get[] =
 
676
        if(SDL_PollEvent(&event))
 
677
                return PyEvent_New(&event);
 
678
        return PyEvent_New(NULL);
 
679
}
 
680
 
 
681
 
 
682
    /*DOC*/ static char doc_event_clear[] =
 
683
    /*DOC*/    "pygame.event.clear([type]) -> None\n"
 
684
    /*DOC*/    "remove all of an event type from the queue\n"
 
685
    /*DOC*/    "\n"
 
686
    /*DOC*/    "Pass this a type of event to discard, and it will\n"
 
687
    /*DOC*/    "remove all matching event types from the queue. If no\n"
 
688
    /*DOC*/    "types are passed, this will remove all the events from the queue.\n"
 
689
    /*DOC*/    "You may also optionally pass a sequence of event types. For\n"
 
690
    /*DOC*/    "example, to remove all the mouse motion events from the queue, you\n"
 
691
    /*DOC*/    "would call, 'pygame.event.clear(MOUSEMOTION)'.\n"
 
692
    /*DOC*/ ;
 
693
 
 
694
static PyObject* event_clear(PyObject* self, PyObject* args)
 
695
{
 
696
        SDL_Event event;
 
697
        int mask = 0;
 
698
        int loop, num;
 
699
        PyObject* type;
 
700
        int val;
 
701
 
 
702
        if(PyTuple_Size(args) != 0 && PyTuple_Size(args) != 1)
 
703
                return RAISE(PyExc_ValueError, "get requires 0 or 1 argument");
 
704
 
 
705
        VIDEO_INIT_CHECK();
 
706
 
 
707
        if(PyTuple_Size(args) == 0)
 
708
                mask = SDL_ALLEVENTS;
 
709
        else
 
710
        {
 
711
                type = PyTuple_GET_ITEM(args, 0);
 
712
                if(PySequence_Check(type))
 
713
                {
 
714
                        num = PySequence_Size(type);
 
715
                        for(loop=0; loop<num; ++loop)
 
716
                        {
 
717
                                if(!IntFromObjIndex(type, loop, &val))
 
718
                                        return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
 
719
                                mask |= SDL_EVENTMASK(val);
 
720
                        }
 
721
                }
 
722
                else if(IntFromObj(type, &val))
 
723
                        mask = SDL_EVENTMASK(val);
 
724
                else
 
725
                        return RAISE(PyExc_TypeError, "get type must be numeric or a sequence");
 
726
        }
 
727
 
 
728
        SDL_PumpEvents();
 
729
 
 
730
        while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, mask) == 1)
 
731
        {}
 
732
 
 
733
        RETURN_NONE;
 
734
}
 
735
 
 
736
 
 
737
    /*DOC*/ static char doc_event_get[] =
596
738
    /*DOC*/    "pygame.event.get([type]) -> list of Events\n"
597
739
    /*DOC*/    "get all of an event type from the queue\n"
598
740
    /*DOC*/    "\n"
604
746
    /*DOC*/    "would call, 'pygame.event.get([KEYDOWN,KEYUP])'.\n"
605
747
    /*DOC*/ ;
606
748
 
607
 
static PyObject* get(PyObject* self, PyObject* args)
 
749
static PyObject* event_get(PyObject* self, PyObject* args)
608
750
{
609
751
        SDL_Event event;
610
752
        int mask = 0;
611
753
        int loop, num;
612
754
        PyObject* type, *list, *e;
613
 
        short val;
 
755
        int val;
614
756
 
615
757
        if(PyTuple_Size(args) != 0 && PyTuple_Size(args) != 1)
616
 
                return RAISE(PyExc_ValueError, "peek requires 0 or 1 argument");
 
758
                return RAISE(PyExc_ValueError, "get requires 0 or 1 argument");
617
759
 
618
760
        VIDEO_INIT_CHECK();
619
761
 
627
769
                        num = PySequence_Size(type);
628
770
                        for(loop=0; loop<num; ++loop)
629
771
                        {
630
 
                                if(!ShortFromObjIndex(type, loop, &val))
 
772
                                if(!IntFromObjIndex(type, loop, &val))
631
773
                                        return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
632
774
                                mask |= SDL_EVENTMASK(val);
633
775
                        }
634
776
                }
635
 
                else if(ShortFromObj(type, &val))
 
777
                else if(IntFromObj(type, &val))
636
778
                        mask = SDL_EVENTMASK(val);
637
779
                else
638
 
                        return RAISE(PyExc_TypeError, "peek type must be numeric or a sequence");
 
780
                        return RAISE(PyExc_TypeError, "get type must be numeric or a sequence");
639
781
        }
640
 
        
 
782
 
641
783
        list = PyList_New(0);
642
784
        if(!list)
643
785
                return NULL;
667
809
    /*DOC*/    "\n"
668
810
    /*DOC*/    "Pass this a type of event that you are interested in, and it will\n"
669
811
    /*DOC*/    "return true if there are any of that type of event on the queue.\n"
670
 
    /*DOC*/    "If no types are passed, this will return true if any events are\n"
671
 
    /*DOC*/    "on the queue. You may also optionally pass a sequence of event\n"
 
812
    /*DOC*/    "If no types are passed, this will return the next event on the queue\n"
 
813
    /*DOC*/    "without removing it. You may also optionally pass a sequence of event\n"
672
814
    /*DOC*/    "types. For example, to find if any keyboard events are on the\n"
673
815
    /*DOC*/    "queue, you would call, 'pygame.event.peek([KEYDOWN,KEYUP])'.\n"
674
816
    /*DOC*/ ;
678
820
        SDL_Event event;
679
821
        int result;
680
822
        int mask = 0;
681
 
        int loop, num;
 
823
        int loop, num, noargs=0;
682
824
        PyObject* type;
683
 
        short val;
 
825
        int val;
684
826
 
685
827
        if(PyTuple_Size(args) != 0 && PyTuple_Size(args) != 1)
686
828
                return RAISE(PyExc_ValueError, "peek requires 0 or 1 argument");
688
830
        VIDEO_INIT_CHECK();
689
831
 
690
832
        if(PyTuple_Size(args) == 0)
 
833
        {
691
834
                mask = SDL_ALLEVENTS;
 
835
                noargs=1;
 
836
        }
692
837
        else
693
838
        {
694
839
                type = PyTuple_GET_ITEM(args, 0);
697
842
                        num = PySequence_Size(type);
698
843
                        for(loop=0; loop<num; ++loop)
699
844
                        {
700
 
                                if(!ShortFromObjIndex(type, loop, &val))
 
845
                                if(!IntFromObjIndex(type, loop, &val))
701
846
                                        return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
702
847
                                mask |= SDL_EVENTMASK(val);
703
848
                        }
704
849
                }
705
 
                else if(ShortFromObj(type, &val))
 
850
                else if(IntFromObj(type, &val))
706
851
                        mask = SDL_EVENTMASK(val);
707
852
                else
708
853
                        return RAISE(PyExc_TypeError, "peek type must be numeric or a sequence");
709
854
        }
710
 
        
 
855
 
711
856
        SDL_PumpEvents();
712
857
        result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, mask);
 
858
 
 
859
        if(noargs)
 
860
                return PyEvent_New(&event);
713
861
        return PyInt_FromLong(result == 1);
714
862
}
715
863
 
748
896
        event.user.data2 = userobj;
749
897
 
750
898
        if(SDL_PushEvent(&event) == -1)
751
 
                return RAISE(PyExc_SDLError, SDL_GetError());
 
899
                return RAISE(PyExc_SDLError, "Event queue full");
752
900
 
753
901
        RETURN_NONE
754
902
}
769
917
{
770
918
        int loop, num;
771
919
        PyObject* type;
772
 
        short val;
 
920
        int val;
773
921
 
774
922
        if(PyTuple_Size(args) != 1)
775
923
                return RAISE(PyExc_ValueError, "set_allowed requires 1 argument");
782
930
                num = PySequence_Length(type);
783
931
                for(loop=0; loop<num; ++loop)
784
932
                {
785
 
                        if(!ShortFromObjIndex(type, loop, &val))
 
933
                        if(!IntFromObjIndex(type, loop, &val))
786
934
                                return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
787
935
                        SDL_EventState((Uint8)val, SDL_ENABLE);
788
936
                }
789
937
        }
790
938
        else if(type == Py_None)
791
939
                SDL_EventState((Uint8)0xFF, SDL_IGNORE);
792
 
        else if(ShortFromObj(type, &val))
 
940
        else if(IntFromObj(type, &val))
793
941
                SDL_EventState((Uint8)val, SDL_ENABLE);
794
942
        else
795
943
                return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
813
961
{
814
962
        int loop, num;
815
963
        PyObject* type;
816
 
        short val;
 
964
        int val;
817
965
 
818
966
        if(PyTuple_Size(args) != 1)
819
967
                return RAISE(PyExc_ValueError, "set_blocked requires 1 argument");
826
974
                num = PySequence_Length(type);
827
975
                for(loop=0; loop<num; ++loop)
828
976
                {
829
 
                        if(!ShortFromObjIndex(type, loop, &val))
 
977
                        if(!IntFromObjIndex(type, loop, &val))
830
978
                                return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
831
979
                        SDL_EventState((Uint8)val, SDL_IGNORE);
832
980
                }
833
981
        }
834
982
        else if(type == Py_None)
835
983
                SDL_EventState((Uint8)0, SDL_IGNORE);
836
 
        else if(ShortFromObj(type, &val))
 
984
        else if(IntFromObj(type, &val))
837
985
                SDL_EventState((Uint8)val, SDL_IGNORE);
838
986
        else
839
987
                return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
855
1003
{
856
1004
        int loop, num;
857
1005
        PyObject* type;
858
 
        short val;
 
1006
        int val;
859
1007
        int isblocked = 0;
860
1008
 
861
1009
        if(PyTuple_Size(args) != 1)
869
1017
                num = PySequence_Length(type);
870
1018
                for(loop=0; loop<num; ++loop)
871
1019
                {
872
 
                        if(!ShortFromObjIndex(type, loop, &val))
 
1020
                        if(!IntFromObjIndex(type, loop, &val))
873
1021
                                return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
874
1022
                        isblocked |= SDL_EventState((Uint8)val, SDL_QUERY) == SDL_IGNORE;
875
1023
                }
876
1024
        }
877
 
        else if(ShortFromObj(type, &val))
 
1025
        else if(IntFromObj(type, &val))
878
1026
                isblocked = SDL_EventState((Uint8)val, SDL_QUERY) == SDL_IGNORE;
879
1027
        else
880
1028
                return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
895
1043
        { "pump", pump, 1, doc_pump },
896
1044
        { "wait", pygame_wait, 1, doc_wait },
897
1045
        { "poll", poll, 1, doc_poll },
898
 
        { "get", get, 1, doc_get },
 
1046
        { "clear", event_clear, 1, doc_event_clear },
 
1047
        { "get", event_get, 1, doc_event_get },
899
1048
        { "peek", event_peek, 1, doc_peek },
900
1049
        { "post", event_post, 1, doc_post },
901
1050