home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / keybset.zip / keybset.c < prev    next >
Text File  |  1995-03-20  |  5KB  |  220 lines

  1. /***************************************************************************\
  2. * keybset.c - this is the VIO part of the ShiftLock project   950320
  3. *
  4. * Copyright (c) 1995 Martin Lafaix. All Rights Reserved.
  5. \***************************************************************************/
  6.  
  7. #define INCL_DOSFILEMGR
  8. #define INCL_DOSDEVICES
  9. #define INCL_DOSDEVIOCTL
  10. #define INCL_WININPUT
  11.  
  12. #include <os2.h>
  13. #include <stdio.h>
  14.  
  15. HFILE hf;
  16. SHIFTSTATE ss;
  17. BYTE KeyState[257];
  18. BOOL bFlag;
  19. APIRET rc;
  20.  
  21. static VOID _error(const char *pch)
  22. /* 
  23.  * This function prints an error message on stderr, and halts program
  24.  * execution.
  25.  *
  26.  * Input:  pch - the offending command-line parameter.
  27.  * Returns:  1.
  28.  */
  29. {
  30.   fprintf(stderr, "KEYBSET: error - Invalid switch : %s\n", pch);
  31.   exit(1);
  32. }
  33.  
  34. static VOID _verbose(VOID)
  35. /* 
  36.  * This function prints program informations on stdout.
  37.  *
  38.  * Returns:  nothing.
  39.  */
  40. {
  41.   puts("Operating System/2  Keyboard Flags Modifier");
  42.   puts("Version 0.0.001 Mar 20 1995");
  43.   puts("(C) Copyright Martin Lafaix 1995");
  44.   puts("All rights reserved.\n");
  45.  
  46.   puts("Usage:  keybset [option[:[ON|OFF]]] ...");
  47.   puts("        /Capslock");
  48.   puts("        /Numlock");
  49.   puts("        /Scrolllock");
  50. }
  51.  
  52. static VOID _setflag(BOOL bSet, USHORT usvk, USHORT usflag)
  53. /* 
  54.  * This function sets or resets specified flag.
  55.  *
  56.  * We use the KeyState and ss global structures.
  57.  *
  58.  * Input:  bSet - new flag state.
  59.  *         usvk - flag's corresponding virtual key code.
  60.  *         usflag - flag's corresponding value.
  61.  * Returns:  nothing.
  62.  */
  63. {
  64.   if(bSet)
  65.     {
  66.     KeyState[usvk] |= 0x01;
  67.     ss.fsState |= usflag;
  68.     }
  69.   else
  70.     {
  71.     KeyState[usvk] &= ~0x01;
  72.     ss.fsState &= ~usflag;
  73.     }
  74. }
  75.  
  76. static VOID _readstate(VOID)
  77. /* 
  78.  * This function reads the current keyboard state, and stores the results
  79.  * in the KeyState and ss global structures.
  80.  *
  81.  * We use the hf global variable, as well as the KeyState and ss global
  82.  * structures.
  83.  *
  84.  * Returns:  nothing.
  85.  */
  86. {
  87.   ULONG ulAction, ulLength;
  88.  
  89.   rc = DosOpen("KBD$",
  90.                &hf,
  91.                &ulAction,
  92.                0L,
  93.                0,
  94.                FILE_OPEN,
  95.                OPEN_ACCESS_READONLY|OPEN_SHARE_DENYNONE,
  96.                0);
  97.  
  98.   ulAction = 0;
  99.   ulLength = sizeof(ss);
  100.   rc = DosDevIOCtl(hf,
  101.                    IOCTL_KEYBOARD,
  102.                    KBD_GETSHIFTSTATE,
  103.                    0,
  104.                    0,
  105.                    &ulAction,
  106.                    &ss,
  107.                    sizeof(ss),
  108.                    &ulLength);
  109.  
  110.   WinSetKeyboardStateTable(HWND_DESKTOP, &KeyState[1], FALSE);
  111. }
  112.  
  113. static VOID _writestate(VOID)
  114. /* 
  115.  * This function sets the keyboard state as described by the KeyState
  116.  * and ss global structures.
  117.  *
  118.  * We use the hf global variable as well as the KeyState and ss global
  119.  * structures.
  120.  *
  121.  * Returns:  nothing.
  122.  */
  123. {
  124.   ULONG ulAction, ulLength;
  125.   unsigned short w;
  126.  
  127.   WinSetKeyboardStateTable(HWND_DESKTOP, &KeyState[1], TRUE);
  128.  
  129.   ulAction = sizeof(ss);
  130.   ulLength = 0;
  131.   rc = DosDevIOCtl(hf,
  132.                    IOCTL_KEYBOARD,
  133.                    KBD_SETSHIFTSTATE,
  134.                    &ss,
  135.                    sizeof(ss),
  136.                    &ulAction,
  137.                    0,
  138.                    0,
  139.                    &ulLength);
  140.  
  141.   w  = (ss.fsState & (CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON)) >> 4;
  142.  
  143.   ulAction = sizeof(w);
  144.   rc = DosDevIOCtl(hf,
  145.                    IOCTL_KEYBOARD,
  146.                    0x5A, /* KBD_ALTERKEYBOARDLED */
  147.                    &w,
  148.                    sizeof(w),
  149.                    &ulAction,
  150.                    0,
  151.                    0,
  152.                    &ulLength);
  153.  
  154.   rc = DosClose(hf);
  155. }
  156.  
  157. static BOOL _is(const char *pchOpt, const char *achName)
  158. /* 
  159.  * This function compares pchOpt with achName.  If pchOpt is an abbreviation
  160.  * of achName, it returns TRUE.
  161.  *
  162.  * Side effect:  bFlag is set if pchOpt ends with "", ":" or ":on".  It is
  163.  *               cleared otherwise.
  164.  *
  165.  * Input:  pchOpt - the parameter to examine.
  166.  *         achName - the reference value.
  167.  * Returns:  TRUE if pchOpt is an abbreviation of achName, FALSE otherwise.
  168.  */
  169. {
  170.   BOOL bRet = TRUE;
  171.  
  172.   bFlag = TRUE;
  173.  
  174.   if((pchOpt[0] == '-' || pchOpt[0] == '/') && pchOpt[1])
  175.     {
  176.     while(*++pchOpt && *achName && (*pchOpt|32) == *achName++);
  177.  
  178.     if(*pchOpt)
  179.       if(*pchOpt == ':')
  180.         bFlag = (pchOpt[1] == 0) || (((pchOpt[1]|32) == 'o') && ((pchOpt[2]|32) == 'n'));
  181.       else
  182.         bRet = FALSE;
  183.     else
  184.       bRet = TRUE;
  185.     }
  186.   else
  187.     bRet = FALSE;
  188.  
  189.   return bRet;
  190. }
  191.  
  192. int main(int argc, char *argv[], char *envp[])
  193. {
  194.   USHORT w;
  195.  
  196.   if(argc == 1 || (argc == 2 && argv[1][0] == '/' && argv[1][1] == '?'))
  197.     {
  198.     _verbose();
  199.     return 0;
  200.     }
  201.  
  202.   _readstate();
  203.  
  204.   for(w = 1; w < argc; w++)
  205.     if(_is(argv[w], "numlock"))
  206.       _setflag(bFlag, VK_NUMLOCK+1, NUMLOCK_ON);
  207.     else
  208.     if(_is(argv[w], "capslock"))
  209.       _setflag(bFlag, VK_CAPSLOCK+1, CAPSLOCK_ON);
  210.     else
  211.     if(_is(argv[w], "scrolllock"))
  212.       _setflag(bFlag, VK_SCRLLOCK+1, SCROLLLOCK_ON);
  213.     else
  214.       _error(argv[w]);
  215.  
  216.   _writestate();
  217.  
  218.   return 0;
  219. }
  220.