Hash :
9e6249fa
Author :
Date :
2022-01-08T19:24:47
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WAYLAND
#include "SDL_stdinc.h"
#include "SDL_timer.h"
#include "../../core/unix/SDL_poll.h"
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
#include "../../events/scancodes_xfree86.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
#include "text-input-unstable-v3-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
#ifdef SDL_INPUT_LINUXEV
#include <linux/input.h>
#else
#define BTN_LEFT (0x110)
#define BTN_RIGHT (0x111)
#define BTN_MIDDLE (0x112)
#define BTN_SIDE (0x113)
#define BTN_EXTRA (0x114)
#endif
#include <sys/select.h>
#include <sys/mman.h>
#include <poll.h>
#include <unistd.h>
#include <errno.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include "../../events/imKStoUCS.h"
/* Weston uses a ratio of 10 units per scroll tick */
#define WAYLAND_WHEEL_AXIS_UNIT 10
static const struct {
xkb_keysym_t keysym;
SDL_KeyCode keycode;
} KeySymToSDLKeyCode[] = {
{ XKB_KEY_Shift_L, SDLK_LSHIFT },
{ XKB_KEY_Shift_R, SDLK_RSHIFT },
{ XKB_KEY_Control_L, SDLK_LCTRL },
{ XKB_KEY_Control_R, SDLK_RCTRL },
{ XKB_KEY_Caps_Lock, SDLK_CAPSLOCK },
{ XKB_KEY_Alt_L, SDLK_LALT },
{ XKB_KEY_Alt_R, SDLK_RALT },
{ XKB_KEY_Meta_L, SDLK_LGUI },
{ XKB_KEY_Meta_R, SDLK_RGUI },
{ XKB_KEY_Super_L, SDLK_LGUI },
{ XKB_KEY_Super_R, SDLK_RGUI },
{ XKB_KEY_Hyper_L, SDLK_LGUI },
{ XKB_KEY_Hyper_R, SDLK_RGUI },
};
struct SDL_WaylandTouchPoint {
SDL_TouchID id;
float x;
float y;
struct wl_surface* surface;
struct SDL_WaylandTouchPoint* prev;
struct SDL_WaylandTouchPoint* next;
};
struct SDL_WaylandTouchPointList {
struct SDL_WaylandTouchPoint* head;
struct SDL_WaylandTouchPoint* tail;
};
static struct SDL_WaylandTouchPointList touch_points = {NULL, NULL};
static SDL_KeyCode
Wayland_KeySymToSDLKeyCode(xkb_keysym_t keysym)
{
int i;
for (i = 0; i < SDL_arraysize(KeySymToSDLKeyCode); ++i) {
if (keysym == KeySymToSDLKeyCode[i].keysym) {
return KeySymToSDLKeyCode[i].keycode;
}
}
return SDLK_UNKNOWN;
}
static void
touch_add(SDL_TouchID id, float x, float y, struct wl_surface *surface)
{
struct SDL_WaylandTouchPoint* tp = SDL_malloc(sizeof(struct SDL_WaylandTouchPoint));
tp->id = id;
tp->x = x;
tp->y = y;
tp->surface = surface;
if (touch_points.tail) {
touch_points.tail->next = tp;
tp->prev = touch_points.tail;
} else {
touch_points.head = tp;
tp->prev = NULL;
}
touch_points.tail = tp;
tp->next = NULL;
}
static void
touch_update(SDL_TouchID id, float x, float y)
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
tp->x = x;
tp->y = y;
}
tp = tp->next;
}
}
static void
touch_del(SDL_TouchID id, float* x, float* y, struct wl_surface **surface)
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
*x = tp->x;
*y = tp->y;
*surface = tp->surface;
if (tp->prev) {
tp->prev->next = tp->next;
} else {
touch_points.head = tp->next;
}
if (tp->next) {
tp->next->prev = tp->prev;
} else {
touch_points.tail = tp->prev;
}
{
struct SDL_WaylandTouchPoint *next = tp->next;
SDL_free(tp);
tp = next;
}
} else {
tp = tp->next;
}
}
}
static struct wl_surface*
touch_surface(SDL_TouchID id)
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
return tp->surface;
}
tp = tp->next;
}
return NULL;
}
/* Returns SDL_TRUE if a key repeat event was due */
static SDL_bool
keyboard_repeat_handle(SDL_WaylandKeyboardRepeat* repeat_info, uint32_t elapsed)
{
SDL_bool ret = SDL_FALSE;
while ((elapsed - repeat_info->next_repeat_ms) < 0x80000000U) {
if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
SDL_SendKeyboardKey(SDL_PRESSED, repeat_info->scancode);
}
if (repeat_info->text[0]) {
SDL_SendKeyboardText(repeat_info->text);
}
repeat_info->next_repeat_ms += 1000 / repeat_info->repeat_rate;
ret = SDL_TRUE;
}
return ret;
}
static void
keyboard_repeat_clear(SDL_WaylandKeyboardRepeat* repeat_info) {
if (!repeat_info->is_initialized) {
return;
}
repeat_info->is_key_down = SDL_FALSE;
}
static void
keyboard_repeat_set(SDL_WaylandKeyboardRepeat* repeat_info, uint32_t wl_press_time,
uint32_t scancode, SDL_bool has_text, char text[8]) {
if (!repeat_info->is_initialized || !repeat_info->repeat_rate) {
return;
}
repeat_info->is_key_down = SDL_TRUE;
repeat_info->wl_press_time = wl_press_time;
repeat_info->sdl_press_time = SDL_GetTicks();
repeat_info->next_repeat_ms = repeat_info->repeat_delay;
repeat_info->scancode = scancode;
if (has_text) {
SDL_memcpy(repeat_info->text, text, 8);
} else {
repeat_info->text[0] = '\0';
}
}
static SDL_bool keyboard_repeat_is_set(SDL_WaylandKeyboardRepeat* repeat_info) {
return repeat_info->is_initialized && repeat_info->is_key_down;
}
void
Wayland_SendWakeupEvent(_THIS, SDL_Window *window)
{
SDL_VideoData *d = _this->driverdata;
/* TODO: Maybe use a pipe to avoid the compositor roundtrip? */
wl_display_sync(d->display);
WAYLAND_wl_display_flush(d->display);
}
int
Wayland_WaitEventTimeout(_THIS, int timeout)
{
SDL_VideoData *d = _this->driverdata;
struct SDL_WaylandInput *input = d->input;
SDL_bool key_repeat_active = SDL_FALSE;
WAYLAND_wl_display_flush(d->display);
#ifdef SDL_USE_IME
if (d->text_input_manager == NULL && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_IME_PumpEvents();
}
#endif
/* If key repeat is active, we'll need to cap our maximum wait time to handle repeats */
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
/* A repeat key event was already due */
return 1;
} else {
uint32_t next_repeat_wait_time = (input->keyboard_repeat.next_repeat_ms - elapsed) + 1;
if (timeout >= 0) {
timeout = SDL_min(timeout, next_repeat_wait_time);
} else {
timeout = next_repeat_wait_time;
}
key_repeat_active = SDL_TRUE;
}
}
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
/* Use SDL_IOR_NO_RETRY to ensure SIGINT will break us out of our wait */
int err = SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ | SDL_IOR_NO_RETRY, timeout);
if (err > 0) {
/* There are new events available to read */
WAYLAND_wl_display_read_events(d->display);
WAYLAND_wl_display_dispatch_pending(d->display);
return 1;
} else if (err == 0) {
/* No events available within the timeout */
WAYLAND_wl_display_cancel_read(d->display);
/* If key repeat is active, we might have woken up to generate a key event */
if (key_repeat_active) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
return 1;
}
}
return 0;
} else {
/* Error returned from poll()/select() */
WAYLAND_wl_display_cancel_read(d->display);
if (errno == EINTR) {
/* If the wait was interrupted by a signal, we may have generated a
* SDL_QUIT event. Let the caller know to call SDL_PumpEvents(). */
return 1;
} else {
return err;
}
}
} else {
/* We already had pending events */
WAYLAND_wl_display_dispatch_pending(d->display);
return 1;
}
}
void
Wayland_PumpEvents(_THIS)
{
SDL_VideoData *d = _this->driverdata;
struct SDL_WaylandInput *input = d->input;
int err;
WAYLAND_wl_display_flush(d->display);
#ifdef SDL_USE_IME
if (d->text_input_manager == NULL && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_IME_PumpEvents();
}
#endif
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ, 0) > 0) {
WAYLAND_wl_display_read_events(d->display);
} else {
WAYLAND_wl_display_cancel_read(d->display);
}
}
/* Dispatch any pre-existing pending events or new events we may have read */
err = WAYLAND_wl_display_dispatch_pending(d->display);
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
keyboard_repeat_handle(&input->keyboard_repeat, elapsed);
}
if (err == -1 && !d->display_disconnected) {
/* Something has failed with the Wayland connection -- for example,
* the compositor may have shut down and closed its end of the socket,
* or there is a library-specific error. No recovery is possible. */
d->display_disconnected = 1;
/* Only send a single quit message, as application shutdown might call
* SDL_PumpEvents */
SDL_SendQuit();
}
}
static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->pointer_focus;
input->sx_w = sx_w;
input->sy_w = sy_w;
if (input->pointer_focus) {
const int sx = wl_fixed_to_int(sx_w);
const int sy = wl_fixed_to_int(sy_w);
SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
}
}
static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t sx_w, wl_fixed_t sy_w)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
/* check that this surface belongs to one of the SDL windows */
if (!SDL_WAYLAND_own_surface(surface)) {
return;
}
/* This handler will be called twice in Wayland 1.4
* Once for the window surface which has valid user data
* and again for the mouse cursor surface which does not have valid user data
* We ignore the later
*/
window = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window) {
input->pointer_focus = window;
input->pointer_enter_serial = serial;
SDL_SetMouseFocus(window->sdlwindow);
/* In the case of e.g. a pointer confine warp, we may receive an enter
* event with no following motion event, but with the new coordinates
* as part of the enter event. */
pointer_handle_motion(data, pointer, serial, sx_w, sy_w);
/* If the cursor was changed while our window didn't have pointer
* focus, we might need to trigger another call to
* wl_pointer_set_cursor() for the new cursor to be displayed. */
SDL_SetCursor(NULL);
}
}
static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
if (!surface || !SDL_WAYLAND_own_surface(surface)) {
return;
}
if (input->pointer_focus) {
SDL_SetMouseFocus(NULL);
input->pointer_focus = NULL;
}
}
static SDL_bool
ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
{
SDL_WindowData *window_data = input->pointer_focus;
SDL_Window *window = window_data->sdlwindow;
if (window->hit_test) {
const SDL_Point point = { wl_fixed_to_int(input->sx_w), wl_fixed_to_int(input->sy_w) };
const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
static const uint32_t directions[] = {
XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_TOP,
XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_RIGHT,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_LEFT
};
#ifdef HAVE_LIBDECOR_H
/* ditto for libdecor. */
const uint32_t *directions_libdecor = directions;
#endif
switch (rc) {
case SDL_HITTEST_DRAGGABLE:
#ifdef HAVE_LIBDECOR_H
if (input->display->shell.libdecor) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_move(window_data->shell_surface.libdecor.frame, input->seat, serial);
}
} else
#endif
if (input->display->shell.xdg) {
if (window_data->shell_surface.xdg.roleobj.toplevel) {
xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel,
input->seat,
serial);
}
}
return SDL_TRUE;
case SDL_HITTEST_RESIZE_TOPLEFT:
case SDL_HITTEST_RESIZE_TOP:
case SDL_HITTEST_RESIZE_TOPRIGHT:
case SDL_HITTEST_RESIZE_RIGHT:
case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
case SDL_HITTEST_RESIZE_BOTTOM:
case SDL_HITTEST_RESIZE_BOTTOMLEFT:
case SDL_HITTEST_RESIZE_LEFT:
#ifdef HAVE_LIBDECOR_H
if (input->display->shell.libdecor) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_resize(window_data->shell_surface.libdecor.frame, input->seat, serial, directions_libdecor[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
}
} else
#endif
if (input->display->shell.xdg) {
if (window_data->shell_surface.xdg.roleobj.toplevel) {
xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel,
input->seat,
serial,
directions[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
}
}
return SDL_TRUE;
default: return SDL_FALSE;
}
}
return SDL_FALSE;
}
static void
pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
uint32_t sdl_button;
if (input->pointer_focus) {
switch (button) {
case BTN_LEFT:
sdl_button = SDL_BUTTON_LEFT;
if (ProcessHitTest(input, serial)) {
return; /* don't pass this event on to app. */
}
break;
case BTN_MIDDLE:
sdl_button = SDL_BUTTON_MIDDLE;
break;
case BTN_RIGHT:
sdl_button = SDL_BUTTON_RIGHT;
break;
case BTN_SIDE:
sdl_button = SDL_BUTTON_X1;
break;
case BTN_EXTRA:
sdl_button = SDL_BUTTON_X2;
break;
default:
return;
}
Wayland_data_device_set_serial(input->data_device, serial);
SDL_SendMouseButton(window->sdlwindow, 0,
state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
}
}
static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
pointer_handle_button_common(input, serial, time, button, state_w);
}
static void
pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_axis a = axis;
float x, y;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
y = 0 - (float)wl_fixed_to_double(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
x = 0 - (float)wl_fixed_to_double(value);
y = 0;
break;
default:
return;
}
x /= WAYLAND_WHEEL_AXIS_UNIT;
y /= WAYLAND_WHEEL_AXIS_UNIT;
SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
}
}
static void
pointer_handle_axis_common(struct SDL_WaylandInput *input, SDL_bool discrete,
uint32_t axis, wl_fixed_t value)
{
enum wl_pointer_axis a = axis;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
if (discrete) {
/* this is a discrete axis event so we process it and flag
* to ignore future continuous axis events in this frame */
input->pointer_curr_axis_info.is_y_discrete = SDL_TRUE;
} else if(input->pointer_curr_axis_info.is_y_discrete) {
/* this is a continuous axis event and we have already
* processed a discrete axis event before so we ignore it */
break;
}
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
if (discrete) {
/* this is a discrete axis event so we process it and flag
* to ignore future continuous axis events in this frame */
input->pointer_curr_axis_info.is_x_discrete = SDL_TRUE;
} else if(input->pointer_curr_axis_info.is_x_discrete) {
/* this is a continuous axis event and we have already
* processed a discrete axis event before so we ignore it */
break;
}
input->pointer_curr_axis_info.x = 0 - (float)wl_fixed_to_double(value);
break;
}
}
}
static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
struct SDL_WaylandInput *input = data;
if(wl_seat_get_version(input->seat) >= 5)
pointer_handle_axis_common(input, SDL_FALSE, axis, value);
else
pointer_handle_axis_common_v1(input, time, axis, value);
}
static void
pointer_handle_frame(void *data, struct wl_pointer *pointer)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->pointer_focus;
float x, y;
if (input->pointer_curr_axis_info.is_x_discrete)
x = input->pointer_curr_axis_info.x;
else
x = input->pointer_curr_axis_info.x / WAYLAND_WHEEL_AXIS_UNIT;
if (input->pointer_curr_axis_info.is_y_discrete)
y = input->pointer_curr_axis_info.y;
else
y = input->pointer_curr_axis_info.y / WAYLAND_WHEEL_AXIS_UNIT;
/* clear pointer_curr_axis_info for next frame */
SDL_memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
if(x == 0.0f && y == 0.0f)
return;
else
SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
}
static void
pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
uint32_t axis_source)
{
/* unimplemented */
}
static void
pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis)
{
/* unimplemented */
}
static void
pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
struct SDL_WaylandInput *input = data;
pointer_handle_axis_common(input, SDL_TRUE, axis, wl_fixed_from_int(discrete));
}
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
pointer_handle_frame, // Version 5
pointer_handle_axis_source, // Version 5
pointer_handle_axis_stop, // Version 5
pointer_handle_axis_discrete, // Version 5
};
static void
touch_handler_down(void *data, struct wl_touch *touch, unsigned int serial,
unsigned int timestamp, struct wl_surface *surface,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
const double dblx = wl_fixed_to_double(fx);
const double dbly = wl_fixed_to_double(fy);
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
touch_add(id, x, y, surface);
SDL_SendTouch((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id, window_data->sdlwindow, SDL_TRUE, x, y, 1.0f);
}
static void
touch_handler_up(void *data, struct wl_touch *touch, unsigned int serial,
unsigned int timestamp, int id)
{
float x = 0, y = 0;
struct wl_surface *surface = NULL;
SDL_Window *window = NULL;
touch_del(id, &x, &y, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
window = window_data->sdlwindow;
}
SDL_SendTouch((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id, window, SDL_FALSE, x, y, 0.0f);
}
static void
touch_handler_motion(void *data, struct wl_touch *touch, unsigned int timestamp,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(touch_surface(id));
const double dblx = wl_fixed_to_double(fx);
const double dbly = wl_fixed_to_double(fy);
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
touch_update(id, x, y);
SDL_SendTouchMotion((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id, window_data->sdlwindow, x, y, 1.0f);
}
static void
touch_handler_frame(void *data, struct wl_touch *touch)
{
}
static void
touch_handler_cancel(void *data, struct wl_touch *touch)
{
}
static const struct wl_touch_listener touch_listener = {
touch_handler_down,
touch_handler_up,
touch_handler_motion,
touch_handler_frame,
touch_handler_cancel,
NULL, /* shape */
NULL, /* orientation */
};
static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
{
struct SDL_WaylandInput *input = data;
char *map_str;
const char *locale;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map_str == MAP_FAILED) {
close(fd);
return;
}
input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
map_str,
XKB_KEYMAP_FORMAT_TEXT_V1,
0);
munmap(map_str, size);
close(fd);
if (!input->xkb.keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
if (!input->xkb.state) {
fprintf(stderr, "failed to create XKB state\n");
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
input->xkb.keymap = NULL;
return;
}
/*
* See https://blogs.s-osg.org/compose-key-support-weston/
* for further explanation on dead keys in Wayland.
*/
/* Look up the preferred locale, falling back to "C" as default */
if (!(locale = SDL_getenv("LC_ALL")))
if (!(locale = SDL_getenv("LC_CTYPE")))
if (!(locale = SDL_getenv("LANG")))
locale = "C";
/* Set up XKB compose table */
input->xkb.compose_table = WAYLAND_xkb_compose_table_new_from_locale(input->display->xkb_context,
locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
if (input->xkb.compose_table) {
/* Set up XKB compose state */
input->xkb.compose_state = WAYLAND_xkb_compose_state_new(input->xkb.compose_table,
XKB_COMPOSE_STATE_NO_FLAGS);
if (!input->xkb.compose_state) {
fprintf(stderr, "could not create XKB compose state\n");
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
input->xkb.compose_table = NULL;
}
}
}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
if (!SDL_WAYLAND_own_surface(surface)) {
return;
}
window = wl_surface_get_user_data(surface);
if (window) {
input->keyboard_focus = window;
window->keyboard_device = input;
SDL_SetKeyboardFocus(window->sdlwindow);
}
#ifdef SDL_USE_IME
if (!input->text_input) {
SDL_IME_SetFocus(SDL_TRUE);
}
#endif
}
static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
if (!surface || !SDL_WAYLAND_own_surface(surface)) {
return;
}
/* Stop key repeat before clearing keyboard focus */
keyboard_repeat_clear(&input->keyboard_repeat);
/* This will release any keys still pressed */
SDL_SetKeyboardFocus(NULL);
#ifdef SDL_USE_IME
if (!input->text_input) {
SDL_IME_SetFocus(SDL_FALSE);
}
#endif
}
static SDL_bool
keyboard_input_get_text(char text[8], const struct SDL_WaylandInput *input, uint32_t key, SDL_bool *handled_by_ime)
{
SDL_WindowData *window = input->keyboard_focus;
const xkb_keysym_t *syms;
xkb_keysym_t sym;
if (!window || window->keyboard_device != input || !input->xkb.state) {
return SDL_FALSE;
}
// TODO can this happen?
if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1) {
return SDL_FALSE;
}
sym = syms[0];
#ifdef SDL_USE_IME
if (SDL_IME_ProcessKeyEvent(sym, key + 8)) {
*handled_by_ime = SDL_TRUE;
return SDL_TRUE;
}
#endif
if (input->xkb.compose_state && WAYLAND_xkb_compose_state_feed(input->xkb.compose_state, sym) == XKB_COMPOSE_FEED_ACCEPTED) {
switch(WAYLAND_xkb_compose_state_get_status(input->xkb.compose_state)) {
case XKB_COMPOSE_COMPOSING:
*handled_by_ime = SDL_TRUE;
return SDL_TRUE;
case XKB_COMPOSE_CANCELLED:
default:
sym = XKB_KEY_NoSymbol;
break;
case XKB_COMPOSE_NOTHING:
break;
case XKB_COMPOSE_COMPOSED:
sym = WAYLAND_xkb_compose_state_get_one_sym(input->xkb.compose_state);
break;
}
}
return WAYLAND_xkb_keysym_to_utf8(sym, text, 8) > 0;
}
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
enum wl_keyboard_key_state state = state_w;
uint32_t scancode = SDL_SCANCODE_UNKNOWN;
char text[8];
SDL_bool has_text = SDL_FALSE;
SDL_bool handled_by_ime = SDL_FALSE;
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
has_text = keyboard_input_get_text(text, input, key, &handled_by_ime);
} else {
if (keyboard_repeat_is_set(&input->keyboard_repeat)) {
// Send any due key repeat events before stopping the repeat and generating the key up event
// Compute time based on the Wayland time, as it reports when the release event happened
// Using SDL_GetTicks would be wrong, as it would report when the release event is processed,
// which may be off if the application hasn't pumped events for a while
keyboard_repeat_handle(&input->keyboard_repeat, time - input->keyboard_repeat.wl_press_time);
keyboard_repeat_clear(&input->keyboard_repeat);
}
}
if (!handled_by_ime && key < SDL_arraysize(xfree86_scancode_table2)) {
scancode = xfree86_scancode_table2[key];
if (scancode != SDL_SCANCODE_UNKNOWN) {
SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
SDL_PRESSED : SDL_RELEASED, scancode);
}
}
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
if (has_text && !(SDL_GetModState() & KMOD_CTRL)) {
Wayland_data_device_set_serial(input->data_device, serial);
if (!handled_by_ime) {
SDL_SendKeyboardText(text);
}
}
keyboard_repeat_set(&input->keyboard_repeat, time, scancode, has_text, text);
}
}
typedef struct Wayland_Keymap
{
xkb_layout_index_t layout;
SDL_Keycode keymap[SDL_NUM_SCANCODES];
} Wayland_Keymap;
static void
Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
{
const xkb_keysym_t *syms;
Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
if ((key - 8) < SDL_arraysize(xfree86_scancode_table2)) {
SDL_Scancode scancode = xfree86_scancode_table2[key - 8];
if (scancode == SDL_SCANCODE_UNKNOWN) {
return;
}
if (WAYLAND_xkb_keymap_key_get_syms_by_level(keymap, key, sdlKeymap->layout, 0, &syms) > 0) {
uint32_t keycode = SDL_KeySymToUcs4(syms[0]);
if (!keycode) {
keycode = Wayland_KeySymToSDLKeyCode(syms[0]);
}
if (keycode) {
sdlKeymap->keymap[scancode] = keycode;
} else {
switch (scancode) {
case SDL_SCANCODE_RETURN:
sdlKeymap->keymap[scancode] = SDLK_RETURN;
break;
case SDL_SCANCODE_ESCAPE:
sdlKeymap->keymap[scancode] = SDLK_ESCAPE;
break;
case SDL_SCANCODE_BACKSPACE:
sdlKeymap->keymap[scancode] = SDLK_BACKSPACE;
break;
case SDL_SCANCODE_TAB:
sdlKeymap->keymap[scancode] = SDLK_TAB;
break;
case SDL_SCANCODE_DELETE:
sdlKeymap->keymap[scancode] = SDLK_DELETE;
break;
default:
sdlKeymap->keymap[scancode] = SDL_SCANCODE_TO_KEYCODE(scancode);
break;
}
}
}
}
}
static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed,
uint32_t mods_latched, uint32_t mods_locked,
uint32_t group)
{
struct SDL_WaylandInput *input = data;
Wayland_Keymap keymap;
WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
keymap.layout = group;
SDL_GetDefaultKeymap(keymap.keymap);
WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
Wayland_keymap_iter,
&keymap);
SDL_SetKeymap(0, keymap.keymap, SDL_NUM_SCANCODES);
SDL_SendKeymapChangedEvent();
}
static void
keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
int32_t rate, int32_t delay)
{
struct SDL_WaylandInput *input = data;
input->keyboard_repeat.repeat_rate = SDL_clamp(rate, 0, 1000);
input->keyboard_repeat.repeat_delay = delay;
input->keyboard_repeat.is_initialized = SDL_TRUE;
}
static const struct wl_keyboard_listener keyboard_listener = {
keyboard_handle_keymap,
keyboard_handle_enter,
keyboard_handle_leave,
keyboard_handle_key,
keyboard_handle_modifiers,
keyboard_handle_repeat_info, // Version 4
};
static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
enum wl_seat_capability caps)
{
struct SDL_WaylandInput *input = data;
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
input->pointer = wl_seat_get_pointer(seat);
SDL_memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
input->display->pointer = input->pointer;
wl_pointer_set_user_data(input->pointer, input);
wl_pointer_add_listener(input->pointer, &pointer_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
wl_pointer_destroy(input->pointer);
input->pointer = NULL;
input->display->pointer = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
input->touch = wl_seat_get_touch(seat);
SDL_AddTouch((SDL_TouchID)(intptr_t)input->touch, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
wl_touch_set_user_data(input->touch, input);
wl_touch_add_listener(input->touch, &touch_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
SDL_DelTouch((SDL_TouchID)(intptr_t)input->touch);
wl_touch_destroy(input->touch);
input->touch = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
input->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_set_user_data(input->keyboard, input);
wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
wl_keyboard_destroy(input->keyboard);
input->keyboard = NULL;
}
}
static void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
/* unimplemented */
}
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
seat_handle_name, // Version 2
};
static void
data_source_handle_target(void *data, struct wl_data_source *wl_data_source,
const char *mime_type)
{
}
static void
data_source_handle_send(void *data, struct wl_data_source *wl_data_source,
const char *mime_type, int32_t fd)
{
Wayland_data_source_send((SDL_WaylandDataSource *)data, mime_type, fd);
}
static void
data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
{
Wayland_data_source_destroy(data);
}
static void
data_source_handle_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
{
}
static void
data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
{
}
static void
data_source_handle_action(void *data, struct wl_data_source *wl_data_source,
uint32_t dnd_action)
{
}
static const struct wl_data_source_listener data_source_listener = {
data_source_handle_target,
data_source_handle_send,
data_source_handle_cancelled,
data_source_handle_dnd_drop_performed, // Version 3
data_source_handle_dnd_finished, // Version 3
data_source_handle_action, // Version 3
};
SDL_WaylandDataSource*
Wayland_data_source_create(_THIS)
{
SDL_WaylandDataSource *data_source = NULL;
SDL_VideoData *driver_data = NULL;
struct wl_data_source *id = NULL;
if (_this == NULL || _this->driverdata == NULL) {
SDL_SetError("Video driver uninitialized");
} else {
driver_data = _this->driverdata;
if (driver_data->data_device_manager != NULL) {
id = wl_data_device_manager_create_data_source(
driver_data->data_device_manager);
}
if (id == NULL) {
SDL_SetError("Wayland unable to create data source");
} else {
data_source = SDL_calloc(1, sizeof *data_source);
if (data_source == NULL) {
SDL_OutOfMemory();
wl_data_source_destroy(id);
} else {
WAYLAND_wl_list_init(&(data_source->mimes));
data_source->source = id;
wl_data_source_set_user_data(id, data_source);
wl_data_source_add_listener(id, &data_source_listener,
data_source);
}
}
}
return data_source;
}
static void
data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
const char *mime_type)
{
SDL_WaylandDataOffer *offer = data;
Wayland_data_offer_add_mime(offer, mime_type);
}
static void
data_offer_handle_source_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t source_actions)
{
}
static void
data_offer_handle_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t dnd_action)
{
}
static const struct wl_data_offer_listener data_offer_listener = {
data_offer_handle_offer,
data_offer_handle_source_actions, // Version 3
data_offer_handle_actions, // Version 3
};
static void
data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
SDL_WaylandDataOffer *data_offer = NULL;
data_offer = SDL_calloc(1, sizeof *data_offer);
if (data_offer == NULL) {
SDL_OutOfMemory();
} else {
data_offer->offer = id;
data_offer->data_device = data;
WAYLAND_wl_list_init(&(data_offer->mimes));
wl_data_offer_set_user_data(id, data_offer);
wl_data_offer_add_listener(id, &data_offer_listener, data_offer);
}
}
static void
data_device_handle_enter(void *data, struct wl_data_device *wl_data_device,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id)
{
SDL_WaylandDataDevice *data_device = data;
SDL_bool has_mime = SDL_FALSE;
uint32_t dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
data_device->drag_serial = serial;
if (id != NULL) {
data_device->drag_offer = wl_data_offer_get_user_data(id);
/* TODO: SDL Support more mime types */
has_mime = Wayland_data_offer_has_mime(
data_device->drag_offer, FILE_MIME);
/* If drag_mime is NULL this will decline the offer */
wl_data_offer_accept(id, serial,
(has_mime == SDL_TRUE) ? FILE_MIME : NULL);
/* SDL only supports "copy" style drag and drop */
if (has_mime == SDL_TRUE) {
dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
}
if (wl_data_offer_get_version(data_device->drag_offer->offer) >= 3) {
wl_data_offer_set_actions(data_device->drag_offer->offer,
dnd_action, dnd_action);
}
}
}
static void
data_device_handle_leave(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
SDL_WaylandDataOffer *offer = NULL;
if (data_device->selection_offer != NULL) {
data_device->selection_offer = NULL;
Wayland_data_offer_destroy(offer);
}
}
static void
data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
}
static void
data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
void *buffer = NULL;
size_t length = 0;
const char *current_uri = NULL;
const char *last_char = NULL;
char *current_char = NULL;
if (data_device->drag_offer != NULL) {
/* TODO: SDL Support more mime types */
buffer = Wayland_data_offer_receive(data_device->drag_offer,
&length, FILE_MIME, SDL_FALSE);
/* uri-list */
current_uri = (const char *)buffer;
last_char = (const char *)buffer + length;
for (current_char = buffer; current_char < last_char; ++current_char) {
if (*current_char == '\n' || *current_char == 0) {
if (*current_uri != 0 && *current_uri != '#') {
*current_char = 0;
SDL_SendDropFile(NULL, current_uri);
}
current_uri = (const char *)current_char + 1;
}
}
SDL_free(buffer);
}
}
static void
data_device_handle_selection(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
SDL_WaylandDataDevice *data_device = data;
SDL_WaylandDataOffer *offer = NULL;
if (id != NULL) {
offer = wl_data_offer_get_user_data(id);
}
if (data_device->selection_offer != offer) {
Wayland_data_offer_destroy(data_device->selection_offer);
data_device->selection_offer = offer;
}
SDL_SendClipboardUpdate();
}
static const struct wl_data_device_listener data_device_listener = {
data_device_handle_data_offer,
data_device_handle_enter,
data_device_handle_leave,
data_device_handle_motion,
data_device_handle_drop,
data_device_handle_selection
};
static void
text_input_enter(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
/* No-op */
}
static void
text_input_leave(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
/* No-op */
}
static void
text_input_preedit_string(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text,
int32_t cursor_begin,
int32_t cursor_end)
{
char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
if (text) {
size_t text_bytes = SDL_strlen(text), i = 0;
size_t cursor = 0;
do {
const size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
const size_t chars = SDL_utf8strlen(buf);
SDL_SendEditingText(buf, cursor, chars);
i += sz;
cursor += chars;
} while (i < text_bytes);
} else {
buf[0] = '\0';
SDL_SendEditingText(buf, 0, 0);
}
}
static void
text_input_commit_string(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text)
{
if (text && *text) {
char buf[SDL_TEXTINPUTEVENT_TEXT_SIZE];
size_t text_bytes = SDL_strlen(text), i = 0;
while (i < text_bytes) {
size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
SDL_SendKeyboardText(buf);
i += sz;
}
}
}
static void
text_input_delete_surrounding_text(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t before_length,
uint32_t after_length)
{
/* FIXME: Do we care about this event? */
}
static void
text_input_done(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t serial)
{
/* No-op */
}
static const struct zwp_text_input_v3_listener text_input_listener = {
text_input_enter,
text_input_leave,
text_input_preedit_string,
text_input_commit_string,
text_input_delete_surrounding_text,
text_input_done
};
static void
Wayland_create_data_device(SDL_VideoData *d)
{
SDL_WaylandDataDevice *data_device = NULL;
data_device = SDL_calloc(1, sizeof *data_device);
if (data_device == NULL) {
return;
}
data_device->data_device = wl_data_device_manager_get_data_device(
d->data_device_manager, d->input->seat
);
data_device->video_data = d;
if (data_device->data_device == NULL) {
SDL_free(data_device);
} else {
wl_data_device_set_user_data(data_device->data_device, data_device);
wl_data_device_add_listener(data_device->data_device,
&data_device_listener, data_device);
d->input->data_device = data_device;
}
}
static void
Wayland_create_text_input(SDL_VideoData *d)
{
SDL_WaylandTextInput *text_input = NULL;
text_input = SDL_calloc(1, sizeof *text_input);
if (text_input == NULL) {
return;
}
text_input->text_input = zwp_text_input_manager_v3_get_text_input(
d->text_input_manager, d->input->seat
);
if (text_input->text_input == NULL) {
SDL_free(text_input);
} else {
zwp_text_input_v3_set_user_data(text_input->text_input, text_input);
zwp_text_input_v3_add_listener(text_input->text_input,
&text_input_listener, text_input);
d->input->text_input = text_input;
}
}
void
Wayland_add_data_device_manager(SDL_VideoData *d, uint32_t id, uint32_t version)
{
d->data_device_manager = wl_registry_bind(d->registry, id, &wl_data_device_manager_interface, SDL_min(3, version));
if (d->input != NULL) {
Wayland_create_data_device(d);
}
}
void
Wayland_add_text_input_manager(SDL_VideoData *d, uint32_t id, uint32_t version)
{
d->text_input_manager = wl_registry_bind(d->registry, id, &zwp_text_input_manager_v3_interface, 1);
if (d->input != NULL) {
Wayland_create_text_input(d);
}
}
void
Wayland_display_add_input(SDL_VideoData *d, uint32_t id, uint32_t version)
{
struct SDL_WaylandInput *input;
input = SDL_calloc(1, sizeof *input);
if (input == NULL)
return;
input->display = d;
input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, SDL_min(5, version));
input->sx_w = wl_fixed_from_int(0);
input->sy_w = wl_fixed_from_int(0);
d->input = input;
if (d->data_device_manager != NULL) {
Wayland_create_data_device(d);
}
if (d->text_input_manager != NULL) {
Wayland_create_text_input(d);
}
wl_seat_add_listener(input->seat, &seat_listener, input);
wl_seat_set_user_data(input->seat, input);
WAYLAND_wl_display_flush(d->display);
}
void Wayland_display_destroy_input(SDL_VideoData *d)
{
struct SDL_WaylandInput *input = d->input;
if (!input)
return;
if (input->data_device != NULL) {
Wayland_data_device_clear_selection(input->data_device);
if (input->data_device->selection_offer != NULL) {
Wayland_data_offer_destroy(input->data_device->selection_offer);
}
if (input->data_device->drag_offer != NULL) {
Wayland_data_offer_destroy(input->data_device->drag_offer);
}
if (input->data_device->data_device != NULL) {
wl_data_device_release(input->data_device->data_device);
}
SDL_free(input->data_device);
}
if (input->text_input != NULL) {
zwp_text_input_v3_destroy(input->text_input->text_input);
SDL_free(input->text_input);
}
if (input->keyboard)
wl_keyboard_destroy(input->keyboard);
if (input->pointer)
wl_pointer_destroy(input->pointer);
if (input->touch) {
SDL_DelTouch(1);
wl_touch_destroy(input->touch);
}
if (input->seat)
wl_seat_destroy(input->seat);
if (input->xkb.compose_state)
WAYLAND_xkb_compose_state_unref(input->xkb.compose_state);
if (input->xkb.compose_table)
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
if (input->xkb.state)
WAYLAND_xkb_state_unref(input->xkb.state);
if (input->xkb.keymap)
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
SDL_free(input);
d->input = NULL;
}
/* !!! FIXME: just merge these into display_handle_global(). */
void Wayland_display_add_relative_pointer_manager(SDL_VideoData *d, uint32_t id)
{
d->relative_pointer_manager =
wl_registry_bind(d->registry, id,
&zwp_relative_pointer_manager_v1_interface, 1);
}
void Wayland_display_destroy_relative_pointer_manager(SDL_VideoData *d)
{
if (d->relative_pointer_manager)
zwp_relative_pointer_manager_v1_destroy(d->relative_pointer_manager);
}
void Wayland_display_add_pointer_constraints(SDL_VideoData *d, uint32_t id)
{
d->pointer_constraints =
wl_registry_bind(d->registry, id,
&zwp_pointer_constraints_v1_interface, 1);
}
void Wayland_display_destroy_pointer_constraints(SDL_VideoData *d)
{
if (d->pointer_constraints)
zwp_pointer_constraints_v1_destroy(d->pointer_constraints);
}
static void
relative_pointer_handle_relative_motion(void *data,
struct zwp_relative_pointer_v1 *pointer,
uint32_t time_hi,
uint32_t time_lo,
wl_fixed_t dx_w,
wl_fixed_t dy_w,
wl_fixed_t dx_unaccel_w,
wl_fixed_t dy_unaccel_w)
{
struct SDL_WaylandInput *input = data;
SDL_VideoData *d = input->display;
SDL_WindowData *window = input->pointer_focus;
double dx_unaccel;
double dy_unaccel;
double dx;
double dy;
dx_unaccel = wl_fixed_to_double(dx_unaccel_w);
dy_unaccel = wl_fixed_to_double(dy_unaccel_w);
/* Add left over fraction from last event. */
dx_unaccel += input->dx_frac;
dy_unaccel += input->dy_frac;
input->dx_frac = modf(dx_unaccel, &dx);
input->dy_frac = modf(dy_unaccel, &dy);
if (input->pointer_focus && d->relative_mouse_mode) {
SDL_SendMouseMotion(window->sdlwindow, 0, 1, (int)dx, (int)dy);
}
}
static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
relative_pointer_handle_relative_motion,
};
static void
locked_pointer_locked(void *data,
struct zwp_locked_pointer_v1 *locked_pointer)
{
}
static void
locked_pointer_unlocked(void *data,
struct zwp_locked_pointer_v1 *locked_pointer)
{
}
static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
locked_pointer_locked,
locked_pointer_unlocked,
};
static void
lock_pointer_to_window(SDL_Window *window,
struct SDL_WaylandInput *input)
{
SDL_WindowData *w = window->driverdata;
SDL_VideoData *d = input->display;
struct zwp_locked_pointer_v1 *locked_pointer;
if (w->locked_pointer)
return;
locked_pointer =
zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints,
w->surface,
input->pointer,
NULL,
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
zwp_locked_pointer_v1_add_listener(locked_pointer,
&locked_pointer_listener,
window);
w->locked_pointer = locked_pointer;
}
static void pointer_confine_destroy(SDL_Window *window)
{
SDL_WindowData *w = window->driverdata;
if (w->confined_pointer) {
zwp_confined_pointer_v1_destroy(w->confined_pointer);
w->confined_pointer = NULL;
}
}
int Wayland_input_lock_pointer(struct SDL_WaylandInput *input)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = input->display;
SDL_Window *window;
struct zwp_relative_pointer_v1 *relative_pointer;
if (!d->relative_pointer_manager)
return -1;
if (!d->pointer_constraints)
return -1;
if (!input->pointer)
return -1;
/* If we have a pointer confine active, we must destroy it here because
* creating a locked pointer otherwise would be a protocol error. */
for (window = vd->windows; window; window = window->next)
pointer_confine_destroy(window);
if (!input->relative_pointer) {
relative_pointer =
zwp_relative_pointer_manager_v1_get_relative_pointer(
d->relative_pointer_manager,
input->pointer);
zwp_relative_pointer_v1_add_listener(relative_pointer,
&relative_pointer_listener,
input);
input->relative_pointer = relative_pointer;
}
for (window = vd->windows; window; window = window->next)
lock_pointer_to_window(window, input);
d->relative_mouse_mode = 1;
return 0;
}
int Wayland_input_unlock_pointer(struct SDL_WaylandInput *input)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = input->display;
SDL_Window *window;
SDL_WindowData *w;
for (window = vd->windows; window; window = window->next) {
w = window->driverdata;
if (w->locked_pointer)
zwp_locked_pointer_v1_destroy(w->locked_pointer);
w->locked_pointer = NULL;
}
zwp_relative_pointer_v1_destroy(input->relative_pointer);
input->relative_pointer = NULL;
d->relative_mouse_mode = 0;
for (window = vd->windows; window; window = window->next)
Wayland_input_confine_pointer(input, window);
return 0;
}
static void
confined_pointer_confined(void *data,
struct zwp_confined_pointer_v1 *confined_pointer)
{
}
static void
confined_pointer_unconfined(void *data,
struct zwp_confined_pointer_v1 *confined_pointer)
{
}
static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
confined_pointer_confined,
confined_pointer_unconfined,
};
int Wayland_input_confine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
SDL_WindowData *w = window->driverdata;
SDL_VideoData *d = input->display;
struct zwp_confined_pointer_v1 *confined_pointer;
struct wl_region *confine_rect;
if (!d->pointer_constraints)
return -1;
if (!input->pointer)
return -1;
/* A confine may already be active, in which case we should destroy it and
* create a new one. */
pointer_confine_destroy(window);
/* We cannot create a confine if the pointer is already locked. Defer until
* the pointer is unlocked. */
if (d->relative_mouse_mode)
return 0;
if (SDL_RectEmpty(&window->mouse_rect)) {
confine_rect = NULL;
} else {
confine_rect = wl_compositor_create_region(d->compositor);
wl_region_add(confine_rect,
window->mouse_rect.x,
window->mouse_rect.y,
window->mouse_rect.w,
window->mouse_rect.h);
}
confined_pointer =
zwp_pointer_constraints_v1_confine_pointer(d->pointer_constraints,
w->surface,
input->pointer,
confine_rect,
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
zwp_confined_pointer_v1_add_listener(confined_pointer,
&confined_pointer_listener,
window);
if (confine_rect != NULL) {
wl_region_destroy(confine_rect);
}
w->confined_pointer = confined_pointer;
return 0;
}
int Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
pointer_confine_destroy(window);
return 0;
}
int Wayland_input_grab_keyboard(SDL_Window *window, struct SDL_WaylandInput *input)
{
SDL_WindowData *w = window->driverdata;
SDL_VideoData *d = input->display;
if (!d->key_inhibitor_manager)
return -1;
if (w->key_inhibitor)
return 0;
w->key_inhibitor =
zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(d->key_inhibitor_manager,
w->surface,
input->seat);
return 0;
}
int Wayland_input_ungrab_keyboard(SDL_Window *window)
{
SDL_WindowData *w = window->driverdata;
if (w->key_inhibitor) {
zwp_keyboard_shortcuts_inhibitor_v1_destroy(w->key_inhibitor);
w->key_inhibitor = NULL;
}
return 0;
}
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
/* vi: set ts=4 sw=4 expandtab: */