155
#ifdef Py_USING_UNICODE
157
static PyObject* our_unichr(long uni)
159
static PyObject* bltin_unichr = NULL;
161
if (bltin_unichr == NULL) {
164
bltins = PyImport_ImportModule("__builtin__");
165
bltin_unichr = PyObject_GetAttrString(bltins, "unichr");
170
return PyEval_CallFunction(bltin_unichr, "(l)", uni);
173
static PyObject* our_empty_ustr(void)
175
static PyObject* empty_ustr = NULL;
177
if (empty_ustr == NULL) {
179
PyObject* bltin_unicode;
181
bltins = PyImport_ImportModule("__builtin__");
182
bltin_unicode = PyObject_GetAttrString(bltins, "unicode");
183
empty_ustr = PyEval_CallFunction(bltin_unicode, "(s)", "");
185
Py_DECREF(bltin_unicode);
189
Py_INCREF(empty_ustr);
196
static PyObject* our_unichr(long uni)
198
return PyInt_FromLong(uni);
201
static PyObject* our_empty_ustr(void)
203
return PyInt_FromLong(0);
206
#endif /* Py_USING_UNICODE */
156
208
static PyObject* dict_from_event(SDL_Event* event)
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));
184
insobj(dict, "unicode",
185
PyUnicode_FromObject(PyString_FromString("")));
233
insobj(dict, "unicode", our_empty_ustr());
187
235
insobj(dict, "key", PyInt_FromLong(event->key.keysym.sym));
188
236
insobj(dict, "mod", PyInt_FromLong(event->key.keysym.mod));
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));
313
361
/*DOC*/ "lookups will be passed through to the Event's dictionary values.\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"
385
static int event_nonzero(PyEventObject *self)
387
return self->type != SDL_NOEVENT;
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*/
340
417
static PyTypeObject PyEvent_Type =
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"
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"
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"
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"
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"
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"
524
612
static PyObject* pump(PyObject* self, PyObject* args)
579
667
static PyObject* poll(PyObject* self, PyObject* args)
583
671
if(!PyArg_ParseTuple(args, ""))
586
674
VIDEO_INIT_CHECK();
588
if(!SDL_PollEvent(&event))
589
return PyEvent_New2(SDL_NOEVENT, NULL);
591
return PyEvent_New(&event);
595
/*DOC*/ static char doc_get[] =
676
if(SDL_PollEvent(&event))
677
return PyEvent_New(&event);
678
return PyEvent_New(NULL);
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"
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"
694
static PyObject* event_clear(PyObject* self, PyObject* args)
702
if(PyTuple_Size(args) != 0 && PyTuple_Size(args) != 1)
703
return RAISE(PyExc_ValueError, "get requires 0 or 1 argument");
707
if(PyTuple_Size(args) == 0)
708
mask = SDL_ALLEVENTS;
711
type = PyTuple_GET_ITEM(args, 0);
712
if(PySequence_Check(type))
714
num = PySequence_Size(type);
715
for(loop=0; loop<num; ++loop)
717
if(!IntFromObjIndex(type, loop, &val))
718
return RAISE(PyExc_TypeError, "type sequence must contain valid event types");
719
mask |= SDL_EVENTMASK(val);
722
else if(IntFromObj(type, &val))
723
mask = SDL_EVENTMASK(val);
725
return RAISE(PyExc_TypeError, "get type must be numeric or a sequence");
730
while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, mask) == 1)
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"
604
746
/*DOC*/ "would call, 'pygame.event.get([KEYDOWN,KEYUP])'.\n"
607
static PyObject* get(PyObject* self, PyObject* args)
749
static PyObject* event_get(PyObject* self, PyObject* args)
612
754
PyObject* type, *list, *e;
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");
618
760
VIDEO_INIT_CHECK();
627
769
num = PySequence_Size(type);
628
770
for(loop=0; loop<num; ++loop)
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);
635
else if(ShortFromObj(type, &val))
777
else if(IntFromObj(type, &val))
636
778
mask = SDL_EVENTMASK(val);
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");
641
783
list = PyList_New(0);
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"
697
842
num = PySequence_Size(type);
698
843
for(loop=0; loop<num; ++loop)
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);
705
else if(ShortFromObj(type, &val))
850
else if(IntFromObj(type, &val))
706
851
mask = SDL_EVENTMASK(val);
708
853
return RAISE(PyExc_TypeError, "peek type must be numeric or a sequence");
711
856
SDL_PumpEvents();
712
857
result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, mask);
860
return PyEvent_New(&event);
713
861
return PyInt_FromLong(result == 1);
782
930
num = PySequence_Length(type);
783
931
for(loop=0; loop<num; ++loop)
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);
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);
795
943
return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
826
974
num = PySequence_Length(type);
827
975
for(loop=0; loop<num; ++loop)
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);
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);
839
987
return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
869
1017
num = PySequence_Length(type);
870
1018
for(loop=0; loop<num; ++loop)
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;
877
else if(ShortFromObj(type, &val))
1025
else if(IntFromObj(type, &val))
878
1026
isblocked = SDL_EventState((Uint8)val, SDL_QUERY) == SDL_IGNORE;
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 },