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 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
|
/*
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"
// The system message code is only tested on windows, so only
// include it there for now.
#include <SDL_syswm.h>
#define JOYEVENT_INSTANCE_ID "instance_id"
#define JOYEVENT_DEVICE_INDEX "device_index"
/* Define custom functions for peep events, for SDL1/2 compat */
#define PG_PEEP_EVENT(a, b, c, d) SDL_PeepEvents(a, b, c, d, d)
#define PG_PEEP_EVENT_ALL(x, y, z) \
SDL_PeepEvents(x, y, z, SDL_FIRSTEVENT, SDL_LASTEVENT)
/* These are used for checks. The checks are kinda redundant because we
* have proxy events anyways, but this is needed for SDL1 */
#define USEROBJ_CHECK (Sint32)0xFEEDF00D
#define MAX_UINT32 0xFFFFFFFF
#define PG_GET_LIST_LEN 128
// Map joystick instance IDs to device ids for partial backwards compatibility
static PyObject *joy_instance_map = NULL;
/* _custom_event stores the next custom user event type that will be
* returned by pygame.event.custom_type() */
#define _PGE_CUSTOM_EVENT_INIT PGE_USEREVENT + 1
static int _custom_event = _PGE_CUSTOM_EVENT_INIT;
static int _pg_event_is_init = 0;
/* Length of our unicode string in bytes. We need 1 to 3 bytes to store
* our unicode data, so we use a length of 4, to include the NULL byte
* at the end as well */
#define UNICODE_LEN 4
/* This defines the maximum values of key-press and unicode values we
* can store at a time, it is used for determining the unicode attribute
* for KEYUP events. Now that its set to 15, it means that a user can
* simultaneously hold 15 keys (who would do that?) and on release, all
* KEYUP events will have unicode attribute. Why 15? You can set any
* arbitrary number you like ;) */
#define MAX_SCAN_UNICODE 15
/* SDL mutex to be held in the event filter when global state is modified.
* This mutex is intentionally immortalised (never freed during the entire
* duration of the program) because its cleanup can be messy with multiple
* threads trying to use it. Since it's a singleton we don't need to worry
* about memory leaks */
#ifndef __EMSCRIPTEN__
/* emscripten does not allow multithreading for now and SDL_CreateMutex fails.
* Don't bother with mutexes on emscripten for now */
static SDL_mutex *pg_evfilter_mutex = NULL;
#endif
static struct ScanAndUnicode {
SDL_Scancode key;
char unicode[UNICODE_LEN];
} scanunicode[MAX_SCAN_UNICODE] = {{0}};
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 SDL_Event _pg_last_keydown_event = {0};
#ifdef __EMSCRIPTEN__
/* these macros are no-op here */
#define PG_LOCK_EVFILTER_MUTEX
#define PG_UNLOCK_EVFILTER_MUTEX
#else /* not on emscripten */
#define PG_LOCK_EVFILTER_MUTEX \
if (pg_evfilter_mutex) { \
if (SDL_LockMutex(pg_evfilter_mutex) < 0) { \
/* TODO: better error handling with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
printf("Fatal pygame error in SDL_LockMutex: %s", \
SDL_GetError()); \
PG_EXIT(1); \
} \
}
#define PG_UNLOCK_EVFILTER_MUTEX \
if (pg_evfilter_mutex) { \
if (SDL_UnlockMutex(pg_evfilter_mutex) < 0) { \
/* TODO: handle errors with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
printf("Fatal pygame error in SDL_UnlockMutex: %s", \
SDL_GetError()); \
PG_EXIT(1); \
} \
}
#endif /* not on emscripten */
static Uint32
_pg_repeat_callback(Uint32 interval, void *param)
{
/* This function is called in a SDL Timer thread */
PG_LOCK_EVFILTER_MUTEX
/* This assignment only shallow-copies, but SDL_KeyboardEvent does not have
* any pointer values so it's safe to do */
SDL_Event repeat_event_copy = _pg_repeat_event;
int repeat_interval_copy = pg_key_repeat_interval;
PG_UNLOCK_EVFILTER_MUTEX
repeat_event_copy.type = PGE_KEYREPEAT;
repeat_event_copy.key.state = SDL_PRESSED;
repeat_event_copy.key.repeat = 1;
SDL_PushEvent(&repeat_event_copy);
return repeat_interval_copy;
}
/* This function attempts to determine the unicode attribute from
* the keydown/keyup event. This is used as a last-resort, in case we
* could not determine the unicode from TEXTINPUT field. Why?
* Because this function is really basic and cannot determine the
* fancy unicode characters, just the basic ones
*
* One more advantage of this function is that it can return unicode
* for some keys which TEXTINPUT does not provide (which unicode
* attribute of SDL1 provided) */
static char
_pg_unicode_from_event(SDL_Event *event)
{
int capsheld = event->key.keysym.mod & KMOD_CAPS;
int shiftheld = event->key.keysym.mod & KMOD_SHIFT;
int capitalize = (capsheld && !shiftheld) || (shiftheld && !capsheld);
SDL_Keycode key = event->key.keysym.sym;
if (event->key.keysym.mod & KMOD_CTRL) {
/* Control Key held, send control-key related unicode. */
if (key >= SDLK_a && key <= SDLK_z)
return key - SDLK_a + 1;
else {
switch (key) {
case SDLK_2:
case SDLK_AT:
return '\0';
case SDLK_3:
case SDLK_LEFTBRACKET:
return '\x1b';
case SDLK_4:
case SDLK_BACKSLASH:
return '\x1c';
case SDLK_5:
case SDLK_RIGHTBRACKET:
return '\x1d';
case SDLK_6:
case SDLK_CARET:
return '\x1e';
case SDLK_7:
case SDLK_UNDERSCORE:
return '\x1f';
case SDLK_8:
return '\x7f';
}
}
}
if (key < 128) {
if (capitalize && key >= SDLK_a && key <= SDLK_z)
return key + 'A' - 'a';
return key;
}
switch (key) {
case SDLK_KP_PERIOD:
return '.';
case SDLK_KP_DIVIDE:
return '/';
case SDLK_KP_MULTIPLY:
return '*';
case SDLK_KP_MINUS:
return '-';
case SDLK_KP_PLUS:
return '+';
case SDLK_KP_ENTER:
return '\r';
case SDLK_KP_EQUALS:
return '=';
}
return '\0';
}
/* Strip a utf-8 encoded string to contain only first character. Also
* ensure that character can be represented within 3 bytes, because SDL1
* did not support unicode characters that took up 4 bytes. In case this
* bit of code is not clear, here is a python equivalent
def _pg_strip_utf8(string):
if chr(string[0]) <= 0xFFFF:
return string[0]
else:
return ""
*/
static void
_pg_strip_utf8(char *str, char *ret)
{
Uint8 firstbyte = (Uint8)*str;
/* Zero unicode buffer */
memset(ret, 0, UNICODE_LEN);
/* 1111 0000 is 0xF0 */
if (firstbyte >= 0xF0) {
/* Too large UTF8 string, do nothing */
return;
}
/* 1110 0000 is 0xE0 */
if (firstbyte >= 0xE0) {
/* Copy first 3 bytes */
memcpy(ret, str, 3);
}
/* 1100 0000 is 0xC0 */
else if (firstbyte >= 0xC0) {
/* Copy first 2 bytes */
memcpy(ret, str, 2);
}
/* 1000 0000 is 0x80 */
else if (firstbyte < 0x80) {
/* Copy first byte */
memcpy(ret, str, 1);
}
}
static int
_pg_put_event_unicode(SDL_Event *event, char *uni)
{
int i;
for (i = 0; i < MAX_SCAN_UNICODE; i++) {
if (!scanunicode[i].key) {
scanunicode[i].key = event->key.keysym.scancode;
_pg_strip_utf8(uni, scanunicode[i].unicode);
return 1;
}
}
return 0;
}
static PyObject *
_pg_get_event_unicode(SDL_Event *event)
{
char c[20];
int i;
for (i = 0; i < MAX_SCAN_UNICODE; i++) {
if (scanunicode[i].key == event->key.keysym.scancode) {
if (event->type == SDL_KEYUP) {
/* mark the position as free real estate for other
* events to occupy. */
scanunicode[i].key = 0;
}
return PyUnicode_FromString(scanunicode[i].unicode);
}
}
/* fallback to function that determines unicode from the event.
* We try to get the unicode attribute, and store it in memory*/
c[0] = _pg_unicode_from_event(event);
if (_pg_put_event_unicode(event, &c[0]))
return _pg_get_event_unicode(event);
return PyUnicode_FromString("");
}
#define _PG_HANDLE_PROXIFY(name) \
case SDL_##name: \
case PGPOST_##name: \
return proxify ? PGPOST_##name : SDL_##name
#define _PG_HANDLE_PROXIFY_PGE(name) \
case PGE_##name: \
case PGPOST_##name: \
return proxify ? PGPOST_##name : PGE_##name
/* The next three functions are used for proxying SDL events to and from
* PGPOST_* events.
*
* Some SDL1 events (SDL_ACTIVEEVENT, SDL_VIDEORESIZE and SDL_VIDEOEXPOSE)
* are redefined with SDL2, they HAVE to be proxied.
*
* SDL_USEREVENT is not proxied, because with SDL2, pygame assigns a
* different event in place of SDL_USEREVENT, and users use PGE_USEREVENT
*
* Each WINDOW_* event must be defined twice, once as an event, and also
* again, as a proxy event. WINDOW_* events MUST be proxied.
*/
static Uint32
_pg_pgevent_proxify_helper(Uint32 type, Uint8 proxify)
{
switch (type) {
_PG_HANDLE_PROXIFY(ACTIVEEVENT);
_PG_HANDLE_PROXIFY(APP_TERMINATING);
_PG_HANDLE_PROXIFY(APP_LOWMEMORY);
_PG_HANDLE_PROXIFY(APP_WILLENTERBACKGROUND);
_PG_HANDLE_PROXIFY(APP_DIDENTERBACKGROUND);
_PG_HANDLE_PROXIFY(APP_WILLENTERFOREGROUND);
_PG_HANDLE_PROXIFY(APP_DIDENTERFOREGROUND);
_PG_HANDLE_PROXIFY(AUDIODEVICEADDED);
_PG_HANDLE_PROXIFY(AUDIODEVICEREMOVED);
_PG_HANDLE_PROXIFY(CLIPBOARDUPDATE);
_PG_HANDLE_PROXIFY(CONTROLLERAXISMOTION);
_PG_HANDLE_PROXIFY(CONTROLLERBUTTONDOWN);
_PG_HANDLE_PROXIFY(CONTROLLERBUTTONUP);
_PG_HANDLE_PROXIFY(CONTROLLERDEVICEADDED);
_PG_HANDLE_PROXIFY(CONTROLLERDEVICEREMOVED);
_PG_HANDLE_PROXIFY(CONTROLLERDEVICEREMAPPED);
#if SDL_VERSION_ATLEAST(2, 0, 14)
_PG_HANDLE_PROXIFY(CONTROLLERTOUCHPADDOWN);
_PG_HANDLE_PROXIFY(CONTROLLERTOUCHPADMOTION);
_PG_HANDLE_PROXIFY(CONTROLLERTOUCHPADUP);
_PG_HANDLE_PROXIFY(CONTROLLERSENSORUPDATE);
#endif
_PG_HANDLE_PROXIFY(DOLLARGESTURE);
_PG_HANDLE_PROXIFY(DOLLARRECORD);
_PG_HANDLE_PROXIFY(DROPFILE);
_PG_HANDLE_PROXIFY(DROPTEXT);
_PG_HANDLE_PROXIFY(DROPBEGIN);
_PG_HANDLE_PROXIFY(DROPCOMPLETE);
_PG_HANDLE_PROXIFY(FINGERMOTION);
_PG_HANDLE_PROXIFY(FINGERDOWN);
_PG_HANDLE_PROXIFY(FINGERUP);
_PG_HANDLE_PROXIFY(KEYDOWN);
_PG_HANDLE_PROXIFY(KEYUP);
_PG_HANDLE_PROXIFY(KEYMAPCHANGED);
_PG_HANDLE_PROXIFY(JOYAXISMOTION);
_PG_HANDLE_PROXIFY(JOYBALLMOTION);
_PG_HANDLE_PROXIFY(JOYHATMOTION);
_PG_HANDLE_PROXIFY(JOYBUTTONDOWN);
_PG_HANDLE_PROXIFY(JOYBUTTONUP);
_PG_HANDLE_PROXIFY(JOYDEVICEADDED);
_PG_HANDLE_PROXIFY(JOYDEVICEREMOVED);
#if SDL_VERSION_ATLEAST(2, 0, 14)
_PG_HANDLE_PROXIFY(LOCALECHANGED);
#endif
_PG_HANDLE_PROXIFY(MOUSEMOTION);
_PG_HANDLE_PROXIFY(MOUSEBUTTONDOWN);
_PG_HANDLE_PROXIFY(MOUSEBUTTONUP);
_PG_HANDLE_PROXIFY(MOUSEWHEEL);
_PG_HANDLE_PROXIFY(MULTIGESTURE);
_PG_HANDLE_PROXIFY(NOEVENT);
_PG_HANDLE_PROXIFY(QUIT);
_PG_HANDLE_PROXIFY(RENDER_TARGETS_RESET);
_PG_HANDLE_PROXIFY(RENDER_DEVICE_RESET);
_PG_HANDLE_PROXIFY(SYSWMEVENT);
_PG_HANDLE_PROXIFY(TEXTEDITING);
_PG_HANDLE_PROXIFY(TEXTINPUT);
_PG_HANDLE_PROXIFY(VIDEORESIZE);
_PG_HANDLE_PROXIFY(VIDEOEXPOSE);
_PG_HANDLE_PROXIFY_PGE(MIDIIN);
_PG_HANDLE_PROXIFY_PGE(MIDIOUT);
_PG_HANDLE_PROXIFY_PGE(WINDOWSHOWN);
_PG_HANDLE_PROXIFY_PGE(WINDOWHIDDEN);
_PG_HANDLE_PROXIFY_PGE(WINDOWEXPOSED);
_PG_HANDLE_PROXIFY_PGE(WINDOWMOVED);
_PG_HANDLE_PROXIFY_PGE(WINDOWRESIZED);
_PG_HANDLE_PROXIFY_PGE(WINDOWSIZECHANGED);
_PG_HANDLE_PROXIFY_PGE(WINDOWMINIMIZED);
_PG_HANDLE_PROXIFY_PGE(WINDOWMAXIMIZED);
_PG_HANDLE_PROXIFY_PGE(WINDOWRESTORED);
_PG_HANDLE_PROXIFY_PGE(WINDOWENTER);
_PG_HANDLE_PROXIFY_PGE(WINDOWLEAVE);
_PG_HANDLE_PROXIFY_PGE(WINDOWFOCUSGAINED);
_PG_HANDLE_PROXIFY_PGE(WINDOWFOCUSLOST);
_PG_HANDLE_PROXIFY_PGE(WINDOWCLOSE);
_PG_HANDLE_PROXIFY_PGE(WINDOWTAKEFOCUS);
_PG_HANDLE_PROXIFY_PGE(WINDOWHITTEST);
_PG_HANDLE_PROXIFY_PGE(WINDOWICCPROFCHANGED);
_PG_HANDLE_PROXIFY_PGE(WINDOWDISPLAYCHANGED);
default:
return type;
}
}
static Uint32
_pg_pgevent_proxify(Uint32 type)
{
return _pg_pgevent_proxify_helper(type, 1);
}
static Uint32
_pg_pgevent_deproxify(Uint32 type)
{
return _pg_pgevent_proxify_helper(type, 0);
}
static int
_pg_translate_windowevent(void *_, SDL_Event *event)
{
if (event->type == SDL_WINDOWEVENT) {
event->type = PGE_WINDOWSHOWN + event->window.event - 1;
return SDL_EventState(_pg_pgevent_proxify(event->type), SDL_QUERY);
}
return 1;
}
static int SDLCALL
_pg_remove_pending_VIDEORESIZE(void *userdata, SDL_Event *event)
{
SDL_Event *new_event = (SDL_Event *)userdata;
if (event->type == SDL_VIDEORESIZE &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new size event, drop the old ones */
return 0;
}
return 1;
}
static int SDLCALL
_pg_remove_pending_VIDEOEXPOSE(void *userdata, SDL_Event *event)
{
SDL_Event *new_event = (SDL_Event *)userdata;
if (event->type == SDL_VIDEOEXPOSE &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new videoexpose event, drop the old ones */
return 0;
}
return 1;
}
/* SDL 2 to SDL 1.2 event mapping and SDL 1.2 key repeat emulation,
* this can alter events in-place.
* This function can be called from multiple threads, so a mutex must be held
* when this function tries to modify any global state (the mutex is not needed
* on all branches of this function) */
static int SDLCALL
pg_event_filter(void *_, SDL_Event *event)
{
SDL_Event newdownevent, newupevent, newevent = *event;
int x, y, i;
if (event->type == SDL_WINDOWEVENT) {
/* DON'T filter SDL_WINDOWEVENTs here. If we delete events, they
* won't be available to low-level SDL2 either.*/
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED:
SDL_FilterEvents(_pg_remove_pending_VIDEORESIZE, &newevent);
newevent.type = SDL_VIDEORESIZE;
SDL_PushEvent(&newevent);
break;
case SDL_WINDOWEVENT_EXPOSED:
SDL_FilterEvents(_pg_remove_pending_VIDEOEXPOSE, &newevent);
newevent.type = SDL_VIDEOEXPOSE;
SDL_PushEvent(&newevent);
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:
newevent.type = SDL_ACTIVEEVENT;
SDL_PushEvent(&newevent);
}
}
else if (event->type == SDL_KEYDOWN) {
if (event->key.repeat)
return 0;
PG_LOCK_EVFILTER_MUTEX
if (pg_key_repeat_delay > 0) {
if (_pg_repeat_timer)
SDL_RemoveTimer(_pg_repeat_timer);
_pg_repeat_event = *event;
_pg_repeat_timer =
SDL_AddTimer(pg_key_repeat_delay, _pg_repeat_callback, NULL);
}
/* store the keydown event for later in the SDL_TEXTINPUT */
_pg_last_keydown_event = *event;
PG_UNLOCK_EVFILTER_MUTEX
}
else if (event->type == SDL_TEXTINPUT) {
PG_LOCK_EVFILTER_MUTEX
if (_pg_last_keydown_event.type) {
_pg_put_event_unicode(&_pg_last_keydown_event, event->text.text);
_pg_last_keydown_event.type = 0;
}
PG_UNLOCK_EVFILTER_MUTEX
}
else if (event->type == PGE_KEYREPEAT) {
event->type = SDL_KEYDOWN;
}
else if (event->type == SDL_KEYUP) {
PG_LOCK_EVFILTER_MUTEX
if (_pg_repeat_timer && _pg_repeat_event.key.keysym.scancode ==
event->key.keysym.scancode) {
SDL_RemoveTimer(_pg_repeat_timer);
_pg_repeat_timer = 0;
}
PG_UNLOCK_EVFILTER_MUTEX
}
else if (event->type == SDL_MOUSEBUTTONDOWN ||
event->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 (event->type == SDL_MOUSEWHEEL) {
// #691 We are not moving wheel!
if (!event->wheel.y && !event->wheel.x)
return 0;
SDL_GetMouseState(&x, &y);
/* Generate a MouseButtonDown event and MouseButtonUp for
* compatibility. https://wiki.libsdl.org/SDL_MouseWheelEvent
*/
newdownevent.type = SDL_MOUSEBUTTONDOWN;
newdownevent.button.x = x;
newdownevent.button.y = y;
newdownevent.button.state = SDL_PRESSED;
newdownevent.button.clicks = 1;
newdownevent.button.which = event->button.which;
newupevent.type = SDL_MOUSEBUTTONUP;
newupevent.button.x = x;
newupevent.button.y = y;
newupevent.button.state = SDL_RELEASED;
newupevent.button.clicks = 1;
newupevent.button.which = event->button.which;
/* Use a for loop to simulate multiple events, because SDL 1
* works that way */
for (i = 0; i < abs(event->wheel.y); i++) {
/* Do this in the loop because button.button is mutated before it
* is posted from this filter */
if (event->wheel.y > 0) {
newdownevent.button.button = newupevent.button.button =
PGM_BUTTON_WHEELUP | PGM_BUTTON_KEEP;
}
else {
newdownevent.button.button = newupevent.button.button =
PGM_BUTTON_WHEELDOWN | PGM_BUTTON_KEEP;
}
SDL_PushEvent(&newdownevent);
SDL_PushEvent(&newupevent);
}
/* this doesn't work! This is called by SDL, not Python:
if (SDL_PushEvent(&newdownevent) < 0)
return RAISE(pgExc_SDLError, SDL_GetError()), 0;
*/
}
return SDL_EventState(_pg_pgevent_proxify(event->type), SDL_QUERY);
}
/* The two keyrepeat functions below modify state accessed by the event filter,
* so they too need to hold the safety mutex */
static int
pg_EnableKeyRepeat(int delay, int interval)
{
if (delay < 0 || interval < 0) {
PyErr_SetString(PyExc_ValueError,
"delay and interval must equal at least 0");
return -1;
}
PG_LOCK_EVFILTER_MUTEX
pg_key_repeat_delay = delay;
pg_key_repeat_interval = interval;
PG_UNLOCK_EVFILTER_MUTEX
return 0;
}
static void
pg_GetKeyRepeat(int *delay, int *interval)
{
PG_LOCK_EVFILTER_MUTEX
*delay = pg_key_repeat_delay;
*interval = pg_key_repeat_interval;
PG_UNLOCK_EVFILTER_MUTEX
}
static PyObject *
pgEvent_AutoQuit(PyObject *self, PyObject *_null)
{
if (_pg_event_is_init) {
PG_LOCK_EVFILTER_MUTEX
if (_pg_repeat_timer) {
SDL_RemoveTimer(_pg_repeat_timer);
_pg_repeat_timer = 0;
}
PG_UNLOCK_EVFILTER_MUTEX
/* The main reason for _custom_event to be reset here is so we
* can have a unit test that checks if pygame.event.custom_type()
* stops returning new types when they are finished, without that
* test preventing further tests from getting a custom event type.*/
_custom_event = _PGE_CUSTOM_EVENT_INIT;
}
_pg_event_is_init = 0;
Py_RETURN_NONE;
}
static PyObject *
pgEvent_AutoInit(PyObject *self, PyObject *_null)
{
if (!_pg_event_is_init) {
pg_key_repeat_delay = 0;
pg_key_repeat_interval = 0;
#ifndef __EMSCRIPTEN__
if (!pg_evfilter_mutex) {
/* Create mutex only if it has not been created already */
pg_evfilter_mutex = SDL_CreateMutex();
if (!pg_evfilter_mutex)
return RAISE(pgExc_SDLError, SDL_GetError());
}
#endif
SDL_SetEventFilter(pg_event_filter, NULL);
}
_pg_event_is_init = 1;
Py_RETURN_NONE;
}
/* This function can fill an SDL event from pygame event */
static int
pgEvent_FillUserEvent(pgEventObject *e, SDL_Event *event)
{
Py_INCREF(e->dict);
memset(event, 0, sizeof(SDL_Event));
event->type = _pg_pgevent_proxify(e->type);
event->user.code = USEROBJ_CHECK;
event->user.data1 = (void *)e->dict;
event->user.data2 = NULL;
return 0;
}
static char *
_pg_name_from_eventtype(int type)
{
switch (type) {
case SDL_ACTIVEEVENT:
return "ActiveEvent";
case SDL_APP_TERMINATING:
return "AppTerminating";
case SDL_APP_LOWMEMORY:
return "AppLowMemory";
case SDL_APP_WILLENTERBACKGROUND:
return "AppWillEnterBackground";
case SDL_APP_DIDENTERBACKGROUND:
return "AppDidEnterBackground";
case SDL_APP_WILLENTERFOREGROUND:
return "AppWillEnterForeground";
case SDL_APP_DIDENTERFOREGROUND:
return "AppDidEnterForeground";
case SDL_CLIPBOARDUPDATE:
return "ClipboardUpdate";
case SDL_KEYDOWN:
return "KeyDown";
case SDL_KEYUP:
return "KeyUp";
case SDL_KEYMAPCHANGED:
return "KeyMapChanged";
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_LOCALECHANGED:
return "LocaleChanged";
#endif
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 PGE_MIDIIN:
return "MidiIn";
case PGE_MIDIOUT:
return "MidiOut";
case SDL_NOEVENT:
return "NoEvent";
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";
case SDL_CONTROLLERAXISMOTION:
return "ControllerAxisMotion";
case SDL_CONTROLLERBUTTONDOWN:
return "ControllerButtonDown";
case SDL_CONTROLLERBUTTONUP:
return "ControllerButtonUp";
case SDL_CONTROLLERDEVICEADDED:
return "ControllerDeviceAdded";
case SDL_CONTROLLERDEVICEREMOVED:
return "ControllerDeviceRemoved";
case SDL_CONTROLLERDEVICEREMAPPED:
return "ControllerDeviceMapped";
case SDL_JOYDEVICEADDED:
return "JoyDeviceAdded";
case SDL_JOYDEVICEREMOVED:
return "JoyDeviceRemoved";
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_CONTROLLERTOUCHPADDOWN:
return "ControllerTouchpadDown";
case SDL_CONTROLLERTOUCHPADMOTION:
return "ControllerTouchpadMotion";
case SDL_CONTROLLERTOUCHPADUP:
return "ControllerTouchpadUp";
case SDL_CONTROLLERSENSORUPDATE:
return "ControllerSensorUpdate";
#endif /*SDL_VERSION_ATLEAST(2, 0, 14)*/
case SDL_AUDIODEVICEADDED:
return "AudioDeviceAdded";
case SDL_AUDIODEVICEREMOVED:
return "AudioDeviceRemoved";
case SDL_RENDER_TARGETS_RESET:
return "RenderTargetsReset";
case SDL_RENDER_DEVICE_RESET:
return "RenderDeviceReset";
case PGE_WINDOWSHOWN:
return "WindowShown";
case PGE_WINDOWHIDDEN:
return "WindowHidden";
case PGE_WINDOWEXPOSED:
return "WindowExposed";
case PGE_WINDOWMOVED:
return "WindowMoved";
case PGE_WINDOWRESIZED:
return "WindowResized";
case PGE_WINDOWSIZECHANGED:
return "WindowSizeChanged";
case PGE_WINDOWMINIMIZED:
return "WindowMinimized";
case PGE_WINDOWMAXIMIZED:
return "WindowMaximized";
case PGE_WINDOWRESTORED:
return "WindowRestored";
case PGE_WINDOWENTER:
return "WindowEnter";
case PGE_WINDOWLEAVE:
return "WindowLeave";
case PGE_WINDOWFOCUSGAINED:
return "WindowFocusGained";
case PGE_WINDOWFOCUSLOST:
return "WindowFocusLost";
case PGE_WINDOWCLOSE:
return "WindowClose";
case PGE_WINDOWTAKEFOCUS:
return "WindowTakeFocus";
case PGE_WINDOWHITTEST:
return "WindowHitTest";
case PGE_WINDOWICCPROFCHANGED:
return "WindowICCProfChanged";
case PGE_WINDOWDISPLAYCHANGED:
return "WindowDisplayChanged";
}
if (type >= PGE_USEREVENT && type < PG_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);
}
}
static PyObject *
get_joy_guid(int device_index)
{
char strguid[33];
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
SDL_JoystickGetGUIDString(guid, strguid, 33);
return PyUnicode_FromString(strguid);
}
/** Try to insert the instance ID for a new device into the joystick mapping.
*/
void
_joy_map_add(int device_index)
{
int instance_id = (int)SDL_JoystickGetDeviceInstanceID(device_index);
PyObject *k, *v;
if (instance_id != -1) {
k = PyLong_FromLong(instance_id);
v = PyLong_FromLong(device_index);
if (k != NULL && v != NULL) {
PyDict_SetItem(joy_instance_map, k, v);
}
Py_XDECREF(k);
Py_XDECREF(v);
}
}
/** Look up a device ID for an instance ID. */
PyObject *
_joy_map_instance(int instance_id)
{
PyObject *v, *k = PyLong_FromLong(instance_id);
if (!k) {
Py_RETURN_NONE;
}
v = PyDict_GetItem(joy_instance_map, k);
if (v) {
Py_DECREF(k);
Py_INCREF(v);
return v;
}
return k;
}
/** Discard a joystick from the joystick instance -> device mapping. */
void
_joy_map_discard(int instance_id)
{
PyObject *k = PyLong_FromLong(instance_id);
if (k) {
PyDict_DelItem(joy_instance_map, k);
Py_DECREF(k);
}
}
static PyObject *
dict_from_event(SDL_Event *event)
{
PyObject *dict = NULL, *tuple, *obj;
int hx, hy;
long gain;
long state;
/* check if a proxy event or userevent was posted */
if (event->type >= PGPOST_EVENTBEGIN && event->user.code == USEROBJ_CHECK)
return (PyObject *)event->user.data1;
dict = PyDict_New();
if (!dict)
return NULL;
switch (event->type) {
case SDL_VIDEORESIZE:
obj = Py_BuildValue("(ii)", event->window.data1,
event->window.data2);
_pg_insobj(dict, "size", obj);
_pg_insobj(dict, "w", PyLong_FromLong(event->window.data1));
_pg_insobj(dict, "h", PyLong_FromLong(event->window.data2));
break;
case SDL_ACTIVEEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_ENTER:
gain = 1;
state = SDL_APPMOUSEFOCUS;
break;
case SDL_WINDOWEVENT_LEAVE:
gain = 0;
state = SDL_APPMOUSEFOCUS;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
gain = 1;
state = SDL_APPINPUTFOCUS;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
gain = 0;
state = SDL_APPINPUTFOCUS;
break;
case SDL_WINDOWEVENT_MINIMIZED:
gain = 0;
state = SDL_APPACTIVE;
break;
default:
assert(event->window.event == SDL_WINDOWEVENT_RESTORED);
gain = 1;
state = SDL_APPACTIVE;
}
_pg_insobj(dict, "gain", PyLong_FromLong(gain));
_pg_insobj(dict, "state", PyLong_FromLong(state));
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
PG_LOCK_EVFILTER_MUTEX
/* this accesses state also accessed the event filter, so lock */
_pg_insobj(dict, "unicode", _pg_get_event_unicode(event));
PG_UNLOCK_EVFILTER_MUTEX
_pg_insobj(dict, "key", PyLong_FromLong(event->key.keysym.sym));
_pg_insobj(dict, "mod", PyLong_FromLong(event->key.keysym.mod));
_pg_insobj(dict, "scancode",
PyLong_FromLong(event->key.keysym.scancode));
break;
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,
PyLong_FromLong((event->motion.state &
SDL_BUTTON(1)) != 0));
PyTuple_SET_ITEM(tuple, 1,
PyLong_FromLong((event->motion.state &
SDL_BUTTON(2)) != 0));
PyTuple_SET_ITEM(tuple, 2,
PyLong_FromLong((event->motion.state &
SDL_BUTTON(3)) != 0));
_pg_insobj(dict, "buttons", tuple);
}
_pg_insobj(
dict, "touch",
PyBool_FromLong((event->motion.which == SDL_TOUCH_MOUSEID)));
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", PyLong_FromLong(event->button.button));
_pg_insobj(
dict, "touch",
PyBool_FromLong((event->button.which == SDL_TOUCH_MOUSEID)));
break;
case SDL_JOYAXISMOTION:
_pg_insobj(dict, "joy", _joy_map_instance(event->jaxis.which));
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->jaxis.which));
_pg_insobj(dict, "axis", PyLong_FromLong(event->jaxis.axis));
_pg_insobj(dict, "value",
PyFloat_FromDouble(event->jaxis.value / 32767.0));
break;
case SDL_JOYBALLMOTION:
_pg_insobj(dict, "joy", _joy_map_instance(event->jaxis.which));
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->jball.which));
_pg_insobj(dict, "ball", PyLong_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", _joy_map_instance(event->jaxis.which));
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->jhat.which));
_pg_insobj(dict, "hat", PyLong_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", _joy_map_instance(event->jaxis.which));
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->jbutton.which));
_pg_insobj(dict, "button", PyLong_FromLong(event->jbutton.button));
break;
case PGE_WINDOWDISPLAYCHANGED:
_pg_insobj(dict, "display_index",
PyLong_FromLong(event->window.data1));
case PGE_WINDOWMOVED:
case PGE_WINDOWRESIZED:
case PGE_WINDOWSIZECHANGED:
/*other PGE_WINDOW* events do not have attributes */
_pg_insobj(dict, "x", PyLong_FromLong(event->window.data1));
_pg_insobj(dict, "y", PyLong_FromLong(event->window.data2));
break;
case SDL_AUDIODEVICEADDED:
case SDL_AUDIODEVICEREMOVED:
_pg_insobj(
dict, "which",
PyLong_FromLong(
event->adevice
.which)); // The audio device index for the ADDED
// event (valid until next
// SDL_GetNumAudioDevices() call),
// SDL_AudioDeviceID for the REMOVED event
_pg_insobj(dict, "iscapture",
PyLong_FromLong(event->adevice.iscapture));
break;
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",
PyLong_FromLong(event->mgesture.numFingers));
break;
case SDL_MOUSEWHEEL:
/* https://wiki.libsdl.org/SDL_MouseWheelEvent */
#ifndef NO_SDL_MOUSEWHEEL_FLIPPED
_pg_insobj(dict, "flipped",
PyBool_FromLong(event->wheel.direction ==
SDL_MOUSEWHEEL_FLIPPED));
#else
_pg_insobj(dict, "flipped", PyBool_FromLong(0));
#endif
_pg_insobj(dict, "x", PyLong_FromLong(event->wheel.x));
_pg_insobj(dict, "y", PyLong_FromLong(event->wheel.y));
#if SDL_VERSION_ATLEAST(2, 0, 18)
_pg_insobj(dict, "precise_x",
PyFloat_FromDouble((double)event->wheel.preciseX));
_pg_insobj(dict, "precise_y",
PyFloat_FromDouble((double)event->wheel.preciseY));
#else /* ~SDL_VERSION_ATLEAST(2, 0, 18) */
/* fallback to regular x and y when SDL version used does not
* support precise fields */
_pg_insobj(dict, "precise_x",
PyFloat_FromDouble((double)event->wheel.x));
_pg_insobj(dict, "precise_y",
PyFloat_FromDouble((double)event->wheel.y));
#endif /* ~SDL_VERSION_ATLEAST(2, 0, 18) */
_pg_insobj(
dict, "touch",
PyBool_FromLong((event->wheel.which == SDL_TOUCH_MOUSEID)));
break;
case SDL_TEXTINPUT:
/* https://wiki.libsdl.org/SDL_TextInputEvent */
_pg_insobj(dict, "text", PyUnicode_FromString(event->text.text));
break;
case SDL_TEXTEDITING:
/* https://wiki.libsdl.org/SDL_TextEditingEvent */
_pg_insobj(dict, "text", PyUnicode_FromString(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", PyUnicode_FromString(event->drop.file));
SDL_free(event->drop.file);
break;
case SDL_DROPTEXT:
_pg_insobj(dict, "text", PyUnicode_FromString(event->drop.file));
SDL_free(event->drop.file);
break;
case SDL_DROPBEGIN:
case SDL_DROPCOMPLETE:
break;
case SDL_CONTROLLERAXISMOTION:
/* https://wiki.libsdl.org/SDL_ControllerAxisEvent */
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->caxis.which));
_pg_insobj(dict, "axis", PyLong_FromLong(event->caxis.axis));
_pg_insobj(dict, "value", PyLong_FromLong(event->caxis.value));
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
/* https://wiki.libsdl.org/SDL_ControllerButtonEvent */
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->cbutton.which));
_pg_insobj(dict, "button", PyLong_FromLong(event->cbutton.button));
break;
case SDL_CONTROLLERDEVICEADDED:
_pg_insobj(dict, "device_index",
PyLong_FromLong(event->cdevice.which));
_pg_insobj(dict, "guid", get_joy_guid(event->jdevice.which));
break;
case SDL_JOYDEVICEADDED:
_joy_map_add(event->jdevice.which);
_pg_insobj(dict, "device_index",
PyLong_FromLong(event->jdevice.which));
_pg_insobj(dict, "guid", get_joy_guid(event->jdevice.which));
break;
case SDL_CONTROLLERDEVICEREMOVED:
case SDL_CONTROLLERDEVICEREMAPPED:
/* https://wiki.libsdl.org/SDL_ControllerDeviceEvent */
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->cdevice.which));
break;
case SDL_JOYDEVICEREMOVED:
_joy_map_discard(event->jdevice.which);
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->jdevice.which));
break;
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
_pg_insobj(dict, "instance_id",
PyLong_FromLong(event->ctouchpad.which));
_pg_insobj(dict, "touch_id",
PyLong_FromLongLong(event->ctouchpad.touchpad));
_pg_insobj(dict, "finger_id",
PyLong_FromLongLong(event->ctouchpad.finger));
_pg_insobj(dict, "x", PyFloat_FromDouble(event->ctouchpad.x));
_pg_insobj(dict, "y", PyFloat_FromDouble(event->ctouchpad.y));
_pg_insobj(dict, "pressure",
PyFloat_FromDouble(event->ctouchpad.pressure));
break;
#endif /*SDL_VERSION_ATLEAST(2, 0, 14)*/
#ifdef WIN32
case SDL_SYSWMEVENT:
_pg_insobj(dict, "hwnd",
PyLong_FromLongLong(
(long long)(event->syswm.msg->msg.win.hwnd)));
_pg_insobj(dict, "msg",
PyLong_FromLong(event->syswm.msg->msg.win.msg));
_pg_insobj(dict, "wparam",
PyLong_FromLongLong(event->syswm.msg->msg.win.wParam));
_pg_insobj(dict, "lparam",
PyLong_FromLongLong(event->syswm.msg->msg.win.lParam));
break;
#endif /* WIN32 */
#if (defined(unix) || defined(__unix__) || defined(_AIX) || \
defined(__OpenBSD__)) && \
(defined(SDL_VIDEO_DRIVER_X11) && !defined(__CYGWIN32__) && \
!defined(ENABLE_NANOX) && !defined(__QNXNTO__))
case SDL_SYSWMEVENT:
if (event->syswm.msg->subsystem == SDL_SYSWM_X11) {
XEvent *xevent = (XEvent *)&event->syswm.msg->msg.x11.event;
obj =
PyBytes_FromStringAndSize((char *)xevent, sizeof(XEvent));
_pg_insobj(dict, "event", obj);
}
break;
#endif /* (defined(unix) || ... */
} /* switch (event->type) */
/* Events that dont have any attributes are not handled in switch
* statement */
SDL_Window *window;
switch (event->type) {
case PGE_WINDOWSHOWN:
case PGE_WINDOWHIDDEN:
case PGE_WINDOWEXPOSED:
case PGE_WINDOWMOVED:
case PGE_WINDOWRESIZED:
case PGE_WINDOWSIZECHANGED:
case PGE_WINDOWMINIMIZED:
case PGE_WINDOWMAXIMIZED:
case PGE_WINDOWRESTORED:
case PGE_WINDOWENTER:
case PGE_WINDOWLEAVE:
case PGE_WINDOWFOCUSGAINED:
case PGE_WINDOWFOCUSLOST:
case PGE_WINDOWCLOSE:
case PGE_WINDOWTAKEFOCUS:
case PGE_WINDOWHITTEST:
case PGE_WINDOWICCPROFCHANGED:
case PGE_WINDOWDISPLAYCHANGED: {
window = SDL_GetWindowFromID(event->window.windowID);
break;
}
case SDL_TEXTEDITING: {
window = SDL_GetWindowFromID(event->edit.windowID);
break;
}
case SDL_TEXTINPUT: {
window = SDL_GetWindowFromID(event->text.windowID);
break;
}
case SDL_DROPBEGIN:
case SDL_DROPCOMPLETE:
case SDL_DROPTEXT:
case SDL_DROPFILE: {
window = SDL_GetWindowFromID(event->drop.windowID);
break;
}
case SDL_KEYDOWN:
case SDL_KEYUP: {
window = SDL_GetWindowFromID(event->key.windowID);
break;
}
case SDL_MOUSEWHEEL: {
window = SDL_GetWindowFromID(event->wheel.windowID);
break;
}
case SDL_MOUSEMOTION: {
window = SDL_GetWindowFromID(event->motion.windowID);
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
window = SDL_GetWindowFromID(event->button.windowID);
break;
}
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP: {
window = SDL_GetWindowFromID(event->tfinger.windowID);
break;
}
#endif
default: {
return dict;
}
}
PyObject *pgWindow;
if (!window || !(pgWindow = SDL_GetWindowData(window, "pg_window"))) {
pgWindow = Py_None;
}
Py_INCREF(pgWindow);
_pg_insobj(dict, "window", pgWindow);
return dict;
}
/* event object internals */
static void
pg_event_dealloc(PyObject *self)
{
pgEventObject *e = (pgEventObject *)self;
Py_XDECREF(e->dict);
PyObject_Free(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;
return PyUnicode_FromFormat("<Event(%d-%s %S)>", e->type,
_pg_name_from_eventtype(e->type), e->dict);
}
static int
_pg_event_nonzero(pgEventObject *self)
{
return self->type != SDL_NOEVENT;
}
static PyNumberMethods pg_event_as_number = {
.nb_bool = (inquiry)_pg_event_nonzero,
};
static PyTypeObject pgEvent_Type;
#define pgEvent_Check(x) ((x)->ob_type == &pgEvent_Type)
#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 int
_pg_event_populate(pgEventObject *event, int type, PyObject *dict)
{
event->type = _pg_pgevent_deproxify(type);
if (!dict) {
dict = PyDict_New();
if (!dict) {
PyErr_NoMemory();
return -1;
}
}
else {
if (PyDict_GetItemString(dict, "type")) {
PyErr_SetString(PyExc_ValueError,
"redundant type field in event dict");
return -1;
}
Py_INCREF(dict);
}
event->dict = dict;
return 0;
}
static int
pg_event_init(pgEventObject *self, PyObject *args, PyObject *kwargs)
{
int type;
PyObject *dict = NULL;
if (!PyArg_ParseTuple(args, "i|O!", &type, &PyDict_Type, &dict)) {
return -1;
}
if (!dict) {
dict = PyDict_New();
if (!dict) {
PyErr_NoMemory();
return -1;
}
}
else {
Py_INCREF(dict);
}
if (kwargs) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(kwargs, &pos, &key, &value)) {
if (PyDict_SetItem(dict, key, value) < 0) {
Py_DECREF(dict);
return -1;
}
}
}
if (_pg_event_populate(self, type, dict) == -1) {
return -1;
}
Py_DECREF(dict);
return 0;
}
static PyTypeObject pgEvent_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.event.Event",
.tp_basicsize = sizeof(pgEventObject),
.tp_dealloc = pg_event_dealloc,
.tp_repr = pg_event_str,
.tp_as_number = &pg_event_as_number,
#ifdef PYPY_VERSION
.tp_getattro = pg_EventGetAttr,
.tp_setattro = pg_EventSetAttr,
#else
.tp_getattro = PyObject_GenericGetAttr,
.tp_setattro = PyObject_GenericSetAttr,
#endif
.tp_doc = DOC_PYGAMEEVENTEVENT,
.tp_richcompare = pg_event_richcompare,
.tp_members = pg_event_members,
.tp_dictoffset = offsetof(pgEventObject, dict),
.tp_init = (initproc)pg_event_init,
.tp_new = PyType_GenericNew,
};
static PyObject *
pgEvent_New(SDL_Event *event)
{
pgEventObject *e;
e = PyObject_New(pgEventObject, &pgEvent_Type);
if (!e)
return PyErr_NoMemory();
if (event) {
e->type = _pg_pgevent_deproxify(event->type);
e->dict = dict_from_event(event);
}
else {
e->type = SDL_NOEVENT;
e->dict = PyDict_New();
}
if (!e->dict) {
PyObject_Free(e);
return PyErr_NoMemory();
}
return (PyObject *)e;
}
static PyObject *
pgEvent_New2(int type, PyObject *dict)
{
pgEventObject *e;
e = PyObject_New(pgEventObject, &pgEvent_Type);
if (!e)
return PyErr_NoMemory();
if (_pg_event_populate(e, type, dict) == -1) {
PyObject_Free(e);
return NULL;
}
return (PyObject *)e;
}
/* event module functions */
static PyObject *
event_name(PyObject *self, PyObject *args, PyObject *kwargs)
{
int type;
static char *keywords[] = {
"type",
NULL,
};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", keywords, &type))
return NULL;
return PyUnicode_FromString(_pg_name_from_eventtype(type));
}
static PyObject *
set_grab(PyObject *self, PyObject *arg)
{
int doit = PyObject_IsTrue(arg);
if (doit == -1)
return NULL;
VIDEO_INIT_CHECK();
SDL_Window *win = pg_GetDefaultWindow();
if (win) {
if (doit) {
SDL_SetWindowGrab(win, SDL_TRUE);
if (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE)
SDL_SetRelativeMouseMode(1);
else
SDL_SetRelativeMouseMode(0);
}
else {
SDL_SetWindowGrab(win, SDL_FALSE);
SDL_SetRelativeMouseMode(0);
}
}
Py_RETURN_NONE;
}
static PyObject *
get_grab(PyObject *self, PyObject *_null)
{
SDL_Window *win;
SDL_bool mode = SDL_FALSE;
VIDEO_INIT_CHECK();
win = pg_GetDefaultWindow();
if (win)
mode = SDL_GetWindowGrab(win);
return PyBool_FromLong(mode);
}
static PyObject *
set_keyboard_grab(PyObject *self, PyObject *arg)
{
#if SDL_VERSION_ATLEAST(2, 0, 16)
VIDEO_INIT_CHECK();
int doit = PyObject_IsTrue(arg);
if (doit == -1)
return NULL;
SDL_Window *win = pg_GetDefaultWindow();
if (win) {
if (doit) {
SDL_SetWindowKeyboardGrab(win, SDL_TRUE);
}
else {
SDL_SetWindowKeyboardGrab(win, SDL_FALSE);
}
}
#endif
Py_RETURN_NONE;
}
static PyObject *
get_keyboard_grab(PyObject *self, PyObject *_null)
{
#if SDL_VERSION_ATLEAST(2, 0, 16)
SDL_Window *win;
SDL_bool mode = SDL_FALSE;
VIDEO_INIT_CHECK();
win = pg_GetDefaultWindow();
if (win)
mode = SDL_GetWindowKeyboardGrab(win);
return PyBool_FromLong(mode);
#else
Py_RETURN_NONE;
#endif
}
static void
_pg_event_pump(int dopump)
{
if (dopump) {
SDL_PumpEvents();
}
/* We need to translate WINDOWEVENTS. But if we do that from the
* from event filter, internal SDL stuff that rely on WINDOWEVENT
* might break. So after every event pump, we translate events from
* here */
SDL_FilterEvents(_pg_translate_windowevent, NULL);
}
static int
_pg_event_wait(SDL_Event *event, int timeout)
{
/* Custom re-implementation of SDL_WaitEventTimeout, doing this has
* many advantages. This is copied from SDL source code, with a few
* minor modifications */
Uint32 finish = 0;
if (timeout > 0)
finish = SDL_GetTicks() + timeout;
while (1) {
_pg_event_pump(1); /* Use our custom pump here */
switch (PG_PEEP_EVENT_ALL(event, 1, SDL_GETEVENT)) {
case -1:
return 0; /* Because this never happens, SDL does it too*/
case 1:
return 1;
default:
if (timeout >= 0 && SDL_GetTicks() >= finish) {
/* no events */
return 0;
}
SDL_Delay(1);
}
}
}
static PyObject *
pg_event_pump(PyObject *self, PyObject *_null)
{
VIDEO_INIT_CHECK();
_pg_event_pump(1);
Py_RETURN_NONE;
}
static PyObject *
pg_event_poll(PyObject *self, PyObject *_null)
{
SDL_Event event;
VIDEO_INIT_CHECK();
/* polling is just waiting for 0 timeout */
if (!_pg_event_wait(&event, 0))
return pgEvent_New(NULL);
return pgEvent_New(&event);
}
static PyObject *
pg_event_wait(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
int status, timeout = 0;
static char *kwids[] = {"timeout", NULL};
VIDEO_INIT_CHECK();
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwids, &timeout)) {
return NULL;
}
if (!timeout)
timeout = -1;
Py_BEGIN_ALLOW_THREADS;
status = _pg_event_wait(&event, timeout);
Py_END_ALLOW_THREADS;
if (!status)
return pgEvent_New(NULL);
return pgEvent_New(&event);
}
static int
_pg_eventtype_from_seq(PyObject *seq, int ind)
{
int val = 0;
if (!pg_IntFromObjIndex(seq, ind, &val)) {
PyErr_SetString(PyExc_TypeError,
"type sequence must contain valid event types");
return -1;
}
if (val < 0 || val >= PG_NUMEVENTS) {
PyErr_SetString(PyExc_ValueError, "event type out of range");
return -1;
}
return val;
}
static PyObject *
_pg_eventtype_as_seq(PyObject *obj, Py_ssize_t *len)
{
*len = 1;
if (PySequence_Check(obj)) {
*len = PySequence_Size(obj);
/* The returned object gets decref'd later, so incref now */
Py_INCREF(obj);
return obj;
}
else if (PyLong_Check(obj))
return Py_BuildValue("(O)", obj);
else
return RAISE(PyExc_TypeError,
"event type must be numeric or a sequence");
}
static void
_pg_flush_events(Uint32 type)
{
if (type == MAX_UINT32)
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
else {
SDL_FlushEvent(type);
SDL_FlushEvent(_pg_pgevent_proxify(type));
}
}
static PyObject *
pg_event_clear(PyObject *self, PyObject *args, PyObject *kwargs)
{
Py_ssize_t len;
int loop, type;
PyObject *seq, *obj = NULL;
int dopump = 1;
static char *kwids[] = {"eventtype", "pump", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids, &obj,
&dopump))
return NULL;
VIDEO_INIT_CHECK();
_pg_event_pump(dopump);
if (obj == NULL || obj == Py_None) {
_pg_flush_events(MAX_UINT32);
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq) /* error aldready set */
return NULL;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL; /* PyErr aldready set */
}
_pg_flush_events(type);
}
Py_DECREF(seq);
}
Py_RETURN_NONE;
}
static int
_pg_event_append_to_list(PyObject *list, SDL_Event *event)
{
/* The caller of this function must handle decref of list on error */
PyObject *e = pgEvent_New(event);
if (!e) /* Exception already set. */
return 0;
if (PyList_Append(list, e)) {
Py_DECREF(e);
return 0; /* Exception already set. */
}
Py_DECREF(e);
return 1;
}
static PyObject *
_pg_get_all_events_except(PyObject *obj)
{
SDL_Event event;
Py_ssize_t len;
int loop, type, ret;
PyObject *seq, *list;
SDL_Event *filtered_events;
int filtered_index = 0;
int filtered_events_len = 16;
SDL_Event eventbuf[PG_GET_LIST_LEN];
filtered_events = malloc(sizeof(SDL_Event) * filtered_events_len);
if (!filtered_events)
return PyErr_NoMemory();
list = PyList_New(0);
if (!list) {
free(filtered_events);
return PyErr_NoMemory();
}
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
goto error;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1)
goto error;
do {
ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, type);
if (ret < 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
else if (ret > 0) {
if (filtered_index == filtered_events_len) {
SDL_Event *new_filtered_events =
malloc(sizeof(SDL_Event) * filtered_events_len * 4);
if (new_filtered_events == NULL) {
goto error;
}
memcpy(new_filtered_events, filtered_events,
sizeof(SDL_Event) * filtered_events_len);
filtered_events_len *= 4;
free(filtered_events);
filtered_events = new_filtered_events;
}
filtered_events[filtered_index] = event;
filtered_index++;
}
} while (ret);
do {
ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT,
_pg_pgevent_proxify(type));
if (ret < 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
else if (ret > 0) {
if (filtered_index == filtered_events_len) {
SDL_Event *new_filtered_events =
malloc(sizeof(SDL_Event) * filtered_events_len * 4);
if (new_filtered_events == NULL) {
free(filtered_events);
goto error;
}
memcpy(new_filtered_events, filtered_events,
sizeof(SDL_Event) * filtered_events_len);
filtered_events_len *= 4;
free(filtered_events);
filtered_events = new_filtered_events;
}
filtered_events[filtered_index] = event;
filtered_index++;
}
} while (ret);
}
do {
len = PG_PEEP_EVENT_ALL(eventbuf, PG_GET_LIST_LEN, SDL_GETEVENT);
if (len == -1) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
for (loop = 0; loop < len; loop++) {
if (!_pg_event_append_to_list(list, &eventbuf[loop]))
goto error;
}
} while (len == PG_GET_LIST_LEN);
PG_PEEP_EVENT_ALL(filtered_events, filtered_index, SDL_ADDEVENT);
free(filtered_events);
Py_DECREF(seq);
return list;
error:
/* While doing a goto here, PyErr must be set */
free(filtered_events);
Py_DECREF(list);
Py_XDECREF(seq);
return NULL;
}
static PyObject *
_pg_get_all_events(void)
{
SDL_Event eventbuf[PG_GET_LIST_LEN];
PyObject *list;
int loop, len = PG_GET_LIST_LEN;
list = PyList_New(0);
if (!list)
return PyErr_NoMemory();
while (len == PG_GET_LIST_LEN) {
len = PG_PEEP_EVENT_ALL(eventbuf, PG_GET_LIST_LEN, SDL_GETEVENT);
if (len == -1) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
for (loop = 0; loop < len; loop++) {
if (!_pg_event_append_to_list(list, &eventbuf[loop]))
goto error;
}
}
return list;
error:
Py_DECREF(list);
return NULL;
}
static PyObject *
_pg_get_seq_events(PyObject *obj)
{
Py_ssize_t len;
SDL_Event event;
int loop, type, ret;
PyObject *seq, *list;
list = PyList_New(0);
if (!list)
return PyErr_NoMemory();
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
goto error;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1)
goto error;
do {
ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, type);
if (ret < 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
else if (ret > 0) {
if (!_pg_event_append_to_list(list, &event))
goto error;
}
} while (ret);
do {
ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT,
_pg_pgevent_proxify(type));
if (ret < 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
goto error;
}
else if (ret > 0) {
if (!_pg_event_append_to_list(list, &event))
goto error;
}
} while (ret);
}
Py_DECREF(seq);
return list;
error:
/* While doing a goto here, PyErr must be set */
Py_DECREF(list);
Py_XDECREF(seq);
return NULL;
}
static PyObject *
pg_event_get(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *obj_evtype = NULL;
PyObject *obj_exclude = NULL;
int dopump = 1;
static char *kwids[] = {"eventtype", "pump", "exclude", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OpO", kwids, &obj_evtype,
&dopump, &obj_exclude))
return NULL;
VIDEO_INIT_CHECK();
_pg_event_pump(dopump);
if (obj_evtype == NULL || obj_evtype == Py_None) {
if (obj_exclude != NULL && obj_exclude != Py_None) {
return _pg_get_all_events_except(obj_exclude);
}
return _pg_get_all_events();
}
else {
if (obj_exclude != NULL && obj_exclude != Py_None) {
return RAISE(
pgExc_SDLError,
"Invalid combination of excluded and included event type");
}
return _pg_get_seq_events(obj_evtype);
}
}
static PyObject *
pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)
{
SDL_Event event;
Py_ssize_t len;
int type, loop, res;
PyObject *seq, *obj = NULL;
int dopump = 1;
static char *kwids[] = {"eventtype", "pump", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Op", kwids, &obj,
&dopump))
return NULL;
VIDEO_INIT_CHECK();
_pg_event_pump(dopump);
if (obj == NULL || obj == Py_None) {
res = PG_PEEP_EVENT_ALL(&event, 1, SDL_PEEKEVENT);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
return pgEvent_New(res ? &event : NULL);
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
return NULL;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL;
}
res = PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT, type);
if (res) {
Py_DECREF(seq);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
res = PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT,
_pg_pgevent_proxify(type));
if (res) {
Py_DECREF(seq);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
}
Py_DECREF(seq);
Py_RETURN_FALSE; /* No event type match. */
}
}
/* You might notice how we do event blocking stuff on proxy events and
* not the real SDL events. We do this because we want SDL events to pass
* through our event filter, to do emulation stuff correctly. Then the
* event is filtered after that */
static PyObject *
pg_event_post(PyObject *self, PyObject *obj)
{
SDL_Event event;
pgEventObject *e;
int ret;
VIDEO_INIT_CHECK();
if (!pgEvent_Check(obj))
return RAISE(PyExc_TypeError, "argument must be an Event object");
e = (pgEventObject *)obj;
if (SDL_EventState(_pg_pgevent_proxify(e->type), SDL_QUERY) == SDL_IGNORE)
Py_RETURN_FALSE;
pgEvent_FillUserEvent(e, &event);
ret = SDL_PushEvent(&event);
if (ret == 1)
Py_RETURN_TRUE;
else {
Py_DECREF(e->dict);
if (ret == 0)
Py_RETURN_FALSE;
else
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
static PyObject *
pg_event_set_allowed(PyObject *self, PyObject *obj)
{
Py_ssize_t len;
int loop, type;
PyObject *seq;
VIDEO_INIT_CHECK();
if (obj == Py_None) {
int i;
for (i = SDL_FIRSTEVENT; i < SDL_LASTEVENT; i++) {
SDL_EventState(i, SDL_ENABLE);
}
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
return NULL;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL;
}
SDL_EventState(_pg_pgevent_proxify(type), SDL_ENABLE);
}
Py_DECREF(seq);
}
Py_RETURN_NONE;
}
static PyObject *
pg_event_set_blocked(PyObject *self, PyObject *obj)
{
Py_ssize_t len;
int loop, type;
PyObject *seq;
VIDEO_INIT_CHECK();
if (obj == Py_None) {
int i;
/* Start at PGPOST_EVENTBEGIN */
for (i = PGPOST_EVENTBEGIN; i < SDL_LASTEVENT; i++) {
SDL_EventState(i, SDL_IGNORE);
}
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
return NULL;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL;
}
SDL_EventState(_pg_pgevent_proxify(type), SDL_IGNORE);
}
Py_DECREF(seq);
}
/* Never block SDL_WINDOWEVENT, we need them for translation */
SDL_EventState(SDL_WINDOWEVENT, SDL_ENABLE);
/* Never block PGE_KEYREPEAT too, its needed for pygame internal use */
SDL_EventState(PGE_KEYREPEAT, SDL_ENABLE);
Py_RETURN_NONE;
}
static PyObject *
pg_event_get_blocked(PyObject *self, PyObject *obj)
{
Py_ssize_t len;
int loop, type, isblocked = 0;
PyObject *seq;
VIDEO_INIT_CHECK();
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
return NULL;
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL;
}
if (SDL_EventState(_pg_pgevent_proxify(type), SDL_QUERY) ==
SDL_IGNORE) {
isblocked = 1;
break;
}
}
Py_DECREF(seq);
return PyBool_FromLong(isblocked);
}
static PyObject *
pg_event_custom_type(PyObject *self, PyObject *_null)
{
if (_custom_event < PG_NUMEVENTS)
return PyLong_FromLong(_custom_event++);
else
return RAISE(pgExc_SDLError,
"pygame.event.custom_type made too many event types.");
}
static PyMethodDef _event_methods[] = {
{"_internal_mod_init", (PyCFunction)pgEvent_AutoInit, METH_NOARGS,
"auto initialize for event module"},
{"_internal_mod_quit", (PyCFunction)pgEvent_AutoQuit, METH_NOARGS,
"auto quit for event module"},
{"event_name", (PyCFunction)event_name, METH_VARARGS | METH_KEYWORDS,
DOC_PYGAMEEVENTEVENTNAME},
{"set_grab", set_grab, METH_O, DOC_PYGAMEEVENTSETGRAB},
{"get_grab", (PyCFunction)get_grab, METH_NOARGS, DOC_PYGAMEEVENTGETGRAB},
{"set_keyboard_grab", set_keyboard_grab, METH_O,
DOC_PYGAMEEVENTSETKEYBOARDGRAB},
{"get_keyboard_grab", (PyCFunction)get_keyboard_grab, METH_NOARGS,
DOC_PYGAMEEVENTGETKEYBOARDGRAB},
{"pump", (PyCFunction)pg_event_pump, METH_NOARGS, DOC_PYGAMEEVENTPUMP},
{"wait", (PyCFunction)pg_event_wait, METH_VARARGS | METH_KEYWORDS,
DOC_PYGAMEEVENTWAIT},
{"poll", (PyCFunction)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", (PyCFunction)pg_event_post, METH_O, DOC_PYGAMEEVENTPOST},
{"set_allowed", (PyCFunction)pg_event_set_allowed, METH_O,
DOC_PYGAMEEVENTSETALLOWED},
{"set_blocked", (PyCFunction)pg_event_set_blocked, METH_O,
DOC_PYGAMEEVENTSETBLOCKED},
{"get_blocked", (PyCFunction)pg_event_get_blocked, METH_O,
DOC_PYGAMEEVENTGETBLOCKED},
{"custom_type", (PyCFunction)pg_event_custom_type, METH_NOARGS,
DOC_PYGAMEEVENTCUSTOMTYPE},
{NULL, NULL, 0, NULL}};
MODINIT_DEFINE(event)
{
PyObject *module, *apiobj;
static void *c_api[PYGAMEAPI_EVENT_NUMSLOTS];
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
"event",
DOC_PYGAMEEVENT,
-1,
_event_methods,
NULL,
NULL,
NULL,
NULL};
/* imported needed apis; Do this first so if there is an error
the module is not loaded.
*/
import_pygame_base();
if (PyErr_Occurred()) {
return NULL;
}
/* type preparation */
if (PyType_Ready(&pgEvent_Type) < 0) {
return NULL;
}
/* create the module */
module = PyModule_Create(&_module);
if (!module) {
return NULL;
}
joy_instance_map = PyDict_New();
/* need to keep a reference for use in the module */
Py_XINCREF(joy_instance_map);
if (PyModule_AddObject(module, "_joy_instance_map", joy_instance_map)) {
Py_XDECREF(joy_instance_map);
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgEvent_Type);
if (PyModule_AddObject(module, "EventType", (PyObject *)&pgEvent_Type)) {
Py_DECREF(&pgEvent_Type);
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgEvent_Type);
if (PyModule_AddObject(module, "Event", (PyObject *)&pgEvent_Type)) {
Py_DECREF(&pgEvent_Type);
Py_DECREF(module);
return NULL;
}
/* export the c api */
assert(PYGAMEAPI_EVENT_NUMSLOTS == 6);
c_api[0] = &pgEvent_Type;
c_api[1] = pgEvent_New;
c_api[2] = pgEvent_New2;
c_api[3] = pgEvent_FillUserEvent;
c_api[4] = pg_EnableKeyRepeat;
c_api[5] = pg_GetKeyRepeat;
apiobj = encapsulate_api(c_api, "event");
if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
Py_XDECREF(apiobj);
Py_DECREF(module);
return NULL;
}
SDL_RegisterEvents(PG_NUMEVENTS - SDL_USEREVENT);
return module;
}
|