home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_6.ZIP / DOSSRC.ZIP / MOUSE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  6.6 KB  |  310 lines

  1. //    Program name..    Zinc Interface Library
  2. //    Filename......    MOUSE.CPP
  3. //    Version.......    1.0
  4. //    
  5. //    COPYRIGHT (C) 1990.  All Rights Reserved.
  6. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  7.  
  8. #pragma inline
  9.  
  10. #include <dos.h>
  11. #include "ui_evt.hpp"
  12.  
  13. static USHORT _oldMouseMask = 0;
  14.  
  15. UI_EVENT_MANAGER *_eventManagerPtr = 0;
  16. UI_EVENT _mouseEvent;
  17. int _mouseCellWidth;
  18. int _mouseCellHeight;
  19. long _mouseTime;
  20. int _mouseEnabled;
  21.  
  22. static UCHAR _oldButtonState = 0;
  23. static int _processing;
  24.  
  25. struct {
  26.     USHORT offset;
  27.     USHORT segment;
  28. } _oldMouseISR;
  29.  
  30. void far MouseISR(void)
  31. {
  32.     asm cli                                // Disable interrupts.
  33.     asm push ds
  34. #if !defined(__HUGE__)
  35.     asm mov ax, DGROUP
  36.     asm mov ds, ax                        // Get the proper value to DS.
  37. #endif
  38.  
  39.     asm mov ax, [_processing]                // Check for active interrupt.
  40.     asm or ax, ax
  41.     asm jz NotProcessing
  42.     asm sti
  43.     asm jmp EndIsr
  44.  
  45. NotProcessing:
  46.     asm inc ax
  47.     asm mov [_processing], ax
  48.     asm sti
  49.  
  50.     _mouseEvent.position.column = _CX;
  51.     _mouseEvent.position.line = _DX;
  52.     _BH = _BL;
  53.     asm xor bh, _oldButtonState
  54.     asm mov _oldButtonState, bl
  55.     _mouseEvent.rawCode = (((_BH << 4) | _BL) << 8) | (*((UCHAR far *) 0x417L) & 0xF);
  56.     _mouseEvent.type = E_MOUSE;
  57.     _mouseEvent.position.column /= _mouseCellWidth;
  58.     _mouseEvent.position.line /= _mouseCellHeight;
  59.     if (_mouseEnabled && _eventManagerPtr)
  60.     {
  61.         UI_EVENT event;
  62.         if (_eventManagerPtr->Get(event, Q_END | Q_NO_DESTROY | Q_NO_BLOCK | Q_NO_POLL) != -2 &&
  63.             event.type == E_MOUSE && event.rawCode == _mouseEvent.rawCode)
  64.             _eventManagerPtr->Get(event, Q_END | Q_NO_POLL);
  65.         _eventManagerPtr->Put(_mouseEvent, Q_END);
  66.     }
  67.     _mouseTime = *((long far *)0x46CL);
  68.     if (UI_DEVICE::altPressed != ALT_NOT_PRESSED)
  69.         UI_DEVICE::altPressed = ALT_PRESSED_AND_EVENTS_RECEIVED;
  70.  
  71.     asm xor ax, ax
  72.     asm mov [_processing], ax
  73.  
  74. EndIsr:
  75.     asm pop ds
  76. }
  77.  
  78. static int InstallHandler(void)
  79. {
  80.     _AX = 0;
  81.     geninterrupt(0x33);
  82.     if (_AX == -1)
  83.     {
  84.         void far *isrPtr;
  85.  
  86.         isrPtr = MouseISR;
  87.         _CX = 0xFF;                    // Enable all events.
  88.         _DX = FP_OFF(isrPtr);
  89.         _BX = FP_SEG(isrPtr);
  90.         _ES = _BX;
  91.         _AX = 12;                    // Set Interrupt Subroutine call
  92.         geninterrupt(0x33);
  93.         return TRUE;
  94.     }
  95.     return FALSE;
  96. }
  97.  
  98. static void RestoreHandler()
  99. {
  100.     _CX = _oldMouseMask;
  101.     _DX = _oldMouseISR.offset;
  102.     _BX = _oldMouseISR.segment;
  103.     _ES = _BX;
  104.     _AX = 12;
  105.     geninterrupt(0x33);
  106. }
  107.  
  108. UI_MS_MOUSE::UI_MS_MOUSE(USHORT initialState) :
  109.     UI_DEVICE(E_MOUSE, initialState)
  110. {
  111.     installed = FALSE;
  112.     _mouseEnabled = TRUE;
  113.  
  114.     UCHAR far * far *mouse_vec = (UCHAR far * far *) (4 * 0x33);
  115.     if (*mouse_vec && **mouse_vec != 0xCF)
  116.     {
  117.         _AX = 20;
  118.         _BX = 0;
  119.         _DX = 0;
  120.         _CX = 0;
  121.         geninterrupt(0x33);
  122.         _oldMouseMask = _CX;
  123.         _BX = _ES;
  124.         _oldMouseISR.segment = _BX;
  125.         _oldMouseISR.offset = _DX;
  126.         installed = TRUE;
  127.     }
  128.  
  129.     // Get the mouse pointer.
  130.     for (int i = 0; _pointerTable[i]; i++)
  131.         if (_pointerTable[i]->logicalType == DM_VIEW)
  132.         {
  133.             pointer = _pointerTable[i];
  134.             break;
  135.         }
  136. }
  137.  
  138. UI_MS_MOUSE::~UI_MS_MOUSE(void)
  139. {
  140.     // Uninstall the mouse.
  141.     if (installed)
  142.         RestoreHandler();
  143. }
  144.  
  145. int UI_MS_MOUSE::Event(const UI_EVENT &event)
  146. {
  147.     // Make sure the mouse is installed.
  148.     if (!installed)
  149.         return (0);
  150.  
  151.     if (display->isText)
  152.     {
  153.         _mouseCellWidth = (display->columns == 40) ? 16 : 640 / display->columns;
  154.         _mouseCellHeight = 8;
  155.     }
  156.     else
  157.     {
  158.         _mouseCellWidth  = 1;
  159.         _mouseCellHeight = 1;
  160.     }
  161.     USHORT ccode = event.rawCode;
  162.     int t_state = state;
  163.     UI_MOUSE_POINTER *oldPointer = pointer;
  164.  
  165.     // See if the mouse has been initialized.
  166.     if (ccode == D_INITIALIZE)
  167.     {
  168.         installed = InstallHandler();
  169.         if (!installed)
  170.             return (0);
  171.         _eventManagerPtr = eventManager;
  172.         _DX = (display->columns - 1) * _mouseCellWidth;
  173.         _CX = 0;
  174.         _AX = 7;
  175.         geninterrupt(0x33);     // Set min. & max. horizontal cursor position.
  176.         _DX = (display->lines - 1) * _mouseCellHeight;
  177.         _CX = 0;
  178.         _AX = 8;
  179.         geninterrupt(0x33);     // Set min. & max. horizontal cursor position.
  180.         Display();
  181.         ccode = state;
  182.         t_state = D_OFF;
  183.     }
  184.  
  185.     switch (ccode)
  186.     {
  187.     case D_HIDE:
  188.         _AX = 11;
  189.         geninterrupt(0x33);                    // See if mouse is moving.
  190.         if (_CX || _DX)
  191.         {
  192.             // Insure the mouse is turned off if it is moving.
  193.             column = event.region.left * _mouseCellWidth;
  194.             line = event.region.top * _mouseCellHeight;
  195.         }
  196.         else
  197.         {
  198.             _AX = 3;
  199.             geninterrupt(0x33);                // Get the mouse position.
  200.             column = _CX;
  201.             line = _DX;
  202.         }
  203.         // Continue to D_SHOW.
  204.  
  205.     case D_SHOW:
  206.         UI_REGION region;
  207.         region.left = column;
  208.         region.top = line;
  209.         if (display->isText)
  210.         {
  211.             region.left /= _mouseCellWidth;
  212.             region.top  /= _mouseCellHeight;
  213.             region.right = region.left;
  214.             region.bottom = region.top;
  215.         }
  216.         else
  217.         {
  218.             region.left -= pointer->graphicsCursorHorizontal + 8;
  219.             region.top -= pointer->graphicsCursorVertical + 4;
  220.             region.right = region.left + 31;
  221.             region.bottom = region.top + 23;
  222.         }
  223.          if (Max(event.region.left, region.left) > Min(event.region.right, region.right) ||
  224.             Max(event.region.top, region.top) > Min(event.region.bottom, region.bottom))
  225.             return(0);
  226.         state = (ccode == D_SHOW) ? D_ON : D_OFF;
  227.         break;
  228.  
  229.     case D_POSITION:
  230.         _DX = event.position.line;
  231.         _CX = event.position.column;
  232.         _AX = 4;
  233.         geninterrupt(0x33);
  234.         break;
  235.  
  236.     case D_OFF:
  237.     case D_ON:
  238.         _mouseEnabled = enabled = (event.rawCode == D_OFF) ? FALSE : TRUE;
  239.         break;
  240.  
  241.     case D_RESTORE:
  242.         RestoreHandler();
  243.         return (state);
  244.  
  245.     case DM_USER_DEFINED:
  246.         pointer = (UI_MOUSE_POINTER *)event.data; break;
  247.         // Continue to default.
  248.  
  249.     default:
  250.         // Try to find the new match.
  251.         for (int i = 0; _pointerTable[i]; i++)
  252.             if (_pointerTable[i]->logicalType == event.rawCode)
  253.             {
  254.                 pointer = _pointerTable[i];
  255.                 break;
  256.             }
  257.         break;
  258.     }
  259.  
  260.     // Change the mouse state.
  261.     if (pointer != oldPointer)
  262.         Display();
  263.     if (t_state != state)
  264.     {
  265.         _AX = (state != D_OFF) ? 1 : 2;
  266.         geninterrupt(0x33);
  267.     }
  268.  
  269.     // Return the device state.
  270.     return (state);
  271. }
  272.  
  273. void UI_MS_MOUSE::SetSensitivity(int horizontal, int vertical, int doubleSpeed)
  274. {
  275.     if (installed)
  276.     {
  277.         _BX = horizontal;
  278.         _CX = vertical;
  279.         _DX = doubleSpeed;
  280.         _AX = 26;
  281.         geninterrupt(0x33);
  282.     }
  283. }
  284.  
  285. void UI_MS_MOUSE::Display(void)
  286. {
  287.     if (!display || !pointer)
  288.         return;
  289.     else if (display->isText)
  290.     {
  291.         _DX = pointer->textScreenMask;
  292.         _CX = pointer->textCursorMask;
  293.         _BX = 0;                     // BX = 0 for software cursor.
  294.         _AX = 10;
  295.     }
  296.     else
  297.     {
  298.         _DX = (USHORT)pointer->graphicsCursorMask;
  299.         _CX = pointer->graphicsCursorVertical;
  300.         _BX = pointer->graphicsCursorHorizontal;
  301.         _ES = _DS;
  302.         _AX = 9;                    // Set Graphics Cursor call.
  303.     }
  304.     geninterrupt(0x33);
  305. }
  306.  
  307. void UI_MS_MOUSE::Poll(void)
  308. {
  309. }
  310.