home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / MOUSE.C < prev    next >
C/C++ Source or Header  |  1995-10-30  |  24KB  |  423 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   mouse.c                                                                 */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*   Mouse Related Functions.                                                */
  8. /*                                                                           */
  9. /*...Release 1.01 (04/03/92)                                                 */
  10. /*...                                                                        */
  11. /*... 05/08/92  701   Srinivas  Cua Interface.                               */
  12. /*...                                                                        */
  13. /*...Release 1.02 (10/22/92)                                                 */
  14. /*...                                                                        */
  15. /*... 04/14/93  819   Selwyn    Add /k option for keyboard only use.         */
  16. /*****************************************************************************/
  17. #include "all.h"
  18. #include <process.h>
  19.  
  20. /*****************************************************************************/
  21. /* this needs to be returned to static.                                      */
  22. /*****************************************************************************/
  23. static EVENT Event;                     /* The i/o event.                    */
  24. static HEV     EventSem;                /* An event is ready sema4.          */
  25. static HEV     GetItSem;                /* Go get an event sema4.            */
  26. static HMTX    EventWrite;              /* Mutex sema4 for serializing       */
  27.                                         /* access to writing the event.      */
  28. static  HMOU    MouHandle = NULL;       /* Handle to mouse                   */
  29.  
  30. extern  uint    VideoRows;
  31. extern  uint    VideoCols;
  32.  
  33. extern  CmdParms cmd;
  34.  
  35. /*****************************************************************************/
  36. /* StartThreads()                                                            */
  37. /*                                                                           */
  38. /* Description:                                                              */
  39. /*                                                                           */
  40. /*   Starts the i/o threads for the keyboard and the mouse. Starts the       */
  41. /*   semaphores needed to serialize access to the i/o event.                 */
  42. /*                                                                           */
  43. /* Parameters:                                                               */
  44. /*  None                                                                     */
  45. /*                                                                           */
  46. /* Return:                                                                   */
  47. /*  None                                                                     */
  48. /*****************************************************************************/
  49.  
  50. #define STACKSIZE    0x8000             /* Stack size for the i/o threads.   */
  51. #define INITSEMSTATE FALSE              /* Initial sema4 state is "set" for  */
  52.                                         /* an event and "unowned" for mutex. */
  53. #define ATTRFLAGS    0L                 /* Sema4s are unnamed and non-shared.*/
  54.  
  55. void StartThreads( void )
  56. {
  57.   /***************************************************************************/
  58.   /* 1. Start a thread to handle the mouse events.                           */
  59.   /* 2. Start a thread to handle the keyboard events.                        */
  60.   /* 3. Create a sema4 to signal the posting of an event.                    */
  61.   /* 4. Create a sema4 to trigger the i/o threads to go get an event.        */
  62.   /***************************************************************************/
  63.   _beginthread( KbdThread,   NULL, STACKSIZE, NULL );
  64.   if( !cmd.KeyboardOnly )
  65.    _beginthread( MouseThread, NULL, STACKSIZE, NULL );
  66. #if 0
  67.   DosCreateEventSem( (PSZ)NULL, &EventSem, ATTRFLAGS, INITSEMSTATE );
  68.   DosCreateEventSem( (PSZ)NULL, &GetItSem, ATTRFLAGS, INITSEMSTATE );
  69.   DosCreateMutexSem( NULL, &EventWrite, ATTRFLAGS, INITSEMSTATE );
  70. #else
  71.   {
  72.      char semaphorename[80];
  73.      USHORT         pid=getpid();
  74.  
  75. /*Allow other processes to communicate by simulating Mouse Events.*/
  76.   sprintf(semaphorename,"\\SEM32\\SD386\\EventSem\\%4.4hd",pid);
  77.   DosCreateEventSem( (PSZ) semaphorename,
  78.       &EventSem, ATTRFLAGS, INITSEMSTATE );
  79.   sprintf(semaphorename,"\\SEM32\\SD386\\GetItSem\\%4.4hd",pid);
  80.   DosCreateEventSem( (PSZ)semaphorename,
  81.       &GetItSem, ATTRFLAGS, INITSEMSTATE );
  82.   sprintf(semaphorename,"\\SEM32\\SD386\\EventWrite\\%4.4d",pid);
  83.   DosCreateMutexSem( semaphorename, &EventWrite, ATTRFLAGS, INITSEMSTATE );
  84.   }
  85. #endif
  86. }
  87.  
  88. /*****************************************************************************/
  89. /* AccessMouse()                                                             */
  90. /*                                                                           */
  91. /* Description:                                                              */
  92. /*                                                                           */
  93. /*  Opens the mouse and starts the threads for handling the i/o events.      */
  94. /*                                                                           */
  95. /* Parameters:                                                               */
  96. /*   None                                                                    */
  97. /*                                                                           */
  98. /* Return:                                                                   */
  99. /*   None                                                                    */
  100. /*****************************************************************************/
  101.  
  102. #define BUTTON1PRESS    0x0004          /* Button 1 press/release events     */
  103. #define BUTTON1MOVE     0x0002          /* Button 1 press/release + motion   */
  104. #define BUTTON2PRESS    0x0010          /* Button 2 press/release events     */
  105. #define BUTTON2MOVE     0x0008          /* Button 2 press/release + motion   */
  106.  
  107. void AccessMouse( void )
  108. {
  109.   ushort MouseMask;
  110.  
  111.   /***************************************************************************/
  112.   /* Define the mask for the "actions" that we want to cause mouse "events"  */
  113.   /* to be generated. MouState will tell us the "state" of the mouse at      */
  114.   /* the "time" of the event. With this mask, we should see four states:     */
  115.   /*                                                                         */
  116.   /*  1. No buttons down.                                                    */
  117.   /*  2. No buttons down AND the mouse is moving.                            */
  118.   /*  3. Button 1 down.                                                      */
  119.   /*  2. Button 1 down AND the mouse is moving.                              */
  120.   /*                                                                         */
  121.   /***************************************************************************/
  122.   MouseMask = BUTTON1PRESS | BUTTON1MOVE | BUTTON2PRESS | BUTTON2MOVE;
  123.  
  124.   /***************************************************************************/
  125.   /*  - Open the mouse.                                                      */
  126.   /*  - Set up the event mask.                                               */
  127.   /*  - Draw the mouse.                                                      */
  128.   /*  - Start the i/o threads.                                               */
  129.   /***************************************************************************/
  130.   if( !cmd.KeyboardOnly )
  131.   {
  132.     if( MouOpen( 0L, &MouHandle ) || MouSetEventMask( &MouseMask, MouHandle) )
  133.     {
  134.       extern  uchar  VideoAtr;
  135. char msg[] = { "\rMouse open failure. Only keyboard will be active.\r\
  136. Press any key to continue..." };
  137.  
  138.       VideoAtr = vaMenuBar;
  139.       ShowHelpBox( msg, (UINTFUNC)GetKey, NULL, NULL );
  140.       cmd.KeyboardOnly = TRUE;
  141.     }
  142.   }
  143.   StartThreads();
  144. }
  145.  
  146. /*****************************************************************************/
  147. /* GetEvent()                                                                */
  148. /*                                                                           */
  149. /* Description:                                                              */
  150. /*                                                                           */
  151. /*  Triggers the i/o threads to post an event and then waits until           */
  152. /*  the event gets posted. If a wait time is specified, then a timeout       */
  153. /*  will occur and the last event will essentially be recycled.              */
  154. /*                                                                           */
  155. /* Parameters:                                                               */
  156. /*                                                                           */
  157. /*  WaitTime    input - How long to wait before timing out.                  */
  158. /*                                                                           */
  159. /* Return:                                                                   */
  160. /*                                                                           */
  161. /*  &Event      ptr to the event structure containing the posted event.      */
  162. /*                                                                           */
  163. /*****************************************************************************/
  164. PEVENT GetEvent( uint WaitTime )
  165. {
  166.   ulong count;
  167.  
  168.   /***************************************************************************/
  169.   /* 1. Set the RepeatEvent flag. If the event semaphore times out on        */
  170.   /*    WaitTime, then the caller will use this flag to know that an         */
  171.   /*    event is repeated. This us used mainly for scroll bars.              */
  172.   /* 2. Set the i/o event semaphore. ( We don't really care about the        */
  173.   /*    count at this time. )                                                */
  174.   /* 3. Trigger the i/o threads to post an event.                            */
  175.   /* 4. Wait for an event to be posted.                                      */
  176.   /***************************************************************************/
  177.   APIRET rc;
  178.   ShowMouse();
  179.   Event.FakeEvent  = TRUE;
  180.  
  181.   DosResetEventSem( EventSem, &count );
  182.   DosPostEventSem( GetItSem );
  183. #ifndef MSH
  184.   DosWaitEventSem( EventSem, WaitTime );
  185. #else
  186.   rc=DosWaitEventSem( EventSem, WaitTime );
  187.   if(rc==ERROR_TIMEOUT) {
  188.       Event.FakeEvent = TRUE;
  189.   }
  190.   else if(Event.FakeEvent)  /*MSH Event must have interrupted */
  191.   {
  192.       Event.FakeEvent = FALSE;
  193.       Event.Type  = TYPE_MSH_EVENT;
  194.   }/* End if*/
  195. #endif
  196.   HideMouse();
  197.   return( &Event );
  198. }
  199.  
  200. /*****************************************************************************/
  201. /* MouseThread()                                                             */
  202. /*                                                                           */
  203. /* Description:                                                              */
  204. /*                                                                           */
  205. /*  Waits for a trigger and then reads one event from the mouse queue.       */
  206. /*                                                                           */
  207. /* Parameters:                                                               */
  208. /*                                                                           */
  209. /*  Dummy       this is just a dummy parm to keep the compiler happy.        */
  210. /*                                                                           */
  211. /* Return:                                                                   */
  212. /*                                                                           */
  213. /*  None                                                                     */
  214. /*                                                                           */
  215. /*****************************************************************************/
  216.  
  217. #define WAIT_EMPTYQ   1
  218. #define NOWAIT_EMPTYQ 0
  219. #define BUTTON1FILTER 0x0007
  220. #define BUTTON2FILTER 0x0019
  221.  
  222. void MouseThread( void *Dummy )
  223. {
  224.   ushort        WaitOption;
  225.   MOUEVENTINFO  MouEvent;
  226.   ulong         count;
  227.  
  228.   /***************************************************************************/
  229.   /* 1. Wait for a trigger to go get an event.                               */
  230.   /* 2. Read the mouse queue.                                                */
  231.   /* 3. Reset the trigger.                                                   */
  232.   /* 4. Filter out any mouse states such as "button 2" that we're not        */
  233.   /*    interested in.                                                       */
  234.   /* 5. Acquire the mutex semaphore to serialize writing the event.          */
  235.   /* 6. Write the event. We test to see if an event has already been         */
  236.   /*    written since we could have a race between the keyboard and the      */
  237.   /*    mouse to post the event. We handle this as first one to get the      */
  238.   /*    mutex semaphore gets to post the event.                              */
  239.   /* 7. Set a flag to indicate this is not a fake event.                     */
  240.   /* 8. Release the mutex.                                                   */
  241.   /* 9. Post the event.                                                      */
  242.   /*                                                                         */
  243.   /***************************************************************************/
  244.   WaitOption = WAIT_EMPTYQ;
  245.   for( ;; )
  246.   {
  247.     DosWaitEventSem( GetItSem, SEM_INDEFINITE_WAIT );
  248.     MouReadEventQue( &MouEvent, &WaitOption, MouHandle );
  249.     DosResetEventSem( GetItSem, &count );
  250.  
  251.     MouEvent.fs &= (BUTTON1FILTER | BUTTON2FILTER);
  252.  
  253.     DosRequestMutexSem( EventWrite, SEM_INDEFINITE_WAIT );
  254.     if( Event.FakeEvent == TRUE )
  255.     {
  256.      Event.Row   = MouEvent.row;
  257.      Event.Col   = MouEvent.col;
  258.      Event.Type  = TYPE_MOUSE_EVENT;
  259.      Event.Value = MouEvent.fs;
  260.      Event.FakeEvent  = FALSE;
  261.     }
  262.     DosReleaseMutexSem( EventWrite );
  263.  
  264.     DosPostEventSem( EventSem );
  265.   }
  266. }
  267.  
  268. /*****************************************************************************/
  269. /*  HideMouse()                                                              */
  270. /*                                                                           */
  271. /* Description:                                                              */
  272. /*                                                                           */
  273. /*  Hides the mouse.                                                         */
  274. /*                                                                           */
  275. /* Parameters:                                                               */
  276. /*                                                                           */
  277. /*  None                                                                     */
  278. /*                                                                           */
  279. /* Return:                                                                   */
  280. /*                                                                           */
  281. /*  None                                                                     */
  282. /*                                                                           */
  283. /*****************************************************************************/
  284. void HideMouse( void )
  285. {
  286.   NOPTRRECT NoPtrRect;
  287.  
  288.   NoPtrRect.row  = 0;
  289.   NoPtrRect.col  = 0;
  290.   NoPtrRect.cRow = VideoRows - 1;
  291.   NoPtrRect.cCol = VideoCols - 1;
  292.  
  293.   MouRemovePtr( &NoPtrRect, MouHandle );
  294. }
  295.  
  296. /*****************************************************************************/
  297. /* ShowMouse()                                                              */
  298. /*                                                                           */
  299. /* Description:                                                              */
  300. /*                                                                           */
  301. /*  Redraws the mouse.                                                       */
  302. /*                                                                           */
  303. /* Parameters:                                                               */
  304. /*                                                                           */
  305. /*  None                                                                     */
  306. /*                                                                           */
  307. /* Return:                                                                   */
  308. /*                                                                           */
  309. /*  None                                                                     */
  310. /*                                                                           */
  311. /*****************************************************************************/
  312. void ShowMouse( void )
  313. {
  314.   MouDrawPtr( MouHandle );
  315. }
  316.  
  317. /*****************************************************************************/
  318. /* CloseMouse()                                                             */
  319. /*                                                                           */
  320. /* Description:                                                              */
  321. /*                                                                           */
  322. /*  Redraws the mouse.                                                       */
  323. /*                                                                           */
  324. /* Parameters:                                                               */
  325. /*                                                                           */
  326. /*  None                                                                     */
  327. /*                                                                           */
  328. /* Return:                                                                   */
  329. /*                                                                           */
  330. /*  None                                                                     */
  331. /*                                                                           */
  332. /*****************************************************************************/
  333. void CloseMouse( void )
  334. {
  335.   if( MouHandle )
  336.     MouClose( MouHandle );
  337. }
  338.  
  339. /*****************************************************************************/
  340. /*  KbdThread()                                                              */
  341. /*                                                                           */
  342. /* Description:                                                              */
  343. /*                                                                           */
  344. /*  Gets a key from the keyboard and posts as an event.                      */
  345. /*                                                                           */
  346. /*                                                                           */
  347. /* Parameters:                                                               */
  348. /*                                                                           */
  349. /*  None                                                                     */
  350. /*                                                                           */
  351. /* Return:                                                                   */
  352. /*                                                                           */
  353. /*  None                                                                     */
  354. /*                                                                           */
  355. /*****************************************************************************/
  356. void KbdThread( void *Dummy )
  357. {
  358.   ulong  count;
  359.   uint   key;
  360.  
  361.   /***************************************************************************/
  362.   /* 1. Wait for a trigger to go get a key.                                  */
  363.   /* 2. Read the key.                                                        */
  364.   /* 3. Reset the trigger.                                                   */
  365.   /* 5. Acquire the mutex semaphore to serialize writing the event.          */
  366.   /* 6. Write the event. We test to see if an event has already been         */
  367.   /*    written since we could have a race between the keyboard and the      */
  368.   /*    mouse to post the event. We handle this as first one to get the      */
  369.   /*    mutex semaphore gets to post the event.                              */
  370.   /* 7. Set a flag to indicate this is not a fake event.                     */
  371.   /* 8. Release the mutex.                                                   */
  372.   /* 9. Post the event.                                                      */
  373.   /*                                                                         */
  374.   /***************************************************************************/
  375.   for( ;; )
  376.   {
  377.     DosWaitEventSem( GetItSem, SEM_INDEFINITE_WAIT );
  378.     key = GetKey();
  379.     DosResetEventSem( GetItSem, &count );
  380.  
  381.     DosRequestMutexSem( EventWrite, SEM_INDEFINITE_WAIT );
  382.     if( Event.FakeEvent == TRUE )
  383.     {
  384.      Event.Type  = TYPE_KBD_EVENT;
  385.      Event.Value = key;
  386.      Event.FakeEvent  = FALSE;
  387.     }
  388.     DosReleaseMutexSem( EventWrite );
  389.  
  390.     DosPostEventSem( EventSem );
  391.   }
  392. }
  393.  
  394. /*****************************************************************************/
  395. /* GetCurrentEvent()                                                         */
  396. /*                                                                           */
  397. /* Description:                                                              */
  398. /*                                                                           */
  399. /*  Get a ptr to the current i/o event. This is used mainly to get the       */
  400. /*  current row and column of the last mouse state.                          */
  401. /*                                                                           */
  402. /* Parameters:                                                               */
  403. /*                                                                           */
  404. /*  None                                                                     */
  405. /*                                                                           */
  406. /* Return:                                                                   */
  407. /*                                                                           */
  408. /*  &Event      ptr to the event structure.                                   /
  409. /*                                                                           */
  410. /*****************************************************************************/
  411. PEVENT  GetCurrentEvent()
  412. {
  413.   return( &Event );
  414. }
  415.  
  416. void CloseEventSemaphores(void) {
  417.   DosClose(EventSem);   /*Do these first so that MSH will not submit any */
  418.                         /*more commands.*/
  419.   DosClose(EventWrite);
  420.   DosPostEventSem( GetItSem ); /*MSH may be waiting.*/
  421.   DosClose(GetItSem);
  422. }
  423.