home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12331.ZIP / KMSTATUS.C < prev    next >
C/C++ Source or Header  |  1989-04-02  |  12KB  |  425 lines

  1. /* kmstatus.c RHS 4/1/89
  2.  *
  3.  * Uses    OS/2 keyboard and mouse    routines.
  4.  */
  5.  
  6. #define    INCL_SUB
  7. #define    INCL_DOSERRORS
  8. #define    INCL_DOS
  9. #include<stdio.h>
  10. #include<string.h>
  11. #include<assert.h>
  12. #include<stdlib.h>
  13. #include<process.h>
  14. #include<malloc.h>
  15. #include<conio.h>
  16. #include<dos.h>
  17. #include<os2.h>
  18.  
  19. #define    VIOHDL                0
  20. #define    THREADSTACK            800                    /* size    of stack each thread*/
  21.  
  22. #define    MOU_NBUTTON_MOVE    0x0001
  23. #define    MOU_B1_MOVE            0x0002
  24. #define    MOU_B1_NMOVE        0x0004
  25. #define    MOU_B2_MOVE            0x0008
  26. #define    MOU_B2_NMOVE        0x0010
  27. #define    MOU_B3_MOVE            0x0020
  28. #define    MOU_B3_NMOVE        0x0040
  29.  
  30. #define    MOU_B1                (MOU_B1_MOVE | MOU_B1_NMOVE)
  31. #define    MOU_B2                (MOU_B2_MOVE | MOU_B2_NMOVE)
  32. #define    MOU_B3                (MOU_B3_MOVE | MOU_B3_NMOVE)
  33.  
  34. #define    MOUNOMOVE  (MOU_B1_NMOVE | MOU_B2_NMOVE    | MOU_B3_NMOVE)
  35. #define    MOUMOVED   (MOU_NBUTTON_MOVE | MOU_B1_MOVE | MOU_B2_MOVE | MOU_B3_MOVE)
  36.  
  37. #define    STATUS_ROW        0
  38. #define    BUTTON_ROW        7
  39. #define    PRESSED_COL        3
  40. #define    RELEASED_COL    42
  41. #define    KBD_ROW            20
  42.  
  43. #define    MouNoMove(event)                (MOUNOMOVE & event.fs)
  44. #define    MouMoved(event)                    (MOUMOVED &    event.fs)
  45. #define    MouButtonPressed(event,button)    (event.fs &    button)
  46. #define    MouB1Pressed(event)                (event & MOU_B1)
  47. #define    MouB2Pressed(event)                (event & MOU_B2)
  48. #define    MouB3Pressed(event)                (event & MOU_B3)
  49. #define    MouButtons(event)                (event.fs &    (MOU_B1    | MOU_B2 | MOU_B3))
  50.  
  51. #define    LEFTSHIFT                0x0002
  52. #define    LEFTCTRL                0x0100
  53. #define    LEFTALT                    0x0200
  54. #define    CAPS                    0x0040
  55. #define    NUMLOCK                    0x0020
  56. #define    SCROLLLOCK                0x0010
  57. #define    INSERT                    0x0080
  58. #define    SYSREQ                    0x8000
  59. #define    RIGHTALT                0x0800
  60. #define    RIGHTCTRL                0x0400
  61. #define    RIGHTSHIFT                0x0001
  62. #define    ECHO                    0x0001
  63. #define    RAW                        0x0004
  64. #define    COOKED                    0x0008
  65.  
  66. #define    KbdLeftShift(info)        (info.fsState &    LEFTSHIFT)
  67. #define    KbdLeftCtrl(info)        (info.fsState &    LEFTCTRL)
  68. #define    KbdLeftAlt(info)        (info.fsState &    LEFTALT)
  69. #define    KbdCapsL(info)            (info.fsState &    CAPS)
  70. #define    KbdNumL(info)            (info.fsState &    NUMLOCK)
  71. #define    KbdScrLock(info)        (info.fsState &    SCROLLLOCK)
  72. #define    KbdInsert(info)            (info.fsState &    INSERT)
  73. #define    KbdSysReq(info)            (info.fsState &    SYSREQ)
  74. #define    KbdRightAlt(info)        (info.fsState &    RIGHTALT)
  75. #define    KbdRightCtrl(info)        (info.fsState &    RIGHTCTRL)
  76. #define    KbdRightShift(info)        (info.fsState &    RIGHTSHIFT)
  77.  
  78. #define    KbdEcho(info)            (info.fsMask & ECHO)
  79. #define    KbdMode(info)            (info.fsMask & RAW)
  80.  
  81. typedef    struct _button
  82.     {
  83.     int    startrow;
  84.     int    startcol;
  85.     int    endrow;
  86.     int    endcol;
  87.     char **text;
  88.     } BUTTON;
  89.  
  90. #define    QUIT_LENGTH    5
  91. char *quit_button_text[QUIT_LENGTH+1] =
  92.     {
  93.     "╒════════╕",
  94.     "│        │",
  95.     "│  QUIT  │",
  96.     "│        │",
  97.     "╘════════╛",
  98.     "\0"
  99.     };
  100.  
  101. BUTTON quit_button = {12,68,12+QUIT_LENGTH-1,77,quit_button_text};
  102.  
  103. #define    CA_LENGTH 5
  104. char *collision_area_text[CA_LENGTH+1] =
  105.     {
  106.     "╒══════════════════════════════════╕",
  107.     "│                                  │",
  108.     "│ You can't get here from there... │",
  109.     "│                                  │",
  110.     "╘══════════════════════════════════╛",
  111.     "\0"
  112.     };
  113.  
  114. BUTTON collision_area =    {12,3,12+CA_LENGTH-1,38,collision_area_text};
  115.  
  116. #define    HSCF_LENGTH    5
  117. char *hscf_button_text[HSCF_LENGTH+1] =
  118.     {
  119.     "╒══════════╕",
  120.     "│HORIZONTAL│",
  121.     "│  SCALING │",
  122.     "│  FACTOR  │",
  123.     "╘══════════╛",
  124.     "\0"
  125.     };
  126.  
  127. BUTTON hscf_button = {12,42,12+HSCF_LENGTH-1,53,hscf_button_text};
  128.  
  129. #define    VSCF_LENGTH    5
  130. char *vscf_button_text[VSCF_LENGTH+1] =
  131.     {
  132.     "╒════════╕",
  133.     "│VERTICAL│",
  134.     "│ SCALING│",
  135.     "│ FACTOR │",
  136.     "╘════════╛",
  137.     "\0"
  138.     };
  139.  
  140. BUTTON vscf_button = {12,55,12+VSCF_LENGTH-1,64,vscf_button_text};
  141.  
  142. #define    MODE_LENGTH    3
  143. char *mode_button_text[MODE_LENGTH+1] =
  144.     {
  145.     "╒══════════╕",
  146.     "│INPUT MODE│",
  147.     "╘══════════╛",
  148.     "\0"
  149.     };
  150.  
  151. BUTTON mode_button = {19,60,19+MODE_LENGTH-1,71,mode_button_text};
  152.  
  153. long modeSem = 0L;
  154. unsigned InputMode = 0;
  155.  
  156. unsigned char MouseThreadStack[THREADSTACK];
  157. unsigned char KeyboardThreadStack[THREADSTACK];
  158.  
  159. void main(void);
  160. void display_button(BUTTON *button,unsigned    char attr);
  161. unsigned ButtonPressed(BUTTON *b,unsigned mask,MOUEVENTINFO    *event);
  162. void MouseThread(void);
  163. void KeyboardThread(void);
  164.  
  165.  
  166. void main(void)
  167.     {
  168.                                                 /* start mouse thread        */
  169.     if(_beginthread(MouseThread,MouseThreadStack,THREADSTACK,NULL) == -1)
  170.         DosExit(EXIT_PROCESS,-1);
  171.  
  172.     if(_beginthread(KeyboardThread,KeyboardThreadStack,THREADSTACK,NULL) ==    -1)
  173.         DosExit(EXIT_PROCESS,-1);
  174.  
  175.     while(TRUE)
  176.         DosSleep(-1L);
  177.     }
  178.  
  179. void display_button(BUTTON *b, unsigned    char attr)
  180.     {
  181.     int    i;
  182.  
  183.     for(i =    0; *b->text[i];    i++)
  184.         VioWrtCharStrAtt(b->text[i],strlen(b->text[i]),b->startrow+i,
  185.                 b->startcol,&attr,VIOHDL);
  186.     }
  187.  
  188. unsigned ButtonPressed(BUTTON *b,unsigned mask,MOUEVENTINFO    *event)
  189.     {
  190.     unsigned ev    = event->fs, row = event->row, col = event->col, retval    = 0;
  191.     unsigned char attr = 0x4f;
  192.  
  193.     ev &= 0xff7e;                                /* turn    off    the    low    bit        */
  194.  
  195.     if(!(ev    & mask))                            /* if button not pressed    */
  196.         return 0;
  197.                                                 /* if pointer on button        */
  198.     if((row    >= b->startrow)    && (row    <= b->endrow) && (col >= b->startcol) &&
  199.             (col <=    b->endcol))
  200.         {
  201.         display_button(b,attr);                    /* redisplay highlighted    */
  202.         DosSleep(100L);                            /* wait    a sec...            */
  203.         attr = 0x17;
  204.         display_button(b,attr);                    /* redisplay normal            */
  205.         return ev;
  206.         }
  207.     return 0;                                    /* pointer not on button    */
  208.     }
  209.  
  210.  
  211. void KeyboardThread(void)
  212.     {
  213.     KBDINFO    kbdinfo;
  214.     KBDKEYINFO kbdkeyinfo;
  215.     HKBD KbdHandle = 0;
  216.  
  217.     char Outstr[85];
  218.     unsigned inputmode = 0;
  219.  
  220.     strcpy(Outstr,"Ascii Scan");
  221.     VioWrtCharStr(Outstr,strlen(Outstr),KBD_ROW-1,15,VIOHDL);
  222.     KbdFlushBuffer(KbdHandle);                    /* flush keyboard buffer    */
  223.  
  224.     while(TRUE)
  225.         {
  226.         DosSemRequest(&modeSem,-1L);
  227.         if(inputmode !=    InputMode)                /* if mode has changed        */
  228.             {
  229.             if(InputMode)                        /* if COOKED mode            */
  230.                 {
  231.                 kbdinfo.fsMask &= ~RAW;            /* turn    off    RAW    bit            */
  232.                 kbdinfo.fsMask |= COOKED;        /* turn    on COOKED bit        */
  233.                 }
  234.             else
  235.                 {
  236.                 kbdinfo.fsMask &= ~COOKED;        /* turn    off    COOKED bit        */
  237.                 kbdinfo.fsMask |= RAW;            /* turn    on RAW bit            */
  238.                 }
  239.             KbdSetStatus(&kbdinfo,KbdHandle);
  240.             inputmode =    InputMode;
  241.             }
  242.         DosSemClear(&modeSem);
  243.         KbdGetStatus(&kbdinfo,KbdHandle);
  244.         sprintf(Outstr,"States: Echo is %3s  Input Mode is %6s",
  245.             KbdEcho(kbdinfo) ? "ON"    : "OFF",
  246.             KbdMode(kbdinfo) ? "RAW" : "COOKED");
  247.         VioWrtCharStr(Outstr,strlen(Outstr),KBD_ROW,0,VIOHDL);
  248.  
  249.         KbdCharIn(&kbdkeyinfo,IO_WAIT,KbdHandle);
  250.         sprintf(Outstr,
  251.                 "%5s %4s %3s [%3d] [%3d] %4s %7s %11s %6s %6s %3s %4s %5s",
  252.                 KbdLeftShift(kbdkeyinfo) ? "SHIFT" : " ",
  253.                 KbdLeftCtrl(kbdkeyinfo)    ? "CTRL" : " ",
  254.                 KbdLeftAlt(kbdkeyinfo) ? "ALT" : " ",
  255.                 kbdkeyinfo.chChar,
  256.                 kbdkeyinfo.chScan,
  257.                 KbdCapsL(kbdkeyinfo) ? "CAPS" :    " ",
  258.                 KbdNumL(kbdkeyinfo)    ? "NUMLOCK"    : " ",
  259.                 KbdScrLock(kbdkeyinfo) ? "SCROLL-LOCK" : " ",
  260.                 KbdInsert(kbdkeyinfo) ?    "INSERT" : " ",
  261.                 KbdSysReq(kbdkeyinfo) ?    "SYSREQ" : " ",
  262.                 KbdRightAlt(kbdkeyinfo)    ? "ALT"    : " ",
  263.                 KbdRightCtrl(kbdkeyinfo) ? "CTRL" :    " ",
  264.                 KbdRightShift(kbdkeyinfo) ?    "SHIFT"    : " ");
  265.  
  266.         VioWrtCharStr(Outstr,strlen(Outstr),KBD_ROW+2,0,VIOHDL);
  267.         }
  268.     }
  269.  
  270.  
  271. void MouseThread(void)
  272.     {
  273.     MOUEVENTINFO MouEvent;
  274.     MOUQUEINFO mouqueinfo;
  275.     SCALEFACT scalefact;
  276.  
  277.     char Outstr[90];
  278.     unsigned BlankCell = 0x1720;
  279.     char linechar =    '═';
  280.     unsigned char attr = 0x17;
  281.  
  282.     HMOU MouHandle;
  283.     unsigned Status,numbuttons,nummickeys;
  284.     int    WaitOption = 1;                            /* set to block    on input    */
  285.     char *p,*q;
  286.     int    i;
  287.     unsigned buttonmasks[3], button;
  288.     char buttondown[3],blank = ' ';
  289.  
  290.     if((Status = MouOpen((PSZ)NULL,(PHMOU)&MouHandle)))
  291.         {
  292.         printf("%u error opening Mouse \n",Status);
  293.         DosExit(EXIT_PROCESS,0);
  294.         }
  295.  
  296.     DosSemClear(&modeSem);                       /* so keyboard thread can go    */
  297.  
  298.     VioScrollUp(0,0,-1,-1,-1,(char *)&BlankCell,VIOHDL);
  299.     VioWrtNChar(&linechar,80,18,0,VIOHDL);
  300.  
  301.     display_button(&collision_area,attr);        /* display collision area    */
  302.     display_button(&hscf_button,attr);            /* display hscf    button        */
  303.     display_button(&vscf_button,attr);            /* display vscf    button        */
  304.     display_button(&quit_button,attr);            /* display quit    button        */
  305.     display_button(&mode_button,attr);            /* display mode    button        */
  306.  
  307.     buttonmasks[0] = MOU_B1;
  308.     buttonmasks[1] = MOU_B2;
  309.     buttonmasks[2] = MOU_B3;
  310.     memset(buttondown,0,sizeof(buttondown));
  311.  
  312.     MouDrawPtr(MouHandle);                        /* display mouse pointer    */
  313.                                                 /* lock    out    collision area    */
  314.     MouRemovePtr((NOPTRRECT    *)&collision_area,MouHandle);
  315.  
  316.     MouGetNumMickeys(&nummickeys,MouHandle);    /* get mickey count            */
  317.     MouGetNumButtons(&numbuttons,MouHandle);    /* get button count            */
  318.  
  319.     p =    "Mouse Button:";
  320.     VioWrtCharStr(p,strlen(p),BUTTON_ROW-1,0,VIOHDL);
  321.     q =    "1:";
  322.     for(i =    0; i < numbuttons; i++)
  323.         {
  324.         *q = (char)(i+'0'+1);
  325.         VioWrtCharStr(q,strlen(q),i+BUTTON_ROW,0,VIOHDL);
  326.         }
  327.  
  328.     sprintf(Outstr,"Mickeys per Centimeter: %2u  Number of Buttons:%u",
  329.             nummickeys,numbuttons);
  330.                                                 /* display statistics        */
  331.     VioWrtCharStr(Outstr,strlen(Outstr),0,0,VIOHDL);
  332.  
  333.     MouFlushQue(MouHandle);                        /* flush mouse queue        */
  334.  
  335.     while(TRUE)
  336.         {
  337.         MouGetDevStatus(&Status,MouHandle);        /* get status                */
  338.         MouGetScaleFact(&scalefact,MouHandle);    /* get scaling factor        */
  339.                                                 /* read    the    queue            */
  340.         MouReadEventQue(&MouEvent,&WaitOption,MouHandle);
  341.         MouGetNumQueEl(&mouqueinfo,MouHandle);    /* any events left in queue?*/
  342.  
  343.         sprintf(Outstr,
  344.                 "Time:%8ld  Event Mask:%04x  Status:%04x  Pending Events:%u ",
  345.                 MouEvent.Time, MouEvent.fs,    Status,    mouqueinfo.cEvents);
  346.                                                 /* display event information*/
  347.         VioWrtCharStr(Outstr,strlen(Outstr),1,18,VIOHDL);
  348.         sprintf(Outstr,
  349.                 "Horizontal Scaling Factor:%5u    Vertical Scaling Factor:%5u",
  350.                 scalefact.colScale,scalefact.rowScale);
  351.                                                 /* display scaling factor    */
  352.         VioWrtCharStr(Outstr,strlen(Outstr),2,0,VIOHDL);
  353.  
  354.         if(MouMoved(MouEvent))                    /* if moved, display row/col*/
  355.             {
  356.             sprintf(Outstr,"Row:%2d Col:%2d",MouEvent.row,MouEvent.col);
  357.             VioWrtCharStr(Outstr,strlen(Outstr),1,0,VIOHDL);
  358.             }
  359.  
  360.         for(i =    0; i < numbuttons; i++)            /* check each button        */
  361.                                                 /* if mouse    button is down    */
  362.             if(MouButtonPressed(MouEvent,buttonmasks[i]))
  363.                 {
  364.                 if(!buttondown[i])                /* and wasn't already down  */
  365.                     {
  366.                     sprintf(Outstr," Pressed @ Row:%2u Col:%2u Time:%8ld",
  367.                             MouEvent.row,MouEvent.col,MouEvent.Time);
  368.                     VioWrtNChar(&blank,80-PRESSED_COL,BUTTON_ROW+i,
  369.                             PRESSED_COL,VIOHDL);
  370.                     VioWrtCharStr(Outstr,strlen(Outstr),BUTTON_ROW+i,3,VIOHDL);
  371.                     buttondown[i] =    TRUE;
  372.                     }
  373.                 }
  374.             else                                /* if mouse    button is up    */
  375.                 {
  376.                 if(buttondown[i])                /* and was previously down    */
  377.                     {
  378.                     sprintf(Outstr,"Released @ Row:%2u Col:%2u Time:%8ld",
  379.                             MouEvent.row,MouEvent.col,MouEvent.Time);
  380.                     VioWrtCharStr(Outstr,strlen(Outstr),BUTTON_ROW+i,
  381.                             RELEASED_COL,VIOHDL);
  382.                     buttondown[i] =    FALSE;
  383.                     }
  384.                 }
  385.                                                 /* if Quit button pressed    */
  386.         if(ButtonPressed(&quit_button,0xffff,&MouEvent))
  387.             break;
  388.                                                 /* if Horizontal SF    button    */
  389.         if(button =    ButtonPressed(&hscf_button,0xffff,&MouEvent))
  390.             {
  391.             if(MouB1Pressed(button))            /* if button1 (increment)    */
  392.                 scalefact.colScale++;
  393.             if(MouB2Pressed(button))            /* if button2 (decrement)    */
  394.                 scalefact.colScale--;
  395.             MouSetScaleFact(&scalefact,MouHandle);
  396.             }
  397.                                                 /* if Vertical SF button    */
  398.         if(button =    ButtonPressed(&vscf_button,0xffff,&MouEvent))
  399.             {
  400.             if(MouB1Pressed(button))            /* if button1 (increment)    */
  401.                 scalefact.rowScale++;
  402.             if(MouB2Pressed(button))            /* if button2 (decrement)    */
  403.                 scalefact.rowScale--;
  404.             MouSetScaleFact(&scalefact,MouHandle);
  405.             }
  406.         if(ButtonPressed(&mode_button,0xffff,&MouEvent))
  407.             {
  408.             DosSemRequest(&modeSem,-1L);
  409.             InputMode =    !InputMode;
  410.             DosSemClear(&modeSem);
  411.             }
  412.         DosSleep(32L);
  413.         }
  414.  
  415.     MouClose(MouHandle);
  416.  
  417.     BlankCell =    0x0720;
  418.     VioScrollUp(0,0,-1,-1,-1,(char *)&BlankCell,VIOHDL);
  419.  
  420.     DosExit(EXIT_PROCESS,0);
  421.     }
  422.  
  423.     /* end of kmstatus.c    */
  424.  
  425.