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
|
#include "config.h"
#include "broadway-server.h"
#include "broadway-output.h"
#include <glib.h>
#include <glib/gprintf.h>
#include "gdktypes.h"
#include "gdkinternals.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#elif defined (G_OS_WIN32)
#include <io.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#ifdef G_OS_UNIX
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#ifdef HAVE_GIO_UNIX
#include <gio/gunixsocketaddress.h>
#endif
#ifdef G_OS_WIN32
#include <windows.h>
#include <string.h>
#endif
#if !GLIB_CHECK_VERSION (2, 67, 3)
# define g_memdup2(mem,size) g_memdup((mem), (size))
#endif
typedef struct {
int id;
guint32 tag;
} BroadwayOutstandingRoundtrip;
typedef struct BroadwayInput BroadwayInput;
typedef struct BroadwaySurface BroadwaySurface;
struct _BroadwayServer {
GObject parent_instance;
char *address;
int port;
char *ssl_cert;
char *ssl_key;
GSocketService *service;
BroadwayOutput *output;
guint32 id_counter;
guint32 saved_serial;
guint64 last_seen_time;
BroadwayInput *input;
GList *input_messages;
guint process_input_idle;
GHashTable *surface_id_hash;
GList *surfaces;
BroadwaySurface *root;
gint32 focused_surface_id; /* -1 => none */
int show_keyboard;
guint32 next_texture_id;
GHashTable *textures;
guint32 screen_scale;
gint32 mouse_in_surface_id;
int last_x, last_y; /* in root coords */
guint32 last_state;
gint32 real_mouse_in_surface_id; /* Not affected by grabs */
/* Explicit pointer grabs: */
gint32 pointer_grab_surface_id; /* -1 => none */
gint32 pointer_grab_client_id; /* -1 => none */
guint32 pointer_grab_time;
gboolean pointer_grab_owner_events;
/* Future data, from the currently queued events */
int future_root_x;
int future_root_y;
guint32 future_state;
int future_mouse_in_surface;
GList *outstanding_roundtrips;
};
struct _BroadwayServerClass
{
GObjectClass parent_class;
};
typedef struct HttpRequest {
BroadwayServer *server;
GSocketConnection *socket_connection;
GIOStream *connection;
GDataInputStream *data;
GString *request;
} HttpRequest;
struct BroadwayInput {
BroadwayServer *server;
BroadwayOutput *output;
GIOStream *connection;
GByteArray *buffer;
GSource *source;
gboolean seen_time;
gint64 time_base;
gboolean active;
};
struct BroadwaySurface {
guint32 owner;
gint32 id;
gint32 x;
gint32 y;
gint32 width;
gint32 height;
gboolean visible;
gint32 transient_for;
guint32 texture;
BroadwayNode *nodes;
GHashTable *node_lookup;
};
struct _BroadwayTexture {
grefcount refcount;
guint32 id;
GBytes *bytes;
};
static void broadway_server_resync_surfaces (BroadwayServer *server);
static void send_outstanding_roundtrips (BroadwayServer *server);
static void broadway_server_ref_texture (BroadwayServer *server,
guint32 id);
static GType broadway_server_get_type (void);
G_DEFINE_TYPE (BroadwayServer, broadway_server, G_TYPE_OBJECT)
static void
broadway_texture_free (BroadwayTexture *texture)
{
g_bytes_unref (texture->bytes);
g_free (texture);
}
static void
broadway_node_unref (BroadwayServer *server,
BroadwayNode *node)
{
int i;
if (g_ref_count_dec (&node->refcount))
{
for (i = 0; i < node->n_children; i++)
broadway_node_unref (server, node->children[i]);
if (node->texture_id)
broadway_server_release_texture (server, node->texture_id);
g_free (node);
}
}
static BroadwayNode *
broadway_node_ref (BroadwayNode *node)
{
g_ref_count_inc (&node->refcount);
return node;
}
gboolean
broadway_node_equal (BroadwayNode *a,
BroadwayNode *b)
{
int i;
if (a->type != b->type)
return FALSE;
if (a->n_data != b->n_data)
return FALSE;
/* Don't check data for containers, that is just n_children, which
we don't want to compare for a shallow equal */
if (a->type != BROADWAY_NODE_CONTAINER)
{
for (i = 0; i < a->n_data; i++)
if (a->data[i] != b->data[i])
return FALSE;
}
return TRUE;
}
gboolean
broadway_node_deep_equal (BroadwayNode *a,
BroadwayNode *b)
{
int i;
if (a->hash != b->hash)
return FALSE;
if (!broadway_node_equal (a,b))
return FALSE;
if (a->n_children != b->n_children)
return FALSE;
for (i = 0; i < a->n_children; i++)
if (!broadway_node_deep_equal (a->children[i], b->children[i]))
return FALSE;
return TRUE;
}
void
broadway_node_mark_deep_reused (BroadwayNode *node,
gboolean reused)
{
node->reused = reused;
for (int i = 0; i < node->n_children; i++)
broadway_node_mark_deep_reused (node->children[i], reused);
}
void
broadway_node_mark_deep_consumed (BroadwayNode *node,
gboolean consumed)
{
node->consumed = consumed;
for (int i = 0; i < node->n_children; i++)
broadway_node_mark_deep_consumed (node->children[i], consumed);
}
void
broadway_node_add_to_lookup (BroadwayNode *node,
GHashTable *node_lookup)
{
g_hash_table_insert (node_lookup, GINT_TO_POINTER(node->id), node);
for (int i = 0; i < node->n_children; i++)
broadway_node_add_to_lookup (node->children[i], node_lookup);
}
static void
broadway_server_init (BroadwayServer *server)
{
BroadwaySurface *root;
server->service = g_socket_service_new ();
server->pointer_grab_surface_id = -1;
server->saved_serial = 1;
server->last_seen_time = 1;
server->surface_id_hash = g_hash_table_new (NULL, NULL);
server->id_counter = 0;
server->textures = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
(GDestroyNotify)broadway_texture_free);
root = g_new0 (BroadwaySurface, 1);
root->id = server->id_counter++;
root->width = 1024;
root->height = 768;
root->visible = TRUE;
server->root = root;
server->screen_scale = 1;
g_hash_table_insert (server->surface_id_hash,
GINT_TO_POINTER (root->id),
root);
}
static void
broadway_server_finalize (GObject *object)
{
BroadwayServer *server = BROADWAY_SERVER (object);
g_free (server->address);
g_free (server->ssl_cert);
g_free (server->ssl_key);
g_hash_table_destroy (server->textures);
G_OBJECT_CLASS (broadway_server_parent_class)->finalize (object);
}
static void
broadway_server_class_init (BroadwayServerClass * class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = broadway_server_finalize;
}
static void
broadway_surface_free (BroadwayServer *server,
BroadwaySurface *surface)
{
if (surface->nodes)
broadway_node_unref (server, surface->nodes);
g_hash_table_unref (surface->node_lookup);
g_free (surface);
}
static BroadwaySurface *
broadway_server_lookup_surface (BroadwayServer *server,
guint32 id)
{
return g_hash_table_lookup (server->surface_id_hash,
GINT_TO_POINTER (id));
}
static void start (BroadwayInput *input);
static void
http_request_free (HttpRequest *request)
{
g_object_unref (request->socket_connection);
g_object_unref (request->connection);
g_object_unref (request->data);
g_string_free (request->request, TRUE);
g_free (request);
}
static void
broadway_input_free (BroadwayInput *input)
{
g_object_unref (input->connection);
g_byte_array_free (input->buffer, FALSE);
g_source_destroy (input->source);
g_free (input);
}
static void
update_event_state (BroadwayServer *server,
BroadwayInputMsg *message)
{
BroadwaySurface *surface;
switch (message->base.type) {
case BROADWAY_EVENT_ENTER:
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
server->last_state = message->pointer.state;
server->real_mouse_in_surface_id = message->pointer.mouse_surface_id;
/* TODO: Unset when it dies */
server->mouse_in_surface_id = message->pointer.event_surface_id;
break;
case BROADWAY_EVENT_LEAVE:
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
server->last_state = message->pointer.state;
server->real_mouse_in_surface_id = message->pointer.mouse_surface_id;
server->mouse_in_surface_id = 0;
break;
case BROADWAY_EVENT_POINTER_MOVE:
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
server->last_state = message->pointer.state;
server->real_mouse_in_surface_id = message->pointer.mouse_surface_id;
break;
case BROADWAY_EVENT_BUTTON_PRESS:
case BROADWAY_EVENT_BUTTON_RELEASE:
if (message->base.type == BROADWAY_EVENT_BUTTON_PRESS &&
server->focused_surface_id != message->pointer.mouse_surface_id &&
server->pointer_grab_surface_id == -1)
{
broadway_server_surface_raise (server, message->pointer.mouse_surface_id);
broadway_server_focus_surface (server, message->pointer.mouse_surface_id);
broadway_server_flush (server);
}
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
server->last_state = message->pointer.state;
server->real_mouse_in_surface_id = message->pointer.mouse_surface_id;
break;
case BROADWAY_EVENT_SCROLL:
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
server->last_state = message->pointer.state;
server->real_mouse_in_surface_id = message->pointer.mouse_surface_id;
break;
case BROADWAY_EVENT_TOUCH:
if (message->touch.touch_type == 0 && message->touch.is_emulated &&
server->focused_surface_id != message->touch.event_surface_id)
{
broadway_server_surface_raise (server, message->touch.event_surface_id);
broadway_server_focus_surface (server, message->touch.event_surface_id);
broadway_server_flush (server);
}
if (message->touch.is_emulated)
{
server->last_x = message->pointer.root_x;
server->last_y = message->pointer.root_y;
}
server->last_state = message->touch.state;
break;
case BROADWAY_EVENT_KEY_PRESS:
case BROADWAY_EVENT_KEY_RELEASE:
server->last_state = message->key.state;
break;
case BROADWAY_EVENT_GRAB_NOTIFY:
case BROADWAY_EVENT_UNGRAB_NOTIFY:
break;
case BROADWAY_EVENT_CONFIGURE_NOTIFY:
surface = broadway_server_lookup_surface (server, message->configure_notify.id);
if (surface != NULL)
{
surface->x = message->configure_notify.x;
surface->y = message->configure_notify.y;
}
break;
case BROADWAY_EVENT_ROUNDTRIP_NOTIFY:
break;
case BROADWAY_EVENT_SCREEN_SIZE_CHANGED:
server->root->width = message->screen_resize_notify.width;
server->root->height = message->screen_resize_notify.height;
server->screen_scale = message->screen_resize_notify.scale;
break;
default:
g_printerr ("update_event_state - Unknown input command %c\n", message->base.type);
break;
}
}
gboolean
broadway_server_lookahead_event (BroadwayServer *server,
const char *types)
{
BroadwayInputMsg *message;
GList *l;
for (l = server->input_messages; l != NULL; l = l->next)
{
message = l->data;
if (strchr (types, message->base.type) != NULL)
return TRUE;
}
return FALSE;
}
static gboolean
is_pointer_event (BroadwayInputMsg *message)
{
return
message->base.type == BROADWAY_EVENT_ENTER ||
message->base.type == BROADWAY_EVENT_LEAVE ||
message->base.type == BROADWAY_EVENT_POINTER_MOVE ||
message->base.type == BROADWAY_EVENT_BUTTON_PRESS ||
message->base.type == BROADWAY_EVENT_BUTTON_RELEASE ||
message->base.type == BROADWAY_EVENT_SCROLL ||
message->base.type == BROADWAY_EVENT_GRAB_NOTIFY ||
message->base.type == BROADWAY_EVENT_UNGRAB_NOTIFY;
}
static void
process_input_message (BroadwayServer *server,
BroadwayInputMsg *message)
{
gint32 client;
BroadwaySurface *surface;
update_event_state (server, message);
switch (message->base.type) {
case BROADWAY_EVENT_ENTER:
case BROADWAY_EVENT_LEAVE:
case BROADWAY_EVENT_POINTER_MOVE:
case BROADWAY_EVENT_BUTTON_PRESS:
case BROADWAY_EVENT_BUTTON_RELEASE:
case BROADWAY_EVENT_SCROLL:
case BROADWAY_EVENT_GRAB_NOTIFY:
case BROADWAY_EVENT_UNGRAB_NOTIFY:
surface = broadway_server_lookup_surface (server, message->pointer.event_surface_id);
break;
case BROADWAY_EVENT_TOUCH:
surface = broadway_server_lookup_surface (server, message->touch.event_surface_id);
break;
case BROADWAY_EVENT_CONFIGURE_NOTIFY:
surface = broadway_server_lookup_surface (server, message->configure_notify.id);
break;
case BROADWAY_EVENT_ROUNDTRIP_NOTIFY:
surface = broadway_server_lookup_surface (server, message->roundtrip_notify.id);
break;
case BROADWAY_EVENT_KEY_PRESS:
case BROADWAY_EVENT_KEY_RELEASE:
/* TODO: Send to keys focused clients only... */
case BROADWAY_EVENT_FOCUS:
case BROADWAY_EVENT_SCREEN_SIZE_CHANGED:
default:
surface = NULL;
break;
}
if (surface)
client = surface->owner;
else
client = -1;
if (is_pointer_event (message) &&
server->pointer_grab_surface_id != -1)
client = server->pointer_grab_client_id;
broadway_events_got_input (message, client);
}
static void
process_input_messages (BroadwayServer *server)
{
BroadwayInputMsg *message;
while (server->input_messages)
{
message = server->input_messages->data;
server->input_messages =
g_list_delete_link (server->input_messages,
server->input_messages);
if (message->base.serial == 0)
{
/* This was sent before we got any requests, but we don't want the
daemon serials to go backwards, so we fix it up to be the last used
serial */
message->base.serial = server->saved_serial - 1;
}
process_input_message (server, message);
g_free (message);
}
}
static void
fake_configure_notify (BroadwayServer *server,
BroadwaySurface *surface)
{
BroadwayInputMsg ev = { {0} };
ev.base.type = BROADWAY_EVENT_CONFIGURE_NOTIFY;
ev.base.serial = server->saved_serial - 1;
ev.base.time = server->last_seen_time;
ev.configure_notify.id = surface->id;
ev.configure_notify.x = surface->x;
ev.configure_notify.y = surface->y;
ev.configure_notify.width = surface->width;
ev.configure_notify.height = surface->height;
process_input_message (server, &ev);
}
static guint32 *
parse_pointer_data (guint32 *p, BroadwayInputPointerMsg *data)
{
data->mouse_surface_id = ntohl (*p++);
data->event_surface_id = ntohl (*p++);
data->root_x = ntohl (*p++);
data->root_y = ntohl (*p++);
data->win_x = ntohl (*p++);
data->win_y = ntohl (*p++);
data->state = ntohl (*p++);
return p;
}
static guint32 *
parse_touch_data (guint32 *p, BroadwayInputTouchMsg *data)
{
data->touch_type = ntohl (*p++);
data->event_surface_id = ntohl (*p++);
data->sequence_id = ntohl (*p++);
data->is_emulated = ntohl (*p++);
data->root_x = ntohl (*p++);
data->root_y = ntohl (*p++);
data->win_x = ntohl (*p++);
data->win_y = ntohl (*p++);
data->state = ntohl (*p++);
return p;
}
static void
update_future_pointer_info (BroadwayServer *server, BroadwayInputPointerMsg *data)
{
server->future_root_x = data->root_x;
server->future_root_y = data->root_y;
server->future_state = data->state;
server->future_mouse_in_surface = data->mouse_surface_id;
}
static void
queue_input_message (BroadwayServer *server, BroadwayInputMsg *msg)
{
server->input_messages = g_list_append (server->input_messages, g_memdup2 (msg, sizeof (BroadwayInputMsg)));
}
static void
parse_input_message (BroadwayInput *input, const unsigned char *message)
{
BroadwayServer *server = input->server;
BroadwayInputMsg msg;
guint32 *p;
gint64 time_;
GList *l;
memset (&msg, 0, sizeof (msg));
p = (guint32 *) message;
msg.base.type = ntohl (*p++);
msg.base.serial = ntohl (*p++);
time_ = ntohl (*p++);
if (time_ == 0) {
time_ = server->last_seen_time;
} else {
if (!input->seen_time) {
input->seen_time = TRUE;
/* Calculate time base so that any following times are normalized to start
5 seconds after last_seen_time, to avoid issues that could appear when
a long hiatus due to a reconnect seems to be instant */
input->time_base = time_ - (server->last_seen_time + 5000);
}
time_ = time_ - input->time_base;
}
server->last_seen_time = time_;
msg.base.time = time_;
switch (msg.base.type) {
case BROADWAY_EVENT_ENTER:
case BROADWAY_EVENT_LEAVE:
p = parse_pointer_data (p, &msg.pointer);
update_future_pointer_info (server, &msg.pointer);
msg.crossing.mode = ntohl (*p++);
break;
case BROADWAY_EVENT_POINTER_MOVE: /* Mouse move */
p = parse_pointer_data (p, &msg.pointer);
update_future_pointer_info (server, &msg.pointer);
break;
case BROADWAY_EVENT_BUTTON_PRESS:
case BROADWAY_EVENT_BUTTON_RELEASE:
p = parse_pointer_data (p, &msg.pointer);
update_future_pointer_info (server, &msg.pointer);
msg.button.button = ntohl (*p++);
break;
case BROADWAY_EVENT_SCROLL:
p = parse_pointer_data (p, &msg.pointer);
update_future_pointer_info (server, &msg.pointer);
msg.scroll.dir = ntohl (*p++);
break;
case BROADWAY_EVENT_TOUCH:
p = parse_touch_data (p, &msg.touch);
break;
case BROADWAY_EVENT_KEY_PRESS:
case BROADWAY_EVENT_KEY_RELEASE:
msg.key.surface_id = server->focused_surface_id;
msg.key.key = ntohl (*p++);
msg.key.state = ntohl (*p++);
break;
case BROADWAY_EVENT_GRAB_NOTIFY:
case BROADWAY_EVENT_UNGRAB_NOTIFY:
msg.grab_reply.res = ntohl (*p++);
break;
case BROADWAY_EVENT_CONFIGURE_NOTIFY:
msg.configure_notify.id = ntohl (*p++);
msg.configure_notify.x = ntohl (*p++);
msg.configure_notify.y = ntohl (*p++);
msg.configure_notify.width = ntohl (*p++);
msg.configure_notify.height = ntohl (*p++);
break;
case BROADWAY_EVENT_ROUNDTRIP_NOTIFY:
msg.roundtrip_notify.id = ntohl (*p++);
msg.roundtrip_notify.tag = ntohl (*p++);
msg.roundtrip_notify.local = FALSE;
/* Remove matched outstanding roundtrips */
for (l = server->outstanding_roundtrips; l != NULL; l = l->next)
{
BroadwayOutstandingRoundtrip *rt = l->data;
if (rt->id == msg.roundtrip_notify.id &&
rt->tag == msg.roundtrip_notify.tag)
break;
}
if (l == NULL)
g_warning ("Got unexpected roundtrip reply for id %d, tag %d\n", msg.roundtrip_notify.id, msg.roundtrip_notify.tag);
else
{
BroadwayOutstandingRoundtrip *rt = l->data;
server->outstanding_roundtrips = g_list_delete_link (server->outstanding_roundtrips, l);
g_free (rt);
}
break;
case BROADWAY_EVENT_SCREEN_SIZE_CHANGED:
msg.screen_resize_notify.width = ntohl (*p++);
msg.screen_resize_notify.height = ntohl (*p++);
msg.screen_resize_notify.scale = ntohl (*p++);
break;
default:
g_printerr ("parse_input_message - Unknown input command %c (%s)\n", msg.base.type, message);
break;
}
queue_input_message (server, &msg);
}
static inline void
hex_dump (guchar *data, gsize len)
{
#ifdef DEBUG_WEBSOCKETS
gsize i, j;
for (j = 0; j < len + 15; j += 16)
{
fprintf (stderr, "0x%.4x ", j);
for (i = 0; i < 16; i++)
{
if ((j + i) < len)
fprintf (stderr, "%.2x ", data[j+i]);
else
fprintf (stderr, " ");
if (i == 8)
fprintf (stderr, " ");
}
fprintf (stderr, " | ");
for (i = 0; i < 16; i++)
if ((j + i) < len && g_ascii_isalnum(data[j+i]))
fprintf (stderr, "%c", data[j+i]);
else
fprintf (stderr, ".");
fprintf (stderr, "\n");
}
#endif
}
static void
parse_input (BroadwayInput *input)
{
if (!input->buffer->len)
return;
hex_dump (input->buffer->data, input->buffer->len);
while (input->buffer->len > 2)
{
gsize len, payload_len;
BroadwayWSOpCode code;
gboolean is_mask, fin;
guchar *buf, *data, *mask;
buf = input->buffer->data;
len = input->buffer->len;
#ifdef DEBUG_WEBSOCKETS
g_print ("Parse input first byte 0x%2x 0x%2x\n", buf[0], buf[1]);
#endif
fin = buf[0] & 0x80;
code = buf[0] & 0x0f;
payload_len = buf[1] & 0x7f;
is_mask = buf[1] & 0x80;
data = buf + 2;
if (payload_len == 126)
{
if (len < 4)
return;
payload_len = GUINT16_FROM_BE( *(guint16 *) data );
data += 2;
}
else if (payload_len == 127)
{
if (len < 10)
return;
payload_len = GUINT64_FROM_BE( *(guint64 *) data );
data += 8;
}
mask = NULL;
if (is_mask)
{
if (data - buf + 4 > len)
return;
mask = data;
data += 4;
}
if (data - buf + payload_len > len)
return; /* wait to accumulate more */
if (is_mask)
{
gsize i;
for (i = 0; i < payload_len; i++)
data[i] ^= mask[i%4];
}
switch (code) {
case BROADWAY_WS_CNX_CLOSE:
break; /* hang around anyway */
case BROADWAY_WS_BINARY:
if (!fin)
{
#ifdef DEBUG_WEBSOCKETS
g_warning ("can't yet accept fragmented input");
#endif
}
else
{
parse_input_message (input, data);
}
break;
case BROADWAY_WS_CNX_PING:
broadway_output_pong (input->output);
break;
case BROADWAY_WS_CNX_PONG:
break; /* we never send pings, but tolerate pongs */
case BROADWAY_WS_TEXT:
case BROADWAY_WS_CONTINUATION:
default:
{
g_warning ("fragmented or unknown input code 0x%2x with fin set", code);
break;
}
}
g_byte_array_remove_range (input->buffer, 0, data - buf + payload_len);
}
}
static gboolean
process_input_idle_cb (BroadwayServer *server)
{
server->process_input_idle = 0;
process_input_messages (server);
return G_SOURCE_REMOVE;
}
static void
queue_process_input_at_idle (BroadwayServer *server)
{
if (server->process_input_idle == 0)
server->process_input_idle =
g_idle_add_full (G_PRIORITY_DEFAULT, (GSourceFunc)process_input_idle_cb, server, NULL);
}
static gboolean
broadway_server_read_all_input_nonblocking (BroadwayInput *input)
{
GInputStream *in;
gssize res;
guint8 buffer[1024];
GError *error = NULL;
if (input == NULL)
return FALSE;
in = g_io_stream_get_input_stream (input->connection);
res = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (in),
buffer, sizeof (buffer), NULL, &error);
if (res <= 0)
{
if (res < 0 &&
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
{
g_error_free (error);
return TRUE;
}
if (input->server->input == input)
{
send_outstanding_roundtrips (input->server);
input->server->input = NULL;
}
broadway_input_free (input);
if (res < 0)
{
g_printerr ("input error %s\n", error->message);
g_error_free (error);
}
return FALSE;
}
g_byte_array_append (input->buffer, buffer, res);
parse_input (input);
return TRUE;
}
static void
broadway_server_consume_all_input (BroadwayServer *server)
{
broadway_server_read_all_input_nonblocking (server->input);
/* Since we're parsing input but not processing the resulting messages
we might not get a readable callback on the stream, so queue an idle to
process the messages */
queue_process_input_at_idle (server);
}
static gboolean
input_data_cb (GObject *stream,
BroadwayInput *input)
{
BroadwayServer *server = input->server;
if (!broadway_server_read_all_input_nonblocking (input))
return FALSE;
if (input->active)
process_input_messages (server);
return TRUE;
}
guint32
broadway_server_get_next_serial (BroadwayServer *server)
{
if (server->output)
return broadway_output_get_next_serial (server->output);
return server->saved_serial;
}
void
broadway_server_get_screen_size (BroadwayServer *server,
guint32 *width,
guint32 *height,
guint32 *scale)
{
*width = server->root->width;
*height = server->root->height;
*scale = server->screen_scale;
}
static void
broadway_server_fake_roundtrip_reply (BroadwayServer *server,
int id,
guint32 tag)
{
BroadwayInputMsg msg;
msg.base.type = BROADWAY_EVENT_ROUNDTRIP_NOTIFY;
msg.base.serial = 0;
msg.base.time = server->last_seen_time;
msg.roundtrip_notify.id = id;
msg.roundtrip_notify.tag = tag;
msg.roundtrip_notify.local = 1;
queue_input_message (server, &msg);
queue_process_input_at_idle (server);
}
void
broadway_server_flush (BroadwayServer *server)
{
if (server->output &&
!broadway_output_flush (server->output))
{
server->saved_serial = broadway_output_get_next_serial (server->output);
broadway_output_free (server->output);
server->output = NULL;
send_outstanding_roundtrips (server);
}
}
void
broadway_server_roundtrip (BroadwayServer *server,
int id,
guint32 tag)
{
if (server->output)
{
BroadwayOutstandingRoundtrip *rt = g_new0 (BroadwayOutstandingRoundtrip, 1);
rt->id = id;
rt->tag = tag;
server->outstanding_roundtrips = g_list_prepend (server->outstanding_roundtrips, rt);
broadway_output_roundtrip (server->output, id, tag);
}
else
broadway_server_fake_roundtrip_reply (server, id, tag);
}
static const char *
parse_line (const char *line, const char *key)
{
const char *p;
if (g_ascii_strncasecmp (line, key, strlen (key)) != 0)
return NULL;
p = line + strlen (key);
if (*p != ':')
return NULL;
p++;
/* Skip optional initial space */
if (*p == ' ')
p++;
return p;
}
static void
send_error (HttpRequest *request,
int error_code,
const char *reason)
{
char *res;
res = g_strdup_printf ("HTTP/1.0 %d %s\r\n\r\n"
"<html><head><title>%d %s</title></head>"
"<body>%s</body></html>",
error_code, reason,
error_code, reason,
reason);
/* TODO: This should really be async */
g_output_stream_write_all (g_io_stream_get_output_stream (request->connection),
res, strlen (res), NULL, NULL, NULL);
g_free (res);
http_request_free (request);
}
/* magic from: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17 */
#define SEC_WEB_SOCKET_KEY_MAGIC "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
/* 'x3JJHMbDL1EzLkh9GBhXDw==' generates 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=' */
static char *
generate_handshake_response_wsietf_v7 (const char *key)
{
gsize digest_len = 20;
guchar digest[20];
GChecksum *checksum;
checksum = g_checksum_new (G_CHECKSUM_SHA1);
if (!checksum)
return NULL;
g_checksum_update (checksum, (guchar *)key, -1);
g_checksum_update (checksum, (guchar *)SEC_WEB_SOCKET_KEY_MAGIC, -1);
g_checksum_get_digest (checksum, digest, &digest_len);
g_checksum_free (checksum);
g_assert (digest_len == 20);
return g_base64_encode (digest, digest_len);
}
static void
start_input (HttpRequest *request)
{
char **lines;
const char *p;
int i;
char *res;
const char *origin, *host;
BroadwayInput *input;
const void *data_buffer;
gsize data_buffer_size;
GInputStream *in;
const char *key;
GSocket *socket;
int flag = 1;
#ifdef DEBUG_WEBSOCKETS
g_print ("incoming request:\n%s\n", request->request->str);
#endif
lines = g_strsplit (request->request->str, "\n", 0);
key = NULL;
origin = NULL;
host = NULL;
for (i = 0; lines[i] != NULL; i++)
{
if ((p = parse_line (lines[i], "Sec-WebSocket-Key")))
key = p;
else if ((p = parse_line (lines[i], "Origin")))
origin = p;
else if ((p = parse_line (lines[i], "Host")))
host = p;
else if ((p = parse_line (lines[i], "Sec-WebSocket-Origin")))
origin = p;
}
if (host == NULL)
{
g_strfreev (lines);
send_error (request, 400, "Bad websocket request");
return;
}
if (key != NULL)
{
char* accept = generate_handshake_response_wsietf_v7 (key);
res = g_strdup_printf ("HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: %s\r\n"
"%s%s%s"
"Sec-WebSocket-Location: ws://%s/socket\r\n"
"Sec-WebSocket-Protocol: broadway\r\n"
"\r\n", accept,
origin?"Sec-WebSocket-Origin: ":"", origin?origin:"", origin?"\r\n":"",
host);
g_free (accept);
#ifdef DEBUG_WEBSOCKETS
g_print ("v7 proto response:\n%s", res);
#endif
g_output_stream_write_all (g_io_stream_get_output_stream (request->connection),
res, strlen (res), NULL, NULL, NULL);
g_free (res);
}
else
{
g_strfreev (lines);
send_error (request, 400, "Bad websocket request");
return;
}
socket = g_socket_connection_get_socket (request->socket_connection);
setsockopt (g_socket_get_fd (socket), IPPROTO_TCP,
TCP_NODELAY, (char *) &flag, sizeof(int));
input = g_new0 (BroadwayInput, 1);
input->server = request->server;
input->connection = g_object_ref (request->connection);
data_buffer = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (request->data), &data_buffer_size);
input->buffer = g_byte_array_sized_new (data_buffer_size);
g_byte_array_append (input->buffer, data_buffer, data_buffer_size);
input->output =
broadway_output_new (g_io_stream_get_output_stream (request->connection), 0);
/* This will free and close the data input stream, but we got all the buffered content already */
http_request_free (request);
in = g_io_stream_get_input_stream (input->connection);
input->source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (in), NULL);
g_source_set_callback (input->source, (GSourceFunc)input_data_cb, input, NULL);
g_source_attach (input->source, NULL);
start (input);
/* Process any data in the pipe already */
parse_input (input);
g_strfreev (lines);
}
static void
send_outstanding_roundtrips (BroadwayServer *server)
{
GList *l;
for (l = server->outstanding_roundtrips; l != NULL; l = l->next)
{
BroadwayOutstandingRoundtrip *rt = l->data;
broadway_server_fake_roundtrip_reply (server, rt->id, rt->tag);
}
g_list_free_full (server->outstanding_roundtrips, g_free);
server->outstanding_roundtrips = NULL;
}
static void
start (BroadwayInput *input)
{
BroadwayServer *server;
input->active = TRUE;
server = BROADWAY_SERVER (input->server);
if (server->output)
{
send_outstanding_roundtrips (server);
broadway_output_disconnected (server->output);
broadway_output_flush (server->output);
}
if (server->input != NULL)
{
send_outstanding_roundtrips (server);
broadway_input_free (server->input);
server->input = NULL;
}
server->input = input;
if (server->output)
{
server->saved_serial = broadway_output_get_next_serial (server->output);
broadway_output_free (server->output);
}
server->output = input->output;
broadway_output_set_next_serial (server->output, server->saved_serial);
broadway_output_flush (server->output);
broadway_server_resync_surfaces (server);
if (server->pointer_grab_surface_id != -1)
broadway_output_grab_pointer (server->output,
server->pointer_grab_surface_id,
server->pointer_grab_owner_events);
process_input_messages (server);
}
static void
send_data (HttpRequest *request,
const char *mimetype,
const char *data, gsize len)
{
char *res;
res = g_strdup_printf ("HTTP/1.0 200 OK\r\n"
"Content-Type: %s\r\n"
"Content-Length: %"G_GSIZE_FORMAT"\r\n"
"\r\n",
mimetype, len);
/* TODO: This should really be async */
g_output_stream_write_all (g_io_stream_get_output_stream (request->connection),
res, strlen (res), NULL, NULL, NULL);
g_free (res);
g_output_stream_write_all (g_io_stream_get_output_stream (request->connection),
data, len, NULL, NULL, NULL);
http_request_free (request);
}
#include "clienthtml.h"
#include "broadwayjs.h"
static void
got_request (HttpRequest *request)
{
char *start, *escaped, *tmp, *version, *query;
if (!g_str_has_prefix (request->request->str, "GET "))
{
send_error (request, 501, "Only GET implemented");
return;
}
start = request->request->str + 4; /* Skip "GET " */
while (*start == ' ')
start++;
for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
;
escaped = g_strndup (start, tmp - start);
version = NULL;
if (*tmp == ' ')
{
start = tmp;
while (*start == ' ')
start++;
for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
;
version = g_strndup (start, tmp - start);
}
query = strchr (escaped, '?');
if (query)
*query = 0;
if (strcmp (escaped, "/client.html") == 0 || strcmp (escaped, "/") == 0)
send_data (request, "text/html", client_html, G_N_ELEMENTS(client_html) - 1);
else if (strcmp (escaped, "/broadway.js") == 0)
send_data (request, "text/javascript", broadway_js, G_N_ELEMENTS(broadway_js) - 1);
else if (strcmp (escaped, "/socket") == 0)
start_input (request);
else
send_error (request, 404, "File not found");
g_free (escaped);
g_free (version);
}
static void
got_http_request_line (GInputStream *stream,
GAsyncResult *result,
HttpRequest *request)
{
char *line;
line = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (stream), result, NULL, NULL);
if (line == NULL)
{
http_request_free (request);
g_printerr ("Error reading request lines\n");
return;
}
if (strlen (line) == 0)
got_request (request);
else
{
/* Protect against overflow in request length */
if (request->request->len > 1024 * 5)
{
send_error (request, 400, "Request too long");
}
else
{
g_string_append_printf (request->request, "%s\n", line);
g_data_input_stream_read_line_async (request->data, 0, NULL,
(GAsyncReadyCallback)got_http_request_line, request);
}
}
g_free (line);
}
static gboolean
handle_incoming_connection (GSocketService *service,
GSocketConnection *connection,
GObject *source_object)
{
HttpRequest *request;
GInputStream *in;
request = g_new0 (HttpRequest, 1);
request->socket_connection = g_object_ref (connection);
request->server = BROADWAY_SERVER (source_object);
request->request = g_string_new ("");
if (request->server->ssl_cert && request->server->ssl_key)
{
GError *error = NULL;
GTlsCertificate *certificate;
certificate = g_tls_certificate_new_from_files (request->server->ssl_cert,
request->server->ssl_key,
&error);
if (!certificate)
{
g_warning ("Cannot create TLS certificate: %s", error->message);
g_error_free (error);
return FALSE;
}
request->connection = g_tls_server_connection_new (G_IO_STREAM (connection),
certificate,
&error);
if (!request->connection)
{
g_warning ("Cannot create TLS connection: %s", error->message);
g_error_free (error);
return FALSE;
}
if (!g_tls_connection_handshake (G_TLS_CONNECTION (request->connection),
NULL, &error))
{
g_warning ("Cannot create TLS connection: %s", error->message);
g_error_free (error);
return FALSE;
}
}
else
{
request->connection = G_IO_STREAM (g_object_ref (connection));
}
in = g_io_stream_get_input_stream (request->connection);
request->data = g_data_input_stream_new (in);
g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (request->data), FALSE);
/* Be tolerant of input */
g_data_input_stream_set_newline_type (request->data, G_DATA_STREAM_NEWLINE_TYPE_ANY);
g_data_input_stream_read_line_async (request->data, 0, NULL,
(GAsyncReadyCallback)got_http_request_line, request);
return TRUE;
}
BroadwayServer *
broadway_server_new (char *address,
int port,
const char *ssl_cert,
const char *ssl_key,
GError **error)
{
BroadwayServer *server;
GInetAddress *inet_address;
GSocketAddress *socket_address;
server = g_object_new (BROADWAY_TYPE_SERVER, NULL);
server->port = port;
server->address = g_strdup (address);
server->ssl_cert = g_strdup (ssl_cert);
server->ssl_key = g_strdup (ssl_key);
if (address == NULL)
{
if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (server->service),
server->port,
G_OBJECT (server),
error))
{
g_prefix_error (error, "Unable to listen to port %d: ", server->port);
g_object_unref (server);
return NULL;
}
}
else
{
inet_address = g_inet_address_new_from_string (address);
if (inet_address == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Invalid ip address %s: ", address);
g_object_unref (server);
return NULL;
}
socket_address = g_inet_socket_address_new (inet_address, port);
g_object_unref (inet_address);
if (!g_socket_listener_add_address (G_SOCKET_LISTENER (server->service),
socket_address,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_TCP,
G_OBJECT (server),
NULL,
error))
{
g_prefix_error (error, "Unable to listen to %s:%d: ", server->address, server->port);
g_object_unref (socket_address);
g_object_unref (server);
return NULL;
}
g_object_unref (socket_address);
}
g_signal_connect (server->service, "incoming",
G_CALLBACK (handle_incoming_connection), NULL);
return server;
}
BroadwayServer *
broadway_server_on_unix_socket_new (char *address, GError **error)
{
BroadwayServer *server;
GSocketAddress *socket_address = NULL;
server = g_object_new (BROADWAY_TYPE_SERVER, NULL);
server->port = -1;
server->address = g_strdup (address);
if (address == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unspecified unix domain socket address");
g_object_unref (server);
return NULL;
}
else
{
#ifdef HAVE_GIO_UNIX
socket_address = g_unix_socket_address_new (address);
#endif
if (socket_address == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Invalid unix domain socket address %s: ", address);
g_object_unref (server);
return NULL;
}
if (!g_socket_listener_add_address (G_SOCKET_LISTENER (server->service),
socket_address,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
G_OBJECT (server),
NULL,
error))
{
g_prefix_error (error, "Unable to listen to %s: ", server->address);
g_object_unref (socket_address);
g_object_unref (server);
return NULL;
}
g_object_unref (socket_address);
}
g_signal_connect (server->service, "incoming",
G_CALLBACK (handle_incoming_connection), NULL);
return server;
}
guint32
broadway_server_get_last_seen_time (BroadwayServer *server)
{
broadway_server_consume_all_input (server);
return (guint32) server->last_seen_time;
}
void
broadway_server_query_mouse (BroadwayServer *server,
guint32 *surface,
gint32 *root_x,
gint32 *root_y,
guint32 *mask)
{
if (server->output)
{
broadway_server_consume_all_input (server);
if (root_x)
*root_x = server->future_root_x;
if (root_y)
*root_y = server->future_root_y;
if (mask)
*mask = server->future_state;
if (surface)
*surface = server->future_mouse_in_surface;
return;
}
/* Fallback when unconnected */
if (root_x)
*root_x = server->last_x;
if (root_y)
*root_y = server->last_y;
if (mask)
*mask = server->last_state;
if (surface)
*surface = server->mouse_in_surface_id;
}
void
broadway_server_destroy_surface (BroadwayServer *server,
int id)
{
BroadwaySurface *surface;
if (server->mouse_in_surface_id == id)
{
/* TODO: Send leave + enter event, update cursors, etc */
server->mouse_in_surface_id = 0;
}
if (server->pointer_grab_surface_id == id)
server->pointer_grab_surface_id = -1;
if (server->output)
broadway_output_destroy_surface (server->output,
id);
surface = broadway_server_lookup_surface (server, id);
if (surface != NULL)
{
server->surfaces = g_list_remove (server->surfaces, surface);
g_hash_table_remove (server->surface_id_hash,
GINT_TO_POINTER (id));
broadway_surface_free (server, surface);
}
}
gboolean
broadway_server_surface_show (BroadwayServer *server,
int id)
{
BroadwaySurface *surface;
gboolean sent = FALSE;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return FALSE;
surface->visible = TRUE;
if (server->output)
{
broadway_output_show_surface (server->output, surface->id);
sent = TRUE;
}
return sent;
}
gboolean
broadway_server_surface_hide (BroadwayServer *server,
int id)
{
BroadwaySurface *surface;
gboolean sent = FALSE;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return FALSE;
surface->visible = FALSE;
if (server->mouse_in_surface_id == id)
{
/* TODO: Send leave + enter event, update cursors, etc */
server->mouse_in_surface_id = 0;
}
if (server->pointer_grab_surface_id == id)
server->pointer_grab_surface_id = -1;
if (server->output)
{
broadway_output_hide_surface (server->output, surface->id);
sent = TRUE;
}
return sent;
}
void
broadway_server_surface_raise (BroadwayServer *server,
int id)
{
BroadwaySurface *surface;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return;
server->surfaces = g_list_remove (server->surfaces, surface);
server->surfaces = g_list_append (server->surfaces, surface);
if (server->output)
broadway_output_raise_surface (server->output, surface->id);
}
void
broadway_server_set_show_keyboard (BroadwayServer *server,
gboolean show)
{
server->show_keyboard = show;
if (server->output)
{
broadway_output_set_show_keyboard (server->output, server->show_keyboard);
broadway_server_flush (server);
}
}
void
broadway_server_surface_lower (BroadwayServer *server,
int id)
{
BroadwaySurface *surface;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return;
server->surfaces = g_list_remove (server->surfaces, surface);
server->surfaces = g_list_prepend (server->surfaces, surface);
if (server->output)
broadway_output_lower_surface (server->output, surface->id);
}
void
broadway_server_surface_set_transient_for (BroadwayServer *server,
int id, int parent)
{
BroadwaySurface *surface;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return;
surface->transient_for = parent;
if (server->output)
{
broadway_output_set_transient_for (server->output, surface->id, surface->transient_for);
broadway_server_flush (server);
}
}
gboolean
broadway_server_has_client (BroadwayServer *server)
{
return server->output != NULL;
}
#define NODE_SIZE_COLOR 1
#define NODE_SIZE_FLOAT 1
#define NODE_SIZE_POINT 2
#define NODE_SIZE_MATRIX 16
#define NODE_SIZE_SIZE 2
#define NODE_SIZE_RECT (NODE_SIZE_POINT + NODE_SIZE_SIZE)
#define NODE_SIZE_RRECT (NODE_SIZE_RECT + 4 * NODE_SIZE_SIZE)
#define NODE_SIZE_COLOR_STOP (NODE_SIZE_FLOAT + NODE_SIZE_COLOR)
#define NODE_SIZE_SHADOW (NODE_SIZE_COLOR + 3 * NODE_SIZE_FLOAT)
static guint32
rotl (guint32 value, int shift)
{
if ((shift &= 32 - 1) == 0)
return value;
return (value << shift) | (value >> (32 - shift));
}
static BroadwayNode *
decode_nodes (BroadwayServer *server,
BroadwaySurface *surface,
int len,
guint32 data[],
GHashTable *client_texture_map,
int *pos)
{
BroadwayNode *node;
guint32 type, id;
guint32 i, n_stops, n_shadows, n_chars;
guint32 size, n_children;
gint32 texture_offset;
guint32 hash;
guint32 transform_type;
g_assert (*pos < len);
size = 0;
n_children = 0;
texture_offset = -1;
type = data[(*pos)++];
id = data[(*pos)++];
switch (type) {
case BROADWAY_NODE_REUSE:
node = g_hash_table_lookup (surface->node_lookup, GINT_TO_POINTER(id));
g_assert (node != NULL);
return broadway_node_ref (node);
break;
case BROADWAY_NODE_COLOR:
size = NODE_SIZE_RECT + NODE_SIZE_COLOR;
break;
case BROADWAY_NODE_BORDER:
size = NODE_SIZE_RRECT + 4 * NODE_SIZE_FLOAT + 4 * NODE_SIZE_COLOR;
break;
case BROADWAY_NODE_INSET_SHADOW:
case BROADWAY_NODE_OUTSET_SHADOW:
size = NODE_SIZE_RRECT + NODE_SIZE_COLOR + 4 * NODE_SIZE_FLOAT;
break;
case BROADWAY_NODE_TEXTURE:
texture_offset = 4;
size = 5;
break;
case BROADWAY_NODE_CONTAINER:
size = 1;
n_children = data[*pos];
break;
case BROADWAY_NODE_ROUNDED_CLIP:
size = NODE_SIZE_RRECT;
n_children = 1;
break;
case BROADWAY_NODE_CLIP:
size = NODE_SIZE_RECT;
n_children = 1;
break;
case BROADWAY_NODE_TRANSFORM:
transform_type = data[(*pos)];
size = 1;
if (transform_type == 0) {
size += NODE_SIZE_POINT;
} else if (transform_type == 1) {
size += NODE_SIZE_MATRIX;
} else {
g_assert_not_reached();
}
n_children = 1;
break;
case BROADWAY_NODE_LINEAR_GRADIENT:
size = NODE_SIZE_RECT + 2 * NODE_SIZE_POINT;
n_stops = data[*pos + size++];
size += n_stops * NODE_SIZE_COLOR_STOP;
break;
case BROADWAY_NODE_SHADOW:
size = 1;
n_shadows = data[*pos];
size += n_shadows * NODE_SIZE_SHADOW;
n_children = 1;
break;
case BROADWAY_NODE_OPACITY:
size = NODE_SIZE_FLOAT;
n_children = 1;
break;
case BROADWAY_NODE_DEBUG:
n_chars = data[*pos];
size = 1 + (n_chars + 3) / 4;
n_children = 1;
break;
default:
g_assert_not_reached ();
}
node = g_malloc (sizeof(BroadwayNode) + (size - 1) * sizeof(guint32) + n_children * sizeof (BroadwayNode *));
g_ref_count_init (&node->refcount);
node->type = type;
node->id = id;
node->output_id = id;
node->texture_id = 0;
node->n_children = n_children;
node->children = (BroadwayNode **)((char *)node + sizeof(BroadwayNode) + (size - 1) * sizeof(guint32));
node->n_data = size;
for (i = 0; i < size; i++)
{
node->data[i] = data[(*pos)++];
if (i == texture_offset)
{
node->texture_id = GPOINTER_TO_INT (g_hash_table_lookup (client_texture_map, GINT_TO_POINTER (node->data[i])));
broadway_server_ref_texture (server, node->texture_id);
node->data[i] = node->texture_id;
}
}
for (i = 0; i < n_children; i++)
node->children[i] = decode_nodes (server, surface, len, data, client_texture_map, pos);
hash = node->type << 16;
for (i = 0; i < size; i++)
hash ^= rotl (node->data[i], i);
for (i = 0; i < n_children; i++)
hash ^= rotl (node->children[i]->hash, i);
node->hash = hash;
return node;
}
/* passes ownership of nodes */
void
broadway_server_surface_update_nodes (BroadwayServer *server,
int id,
guint32 data[],
int len,
GHashTable *client_texture_map)
{
BroadwaySurface *surface;
int pos = 0;
BroadwayNode *root;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return;
root = decode_nodes (server, surface, len, data, client_texture_map, &pos);
if (server->output != NULL)
broadway_output_surface_set_nodes (server->output, surface->id,
root,
surface->nodes,
surface->node_lookup);
if (surface->nodes)
broadway_node_unref (server, surface->nodes);
surface->nodes = root;
g_hash_table_remove_all (surface->node_lookup);
broadway_node_add_to_lookup (root, surface->node_lookup);
}
guint32
broadway_server_upload_texture (BroadwayServer *server,
GBytes *bytes)
{
BroadwayTexture *texture;
texture = g_new0 (BroadwayTexture, 1);
g_ref_count_init (&texture->refcount);
texture->id = ++server->next_texture_id;
texture->bytes = g_bytes_ref (bytes);
g_hash_table_replace (server->textures,
GINT_TO_POINTER (texture->id),
texture);
if (server->output)
broadway_output_upload_texture (server->output, texture->id, texture->bytes);
return texture->id;
}
static void
broadway_server_ref_texture (BroadwayServer *server,
guint32 id)
{
BroadwayTexture *texture;
texture = g_hash_table_lookup (server->textures, GINT_TO_POINTER (id));
if (texture)
g_ref_count_inc (&texture->refcount);
}
void
broadway_server_release_texture (BroadwayServer *server,
guint32 id)
{
BroadwayTexture *texture;
texture = g_hash_table_lookup (server->textures, GINT_TO_POINTER (id));
if (texture && g_ref_count_dec (&texture->refcount))
{
g_hash_table_remove (server->textures, GINT_TO_POINTER (id));
if (server->output)
broadway_output_release_texture (server->output, id);
}
}
gboolean
broadway_server_surface_move_resize (BroadwayServer *server,
int id,
gboolean with_move,
int x,
int y,
int width,
int height)
{
BroadwaySurface *surface;
gboolean with_resize;
gboolean sent = FALSE;
surface = broadway_server_lookup_surface (server, id);
if (surface == NULL)
return FALSE;
with_resize = width != surface->width || height != surface->height;
surface->width = width;
surface->height = height;
if (server->output != NULL)
{
broadway_output_move_resize_surface (server->output,
surface->id,
with_move, x, y,
with_resize, surface->width, surface->height);
sent = TRUE;
}
else
{
if (with_move)
{
surface->x = x;
surface->y = y;
}
fake_configure_notify (server, surface);
}
return sent;
}
void
broadway_server_focus_surface (BroadwayServer *server,
int new_focused_surface)
{
BroadwayInputMsg focus_msg;
if (server->focused_surface_id == new_focused_surface)
return;
memset (&focus_msg, 0, sizeof (focus_msg));
focus_msg.base.type = BROADWAY_EVENT_FOCUS;
focus_msg.base.time = broadway_server_get_last_seen_time (server);
focus_msg.focus.old_id = server->focused_surface_id;
focus_msg.focus.new_id = new_focused_surface;
broadway_events_got_input (&focus_msg, -1);
/* Keep track of the new focused surface */
server->focused_surface_id = new_focused_surface;
}
guint32
broadway_server_grab_pointer (BroadwayServer *server,
int client_id,
int id,
gboolean owner_events,
guint32 event_mask,
guint32 time_)
{
if (server->pointer_grab_surface_id != -1 &&
time_ != 0 && server->pointer_grab_time > time_)
return GDK_GRAB_ALREADY_GRABBED;
if (time_ == 0)
time_ = server->last_seen_time;
server->pointer_grab_surface_id = id;
server->pointer_grab_client_id = client_id;
server->pointer_grab_owner_events = owner_events;
server->pointer_grab_time = time_;
if (server->output)
{
broadway_output_grab_pointer (server->output,
id,
owner_events);
broadway_server_flush (server);
}
/* TODO: What about surface grab events if we're not connected? */
return GDK_GRAB_SUCCESS;
}
guint32
broadway_server_ungrab_pointer (BroadwayServer *server,
guint32 time_)
{
guint32 serial;
if (server->pointer_grab_surface_id != -1 &&
time_ != 0 && server->pointer_grab_time > time_)
return 0;
/* TODO: What about surface grab events if we're not connected? */
if (server->output)
{
serial = broadway_output_ungrab_pointer (server->output);
broadway_server_flush (server);
}
else
{
serial = server->saved_serial;
}
server->pointer_grab_surface_id = -1;
return serial;
}
guint32
broadway_server_new_surface (BroadwayServer *server,
guint32 client,
int x,
int y,
int width,
int height)
{
BroadwaySurface *surface;
surface = g_new0 (BroadwaySurface, 1);
surface->owner = client;
surface->id = server->id_counter++;
surface->x = x;
surface->y = y;
surface->width = width;
surface->height = height;
surface->node_lookup = g_hash_table_new (g_direct_hash, g_direct_equal);
g_hash_table_insert (server->surface_id_hash,
GINT_TO_POINTER (surface->id),
surface);
server->surfaces = g_list_append (server->surfaces, surface);
if (server->output)
broadway_output_new_surface (server->output,
surface->id,
surface->x,
surface->y,
surface->width,
surface->height);
else
fake_configure_notify (server, surface);
return surface->id;
}
static void
broadway_server_resync_surfaces (BroadwayServer *server)
{
GHashTableIter iter;
gpointer key, value;
GList *l;
if (server->output == NULL)
return;
/* First upload all textures */
g_hash_table_iter_init (&iter, server->textures);
while (g_hash_table_iter_next (&iter, &key, &value))
{
BroadwayTexture *texture = value;
broadway_output_upload_texture (server->output,
GPOINTER_TO_INT (key),
texture->bytes);
}
/* Then create all surfaces */
for (l = server->surfaces; l != NULL; l = l->next)
{
BroadwaySurface *surface = l->data;
if (surface->id == 0)
continue; /* Skip root */
broadway_output_new_surface (server->output,
surface->id,
surface->x,
surface->y,
surface->width,
surface->height);
}
/* Then do everything that may reference other surfaces */
for (l = server->surfaces; l != NULL; l = l->next)
{
BroadwaySurface *surface = l->data;
if (surface->id == 0)
continue; /* Skip root */
if (surface->transient_for != -1)
broadway_output_set_transient_for (server->output, surface->id,
surface->transient_for);
if (surface->nodes)
broadway_output_surface_set_nodes (server->output, surface->id,
surface->nodes,
NULL, NULL);
if (surface->visible)
broadway_output_show_surface (server->output, surface->id);
}
if (server->show_keyboard)
broadway_output_set_show_keyboard (server->output, TRUE);
broadway_server_flush (server);
}
|