home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / pmvnc100.zip / mouse.c < prev    next >
C/C++ Source or Header  |  1999-02-15  |  7KB  |  325 lines

  1. /*
  2.  * mouse.c - PM VNC Viewer, handling pointer events
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9.  
  10. #define INCL_PM
  11. #include <os2.h>
  12.  
  13. #include "pmvncdef.h"
  14. #include "pmvncres.h"
  15.  
  16. /*
  17.  * mouse control variables
  18.  *
  19.  *      for center button emulation
  20.  */
  21.  
  22. static  HPOINTER    hptrMouse = NULLHANDLE ;    /* mouse pointer    */
  23. static  POINTL          ptLast   ;
  24. static  struct timeval  tvLast   ;
  25. static  USHORT          fsCheck  ;
  26. static  USHORT          fsState  ;
  27. static  USHORT          fsNotify ;
  28.  
  29. /*
  30.  * mouseInit - initialize mouse handling
  31.  */
  32.  
  33. void    mouseInit(HWND hwnd)
  34. {
  35.     hptrMouse = WinLoadPointer(HWND_DESKTOP, NULLHANDLE, ID_CURSOR) ;
  36.     fsCheck = fsState = fsNotify = 0 ;
  37.     ptLast.x = ptLast.y = 0 ;
  38.     gettimeofday(&tvLast, NULL) ;
  39. }
  40.  
  41. /*
  42.  * mouseDone - finialize mouse handling
  43.  */
  44.  
  45. void    mouseDone(HWND hwnd)
  46. {
  47.     if (hptrMouse != NULLHANDLE) {
  48.         WinDestroyPointer(hptrMouse) ;
  49.     }
  50. }
  51.  
  52. /*
  53.  * chkMoved - check if point moved
  54.  */
  55.  
  56. #define MOVELIM     8
  57.  
  58. static  BOOL    chkMoved(PPOINTL np)
  59. {
  60.     LONG    horz, vert ;
  61.     PPOINTL op = (PPOINTL) &ptLast ;
  62.     
  63.     horz = (op->x > np->x) ? op->x - np->x : np->x - op->x ;
  64.     vert = (op->y > np->y) ? op->y - np->y : np->y - op->y ;
  65.     
  66.     if (horz > MOVELIM || vert >MOVELIM) {
  67.         return TRUE ;
  68.     } else {
  69.         return FALSE ;
  70.     }
  71. }
  72.  
  73. /*
  74.  * chkTimed - check if timer expired
  75.  */
  76.  
  77. #define TIMELIM     (1000 * 50)
  78.  
  79. static  BOOL    chkTimed(void)
  80. {
  81.     struct timeval  tv ;
  82.     LONG    sec, usec ;
  83.     
  84.     gettimeofday(&tv, NULL) ;
  85.     if ((sec = tv.tv_sec - tvLast.tv_sec) > 2)  {
  86.         return TRUE ;
  87.     }
  88.     if ((usec = sec * 1000 * 1000 + tv.tv_usec - tvLast.tv_usec) > TIMELIM) {
  89.         return TRUE ;
  90.     }
  91.     return FALSE ;
  92. }
  93.  
  94. /*
  95.  * mouseFlush - flush pending mouse events
  96.  */
  97.  
  98. static  void    mouseFlush(void)
  99. {
  100.     int     i    ;
  101.     USHORT  state, mask ;
  102.     
  103.     state = fsNotify ;
  104.     
  105.     for (i = 0, mask = 0x01 ; i < 3 ; i++, mask <<= 1) {
  106.         if (fsCheck & mask) {
  107.         if (fsState & mask) {
  108.             state |= mask ;
  109.         } else {
  110.             state &= ~mask ;
  111.         }
  112.     }
  113.     }
  114.     if (state != fsNotify) {
  115.         protoSendMouEvent((fsNotify = state), &ptLast) ;
  116.     }
  117.     fsCheck = 0 ;
  118. }
  119.  
  120. /*
  121.  * mouseMove - process MOUSEMOVE event
  122.  */
  123.  
  124. void    mouseMove(HWND hwnd, PPOINTL pt)
  125. {
  126.     if (hptrMouse != NULLHANDLE) {
  127.         WinSetPointer(HWND_DESKTOP, hptrMouse) ;
  128.     }
  129.  
  130.     if (chkMoved(pt) || chkTimed()) {
  131.         mouseFlush() ;
  132.     }
  133.     protoSendMouEvent(fsNotify, pt)  ;
  134.  
  135.     gettimeofday(&tvLast, NULL) ;
  136.     ptLast = *pt ;
  137. }
  138.  
  139. /*
  140.  * mouseTimer - timer entry for mouse handling
  141.  */
  142.     
  143. void    mouseTimer(HWND hwnd)
  144. {
  145.     if (fsCheck && chkTimed()) {
  146.         mouseFlush() ;
  147.     }
  148.     return ;
  149. }
  150.  
  151. /*
  152.  * mouseButtonDn - button down
  153.  */
  154.  
  155. static  void    mouseButtonDn(HWND hwnd, ULONG msg, PPOINTL pt)
  156. {
  157.     USHORT  button ;
  158.     
  159.     if (chkMoved(pt) || chkTimed()) {
  160.         mouseFlush() ;
  161.     }
  162.     
  163.     switch (msg) {
  164.         case WM_BUTTON1DOWN : button = 0x0001 ; break ;
  165.         case WM_BUTTON2DOWN : button = 0x0004 ; break ;
  166.         case WM_BUTTON3DOWN : button = 0x0002 ; break ;
  167.         default             : button = 0x0000 ; break ;
  168.     }
  169.     if (button == 0) {
  170.         return ;
  171.     }
  172.     fsState |= button ;
  173.     
  174.     if ((fsCheck & 0x05) && ((fsState & 0x05) == 0x05)) {
  175.         protoSendMouEvent((fsNotify |= 0x02), pt) ;
  176.     gettimeofday(&tvLast, NULL) ;
  177.     ptLast = *pt ;
  178.     fsCheck = 0 ;
  179.         return ;
  180.     }
  181.     fsCheck |= button ;
  182.     gettimeofday(&tvLast, NULL) ;
  183.     ptLast = *pt ;
  184. }
  185.  
  186. /*
  187.  * mouseButtonUp - button up
  188.  */
  189.  
  190. static  void    mouseButtonUp(HWND hwnd, ULONG msg, PPOINTL pt)
  191. {
  192.     USHORT  button ;
  193.     
  194.     if (chkMoved(pt) || chkTimed()) {
  195.         mouseFlush() ;
  196.     }
  197.     
  198.     switch (msg) {
  199.         case WM_BUTTON1UP : button = 0x0001 ; break ;
  200.         case WM_BUTTON2UP : button = 0x0004 ; break ;
  201.         case WM_BUTTON3UP : button = 0x0002 ; break ;
  202.         default           : button = 0x0000 ; break ;
  203.     }
  204.     if (button == 0) {
  205.         return ;
  206.     }
  207.     fsState &= ~button ;
  208.  
  209.     if (fsNotify & 0x0002) {
  210.         if ((fsState & 0x0005) == 0) {
  211.             fsNotify &= ~0x0002 ;
  212.         protoSendMouEvent(fsNotify, pt) ; 
  213.     }
  214.     } else if (fsNotify & button) {
  215.         fsNotify &= ~button ;
  216.     protoSendMouEvent(fsNotify, pt) ;
  217.     }
  218.     gettimeofday(&tvLast, NULL) ;
  219.     ptLast = *pt ;
  220. }
  221.  
  222. /*
  223.  * mouseButtonClick - button click
  224.  *      Click apprears after DOWN, UP, so confirms button is up.
  225.  */
  226.  
  227. static  void    mouseButtonClick(HWND hwnd, ULONG msg, PPOINTL pt)
  228. {
  229.     USHORT  button ;
  230.     
  231.     if (chkMoved(pt) || chkTimed()) {
  232.         mouseFlush() ;
  233.     }
  234.  
  235.     switch (msg) {
  236.         case WM_BUTTON1CLICK : button = 0x0001 ; break ;
  237.         case WM_BUTTON2CLICK : button = 0x0004 ; break ;
  238.         case WM_BUTTON3CLICK : button = 0x0002 ; break ;
  239.         default              : button = 0x0000 ; break ;
  240.     }
  241.     if (button == 0) {
  242.         return ;
  243.     }
  244.     fsState &= ~button ;
  245.  
  246.     if (fsNotify & 0x0002) {
  247.         if ((fsState & 0x0005) == 0) {
  248.             fsNotify &= ~0x0002 ;
  249.         protoSendMouEvent(fsNotify, pt) ;
  250.     }
  251.     } else if (fsNotify & button) {
  252.         fsNotify &= ~button ;
  253.     protoSendMouEvent(fsNotify, pt) ;
  254.     }
  255.     gettimeofday(&tvLast, NULL) ;
  256.     ptLast = *pt ;
  257. }
  258.  
  259. /*
  260.  * mouseButtonDblClk - button double click
  261.  *      Double Click as DOWN, UP, DBLCLK, UP, so do same as Down Notify
  262.  */
  263.  
  264. static  void    mouseButtonDblClk(HWND hwnd, ULONG msg, PPOINTL pt)
  265. {
  266.     USHORT  button ;
  267.     
  268.     if (chkMoved(pt) || chkTimed()) {
  269.         mouseFlush() ;
  270.     }
  271.  
  272.     switch (msg) {
  273.         case WM_BUTTON1DBLCLK : button = 0x0001 ; break ;
  274.         case WM_BUTTON2DBLCLK : button = 0x0004 ; break ;
  275.         case WM_BUTTON3DBLCLK : button = 0x0002 ; break ;
  276.         default               : button = 0x0000 ; break ;
  277.     }
  278.     if (button == 0) {
  279.         return ;
  280.     }
  281.     fsState |= button ;
  282.     
  283.     if ((fsCheck & 0x05) && ((fsState & 0x05) == 0x05)) {
  284.         protoSendMouEvent((fsNotify |= 0x02), pt) ;
  285.     gettimeofday(&tvLast, NULL) ;
  286.     ptLast = *pt ;
  287.     fsCheck = 0  ;
  288.         return ;
  289.     }
  290.     fsCheck |= button ;
  291.     gettimeofday(&tvLast, NULL) ;
  292.     ptLast = *pt ;
  293. }
  294.  
  295. /*
  296.  * mouseEvent - mouse down/up
  297.  */
  298.  
  299. void    mouseEvent(HWND hwnd, ULONG msg, PPOINTL pt)
  300. {
  301.     switch (msg) {
  302.     case WM_BUTTON1DOWN :
  303.     case WM_BUTTON2DOWN :
  304.     case WM_BUTTON3DOWN :
  305.         mouseButtonDn(hwnd, msg, pt) ;
  306.     break ;
  307.     case WM_BUTTON1UP   :
  308.     case WM_BUTTON2UP   :
  309.     case WM_BUTTON3UP   :
  310.         mouseButtonUp(hwnd, msg, pt) ;
  311.     break ;
  312.     case WM_BUTTON1CLICK :
  313.     case WM_BUTTON2CLICK :
  314.     case WM_BUTTON3CLICK :
  315.         mouseButtonClick(hwnd, msg, pt) ;
  316.     break ;
  317.     case WM_BUTTON1DBLCLK :
  318.     case WM_BUTTON2DBLCLK :
  319.     case WM_BUTTON3DBLCLK :
  320.         mouseButtonDblClk(hwnd, msg, pt) ;
  321.     break ;
  322.     }
  323.     return ;
  324. }
  325.