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
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <sys/times.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_CDL /* If we have the CDL we can build a proxy server. */
#include "cdl.h"
#endif
/*
* VXIMTOOL.C -- Virtual image display server. This is a server process much
* like XIMTOOL, except that all the process does is respond to datastream
* requests to read and write to internal frame buffers maintained as arrays
* in memory. Multiple frame buffers and frame buffer configurations are
* supported. A log is kept to the stderr of all datastream requests. The
* process is terminated with an EOF on the stdin.
*
*
* To build: cc vximtool.c -o vximtool
* cc vximtool.c -o vximtool -lsocket # For Solaris systems
* cc -DANSI_FUNC vximtool.c # Use ANSI function prototypes
*
* Usage: vximtool -verbose >& spool # run server, log output
* vximtool -i # run interactively
* vximtool -noraster # don't store images in memory
* vximtool -i < cursor_file # take cursor input from file
*
* Options
*
* vximtool [-background] [-config <num>] [-fifo <pipe>] [-fifo_only] [-help]
* [-i] [-imtoolrc <file>] [-inet_only | -port_only] [-noraster]
* [-nframes <num>] [-port <num>] [-proxy] [-verbose] [-unix <name>]
* [-unix_only]
*
*
* Minimal match for command line options may be used. In interactive mode,
* cursor value strings may be typed in on the server stdin in response to
* cursor read requests from the client. Otherwise, a constant cursor value
* "1.0 1.0 101 q" is returned.
*/
/* Default values, size limiting values.
*/
#define MAX_FBCONFIG 128 /* max possible frame buf sizes */
#ifndef HAVE_CDL
#define MAX_FRAMES 16 /* max number of frames */
#endif
#define MAX_CLIENTS 8 /* max display server clients */
#define DEF_NFRAMES 1 /* save memory; only one frame */
#define DEF_FRAME_WIDTH 512 /* 512 square frame */
#define DEF_FRAME_HEIGHT 512 /* 512 square frame */
#define SZ_LABEL 256 /* main frame label string */
#define SZ_IMTITLE 128 /* image title string */
#define SZ_WCSBUF 320 /* WCS text buffer size */
#define SZ_FIFOBUF 4000 /* transfer size for FIFO i/o */
#define SZ_FNAME 256
#define SZ_LINE 256
/* Magic numbers. */
#define DEF_PORT 5137 /* default tcp/ip socket */
#define DEF_PROXY_PORT 5136 /* default proxy socket */
#define I_DEVNAME "/dev/imt1o" /* pseudo device names */
#define O_DEVNAME "/dev/imt1i" /* our IN is client's OUT */
#define DEF_UNIXADDR "/tmp/.IMT%d" /* default unix socket */
#define FBCONFIG_1 ".imtoolrc"
#define FBCONFIG_2 "/usr/local/lib/imtoolrc"
#define FBCONFIG_ENV1 "imtoolrc"
#define FBCONFIG_ENV2 "IMTOOLRC"
/* IIS definitions. */
#define IO_TIMEOUT 30
#define MAXCONN 5
#define SZ_IOBUF 65536 /* max size data transfer */
#define SZ_FIFOBUF 4000
#define SZ_WCSBUF 320 /* WCS text buffer size */
#define SZ_FNAME 256
#define SZ_IMCURVAL 160
#define MEMORY 01 /* frame buffer i/o */
#define LUT 02 /* lut i/o */
#define FEEDBACK 05 /* used for frame clears */
#define IMCURSOR 020 /* logical image cursor */
#define WCS 021 /* used to set WCS */
#define PACKED 0040000
#define COMMAND 0100000
#define IIS_READ 0100000
#define IMC_SAMPLE 0040000
#define IMT_FBCONFIG 077
#define XYMASK 077777
struct iism70 { /* DO NOT change the order of */
short tid; /* this structure. */
short thingct;
short subunit;
short checksum;
short x, y, z;
short t;
};
/* IIS data pixel values. */
#define CMS_DATASTART 1
#define CMS_DATAEND 200
#define CMS_DATARANGE 200
/* WCS definitions. */
#define W_UNITARY 0
#define W_LINEAR 1
#define W_LOG 2
#define W_DEFFORMAT " %7.2f %7.2f %7.1f%c"
/* Rotation matrix defining world coordinate system (WCS) of a frame. */
typedef struct {
int valid; /* has WCS been set? */
float a, b; /* x, y scale factors */
float c, d; /* x, y cross factors */
float tx, ty; /* x, y translation */
float z1, z2; /* greyscale range */
int zt; /* greyscale mapping */
char format[32]; /* wcs output format */
char imtitle[SZ_IMTITLE+1]; /* image title from WCS */
} Ctran, *CtranPtr;
/* The frame buffers. */
typedef struct {
int frameno; /* frame number */
char *framebuf; /* frame buffer raster */
char label[SZ_LABEL+1]; /* frame label string */
Ctran ctran; /* world coordinate system */
char wcsbuf[SZ_WCSBUF]; /* wcs info string */
} FrameBuf, *FrameBufPtr;
/* Possible frame buffer sizes. */
typedef struct {
int nframes; /* number of frames */
int width; /* frame buffer width */
int height; /* frame buffer height */
} FbConfig, *FbConfigPtr;
/* Client I/O channel. */
typedef struct {
void *vxim; /* backpointer to vxim descriptor */
int type; /* channel type */
int listen_fd; /* socket server fd */
int datain; /* input channel */
int dataout; /* output channel */
int keepalive; /* used to keep input fifo ready */
int connected; /* channel is connected to client */
int port; /* inet port number */
char path[SZ_FNAME+1]; /* for unix sockets */
int reference_frame; /* reference (cmd i/o) frame */
FrameBufPtr rf_p; /* reference frame descriptor */
} IoChan, *IoChanPtr;
#define IO_FIFO 1
#define IO_INET 2
#define IO_UNIX 3
/* Application runtime descriptor.
* --------------------------------
*/
typedef struct {
int def_config; /* default FB config */
int def_nframes; /* default number of frames */
char *imtoolrc; /* imtoolrc file name */
char *input_fifo; /* client's output */
char *output_fifo; /* client's input */
char *unixaddr; /* format for unix socket path */
int port; /* port for INET socket */
/* Internal state. */
int display_frame; /* currently displayed frame */
int fb_configno; /* current config number */
int nframes; /* current number of frame bufs */
int width, height; /* current width, height */
IoChan chan[MAX_CLIENTS]; /* client i/o descriptors */
FrameBufPtr df_p; /* display frame descriptor */
FrameBuf frames[MAX_FRAMES]; /* array of frame descriptors */
FbConfig fb_config[MAX_FBCONFIG]; /* fb config table */
} VXimData, *VXimDataPtr;
/* Initialize the structure with out starting values. These can be reset
* with command line options.
*/
VXimData server_data = {
1, /* def_config */
DEF_NFRAMES, /* def_nframes */
FBCONFIG_2, /* def_imtoolrc */
O_DEVNAME, /* input_fifo */
I_DEVNAME, /* output_fifo */
DEF_UNIXADDR, /* unixaddr */
DEF_PORT, /* port */
1, /* display_frame */
1, /* fb_configno */
2, /* nframes */
512, 512 /* width, height */
};
/* Functions.
*/
#ifndef abs
#define abs(a) (((a)<0)?(-(a)):(a))
#endif
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)<(b)?(b):(a))
#endif
#ifdef SOLARIS
#define bzero(a,n) memset(a,0,n)
#define bcopy(a,b,n) memmove(b,a,n)
#endif
#define SELWIDTH 32
#ifdef HAVE_CDL
static int proxy = 0;
static int nclients = 0;
CDLPtr cdl[MAX_CLIENTS];
#endif
static int keep_raster = 1;
extern int errno;
static int background = 0;
static int verbose = 0;
static int interactive = 0;
static float cursor_x = 1.0, cursor_y = 1.0;
static fd_set fds, allset;
#ifdef ANSI_FUNC
int main(int argc, char **argv);
static int vx_iisopen(register VXimDataPtr vxim);
static void vx_iisclose(register VXimDataPtr vxim);
static IoChanPtr open_fifo(register VXimDataPtr vxim);
static IoChanPtr open_inet(register VXimDataPtr vxim);
static IoChanPtr open_unix(register VXimDataPtr vxim);
static void vx_connectClient(IoChanPtr chan, int *source);
static void vx_disconnectClient(register IoChanPtr chan);
static IoChanPtr get_iochan(register VXimDataPtr vxim);
static void vx_iisio(IoChanPtr chan, int *fd_addr, int source);
static void set_fbconfig(IoChanPtr chan, int config, int frame);
static int decode_frameno(register int z);
static void bswap2(char *a, char *b, int nbytes);
static void vx_retCursorVal(register int dataout, float sx, float sy,
int wcs, int key, char *strval);
static CtranPtr wcs_update(register VXimDataPtr vxim, FrameBufPtr fr);
static void vx_initialize(register VXimDataPtr vxim, int config,
int nframes, int reset);
static void vx_initFrame(register VXimDataPtr vxim, int frame,
int nframes, FbConfigPtr config);
static void vx_eraseFrame(register VXimDataPtr vxim, int frame);
static void get_fbconfig(register VXimDataPtr vxim);
static void Usage(void);
static void printoption(char *st);
#ifdef HAVE_CDL
static void vx_flip(char *buffer, int nx, int ny);
#endif
static int iis_read (int fd, void *vptr, int nbytes);
static int iis_write (int fd, void *vptr, int nbytes);
#else
static void vx_iisclose(), vx_connectClient(), vx_disconnectClient();
static void vx_iisio(), set_fbconfig(), vx_retCursorVal();
static void vx_initialize(), vx_initFrame(), vx_eraseFrame();
static void get_fbconfig(), Usage(), printoption();
#ifdef HAVE_CDL
static void vx_flip();
#endif
static int vx_iisopen(), decode_frameno(), iis_read(), iis_write();
static void bswap2();
static IoChanPtr open_fifo(), open_inet(), open_unix(), get_iochan();
static CtranPtr wcs_update();
#endif
/*
* VXIMTOOL -- Virtual display server process. This task is an image display
* server like XImtool, responding to datastream requests on fifo pipes,
* inet sockets, or unix sockets. Up to 16 frames are supported, frame
* buffers may be any of the defined frames in the imtoolrc file. Images
* are stored in memory, allowing readback by the client. Cursor input can
* come from stdin (or a redirected file) allowing the user to respond to
* client cursor requests. The task is terminated with an EOF on stdin.
*/
#ifdef ANSI_FUNC
int
main (int argc, char **argv)
#else
main (argc, argv)
int argc;
char **argv;
#endif
{
register VXimDataPtr vxim = &server_data;
register IoChanPtr chan;
register int i, nopen, n;
char buf[SZ_FNAME];
int fd;
/* Process the command line arguments. */
for (i=1; i < argc; i++) {
#ifdef HAVE_CDL
/* Anything without a '-' is a client device to add to the proxy
* list. Format of the arg must be a valid IMTDEV string.
*/
if (proxy && argv[i][0] != '-') {
if ((cdl[nclients++] = cdl_open (argv[i])) == (CDLPtr) NULL)
nclients--;
else if (verbose)
printf ("Connected to server on %s\n", argv[i]);
continue;
}
#endif
if (strncmp (argv[i], "-background", 2) == 0) {
background = 1;
} else if (strncmp (argv[i], "-config", 2) == 0) {
vxim->def_config = atoi (argv[++i]);
} else if (strncmp (argv[i], "-fifo_only", 6) == 0) {
vxim->unixaddr = "none";
vxim->port = 0;
} else if (strncmp (argv[i], "-fifo", 5) == 0) {
vxim->input_fifo = (char *) calloc (SZ_FNAME, sizeof(char));
vxim->output_fifo = (char *) calloc (SZ_FNAME, sizeof(char));
sprintf (vxim->input_fifo, "%si", argv[++i]);
sprintf (vxim->output_fifo, "%so", argv[i]);
} else if (strncmp (argv[i], "-help", 2) == 0) {
Usage ();
exit (0);
} else if (strncmp (argv[i], "-imtoolrc", 3) == 0) {
vxim->imtoolrc = argv[++i];
} else if (strncmp (argv[i], "-inet_only", 3) == 0) {
vxim->input_fifo = "";
vxim->unixaddr = "none";
} else if (strcmp (argv[i], "-i") == 0) {
interactive++;
} else if (strncmp (argv[i], "-noraster", 3) == 0) {
keep_raster = 0;
} else if (strncmp (argv[i], "-nframes", 3) == 0) {
i++;
vxim->def_nframes = min (MAX_FRAMES, atoi (argv[i]));
} else if (strncmp (argv[i], "-port_only", 6) == 0) {
vxim->input_fifo = "";
vxim->unixaddr = "none";
} else if (strncmp (argv[i], "-port", 5) == 0) {
vxim->port = atoi (argv[++i]);
#ifdef HAVE_CDL
} else if (strncmp (argv[i], "-proxy", 5) == 0) {
proxy = 1;
vxim->port = DEF_PROXY_PORT; /* re-assign port */
vxim->input_fifo = ""; /* shut off other connections */
vxim->unixaddr = "none";
#endif
} else if (strncmp (argv[i], "-verbose", 2) == 0) {
verbose = 1;
} else if (strncmp (argv[i], "-unix_only", 6) == 0) {
vxim->input_fifo = "";
vxim->port = 0;
} else if (strncmp (argv[i], "-unix", 5) == 0) {
vxim->unixaddr = argv[++i];
}
}
#ifdef HAVE_CDL
/* If we're acting as a proxy server, but can't connect to anything,
* exit. In this case it is required that the servers be running
* before starting the proxy program so we have a connection waiting.
*/
if (!nclients && proxy) {
fprintf (stderr, "Error: No servers available for display.\007\n");
exit (-1);
}
#endif
/* Initialize the frame buffers */
vx_initialize (vxim, vxim->def_config, vxim->def_nframes, 1);
/* Listen for a client connection and initialize the fdset. */
if (!(nopen = vx_iisopen (vxim))) {
fprintf (stderr, "Error: Cannot open client communications.\007\n");
exit (-1);
}
FD_ZERO (&allset);
for (i=0; i < nopen; i++) {
chan = &vxim->chan[i];
FD_SET (chan->datain, &allset);
}
if (!background || interactive)
FD_SET (fileno(stdin), &allset);
/* Sit in a loop waiting on input, processing the events. */
while (1) {
fds = allset; /* reset the FD set on each pass */
if ((n = select (SELWIDTH, &fds, NULL, NULL, NULL)) > 0) {
/* Loop over each of the open connections, checking for and
* processing input on any that are ready.
*/
for (i=0; i < nopen; i++) {
chan = &vxim->chan[i];
fd = chan->datain;
if (FD_ISSET(fd, &fds)) {
/* Connect the client if not already connected. */
if (!chan->connected) {
if (verbose) {
if (chan->type == IO_UNIX)
fprintf (stderr,
"connecting client on %s\n",
chan->path);
else if (chan->type == IO_INET)
fprintf (stderr,
"connecting client on port %d\n",
vxim->port);
}
vx_connectClient (chan, &chan->datain);
}
/* Process any waiting input. */
vx_iisio (chan, &chan->datain, chan->type);
fflush (stdout); fflush (stderr);
}
}
/* Check the stdin for an EOF so we can quit gracefully. */
if (!background) {
if (FD_ISSET(fileno(stdin), &fds)) {
if ((n = read (fileno(stdin), buf, SZ_FNAME)) <= 0) {
/* Shut it down. */
vx_iisclose (vxim);
exit (0);
}
}
}
} else if (n < 0) {
fprintf (stderr, "Error: select error\007\n");
exit (-1);
}
}
}
/* VX_IISOPEN -- Initialize the IIS protocol module and ready the module to
* accept client connections and begin processing client requests. Clients
* may connect to the server using a fifo connection or an internet or
* UNIX domain socket connection. All three types of server ports are
* simultaneously ready to receive client connections.
*/
#ifdef ANSI_FUNC
static int
vx_iisopen (register VXimDataPtr vxim)
#else
static int
vx_iisopen (vxim)
register VXimDataPtr vxim;
#endif
{
int nopen = 0;
if (open_fifo (vxim))
nopen++;
if (open_inet (vxim))
nopen++;
if (open_unix (vxim))
nopen++;
return (nopen);
}
/* VX_IISCLOSE -- Close down the IIS protocol module.
*/
#ifdef ANSI_FUNC
static void
vx_iisclose (register VXimDataPtr vxim)
#else
static void
vx_iisclose (vxim)
register VXimDataPtr vxim;
#endif
{
register IoChanPtr chan = NULL;
register FrameBufPtr fb;
register int i, j;
for (i=0; i < (sizeof(vxim->chan) / sizeof(vxim->chan[0])); i++) {
chan = &vxim->chan[i];
/* Free the in-memory frame buffer rasters. */
for (j=0; j < vxim->nframes; j++) {
fb = &vxim->frames[j];
if (keep_raster && fb->framebuf)
free (fb->framebuf);
}
/* Close the I/O channels. */
switch (chan->type) {
case IO_FIFO:
if (chan->keepalive >= 0)
close (chan->keepalive);
if (chan->datain >= 0)
close (chan->datain);
if (chan->dataout >= 0)
close (chan->dataout);
chan->type = 0;
if (verbose)
fprintf (stderr, "vximtool: closing fifo connection\n");
break;
case IO_INET:
close (chan->datain);
chan->type = 0;
if (verbose)
fprintf (stderr, "vximtool: closing inet socket\n");
break;
case IO_UNIX:
close (chan->datain);
unlink (chan->path);
chan->type = 0;
if (verbose)
fprintf (stderr, "vximtool: closing unix socket\n");
break;
}
}
}
/* OPEN_FIFO -- Open the server fifo port and make ready to accept client
* connections and begin processing client requests. There is no client
* yet at this stage.
*/
#ifdef ANSI_FUNC
static IoChanPtr
open_fifo (register VXimDataPtr vxim)
#else
static IoChanPtr
open_fifo (vxim)
register VXimDataPtr vxim;
#endif
{
register IoChanPtr chan;
int datain, dataout;
int keepalive;
/* Setting the input fifo to "none" or the null string disables
* fifo support.
*/
if (!vxim->input_fifo[0] || strcmp(vxim->input_fifo,"none")==0)
return (NULL);
datain = dataout = -1;
/* Open the output fifo (which is the client's input fifo). We have
* to open it ourselves first as a client to get around the fifo
* open-no-client error.
*/
if ((datain = open (vxim->input_fifo, O_RDONLY|O_NDELAY)) != -1) {
if ((dataout = open (vxim->input_fifo, O_WRONLY|O_NDELAY)) != -1)
fcntl (dataout, F_SETFL, O_WRONLY);
else
goto done;
close (datain);
} else
goto done;
/* Open the input stream, a FIFO pseudodevice file used by
* applications to send us commands and data.
*/
if ((datain = open (vxim->output_fifo, O_RDONLY|O_NDELAY)) == -1)
goto done;
else {
/* Clear O_NDELAY for reading. */
fcntl (datain, F_SETFL, O_RDONLY);
/* Open the client's output fifo as a pseudo-client to make it
* appear that a client is connected.
*/
keepalive = open (vxim->output_fifo, O_WRONLY);
}
done:
/* Allocate and fill in i/o channel descriptor. */
if (datain > 0 && dataout > 0 && (chan = get_iochan(vxim))) {
chan->vxim = (void *) vxim;
chan->type = IO_FIFO;
chan->datain = datain;
chan->dataout = dataout;
chan->keepalive = keepalive;
chan->connected = 1;
chan->reference_frame = 1;
chan->rf_p = &vxim->frames[0];
} else {
fprintf (stderr, "Warning: cannot open %s\n", vxim->output_fifo);
chan = NULL;
}
/* Register input callback. */
if (!chan) {
if (datain > 0)
close (datain);
if (dataout > 0)
close (dataout);
} else if (verbose) {
fprintf (stderr,
"vximtool: Open to accept input on fifo: %s\n", vxim->input_fifo);
}
return (chan);
}
/* OPEN_INET -- Set up a port to be used for incoming client connections
* using internet domain sockets.
*/
#ifdef ANSI_FUNC
static IoChanPtr
open_inet (register VXimDataPtr vxim)
#else
static IoChanPtr
open_inet (vxim)
register VXimDataPtr vxim;
#endif
{
register int s = 0;
register IoChanPtr chan;
struct sockaddr_in sockaddr;
/* Setting the port to zero disables inet socket support. */
if (vxim->port <= 0)
return (NULL);
if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
goto err;
memset ((void *)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons((short)vxim->port);
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (s, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
goto err;
if (listen (s, MAXCONN) < 0)
goto err;
/* Allocate and fill in i/o channel descriptor. */
if ((chan = get_iochan(vxim))) {
chan->vxim = (void *) vxim;
chan->type = IO_INET;
chan->port = vxim->port;
chan->datain = s;
chan->dataout = s;
chan->listen_fd = s;
chan->connected = 0;
chan->reference_frame = 1;
chan->rf_p = &vxim->frames[0];
if (verbose)
fprintf (stderr,
"vximtool: Open to accept input on inet: port %d\n",
vxim->port);
return (chan);
}
err:
fprintf (stderr, "vximtool: cannot open socket on port %d, errno=%d\n",
vxim->port, errno);
if (s)
close (s);
return (NULL);
}
/* OPEN_UNIX -- Set up a port to be used for incoming client connections
* using unix domain sockets.
*/
#ifdef ANSI_FUNC
static IoChanPtr
open_unix (register VXimDataPtr vxim)
#else
static IoChanPtr
open_unix (vxim)
register VXimDataPtr vxim;
#endif
{
register int s = 0;
register IoChanPtr chan;
struct sockaddr_un sockaddr;
char path[256];
/* Setting the addr to "none" or the null string disables unix
* socket support.
*/
if (!vxim->unixaddr[0] || strcmp(vxim->unixaddr,"none")==0)
return (NULL);
/* Get path to be used for the unix domain socket. */
sprintf (path, vxim->unixaddr, getuid());
unlink (path);
if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
goto err;
memset ((void *)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sun_family = AF_UNIX;
strcpy (sockaddr.sun_path, path);
if (bind (s, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
goto err;
if (listen (s, MAXCONN) < 0)
goto err;
/* Allocate and fill in i/o channel descriptor. */
if ((chan = get_iochan(vxim))) {
chan->vxim = (void *) vxim;
chan->type = IO_UNIX;
chan->datain = s;
chan->dataout = s;
chan->listen_fd = s;
chan->connected = 0;
chan->reference_frame = 1;
chan->rf_p = &vxim->frames[0];
strncpy (chan->path, path, SZ_FNAME);
if (verbose)
fprintf (stderr,
"vximtool: Open to accept input on unix: %s\n", path);
return (chan);
}
err:
fprintf (stderr, "vximtool: cannot open socket on port %s, errno=%d\n",
path, errno);
if (s)
close (s);
return (NULL);
}
/* VX_CONNECTCLIENT -- Called when a client has attempted a connection on
* a socket port. Accept the connection and set up a new i/o channel to
* communicate with the new client.
*/
#ifdef ANSI_FUNC
static void
vx_connectClient (IoChanPtr chan, int *source)
#else
static void
vx_connectClient (chan, source)
IoChanPtr chan;
int *source;
#endif
{
register VXimDataPtr vxim = (VXimDataPtr) chan->vxim;
register int s;
/* Accept connection. */
if ((s = accept ((int)*source, (struct sockaddr *)0, (int *)0)) < 0)
return;
if (fcntl (s, F_SETFD, O_RDWR|O_NDELAY) < 0) {
close (s);
return;
}
/* Allocate and fill in i/o channel descriptor. */
FD_SET(s, &allset);
chan->datain = s;
chan->dataout = s;
chan->connected = 1;
chan->reference_frame = 1;
chan->rf_p = &vxim->frames[0];
}
/* VX_DISCONNECTCLIENT -- Called to close a client connection when EOF is
* seen on the input port. Close the connection and free the channel
* descriptor.
*/
#ifdef ANSI_FUNC
static void
vx_disconnectClient (register IoChanPtr chan)
#else
static void
vx_disconnectClient (chan)
register IoChanPtr chan;
#endif
{
switch (chan->type) {
case IO_INET:
if (verbose)
fprintf (stderr,
"disconnecting client on port %d\n", chan->port);
case IO_UNIX:
if (verbose && chan->type == IO_UNIX)
fprintf (stderr,
"disconnecting client on %s\n", chan->path);
FD_CLR(chan->datain, &allset);
close (chan->datain);
chan->datain = chan->dataout = chan->listen_fd;
chan->connected = 0;
break;
default:
break;
}
}
/* GET_IOCHAN --- Get an i/o channel descriptor.
*/
#ifdef ANSI_FUNC
static IoChanPtr
get_iochan (register VXimDataPtr vxim)
#else
static IoChanPtr
get_iochan (vxim)
register VXimDataPtr vxim;
#endif
{
register int i;
for (i=0; i < MAX_CLIENTS; i++)
if (!vxim->chan[i].type)
return (&vxim->chan[i]);
return (NULL);
}
/* VX_IISIO -- File i/o callback procedure, called when there is input
* pending on the data stream to the vximtool client.
*/
#ifdef ANSI_FUNC
static void
vx_iisio (IoChanPtr chan, int *fd_addr, int source)
#else
static void
vx_iisio (chan, fd_addr, source)
IoChanPtr chan;
int *fd_addr;
int source;
#endif
{
register VXimDataPtr vxim = (VXimDataPtr) chan->vxim;
register int sum, i;
register short *p;
int datain = *fd_addr;
int dataout = chan->dataout;
int ndatabytes, nbytes, n, ntrys=0;
struct iism70 iis;
char buf[SZ_FIFOBUF];
static int errmsg=0, bswap=0;
/* Get the IIS header. */
if ((n = iis_read (datain, (char *)&iis, sizeof(iis))) < sizeof(iis)) {
if (n <= 0)
vx_disconnectClient (chan);
else
fprintf (stderr, "vximtool: command input read error\n");
return;
} else if (bswap)
bswap2 ((char *)&iis, (char *)&iis, sizeof(iis));
/* Verify the checksum. If it fails swap the bytes and try again.
*/
for (;;) {
for (i=0, sum=0, p=(short *)&iis; i < 8; i++)
sum += *p++;
if ((sum & 0177777) == 0177777)
break;
if (ntrys++) {
if (!errmsg++) {
fprintf (stderr, "vximtool: bad data header checksum\n");
if (bswap)
bswap2 ((char *)&iis, (char *)&iis, sizeof(iis));
fprintf (stderr, "noswap:");
for (i=0, p=(short *)&iis; i < 8; i++)
fprintf (stderr, " %6o", p[i]);
fprintf (stderr, "\n");
bswap2 ((char *)&iis, (char *)&iis, sizeof(iis));
fprintf (stderr, " swap:");
for (i=0, p=(short *)&iis; i < 8; i++)
fprintf (stderr, " %6o", p[i]);
fprintf (stderr, "\n");
}
break;
} else {
bswap2 ((char *)&iis, (char *)&iis, sizeof(iis));
bswap = !bswap;
}
}
ndatabytes = -iis.thingct;
if (!(iis.tid & PACKED))
ndatabytes *= 2;
if (verbose) {
fprintf (stderr, "%s: ", (source == IO_FIFO ? "fifo" :
(source == IO_INET ? "inet" : "unix")));
fprintf (stderr,
"subunit=%03o tid=%06o nbytes=%6d x=%06o y=%06o z=%06o t=%06o\n",
iis.subunit & 077,
iis.tid,
ndatabytes,
iis.x & 0177777,
iis.y & 0177777,
iis.z & 0177777,
iis.t & 0177777);
fflush (stdout);
}
switch (iis.subunit & 077) {
case FEEDBACK:
/* The feedback unit is used only to clear a frame.
*/
chan->reference_frame = decode_frameno (iis.z & 07777);
vx_eraseFrame (vxim, chan->reference_frame);
#ifdef HAVE_CDL
if (proxy) {
for (i=0; i < nclients; i++)
cdl_clearFrame (cdl[i]);
}
#endif
if (verbose)
fprintf (stderr, "erase frame %d - ref = %d\n",
decode_frameno(iis.z & 07777), chan->reference_frame);
break;
case LUT:
/* Data mode writes to the frame lookup tables are not implemented.
* A command mode write to the LUT subunit is used to connect
* image memories up to the RGB channels, i.e., to select the frame
* to be displayed. We ignore any attempt to assign multiple
* frames to multiple color channels, and just do a simple frame
* select.
*/
if (iis.subunit & COMMAND) {
int frame, z, n;
short x[14];
if (iis_read (datain, (char *)x, ndatabytes) == ndatabytes) {
if (bswap)
bswap2 ((char *)x, (char *)x, ndatabytes);
z = x[0];
if (!z) z = 1;
for (n=0; !(z & 1); z >>= 1)
n++;
frame = max (1, n + 1);
if (frame > vxim->nframes) {
if (frame < MAX_FRAMES) {
#ifdef HAVE_CDL
if (proxy) {
for (i=0; i < nclients; i++) {
cdl_setFBConfig (cdl[i], vxim->fb_configno);
cdl_setFrame (cdl[i], frame);
}
}
#endif
set_fbconfig (chan, vxim->fb_configno, frame);
if (verbose)
fprintf (stderr, "set_fbconfig (%d, %d)\n",
vxim->fb_configno, frame);
} else {
fprintf (stderr, "imtool warning: ");
fprintf (stderr,
"attempt to display nonexistent frame %d\n",
frame);
return;
}
}
vxim->display_frame = frame;
if (verbose)
fprintf (stderr, "set_frame (%d)\n", frame);
return;
}
}
case MEMORY:
/* Load data into the frame buffer. Data is assumed to be byte
* packed.
*/
if (iis.tid & IIS_READ) {
/* Read from the display.
*/
register FrameBufPtr fb;
unsigned char *ip, iobuf[SZ_IOBUF];
int nbytes, nleft, n, x, y;
long starttime;
/* Get the frame to be read from. */
chan->reference_frame = decode_frameno (iis.z & 07777);
fb = &vxim->frames[chan->reference_frame-1];
nbytes = ndatabytes;
x = iis.x & XYMASK;
y = iis.y & XYMASK;
if (x < 0 || x >= vxim->width || y < 0 || y >= vxim->height) {
fprintf (stderr,
"vximtool: attempted read out of bounds on framebuf\n");
fprintf (stderr,
"read %d bytes at [%d,%d]\n", nbytes, x, y);
memset ((void *)iobuf, 0, nbytes);
} else {
if (verbose)
fprintf (stderr, "read %d bytes at [%d,%d]\n",
nbytes, x, y);
if (keep_raster)
bcopy(&fb->framebuf[(y * vxim->width)+x], iobuf, nbytes);
else
bzero (iobuf, nbytes);
#ifdef HAVE_CDL
if (proxy) {
unsigned char *pix = (unsigned char *)malloc(SZ_IOBUF);
cdl_readSubRaster (cdl[0], x,
(vxim->height-y-max(1, nbytes/vxim->width)),
min(vxim->width, nbytes), max(1, nbytes/vxim->width),
&pix);
bcopy(pix, iobuf, nbytes);
vx_flip ((char *)iobuf, min(vxim->width, nbytes),
max(1, nbytes/vxim->width));
free ((char *)pix);
}
#endif
}
/* Return the data from the frame buffer. */
starttime = time(0);
for (nleft=nbytes, ip=iobuf; nleft > 0; nleft -= n) {
n = (nleft < SZ_FIFOBUF) ? nleft : SZ_FIFOBUF;
if ((n = iis_write (dataout, ip, n)) <= 0) {
if (n < 0 || (time(0) - starttime > IO_TIMEOUT)) {
fprintf (stderr, "IMTOOL: timeout on write\n");
break;
}
} else
ip += n;
}
return;
} else {
/* Write to the display.
*/
register FrameBufPtr fb;
unsigned char *op, iobuf[SZ_IOBUF];
int nbytes, nleft, n, x, y;
long starttime;
/* Get the frame to be written into (encoded with a bit for
* each frame, 01 is frame 1, 02 is frame 2, 04 is frame 3,
* and so on).
*/
chan->reference_frame = decode_frameno (iis.z & 07777);
fb = &vxim->frames[chan->reference_frame-1];
nbytes = ndatabytes;
x = iis.x & XYMASK;
y = iis.y & XYMASK;
/* Read the data into the frame buffer.
*/
starttime = time(0);
for (nleft=nbytes, op=iobuf; nleft > 0; nleft -= n) {
n = (nleft < SZ_FIFOBUF) ? nleft : SZ_FIFOBUF;
if ((n = iis_read (datain, op, n)) <= 0) {
if (n < 0 || (time(0) - starttime > IO_TIMEOUT))
break;
} else
op += n;
}
if (x < 0 || x >= vxim->width || y < 0 || y >= vxim->height) {
fprintf (stderr,
"vximtool: attempted write out of bounds on framebuf\n");
fprintf (stderr,
"write %d bytes at [%d,%d]\n", nbytes, x, y);
bzero ((void *)iobuf, nbytes);
} else {
if (verbose)
fprintf (stderr, "write %d bytes at x=%d, y=%d\n",
nbytes, x, y);
if (keep_raster)
bcopy(iobuf, &fb->framebuf[(y * vxim->width)+x], nbytes);
#ifdef HAVE_CDL
if (proxy) {
vx_flip ((char *)iobuf, min(vxim->width, nbytes),
max(1, nbytes/vxim->width));
for (i=0; i < nclients; i++)
cdl_writeSubRaster (cdl[i], x,
(vxim->height-y-max(1, nbytes/vxim->width)),
min(vxim->width, nbytes),
max(1, nbytes/vxim->width),
iobuf);
}
#endif
}
return;
}
break;
case WCS:
/* Read or write the WCS for a frame. The frame number to
* which the WCS applies is passed in Z and the frame buffer
* configuration in T. The client changes the frame buffer
* configuration in a WCS set. The WCS text follows the header
* as byte packed ASCII data.
*/
if (iis.tid & IIS_READ) {
/* Return the WCS for the referenced frame.
*/
char emsg[SZ_FNAME];
char *text;
int frame;
frame = decode_frameno (iis.z & 07777);
chan->reference_frame = frame;
if (chan->rf_p->frameno <= 0)
strcpy (text=emsg, "[NOSUCHFRAME]\n");
else
text = chan->rf_p->wcsbuf;
iis_write (dataout, text, SZ_WCSBUF);
if (verbose) {
fprintf (stderr, "query wcs:\n");
write (2, text, SZ_WCSBUF);
}
} else {
/* Set the WCS for the referenced frame.
*/
register CtranPtr ct;
int fb_config, frame;
frame = decode_frameno (iis.z & 07777);
fb_config = (iis.t & 0777) + 1;
/* See if we need to change the frame buffer configuration,
* or allocate a new frame.
*/
if (fb_config != vxim->fb_configno) {
#ifdef HAVE_CDL
if (proxy) {
for (i=0; i < nclients; i++) {
cdl_setFBConfig (cdl[i], fb_config);
cdl_setFrame (cdl[i], frame);
}
}
#endif
set_fbconfig (chan, fb_config, frame);
} else if (frame > vxim->nframes && frame < MAX_FRAMES) {
#ifdef HAVE_CDL
if (proxy) {
for (i=0; i < nclients; i++) {
cdl_setFBConfig (cdl[i], vxim->fb_configno);
cdl_setFrame (cdl[i], frame);
}
}
#endif
set_fbconfig (chan, vxim->fb_configno, frame);
}
/* Read in and set up the WCS. */
chan->reference_frame = frame;
if (iis_read (datain, buf, ndatabytes) == ndatabytes)
strncpy (chan->rf_p->wcsbuf, buf, SZ_WCSBUF);
if (verbose) {
fprintf (stderr, "set wcs:\n");
write (2, buf, ndatabytes);
}
strcpy (chan->rf_p->ctran.format, W_DEFFORMAT);
chan->rf_p->ctran.imtitle[0] = '\0';
chan->rf_p->ctran.valid = 0;
ct = wcs_update (vxim, chan->rf_p);
#ifdef HAVE_CDL
if (proxy) {
for (i=0; i < nclients; i++)
cdl_setWCS (cdl[i], " ", ct->imtitle,
ct->a, ct->b, ct->c, ct->d,
ct->tx, ct->ty, ct->z1, ct->z2, ct->zt);
}
#endif
}
return;
case IMCURSOR:
/* Read or write the logical image cursor. This is an extension
* added to provide a high level cursor read facility; this is
* not the same as a low level access to the IIS cursor subunit.
* Cursor reads may be either nonblocking (immediate) or blocking,
* using the keyboard or mouse to terminate the read, and
* coordinates may be returned in either image (world) or frame
* buffer pixel coordinates.
*/
if (iis.tid & IIS_READ) {
/* Read the logical image cursor. In the case of a blocking
* read all we do is initiate a cursor read; completion occurs
* when the user hits a key or button.
*/
float sx, sy;
if (verbose)
fprintf (stderr, "read cursor position\n");
if (iis.tid & IMC_SAMPLE) {
/* Sample the cursor position and return the cursor value
* on the output datastream encoded in a fixed size
* ascii buffer.
*/
int wcs = iis.z;
sx = cursor_x;
sy = cursor_y;
#ifdef HAVE_CDL
if (proxy) {
char key = ' ';
char curval[SZ_IMCURVAL], keystr[20];
int wcs = 0;
cdl_readCursor (cdl[0], 1, &sx, &sy, &wcs, &key);
/* Encode the cursor value. */
if (key == EOF)
sprintf (curval, "EOF\n");
else {
if (isprint (key) && !isspace(key)) {
keystr[0] = key;
keystr[1] = '\0';
} else
sprintf (keystr, "\\%03o", key);
sprintf (curval, "%10.3f %10.3f %d %s %s\n",
sx, sy, 1, keystr, "");
}
iis_write (dataout, curval, sizeof(curval));
} else
#endif
vx_retCursorVal (chan->dataout, sx, sy, wcs, 0, "");
} else {
/* Initiate a user triggered cursor read. */
char key = 'q';
#ifdef HAVE_CDL
if (proxy) {
char curval[SZ_IMCURVAL], keystr[20];
int wcs = 0;
cdl_readCursor (cdl[0], 0, &sx, &sy, &wcs, &key);
/* Encode the cursor value. */
if (key == EOF)
sprintf (curval, "EOF\n");
else {
if (isprint (key) && !isspace(key)) {
keystr[0] = key;
keystr[1] = '\0';
} else
sprintf (keystr, "\\%03o", key);
sprintf (curval, "%10.3f %10.3f %d %s %s\n\0",
sx, sy, 1, keystr, "");
}
iis_write (chan->dataout, curval, sizeof(curval));
} else
#endif
vx_retCursorVal (chan->dataout, 1., 1., 101, key, "");
}
} else {
/* Write (set) the logical image cursor position. */
register CtranPtr ct;
int sx = iis.x, sy = iis.y;
float wx = sx, wy = sy;
int wcs = iis.z;
if (verbose)
fprintf (stderr, "write cursor position: [%d,%d]\n", sx,sy);
if (wcs) {
ct = wcs_update (vxim, vxim->df_p);
if (ct->valid) {
if (abs(ct->a) > .001)
sx = (wx - ct->tx) / ct->a;
if (abs(ct->d) > .001)
sy = (wy - ct->ty) / ct->d;
}
}
cursor_x = sx;
cursor_y = sy;
#ifdef HAVE_CDL
if (proxy)
cdl_setCursor (cdl[0], sx, sy, wcs);
#endif
}
return;
default:
/* Ignore unsupported command input.
*/
break;
}
/* Discard any data following the header. */
if (!(iis.tid & IIS_READ))
for (nbytes = ndatabytes; nbytes > 0; nbytes -= n) {
n = (nbytes < SZ_FIFOBUF) ? nbytes : SZ_FIFOBUF;
if ((n = iis_read (datain, buf, n)) <= 0)
break;
}
}
/* SET_FBCONFIG -- Set the frame buffer configuration, or add additional
* frames to the current configuration.
*/
#ifdef ANSI_FUNC
static void
set_fbconfig (IoChanPtr chan, int config, int frame)
#else
static void
set_fbconfig (chan, config, frame)
IoChanPtr chan;
int config;
int frame;
#endif
{
register VXimDataPtr vxim = (VXimDataPtr) chan->vxim;
register FrameBufPtr fb = &vxim->frames[frame-1];
register int i;
if (config != vxim->fb_configno) {
/* Change the frame buffer configuration. */
vx_initialize (vxim, config,
max (vxim->fb_config[config-1].nframes, frame), 1);
} else if (frame > vxim->nframes) {
/* Add additional frames. */
for (i=1; i <= frame; i++) {
fb = &vxim->frames[i-1];
if (fb->frameno != i)
vx_initFrame (vxim, i, frame, &vxim->fb_config[config-1]);
}
}
chan->reference_frame = frame;
}
/* DECODE_FRAMENO -- Decode encoded IIS register frame number.
*/
#ifdef ANSI_FUNC
static int
decode_frameno (register int z)
#else
static int
decode_frameno (z)
register int z;
#endif
{
register int n;
/* Get the frame number, encoded with a bit for each frame, 01 is
* frame 1, 02 is frame 2, 04 is frame 3, and so on.
*/
if (!z) z = 1;
for (n=0; !(z & 1); z >>= 1)
n++;
return (max (1, n + 1));
}
/* BSWAP2 - Move bytes from array "a" to array "b", swapping successive
* pairs of bytes. The two arrays may be the same but may not be offset
* and overlapping.
*/
#ifdef ANSI_FUNC
static void
bswap2 (
char *a,
char *b, /* input array */
int nbytes /* number of bytes to swap */
)
#else
static void
bswap2 (a, b, nbytes)
char *a, *b; /* input array */
int nbytes; /* number of bytes to swap */
#endif
{
register char *ip=a, *op=b, *otop;
register unsigned temp;
/* Swap successive pairs of bytes.
*/
for (otop = op + (nbytes & ~1); op < otop; ) {
temp = *ip++;
*op++ = *ip++;
*op++ = temp;
}
/* If there is an odd byte left, move it to the output array.
*/
if (nbytes & 1)
*op = *ip;
}
/* VX_RETCURSORVAL -- Return the cursor value on the output datastream to
* the client which requested the cursor read.
*/
#ifdef ANSI_FUNC
static void
vx_retCursorVal (
register int dataout,
float sx,
float sy, /* cursor screen coordinates */
int wcs, /* nonzero if WCS coords desired */
int key, /* keystroke used as trigger */
char *strval /* optional string value */
)
#else
static void
vx_retCursorVal (dataout, sx, sy, wcs, key, strval)
register int dataout;
float sx, sy; /* cursor screen coordinates */
int wcs; /* nonzero if WCS coords desired */
int key; /* keystroke used as trigger */
char *strval; /* optional string value */
#endif
{
char curval[SZ_IMCURVAL];
char keystr[20];
/* If running SERVER in interactive mode, allow the user to type
* in the cursor value on the standard input.
*/
if (interactive) {
printf ("enter cursor value string (x y wcs key str): ");
fflush (stdout);
if (fgets (curval, SZ_IMCURVAL, stdin) != NULL)
goto ret;
}
/* Encode the cursor value. */
if (key == EOF)
sprintf (curval, "EOF\n");
else {
if (isprint (key) && !isspace(key)) {
keystr[0] = key;
keystr[1] = '\0';
} else
sprintf (keystr, "\\%03o", key);
sprintf (curval, "%10.3f %10.3f %d %s %s\n",
sx, sy, wcs, keystr, strval);
}
ret:
fprintf (stderr, "%s", curval);
/* Send it to the client program and terminate cursor mode. */
write (dataout, curval, sizeof(curval));
}
/* WCS_UPDATE -- Load the screen WCS, if not yet validated, from the user
* wcs file, if any.
*
* File format (two lines):
*
* image title (imtool header label string)\n
* a b c d tx ty z1 z2 zt
*
* The WCS text is passed in via the data stream as a write to the subunit
* WCS and left in the buffer "wcsbuf".
*/
#ifdef ANSI_FUNC
static CtranPtr
wcs_update (register VXimDataPtr vxim, FrameBufPtr fr)
#else
static CtranPtr
wcs_update (vxim, fr)
register VXimDataPtr vxim;
FrameBufPtr fr;
#endif
{
register CtranPtr ct = &fr->ctran;
char buf[1024], *format;
/* Get the new WCS. */
if (!ct->valid) {
fr->label[0] = '\0';
ct->zt = W_UNITARY;
/* Attempt to read the WCS and set up a unitary transformation
* if the information cannot be read.
*/
if (sscanf (fr->wcsbuf, "%[^\n]\n%f%f%f%f%f%f%f%f%d",
buf, &ct->a, &ct->b, &ct->c, &ct->d, &ct->tx, &ct->ty,
&ct->z1, &ct->z2, &ct->zt) < 7) {
if (fr->wcsbuf[0])
fprintf (stderr, "vximtool: error decoding WCS\n");
strncpy (ct->imtitle, "[NO WCS]\n", SZ_IMTITLE);
ct->a = ct->d = 1;
ct->b = ct->c = 0;
ct->tx = ct->ty = 0;
ct->zt = W_UNITARY;
} else
strncpy (ct->imtitle, buf, SZ_IMTITLE);
ct->valid++;
}
/* Determine best format for wcs output. */
if (ct->valid && ct->zt == W_LINEAR) {
float z1, z2, zrange;
z1 = ct->z1;
z2 = ct->z2;
zrange = (z1 > z2) ? z1 - z2 : z2 - z1;
if (zrange < 100.0 && (abs(z1) + abs(z2)) / 2.0 < 200.0)
format = " %7.2f %7.2f %7.3f%c";
else if (zrange > 99999.0 || (abs(z1) + abs(z2)) / 2.0 > 99999.0)
format = " %7.2f %7.2f %7.3g%c";
else
format = W_DEFFORMAT;
} else
format = " %7.2f %7.2f %7.0f%c";
strcpy (ct->format, format);
return (ct);
}
/* VX_INITIALIZE -- Initialize the imaging subsystem. Read the config file
* and create the frame buffers, mappings, and colormaps.
*/
#ifdef ANSI_FUNC
static void
vx_initialize (register VXimDataPtr vxim, int config, int nframes, int reset)
#else
static void
vx_initialize (vxim, config, nframes, reset)
register VXimDataPtr vxim;
int config;
int nframes;
int reset;
#endif
{
if (reset)
get_fbconfig (vxim);
vxim->fb_configno = config;
vxim->nframes = vxim->fb_config[config].nframes;
vxim->width = vxim->fb_config[config].width;
vxim->height = vxim->fb_config[config].height;
vx_initFrame (vxim, vxim->display_frame, nframes,
&vxim->fb_config[config-1]);
}
/* VX_INITFRAME -- Initialize a frame buffer.
*/
#ifdef ANSI_FUNC
static void
vx_initFrame (register VXimDataPtr vxim, int frame, int nframes, FbConfigPtr config)
#else
static void
vx_initFrame (vxim, frame, nframes, config)
register VXimDataPtr vxim;
int frame, nframes;
FbConfigPtr config;
#endif
{
register FrameBufPtr fb = &vxim->frames[frame-1];
if (frame < 1 || frame > MAX_FRAMES)
return;
/* Create the frame buffer. */
fb->frameno = frame;
if (keep_raster) {
if (fb->framebuf)
free (fb->framebuf);
fb->framebuf = (char *) malloc (config->width * config->height);
}
vxim->width = config->width;
vxim->height = config->height;
vxim->nframes = nframes;
}
/* VX_ERASEFRAME -- Erase a frame.
*/
#ifdef ANSI_FUNC
static void
vx_eraseFrame (register VXimDataPtr vxim, int frame)
#else
static void
vx_eraseFrame (vxim, frame)
register VXimDataPtr vxim;
int frame;
#endif
{
register FrameBufPtr fb = &vxim->frames[frame-1];
if (keep_raster)
bzero (fb->framebuf, vxim->width * vxim->height);
}
/* GET_FBCONFIG -- Read the XIMTOOL startup file to get the set of possible
* frame buffer sizes.
*
* File format: configno nframes width height [extra fields]
* e.g., 1 2 512 512
* 2 2 800 800
* 3 1 1024 1024 # comment
*/
#ifdef ANSI_FUNC
static void
get_fbconfig (register VXimDataPtr vxim)
#else
static void
get_fbconfig (vxim)
register VXimDataPtr vxim;
#endif
{
register char *ip;
register FILE *fp = NULL;
int config, nframes, width, height, i;
char lbuf[SZ_LINE+1], *fname;
/* Initialize the config table. */
vxim->fb_configno = 1;
for (i=0; i < MAX_FBCONFIG; i++) {
vxim->fb_config[i].nframes = 1;
vxim->fb_config[i].width = DEF_FRAME_WIDTH;
vxim->fb_config[i].height = DEF_FRAME_HEIGHT;
}
/* Attempt to open the config file. */
if ((fname=getenv(FBCONFIG_ENV1)) || (fname=getenv(FBCONFIG_ENV2)))
fp = fopen (fname, "r");
if (!fp && (fname = getenv ("HOME"))) {
sprintf (lbuf, "%s/%s", fname, FBCONFIG_1);
fp = fopen (fname = lbuf, "r");
}
if (!fp)
fp = fopen (fname = vxim->imtoolrc, "r");
if (!fp)
return;
/* Scan the frame buffer configuration file.
*/
lbuf[0] = '\0';
while (fgets (lbuf, SZ_LINE, fp) != NULL) {
/* Skip comment lines and blank lines. */
for (ip=lbuf; *ip == ' ' || *ip == '\t'; ip++)
;
if (*ip == '\n' || *ip == '#')
continue;
if (!isdigit (*ip))
continue;
switch (sscanf (ip, "%d%d%d%d", &config,&nframes,&width,&height)) {
case 4:
break; /* normal case */
case 3:
height = width; /* default to square format */
break;
default:
fprintf (stderr, "vximtool: bad config `%s'\n", ip);
continue;
}
nframes = max (1, nframes);
width = max (1, width);
height = max (1, height);
/* Since the frame buffer is stored in a memory pixrect
* (effectively), the line length should be an integral number
* of 16 bit words.
*/
if (width & 1) {
fprintf (stderr, "imtool warning: fb config %d [%d-%dx%d] - ",
config, nframes, width, height);
fprintf (stderr, "frame width should be even, reset to %d\n",
--width);
}
config = max(1, min(MAX_FBCONFIG, config)) - 1;
vxim->fb_config[config].nframes = nframes;
vxim->fb_config[config].width = width;
vxim->fb_config[config].height = height;
}
fclose (fp);
}
/* USAGE -- Print a list of command-line options.
*/
#ifdef ANSI_FUNC
static void
Usage (void)
#else
static void
Usage ()
#endif
{
fprintf (stderr, "Usage:\n\n");
printoption (" vximtool");
printoption ("[-background]"); /* run in background */
printoption ("[-config <num>]"); /* initial config */
printoption ("[-fifo <pipe>]"); /* fifo pipe */
printoption ("[-fifo_only]"); /* use fifo only */
printoption ("[-help]"); /* Print help */
printoption ("[-i]"); /* interactive */
printoption ("[-imtoolrc <file>]"); /* fbconfig file */
printoption ("[-inet_only | -port_only]"); /* use inet only */
printoption ("[-noraster]"); /* don't save pix */
printoption ("[-nframes <num>]"); /* # of frames */
printoption ("[-port <num>]"); /* inet port */
printoption ("[-proxy]"); /* run a proxy server */
printoption ("[-verbose]"); /* verbose output */
printoption ("[-unix <name>]"); /* unix socket */
printoption ("[-unix_only]"); /* use unix only */
fprintf (stderr,"\n");
}
/* PRINTOPTION -- Pretty-print an option string.
*/
static int cpos = 0;
#ifdef ANSI_FUNC
static void
printoption (char *st)
#else
static void
printoption(st)
char *st;
#endif
{
if (strlen(st) + cpos > 78) {
fprintf (stderr,"\n\t");
cpos = 8;
}
fprintf (stderr,"%s ",st);
cpos = cpos + strlen(st) + 1;
}
#ifdef HAVE_CDL
/* VX_FLIP -- Reverse order of lines in raster.
*/
#ifdef ANSI_FUNC
static void
vx_flip (char *buffer, int nx, int ny)
#else
static void
vx_flip (buffer, nx, ny)
char *buffer;
int nx;
int ny;
#endif
{
register int i, j, v;
register char *buff1, *buff2;
for (i = 0; i < ny / 2; i++) {
buff1 = &buffer[i*nx];
buff2 = &buffer[(ny-1-i)*nx];
for (j = 0; j < nx; j++) {
v = *buff1;
*(buff1++) = *buff2;
*(buff2++) = v;
}
}
}
#endif
/* IIS_READ -- Read exactly "n" bytes from a descriptor.
*/
#ifdef ANSI_FUNC
static int
iis_read (int fd, void *vptr, int nbytes)
#else
static int
iis_read (fd, vptr, nbytes)
int fd;
void *vptr;
int nbytes;
#endif
{
char *ptr = vptr;
int nread = 0, nleft = nbytes, nb = 0;
while (nleft > 0) {
if ( (nb = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nb = 0; /* and call read() again */
else
return(-1);
} else if (nb == 0)
break; /* EOF */
nleft -= nb;
ptr += nb;
nread += nb;
}
return (nread); /* return no. of bytes read */
}
/* IIS_WRITE -- Write exactly "n" bytes to a descriptor.
*/
#ifdef ANSI_FUNC
static int
iis_write (int fd, void *vptr, int nbytes)
#else
static int
iis_write (fd, vptr, nbytes)
int fd;
void *vptr;
int nbytes;
#endif
{
char *ptr = vptr;
int nwritten = 0, nleft = nbytes, nb = 0;
while (nleft > 0) {
if ( (nb = write(fd, ptr, nleft)) <= 0) {
if (errno == EINTR)
nb = 0; /* and call write() again */
else
return(-1); /* error */
}
nleft -= nb;
ptr += nb;
nwritten += nb;
}
return (nwritten);
}
|