home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / macII / macIIIo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-14  |  7.2 KB  |  254 lines

  1. /************************************************************ 
  2. Copyright 1988 by Apple Computer, Inc, Cupertino, California
  3.             All Rights Reserved
  4.  
  5. Permission to use, copy, modify, and distribute this software
  6. for any purpose and without fee is hereby granted, provided
  7. that the above copyright notice appear in all copies.
  8.  
  9. APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS,
  10. OR IMPLIED, WITH RESPECT TO THIS SOFTWARE, ITS QUALITY,
  11. PERFORMANCE, MERCHANABILITY, OR FITNESS FOR A PARTICULAR
  12. PURPOSE. AS A RESULT, THIS SOFTWARE IS PROVIDED "AS IS,"
  13. AND YOU THE USER ARE ASSUMING THE ENTIRE RISK AS TO ITS
  14. QUALITY AND PERFORMANCE. IN NO EVENT WILL APPLE BE LIABLE 
  15. FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  16. DAMAGES RESULTING FROM ANY DEFECT IN THE SOFTWARE.
  17.  
  18. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE
  19. AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR
  20. IMPLIED.
  21.  
  22. ************************************************************/
  23. /*-
  24.  * macIIIo.c --
  25.  *    Functions to handle input from the keyboard and mouse.
  26.  *
  27.  * Copyright (c) 1987 by the Regents of the University of California
  28.  *
  29.  * Permission to use, copy, modify, and distribute this
  30.  * software and its documentation for any purpose and without
  31.  * fee is hereby granted, provided that the above copyright
  32.  * notice appear in all copies.  The University of California
  33.  * makes no representations about the suitability of this
  34.  * software for any purpose.  It is provided "as is" without
  35.  * express or implied warranty.
  36.  *
  37.  *
  38.  */
  39.  
  40. /************************************************************
  41. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  42.  
  43.                     All Rights Reserved
  44.  
  45. Permission  to  use,  copy,  modify,  and  distribute   this
  46. software  and  its documentation for any purpose and without
  47. fee is hereby granted, provided that the above copyright no-
  48. tice  appear  in all copies and that both that copyright no-
  49. tice and this permission notice appear in  supporting  docu-
  50. mentation,  and  that the names of Sun or MIT not be used in
  51. advertising or publicity pertaining to distribution  of  the
  52. software  without specific prior written permission. Sun and
  53. M.I.T. make no representations about the suitability of this
  54. software for any purpose. It is provided "as is" without any
  55. express or implied warranty.
  56.  
  57. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  58. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  59. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  60. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  61. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  62. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  63. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  64. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  65.  
  66. ********************************************************/
  67.  
  68. #include    "macII.h"
  69. #include    "opaque.h"
  70.  
  71. int            lastEventTime = 0;
  72. struct timeval    lastEventTimeTv;
  73. extern int      screenIsSaved;
  74. extern void    SaveScreens();
  75.  
  76. int        consoleFd = 0;
  77. #define    INPBUFSIZE    3*64
  78.  
  79. /*-
  80.  *-----------------------------------------------------------------------
  81.  * TimeSinceLastInputEvent --
  82.  *    Function used for screensaver purposes by the os module.
  83.  *
  84.  * Results:
  85.  *    The time in milliseconds since there last was any
  86.  *    input.
  87.  *
  88.  * Side Effects:
  89.  *    None.
  90.  *
  91.  *-----------------------------------------------------------------------
  92.  */
  93. int
  94. TimeSinceLastInputEvent()
  95. {
  96.     struct timeval    now;
  97.  
  98.     gettimeofday (&now, (struct timezone *)0);
  99.  
  100.     if (lastEventTime == 0) {
  101.     lastEventTimeTv = now;
  102.     lastEventTime = TVTOMILLI(now);
  103.     }
  104.     return TVTOMILLI(now) - lastEventTime;
  105. }
  106.  
  107. /*-
  108.  *-----------------------------------------------------------------------
  109.  * ProcessInputEvents --
  110.  *    Retrieve all waiting input events and pass them to DIX in their
  111.  *    correct chronological order. Only reads from the system pointer
  112.  *    and keyboard.
  113.  *
  114.  * Results:
  115.  *    None.
  116.  *
  117.  * Side Effects:
  118.  *    Events are passed to the DIX layer.
  119.  *
  120.  *-----------------------------------------------------------------------
  121.  */
  122. void
  123. ProcessInputEvents ()
  124. {
  125.     mieqProcessInputEvents ();
  126.     miPointerUpdate ();
  127. }
  128.  
  129. /*
  130.  *-----------------------------------------------------------------------
  131.  * macIIEnqueueEvents
  132.  *    When a SIGIO is received, read device hard events and
  133.  *    enqueue them using the mi event queue
  134.  */
  135.  
  136. macIIEnqueueEvents ()
  137. {
  138.     DevicePtr            pPointer;
  139.     DevicePtr            pKeyboard;
  140.     register PtrPrivPtr        ptrPriv;
  141.     register KbPrivPtr      kbdPriv;
  142.     enum {
  143.         NoneYet, Ptr, Kbd
  144.     }                       lastType = NoneYet; /* Type of last event */
  145.  
  146.     unsigned char macIIevents[INPBUFSIZE];
  147.     register unsigned char *me, *meL;
  148.     int         n;
  149.  
  150.     static int optionKeyUp = 1;
  151.  
  152. #define IS_OPTION_KEY(x)    (KEY_DETAIL(x) == 0x3a)
  153. #define IS_LEFT_ARROW_KEY(x)    (KEY_DETAIL(x) == 0x3b)
  154. #define IS_RIGHT_ARROW_KEY(x)    (KEY_DETAIL(x) == 0x3c)
  155. #define IS_DOWN_ARROW_KEY(x)    (KEY_DETAIL(x) == 0x3d)
  156. #define IS_UP_ARROW_KEY(x)    (KEY_DETAIL(x) == 0x3e)
  157. #define IS_ARROW_KEY(x)                        \
  158.     (IS_LEFT_ARROW_KEY(x) || IS_RIGHT_ARROW_KEY(x) ||     \
  159.      IS_DOWN_ARROW_KEY(x) || IS_UP_ARROW_KEY(x))
  160.  
  161.     pPointer = LookupPointerDevice();
  162.     pKeyboard = LookupKeyboardDevice();
  163.     if (!pPointer->on || !pKeyboard->on)
  164.     return;
  165.     ptrPriv = (PtrPrivPtr)pPointer->devicePrivate;
  166.     kbdPriv = (KbPrivPtr)pKeyboard->devicePrivate;
  167.  
  168.     while ((n = read (consoleFd, macIIevents,
  169.               INPBUFSIZE*sizeof macIIevents[0])) >= 0 ||
  170.         errno == ENODATA || errno == EWOULDBLOCK)
  171.     {
  172.     if (n <= 0)
  173.         break;
  174.  
  175.     meL = macIIevents + (n/(sizeof macIIevents[0]));
  176.  
  177.     for (me = macIIevents; me < meL; me++)
  178.     {
  179.             gettimeofday (&lastEventTimeTv, (struct timezone *)0);
  180.             lastEventTime = TVTOMILLI(lastEventTimeTv);
  181.     
  182.             /*
  183.              * Figure out the X device this event should be reported on.
  184.              */
  185.     
  186.         if (IS_OPTION_KEY(*me)) {
  187.             optionKeyUp = KEY_UP(*me);
  188.         }
  189.     
  190.         /*
  191.          * Patch up the event if the option key is down and an arrow key
  192.          * is hit in order to generate arrow key codes.
  193.          */
  194.     
  195.         if (!optionKeyUp && IS_ARROW_KEY(*me)) {
  196.             int keyUp = KEY_UP(*me);
  197.     
  198.             if (IS_RIGHT_ARROW_KEY(*me))
  199.             *me = 0x7b;
  200.             else if (IS_LEFT_ARROW_KEY(*me))
  201.             *me = 0x7c;
  202.             else if (IS_DOWN_ARROW_KEY(*me))
  203.             *me = 0x7d;
  204.             else
  205.             *me = 0x70;     /* code for UP arrow */
  206.             if (keyUp)
  207.             *me |= 0x80;
  208.         }
  209.     
  210.         if (KEY_DETAIL(*me) == MOUSE_ESCAPE) { 
  211.                 (* ptrPriv->EnqueueEvent) (pPointer,me);
  212.             me += 2;
  213.             lastType = Ptr;
  214.         }
  215.     
  216.         else if (IS_MOUSE_KEY(*me))
  217.             {
  218.                 (* ptrPriv->EnqueueEvent) (pPointer,me);
  219.             lastType = Ptr;
  220.         }
  221.     
  222.         else if (IS_OPTION_KEY(*me)) {
  223.             /* do nothing */
  224.         }
  225.     
  226.         else {
  227.                 (* kbdPriv->EnqueueEvent) (pKeyboard,me);
  228.             lastType = Kbd;
  229.             }
  230.     }
  231.     }
  232. }
  233.  
  234.  
  235. /*-
  236.  *-----------------------------------------------------------------------
  237.  * SetTimeSinceLastInputEvent --
  238.  *    Set the lastEventTime to now.
  239.  *
  240.  * Results:
  241.  *    None.
  242.  *
  243.  * Side Effects:
  244.  *    lastEventTime is altered.
  245.  *
  246.  *-----------------------------------------------------------------------
  247.  */
  248. void
  249. SetTimeSinceLastInputEvent()
  250. {
  251.     gettimeofday (&lastEventTimeTv, (struct timezone *)0);
  252.     lastEventTime = TVTOMILLI(lastEventTimeTv);
  253. }
  254.