home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / common / ibmIO.c < prev    next >
C/C++ Source or Header  |  1992-02-11  |  11KB  |  394 lines

  1. /*
  2.  * $Id: ibmIO.c,v 5.1 1992/02/12 00:45:55 jfc Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989
  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 name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18.  * IBM 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. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  27. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  28.  
  29.                         All Rights Reserved
  30.  
  31. Permission to use, copy, modify, and distribute this software and its
  32. documentation for any purpose and without fee is hereby granted,
  33. provided that the above copyright notice appear in all copies and that
  34. both that copyright notice and this permission notice appear in
  35. supporting documentation, and that the names of Digital or MIT not be
  36. used in advertising or publicity pertaining to distribution of the
  37. software without specific, written prior permission.
  38.  
  39. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  40. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  41. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  42. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  43. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  44. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  45. SOFTWARE.
  46.  
  47. ******************************************************************/
  48.  
  49. #include <sys/types.h>
  50. #include <sys/time.h>
  51.  
  52. #define NEED_EVENTS
  53.  
  54. #include "X.h"
  55. #include "Xproto.h"
  56. #include "inputstr.h"
  57. #include "miscstruct.h"
  58. #include "scrnintstr.h"
  59. #include "cursorstr.h"
  60.  
  61. #include "OScompiler.h"
  62.  
  63. #include "ibmKeybd.h"
  64. #include "ibmMouse.h"
  65. #include "ibmScreen.h"
  66. #include "ibmCmdArgs.h"
  67.  
  68. #include "OSio.h"
  69. #include "OScursor.h"
  70.  
  71. #include "ibmTrace.h"
  72.  
  73. int lastEventTime;
  74.  
  75. #ifdef IBM_OS_HAS_X_QUEUE
  76. #include "OSxqueue.h"
  77.  
  78. XEventQueue *ibmQueue;
  79. extern int screenIsSaved;
  80.  
  81. /*****************
  82.  * ProcessInputEvents:
  83.  *    processes all the pending input events
  84.  *****************/
  85.  
  86. void
  87. ProcessInputEvents()
  88. {
  89.     register int i;
  90.     register ibmPerScreenInfo *screenInfo;
  91.     register int x, y;
  92.     register XEvent *pE;
  93.     xEvent e;
  94.     int nowInCentiSecs, nowInMilliSecs, adjustCentiSecs;
  95.     struct timeval tp;
  96.     int needTime = 1;
  97.     int    oldScr;
  98.     int    setCursor;
  99. #ifdef IBM_SPECIAL_MALLOC
  100.     extern int ibmShouldDumpArena;
  101. #endif /* IBM_SPECIAL_MALLOC */
  102.     static int CannotBeReentrant = 0;
  103.  
  104. /*    TRACE( ( "ProcessInputEvents()\n" ) );*/
  105.  
  106. #ifdef IBM_SPECIAL_MALLOC
  107.     if ( ibmShouldDumpArena )
  108.        ibmDumpArena();
  109. #endif /* IBM_SPECIAL_MALLOC */
  110.  
  111.     if ( CannotBeReentrant )
  112.     return;
  113.     else
  114.     CannotBeReentrant = 1;
  115.  
  116. #if defined(IBM_APA16) && 0
  117.     {
  118.     extern int apa16CheckQ();
  119.     extern ibmPerScreenInfo apa16ScreenInfoStruct;
  120.  
  121.     if (ibmScreens[ibmCurrentScreen] == &apa16ScreenInfoStruct)
  122.         apa16CheckQ();
  123.     }
  124. #endif /* IBM_APA16 */
  125.  
  126.     for ( i = ibmQueue->head;
  127.       i != ibmQueue->tail;
  128. i = ( ibmQueue->head = ( ( i == ibmQueue->size ) ? 0 : ( ibmQueue->head + 1 ) ) ) )
  129.     {
  130.     if (screenIsSaved == SCREEN_SAVER_ON)
  131.         SaveScreens( SCREEN_SAVER_OFF, ScreenSaverReset );
  132.     pE = &ibmQueue->events[i];
  133.  
  134.     if ( pE->xe_device == XE_CONSOLE ) { /* virtual screen event */
  135.         OS_ScreenStateChange( pE );
  136.     }
  137.     else if ( pE->xe_device ) {
  138.         int y_frac, scale_y;
  139.         x = ( signed short ) pE->xe_x;
  140.         y = ( signed short ) pE->xe_y;
  141.         screenInfo = ibmScreens[ oldScr = ibmCurrentScreen ];
  142.         setCursor = FALSE;
  143.  
  144.         /* y_frac is 64K * the fractional distance
  145.            along the screen edge, used to scale the cursor position
  146.            so the edges of adjacent screens are aligned */
  147.         y_frac = (y * 65536) / (screenInfo->ibm_ScreenBounds.y2 - screenInfo->ibm_ScreenBounds.y1);
  148.         scale_y = 0;
  149.  
  150.         if ( ibmYWrapScreen ) {
  151.             while ( y < screenInfo->ibm_ScreenBounds.y1 ) {
  152.             y +=
  153.     screenInfo->ibm_ScreenBounds.y2 - screenInfo->ibm_ScreenBounds.y1;
  154.             setCursor = TRUE;
  155.             }
  156.  
  157.             while ( y > screenInfo->ibm_ScreenBounds.y2 ) {
  158.             y -=
  159.     screenInfo->ibm_ScreenBounds.y2 - screenInfo->ibm_ScreenBounds.y1;
  160.             setCursor = TRUE;
  161.             }
  162.         }
  163.  
  164.         while ( x < screenInfo->ibm_ScreenBounds.x1 ) {
  165.             if ( screenInfo == ibmScreens[ 0 ] ) {
  166.             /* Already At First Possible Screen */
  167.             if ( ibmXWrapScreen ) {
  168.                 x -= screenInfo->ibm_ScreenBounds.x1; /* x < 0 */
  169.                 screenInfo =
  170.                 ibmScreens[ ibmCurrentScreen = ibmNumScreens - 1 ];
  171.                 x += screenInfo->ibm_ScreenBounds.x2;
  172.                 scale_y = 1;
  173.             }
  174.             else {
  175.                 x = screenInfo->ibm_ScreenBounds.x1;
  176.             }
  177.             }
  178.             else {
  179.             x -= screenInfo->ibm_ScreenBounds.x1; /* Now x < 0 */
  180.             screenInfo = ibmScreens[ --ibmCurrentScreen ];
  181.             x += screenInfo->ibm_ScreenBounds.x2;
  182.             scale_y = 1;
  183.             }
  184.             setCursor = TRUE;
  185.         }
  186.  
  187.         while ( x > screenInfo->ibm_ScreenBounds.x2 ) {
  188.             if ( screenInfo == ibmScreens[ ibmNumScreens - 1 ] ) {
  189.             /* Already At Last Possible Screen */
  190.             if ( ibmXWrapScreen ) {
  191.                 x -= screenInfo->ibm_ScreenBounds.x2; /* x > 0 */
  192.                 screenInfo = ibmScreens[ ibmCurrentScreen = 0 ];
  193.                 x += screenInfo->ibm_ScreenBounds.x1;
  194.                 scale_y = 1;
  195.             }
  196.             else {
  197.                 x = screenInfo->ibm_ScreenBounds.x2;
  198.             }
  199.             }
  200.             else {
  201.             x -= screenInfo->ibm_ScreenBounds.x2; /* Now x > 0 */
  202.             screenInfo = ibmScreens[ ++ibmCurrentScreen ];
  203.             x += screenInfo->ibm_ScreenBounds.x1;
  204.             scale_y = 1;
  205.             }
  206.             setCursor = TRUE;
  207.         }
  208.  
  209.         /* Scale new y coordinate. */
  210.         if (scale_y)
  211.             y = y_frac * (screenInfo->ibm_ScreenBounds.y2 - screenInfo->ibm_ScreenBounds.y1) / 65536;
  212.  
  213.         if ( y > screenInfo->ibm_ScreenBounds.y2 ) {
  214.             y = screenInfo->ibm_ScreenBounds.y2;
  215.             setCursor = TRUE;
  216.         }
  217.         else if ( y < screenInfo->ibm_ScreenBounds.y1 ) {
  218.             y = screenInfo->ibm_ScreenBounds.y1;
  219.             setCursor = TRUE;
  220.         }
  221.  
  222.         if ( setCursor ) {
  223.             /* OS-DEPENDANT MACRO GOES HERE !!
  224.              * MACRO DEFINED IN FILE "OScursor.h"
  225.              * TELL OS THAT CURSOR HAS MOVED
  226.              * TO A NEW POSTION
  227.              */
  228.             OS_TellCursorPosition( x, y );
  229.         }
  230.  
  231.         e.u.keyButtonPointer.rootX =
  232.           ( x -= screenInfo->ibm_ScreenBounds.x1 );
  233.         e.u.keyButtonPointer.rootY =
  234.           ( y -= screenInfo->ibm_ScreenBounds.y1 );
  235.  
  236.         if ( oldScr != ibmCurrentScreen ) {
  237.             CursorPtr prevCursor = screenInfo->ibm_CurrentCursor;
  238.             ( * ( ibmHideCursor( oldScr ) ) )( oldScr );
  239.             NewCurrentScreen( screenInfo->ibm_Screen, x, y );
  240.             if ( screenInfo->ibm_CurrentCursor == prevCursor ) {
  241.             ( * screenInfo->ibm_Screen->DisplayCursor )(
  242.                     screenInfo->ibm_Screen,
  243.                     ibmCurrentCursor( oldScr ) );
  244.             }
  245.         }
  246.  
  247.         if ( pE->xe_device == XE_MOUSE )
  248.             (* screenInfo->ibm_CursorShow )( x, y );
  249.         /*
  250.          * The following silly looking code is because the old version of
  251.          * the driver only delivers 16 bits worth of centiseconds. We are
  252.          * supposed to be keeping time in terms of 32 bits of milliseconds.
  253.          */
  254.         if ( needTime ) {
  255.             needTime = 0;
  256.             gettimeofday( &tp, 0 );
  257.             nowInCentiSecs = ( ( tp.tv_sec * 100 )
  258.                    + ( tp.tv_usec / 10000 ) ) & 0xFFFF;
  259.             /* same as driver */
  260.             nowInMilliSecs = ( tp.tv_sec * 1000 )
  261.                    + ( tp.tv_usec / 1000 );
  262.             /* beware overflow */
  263.         }
  264.  
  265.         if ( ( adjustCentiSecs = nowInCentiSecs - pE->xe_time ) < -20000 )
  266.             adjustCentiSecs += 0x10000;
  267.         else
  268.             if ( adjustCentiSecs > 20000 )
  269.             adjustCentiSecs -= 0x10000;
  270.  
  271. /*
  272.  * sometimes the kernel is sending us events with time in the future,
  273.  * if so then say the event is now.
  274.  */
  275.         e.u.keyButtonPointer.time = lastEventTime =
  276.             nowInMilliSecs - ( ( adjustCentiSecs < 0 ) ? 0 : adjustCentiSecs ) * 10;
  277.  
  278.         if ( ( pE->xe_type == XE_BUTTON )
  279.           && ( pE->xe_device == XE_DKB ) ) {
  280.             extern CARD16 keyModifiersList[];
  281.  
  282.             /*
  283.              * Caps Lock code courtesy of John Kubiatowicz
  284.              * IBM/Athena
  285.              */
  286.             if ((((DeviceIntPtr)ibmKeybd)->key->modifierMap[pE->xe_key]
  287.                 & LockMask ) && ibmLockEnabled ) {
  288.             /* Deal with shift lock toggling */
  289.             if ( pE->xe_direction == XE_KBTDOWN ) {
  290.                 ibmLockState = !ibmLockState;
  291.                 OS_CapsLockFeedback( ibmLockState );
  292.                 if ( ibmLockState ) { /* turning lock on */
  293.                 e.u.u.type = KeyPress;
  294.                 e.u.u.detail = ibmCurLockKey = pE->xe_key;
  295.                 }
  296.                 else {
  297.                 /* turn off same key as originally locked */
  298.                 e.u.u.type = KeyRelease;
  299.                 e.u.u.detail = ibmCurLockKey;
  300.                 }
  301.                 (* ibmKeybd->processInputProc)( &e, ibmKeybd, 1 );
  302.             }
  303.             }
  304.             else {
  305.             e.u.u.detail = pE->xe_key;
  306.                         /*
  307.                          * The following bit of hackery is only for the 
  308.              * development server
  309.                          */
  310. #ifdef  PARALLAX_CMDKEYS
  311. #define RT_FN1        0x07
  312. #define RT_FN2        0x0f
  313. #define RT_FN3        0x17
  314. #define RT_FN4        0x1f
  315.  
  316.             switch (e.u.u.detail) {
  317.             case RT_FN1:
  318.             case RT_FN2:
  319.             case RT_FN3:
  320.             case RT_FN4:
  321.                 plx_check_pan(e.u.u.detail);
  322.                 break;
  323.  
  324.             default:
  325. #endif  PARALLAX_CMDKEYS
  326.  
  327.             switch ( pE->xe_direction ) {
  328.                 case XE_KBTDOWN:
  329.                 e.u.u.type = KeyPress;
  330.                 (* ibmKeybd->processInputProc)( &e, ibmKeybd, 1 );
  331.                 break;
  332.                 case XE_KBTUP:
  333.                 e.u.u.type = KeyRelease;
  334.                 (* ibmKeybd->processInputProc)( &e, ibmKeybd, 1 );
  335.                 break;
  336.                 default:    /* hopefully BUTTON_RAW_TYPE */
  337.                 ErrorF( "got a raw button, what do I do?\n" );
  338.                 break;
  339.             }
  340. #ifdef PARALLAX_CMDKEYS
  341.             }
  342. #endif /* PARALLAX_CMDKEYS */
  343.             }
  344.         }
  345.         else if ( ( pE->xe_device == XE_MOUSE )
  346.                || ( pE->xe_device == XE_TABLET ) ) {
  347.             if ( pE->xe_type == XE_BUTTON ) {
  348.             if ( pE->xe_direction == XE_KBTDOWN )
  349.                 e.u.u.type = ButtonPress;
  350.             else
  351.                 e.u.u.type = ButtonRelease;
  352.             /* mouse buttons numbered from one */
  353.             e.u.u.detail = pE->xe_key + 1;
  354.             }
  355.             else
  356.             e.u.u.type = MotionNotify;
  357.             (* ibmPtr->processInputProc)( &e, ibmPtr, 1 );
  358.         }
  359.     }
  360.     }
  361.     CannotBeReentrant = 0;
  362.     return;
  363. }
  364.  
  365. #endif
  366.  
  367. TimeSinceLastInputEvent()
  368. {
  369. /*    TRACE( ( "TimeSinceLastInputEvent()\n" ) );*/
  370.  
  371.     if ( lastEventTime == 0 )
  372.     lastEventTime = GetTimeInMillis();
  373.     return GetTimeInMillis() - lastEventTime;
  374. }
  375.  
  376. /***==================================================================***/
  377.  
  378. ibmSaveScreen(pScreen, on)
  379.     ScreenPtr    pScreen;
  380.     int        on;
  381. {
  382.     TRACE(("ibmSaveScreen(pScreen=0x%x,on=%d)\n", pScreen, on));
  383.  
  384. #ifdef OS_SaveScreen
  385.     OS_SaveScreen(pScreen, on);
  386. #endif
  387.     if (on == SCREEN_SAVER_FORCER) {
  388.     lastEventTime = GetTimeInMillis();
  389.     return TRUE;
  390.     }
  391.     else
  392.     return FALSE;
  393. }
  394.