1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
|
/*
pygame - Python Game Library
Copyright (C) 2000-2001 Pete Shinners
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Pete Shinners
pete@shinners.org
*/
/*
* pygame event module
*/
#define PYGAMEAPI_EVENT_INTERNAL
#include "pygame.h"
#include "pgcompat.h"
#include "doc/event_doc.h"
#include "structmember.h"
#if IS_SDLv2
/*only register one block of user events.*/
static int have_registered_events = 0;
#endif /* IS_SDLv2 */
// FIXME: The system message code is only tested on windows, so only
// include it there for now.
#include <SDL_syswm.h>
/*this user event object is for safely passing
*objects through the event queue.
*/
#define USEROBJECT_CHECK1 0xDEADBEEF
#define USEROBJECT_CHECK2 0xFEEDF00D
typedef struct UserEventObject {
struct UserEventObject *next;
PyObject *object;
} UserEventObject;
static UserEventObject *user_event_objects = NULL;
#if IS_SDLv2
static int pg_key_repeat_delay = 0;
static int pg_key_repeat_interval = 0;
static SDL_TimerID _pg_repeat_timer = 0;
static SDL_Event _pg_repeat_event;
static Uint32
_pg_repeat_callback(Uint32 interval, void *param)
{
_pg_repeat_event.type = PGE_KEYREPEAT;
_pg_repeat_event.key.state = SDL_PRESSED;
_pg_repeat_event.key.repeat = 1;
SDL_PushEvent(&_pg_repeat_event);
return pg_key_repeat_interval;
}
static int _pg_event_is_init = 0;
static void
_pg_repeat_cleanup(void)
{
if (_pg_repeat_timer) {
SDL_RemoveTimer(_pg_repeat_timer);
_pg_repeat_timer = 0;
}
_pg_event_is_init = 0;
}
static PyObject *
pgEvent_AutoInit(PyObject *self, PyObject *args)
{
if (!_pg_event_is_init) {
pg_key_repeat_delay = 0;
pg_key_repeat_interval = 0;
pg_RegisterQuit(_pg_repeat_cleanup);
_pg_event_is_init = 1;
}
Py_RETURN_NONE;
}
static char _pg_last_unicode_char[32] = { 0 };
/*SDL 2 to SDL 1.2 event mapping and SDL 1.2 key repeat emulation*/
static int
pg_event_filter(void *_, SDL_Event *event)
{
/* This event filter alters events inplace.
*/
Uint32 type = event->type;
if (type == SDL_WINDOWEVENT) {
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED:
event->type = SDL_VIDEORESIZE;
break;
case SDL_WINDOWEVENT_EXPOSED:
event->type = SDL_VIDEOEXPOSE;
break;
case SDL_WINDOWEVENT_ENTER:
case SDL_WINDOWEVENT_LEAVE:
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_FOCUS_LOST:
case SDL_WINDOWEVENT_MINIMIZED:
case SDL_WINDOWEVENT_RESTORED:
event->type = SDL_ACTIVEEVENT;
break;
case SDL_WINDOWEVENT_CLOSE:
break;
default:
/*ignore other SDL_WINDOWEVENTs for now.*/
return 0;
}
}
#pragma PG_WARN(Add event blocking here.)
else if (type == SDL_KEYDOWN) {
SDL_Event inputEvent[2];
if (event->key.repeat) {
return 0;
}
else if (pg_key_repeat_delay > 0) {
if (_pg_repeat_timer) {
SDL_RemoveTimer(_pg_repeat_timer);
}
memcpy(&_pg_repeat_event, event, sizeof(SDL_Event));
_pg_repeat_timer = SDL_AddTimer(pg_key_repeat_delay, _pg_repeat_callback,
NULL);
}
#pragma PG_WARN(PumpEvents is not thread-safe)
SDL_PumpEvents();
if (SDL_PeepEvents(inputEvent, 1, SDL_PEEKEVENT,
SDL_TEXTINPUT, SDL_TEXTINPUT) == 1)
{
SDL_Event *ev = inputEvent;
SDL_PumpEvents();
if (_pg_last_unicode_char[0] == 0) {
if (SDL_PeepEvents(inputEvent, 2, SDL_PEEKEVENT,
SDL_TEXTINPUT, SDL_TEXTINPUT) == 2)
ev = &inputEvent[1];
}
strncpy(_pg_last_unicode_char, ev->text.text,
sizeof(_pg_last_unicode_char));
}
else {
_pg_last_unicode_char[0] = 0;
}
}
else if (type == SDL_KEYUP) {
if (_pg_repeat_timer &&
_pg_repeat_event.key.keysym.scancode == event->key.keysym.scancode) {
SDL_RemoveTimer(_pg_repeat_timer);
_pg_repeat_timer = 0;
}
}
else if (type == PGE_KEYREPEAT) {
event->type = SDL_KEYDOWN;
}
else if (type == SDL_MOUSEBUTTONDOWN || type == SDL_MOUSEBUTTONUP) {
if (event->button.button & PGM_BUTTON_KEEP) {
event->button.button ^= PGM_BUTTON_KEEP;
}
else if (event->button.button >= PGM_BUTTON_WHEELUP) {
event->button.button += (PGM_BUTTON_X1 - PGM_BUTTON_WHEELUP);
}
}
else if (type == SDL_MOUSEWHEEL) {
SDL_Event newevent;
int x, y;
if (event->wheel.x == 0 && event->wheel.y == 0) {
//#691 We are not moving wheel!
return 1;
}
// Generate a MouseButtonDown event for compatibility.
// https://wiki.libsdl.org/SDL_MouseWheelEvent
newevent.type = SDL_MOUSEBUTTONDOWN;
SDL_GetMouseState(&x, &y);
newevent.button.x = x;
newevent.button.y = y;
newevent.button.state = SDL_PRESSED;
newevent.button.clicks = 1;
if (event->wheel.y != 0) {
newevent.button.button = (event->wheel.y > 0) ?
PGM_BUTTON_WHEELUP : PGM_BUTTON_WHEELDOWN;
}
else if (event->wheel.x != 0) {
newevent.button.button = (event->wheel.x > 0) ?
PGM_BUTTON_WHEELUP : PGM_BUTTON_WHEELDOWN;
}
newevent.button.button |= PGM_BUTTON_KEEP;
if (SDL_PushEvent(&newevent) < 0)
return RAISE(pgExc_SDLError, SDL_GetError()), 0;
}
return 1;
}
static int
pg_EnableKeyRepeat(int delay, int interval)
{
if (delay < 0 || interval < 0) {
RAISE(PyExc_ValueError, "delay and interval must equal at least 0");
return -1;
}
pg_key_repeat_delay = delay;
pg_key_repeat_interval = interval;
return 0;
}
static void
pg_GetKeyRepeat(int *delay, int *interval)
{
*delay = pg_key_repeat_delay;
*interval = pg_key_repeat_interval;
}
#endif /* IS_SDLv2 */
/*must pass dictionary as this object*/
static UserEventObject *
_pg_user_event_addobject(PyObject *obj)
{
UserEventObject *userobj = PyMem_New(UserEventObject, 1);
if (!userobj)
return NULL;
Py_INCREF(obj);
userobj->next = user_event_objects;
userobj->object = obj;
user_event_objects = userobj;
return userobj;
}
/*note, we doublecheck to make sure the pointer is in our list,
*not just some random pointer. this will keep us safe(r).
*/
static PyObject *
_pg_user_pg_event_getobject(UserEventObject *userobj)
{
PyObject *obj = NULL;
if (!user_event_objects) /*fail in most common case*/
return NULL;
if (user_event_objects == userobj) {
obj = userobj->object;
user_event_objects = userobj->next;
}
else {
UserEventObject *hunt = user_event_objects;
while (hunt && hunt->next != userobj)
hunt = hunt->next;
if (hunt) {
hunt->next = userobj->next;
obj = userobj->object;
}
}
if (obj)
PyMem_Del(userobj);
return obj;
}
static void
_pg_user_event_cleanup(void)
{
if (user_event_objects) {
UserEventObject *hunt, *kill;
hunt = user_event_objects;
while (hunt) {
kill = hunt;
hunt = hunt->next;
Py_DECREF(kill->object);
PyMem_Del(kill);
}
user_event_objects = NULL;
}
}
static int
pgEvent_FillUserEvent(pgEventObject *e, SDL_Event *event)
{
UserEventObject *userobj = _pg_user_event_addobject(e->dict);
if (!userobj)
return -1;
event->type = e->type;
event->user.code = USEROBJECT_CHECK1;
event->user.data1 = (void *)USEROBJECT_CHECK2;
event->user.data2 = userobj;
return 0;
}
static PyTypeObject pgEvent_Type;
static PyObject *
pgEvent_New(SDL_Event *);
static PyObject *
pgEvent_New2(int, PyObject *);
#define pgEvent_Check(x) ((x)->ob_type == &pgEvent_Type)
static char *
_pg_name_from_eventtype(int type)
{
switch (type) {
case SDL_ACTIVEEVENT:
return "ActiveEvent";
#if IS_SDLv2
case SDL_AUDIODEVICEADDED:
return "AudioDeviceAdded";
case SDL_AUDIODEVICEREMOVED:
return "AudioDeviceRemoved";
#endif
case SDL_KEYDOWN:
return "KeyDown";
case SDL_KEYUP:
return "KeyUp";
case SDL_MOUSEMOTION:
return "MouseMotion";
case SDL_MOUSEBUTTONDOWN:
return "MouseButtonDown";
case SDL_MOUSEBUTTONUP:
return "MouseButtonUp";
case SDL_JOYAXISMOTION:
return "JoyAxisMotion";
case SDL_JOYBALLMOTION:
return "JoyBallMotion";
case SDL_JOYHATMOTION:
return "JoyHatMotion";
case SDL_JOYBUTTONUP:
return "JoyButtonUp";
case SDL_JOYBUTTONDOWN:
return "JoyButtonDown";
case SDL_QUIT:
return "Quit";
case SDL_SYSWMEVENT:
return "SysWMEvent";
case SDL_VIDEORESIZE:
return "VideoResize";
case SDL_VIDEOEXPOSE:
return "VideoExpose";
case SDL_NOEVENT:
return "NoEvent";
#if IS_SDLv2
case SDL_FINGERMOTION:
return "FingerMotion";
case SDL_FINGERDOWN:
return "FingerDown";
case SDL_FINGERUP:
return "FingerUp";
case SDL_MULTIGESTURE:
return "MultiGesture";
case SDL_MOUSEWHEEL:
return "MouseWheel";
case SDL_TEXTINPUT:
return "TextInput";
case SDL_TEXTEDITING:
return "TextEditing";
case SDL_DROPFILE:
return "DropFile";
case SDL_DROPTEXT:
return "DropText";
case SDL_DROPBEGIN:
return "DropBegin";
case SDL_DROPCOMPLETE:
return "DropComplete";
#endif
}
if (type >= SDL_USEREVENT && type < SDL_NUMEVENTS)
return "UserEvent";
return "Unknown";
}
/* Helper for adding objects to dictionaries. Check for errors with
PyErr_Occurred() */
static void
_pg_insobj(PyObject *dict, char *name, PyObject *v)
{
if (v) {
PyDict_SetItemString(dict, name, v);
Py_DECREF(v);
}
}
#if defined(Py_USING_UNICODE)
static PyObject *
_pg_our_unichr(long uni)
{
static PyObject *bltin_unichr = NULL;
if (bltin_unichr == NULL) {
PyObject *bltins;
bltins = PyImport_ImportModule(BUILTINS_MODULE);
bltin_unichr = PyObject_GetAttrString(bltins, BUILTINS_UNICHR);
Py_DECREF(bltins);
}
return PyEval_CallFunction(bltin_unichr, "(l)", uni);
}
static PyObject *
_pg_our_empty_ustr(void)
{
static PyObject *empty_ustr = NULL;
if (empty_ustr == NULL) {
PyObject *bltins;
PyObject *bltin_unicode;
bltins = PyImport_ImportModule(BUILTINS_MODULE);
bltin_unicode = PyObject_GetAttrString(bltins, BUILTINS_UNICODE);
empty_ustr = PyEval_CallFunction(bltin_unicode, "(s)", "");
Py_DECREF(bltin_unicode);
Py_DECREF(bltins);
}
Py_INCREF(empty_ustr);
return empty_ustr;
}
#else
static PyObject *
_pg_our_unichr(long uni)
{
return PyInt_FromLong(uni);
}
static PyObject *
_pg_our_empty_ustr(void)
{
return PyInt_FromLong(0);
}
#endif /* Py_USING_UNICODE */
static PyObject *
dict_from_event(SDL_Event *event)
{
PyObject *dict = NULL, *tuple, *obj;
int hx, hy;
#if IS_SDLv2
long gain;
long state;
#endif /* IS_SDLv2 */
/*check if it is an event the user posted*/
if (event->user.code == USEROBJECT_CHECK1 &&
event->user.data1 == (void *)USEROBJECT_CHECK2) {
dict = _pg_user_pg_event_getobject((UserEventObject *)event->user.data2);
if (dict)
return dict;
}
if (!(dict = PyDict_New()))
return NULL;
switch (event->type) {
#if IS_SDLv1
case SDL_ACTIVEEVENT:
_pg_insobj(dict, "gain", PyInt_FromLong(event->active.gain));
_pg_insobj(dict, "state", PyInt_FromLong(event->active.state));
break;
case SDL_KEYDOWN:
if (event->key.keysym.unicode)
_pg_insobj(dict, "unicode", _pg_our_unichr(event->key.keysym.unicode));
else
_pg_insobj(dict, "unicode", _pg_our_empty_ustr());
case SDL_KEYUP:
_pg_insobj(dict, "key", PyInt_FromLong(event->key.keysym.sym));
_pg_insobj(dict, "mod", PyInt_FromLong(event->key.keysym.mod));
_pg_insobj(dict, "scancode",
PyInt_FromLong(event->key.keysym.scancode));
break;
#else /* IS_SDLv2 */
case SDL_WINDOWEVENT:
_pg_insobj(dict, "event", PyInt_FromLong(event->window.event));
switch (event->window.event) {
case SDL_WINDOWEVENT_CLOSE:
break;
}
break;
case SDL_ACTIVEEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_ENTER:
gain = 1;
state = (long)SDL_APPFOCUSMOUSE;
break;
case SDL_WINDOWEVENT_LEAVE:
gain = 0;
state = (long)SDL_APPFOCUSMOUSE;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
gain = 1;
state = (long)SDL_APPINPUTFOCUS;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
gain = 0;
state = (long)SDL_APPINPUTFOCUS;
break;
case SDL_WINDOWEVENT_MINIMIZED:
gain = 0;
state = (long)SDL_APPACTIVE;
break;
default:
assert(event->window.event == SDL_WINDOWEVENT_RESTORED);
gain = 1;
state = (long)SDL_APPACTIVE;
}
_pg_insobj(dict, "gain", PyInt_FromLong(gain));
_pg_insobj(dict, "state", PyInt_FromLong(state));
break;
case SDL_AUDIODEVICEADDED:
case SDL_AUDIODEVICEREMOVED:
_pg_insobj(dict, "which", PyInt_FromLong(&event->adevice.which));
_pg_insobj(dict, "iscapture", PyInt_FromLong(&event->adevice.iscapture));
break;
case SDL_KEYDOWN:
_pg_insobj(dict, "unicode", Text_FromUTF8(_pg_last_unicode_char));
/* fall through */
case SDL_KEYUP:
_pg_insobj(dict, "key", PyInt_FromLong(event->key.keysym.sym));
_pg_insobj(dict, "mod", PyInt_FromLong(event->key.keysym.mod));
_pg_insobj(dict, "scancode", PyInt_FromLong(event->key.keysym.scancode));
break;
#endif /* IS_SDLv2 */
case SDL_MOUSEMOTION:
obj = Py_BuildValue("(ii)", event->motion.x, event->motion.y);
_pg_insobj(dict, "pos", obj);
obj =
Py_BuildValue("(ii)", event->motion.xrel, event->motion.yrel);
_pg_insobj(dict, "rel", obj);
if ((tuple = PyTuple_New(3))) {
PyTuple_SET_ITEM(tuple, 0,
PyInt_FromLong((event->motion.state &
SDL_BUTTON(1)) != 0));
PyTuple_SET_ITEM(tuple, 1,
PyInt_FromLong((event->motion.state &
SDL_BUTTON(2)) != 0));
PyTuple_SET_ITEM(tuple, 2,
PyInt_FromLong((event->motion.state &
SDL_BUTTON(3)) != 0));
_pg_insobj(dict, "buttons", tuple);
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
obj = Py_BuildValue("(ii)", event->button.x, event->button.y);
_pg_insobj(dict, "pos", obj);
_pg_insobj(dict, "button", PyInt_FromLong(event->button.button));
break;
case SDL_JOYAXISMOTION:
_pg_insobj(dict, "joy", PyInt_FromLong(event->jaxis.which));
_pg_insobj(dict, "axis", PyInt_FromLong(event->jaxis.axis));
_pg_insobj(dict, "value",
PyFloat_FromDouble(event->jaxis.value / 32767.0));
break;
case SDL_JOYBALLMOTION:
_pg_insobj(dict, "joy", PyInt_FromLong(event->jball.which));
_pg_insobj(dict, "ball", PyInt_FromLong(event->jball.ball));
obj = Py_BuildValue("(ii)", event->jball.xrel, event->jball.yrel);
_pg_insobj(dict, "rel", obj);
break;
case SDL_JOYHATMOTION:
_pg_insobj(dict, "joy", PyInt_FromLong(event->jhat.which));
_pg_insobj(dict, "hat", PyInt_FromLong(event->jhat.hat));
hx = hy = 0;
if (event->jhat.value & SDL_HAT_UP)
hy = 1;
else if (event->jhat.value & SDL_HAT_DOWN)
hy = -1;
if (event->jhat.value & SDL_HAT_RIGHT)
hx = 1;
else if (event->jhat.value & SDL_HAT_LEFT)
hx = -1;
_pg_insobj(dict, "value", Py_BuildValue("(ii)", hx, hy));
break;
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
_pg_insobj(dict, "joy", PyInt_FromLong(event->jbutton.which));
_pg_insobj(dict, "button", PyInt_FromLong(event->jbutton.button));
break;
#if IS_SDLv2
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
/* https://wiki.libsdl.org/SDL_TouchFingerEvent */
_pg_insobj(dict, "touch_id", PyLong_FromLongLong(event->tfinger.touchId));
_pg_insobj(dict, "finger_id", PyLong_FromLongLong(event->tfinger.fingerId));
_pg_insobj(dict, "x", PyFloat_FromDouble(event->tfinger.x));
_pg_insobj(dict, "y", PyFloat_FromDouble(event->tfinger.y));
_pg_insobj(dict, "dx", PyFloat_FromDouble(event->tfinger.dx));
_pg_insobj(dict, "dy", PyFloat_FromDouble(event->tfinger.dy));
_pg_insobj(dict, "pressure", PyFloat_FromDouble(event->tfinger.dy));
break;
case SDL_MULTIGESTURE:
/* https://wiki.libsdl.org/SDL_MultiGestureEvent */
_pg_insobj(dict, "touch_id", PyLong_FromLongLong(event->mgesture.touchId));
_pg_insobj(dict, "x", PyFloat_FromDouble(event->mgesture.x));
_pg_insobj(dict, "y", PyFloat_FromDouble(event->mgesture.y));
_pg_insobj(dict, "rotated", PyFloat_FromDouble(event->mgesture.dTheta));
_pg_insobj(dict, "pinched", PyFloat_FromDouble(event->mgesture.dDist));
_pg_insobj(dict, "num_fingers", PyInt_FromLong(event->mgesture.numFingers));
break;
case SDL_MOUSEWHEEL:
/* https://wiki.libsdl.org/SDL_MouseWheelEvent */
_pg_insobj(dict, "flipped", PyBool_FromLong(event->wheel.direction == SDL_MOUSEWHEEL_FLIPPED));
_pg_insobj(dict, "y", PyInt_FromLong(event->wheel.y));
_pg_insobj(dict, "x", PyInt_FromLong(event->wheel.x));
_pg_insobj(dict, "which", PyInt_FromLong(event->wheel.which));
break;
case SDL_TEXTINPUT:
/* https://wiki.libsdl.org/SDL_TextInputEvent */
_pg_insobj(dict, "text", Text_FromUTF8(event->text.text));
break;
case SDL_TEXTEDITING:
/* https://wiki.libsdl.org/SDL_TextEditingEvent */
_pg_insobj(dict, "text", Text_FromUTF8(event->edit.text));
_pg_insobj(dict, "start", PyLong_FromLong(event->edit.start));
_pg_insobj(dict, "length", PyLong_FromLong(event->edit.length));
break;
/* https://wiki.libsdl.org/SDL_DropEvent */
case SDL_DROPFILE:
_pg_insobj(dict, "file", Text_FromUTF8(event->drop.file));
SDL_free(event->drop.file);
break;
case SDL_DROPTEXT:
_pg_insobj(dict, "text", Text_FromUTF8(event->drop.file));
SDL_free(event->drop.file);
break;
case SDL_DROPBEGIN:
case SDL_DROPCOMPLETE:
break;
#endif
#if IS_SDLv1
case SDL_VIDEORESIZE:
obj = Py_BuildValue("(ii)", event->resize.w, event->resize.h);
_pg_insobj(dict, "size", obj);
_pg_insobj(dict, "w", PyInt_FromLong(event->resize.w));
_pg_insobj(dict, "h", PyInt_FromLong(event->resize.h));
break;
#else /* IS_SDLv2 */
case SDL_VIDEORESIZE:
obj = Py_BuildValue("(ii)", event->window.data1,
event->window.data2);
_pg_insobj(dict, "size", obj);
_pg_insobj(dict, "w", PyInt_FromLong(event->window.data1));
_pg_insobj(dict, "h", PyInt_FromLong(event->window.data2));
break;
#endif /* IS_SDLv2 */
#ifdef WIN32
#if IS_SDLv1
case SDL_SYSWMEVENT:
_pg_insobj(dict, "hwnd",
PyInt_FromLong((long)(event->syswm.msg->hwnd)));
_pg_insobj(dict, "msg", PyInt_FromLong(event->syswm.msg->msg));
_pg_insobj(dict, "wparam", PyInt_FromLong(event->syswm.msg->wParam));
_pg_insobj(dict, "lparam", PyInt_FromLong(event->syswm.msg->lParam));
break;
#else /* IS_SDLv2 */
case SDL_SYSWMEVENT:
_pg_insobj(dict, "hwnd",
PyInt_FromLong((long)(event->syswm.msg->msg.win.hwnd)));
_pg_insobj(dict, "msg", PyInt_FromLong(event->syswm.msg->msg.win.msg));
_pg_insobj(dict, "wparam", PyInt_FromLong(event->syswm.msg->msg.win.wParam));
_pg_insobj(dict, "lparam", PyInt_FromLong(event->syswm.msg->msg.win.lParam));
break;
#endif /* IS_SDLv2 */
#endif /* WIN32 */
#if (defined(unix) || defined(__unix__) || defined(_AIX) || \
defined(__OpenBSD__)) && \
(defined(SDL_VIDEO_DRIVER_X11) && !defined(__CYGWIN32__) && \
!defined(ENABLE_NANOX) && !defined(__QNXNTO__))
#if IS_SDLv1
case SDL_SYSWMEVENT:
_pg_insobj(dict, "event",
Bytes_FromStringAndSize(
(char *)&(event->syswm.msg->event.xevent),
sizeof(XEvent)));
break;
#else /* IS_SDLv2 */
case SDL_SYSWMEVENT:
if (event->syswm.msg->subsystem == SDL_SYSWM_X11) {
XEvent *xevent = (XEvent *)&event->syswm.msg->msg.x11.event;
obj = Bytes_FromStringAndSize((char *)xevent, sizeof(XEvent));
_pg_insobj(dict, "event", obj);
}
break;
#endif /* IS_SDLv2 */
#endif /* (defined(unix) || ... */
/* SDL_VIDEOEXPOSE and SDL_QUIT have no attributes */
} /* switch (event->type) */
if (event->type == SDL_USEREVENT && event->user.code == 0x1000) {
_pg_insobj(dict, "filename", Text_FromUTF8(event->user.data1));
free(event->user.data1);
event->user.data1 = NULL;
}
if (event->type >= SDL_USEREVENT && event->type < SDL_NUMEVENTS)
_pg_insobj(dict, "code", PyInt_FromLong(event->user.code));
switch (event->type) {
#if IS_SDLv2
case SDL_WINDOWEVENT:
case SDL_TEXTEDITING:
case SDL_TEXTINPUT:
case SDL_MOUSEWHEEL:
#endif /* IS_SDLv2 */
case SDL_KEYDOWN:
case SDL_KEYUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_USEREVENT:
{
#if IS_SDLv2
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
PyObject *pgWindow;
if (!window || !(pgWindow=SDL_GetWindowData(window, "pg_window"))) {
pgWindow = Py_None;
}
Py_INCREF(pgWindow);
_pg_insobj(dict, "window", pgWindow);
#else /* IS_SDLv1 */
Py_INCREF(Py_None);
_pg_insobj(dict, "window", Py_None);
#endif /* IS_SDLv1 */
break;
}
}
return dict;
}
/* event object internals */
static void
pg_event_dealloc(PyObject *self)
{
pgEventObject *e = (pgEventObject *)self;
Py_XDECREF(e->dict);
PyObject_DEL(self);
}
#ifdef PYPY_VERSION
/* Because pypy does not work with the __dict__ tp_dictoffset. */
PyObject *
pg_EventGetAttr(PyObject *o, PyObject *attr_name)
{
/* Try e->dict first, if not try the generic attribute. */
PyObject *result = PyDict_GetItem(((pgEventObject *)o)->dict, attr_name);
if (!result) {
return PyObject_GenericGetAttr(o, attr_name);
}
return result;
}
int
pg_EventSetAttr(PyObject *o, PyObject *name, PyObject *value)
{
/* if the variable is in the dict, deal with it there.
else if it's a normal attribute set it there.
else if it's not an attribute, or in the dict, set it in the dict.
*/
int dictResult;
int setInDict = 0;
PyObject *result = PyDict_GetItem(((pgEventObject *)o)->dict, name);
if (result) {
setInDict = 1;
}
else {
result = PyObject_GenericGetAttr(o, name);
if (!result) {
setInDict = 1;
}
}
if (setInDict) {
dictResult = PyDict_SetItem(((pgEventObject *)o)->dict, name, value);
if (dictResult) {
return -1;
}
return 0;
}
else {
return PyObject_GenericSetAttr(o, name, value);
}
}
#endif
PyObject *
pg_event_str(PyObject *self)
{
pgEventObject *e = (pgEventObject *)self;
char *str;
PyObject *strobj;
PyObject *pyobj;
char *s;
size_t size;
#if PY3
PyObject *encodedobj;
#endif
strobj = PyObject_Str(e->dict);
if (strobj == NULL) {
return NULL;
}
#if PY3
encodedobj = PyUnicode_AsUTF8String(strobj);
Py_DECREF(strobj);
strobj = encodedobj;
encodedobj = NULL;
if (strobj == NULL) {
return NULL;
}
s = PyBytes_AsString(strobj);
#else
s = PyString_AsString(strobj);
#endif
size = (11 + strlen(_pg_name_from_eventtype(e->type)) + strlen(s) +
sizeof(e->type) * 3 + 1);
str = (char *)PyMem_Malloc(size);
sprintf(str, "<Event(%d-%s %s)>", e->type, _pg_name_from_eventtype(e->type),
s);
Py_DECREF(strobj);
pyobj = Text_FromUTF8(str);
PyMem_Free(str);
return (pyobj);
}
static int
_pg_event_nonzero(pgEventObject *self)
{
return self->type != SDL_NOEVENT;
}
static PyNumberMethods pg_event_as_number = {
(binaryfunc)NULL, /*Add*/
(binaryfunc)NULL, /*subtract*/
(binaryfunc)NULL, /*multiply*/
#if !PY3
(binaryfunc)NULL, /*divide*/
#endif
(binaryfunc)NULL, /*remainder*/
(binaryfunc)NULL, /*divmod*/
(ternaryfunc)NULL, /*power*/
(unaryfunc)NULL, /*negative*/
(unaryfunc)NULL, /*pos*/
(unaryfunc)NULL, /*abs*/
(inquiry)_pg_event_nonzero, /*nonzero*/
(unaryfunc)NULL, /*invert*/
(binaryfunc)NULL, /*lshift*/
(binaryfunc)NULL, /*rshift*/
(binaryfunc)NULL, /*and*/
(binaryfunc)NULL, /*xor*/
(binaryfunc)NULL, /*or*/
#if !PY3
(coercion)NULL, /*coerce*/
#endif
(unaryfunc)NULL, /*int*/
#if !PY3
(unaryfunc)NULL, /*long*/
#endif
(unaryfunc)NULL, /*float*/
};
#define OFF(x) offsetof(pgEventObject, x)
static PyMemberDef pg_event_members[] = {
{"__dict__", T_OBJECT, OFF(dict), READONLY},
{"type", T_INT, OFF(type), READONLY},
{"dict", T_OBJECT, OFF(dict), READONLY},
{NULL} /* Sentinel */
};
/*
* eventA == eventB
* eventA != eventB
*/
static PyObject *
pg_event_richcompare(PyObject *o1, PyObject *o2, int opid)
{
pgEventObject *e1, *e2;
if (!pgEvent_Check(o1) || !pgEvent_Check(o2)) {
goto Unimplemented;
}
e1 = (pgEventObject *)o1;
e2 = (pgEventObject *)o2;
switch (opid) {
case Py_EQ:
return PyBool_FromLong(
e1->type == e2->type &&
PyObject_RichCompareBool(e1->dict, e2->dict, Py_EQ) == 1);
case Py_NE:
return PyBool_FromLong(
e1->type != e2->type ||
PyObject_RichCompareBool(e1->dict, e2->dict, Py_NE) == 1);
default:
break;
}
Unimplemented:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
static PyTypeObject pgEvent_Type = {
TYPE_HEAD(NULL, 0) "Event", /*name*/
sizeof(pgEventObject), /*basic size*/
0, /*itemsize*/
pg_event_dealloc, /*dealloc*/
0, /*print*/
0, /*getattr*/
0, /*setattr*/
0, /*compare*/
pg_event_str, /*repr*/
&pg_event_as_number, /*as_number*/
0, /*as_sequence*/
0, /*as_mapping*/
(hashfunc)NULL, /*hash*/
(ternaryfunc)NULL, /*call*/
(reprfunc)NULL, /*str*/
#ifdef PYPY_VERSION
pg_EventGetAttr, /* tp_getattro */
pg_EventSetAttr, /* tp_setattro */
#else
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
#endif
0, /* tp_as_buffer */
#if PY3
0,
#else
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_RICHCOMPARE,
#endif
DOC_PYGAMEEVENTEVENT, /* Documentation string */
0, /* tp_traverse */
0, /* tp_clear */
pg_event_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
pg_event_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(pgEventObject, dict), /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static PyObject *
pgEvent_New(SDL_Event *event)
{
pgEventObject *e;
e = PyObject_NEW(pgEventObject, &pgEvent_Type);
if (!e)
return NULL;
if (event) {
e->type = event->type;
e->dict = dict_from_event(event);
}
else {
e->type = SDL_NOEVENT;
e->dict = PyDict_New();
}
return (PyObject *)e;
}
static PyObject *
pgEvent_New2(int type, PyObject *dict)
{
pgEventObject *e;
e = PyObject_NEW(pgEventObject, &pgEvent_Type);
if (e) {
e->type = type;
if (!dict)
dict = PyDict_New();
else
Py_INCREF(dict);
e->dict = dict;
}
return (PyObject *)e;
}
/* event module functions */
static PyObject *
pg_Event(PyObject *self, PyObject *arg, PyObject *keywords)
{
PyObject *dict = NULL;
PyObject *event;
int type;
if (!PyArg_ParseTuple(arg, "i|O!", &type, &PyDict_Type, &dict))
return NULL;
if (!dict)
dict = PyDict_New();
else
Py_INCREF(dict);
if (keywords) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(keywords, &pos, &key, &value)) {
PyDict_SetItem(dict, key, value);
}
}
event = pgEvent_New2(type, dict);
Py_DECREF(dict);
return event;
}
static PyObject *
event_name(PyObject *self, PyObject *arg)
{
int type;
if (!PyArg_ParseTuple(arg, "i", &type))
return NULL;
return Text_FromUTF8(_pg_name_from_eventtype(type));
}
static PyObject *
set_grab(PyObject *self, PyObject *arg)
{
int doit;
#if IS_SDLv2
SDL_Window *win = NULL;
#endif /* IS_SDLv2 */
if (!PyArg_ParseTuple(arg, "i", &doit))
return NULL;
VIDEO_INIT_CHECK();
#if IS_SDLv1
if (doit)
SDL_WM_GrabInput(SDL_GRAB_ON);
else
SDL_WM_GrabInput(SDL_GRAB_OFF);
#else /* IS_SDLv2 */
win = pg_GetDefaultWindow();
if (win) {
if (doit)
SDL_SetWindowGrab(win, SDL_TRUE);
else
SDL_SetWindowGrab(win, SDL_FALSE);
}
#endif /* IS_SDLv2 */
Py_RETURN_NONE;
}
static PyObject *
get_grab(PyObject *self, PyObject *arg)
{
#if IS_SDLv1
int mode;
#else /* IS_SDLv2 */
SDL_Window *win;
SDL_bool mode = SDL_FALSE;
#endif /* IS_SDLv2 */
VIDEO_INIT_CHECK();
#if IS_SDLv1
mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
return PyInt_FromLong(mode == SDL_GRAB_ON);
#else /* IS_SDLv2 */
win = pg_GetDefaultWindow();
if (win)
mode = SDL_GetWindowGrab(win);
return PyInt_FromLong(mode);
#endif /* IS_SDLv2 */
}
static PyObject *
pg_event_pump(PyObject *self, PyObject *args)
{
VIDEO_INIT_CHECK();
SDL_PumpEvents();
Py_RETURN_NONE;
}
static PyObject *
pg_event_wait(PyObject *self, PyObject *args)
{
SDL_Event event;
int status;
VIDEO_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
status = SDL_WaitEvent(&event);
Py_END_ALLOW_THREADS;
if (!status)
return RAISE(pgExc_SDLError, SDL_GetError());
return pgEvent_New(&event);
}
static PyObject *
pg_event_poll(PyObject *self, PyObject *args)
{
SDL_Event event;
VIDEO_INIT_CHECK();
if (SDL_PollEvent(&event))
return pgEvent_New(&event);
return pgEvent_New(NULL);
}
#if IS_SDLv1
static PyObject *
pg_event_clear(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int mask = 0;
int loop, num;
PyObject *type = NULL;
int dopump = 1;
int val;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
if (type == NULL || type == Py_None)
mask = SDL_ALLEVENTS;
else {
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
mask |= SDL_EVENTMASK(val);
}
}
else if (pg_IntFromObj(type, &val))
mask = SDL_EVENTMASK(val);
else
return RAISE(PyExc_TypeError,
"get type must be numeric or a sequence");
}
if (dopump)
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, mask) == 1)
{
}
Py_RETURN_NONE;
}
#else /* IS_SDLv2 */
static PyObject *
pg_event_clear(PyObject *self, PyObject *args, PyObject *kwargs)
{
int loop, num;
PyObject *type = NULL;
int dopump = 1;
int val;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
if (dopump)
SDL_PumpEvents();
if (type == NULL || type == Py_None) {
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
} else {
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
SDL_FlushEvent(val);
}
}
else if (pg_IntFromObj(type, &val))
SDL_FlushEvent(val);
else
return RAISE(PyExc_TypeError,
"get type must be numeric or a sequence");
}
Py_RETURN_NONE;
}
#endif /* IS_SDLv2 */
#if IS_SDLv1
static PyObject *
pg_event_get(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int mask = 0;
int loop, num;
PyObject *type = NULL, *list, *e;
int dopump = 1;
int val;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
if (type == NULL || type == Py_None)
mask = SDL_ALLEVENTS;
else {
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
mask |= SDL_EVENTMASK(val);
}
}
else if (pg_IntFromObj(type, &val))
mask = SDL_EVENTMASK(val);
else
return RAISE(PyExc_TypeError,
"eventtype must be numeric or a sequence");
}
list = PyList_New(0);
if (!list)
return NULL;
if (dopump)
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, mask) == 1)
{
e = pgEvent_New(&event);
if (!e) {
Py_DECREF(list);
return NULL;
}
PyList_Append(list, e);
Py_DECREF(e);
}
return list;
}
#else /* IS_SDLv2 */
static PG_INLINE int
_pg_event_append_to_list(PyObject *list, SDL_Event *event)
{
PyObject *e = pgEvent_New(event);
if (!e) {
Py_DECREF(list);
return 0;
}
PyList_Append(list, e);
Py_DECREF(e);
return 1;
}
static PyObject *
pg_event_get(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int loop, num;
PyObject *type = NULL, *list;
int dopump = 1;
int val;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
list = PyList_New(0);
if (!list)
return NULL;
if (dopump)
SDL_PumpEvents();
if (type == NULL || type == Py_None) {
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT,
SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) {
if(!_pg_event_append_to_list(list, &event))
return NULL;
}
return list;
}
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val)) {
Py_DECREF(list);
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
}
if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, val, val) < 0) {
Py_DECREF(list);
return RAISE(pgExc_SDLError, SDL_GetError());
}
if(!_pg_event_append_to_list(list, &event))
return NULL;
}
}
else if (pg_IntFromObj(type, &val)) {
if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, val, val) < 0) {
Py_DECREF(list);
return RAISE(pgExc_SDLError, SDL_GetError());
}
if(!_pg_event_append_to_list(list, &event))
return NULL;
}
else {
Py_DECREF(list);
return RAISE(PyExc_TypeError,
"get type must be numeric or a sequence");
}
return list;
}
#endif /* IS_SDLv2 */
#if IS_SDLv1
static PyObject *
pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int result;
int mask = 0;
int loop, num, noargs = 0;
PyObject *type = NULL;
int val;
int dopump = 1;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
if (!type || type == Py_None) {
mask = SDL_ALLEVENTS;
noargs = 1;
}
else {
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
mask |= SDL_EVENTMASK(val);
}
}
else if (pg_IntFromObj(type, &val))
mask = SDL_EVENTMASK(val);
else
return RAISE(PyExc_TypeError,
"peek type must be numeric or a sequence");
}
if (dopump)
SDL_PumpEvents();
result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, mask);
if (result < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
if (noargs)
return pgEvent_New(result ? &event : NULL);
return PyInt_FromLong(result == 1);
}
#else /* IS_SDLv2 */
static PyObject *
pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int result;
int loop, num;
PyObject *type = NULL;
int val;
int dopump = 1;
static char *kwids[] = {
"eventtype",
"pump",
NULL
};
#if PY3
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids,
&type, &dopump))
return NULL;
#else
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", kwids,
&type, &dopump))
return NULL;
#endif
VIDEO_INIT_CHECK();
if (dopump)
SDL_PumpEvents();
if (type == NULL || type == Py_None) {
result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
if (result < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
return pgEvent_New(result ? &event : NULL);
}
if (PySequence_Check(type)) {
num = PySequence_Size(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(
PyExc_TypeError,
"type sequence must contain valid event types");
result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, val, val);
if (result < 0) {
return RAISE(pgExc_SDLError, SDL_GetError());
} else if (result == 1) {
return PyInt_FromLong(1);
}
}
}
else if (pg_IntFromObj(type, &val)) {
result = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, val, val);
if (result < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
return PyInt_FromLong(result == 1);
}
return RAISE(PyExc_TypeError,
"peek type must be numeric or a sequence");
}
#endif /* IS_SDLv2 */
static PyObject *
pg_event_post(PyObject *self, PyObject *args)
{
pgEventObject *e;
SDL_Event event;
int isblocked = 0;
if (!PyArg_ParseTuple(args, "O!", &pgEvent_Type, &e))
return NULL;
VIDEO_INIT_CHECK();
/* see if the event is blocked before posting it. */
isblocked = SDL_EventState(e->type, SDL_QUERY) == SDL_IGNORE;
if (isblocked) {
/* event is blocked, so we do not post it. */
Py_RETURN_NONE;
}
if (pgEvent_FillUserEvent(e, &event))
return NULL;
#if IS_SDLv1
if (SDL_PushEvent(&event) == -1)
#else /* IS_SDLv2 */
if (SDL_PushEvent(&event) < 0)
#endif /* IS_SDLv2 */
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_NONE;
}
static int
_pg_check_event_in_range(int evt)
{
#if IS_SDLv1
return evt >= 0 && evt < SDL_NUMEVENTS;
#else /* IS_SDLv2 */
return evt >= 0 && evt < PGE_EVENTEND; /* needed for extras */
#endif /* IS_SDLv2 */
}
static PyObject *
pg_event_set_allowed(PyObject *self, PyObject *args)
{
int loop, num;
PyObject *type;
int val;
if (PyTuple_Size(args) != 1)
return RAISE(PyExc_ValueError, "set_allowed requires 1 argument");
VIDEO_INIT_CHECK();
type = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(type)) {
num = PySequence_Length(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(PyExc_TypeError,
"type sequence must contain valid event types");
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event in sequence");
SDL_EventState(val, SDL_ENABLE);
}
}
else if (type == Py_None) {
#if IS_SDLv2
int i;
for (i=SDL_FIRSTEVENT; i<SDL_LASTEVENT; i++) {
SDL_EventState(i, SDL_ENABLE);
}
#else
SDL_EventState(0xFF, SDL_ENABLE);
#endif /* IS_SDLv2 */
} else if (pg_IntFromObj(type, &val)) {
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event");
SDL_EventState(val, SDL_ENABLE);
}
else
return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
Py_RETURN_NONE;
}
static PyObject *
pg_event_set_blocked(PyObject *self, PyObject *args)
{
int loop, num;
PyObject *type;
int val;
if (PyTuple_Size(args) != 1)
return RAISE(PyExc_ValueError, "set_blocked requires 1 argument");
VIDEO_INIT_CHECK();
type = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(type)) {
num = PySequence_Length(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(PyExc_TypeError,
"type sequence must contain valid event types");
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event in sequence");
SDL_EventState(val, SDL_IGNORE);
}
}
else if (type == Py_None) {
#if IS_SDLv2
int i;
for (i=SDL_FIRSTEVENT; i<SDL_LASTEVENT; i++) {
SDL_EventState(i, SDL_IGNORE);
}
#else
SDL_EventState(0xFF, SDL_IGNORE);
#endif /* IS_SDLv2 */
} else if (pg_IntFromObj(type, &val)) {
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event");
SDL_EventState(val, SDL_IGNORE);
}
else
return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
Py_RETURN_NONE;
}
static PyObject *
pg_event_get_blocked(PyObject *self, PyObject *args)
{
int loop, num;
PyObject *type;
int val;
int isblocked = 0;
if (PyTuple_Size(args) != 1)
return RAISE(PyExc_ValueError, "get_blocked requires 1 argument");
VIDEO_INIT_CHECK();
type = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(type)) {
num = PySequence_Length(type);
for (loop = 0; loop < num; ++loop) {
if (!pg_IntFromObjIndex(type, loop, &val))
return RAISE(PyExc_TypeError,
"type sequence must contain valid event types");
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event in sequence");
isblocked |= SDL_EventState(val, SDL_QUERY) == SDL_IGNORE;
}
}
else if (pg_IntFromObj(type, &val)) {
if (!_pg_check_event_in_range(val))
return RAISE(PyExc_ValueError, "Invalid event");
isblocked = SDL_EventState(val, SDL_QUERY) == SDL_IGNORE;
}
else
return RAISE(PyExc_TypeError, "type must be numeric or a sequence");
return PyInt_FromLong(isblocked);
}
static PyMethodDef _event_methods[] = {
#if IS_SDLv2
{"__PYGAMEinit__", pgEvent_AutoInit, METH_NOARGS,
"auto initialize for event module"},
#endif /* IS_SDLv2 */
{"Event", pg_Event, 3, DOC_PYGAMEEVENTEVENT},
{"event_name", event_name, METH_VARARGS, DOC_PYGAMEEVENTEVENTNAME},
{"set_grab", set_grab, METH_VARARGS, DOC_PYGAMEEVENTSETGRAB},
{"get_grab", get_grab, METH_NOARGS, DOC_PYGAMEEVENTGETGRAB},
{"pump", pg_event_pump, METH_NOARGS, DOC_PYGAMEEVENTPUMP},
{"wait", pg_event_wait, METH_NOARGS, DOC_PYGAMEEVENTWAIT},
{"poll", pg_event_poll, METH_NOARGS, DOC_PYGAMEEVENTPOLL},
{"clear", (PyCFunction)pg_event_clear, METH_VARARGS | METH_KEYWORDS, DOC_PYGAMEEVENTCLEAR},
{"get", (PyCFunction)pg_event_get, METH_VARARGS | METH_KEYWORDS, DOC_PYGAMEEVENTGET},
{"peek", (PyCFunction)pg_event_peek, METH_VARARGS | METH_KEYWORDS, DOC_PYGAMEEVENTPEEK},
{"post", pg_event_post, METH_VARARGS, DOC_PYGAMEEVENTPOST},
{"set_allowed", pg_event_set_allowed, METH_VARARGS, DOC_PYGAMEEVENTSETALLOWED},
{"set_blocked", pg_event_set_blocked, METH_VARARGS, DOC_PYGAMEEVENTSETBLOCKED},
{"get_blocked", pg_event_get_blocked, METH_VARARGS, DOC_PYGAMEEVENTGETBLOCKED},
{NULL, NULL, 0, NULL}};
MODINIT_DEFINE(event)
{
PyObject *module, *dict, *apiobj;
int ecode;
static void *c_api[PYGAMEAPI_EVENT_NUMSLOTS];
#if PY3
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
"event",
DOC_PYGAMEEVENT,
-1,
_event_methods,
NULL,
NULL,
NULL,
NULL};
#endif
/* imported needed apis; Do this first so if there is an error
the module is not loaded.
*/
import_pygame_base();
if (PyErr_Occurred()) {
MODINIT_ERROR;
}
/* type preparation */
if (PyType_Ready(&pgEvent_Type) < 0) {
MODINIT_ERROR;
}
/* create the module */
#if PY3
module = PyModule_Create(&_module);
#else
module =
Py_InitModule3(MODPREFIX "event", _event_methods, DOC_PYGAMEEVENT);
#endif
dict = PyModule_GetDict(module);
if (PyDict_SetItemString(dict, "EventType", (PyObject *)&pgEvent_Type) ==
-1) {
DECREF_MOD(module);
MODINIT_ERROR;
}
#if IS_SDLv2
if (!have_registered_events) {
int numevents = SDL_NUMEVENTS - SDL_USEREVENT;
Uint32 user_event = SDL_RegisterEvents(numevents);
if (user_event == (Uint32)-1) {
PyErr_SetString(pgExc_SDLError, "unable to register user events");
DECREF_MOD(module);
MODINIT_ERROR;
}
if (user_event != SDL_USEREVENT) {
PyErr_SetString(PyExc_ImportError,
"Unable to create another module instance");
DECREF_MOD(module);
MODINIT_ERROR;
}
if (SDL_RegisterEvents(PGE_NUMEVENTS) != PGE_EVENTBEGIN) {
PyErr_SetString(PyExc_ImportError,
"Unable to register pygame events");
DECREF_MOD(module);
MODINIT_ERROR;
}
have_registered_events = 1;
}
SDL_SetEventFilter(pg_event_filter, NULL);
#endif /* IS_SDLv2 */
/* export the c api */
#if IS_SDLv2
assert(PYGAMEAPI_EVENT_NUMSLOTS == 6);
#endif /* IS_SDLv2 */
c_api[0] = &pgEvent_Type;
c_api[1] = pgEvent_New;
c_api[2] = pgEvent_New2;
c_api[3] = pgEvent_FillUserEvent;
#if IS_SDLv2
c_api[4] = pg_EnableKeyRepeat;
c_api[5] = pg_GetKeyRepeat;
#endif /* IS_SDLv2 */
apiobj = encapsulate_api(c_api, "event");
if (apiobj == NULL) {
DECREF_MOD(module);
MODINIT_ERROR;
}
ecode = PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
Py_DECREF(apiobj);
if (ecode) {
DECREF_MOD(module);
MODINIT_ERROR;
}
/* Assume if there are events in the user events list
* there is also a registered cleanup callback for them.
*/
if (user_event_objects == NULL) {
pg_RegisterQuit(_pg_user_event_cleanup);
}
MODINIT_RETURN(module);
}
|