home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / mouse.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  5KB  |  183 lines

  1. #include <config.h>
  2. #include "src/mouse.h"
  3.  
  4. #define  INCL_DOS
  5. #define  INCL_VIO
  6. #define  INCL_KBD
  7. #define  INCL_MOU
  8. #define  INCL_DOSPROCESS
  9.  
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stddef.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <sys/time.h>
  17.  
  18. #define MOU_BUFF_SIZE 0x200
  19. static int MouseBuffer[MOU_BUFF_SIZE]; 
  20. static int MouseBufferHead,MouseBufferTail;
  21.  
  22. static MouDataValid=0;
  23.  
  24. int mouse_has_data()
  25. { return MouDataValid;
  26. }
  27.  
  28. static
  29. int mouse_getch()
  30. { int rc=-1;
  31.   if(MouseBufferHead!=MouseBufferTail)
  32.     { rc=MouseBuffer[MouseBufferTail++];
  33.       MouseBufferTail%=MOU_BUFF_SIZE;
  34.       MouDataValid--;
  35.     }
  36.   return rc;
  37. }
  38.  
  39. static
  40. void mouse_to_buff(int event, int col, int row)
  41. {  int d=MouseBufferTail-MouseBufferHead;
  42.    if(d<=0)d+=MOU_BUFF_SIZE;
  43.    if(d>2){ MouseBuffer[MouseBufferHead++]=event;
  44.             MouseBufferHead%=MOU_BUFF_SIZE;
  45.             MouseBuffer[MouseBufferHead++]=col;
  46.             MouseBufferHead%=MOU_BUFF_SIZE;
  47.             MouseBuffer[MouseBufferHead++]=row;
  48.             MouseBufferHead%=MOU_BUFF_SIZE;
  49.             MouDataValid+=3;  
  50.           } 
  51. /*   DosBeep(600,50); */
  52. }
  53.  
  54. #define BTN1 1
  55. #define BTN2 2
  56. #define BTN3 3
  57. #define BTUP 4
  58. #define MMOT 128
  59.  
  60. static int mouse_Quit=0;
  61. static MOUEVENTINFO mouev;
  62. static HMOU hmou;
  63.  
  64. static 
  65. void ReadMouse(void *Data)
  66. {
  67.      static int oldfs = 0; 
  68.      USHORT     fWait = MOU_WAIT;
  69.      NOPTRRECT  mourt = { 0,0,24,79 };
  70.  
  71.      mourt.cRow=GetScrCols()-1;
  72.      mourt.cCol=GetScrRows()-1;
  73.  
  74.      MouOpen(NULL,&hmou);
  75.      MouDrawPtr(hmou);
  76.  
  77.      do
  78.      {
  79.           MouReadEventQue(&mouev,&fWait,hmou);
  80.  
  81.           if(mouev.time)
  82.           { MouRemovePtr(&mourt,hmou);
  83.             if(mouev.fs & MOUSE_BN1_DOWN )
  84.                  mouse_to_buff(BTN1, mouev.col, mouev.row);
  85.             else if(mouev.fs & MOUSE_BN2_DOWN )
  86.                  mouse_to_buff(BTN2, mouev.col, mouev.row);
  87.             else if(mouev.fs & MOUSE_BN3_DOWN )
  88.                  mouse_to_buff(BTN3, mouev.col, mouev.row);
  89.             else if(mouev.fs & MOUSE_MOTION_WITH_BN1_DOWN )
  90.                  mouse_to_buff(BTN1|MMOT, mouev.col, mouev.row);
  91.             else if(mouev.fs & MOUSE_MOTION_WITH_BN2_DOWN )
  92.                  mouse_to_buff(BTN2|MMOT, mouev.col, mouev.row);
  93.             else if(mouev.fs & MOUSE_MOTION_WITH_BN3_DOWN )
  94.                  mouse_to_buff(BTN3|MMOT, mouev.col, mouev.row);
  95.             if(!mouev.fs)
  96.                { if(oldfs) mouse_to_buff(BTUP, mouev.col, mouev.row);
  97.                  else mouse_to_buff(0, mouev.col, mouev.row);
  98.                }
  99.             oldfs=mouev.fs;
  100.             MouDrawPtr(hmou);
  101.           }
  102.  
  103.      }while(!mouse_Quit );
  104.  
  105.      DosExit(EXIT_THREAD, 0L );
  106. }
  107.  
  108.  
  109. #define STACK_SIZE_MOUTHRD  32768
  110.  
  111. int os2_create_mouse_thread(void)
  112. { int sValue;
  113.   return _beginthread(  ReadMouse,
  114.                         NULL,
  115.                         STACK_SIZE_MOUTHRD,
  116.                         &sValue)<0?-1:0;
  117. }
  118.  
  119. /* This macros were stolen from gpm 0.15 */
  120. #define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *)NULL))
  121. #define DIF_TIME(t1,t2) ((t2.tv_sec -t1.tv_sec) *1000+ \
  122.              (t2.tv_usec-t1.tv_usec)/1000)
  123.  
  124. extern int double_click_speed;
  125.              
  126. int os2_mouse_get_event (Gpm_Event *ev)
  127. {
  128.     int btn;
  129.     static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
  130.     static struct timeval tv2;
  131.     static int clicks;
  132.  
  133.     /* Decode Xterm mouse information to a GPM style event */
  134.  
  135.     /* Variable btn has following meaning: */
  136.     /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
  137.     btn = mouse_getch ();
  138.     if(btn<0)return -1; 
  139.         
  140.     /* There seems to be no way of knowing which button was released */
  141.     /* So we assume all the buttons were released */
  142.     if(!btn){
  143.         ev->type = GPM_MOVE;
  144.         ev->buttons = 0;
  145.     GET_TIME (tv1);
  146.     } else if (btn == BTUP){
  147.         ev->type = GPM_UP | (GPM_SINGLE << clicks);
  148.         ev->buttons = 0;
  149.     GET_TIME (tv1);
  150.     clicks = 0;
  151.     } else {
  152.         ev->type = btn&MMOT?GPM_DRAG:GPM_DOWN;
  153.     GET_TIME (tv2);
  154.     if (tv1.tv_sec && (DIF_TIME (tv1,tv2) < double_click_speed)){
  155.         clicks++;
  156.         clicks %= 3;
  157.     } else
  158.         clicks = 0;
  159.     
  160.         switch (btn&0xF) {
  161.     case BTN1:
  162.             ev->buttons = GPM_B_LEFT;
  163.             break;
  164.     case BTN2:
  165.             ev->buttons = GPM_B_RIGHT;
  166.             break;
  167.     case BTN3:
  168.             ev->buttons = GPM_B_MIDDLE;
  169.             break;
  170.     default:
  171.             /* Nothing */
  172.             break;
  173.         }
  174.     }
  175.  
  176.     ev->x = mouse_getch () +1;
  177.     ev->y = mouse_getch () +1;
  178.     return 0;
  179. }
  180.  
  181.  
  182.  
  183.