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
|
/* $Xorg: lbxmain.c,v 1.4 2001/02/09 02:05:16 xorgcvs Exp $ */
/*
Copyright 1996, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
/*
* Copyright 1992 Network Computing Devices
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of NCD. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. NCD. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* NCD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NCD.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/* $XFree86: xc/programs/Xserver/lbx/lbxmain.c,v 1.13 2001/12/14 20:00:00 dawes Exp $ */
#include <sys/types.h>
#define NEED_REPLIES
#define NEED_EVENTS
#include "X.h"
#include "Xproto.h"
#include "Xos.h"
#include "misc.h"
#include "os.h"
#include "dixstruct.h"
#include "resource.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "gcstruct.h"
#include "extnsionst.h"
#include "servermd.h"
#define _XLBX_SERVER_
#include "lbxstr.h"
#include "lbxdeltastr.h"
#include "lbxserve.h"
#include "lbximage.h"
#include "lbxsrvopts.h"
#include "lbxtags.h"
#include "Xfuncproto.h"
#include <errno.h>
#ifndef Lynx
#include <sys/uio.h>
#else
#include <uio.h>
#endif
#include <stdio.h>
#ifndef X_NOT_POSIX
#include <unistd.h>
#endif
#define CloseLbxClient 0xff
#define MAXBYTESDIFF 8
int LbxWhoAmI = 1; /*
* for lbx zlib library to know who we are
* server = 1
* proxy = 0
*/
static void LbxResetProc ( ExtensionEntry *extEntry );
static void LbxFreeClient ( ClientPtr client );
static void LbxShutdownProxy ( LbxProxyPtr proxy );
static int DecodeLbxDelta ( ClientPtr client );
static LbxProxyPtr proxyList;
unsigned char LbxReqCode;
int LbxEventCode;
static int BadLbxClientCode;
static int uid_seed;
static int lbxCompressWorkProcCount;
LbxClientPtr lbxClients[MAXCLIENTS];
extern xConnSetupPrefix connSetupPrefix;
extern char *ConnectionInfo;
extern int (*LbxInitialVector[3])(ClientPtr);
#ifdef DEBUG
int lbxDebug = 0;
#endif
void
LbxExtensionInit(void)
{
ExtensionEntry *extEntry;
lbxCompressWorkProcCount = 0;
proxyList = NULL;
uid_seed = 0;
if ((extEntry = AddExtension(LBXNAME, LbxNumberEvents, LbxNumberErrors,
ProcLbxDispatch, SProcLbxDispatch,
LbxResetProc, StandardMinorOpcode)))
{
LbxReqCode = (unsigned char)extEntry->base;
LbxEventCode = extEntry->eventBase;
BadLbxClientCode = extEntry->errorBase + BadLbxClient;
LbxDixInit();
LbxCmapInit ();
DeclareExtensionSecurity(LBXNAME, TRUE);
}
}
/*ARGSUSED*/
static void
LbxResetProc (ExtensionEntry *extEntry)
{
LbxResetTags();
uid_seed = 0;
}
void
LbxCloseClient (ClientPtr client)
{
xLbxCloseEvent closeEvent;
ClientPtr master;
LbxProxyPtr proxy;
LbxClientPtr lbxClient = LbxClient(client);
CARD32 id;
if (!lbxClient)
return;
id = lbxClient->id;
proxy = lbxClient->proxy;
DBG (DBG_CLIENT, (stderr, "Close client %d\n", client->index));
LbxFreeClient (client);
if (!id)
{
isItTimeToYield = TRUE;
CloseDownFileDescriptor (client);
LbxShutdownProxy (proxy);
}
else
{
master = NULL;
if (proxy->lbxClients[0])
master = LbxProxyClient(proxy);
if (master && !master->clientGone)
{
closeEvent.type = LbxEventCode;
closeEvent.lbxType = LbxCloseEvent;
closeEvent.client = id;
closeEvent.sequenceNumber = master->sequence;
closeEvent.pad1 = closeEvent.pad2 = closeEvent.pad3 =
closeEvent.pad4 = closeEvent.pad5 = closeEvent.pad6 = 0;
if (master->swapped) {
int n;
swaps(&closeEvent.sequenceNumber, n);
swapl(&closeEvent.client, n);
}
WriteToClient(master, sizeof (closeEvent), (char *)&closeEvent);
LbxForceOutput(proxy);
}
}
}
static int
LbxReencodeEvent(ClientPtr client,
LbxProxyPtr proxy,
char *buf)
{
xEvent *ev = (xEvent *)buf;
int n;
lbxMotionCache *motionCache = &proxy->motionCache;
int motionDelta = 0;
Bool swapCache;
xEvent tev, *sev;
if (ev->u.u.type != MotionNotify) {
if (proxy->dosquishing)
return LbxSquishEvent(buf);
return 0;
}
/*
* Check if we can generate a motion delta event.
*
* The motion cache contains the last motion event the server sent.
*
* The following are always stored in the cache in the server's
* byte order:
* sequenceNumber, time, rootX, rootY, eventX, eventY
* This is because when determining if we can do a delta, all
* arithmetic must be done using the server's byte order.
*
* The following are stored in the byte order of the latest client
* receiving a motion event (indicated by motionCache->swapped):
* root, event, child, state
* These fields do not need to be stored in the server's byte order
* because we only use the '==' operator on them.
*/
if (!proxy->motion_allowed_events) {
DBG(DBG_CLIENT, (stderr, "throttling motion event for client %d\n", client->index));
return sz_xEvent;
}
proxy->motion_allowed_events--;
motionCache = &proxy->motionCache;
if (!client->swapped)
{
swapCache = motionCache->swapped;
sev = ev;
}
else
{
swapCache = !motionCache->swapped;
sev = &tev;
cpswaps (ev->u.keyButtonPointer.rootX,
sev->u.keyButtonPointer.rootX);
cpswaps (ev->u.keyButtonPointer.rootY,
sev->u.keyButtonPointer.rootY);
cpswaps (ev->u.keyButtonPointer.eventX,
sev->u.keyButtonPointer.eventX);
cpswaps (ev->u.keyButtonPointer.eventY,
sev->u.keyButtonPointer.eventY);
cpswaps (ev->u.u.sequenceNumber,
sev->u.u.sequenceNumber);
cpswapl (ev->u.keyButtonPointer.time,
sev->u.keyButtonPointer.time);
}
if (swapCache)
{
swapl (&motionCache->root, n);
swapl (&motionCache->event, n);
swapl (&motionCache->child, n);
swaps (&motionCache->state, n);
motionCache->swapped = !motionCache->swapped;
}
motionDelta = 0;
if (ev->u.u.detail == motionCache->detail &&
ev->u.keyButtonPointer.root == motionCache->root &&
ev->u.keyButtonPointer.event == motionCache->event &&
ev->u.keyButtonPointer.child == motionCache->child &&
ev->u.keyButtonPointer.state == motionCache->state &&
ev->u.keyButtonPointer.sameScreen == motionCache->sameScreen) {
int root_delta_x =
sev->u.keyButtonPointer.rootX - motionCache->rootX;
int root_delta_y =
sev->u.keyButtonPointer.rootY - motionCache->rootY;
int event_delta_x =
sev->u.keyButtonPointer.eventX - motionCache->eventX;
int event_delta_y =
sev->u.keyButtonPointer.eventY - motionCache->eventY;
unsigned long sequence_delta =
sev->u.u.sequenceNumber - motionCache->sequenceNumber;
unsigned long time_delta =
sev->u.keyButtonPointer.time - motionCache->time;
if (root_delta_x == event_delta_x &&
event_delta_x >= -128 && event_delta_x < 128 &&
root_delta_y == event_delta_y &&
event_delta_y >= -128 && event_delta_y < 128) {
if (sequence_delta == 0 && time_delta < 256) {
lbxQuickMotionDeltaEvent *mev =
(lbxQuickMotionDeltaEvent *)(buf + sz_xEvent -
sz_lbxQuickMotionDeltaEvent);
mev->type = LbxEventCode + LbxQuickMotionDeltaEvent;
mev->deltaTime = time_delta;
mev->deltaX = event_delta_x;
mev->deltaY = event_delta_y;
motionDelta = sz_xEvent - sz_lbxQuickMotionDeltaEvent;
} else if (sequence_delta < 65536 && time_delta < 65536) {
lbxMotionDeltaEvent *mev =
(lbxMotionDeltaEvent *)(buf + sz_xEvent -
sz_lbxMotionDeltaEvent);
mev->type = LbxEventCode;
mev->lbxType = LbxMotionDeltaEvent;
mev->deltaTime = time_delta;
mev->deltaSequence = sequence_delta;
mev->deltaX = event_delta_x;
mev->deltaY = event_delta_y;
if (LbxProxyClient(proxy)->swapped)
{
swaps (&mev->deltaTime, n);
swaps (&mev->deltaSequence, n);
}
motionDelta = sz_xEvent - sz_lbxMotionDeltaEvent;
}
}
}
motionCache->sequenceNumber = sev->u.u.sequenceNumber;
motionCache->time = sev->u.keyButtonPointer.time;
motionCache->rootX = sev->u.keyButtonPointer.rootX;
motionCache->rootY = sev->u.keyButtonPointer.rootY;
motionCache->eventX = sev->u.keyButtonPointer.eventX;
motionCache->eventY = sev->u.keyButtonPointer.eventY;
if (motionDelta)
return motionDelta;
ev->u.keyButtonPointer.pad1 = 0;
motionCache->detail = ev->u.u.detail;
motionCache->root = ev->u.keyButtonPointer.root;
motionCache->event = ev->u.keyButtonPointer.event;
motionCache->child = ev->u.keyButtonPointer.child;
motionCache->state = ev->u.keyButtonPointer.state;
motionCache->sameScreen = ev->u.keyButtonPointer.sameScreen;
return 0;
}
static int
LbxComposeDelta(LbxProxyPtr proxy,
char *reply,
int len,
char *buf)
{
int diffs;
int cindex;
int n;
xLbxDeltaReq *p = (xLbxDeltaReq *)buf;
diffs = LBXDeltaMinDiffs(&proxy->outdeltas, (unsigned char *)reply, len,
min(MAXBYTESDIFF, (len - sz_xLbxDeltaReq) >> 1),
&cindex);
if (diffs < 0) {
LBXAddDeltaOut(&proxy->outdeltas, (unsigned char *)reply, len);
return 0;
}
LBXEncodeDelta(&proxy->outdeltas, (unsigned char *)reply, diffs, cindex,
(unsigned char *)(&buf[sz_xLbxDeltaReq]));
LBXAddDeltaOut(&proxy->outdeltas, (unsigned char *)reply, len);
p->reqType = LbxEventCode;
p->lbxReqType = LbxDeltaEvent;
p->diffs = diffs;
p->cindex = cindex;
len = (sz_xLbxDeltaReq + sz_xLbxDiffItem * diffs + 3) & ~3;
p->length = len >> 2;
if (LbxProxyClient(proxy)->swapped) {
swaps(&p->length, n);
}
return len;
}
void
LbxReencodeOutput(ClientPtr client,
char *pbuf,
int *pcount,
char *cbuf,
int *ccount)
{
LbxClientPtr lbxClient = LbxClient(client);
LbxProxyPtr proxy = lbxClient->proxy;
CARD32 len;
int n;
int count = *ccount;
char *obuf = cbuf;
if (client->clientState != ClientStateRunning) {
if (DELTA_CACHEABLE(&proxy->outdeltas, count) &&
(n = LbxComposeDelta(proxy, cbuf, count, proxy->oDeltaBuf))) {
memcpy(obuf, proxy->oDeltaBuf, n);
*ccount -= (count - n);
}
return;
}
if (lbxClient->bytes_remaining) {
if (count < lbxClient->bytes_remaining) {
lbxClient->bytes_remaining -= count;
return;
}
if (DELTA_CACHEABLE(&proxy->outdeltas, lbxClient->bytes_in_reply)) {
len = lbxClient->bytes_in_reply - lbxClient->bytes_remaining;
pbuf += (*pcount - len);
memcpy(proxy->replyBuf, pbuf, len);
memcpy(proxy->replyBuf + len, cbuf, lbxClient->bytes_remaining);
n = LbxComposeDelta(proxy, proxy->replyBuf,
lbxClient->bytes_in_reply, proxy->oDeltaBuf);
if (!n)
obuf += lbxClient->bytes_remaining;
else if (n <= len) {
memcpy(pbuf, proxy->oDeltaBuf, n);
*pcount -= (len - n);
*ccount -= lbxClient->bytes_remaining;
} else {
memcpy(pbuf, proxy->oDeltaBuf, len);
memcpy(obuf, proxy->oDeltaBuf + len, n - len);
*ccount -= lbxClient->bytes_remaining - (n - len);
obuf += n - len;
}
} else
obuf += lbxClient->bytes_remaining;
cbuf += lbxClient->bytes_remaining;
count -= lbxClient->bytes_remaining;
lbxClient->bytes_remaining = 0;
}
while (count) {
lbxClient->bytes_in_reply = sz_xEvent;
if (((xGenericReply *)cbuf)->type == X_Reply) {
len = ((xGenericReply *)cbuf)->length;
if (client->swapped) {
swapl(&len, n);
}
lbxClient->bytes_in_reply += (len << 2);
if (LbxProxyClient(proxy)->swapped != client->swapped) {
swapl(&((xGenericReply *)cbuf)->length, n);
}
if (count < lbxClient->bytes_in_reply) {
lbxClient->bytes_remaining = lbxClient->bytes_in_reply - count;
if (obuf != cbuf)
memmove(obuf, cbuf, count);
return;
}
} else if (((xGenericReply *)cbuf)->type > X_Reply &&
((xGenericReply *)cbuf)->type < LASTEvent &&
(n = LbxReencodeEvent(client, proxy, cbuf))) {
cbuf += n;
*ccount -= n;
count -= n;
if (n == sz_xEvent)
continue;
lbxClient->bytes_in_reply -= n;
}
if (DELTA_CACHEABLE(&proxy->outdeltas, lbxClient->bytes_in_reply) &&
(n = LbxComposeDelta(proxy, cbuf, lbxClient->bytes_in_reply,
proxy->oDeltaBuf))) {
memcpy(obuf, proxy->oDeltaBuf, n);
obuf += n;
*ccount -= (lbxClient->bytes_in_reply - n);
} else {
if (obuf != cbuf)
memmove(obuf, cbuf, lbxClient->bytes_in_reply);
obuf += lbxClient->bytes_in_reply;
}
cbuf += lbxClient->bytes_in_reply;
count -= lbxClient->bytes_in_reply;
}
}
/*ARGSUSED*/
static void
LbxReplyCallback(CallbackListPtr *pcbl,
pointer nulldata,
pointer calldata)
{
ReplyInfoRec *pri = (ReplyInfoRec *)calldata;
ClientPtr client = pri->client;
LbxClientPtr lbxClient;
REQUEST(xReq);
if (!pri->startOfReply || stuff->reqType > 127)
return;
lbxClient = LbxClient(client);
if (lbxClient)
ZeroReplyPadBytes(pri->replyData, stuff->reqType);
}
/*
* XXX If you think this is moronic, you're in good company,
* but things definitely hang if we don't have this.
*/
/* ARGSUSED */
static Bool
LbxCheckCompressInput (ClientPtr dummy1,
pointer dummy2)
{
LbxProxyPtr proxy;
if (!lbxCompressWorkProcCount)
return TRUE;
for (proxy = proxyList; proxy; proxy = proxy->next) {
if (proxy->compHandle &&
proxy->streamOpts.streamCompInputAvail(proxy->fd))
AvailableClientInput (LbxProxyClient(proxy));
}
return FALSE;
}
static Bool
LbxIsClientBlocked (LbxClientPtr lbxClient)
{
LbxProxyPtr proxy = lbxClient->proxy;
return (lbxClient->ignored ||
(GrabInProgress && lbxClient->client->index != GrabInProgress &&
lbxClient != proxy->lbxClients[0]));
}
static void
LbxSwitchRecv (LbxProxyPtr proxy,
LbxClientPtr lbxClient)
{
ClientPtr client;
proxy->curRecv = lbxClient;
if (!lbxClient || lbxClient->client->clientGone)
{
DBG(DBG_CLIENT, (stderr, "switching to dispose input\n"));
lbxClient = proxy->lbxClients[0];
if (!lbxClient)
return;
}
client = lbxClient->client;
DBG (DBG_SWITCH, (stderr, "switching input to client %d\n", client->index));
SwitchClientInput (client, FALSE);
proxy->curDix = lbxClient;
}
/* ARGSUSED */
static Bool
LbxWaitForUnblocked (ClientPtr client,
pointer closure)
{
LbxClientPtr lbxClient;
LbxProxyPtr proxy;
if (client->clientGone)
return TRUE;
lbxClient = LbxClient(client);
if (!lbxClient)
return TRUE;
proxy = lbxClient->proxy;
if (LbxIsClientBlocked (lbxClient) ||
((lbxClient != proxy->curDix) && proxy->curDix->reqs_pending &&
!LbxIsClientBlocked(proxy->curDix)))
return FALSE;
lbxClient->input_blocked = FALSE;
DBG (DBG_BLOCK, (stderr, "client %d no longer blocked, switching\n",
client->index));
SwitchClientInput (client, TRUE);
proxy->curDix = lbxClient;
return TRUE;
}
void
LbxSetForBlock(LbxClientPtr lbxClient)
{
lbxClient->reqs_pending++;
if (!lbxClient->input_blocked)
{
lbxClient->input_blocked = TRUE;
QueueWorkProc(LbxWaitForUnblocked, lbxClient->client, NULL);
}
}
/* ARGSUSED */
static int
LbxWaitForUngrab (ClientPtr client,
pointer closure)
{
LbxClientPtr lbxClient = LbxClient(client);
LbxProxyPtr proxy;
xLbxListenToAllEvent ungrabEvent;
if (client->clientGone || !lbxClient)
return TRUE;
if (GrabInProgress)
return FALSE;
proxy = lbxClient->proxy;
proxy->grabClient = 0;
ungrabEvent.type = LbxEventCode;
ungrabEvent.lbxType = LbxListenToAll;
ungrabEvent.pad1 = ungrabEvent.pad2 = ungrabEvent.pad3 =
ungrabEvent.pad4 = ungrabEvent.pad5 = ungrabEvent.pad6 =
ungrabEvent.pad7 = 0;
WriteToClient (client,
sizeof(xLbxListenToAllEvent), (char *)&ungrabEvent);
LbxForceOutput(proxy);
return TRUE;
}
static void
LbxServerGrab(LbxProxyPtr proxy)
{
LbxClientPtr grabbingLbxClient;
xLbxListenToOneEvent grabEvent;
/*
* If the current grabbing client has changed, then we need
* to send a message to update the proxy.
*/
grabEvent.type = LbxEventCode;
grabEvent.lbxType = LbxListenToOne;
if (!(grabbingLbxClient = lbxClients[GrabInProgress]) ||
grabbingLbxClient->proxy != proxy)
grabEvent.client = 0xffffffff; /* client other than a proxy client */
else
grabEvent.client = grabbingLbxClient->id;
grabEvent.pad1 = grabEvent.pad2 = grabEvent.pad3 =
grabEvent.pad4 = grabEvent.pad5 = grabEvent.pad6 = 0;
if (LbxProxyClient(proxy)->swapped) {
int n;
swapl(&grabEvent.client, n);
}
WriteToClient(LbxProxyClient(proxy),
sizeof(xLbxListenToOneEvent), (char *)&grabEvent);
LbxForceOutput(proxy);
if (!proxy->grabClient)
QueueWorkProc(LbxWaitForUngrab, LbxProxyClient(proxy), NULL);
proxy->grabClient = GrabInProgress;
}
#define MAJOROP(client) ((xReq *)client->requestBuffer)->reqType
#define MINOROP(client) ((xReq *)client->requestBuffer)->data
static Bool lbxCacheable[] = {
FALSE, /* LbxQueryVersion 0 */
FALSE, /* LbxStartProxy 1 */
TRUE, /* LbxStopProxy 2 */
FALSE, /* LbxSwitch 3 */
FALSE, /* LbxNewClient 4 */
TRUE, /* LbxCloseClient 5 */
TRUE, /* LbxModifySequence 6 */
FALSE, /* LbxAllowMotion 7 */
TRUE, /* LbxIncrementPixel 8 */
FALSE, /* LbxDelta 9 */
TRUE, /* LbxGetModifierMapping 10 */
FALSE, /* nothing 11 */
TRUE, /* LbxInvalidateTag 12 */
TRUE, /* LbxPolyPoint 13 */
TRUE, /* LbxPolyLine 14 */
TRUE, /* LbxPolySegment 15 */
TRUE, /* LbxPolyRectangle 16 */
TRUE, /* LbxPolyArc 17 */
TRUE, /* LbxFillPoly 18 */
TRUE, /* LbxPolyFillRectangle 19 */
TRUE, /* LbxPolyFillArc 20 */
TRUE, /* LbxGetKeyboardMapping 21 */
TRUE, /* LbxQueryFont 22 */
TRUE, /* LbxChangeProperty 23 */
TRUE, /* LbxGetProperty 24 */
TRUE, /* LbxTagData 25 */
TRUE, /* LbxCopyArea 26 */
TRUE, /* LbxCopyPlane 27 */
TRUE, /* LbxPolyText8 28 */
TRUE, /* LbxPolyText16 29 */
TRUE, /* LbxImageText8 30 */
TRUE, /* LbxImageText16 31 */
FALSE, /* LbxQueryExtension 32 */
TRUE, /* LbxPutImage 33 */
TRUE, /* LbxGetImage 34 */
FALSE, /* LbxBeginLargeRequest 35 */
FALSE, /* LbxLargeRequestData 36 */
FALSE, /* LbxEndLargeRequest 37 */
FALSE, /* LbxInternAtoms 38 */
TRUE, /* LbxGetWinAttrAndGeom 39 */
TRUE, /* LbxGrabCmap 40 */
TRUE, /* LbxReleaseCmap 41 */
TRUE, /* LbxAllocColor 42 */
TRUE, /* LbxSync 43 */
};
#define NUM(a) (sizeof (a) / sizeof (a[0]))
static int
LbxReadRequestFromClient (ClientPtr client)
{
int ret;
LbxClientPtr lbxClient = LbxClient(client);
LbxProxyPtr proxy = lbxClient->proxy;
ClientPtr masterClient = LbxProxyClient(proxy);
Bool isblocked;
Bool cacheable;
DBG (DBG_READ_REQ, (stderr, "Reading request from client %d\n", client->index));
if (GrabInProgress && (proxy->grabClient != GrabInProgress))
LbxServerGrab(proxy);
isblocked = LbxIsClientBlocked(lbxClient);
if (lbxClient->reqs_pending && !isblocked) {
ret = StandardReadRequestFromClient(client);
if (ret > 0 && (MAJOROP(client) == LbxReqCode) &&
(MINOROP(client) == X_LbxEndLargeRequest))
ret = PrepareLargeReqBuffer(client);
if (!--lbxClient->reqs_pending && (lbxClient != proxy->curRecv))
LbxSwitchRecv (proxy, proxy->curRecv);
return ret;
}
while (1) {
ret = StandardReadRequestFromClient(masterClient);
if (ret <= 0)
return ret;
client->requestBuffer = masterClient->requestBuffer;
client->req_len = masterClient->req_len;
cacheable = client->clientState == ClientStateRunning;
if (cacheable && (MAJOROP(client) == LbxReqCode)) {
/* Check to see if this request is delta cached */
if (MINOROP(client) < NUM(lbxCacheable))
cacheable = lbxCacheable[MINOROP(client)];
switch (MINOROP(client)) {
case X_LbxSwitch:
/* Switch is sent by proxy */
if (masterClient->swapped)
SProcLbxSwitch (client);
else
ProcLbxSwitch (client);
return 0;
case X_LbxDelta:
ret = DecodeLbxDelta (client);
DBG(DBG_DELTA,
(stderr,"delta decompressed msg %d, len = %d\n",
(unsigned)((unsigned char *)client->requestBuffer)[0],
ret));
break;
case X_LbxEndLargeRequest:
if (!isblocked)
ret = PrepareLargeReqBuffer(client);
break;
}
}
if (cacheable && DELTA_CACHEABLE(&proxy->indeltas, ret)) {
DBG(DBG_DELTA,
(stderr, "caching msg %d, len = %d, index = %d\n",
(unsigned)((unsigned char *)client->requestBuffer)[0],
ret, proxy->indeltas.nextDelta));
LBXAddDeltaIn(&proxy->indeltas, client->requestBuffer, ret);
}
if (client->swapped != masterClient->swapped) {
char n;
/* put length in client order */
swaps(&((xReq *)client->requestBuffer)->length, n);
}
if (!isblocked)
return ret;
DBG (DBG_BLOCK, (stderr, "Stashing %d bytes for %d\n",
ret, client->index));
AppendFakeRequest (client, client->requestBuffer, ret);
LbxSetForBlock(lbxClient);
}
}
static LbxClientPtr
LbxInitClient (LbxProxyPtr proxy,
ClientPtr client,
CARD32 id)
{
LbxClientPtr lbxClient;
int i;
lbxClient = (LbxClientPtr) xalloc (sizeof (LbxClientRec));
if (!lbxClient)
return NULL;
lbxClient->id = id;
lbxClient->client = client;
lbxClient->proxy = proxy;
lbxClient->ignored = FALSE;
lbxClient->input_blocked = FALSE;
lbxClient->reqs_pending = 0;
lbxClient->bytes_in_reply = 0;
lbxClient->bytes_remaining = 0;
client->readRequest = LbxReadRequestFromClient;
bzero (lbxClient->drawableCache, sizeof (lbxClient->drawableCache));
bzero (lbxClient->gcontextCache, sizeof (lbxClient->gcontextCache));
lbxClients[client->index] = lbxClient;
for (i = 0; proxy->lbxClients[i]; i++)
;
if (i > proxy->maxIndex)
proxy->maxIndex = i;
proxy->lbxClients[i] = lbxClient;
proxy->numClients++;
lbxClient->gfx_buffer = (pointer) NULL;
lbxClient->gb_size = 0;
return lbxClient;
}
static void
LbxFreeClient (ClientPtr client)
{
LbxClientPtr lbxClient = LbxClient(client);
LbxProxyPtr proxy = lbxClient->proxy;
int i;
if (lbxClient != proxy->lbxClients[0]) {
if (lbxClient == proxy->curRecv)
LbxSwitchRecv(proxy, NULL);
else if (lbxClient == proxy->curDix)
LbxSwitchRecv(proxy, proxy->curRecv);
}
--proxy->numClients;
lbxClients[client->index] = NULL;
for (i = 0; i <= proxy->maxIndex; i++) {
if (proxy->lbxClients[i] == lbxClient) {
proxy->lbxClients[i] = NULL;
break;
}
}
while (proxy->maxIndex >= 0 && !proxy->lbxClients[proxy->maxIndex])
--proxy->maxIndex;
xfree(lbxClient->gfx_buffer);
client->readRequest = StandardReadRequestFromClient;
xfree (lbxClient);
}
static void
LbxFreeProxy (LbxProxyPtr proxy)
{
LbxProxyPtr *p;
LBXFreeDeltaCache(&proxy->indeltas);
LBXFreeDeltaCache(&proxy->outdeltas);
LbxFreeOsBuffers(proxy);
if (proxy->iDeltaBuf)
xfree(proxy->iDeltaBuf);
if (proxy->replyBuf)
xfree(proxy->replyBuf);
if (proxy->oDeltaBuf)
xfree(proxy->oDeltaBuf);
if (proxy->compHandle)
proxy->streamOpts.streamCompFreeHandle(proxy->compHandle);
if (proxy->bitmapCompMethods)
xfree (proxy->bitmapCompMethods);
if (proxy->pixmapCompMethods)
xfree (proxy->pixmapCompMethods);
if (proxy->pixmapCompDepths)
{
int i;
for (i = 0; i < proxy->numPixmapCompMethods; i++)
xfree (proxy->pixmapCompDepths[i]);
xfree (proxy->pixmapCompDepths);
}
for (p = &proxyList; *p; p = &(*p)->next) {
if (*p == proxy) {
*p = proxy->next;
break;
}
}
if (!proxyList)
DeleteCallback(&ReplyCallback, LbxReplyCallback, NULL);
xfree (proxy);
}
LbxProxyPtr
LbxPidToProxy(int pid)
{
LbxProxyPtr proxy;
for (proxy = proxyList; proxy; proxy = proxy->next) {
if (proxy->pid == pid)
return proxy;
}
return NULL;
}
static void
LbxShutdownProxy (LbxProxyPtr proxy)
{
int i;
ClientPtr client;
if (proxy->compHandle)
--lbxCompressWorkProcCount;
while (proxy->grabbedCmaps)
LbxReleaseCmap(proxy->grabbedCmaps, FALSE);
for (i = 0; i <= proxy->maxIndex; i++)
{
if (proxy->lbxClients[i])
{
client = proxy->lbxClients[i]->client;
if (!client->clientGone)
CloseDownClient (client);
}
}
LbxFlushTags(proxy);
LbxFreeProxy(proxy);
}
int
ProcLbxQueryVersion (ClientPtr client)
{
/* REQUEST(xLbxQueryVersionReq); */
xLbxQueryVersionReply rep;
register int n;
REQUEST_SIZE_MATCH(xLbxQueryVersionReq);
rep.type = X_Reply;
rep.length = 0;
rep.sequenceNumber = client->sequence;
rep.majorVersion = LBX_MAJOR_VERSION;
rep.minorVersion = LBX_MINOR_VERSION;
rep.pad0 = rep.pad1 = rep.pad2 = rep.pad3 = rep.pad4 = 0;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
swaps(&rep.majorVersion, n);
swaps(&rep.minorVersion, n);
}
WriteToClient(client, sizeof(xLbxQueryVersionReply), (char *)&rep);
return (client->noClientException);
}
static int
NextProxyID (void)
{
LbxProxyPtr proxy;
int id;
for (id = 1; id < MAX_NUM_PROXIES; id++) {
for (proxy = proxyList; proxy && proxy->pid != id; proxy = proxy->next)
;
if (!proxy)
return id;
}
return -1;
}
int
ProcLbxStartProxy (ClientPtr client)
{
REQUEST(xLbxStartProxyReq);
LbxProxyPtr proxy;
LbxClientPtr lbxClient;
int reqlen;
int replylen;
xLbxStartReply *replybuf;
LbxNegOptsRec negopt;
register int n;
pointer compHandle = NULL;
REQUEST_AT_LEAST_SIZE(xLbxStartProxyReq);
if (lbxClients[client->index])
return BadLbxClientCode;
proxy = (LbxProxyPtr) xalloc (sizeof (LbxProxyRec));
if (!proxy)
return BadAlloc;
bzero(proxy, sizeof (LbxProxyRec));
proxy->pid = NextProxyID();
if (proxy->pid < 0) { /* too many proxies */
xfree(proxy);
return BadAlloc;
}
proxy->uid = ++uid_seed;
if (!proxyList)
AddCallback(&ReplyCallback, LbxReplyCallback, NULL);
if(!proxyList)
proxyList = proxy;
else{
proxy->next = proxyList;
proxyList = proxy;
}
/*
* Don't know exactly how big the reply will be, but it won't be
* bigger than the request
*/
reqlen = client->req_len << 2;
replybuf = (xLbxStartReply *) xalloc(max(reqlen, sz_xLbxStartReply));
if (!replybuf) {
LbxFreeProxy(proxy);
return BadAlloc;
}
LbxOptionInit(&negopt);
replylen = LbxOptionParse(&negopt,
(unsigned char *)&stuff[1],
reqlen - sz_xLbxStartProxyReq,
(unsigned char *)&replybuf->optDataStart);
if (replylen < 0) {
/*
* Didn't understand option format, so we'll just end up
* using the defaults. Set nopts so that the proxy will
* be informed that we rejected the options because of
* decoding problems.
*/
LbxOptionInit(&negopt);
negopt.nopts = 0xff;
replylen = 0;
}
if (LBXInitDeltaCache(&proxy->indeltas, negopt.proxyDeltaN,
negopt.proxyDeltaMaxLen) < 0
||
LBXInitDeltaCache(&proxy->outdeltas, negopt.serverDeltaN,
negopt.serverDeltaMaxLen) < 0) {
LbxFreeProxy(proxy);
xfree(replybuf);
return BadAlloc;
}
n = 0;
if (negopt.proxyDeltaN)
n = negopt.proxyDeltaMaxLen;
if (negopt.serverDeltaN && negopt.serverDeltaMaxLen > n)
n = negopt.serverDeltaMaxLen;
if (n &&
(!(proxy->iDeltaBuf = (char *)xalloc (n)) ||
!(proxy->replyBuf = (char *)xalloc (n)) ||
!(proxy->oDeltaBuf = (char *)xalloc (n)))) {
LbxFreeProxy(proxy);
xfree(replybuf);
return BadAlloc;
}
MakeClientGrabImpervious(client); /* proxy needs to be grab-proof */
proxy->fd = ClientConnectionNumber(client);
if (negopt.streamOpts.streamCompInit) {
compHandle =
(*negopt.streamOpts.streamCompInit)(proxy->fd, negopt.streamOpts.streamCompArg);
if (!compHandle) {
LbxFreeProxy(proxy);
xfree(replybuf);
return BadAlloc;
}
}
proxy->ofirst = NULL;
proxy->olast = NULL;
if (!LbxInitClient (proxy, client, 0))
{
LbxFreeProxy(proxy);
xfree(replybuf);
return BadAlloc;
}
proxy->dosquishing = negopt.squish;
proxy->numBitmapCompMethods = negopt.numBitmapCompMethods;
proxy->bitmapCompMethods = negopt.bitmapCompMethods;
proxy->numPixmapCompMethods = negopt.numPixmapCompMethods;
proxy->pixmapCompMethods = negopt.pixmapCompMethods;
proxy->pixmapCompDepths = negopt.pixmapCompDepths;
proxy->streamOpts = negopt.streamOpts;
proxy->useTags = negopt.useTags;
proxy->grabbedCmaps = NULL;
/* send reply */
replybuf->type = X_Reply;
replybuf->nOpts = negopt.nopts;
replybuf->sequenceNumber = client->sequence;
replylen += sz_xLbxStartReplyHdr;
if (replylen < sz_xLbxStartReply)
replylen = sz_xLbxStartReply;
replybuf->length = (replylen - sz_xLbxStartReply + 3) >> 2;
if (client->swapped) {
swaps(&replybuf->sequenceNumber, n);
swapl(&replybuf->length, n);
}
lbxClient = LbxClient(client);
WriteToClient(client, replylen, (char *)replybuf);
LbxProxyConnection(client, proxy);
lbxClient = proxy->lbxClients[0];
proxy->curDix = lbxClient;
proxy->curRecv = lbxClient;
proxy->compHandle = compHandle;
if (proxy->compHandle && !lbxCompressWorkProcCount++)
QueueWorkProc(LbxCheckCompressInput, NULL, NULL);
xfree(replybuf);
return Success;
}
int
ProcLbxStopProxy(ClientPtr client)
{
/* REQUEST(xLbxStopProxyReq); */
LbxProxyPtr proxy;
LbxClientPtr lbxClient = LbxClient(client);
REQUEST_SIZE_MATCH(xLbxStopProxyReq);
if (!lbxClient)
return BadLbxClientCode;
if (lbxClient->id)
return BadLbxClientCode;
proxy = lbxClient->proxy;
LbxFreeClient (client);
LbxShutdownProxy (proxy);
return Success;
}
int
ProcLbxSwitch(ClientPtr client)
{
REQUEST(xLbxSwitchReq);
LbxProxyPtr proxy = LbxMaybeProxy(client);
LbxClientPtr lbxClient;
int i;
REQUEST_SIZE_MATCH(xLbxSwitchReq);
if (!proxy)
return BadLbxClientCode;
for (i = 0; i <= proxy->maxIndex; i++) {
lbxClient = proxy->lbxClients[i];
if (lbxClient && lbxClient->id == stuff->client) {
LbxSwitchRecv (proxy, lbxClient);
return Success;
}
}
LbxSwitchRecv (proxy, NULL);
return BadLbxClientCode;
}
int
ProcLbxBeginLargeRequest(ClientPtr client)
{
REQUEST(xLbxBeginLargeRequestReq);
client->sequence--;
REQUEST_SIZE_MATCH(xLbxBeginLargeRequestReq);
if (!AllocateLargeReqBuffer(client, stuff->largeReqLength << 2))
return BadAlloc;
return Success;
}
int
ProcLbxLargeRequestData(ClientPtr client)
{
REQUEST(xLbxLargeRequestDataReq);
client->sequence--;
REQUEST_AT_LEAST_SIZE(xLbxLargeRequestDataReq);
if (!AddToLargeReqBuffer(client, (char *) (stuff + 1),
(client->req_len - 1) << 2))
return BadAlloc;
return Success;
}
int
ProcLbxEndLargeRequest(ClientPtr client)
{
/* REQUEST(xReq); */
client->sequence--;
REQUEST_SIZE_MATCH(xReq);
return BadAlloc;
}
int
ProcLbxInternAtoms(ClientPtr client)
{
REQUEST(xLbxInternAtomsReq);
LbxClientPtr lbxClient = LbxClient(client);
xLbxInternAtomsReply *replyRet;
char *ptr = (char *) stuff + sz_xLbxInternAtomsReq;
Atom *atomsRet;
int replyLen, i;
char lenbuf[2];
CARD16 len;
char n;
REQUEST_AT_LEAST_SIZE(xLbxInternAtomsReq);
if (!lbxClient)
return BadLbxClientCode;
if (lbxClient->id)
return BadLbxClientCode;
replyLen = sz_xLbxInternAtomsReplyHdr + stuff->num * sizeof (Atom);
if (replyLen < sz_xLbxInternAtomsReply)
replyLen = sz_xLbxInternAtomsReply;
if (!(replyRet = (xLbxInternAtomsReply *) xalloc (replyLen)))
return BadAlloc;
atomsRet = (Atom *) ((char *) replyRet + sz_xLbxInternAtomsReplyHdr);
for (i = 0; i < stuff->num; i++)
{
lenbuf[0] = ptr[0];
lenbuf[1] = ptr[1];
len = *((CARD16 *) lenbuf);
ptr += 2;
if ((atomsRet[i] = MakeAtom (ptr, len, TRUE)) == BAD_RESOURCE)
{
xfree (replyRet);
return BadAlloc;
}
ptr += len;
}
if (client->swapped)
for (i = 0; i < stuff->num; i++)
swapl (&atomsRet[i], n);
replyRet->type = X_Reply;
replyRet->sequenceNumber = client->sequence;
replyRet->length = (replyLen - sz_xLbxInternAtomsReply + 3) >> 2;
if (client->swapped) {
swaps(&replyRet->sequenceNumber, n);
swapl(&replyRet->length, n);
}
WriteToClient (client, replyLen, (char *) replyRet);
xfree (replyRet);
return Success;
}
int
ProcLbxGetWinAttrAndGeom(ClientPtr client)
{
REQUEST(xLbxGetWinAttrAndGeomReq);
xGetWindowAttributesReply wa;
xGetGeometryReply wg;
xLbxGetWinAttrAndGeomReply reply;
WindowPtr pWin;
int status;
REQUEST_SIZE_MATCH(xLbxGetWinAttrAndGeomReq);
pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
SecurityReadAccess);
if (!pWin)
return(BadWindow);
GetWindowAttributes(pWin, client, &wa);
if ((status = GetGeometry(client, &wg)) != Success)
return status;
reply.type = X_Reply;
reply.length = (sz_xLbxGetWinAttrAndGeomReply - 32) >> 2;
reply.sequenceNumber = client->sequence;
reply.backingStore = wa.backingStore;
reply.visualID = wa.visualID;
#if defined(__cplusplus) || defined(c_plusplus)
reply.c_class = wa.c_class;
#else
reply.class = wa.class;
#endif
reply.bitGravity = wa.bitGravity;
reply.winGravity = wa.winGravity;
reply.backingBitPlanes = wa.backingBitPlanes;
reply.backingPixel = wa.backingPixel;
reply.saveUnder = wa.saveUnder;
reply.mapInstalled = wa.mapInstalled;
reply.mapState = wa.mapState;
reply.override = wa.override;
reply.colormap = wa.colormap;
reply.allEventMasks = wa.allEventMasks;
reply.yourEventMask = wa.yourEventMask;
reply.doNotPropagateMask = wa.doNotPropagateMask;
reply.pad1 = 0;
reply.root = wg.root;
reply.x = wg.x;
reply.y = wg.y;
reply.width = wg.width;
reply.height = wg.height;
reply.borderWidth = wg.borderWidth;
reply.depth = wg.depth;
reply.pad2 = 0;
if (client->swapped)
{
register char n;
swaps(&reply.sequenceNumber, n);
swapl(&reply.length, n);
swapl(&reply.visualID, n);
swaps(&reply.class, n);
swapl(&reply.backingBitPlanes, n);
swapl(&reply.backingPixel, n);
swapl(&reply.colormap, n);
swapl(&reply.allEventMasks, n);
swapl(&reply.yourEventMask, n);
swaps(&reply.doNotPropagateMask, n);
swapl(&reply.root, n);
swaps(&reply.x, n);
swaps(&reply.y, n);
swaps(&reply.width, n);
swaps(&reply.height, n);
swaps(&reply.borderWidth, n);
}
WriteToClient(client, sizeof(xLbxGetWinAttrAndGeomReply), (char *)&reply);
return(client->noClientException);
}
int
ProcLbxNewClient(ClientPtr client)
{
REQUEST(xLbxNewClientReq);
ClientPtr newClient;
LbxProxyPtr proxy = LbxMaybeProxy(client);
CARD32 id;
int len, i;
char *setupbuf;
LbxClientPtr lbxClient;
REQUEST_AT_LEAST_SIZE(xLbxNewClientReq);
/* save info before our request disappears */
id = stuff->client;
if (!proxy || !id)
return BadLbxClientCode;
if (proxy->numClients == MAX_LBX_CLIENTS)
return BadAlloc;
for (i = 1; i <= proxy->maxIndex; i++) {
if (proxy->lbxClients[i] && proxy->lbxClients[i]->id == id)
return BadLbxClientCode;
}
len = (client->req_len << 2) - sizeof(xLbxNewClientReq);
setupbuf = (char *)xalloc (len);
if (!setupbuf)
return BadAlloc;
memcpy (setupbuf, (char *)&stuff[1], len);
newClient = AllocLbxClientConnection (client, proxy);
if (!newClient)
return BadAlloc;
newClient->requestVector = LbxInitialVector;
lbxClient = LbxInitClient (proxy, newClient, id);
if (!lbxClient)
{
CloseDownClient (newClient);
return BadAlloc;
}
AppendFakeRequest (newClient, setupbuf, len);
xfree (setupbuf);
LbxSetForBlock(lbxClient);
DBG (DBG_CLIENT, (stderr, "lbxNewClient X %d\n", newClient->index));
return Success;
}
int
ProcLbxEstablishConnection(ClientPtr client)
{
char *reason = NULL;
char *auth_proto, *auth_string;
register xConnClientPrefix *prefix;
REQUEST(xReq);
prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
auth_proto = (char *)prefix + sz_xConnClientPrefix;
auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
if ((prefix->majorVersion != X_PROTOCOL) ||
(prefix->minorVersion != X_PROTOCOL_REVISION))
reason = "Protocol version mismatch";
else
reason = ClientAuthorized(client,
prefix->nbytesAuthProto,
auth_proto,
prefix->nbytesAuthString,
auth_string);
if (client->clientState == ClientStateCheckingSecurity ||
client->clientState == ClientStateAuthenticating)
return (client->noClientException = -1); /* XXX some day */
return(LbxSendConnSetup(client, reason));
}
int
ProcLbxCloseClient (ClientPtr client)
{
REQUEST(xLbxCloseClientReq);
LbxClientPtr lbxClient = LbxClient(client);
REQUEST_SIZE_MATCH(xLbxCloseClientReq);
if (!lbxClient || lbxClient->id != stuff->client)
return BadLbxClientCode;
/* this will cause the client to be closed down back in Dispatch() */
return(client->noClientException = CloseLbxClient);
}
int
ProcLbxModifySequence (ClientPtr client)
{
REQUEST(xLbxModifySequenceReq);
REQUEST_SIZE_MATCH(xLbxModifySequenceReq);
client->sequence += (stuff->adjust - 1); /* Dispatch() adds 1 */
return Success;
}
int
ProcLbxAllowMotion (ClientPtr client)
{
REQUEST(xLbxAllowMotionReq);
client->sequence--;
REQUEST_SIZE_MATCH(xLbxAllowMotionReq);
LbxAllowMotion(client, stuff->num);
return Success;
}
static int
DecodeLbxDelta (ClientPtr client)
{
REQUEST(xLbxDeltaReq);
LbxClientPtr lbxClient = LbxClient(client);
LbxProxyPtr proxy = lbxClient->proxy;
int len;
unsigned char *buf;
/* Note that LBXDecodeDelta decodes and adds current msg to the cache */
len = LBXDecodeDelta(&proxy->indeltas,
(xLbxDiffItem *)(((char *)stuff) + sz_xLbxDeltaReq),
stuff->diffs, stuff->cindex, &buf);
/*
* Some requests, such as FillPoly, result in the protocol input
* buffer being modified. So we need to copy the request
* into a temporary buffer where a write would be harmless.
* Maybe some day do this copying on a case by case basis,
* since not all requests are guilty of this.
*/
memcpy(proxy->iDeltaBuf, buf, len);
client->requestBuffer = proxy->iDeltaBuf;
client->req_len = len >> 2;
return len;
}
int
ProcLbxGetModifierMapping(ClientPtr client)
{
/* REQUEST(xLbxGetModifierMappingReq); */
REQUEST_SIZE_MATCH(xLbxGetModifierMappingReq);
return LbxGetModifierMapping(client);
}
int
ProcLbxGetKeyboardMapping(ClientPtr client)
{
/* REQUEST(xLbxGetKeyboardMappingReq); */
REQUEST_SIZE_MATCH(xLbxGetKeyboardMappingReq);
return LbxGetKeyboardMapping(client);
}
int
ProcLbxQueryFont(ClientPtr client)
{
/* REQUEST(xLbxQueryFontReq); */
REQUEST_SIZE_MATCH(xLbxQueryFontReq);
return LbxQueryFont(client);
}
int
ProcLbxChangeProperty(ClientPtr client)
{
/* REQUEST(xLbxChangePropertyReq); */
REQUEST_SIZE_MATCH(xLbxChangePropertyReq);
return LbxChangeProperty(client);
}
int
ProcLbxGetProperty(ClientPtr client)
{
/* REQUEST(xLbxGetPropertyReq); */
REQUEST_SIZE_MATCH(xLbxGetPropertyReq);
return LbxGetProperty(client);
}
int
ProcLbxTagData(ClientPtr client)
{
REQUEST(xLbxTagDataReq);
client->sequence--; /* not a counted request */
REQUEST_AT_LEAST_SIZE(xLbxTagDataReq);
return LbxTagData(client, stuff->tag, stuff->real_length,
(pointer)&stuff[1]); /* better not give any errors */
}
int
ProcLbxInvalidateTag(ClientPtr client)
{
REQUEST(xLbxInvalidateTagReq);
client->sequence--;
REQUEST_SIZE_MATCH(xLbxInvalidateTagReq);
return LbxInvalidateTag(client, stuff->tag);
}
int
ProcLbxPolyPoint(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyPoint, LbxDecodePoints);
}
int
ProcLbxPolyLine(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyLine, LbxDecodePoints);
}
int
ProcLbxPolySegment(ClientPtr client)
{
return LbxDecodePoly(client, X_PolySegment, LbxDecodeSegment);
}
int
ProcLbxPolyRectangle(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyRectangle, LbxDecodeRectangle);
}
int
ProcLbxPolyArc(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyArc, LbxDecodeArc);
}
int
ProcLbxFillPoly(ClientPtr client)
{
return LbxDecodeFillPoly(client);
}
int
ProcLbxPolyFillRectangle(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyFillRectangle, LbxDecodeRectangle);
}
int
ProcLbxPolyFillArc(ClientPtr client)
{
return LbxDecodePoly(client, X_PolyFillArc, LbxDecodeArc);
}
int
ProcLbxCopyArea(ClientPtr client)
{
return LbxDecodeCopyArea(client);
}
int
ProcLbxCopyPlane(ClientPtr client)
{
return LbxDecodeCopyPlane(client);
}
int
ProcLbxPolyText(ClientPtr client)
{
return LbxDecodePolyText(client);
}
int
ProcLbxImageText(ClientPtr client)
{
return LbxDecodeImageText(client);
}
int
ProcLbxQueryExtension(ClientPtr client)
{
REQUEST(xLbxQueryExtensionReq);
char *ename;
REQUEST_AT_LEAST_SIZE(xLbxQueryExtensionReq);
ename = (char *) &stuff[1];
return LbxQueryExtension(client, ename, stuff->nbytes);
}
int
ProcLbxPutImage(ClientPtr client)
{
return LbxDecodePutImage(client);
}
int
ProcLbxGetImage(ClientPtr client)
{
return LbxDecodeGetImage(client);
}
int
ProcLbxSync(ClientPtr client)
{
xLbxSyncReply reply;
client->sequence--; /* not a counted request */
#ifdef COLOR_DEBUG
fprintf (stderr, "Got LBX sync, seq = 0x%x\n", client->sequence);
#endif
reply.type = X_Reply;
reply.length = 0;
reply.sequenceNumber = client->sequence;
reply.pad0 = reply.pad1 = reply.pad2 = reply.pad3 = reply.pad4 =
reply.pad5 = reply.pad6 = 0;
if (client->swapped)
{
register char n;
swaps (&reply.sequenceNumber, n);
}
WriteToClient (client, sz_xLbxSyncReply, (char *)&reply);
return (client->noClientException);
}
int
ProcLbxDispatch(ClientPtr client)
{
REQUEST(xReq);
switch (stuff->data)
{
case X_LbxQueryVersion:
return ProcLbxQueryVersion(client);
case X_LbxStartProxy:
return ProcLbxStartProxy(client);
case X_LbxStopProxy:
return ProcLbxStopProxy(client);
case X_LbxNewClient:
return ProcLbxNewClient(client);
case X_LbxCloseClient:
return ProcLbxCloseClient(client);
case X_LbxModifySequence:
return ProcLbxModifySequence(client);
case X_LbxAllowMotion:
return ProcLbxAllowMotion(client);
case X_LbxIncrementPixel:
return ProcLbxIncrementPixel(client);
case X_LbxGrabCmap:
return ProcLbxGrabCmap(client);
case X_LbxReleaseCmap:
return ProcLbxReleaseCmap(client);
case X_LbxAllocColor:
return ProcLbxAllocColor(client);
case X_LbxGetModifierMapping:
return ProcLbxGetModifierMapping(client);
case X_LbxGetKeyboardMapping:
return ProcLbxGetKeyboardMapping(client);
case X_LbxInvalidateTag:
return ProcLbxInvalidateTag(client);
case X_LbxPolyPoint:
return ProcLbxPolyPoint (client);
case X_LbxPolyLine:
return ProcLbxPolyLine (client);
case X_LbxPolySegment:
return ProcLbxPolySegment (client);
case X_LbxPolyRectangle:
return ProcLbxPolyRectangle (client);
case X_LbxPolyArc:
return ProcLbxPolyArc (client);
case X_LbxFillPoly:
return ProcLbxFillPoly (client);
case X_LbxPolyFillRectangle:
return ProcLbxPolyFillRectangle (client);
case X_LbxPolyFillArc:
return ProcLbxPolyFillArc (client);
case X_LbxQueryFont:
return ProcLbxQueryFont (client);
case X_LbxChangeProperty:
return ProcLbxChangeProperty (client);
case X_LbxGetProperty:
return ProcLbxGetProperty (client);
case X_LbxTagData:
return ProcLbxTagData (client);
case X_LbxCopyArea:
return ProcLbxCopyArea (client);
case X_LbxCopyPlane:
return ProcLbxCopyPlane (client);
case X_LbxPolyText8:
case X_LbxPolyText16:
return ProcLbxPolyText (client);
case X_LbxImageText8:
case X_LbxImageText16:
return ProcLbxImageText (client);
case X_LbxQueryExtension:
return ProcLbxQueryExtension (client);
case X_LbxPutImage:
return ProcLbxPutImage (client);
case X_LbxGetImage:
return ProcLbxGetImage (client);
case X_LbxInternAtoms:
return ProcLbxInternAtoms(client);
case X_LbxGetWinAttrAndGeom:
return ProcLbxGetWinAttrAndGeom(client);
case X_LbxSync:
return ProcLbxSync(client);
case X_LbxBeginLargeRequest:
return ProcLbxBeginLargeRequest(client);
case X_LbxLargeRequestData:
return ProcLbxLargeRequestData(client);
case X_LbxEndLargeRequest:
return ProcLbxLargeRequestData(client);
default:
return BadRequest;
}
}
|