home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEV3B1.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  22KB  |  797 lines

  1. /* Copyright (C) 1992, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /*
  20.  * gdev3b1.c
  21.  *
  22.  * This is a driver for the AT&T 3b1/7300/UnixPC console display.
  23.  *
  24.  * The image is built in a buffer the size of the page.  Once complete,
  25.  * a screen-sized subset is copied to the screen, and one can scroll
  26.  * through the entire image (move with "vi" or arrow keys).
  27.  *
  28.  * Written by Andy Fyfe, andy@cs.caltech.edu.
  29.  *
  30.  * There are a couple of undesirable "features" that I have found no
  31.  * way to work around.
  32.  *
  33.  * 1) Gs attempts to save the contents of the window before using it, and
  34.  *    then restores the contents afterward.  However, if the gs window is
  35.  *    not the current window, and there are small windows present, then
  36.  *    the saved image is incorrect, and thus the screen will not be correctly
  37.  *    restored.  This seems to be a bug in the 3b1 window driver.  Making
  38.  *    the gs window current before saving its contents is not an acceptable
  39.  *    solution.
  40.  *
  41.  * 2) Gs will enable the scrolling/help/cancel icons if the window has
  42.  *    a border.  Changing these border icons has the side effect of making
  43.  *    the gs window current.  This does circumvent the first problem though.
  44.  */
  45.  
  46. /*
  47.  * About the ATT3B1_PERF flag (notes by Andy Fyfe):
  48.  *
  49.  * I am unable to profile gs on the 3b1, so I added ATT3B1_PERF as a
  50.  * quick way to find out how much time was spent in the 3b1 driver,
  51.  * through dynamically suppressing parts of the code at run time by
  52.  * setting environment variables.  I can then get the time spent in
  53.  * those parts by comparing the results of "time gs ....".
  54.  *
  55.  * At one point this was very useful, and led to a fairly substantial
  56.  * speedup of the fill and copy_mono routines.  It also showed that I
  57.  * wasn't going to get too much more, overall, by further attempts to
  58.  * optimize the 3b1 driver.  So those parts of the code controlled by
  59.  * ATT3B1_PERF have really now outlived their usefulness.
  60.  */
  61.  
  62. #include "gx.h"
  63. #include "gxdevice.h"
  64. #include "gserrors.h"
  65.  
  66. #include <errno.h>
  67. #include <sys/window.h>
  68. #include <sys/termio.h>
  69.  
  70. typedef struct gx_device_att3b1_s {
  71.     gx_device_common;
  72.     int fd;                /* window file descriptor */
  73.     uchar *screen;            /* pointer to screen image */
  74.     ushort line_size;            /* size of screen line in bytes */
  75.     ulong screen_size;            /* size of screen image in bytes */
  76.     int page_num;                /* page number */
  77. #ifdef ATT3B1_PERF
  78.     char *no_output, *no_fill, *no_copy;
  79. #endif
  80. } gx_device_att3b1;
  81. #define att3b1dev ((gx_device_att3b1 *)dev)
  82.  
  83. #define XDPI    100        /* to get a more-or-less square aspect ratio */
  84. #define YDPI    72
  85. #define XSIZE (8.5 * XDPI)    /* 8.5 x 11 inch page, by default */
  86. #define YSIZE (11 * YDPI)
  87.  
  88. static const ushort masks[] = { 0,
  89.     0x0001, 0x0003, 0x0007, 0x000f,
  90.     0x001f, 0x003f, 0x007f, 0x00ff,
  91.     0x01ff, 0x03ff, 0x07ff, 0x0fff,
  92.     0x1fff, 0x3fff, 0x7fff, 0xffff,
  93. };
  94. static uchar reverse_bits[256] = {
  95.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  96.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  97.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  98.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  99.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  100.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  101.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  102.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  103.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  104.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  105.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  106.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  107.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  108.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  109.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  110.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  111. };
  112.  
  113. dev_proc_open_device(att3b1_open);
  114. dev_proc_close_device(att3b1_close);
  115. dev_proc_fill_rectangle(att3b1_fill_rectangle);
  116. dev_proc_copy_mono(att3b1_copy_mono);
  117. dev_proc_output_page(att3b1_output_page);
  118.  
  119. private gx_device_procs att3b1_procs = {
  120.     att3b1_open,
  121.     gx_default_get_initial_matrix,
  122.     gx_default_sync_output,
  123.     att3b1_output_page,
  124.     att3b1_close,
  125.     gx_default_map_rgb_color,
  126.     gx_default_map_color_rgb,
  127.     att3b1_fill_rectangle,
  128.     gx_default_tile_rectangle,
  129.     att3b1_copy_mono,
  130.     gx_default_copy_color,
  131.     gx_default_draw_line,
  132.     gx_default_get_bits
  133. };
  134.  
  135. gx_device_att3b1 gs_att3b1_device = {
  136.     sizeof(gx_device_att3b1),
  137.     &att3b1_procs,
  138.     "att3b1",
  139.     XSIZE, YSIZE,
  140.     XDPI, YDPI,
  141.     no_margins,
  142.     dci_black_and_white,
  143.     0,
  144.     -1, 0, 0,            /* fd, screen, line_size, */
  145.     0, 0,            /* screen size, page */
  146. #ifdef ATT3B1_PERF
  147.     0, 0, 0,            /* no_output, no_fill, no_copy */
  148. #endif
  149. };
  150.  
  151. int
  152. att3b1_open(gx_device *dev)
  153. {
  154.     struct uwdata uw;
  155.  
  156. #ifdef ATT3B1_PERF
  157.     char *getenv(const char *);
  158. #endif
  159.  
  160.     if (att3b1dev->fd >= 0) {
  161.     close(att3b1dev->fd);
  162.     att3b1dev->fd = -1;
  163.     }
  164.  
  165.     if (att3b1dev->screen != NULL) {
  166.     gs_free((char *)att3b1dev->screen,
  167.         att3b1dev->screen_size, 1, "att3b1_open");
  168.     att3b1dev->screen = 0;
  169.     att3b1dev->screen_size = 0;
  170.     }
  171.  
  172.     att3b1dev->fd = open("/dev/tty", 2);
  173.     if (att3b1dev->fd < 0) {
  174.     lprintf1("att3b1_open: open /dev/tty failed [%d]\n", errno);
  175.     return_error(gs_error_ioerror);
  176.     }
  177.  
  178.     /* Verify that /dev/tty is associated with a console window. */
  179.     if (ioctl(att3b1dev->fd, WIOCGETD, &uw) < 0) {
  180.     lprintf1("att3b1_open: can not obtain window data [%d]\n", errno);
  181.     lprintf("att3b1_open: the att3b1 device requires a console window\n");
  182.     att3b1_close(dev);
  183.     return_error(gs_error_ioerror);
  184.     }
  185.  
  186.     /* we need an even number of bytes per line */
  187.     att3b1dev->line_size = ((att3b1dev->width + 15) / 16) * 2;
  188.     att3b1dev->screen_size = att3b1dev->line_size * att3b1dev->height;
  189.  
  190.     att3b1dev->screen =
  191.     (uchar *)gs_malloc(att3b1dev->screen_size, 1, "att3b1_open");
  192.     if (att3b1dev->screen == NULL) {
  193.     att3b1_close(dev);
  194.     return_error(gs_error_VMerror);
  195.     }
  196.  
  197.     att3b1dev->page_num = 1;
  198.  
  199. #ifdef ATT3B1_PERF
  200.     att3b1dev->no_output = getenv("GS_NOOUTPUT");
  201.     att3b1dev->no_fill = getenv("GS_NOFILL");
  202.     att3b1dev->no_copy = getenv("GS_NOCOPY");
  203. #endif
  204.  
  205.     return 0;
  206. }
  207.  
  208. int
  209. att3b1_close(gx_device *dev)
  210. {
  211.     if (att3b1dev->fd >= 0) {
  212.     close(att3b1dev->fd);
  213.     att3b1dev->fd = -1;
  214.     }
  215.  
  216.     if (att3b1dev->screen != NULL) {
  217.     gs_free((char *)att3b1dev->screen,
  218.         att3b1dev->screen_size, 1, "att3b1_close");
  219.     att3b1dev->screen = 0;
  220.     att3b1dev->screen_size = 0;
  221.     }
  222.  
  223.     return 0;
  224. }
  225.  
  226. int
  227. att3b1_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  228.                       gx_color_index colour)
  229. {
  230.     uint o, b, wl, wr, w2;
  231.     ushort *p, *q, maskl, maskr;
  232.  
  233. #ifdef ATT3B1_PERF
  234.     if (att3b1dev->no_fill) return 0;
  235. #endif
  236.  
  237.     fit_fill(dev, x, y, w, h);
  238.  
  239.     /* following fit_fill, we can assume x, y, w, h are unsigned. */
  240.  
  241.     p = (ushort *)&att3b1dev->screen[(ushort)y*att3b1dev->line_size] +
  242.     (uint)x/16;
  243.     o = (uint)x % 16;
  244.     b = 16 - o;
  245.     wl = ((uint)w < b) ? (uint)w : b;
  246.     maskl = masks[wl] << o;
  247.     w -= wl;
  248.     wr = (uint)w % 16;
  249.     maskr = masks[wr];
  250.  
  251.     if (colour == 0) {
  252.     maskl = ~maskl;
  253.     maskr = ~maskr;
  254.     while (h-- > 0) {
  255.         q = p;
  256.         w2 = w;
  257.         *q++ &= maskl;
  258.         while (w2 >= 16) {
  259.         *q++ = 0;
  260.         w2 -= 16;
  261.         }
  262.         *q &= maskr;
  263.         p += (att3b1dev->line_size / 2);
  264.     }
  265.     }
  266.     else {
  267.     while (h-- > 0) {
  268.         q = p;
  269.         w2 = w;
  270.         *q++ |= maskl;
  271.         while (w2 >= 16) {
  272.         *q++ = 0xffff;
  273.         w2 -= 16;
  274.         }
  275.         *q |= maskr;
  276.         p += (att3b1dev->line_size / 2);
  277.     }
  278.     }
  279.  
  280.     return 0;
  281. }
  282.  
  283. #ifdef __GNUC__
  284. #define rotate(value, count) \
  285.     asm("ror%.l    %2,%0" : "=d" (value) : "0" (value), "d" (count))
  286. #else
  287. #define rotate(value, count) \
  288.     value = (value >> count) | (value << (32-count))
  289. #endif
  290.  
  291. int
  292. att3b1_copy_mono(gx_device *dev, const uchar *data,
  293.          int data_x, int raster, gx_bitmap_id id,
  294.          int x, int y, int width, int height, 
  295.          gx_color_index colour0, gx_color_index colour1)
  296. {
  297.     const ushort *src_p, *src_q;
  298.     ushort *dst_p, *dst_q;
  299.     ulong bits, mask, *p;
  300.     uint src_o, src_b, dst_o, dst_b, op;
  301.     uint w1, w2;
  302.  
  303. #ifdef ATT3B1_PERF
  304.     if (att3b1dev->no_copy) return 0;
  305. #endif
  306.  
  307.     if (colour1 == colour0)        /* vacuous case */
  308.     return att3b1_fill_rectangle(dev, x, y, width, height, colour0);
  309.  
  310.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  311.  
  312.     /* following fit_copy, we can assume x, y, width, height are unsigned. */
  313.  
  314.     /*
  315.      * In what follows, we're assuming that each row of the input bitmap
  316.      * is short-aligned, that is, that both "data" and "raster" are even.
  317.      */
  318.     src_p = ((const ushort *)data) + (uint)data_x/16;
  319.     src_o = (uint)data_x % 16;
  320.     src_b = 16 - src_o;
  321.  
  322.     dst_p = (ushort *)&att3b1dev->screen[(ushort)y*att3b1dev->line_size] +
  323.         (uint)x/16;
  324.     dst_o = (uint)x % 16;
  325.     dst_b = 16 - dst_o;
  326.  
  327.     op = (int)colour0 * 3 + (int)colour1 + 4;
  328.  
  329.     while (height-- > 0) {
  330.     w2 = width;
  331.     src_q = src_p;
  332.     dst_q = dst_p;
  333.  
  334.     while (w2 > 0) {
  335.         w1 = (w2 < 16) ? w2 : 16;
  336.         mask = masks[w1];
  337.         /*
  338.          * We are assuming that the bitmap "data" is typically aligned.
  339.          * Thus the test for this special case is typically a win over
  340.          * a 16-bit shift.
  341.          */
  342.         if (src_o == 0)
  343.         bits = *src_q++;
  344.         else {
  345.         bits = *((ulong *)src_q) >> src_b;
  346.         bits &= 0xffff;
  347.         src_q++;
  348.         }
  349.         if (w1 <= 8)
  350.         bits = reverse_bits[bits>>8];
  351.         else
  352.         bits = (reverse_bits[bits&0xff] << 8) | reverse_bits[bits>>8];
  353.         /*
  354.          * While the input bit map is assumed to be typically aligned, we
  355.          * assume that the place in the image is not.  Thus we don't
  356.          * separate out the aligned case.  Doing so would cost a test,
  357.          * and only reduce the average shift by about 1.
  358.          */
  359.         p = (ulong *)dst_q;
  360.         switch(op) {
  361.         case 1:    /* not src and dst */
  362.         bits = ~(bits & mask);
  363.         rotate(bits,dst_b);
  364.         *p &= bits;
  365.         break;
  366.         case 2:    /* src or dst */
  367.         bits = bits & mask;
  368.         rotate(bits,dst_b);
  369.         *p |= bits;
  370.         break;
  371.         case 3:    /* src and dst */
  372.         bits = bits | ~mask;
  373.         rotate(bits,dst_b);
  374.         *p &= bits;
  375.         break;
  376.         case 5:    /* src */
  377.         rotate(bits,dst_b);
  378.         rotate(mask,dst_b);
  379.         *p = (*p & ~mask) | (bits & mask);
  380.         break;
  381.         case 6:    /* not src or dst */
  382.         bits = ~bits & mask;
  383.         rotate(bits,dst_b);
  384.         *p |= bits;
  385.         break;
  386.         case 7:    /* not src */
  387.         rotate(bits,dst_b);
  388.         rotate(mask,dst_b);
  389.         *p = (*p & ~mask) | (~bits & mask);
  390.         break;
  391.         }
  392.         dst_q++;
  393.         w2 -= w1;
  394.     }
  395.  
  396.     src_p += (raster / 2);
  397.     dst_p += (att3b1dev->line_size / 2);
  398.     }
  399.  
  400.     return 0;
  401. }
  402.  
  403. static int getKeyboard(gx_device *);
  404.  
  405. const char *help_msg[] = {
  406.     "h, j, k, l, UP, DOWN, LEFT, RIGHT  move the page (0.25\" h, 0.5\" v)",
  407.     "H, J, K, L, BEG, END               move to far edge of the page",
  408.     "^U, ^D, ROLL UP, ROLL DOWN            scroll up or down (1/2 screen height)",
  409.     "^F, ^B, PAGE UP, PAGE DOWN            scroll up or down (full screen height)",
  410.     "c, C                               centre page horizontally, vertically",
  411.     "<, >, ^, _                         fine movements (single pixel)",
  412.     "^L, ^R, r, HOME                    move to default position",
  413.     "=, MARK                            make current position the default",
  414.     "I                                  invert the image (black <-> white)",
  415.     "q, x, ^C, EXIT, CANCL, n, f, NEXT,",
  416.     "    SPACE, RETURN, ENTER           end the page",
  417.     "?, HELP                            help screen",
  418. };
  419.  
  420. static void
  421. do_help(gx_device *dev)
  422. {
  423.     int i;
  424.     struct utdata ut;
  425.  
  426.     /* we would like to save the cursor position, but we can't */
  427.     write(att3b1dev->fd, "\033[2J\033[H", 7);
  428.  
  429.     /* write help screen */
  430.     for (i=0; i < sizeof(help_msg)/sizeof(help_msg[0]); ++i) {
  431.     write(att3b1dev->fd, help_msg[i], strlen(help_msg[i]));
  432.     write(att3b1dev->fd, "\n", 1);
  433.     }
  434.     ut.ut_num = WTXTSLK1;
  435.     strcpy(ut.ut_text, "Press any key to continue");
  436.     ioctl(att3b1dev->fd, WIOCSETTEXT, &ut);
  437.  
  438.     /* wait for keyboard input */
  439.     i = getKeyboard(dev);
  440.  
  441.     /* clear screen and put cursor at the bottom of the screen */
  442.     write(att3b1dev->fd, "\033[2J\033[99;1H", 11);
  443. }
  444.  
  445. int
  446. att3b1_output_page(gx_device *dev, int num_copies, int flush)
  447. {
  448.     struct urdata ur;
  449.     struct utdata ut, ut_orig;
  450.     struct uwdata uw;
  451.     int uflags;
  452.     struct termio old, new;
  453.     int xorigin, yorigin;
  454.     static int def_xorigin = 0, def_yorigin = 0;
  455.     int screen_width, screen_height;
  456.     int inverted = 0;
  457.     int error = 0;
  458.     int ch;
  459.     ushort *p;
  460.     ushort save_image[WINWIDTH * WINHEIGHT / 16];
  461.  
  462. #ifdef ATT3B1_PERF
  463.     if (att3b1dev->no_output) return 0;
  464. #endif
  465.  
  466.     /*
  467.      * initialize, and save screen state
  468.      */
  469.  
  470.     if (ioctl(att3b1dev->fd, WIOCGETD, &uw) < 0) {
  471.     lprintf1("att3b1_output_page: window WIOCGETD ioctl failed [%d]\n",
  472.         errno);
  473.     att3b1_close(dev);
  474.     return_error(gs_error_ioerror);
  475.     }
  476.  
  477.     /*
  478.      * we assume, henceforth, that screen ioctl calls will succeed
  479.      */
  480.  
  481.     write(att3b1dev->fd, "\a\033[=1C", 6);
  482.  
  483.     uflags = uw.uw_uflags;
  484.     if (!(uflags & NBORDER)) {
  485.     uw.uw_uflags = BORDHSCROLL | BORDVSCROLL | BORDHELP | BORDCANCEL;
  486.     ioctl(att3b1dev->fd, WIOCSETD, &uw);
  487.     }
  488.  
  489.     ut_orig.ut_num = WTXTSLK1;
  490.     ioctl(att3b1dev->fd, WIOCGETTEXT, &ut_orig);
  491.  
  492.     /* This isn't necessary, but helps a bit when the following attempt
  493.        to get the current screen image fails (without any indication). */
  494.     memset(save_image, '\0', sizeof(save_image));
  495.  
  496.     ur.ur_srcbase = 0;
  497.     ur.ur_srcwidth = 0;
  498.     ur.ur_srcx = 0;
  499.     ur.ur_srcy = 0;
  500.     ur.ur_dstbase = save_image;
  501.     ur.ur_dstwidth = WINWIDTH / 8;
  502.     ur.ur_dstx = 0;
  503.     ur.ur_dsty = 0;
  504.     ur.ur_width = uw.uw_width;
  505.     ur.ur_height = uw.uw_height;
  506.     ur.ur_srcop = SRCSRC;
  507.     ur.ur_dstop = DSTSRC;
  508.     ur.ur_pattern = 0;
  509.     ioctl(att3b1dev->fd, WIOCRASTOP, &ur);
  510.  
  511.     ioctl(att3b1dev->fd, TCGETA, &old);
  512.     new = old;
  513.     new.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  514.     new.c_cc[VMIN] = 1;
  515.     ioctl(att3b1dev->fd, TCSETAF, &new);
  516.  
  517.     screen_width = (uw.uw_width < att3b1dev->width) ? uw.uw_width
  518.                 : att3b1dev->width;
  519.     screen_height = (uw.uw_height < att3b1dev->height) ? uw.uw_height
  520.                 : att3b1dev->height;
  521.  
  522.     write(att3b1dev->fd, "\033[2J", 4);
  523.  
  524.     ur.ur_srcwidth = att3b1dev->line_size;
  525.     ur.ur_width = screen_width;
  526.     ur.ur_height = screen_height;
  527.     ur.ur_dstbase = 0;
  528.     ur.ur_dstwidth = 0;
  529.  
  530.     /*
  531.      * allow one to move the screen window through the entire image
  532.      */
  533.  
  534.     xorigin = def_xorigin;
  535.     yorigin = def_yorigin;
  536.  
  537.     while (1) {
  538.     /* Things go bad if ur_srcx >= 2048 */
  539.     ur.ur_srcbase = (ushort *)att3b1dev->screen + (xorigin >> 4);
  540.     ur.ur_srcx = xorigin & 15;
  541.     ur.ur_srcy = yorigin;
  542.  
  543.     if (ioctl(att3b1dev->fd, WIOCRASTOP, &ur) < 0) {
  544.         lprintf1(
  545.         "att3b1_output_page: window WIOCRASTOP ioctl failed [%d]\n",
  546.         errno);
  547.         error = gs_error_ioerror;
  548.     }
  549.  
  550.     ut.ut_num = WTXTSLK1;
  551.     sprintf(ut.ut_text,
  552.         "%s %d, top right (%d,%d), size (%d,%d), press '?' for help.",
  553.         flush ? "Showpage" : "Copypage", att3b1dev->page_num, xorigin, yorigin,
  554.         att3b1dev->width, att3b1dev->height);
  555.     ioctl(att3b1dev->fd, WIOCSETTEXT, &ut);
  556.  
  557.     ch = error ? 'q' : getKeyboard(dev);
  558.  
  559.     switch(ch) {
  560.     case 'h':
  561.         xorigin -= ((uint)(int)att3b1dev->x_pixels_per_inch+3)/4;
  562.         break;
  563.  
  564.     case 'k':
  565.         yorigin -= ((uint)(int)att3b1dev->y_pixels_per_inch+1)/2;
  566.         break;
  567.  
  568.     case 'l':
  569.         xorigin += ((uint)(int)att3b1dev->x_pixels_per_inch+3)/4;
  570.         break;
  571.  
  572.     case 'j':
  573.         yorigin += ((uint)(int)att3b1dev->y_pixels_per_inch+1)/2;
  574.         break;
  575.  
  576.     case 'H':
  577.         xorigin = 0;
  578.         break;
  579.  
  580.     case 'K':
  581.         yorigin = 0;
  582.         break;
  583.  
  584.     case 'L':
  585.         xorigin = att3b1dev->width - screen_width;
  586.         break;
  587.  
  588.     case 'J':
  589.         yorigin = att3b1dev->height - screen_height;
  590.         break;
  591.  
  592.     case '<':
  593.         xorigin -= 1;
  594.         break;
  595.  
  596.     case '>':
  597.         xorigin += 1;
  598.         break;
  599.  
  600.     case '^':
  601.         yorigin -= 1;
  602.         break;
  603.  
  604.     case '_':
  605.         yorigin += 1;
  606.         break;
  607.  
  608.         
  609.     case '\025':    /* control-U */
  610.         yorigin -= screen_height/2;
  611.         break;
  612.  
  613.     case '\004':    /* control-D */
  614.         yorigin += screen_height/2;
  615.         break;
  616.  
  617.     case '\002':    /* control-B */
  618.         yorigin -= screen_height;
  619.         break;
  620.  
  621.     case '\006':    /* control-F */
  622.         yorigin += screen_height;
  623.         break;
  624.  
  625.     case '\f':
  626.     case 'r' :
  627.     case '\022':    /* control-R */
  628.         xorigin = def_xorigin;
  629.         yorigin = def_yorigin;
  630.         break;
  631.     
  632.     case 'c':    /* centre horizontally */
  633.         xorigin = (att3b1dev->width - screen_width) / 2;
  634.         break;
  635.     
  636.     case 'C':    /* centre vertically */
  637.         yorigin = (att3b1dev->height - screen_height) / 2;
  638.         break;
  639.  
  640.     case '=':
  641.         def_xorigin = xorigin;
  642.         def_yorigin = yorigin;
  643.         break;
  644.  
  645.     case 'I':
  646.         for (p = (ushort *)att3b1dev->screen;
  647.           p < (ushort *)&att3b1dev->screen[att3b1dev->screen_size]; ++p)
  648.         *p = ~ *p;
  649.         inverted = !inverted;
  650.         break;
  651.     
  652.     case '?':
  653.         do_help(dev);
  654.         break;
  655.     
  656.     case -1:
  657.         error = gs_error_ioerror;
  658.         /* fall through, for cleanup */
  659.  
  660.     case 'q':
  661.     case 'x':
  662.     case '\003':    /* control-C */
  663.     case 'n':
  664.     case 'f':
  665.     case ' ':
  666.     case '\n':
  667.     case '\r':
  668.         if (flush)
  669.         att3b1dev->page_num++;
  670.         else if (inverted)    /* restore inverted image for copypage */
  671.         for (p = (ushort *)att3b1dev->screen;
  672.           p < (ushort *)&att3b1dev->screen[att3b1dev->screen_size]; ++p)
  673.             *p = ~ *p;
  674.         if (!(uflags & NBORDER)) {
  675.         ioctl(att3b1dev->fd, WIOCGETD, &uw); /*window may have moved*/
  676.         uw.uw_uflags = uflags;
  677.         ioctl(att3b1dev->fd, WIOCSETD, &uw);
  678.         }
  679.         ur.ur_srcbase = save_image;
  680.         ur.ur_srcwidth = WINWIDTH / 8;
  681.         ur.ur_width = uw.uw_width;
  682.         ur.ur_height = uw.uw_height;
  683.         ur.ur_srcx = 0;
  684.         ur.ur_srcy = 0;
  685.         ioctl(att3b1dev->fd, WIOCRASTOP, &ur);
  686.         ioctl(att3b1dev->fd, WIOCSETTEXT, &ut_orig);
  687.         ioctl(att3b1dev->fd, TCSETAF, &old);
  688.         write(att3b1dev->fd, "\033[=0C", 5);
  689.  
  690.         if (error) {
  691.         att3b1_close(dev);
  692.         return_error(error);
  693.         }
  694.         else
  695.         return 0;
  696.     }
  697.  
  698.     if (xorigin >= att3b1dev->width - screen_width)
  699.         xorigin = att3b1dev->width - screen_width;
  700.     if (xorigin < 0)
  701.         xorigin = 0;
  702.     if (yorigin >= att3b1dev->height - screen_height)
  703.         yorigin = att3b1dev->height - screen_height;
  704.     if (yorigin < 0)
  705.         yorigin = 0;
  706.     }
  707. }
  708.  
  709. static int
  710. get_char(gx_device *dev)
  711. {
  712.     char ch;
  713.     int count;
  714.  
  715.     count = read(att3b1dev->fd, &ch, 1);
  716.     if (count == 0)
  717.     return 'q';
  718.     else if (count < 0)
  719.     return -1;
  720.     else
  721.     return ch;
  722. }
  723.  
  724. static int
  725. getKeyboard(gx_device *dev)
  726. {
  727.     char ch;
  728.  
  729.     ch = get_char(dev);
  730.  
  731.     if (ch != '\033')
  732.     return ch;
  733.  
  734.     /*
  735.      * If the char is escape, interpret the escape sequence and return
  736.      * an equivalent single character.
  737.      *
  738.      * Note that a mouse click on a window border icon is translated
  739.      * to the corresponding key, for example, the "up" icon generates
  740.      * roll-up/page-up/beg for the left/middle/right mouse button.
  741.      */
  742.  
  743.     switch (get_char(dev)) {
  744.     case '[':
  745.     switch(get_char(dev)) {
  746.     case 'A':    /* up arrow */
  747.         return 'k';
  748.     case 'T':    /* shift up arrow (roll up) */
  749.         return '\025';
  750.     case 'B':    /* down arrow */
  751.         return 'j';
  752.     case 'S':    /* shift down arrow (roll down) */
  753.         return '\004';
  754.     case 'C':    /* right arrow */
  755.         return 'l';
  756.     case 'D':    /* left arrow */
  757.         return 'h';
  758.     case 'H':    /* home */
  759.         return 'r';
  760.     case 'U':    /* page down */
  761.         return '\006';
  762.     case 'V':    /* page up */
  763.         return '\002';
  764.     }
  765.     break;
  766.     case 'O':
  767.     switch(get_char(dev)) {
  768.     case 'm':    /* help */
  769.     case 'M':    /* shift help */
  770.         return '?';
  771.     case 'k':    /* exit */
  772.     case 'K':    /* shift exit */
  773.     case 'w':    /* cancl */
  774.     case 'W':    /* shift cancl */
  775.         return 'q';
  776.     }
  777.     break;
  778.     case 'N':
  779.     switch(get_char(dev)) {
  780.     case 'h':    /* next */
  781.         return 'f';
  782.     case 'i':    /* mark */
  783.         return '=';
  784.     case 'L':    /* shift right arrow */
  785.         return 'l';
  786.     case 'K':    /* shift left arrow */
  787.         return 'h';
  788.     }
  789.     break;
  790.     case '9':    /* Beg */
  791.     return 'K';
  792.     case '0':    /* End */
  793.     return 'J';
  794.     }
  795.     return '\0';
  796. }
  797.