home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / tek / pegEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-16  |  26.1 KB  |  1,038 lines

  1. /* $Header: pegEvent.c,v 1.4 89/12/16 13:12:28 rws Exp $ */
  2. /***********************************************************
  3. Copyright 1987 by Tektronix, Beaverton, Oregon,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its
  9. documentation for any purpose and without fee is hereby granted,
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in
  12. supporting documentation, and that the names of Tektronix or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.
  15.  
  16. TEKTRONIX DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25.  
  26. #include "X.h"
  27. #define NEED_EVENTS
  28. #include "Xproto.h"
  29. #include "keysym.h"
  30. #include "inputstr.h"
  31. #include "miscstruct.h"
  32. #include "screenint.h"
  33. #include "keynames_xtl.h"
  34.  
  35. #ifdef    UTEK
  36. #include <box/keyboard.h>
  37. #endif    /* UTEK */
  38.  
  39. #ifdef    UTEKV
  40. #include "redwing/keyboard.h"
  41. #endif    /* UTEKV */
  42.  
  43. #include "peg.h"
  44.  
  45. /*
  46.  * Test Consortium Input Extension Variables
  47.  */
  48. #ifdef XTESTEXT1
  49. SvcCursorXY LastMousePosition;
  50. #endif /* XTESTEXT1 */
  51.  
  52. extern InitInfo pegInfo;
  53. extern CARD16 keyModifiersList[];
  54. extern int screenIsSaved;
  55. static Bool    KanaShiftState;
  56. static Bool    KanaLocked;
  57. static Bool    readyToUnlock;
  58. static KeyCode    InitialKanaLockKey;
  59. static int    NumKanaLocks;
  60. static int    NumKanaShifts;
  61.  
  62. #ifdef XDEBUG
  63. static char *type[] = {
  64.     "button",
  65.     "mmotion",
  66.     "tmotion",
  67.     "timeout",
  68.     "pan",
  69.     "???",
  70.     "???",
  71.     "???",
  72. };
  73.  
  74. static char *direction[] = {
  75.     "kbtup",
  76.     "kbtdown",
  77.     "kbtraw",
  78.     "???",
  79. };
  80.  
  81. static char *device[] = {
  82.     "???",
  83.     "mouse",
  84.     "dkb",
  85.     "tablet",
  86.     "aux",
  87.     "console",
  88.     "???",
  89.     "???",
  90. };
  91. #endif /* XDEBUG */
  92.  
  93. static Event
  94. *RepeatKey()
  95. {
  96.     static Event    ev;
  97.     register        c = pegInfo.kv.timer[ KEYREPEAT ].key + MINKEYCODE,
  98.             index;
  99.     XDisplayState       *dsp;
  100.  
  101.     /*
  102.      * The protocol says that we should deliver up AND down events
  103.      * for auto-repeating keys.  But we need not worry, because
  104.      * ProcessKeyboardEvent() in dix/events.c delivers an extra up event
  105.      * for us if it sees two down events.
  106.      */
  107.     debug10(("key 0x%x timeout", pegInfo.kv.timer[ KEYREPEAT ].key));
  108.     if (KeyDepressed(c)) {
  109.     dsp = pegInfo.dsp;
  110.     ev.e_x = dsp->ds_x;
  111.     ev.e_y = dsp->ds_y;
  112.     ev.e_type = E_BUTTON;
  113.     ev.e_device = E_DKB;
  114.     ev.e_direction = E_KBTDOWN;
  115.     ev.e_key = pegInfo.kv.timer[ KEYREPEAT ].key;
  116.     ev.e_time = (dsp->ds_ltime%1000)/10;
  117.     debug10(("\n"));
  118.     } else {
  119.     pegInfo.kv.timer[ KEYREPEAT ].key = -1;
  120.     debug10(("... already up\n", c));
  121.     return(NULL);
  122.     }
  123.  
  124.     /*
  125.      * figure out next timeout.
  126.      */
  127.     index = pegInfo.kv.timer[ KEYREPEAT ].delay;
  128.     if (++index >= pegInfo.kv.nKeyDelays)
  129.     index = pegInfo.kv.nKeyDelays - 1;
  130.     pegInfo.kv.timer[ KEYREPEAT ].delay = index;
  131.     pegInfo.kv.timer[ KEYREPEAT ].when =
  132.     dsp->ds_ltime + pegInfo.kv.keyDelays[ index ];
  133.     return(&ev);
  134. }
  135.  
  136. static void
  137. pan4405()
  138. {
  139.     register XDisplayState  *dsp = pegInfo.dsp;
  140.     register down;
  141. #ifdef    UTEK
  142.     register x, y, deltax, deltay;
  143.     SvcCursorXY    newViewPort;
  144. #endif    /* UTEK */
  145.  
  146.     int delay;
  147.  
  148.     /*
  149.      * First, decide what keys are being repeated.
  150.      */
  151. #ifdef    UTEK
  152.     debug11(("%x: pan(x,y)=(%s,%s) xydelay=(%d,%d)",
  153.     dsp->ds_ltime / dsp->ds_ltimeinc,
  154.     JoyEWDepressed ? "down" : "up",
  155.     JoyNSDepressed ? "down" : "up",
  156.     pegInfo.kv.timer[ JOYEWREPEAT ].delay,
  157.     pegInfo.kv.timer[ JOYNSREPEAT ].delay));
  158. #endif    /* UTEK */
  159.  
  160.     down = JoyDepressed;
  161.  
  162.     if (!down && pegInfo.kv.panStop) {
  163.     pegInfo.kv.timer[ JOYEWREPEAT ].key = -1;
  164.     pegInfo.kv.timer[ JOYEWREPEAT ].delay = 0;
  165.     pegInfo.kv.timer[ JOYNSREPEAT ].key = -1;
  166.     pegInfo.kv.timer[ JOYNSREPEAT ].delay = 0;
  167.     debug11(("\n"));
  168.     return;
  169.     }
  170.  
  171.     if ((down & JOYEWMASK) == 0) {
  172.     if ((pegInfo.kv.timer[ JOYEWREPEAT ].delay
  173.       -= pegInfo.kv.panInertia) < 0) {
  174.         pegInfo.kv.timer[ JOYEWREPEAT ].key = -1;
  175.         pegInfo.kv.timer[ JOYEWREPEAT ].delay = 0;
  176.     } else {
  177.         if (pegInfo.kv.timer[ JOYEWREPEAT ].key == KBJoyRight)
  178.         down |= JOYRIGHT_BIT;
  179.         else
  180.         down |= JOYLEFT_BIT;
  181.     }
  182.     }
  183.  
  184.     if ((down & JOYNSMASK) == 0) {
  185.     if ((pegInfo.kv.timer[ JOYNSREPEAT ].delay
  186.       -= pegInfo.kv.panInertia) < 0) {
  187.         pegInfo.kv.timer[ JOYNSREPEAT ].key = -1;
  188.         pegInfo.kv.timer[ JOYNSREPEAT ].delay = 0;
  189.     } else {
  190.         if (pegInfo.kv.timer[ JOYNSREPEAT ].key == KBJoyUp)
  191.         down |= JOYUP_BIT;
  192.         else
  193.         down |= JOYDOWN_BIT;
  194.     }
  195.     }
  196.  
  197. #ifdef    UTEK
  198.     /*
  199.      * Now see if the X panning can change.
  200.      */
  201.     GetViewport(&newViewPort);
  202.     if (down & (JOYLEFT_BIT|JOYRIGHT_BIT)) {
  203.     /*
  204.      * Figure out the next timeout ASAP.
  205.      */
  206.     delay = pegInfo.kv.timer[ JOYEWREPEAT ].delay;
  207.     if (JoyEWDepressed) {
  208.         if (++pegInfo.kv.timer[ JOYEWREPEAT ].delay
  209.           >= pegInfo.kv.nPanDelays)
  210.         pegInfo.kv.timer[ JOYEWREPEAT ].delay =
  211.             pegInfo.kv.nPanDelays - 1;
  212.     }
  213.     pegInfo.kv.timer[ JOYEWREPEAT ].when =
  214.         dsp->ds_ltime + pegInfo.kv.panDelays[ delay ];
  215.  
  216.     /*
  217.      * ... and then compute delta x.
  218.      */
  219.     deltax = pegInfo.kv.panDeltaX[ pegInfo.kv.timer[ JOYEWREPEAT ].delay ];
  220.     if (down & JOYLEFT_BIT)
  221.         deltax = -deltax;
  222.     x = newViewPort.x + deltax;
  223.     if (x < 0)
  224.         x = 0;
  225.     else if (x > dsp->ds_panxmax)
  226.         x = dsp->ds_panxmax;
  227.     if (newViewPort.x != x) {
  228.         deltax = x - newViewPort.x;
  229.         newViewPort.x = x;
  230.     } else {
  231.         deltax = 0;
  232.         pegInfo.kv.timer[ JOYEWREPEAT ].key = -1;
  233.         pegInfo.kv.timer[ JOYEWREPEAT ].delay = 0;
  234.     }
  235.     } else {
  236.     x = newViewPort.x;
  237.     deltax = 0;
  238.     }
  239. #endif    /* UTEK */
  240.  
  241. #ifdef    UTEKV
  242.     /*
  243.      * Now see if the X axis panning buttons can change.
  244.      */
  245.     if (down & (JOYLEFT_BIT|JOYRIGHT_BIT)) {
  246.     /*
  247.      * Figure out the next timeout ASAP.
  248.      */
  249.     delay = pegInfo.kv.timer[ JOYEWREPEAT ].delay;
  250.     if (JoyEWDepressed) {
  251.         if (++pegInfo.kv.timer[ JOYEWREPEAT ].delay
  252.           >= pegInfo.kv.nPanDelays)
  253.         pegInfo.kv.timer[ JOYEWREPEAT ].delay =
  254.             pegInfo.kv.nPanDelays - 1;
  255.     }
  256.     pegInfo.kv.timer[ JOYEWREPEAT ].when =
  257.         dsp->ds_ltime + pegInfo.kv.panDelays[ delay ];
  258.     }
  259. #endif    /* UTEKV */
  260.  
  261. #ifdef    UTEK
  262.     /*
  263.      * ... and see if the Y panning can change.
  264.      */
  265.     if (down & (JOYUP_BIT|JOYDOWN_BIT)) {
  266.     /*
  267.      * Figure out the next timeout ASAP.
  268.      */
  269.     delay = pegInfo.kv.timer[ JOYNSREPEAT ].delay;
  270.     if (JoyNSDepressed) {
  271.         if (++pegInfo.kv.timer[ JOYNSREPEAT ].delay
  272.          >= pegInfo.kv.nPanDelays)
  273.         pegInfo.kv.timer[ JOYNSREPEAT ].delay =
  274.             pegInfo.kv.nPanDelays - 1;
  275.     }
  276.     pegInfo.kv.timer[ JOYNSREPEAT ].when =
  277.         dsp->ds_ltime + pegInfo.kv.panDelays[ delay ];
  278.  
  279.     /*
  280.      * And compute delta y.
  281.      */
  282.     deltay = pegInfo.kv.panDeltaY[ pegInfo.kv.timer[ JOYNSREPEAT ].delay ];
  283.     if (down & JOYUP_BIT)
  284.         deltay = -deltay;
  285.     y = newViewPort.y + deltay;
  286.     if (y < 0)
  287.         y = 0;
  288.     else if (y > dsp->ds_panymax)
  289.         y = dsp->ds_panymax;
  290.     if (newViewPort.y != y) {
  291.         deltay = y - newViewPort.y;
  292.         newViewPort.y = y;
  293.     } else {
  294.         deltay = 0;
  295.         pegInfo.kv.timer[ JOYNSREPEAT ].key = -1;
  296.         pegInfo.kv.timer[ JOYNSREPEAT ].delay = 0;
  297.         if (deltax == 0)
  298.         return;
  299.     }
  300.     } else {
  301.     deltay = 0;
  302.     }
  303.  
  304.     debug11((" pan-->(%d,%d)", newViewPort.x, newViewPort.y));
  305.     SetViewport(&newViewPort);
  306. #endif    /* UTEK */
  307.  
  308. #ifdef    UTEKV
  309.     /*
  310.      * Now see if the Y axis panning buttons can change.
  311.      */
  312.     if (down & (JOYUP_BIT|JOYDOWN_BIT)) {
  313.     /*
  314.      * Figure out the next timeout ASAP.
  315.      */
  316.     delay = pegInfo.kv.timer[ JOYNSREPEAT ].delay;
  317.     if (JoyNSDepressed) {
  318.         if (++pegInfo.kv.timer[ JOYNSREPEAT ].delay
  319.          >= pegInfo.kv.nPanDelays)
  320.         pegInfo.kv.timer[ JOYNSREPEAT ].delay =
  321.             pegInfo.kv.nPanDelays - 1;
  322.     }
  323.     pegInfo.kv.timer[ JOYNSREPEAT ].when =
  324.         dsp->ds_ltime + pegInfo.kv.panDelays[ delay ];
  325.     }
  326. #endif    /* UTEK */
  327. }
  328.  
  329. static void
  330. SetupTimeout()
  331. {
  332.     register XDisplayState  *dsp = pegInfo.dsp;
  333.     register    i, when = 0;
  334.  
  335.     for (i=0; i<N_TIMERS; i++) {
  336.     if (pegInfo.kv.timer[ i ].key >= 0) {
  337.         if (pegInfo.kv.timer[ i ].when < dsp->ds_ltime)
  338.         pegInfo.kv.timer[ i ].when = dsp->ds_ltime;
  339.         if (when == 0 || pegInfo.kv.timer[ i ].when < when)
  340.         when = pegInfo.kv.timer[ i ].when;
  341.     }
  342.     }
  343.  
  344.     dsp->ds_timeout = when;
  345. }
  346.  
  347. static void
  348. SetupRepeat(c)
  349.     register    c;
  350. {
  351.     register XDisplayState  *dsp = pegInfo.dsp;
  352.     Bool    somethingRepeated = False;
  353.     register    mappedKey;
  354.  
  355.     debug10(("repeat 0x%x\n", c));
  356.     switch(c) {
  357.     case KBJoyLeft:
  358.     case KBJoyRight:
  359.     if (! pegInfo.kv.panEnabled)
  360.         goto normalRepeat;
  361.     if (pegInfo.kv.timer[ JOYEWREPEAT ].key == c) {
  362.         debug10(("0x%x already repeating\n",
  363.         c, pegInfo.kv.timer[ JOYEWREPEAT ].key));
  364.         break;
  365.     }
  366.     pegInfo.kv.joyState |= JoyKeyToMask(c);
  367.     pegInfo.kv.timer[ JOYEWREPEAT ].delay = 0; /* an index */
  368.     pegInfo.kv.timer[ JOYEWREPEAT ].key = c;
  369.     pegInfo.kv.timer[ JOYEWREPEAT ].when = dsp->ds_ltime;
  370.     somethingRepeated = True;
  371. #ifdef XPEG_TANDEM
  372.     if (pegTandem)
  373.         break;
  374. #endif /* XPEG_TANDEM */
  375.     pan4405();
  376.     break;
  377.     case KBJoyUp:
  378.     case KBJoyDown:
  379.     if (! pegInfo.kv.panEnabled)
  380.         goto normalRepeat;
  381.     if (pegInfo.kv.timer[ JOYNSREPEAT ].key == c) {
  382.         debug10(("0x%x already repeating\n",
  383.         c, pegInfo.kv.timer[ JOYNSREPEAT ].key));
  384.         break;
  385.     }
  386.     pegInfo.kv.joyState |= JoyKeyToMask(c);
  387.     pegInfo.kv.timer[ JOYNSREPEAT ].delay = 0; /* an index */
  388.     pegInfo.kv.timer[ JOYNSREPEAT ].key = c;
  389.     pegInfo.kv.timer[ JOYNSREPEAT ].when = dsp->ds_ltime;
  390.     somethingRepeated = True;
  391. #ifdef XPEG_TANDEM
  392.     if (pegTandem)
  393.         break;
  394. #endif /* XPEG_TANDEM */
  395.     pan4405();
  396.     break;
  397.     default:
  398. normalRepeat:
  399.     if (pegInfo.kv.timer[ KEYREPEAT ].key == c) {
  400.         debug10(("0x%x already repeating\n",
  401.         c, pegInfo.kv.timer[ KEYREPEAT ].key));
  402.         break;
  403.     }
  404.  
  405.     /*
  406.      * If there is already a key repeating, then push it on the stack.
  407.      */
  408.     if (pegInfo.kv.timer[ KEYREPEAT ].key >= 0) {
  409.         pegInfo.kv.keyStack[ pegInfo.kv.keyTOS++ ] =
  410.         pegInfo.kv.timer[ KEYREPEAT ].key;
  411.         pegInfo.kv.timer[ KEYREPEAT ].key = -1;
  412.     }
  413.     /*
  414.      * The protocol says it is desirable that keys being
  415.      * used as modifiers not auto-repeat, reguardless of
  416.      * their auto-repeat setting.
  417.      */
  418.     mappedKey = c+MINKEYCODE;
  419.     if (! AutoRepeatOn()
  420.      || ! KeyRepeatable(mappedKey)
  421.      || KeyIsModifier(mappedKey))
  422.         break;
  423.  
  424.     pegInfo.kv.timer[ KEYREPEAT ].key = c;
  425.     pegInfo.kv.timer[ KEYREPEAT ].delay = 0; /* an index */
  426.     pegInfo.kv.timer[ KEYREPEAT ].when =
  427.         dsp->ds_ltime + pegInfo.kv.keyDelays[ 0 ];
  428.     somethingRepeated = True;
  429.     break;
  430.     }
  431.  
  432.     if (somethingRepeated)
  433.     SetupTimeout();
  434. }
  435.  
  436. /*
  437.  * Pop the previous key being repeated off the stack and start
  438.  * repeating it.
  439.  * We are guarenteed that pegInfo.kv.keyTOS > 0.
  440.  */
  441. static void
  442. PopKeyRepeat(c)
  443.     register    c;
  444. {
  445.     int i;
  446.  
  447.     /*
  448.      * First, we purge the key stack of the key that just
  449.      * got released... it may not be the top of stack.
  450.      */
  451.     debug10(("poprepeat stack: "));
  452.     for (i=0; i<pegInfo.kv.keyTOS; i++)
  453.     if (pegInfo.kv.keyStack[ i ] == c) {
  454.         pegInfo.kv.keyStack[ i ] = -1;
  455.         debug10((" (drop 0x%x)", c));
  456.     } else
  457.         debug10((" 0x%x", pegInfo.kv.keyStack[ i ]));
  458.  
  459.     /*
  460.      * If the key just released is NOT the one being repeated, we simply
  461.      * let the repeat continue...
  462.      */
  463.     if (pegInfo.kv.timer[ KEYREPEAT ].key != -1) {
  464.     if (pegInfo.kv.timer[ KEYREPEAT ].key == c)
  465.         pegInfo.kv.timer[ KEYREPEAT ].key = -1; /* cancel the repeat */
  466.     else {
  467.         debug10((" allow 0x%x to continue\n",
  468.         pegInfo.kv.timer[ KEYREPEAT ].key));
  469.         return;
  470.     }
  471.     }
  472.  
  473.     /*
  474.      * Now peruse from TOS down, looking for a key to repeat.
  475.      */
  476.     do {
  477.     c = pegInfo.kv.keyStack[ --pegInfo.kv.keyTOS ];
  478.     if (c >= 0) {
  479.         SetupRepeat(c);
  480.         return;
  481.     }
  482.     } while (pegInfo.kv.keyTOS > 0);
  483.     debug10((" stack now empty\n"));
  484. }
  485.  
  486. static void
  487. SetLockLED(on)
  488.     Bool on;
  489. {
  490.     if (on)
  491.     ioctl(pegInfo.eventFd, CE_SHIFTLOCKON, 0);
  492.     else
  493.     ioctl(pegInfo.eventFd, CE_SHIFTLOCKOFF, 0);
  494. }
  495.  
  496. void
  497. SetComposeLED(on)
  498.     Bool on;
  499. {
  500.     if (on || (KanaShiftState == True))
  501.     /* if we are in kana mode, keep ComposeLED on when Compose goes off */
  502.     ioctl(pegInfo.eventFd, KEYBD_COMPOSE_LED_ON, 0);
  503.     else
  504.     ioctl(pegInfo.eventFd, KEYBD_COMPOSE_LED_OFF, 0);
  505. }
  506.  
  507. #ifdef    UTEKV
  508.  
  509. #ifndef    NBPC
  510. #define    NBPC    (4096)    /* number of bytes per click (page) */
  511. #endif    /* NBPC */
  512.  
  513. #ifndef    NCPS
  514. #define    NCPS    (1024)    /* number of clicks (pages) per segment */
  515. #endif    /* NCPS */
  516.  
  517. #ifndef    NBPS
  518. #define    NBPS    (NBPC*NCPS)    /* number of bytes per segment */
  519. #endif    /* NBPS */
  520.  
  521. typedef struct _eventqueueFAKE {
  522.     unsigned long offset;        /* input event buffer KVA - offset */
  523.     int size;            /* size of event buffer */
  524.     int head;            /* index into events */
  525.     int tail;            /* index into events */
  526. } EventQueueFAKE;
  527.  
  528.  
  529. /* notes only --- */
  530. /* #define PHYS_HIGH_ADDR (0x60000000) * Servers Virtual Address "offset" from
  531.                        * the physical address. An area
  532.                        * between u and display board.
  533.                        */
  534. #endif    /* UTEKV */
  535.  
  536. long
  537. GetTimeInMillis ()
  538. {
  539.     long   ret = 0;
  540. #ifdef __STDC__
  541.     volatile long  *addr;
  542. #else
  543.     long   *addr;
  544. #endif
  545.  
  546.     if (pegInfo.dsp)
  547.     {
  548.     addr = (long *) &pegInfo.dsp->ds_ltime;
  549.     ret = *addr;
  550.     if (ret == 0)
  551.         ret = *addr;
  552.     }
  553.     return ret;
  554. }
  555.  
  556. void
  557. ProcessInputEvents ()
  558. {
  559.     register Event *ev;
  560.     register u_long c, i;
  561.     register EventQueue *queue = (EventQueue *)pegInfo.pPointer->devicePrivate;
  562.     xEvent  x;
  563.     u_long  nowInCentiSecs, nowInMilliSecs;
  564.     u_long  eventInCentiSecs, eventInMilliSecs;
  565.     int qLimit;
  566.     int count;
  567.     KeyCode keycode;
  568.     KeySym *keysyms;
  569.     int j;
  570.  
  571. #ifdef    UTEKV
  572.     /*
  573.      * Event "queue" address corrected to point
  574.      * to the Kernal Virtual Address.
  575.      */
  576.     register EventQueueFAKE *queueKVA;
  577.     register EventQueue *queueSVA;
  578.     register caddr_t sparePtr;        /* a spare pointer for address calc. */
  579.     register caddr_t SVAbase;        /* server virtual address segment base*/
  580.     register Event *evSVA;   /* server virtual address of -the- current event */
  581.  
  582. #endif    /* UTEKV */
  583.  
  584. #ifdef XPEG_TANDEM
  585.     if (pegTandem)
  586.     pegReadEvents(queue);
  587. #endif /* XPEG_TANDEM */
  588.  
  589.     i = queue->head;
  590.     qLimit = queue->size - 1;
  591.  
  592.     while (i != queue->tail) {
  593.     if (screenIsSaved == SCREEN_SAVER_ON)
  594.         SaveScreens(SCREEN_SAVER_OFF, ScreenSaverReset);
  595.  
  596. #ifdef    UTEK
  597.     ev = &queue->events[i];
  598. #endif    /* UTEKV */
  599.  
  600. #ifdef    UTEKV
  601.     /*
  602.      * The servers mapping (Server Virtual Address - SVA) of the event
  603.      * queue is at 0x60000000 + the physical address of the shared
  604.      * region is typically 0x24a000 or 0x24ac000. Which is currently
  605.      * the same as the kernel virtual mapping. The event queue contains
  606.      * kernel virtual addresses (KVA) in the
  607.      * "EventQueue ds_q  entry -> Event *events" pointer.
  608.      *
  609.      * Sooo.. I must contruct a pointer to the queue and then calculate
  610.      * the address of the event by adding the 0x6...0 value to the
  611.      * KVA as though it were an offset.
  612.      */
  613. #ifdef notdef
  614.         /*
  615.          * Make a copy of the servers pointer
  616.          * to the dsp.ds_q entry.
  617.          *
  618.          * Now add the SVA offset to the
  619.          * segment base address to compute the
  620.          * "real" address.
  621.          */
  622.     sparePtr = (caddr_t) (((unsigned long) queue)
  623.                 + ((unsigned long) SVAbase));
  624.  
  625.     sparePtr = (caddr_t) (((unsigned long)
  626.                 sparePtr) & (~(NBPS-1))); /*prune segment*/
  627.     SVAbase = (caddr_t) (((unsigned long) SVAbase)
  628.                 + ((unsigned long) sparePtr));
  629. #endif    /* notdef */
  630.  
  631.     SVAbase = (caddr_t) ((unsigned long)queue & (~(NBPS-1)));
  632.                 /* strip off the lower part of the
  633.                  * address to get the segment address.
  634.                  */
  635.  
  636.         /*
  637.          * Now fetch the KVA and prune the segment base address
  638.          * to the event queue kernel virtual address to compute
  639.          * the "real" address in SVA space.
  640.          */
  641.     queueKVA = (EventQueueFAKE *) queue;    /* make "FAKE" queue ptr */
  642.  
  643.     sparePtr = (caddr_t) ((unsigned long) ((unsigned long) SVAbase) +
  644.                 ((unsigned long) queueKVA->offset));
  645.             /* add the KVA as though it were just an offset */
  646.  
  647.     sparePtr = (caddr_t) ((unsigned long) ((unsigned long) sparePtr)
  648.                     + ((sizeof (Event)) * i));
  649.  
  650.     ev = (Event *)sparePtr;
  651.  
  652. #endif    /* UTEKV */
  653.  
  654.  
  655. #ifndef MOTION_BUFFER_OFF
  656.     if ((ev->e_type != E_MMOTION) &&
  657.       (pegInfo.motionQueue->mouseMoved == TRUE)) {
  658.         gfbSendMotionEvent(pegInfo.pPointer);
  659.     }
  660. #endif /* MOTION_BUFFER_OFF */
  661.  
  662.     anotherEvent:
  663.     debug5(("(%d,%d) %s",
  664.         ev->e_x, ev->e_y, type[ ev->e_type&0x7 ]));
  665.  
  666.     x.u.keyButtonPointer.rootX = ev->e_x;
  667.     x.u.keyButtonPointer.rootY = ev->e_y;
  668.     /*
  669.      * The following silly looking code is because the old
  670.      * version of the driver only delivers 16 bits worth of
  671.      * centiseconds. We are supposed to be keeping time in
  672.      * terms of 32 bits of milliseconds.  Fortunately, this
  673.      * is available in shared space.
  674.      */
  675.     nowInMilliSecs = pegInfo.dsp->ds_ltime;
  676.     nowInCentiSecs = (nowInMilliSecs / 10);
  677.     eventInCentiSecs = ev->e_time;
  678.     eventInCentiSecs = (nowInCentiSecs & 0xFFFF0000) |
  679.                    (eventInCentiSecs & 0x0000FFFF);
  680.     if (eventInCentiSecs > nowInCentiSecs)
  681.         eventInCentiSecs -= 0x10000;
  682.     x.u.keyButtonPointer.time = eventInCentiSecs * 10;
  683.     switch (ev->e_type) {
  684.     case E_BUTTON:
  685.         debug5((" @%d 0x%-2x %s %s\n",
  686.         ev->e_time,
  687.         ev->e_key,
  688.         direction[ ev->e_direction&0x3 ],
  689.         device[ ev->e_device&0x7 ]));
  690.         switch (ev->e_device) {
  691.         case E_MOUSE:
  692.         x.u.u.type = (ev->e_direction == E_KBTUP) ?
  693.             ButtonRelease : ButtonPress;
  694.         x.u.u.detail = ev->e_key;
  695. #ifdef  XTESTEXT1
  696.         /*
  697.          * If we are stealing input, then send info to the
  698.          * input extension.
  699.          */
  700.         if (!on_steal_input ||
  701.             XTestStealKeyData(x.u.u.detail, x.u.u.type,
  702.                 0,    /* dev_type: used for multiple input devices */
  703.                 x.u.keyButtonPointer.rootX,
  704.                 x.u.keyButtonPointer.rootY))
  705. #endif  /* XTESTEXT1 */
  706.         (*pegInfo.pPointer->processInputProc)(&x, pegInfo.pPointer, 1);
  707.         break;
  708.  
  709.         case E_AUX:
  710.         {
  711.         register XDisplayState  *dsp;
  712.  
  713.         dsp = pegInfo.dsp;
  714.         if ((pegInfo.kv.timer[ JOYNSREPEAT ].key >= 0
  715.          && pegInfo.kv.timer[ JOYNSREPEAT ].when <= dsp->ds_ltime)
  716.         || (pegInfo.kv.timer[ JOYEWREPEAT ].key >= 0
  717.          && pegInfo.kv.timer[ JOYEWREPEAT ].when <= dsp->ds_ltime))
  718.             pan4405();
  719.  
  720.         if (pegInfo.kv.timer[ KEYREPEAT ].key >= 0
  721.          && pegInfo.kv.timer[ KEYREPEAT ].when <= dsp->ds_ltime)
  722.             ev = RepeatKey();
  723.         else
  724.             ev = NULL;
  725.  
  726.         SetupTimeout();
  727.         if (ev)
  728.             goto anotherEvent;
  729.         break;
  730.         }
  731.  
  732.         case E_DKB:
  733.         x.u.u.detail = ev->e_key + MINKEYCODE;
  734.         c = ev->e_key;
  735.         /*
  736.          * Hack attack!  If you hold down various key combinations,
  737.          * and then hit break, special things will happen...
  738.          */
  739.         if (c == KBBreak && ev->e_direction == E_KBTDOWN) {
  740. #ifdef XDEBUG
  741.             /*
  742.              * If you hold down the first four function keys
  743.              * and hit break, the server will exit.
  744.              */
  745.             if (RawKeyDepressed(KBF1)
  746.              && RawKeyDepressed(KBF2)
  747.              && RawKeyDepressed(KBF3)
  748.              && RawKeyDepressed(KBF4)) {
  749.             debug6(("Bailing out\n"));
  750.             exit(1);
  751.             }
  752.             /*
  753.              * If you hold down the left shift, the control key,
  754.              * a single letter and hit break, the debug bit
  755.              * corresponding to the letter (a=0, b=1, c=2, d=4, e=8...)
  756.              * will be toggled.
  757.              */
  758.             if (RawKeyDepressed(KBCtrl) && RawKeyDepressed(KBShiftL)) {
  759.             int    key;
  760.             x_debug("debug key... ");
  761.             for (key=KBA+MINKEYCODE; key<=KBZ+MINKEYCODE; key++) {
  762.                 if (KeyDepressed(key)) {
  763.                 key -= KBA+MINKEYCODE;
  764.                 xflg_debug = (xflg_debug & (~(1<<key)))
  765.                     | ((~xflg_debug) & (1<<key));
  766.                 x_debug("debug%d %s\n", key,
  767.                     xflg_debug & (1<<key) ? "set" : "cleared");
  768.                 break;
  769.                 }
  770.             }
  771.             }
  772. #endif /* XDEBUG */
  773.         }
  774.         switch (ev->e_direction) {
  775.         case E_KBTUP:
  776.             x.u.u.type = KeyRelease;
  777.             if (pegInfo.kv.keyTOS > 0)
  778.             PopKeyRepeat((int)c);
  779.             else
  780.             pegInfo.kv.timer[ KEYREPEAT ].key = -1;
  781.             break;
  782.         case E_KBTDOWN:
  783.             x.u.u.type = KeyPress;
  784.             if (pegInfo.kv.timer[ KEYREPEAT ].key != c)
  785.             SetupRepeat((int)c);
  786.             break;
  787.         }
  788.  
  789.         /*
  790.          * don't ever deliver joypad events unless panning is
  791.          * enabled.
  792.          */
  793.         if (pegInfo.kv.panEnabled
  794.         && (c == KBJoyLeft
  795.          || c == KBJoyRight
  796.          || c == KBJoyUp
  797.          || c == KBJoyDown)) {
  798.             if (ev->e_direction == E_KBTUP)
  799.             pegInfo.kv.joyState &= ~JoyKeyToMask(c);
  800.             break;
  801.         }
  802.  
  803.         /*
  804.          * Implement a toggle for the caps lock led no matter
  805.          * what key is the caps lock. XXX this means no caps shift!
  806.          * If more than one lock key, we're in trouble!!!
  807.          */
  808.         if (KeyIsModifier(x.u.u.detail) & LockMask) {
  809.             if (ev->e_direction == E_KBTDOWN)
  810.             if (KeyDepressed(x.u.u.detail))
  811.                 break; /* don't deliver the event */
  812.             else
  813.                 SetLockLED(True);
  814.             else /* E_KBTUP */
  815.             if (pegInfo.kv.readyToUnlock) {
  816.                 SetLockLED(False);
  817.                 pegInfo.kv.readyToUnlock = False;
  818.             } else {
  819.                 pegInfo.kv.readyToUnlock = True;
  820.                 break; /* don't deliver the event */
  821.             }
  822.         }
  823.  
  824. #ifdef SERVER_COMPOSE
  825.         /* process Compose sequences; toss keycode if a match */
  826.         /* Note that this bypasses the test code */
  827.         if (CheckForCompose(x, (DeviceIntPtr) pegInfo.pKeyboard))
  828.             break;
  829. #endif /* SERVER_COMPOSE */
  830.  
  831. #ifdef USE_KANA_STUFF
  832.         /* process Katakana shift state */
  833.         count = GetCurKeySymList(x.u.u.detail, &keysyms);
  834.         for (j = 0; j < count; j++) {
  835.             switch (keysyms[j]) {
  836.             case XK_Kana_Lock:
  837.             /* its generic, so why not do it for everyone? */
  838.             case XK_Mode_switch:
  839.             if (x.u.u.type == KeyPress) {
  840.                 if (!NumKanaLocks++ && KanaLocked)
  841.                 readyToUnlock = True;
  842.                 /* unlock when NumKanaLocks goes to zero */
  843.                 if (KanaLocked)
  844.                 /* trickery; toss all lock key events except
  845.                    first/last press/release */
  846.                 goto big_break; /* ignore event; no chg */
  847.                 else {
  848.                 /* act only when first of N locks goes down
  849.                    and no shift is down.  Save initial 
  850.                    Lock keycode for release */
  851.                 if (NumKanaLocks == 1) {
  852.                     InitialKanaLockKey = x.u.u.detail;
  853.                     KanaLocked = True;
  854.                     if (!NumKanaShifts) {
  855.                     KanaShiftState = True;
  856.                     SetComposeLED(KanaShiftState);
  857.                 /* Note that compose and kana are separate
  858.                 states, and the LED is turned on for either. */
  859.                     }
  860.                 }
  861.                 }
  862.             }
  863.             else {
  864.                 /* act only when the last of the locks pressed
  865.                    while locked goes up, and no shift is down */
  866.                 if (--NumKanaLocks)
  867.                 goto big_break; /* ignore event; no chg */
  868.                 else {  /* all lock keys up */
  869.                 if (readyToUnlock) {
  870.                     x.u.u.detail = InitialKanaLockKey;
  871.                     KanaLocked = readyToUnlock = False;
  872.                     if (!NumKanaShifts) {
  873.                     KanaShiftState = False;
  874.                     SetComposeLED(KanaShiftState);
  875.                     }
  876.                 }
  877.                 else
  878.                     goto big_break; /* ignore event; no chg */
  879.                 }
  880.             }
  881.             goto little_break;
  882.             case XK_Kana_Shift:
  883.             /* set/unset kana state to inverse of lock
  884.                when first/last shift released/depressed */
  885.             if (x.u.u.type == KeyPress) {
  886.                 if (!NumKanaShifts++) {
  887.                 if (KanaLocked)
  888.                     KanaShiftState = False;
  889.                 else
  890.                     KanaShiftState = True;
  891.                 SetComposeLED(KanaShiftState);
  892.                 }
  893.             }
  894.             else    /* KeyRelease */
  895.                 if (!--NumKanaShifts) {
  896.                 if (KanaLocked)
  897.                     KanaShiftState = True;
  898.                 else
  899.                     KanaShiftState = False;
  900.                 SetComposeLED(KanaShiftState);
  901.                 }
  902.             goto little_break;
  903.             default:    /* no Kana keysym, keep looking */
  904.             break;
  905.             }
  906.         }
  907. /* ignore multiple Kana keysyms on one key */
  908. little_break:
  909.  
  910.         /* if Kana shift is on, convert kana keycodes to kana region. */
  911.         if (KanaShiftState) {
  912.         keycode = x.u.u.detail + KANA_OFFSET;
  913.         /* don't send Kana keycode if it has no keysym! */
  914.         if (GetCurKeySymList(keycode, &keysyms))
  915.             x.u.u.detail = keycode;
  916.         }
  917. #endif /* USE_KANA_STUFF */
  918.  
  919. #ifdef  XTESTEXT1
  920.         /*
  921.          * If we are stealing input, then send info to the
  922.          * input extension.
  923.          */
  924.         if (!on_steal_input ||
  925.             XTestStealKeyData(x.u.u.detail, x.u.u.type, 
  926.                 0,    /* dev_type: useful for multiple keyboards */
  927.                 x.u.keyButtonPointer.rootX,
  928.                 x.u.keyButtonPointer.rootY))
  929. #endif  /* XTESTEXT1 */
  930.         (*pegInfo.pKeyboard->processInputProc)(&x, pegInfo.pKeyboard, 1);
  931.         break;
  932.         }
  933.         break;
  934.  
  935.     case E_MMOTION:
  936.         debug5(("\n"));
  937.         x.u.u.type = MotionNotify;
  938. #ifdef XTESTEXT1
  939.         if (on_steal_input) {
  940.         XTestStealMotionData
  941.             (x.u.keyButtonPointer.rootX - LastMousePosition.x,
  942.             x.u.keyButtonPointer.rootY - LastMousePosition.y,
  943.             0,    /* dev_type: useful for multiple input devices */
  944.             x.u.keyButtonPointer.rootX,
  945.             x.u.keyButtonPointer.rootY);
  946.     }
  947.     LastMousePosition.x = x.u.keyButtonPointer.rootX;
  948.     LastMousePosition.y = x.u.keyButtonPointer.rootY;
  949. #endif  /* XTESTEXT1 */
  950.  
  951. #ifndef MOTION_BUFFER_OFF
  952.         gfbSaveMotionEvent(&x);
  953. #else /* MOTION_BUFFER_OFF */
  954.         (*pegInfo.pPointer->processInputProc)(&x, pegInfo.pPointer, 1);
  955. #endif /* MOTION_BUFFER_OFF */
  956.         break;
  957.     }
  958. big_break:
  959.  
  960.     if (i == qLimit)
  961.         i = queue->head = 0;
  962.     else
  963.         i = ++queue->head;
  964.     }
  965.  
  966. #ifndef MOTION_BUFFER_OFF
  967.     /*
  968.      * At end of processing input buffer if mouseMoved, then send
  969.      * last motion event.
  970.      */
  971.     if (pegInfo.motionQueue->mouseMoved == TRUE) {
  972.         gfbSendMotionEvent(pegInfo.pPointer);
  973.     }
  974. #endif /* MOTION_BUFFER_OFF */
  975. }
  976.  
  977. #ifdef XPEG_TANDEM
  978.  
  979. static
  980. pegReadEvents(queue)
  981.     EventQueue *queue;
  982. {
  983.     int n, mask, index, cnt, red;
  984.     Event   *ep;
  985.     struct timeval  timeout;
  986.     extern int  errno;
  987.     extern int  pegDevFd;
  988.  
  989.     timeout.tv_usec = 100000;
  990.     timeout.tv_sec = 0;
  991.  
  992.     errno = 0;
  993.     for (cnt = 0; ; cnt++) {
  994.     mask = 1<<pegDevFd;
  995.     n = select(pegDevFd+1, &mask, NULL, NULL, &timeout);
  996.     if (n == 0)
  997.         break;
  998.     if (n < 0) {
  999.         fprintf(stderr, "pegReadEvents: select %d, errno =%d\n",
  1000.         n, errno);
  1001.         exit(1);
  1002.     }
  1003.     ep = &queue->events[ queue->tail ];
  1004.     red = read(pegDevFd, ep, sizeof(Event));
  1005.     if (red < sizeof(Event)) {
  1006.         fprintf(stderr, "pegReadEvents: red %d,errno=%d,sel=%d\n",
  1007.         red, errno, n);
  1008.         exit(1);
  1009.     }
  1010.     /*
  1011.      * fprintf(stderr, "server:(%d,%d),time=0x%x,type=%s,",
  1012.      *  ep->e_x,
  1013.      *  ep->e_y,
  1014.      *  ep->e_time,
  1015.      *  type[ ep->e_type & 0x7 ]);
  1016.      * fprintf(stderr, "key=0x%x,dir=%s,dev=%s\n",
  1017.      *  ep->e_key,
  1018.      *  direction[ ep->e_direction & 0x3],
  1019.      *  device[ ep->e_device & 0x7 ]);
  1020.      */
  1021.  
  1022.      index = queue->tail + 1;
  1023.      if (index >= queue->size)
  1024.         index = 0;
  1025.      if (index != queue->head)
  1026.         queue->tail = index;
  1027.     }
  1028. }
  1029.  
  1030. #endif /* XPEG_TANDEM */
  1031.  
  1032. void
  1033. InitKeybdState()
  1034. {
  1035.     KanaLocked = readyToUnlock = KanaShiftState = False;
  1036.     NumKanaLocks = NumKanaShifts = 0;
  1037. }
  1038.