home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 27 Fixes20 / 27-Fixes20.zip / arnea103.zip / SV_DLL.CPP < prev    next >
Text File  |  1994-06-01  |  6KB  |  197 lines

  1. /*******************************************************
  2.    ARNEDLL.CPP - Source code to a DLL for implementing
  3.    foreign keyboard support in "OS/2 Performance beta I"
  4.  
  5.    Swedish version SV_DLL.CPP
  6.           
  7.    Compiled using IBM C/Set++ 2.1 (Probably works with other 
  8.                                   OS/2 C++ compilers)
  9.  
  10.    This code may be modified to suit a specific keyboard as 
  11.    long as all comments are included in unmodified form.
  12.  
  13.  
  14.    Do you have MICROLEARN SWITCHER - a freeware utility from us?
  15.    Available from ftp - filename of current version: MLSW101.ZIP
  16.  
  17.    (c) Jari Williamsson, MicroLearn Nordic, 1994
  18.    - programmer for MicroLearn Game Pack, Volume 1 & 2 for OS/2
  19.  
  20.  
  21.    If you have questions, please contact: jari@microlearn.se
  22.  
  23. *******************************************************/
  24.  
  25. #define INCL_PM
  26. #define INCL_DOSMODULEMGR
  27. #define INCL_DOSMEMMGR
  28. #include <os2.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31.  
  32. #define DLL_NAME "ARNEDLL" // File name of this DLL
  33.  
  34. #define ARR_SIZE 33 // Size of the array below
  35.  
  36. BOOL UseArneAnka; // Use/don't use flag
  37.  
  38. struct {
  39.    BOOL bAltGraf;     // Use Alt key or not
  40.    UCHAR ucScanCode;  // Scan code for key (only used with Alt key)
  41.    char chFrom;       /* Transform from this US char - used
  42.                          when bAltFraf is FALSE */
  43.    char chTo;         // Transform to this foreign char
  44. } usCharArray[ARR_SIZE] = {
  45.    /* To following table is for Swedish keyboard - modify to suit 
  46.       your keyboard: */
  47.    TRUE,  0x03, '2', '@',
  48.    TRUE,  0x04, '3', '£',
  49.    TRUE,  0x05, '4', '$',
  50.    TRUE,  0x08, '7', '{',
  51.    TRUE,  0x09, '8', '[',
  52.    TRUE,  0x0a, '9', ']',
  53.    TRUE,  0x0b, '0', '}',
  54.    TRUE,  0x0c, '-', '\\',
  55.    TRUE, 27, ']', '~',
  56.    FALSE, 0, '@', '"',
  57.    FALSE, 0, '~', '½',
  58.    FALSE, 0, '`', '⌡',
  59.    FALSE, 0, '$', '╧',
  60.    FALSE, 0, '^', '&',
  61.    FALSE, 0, '&', '/',
  62.    FALSE, 0, '*', '(',
  63.    FALSE, 0, '(', ')',
  64.    FALSE, 0, ')', '=',
  65.    FALSE, 0, '-', '+',
  66.    FALSE, 0, '_', '?',
  67.    FALSE, 0, '\\', 39,   // 39 = '
  68.    FALSE, 0, '|', '*',
  69.    FALSE, 0, '[', 'å',
  70.    FALSE, 0, '{', 'Å',
  71.    FALSE, 0, ';', 'ö',
  72.    FALSE, 0, ':', 'Ö',
  73.    FALSE, 0, 39,  'ä',   // 39 = '
  74.    FALSE, 0, '"', 'Ä',
  75.    FALSE, 0, '<', ';',
  76.    FALSE, 0, '>', ':',
  77.    FALSE, 0, '/', '-',
  78.    FALSE, 0, '?', '_',
  79.    FALSE, 0, '}', '^'
  80. };
  81.  
  82. void CharProc(PQMSG pqmMsg)
  83. // Entry for WM_CHAR filter messages:
  84. {
  85.    USHORT fsKeyFlags = (USHORT) SHORT1FROMMP(pqmMsg->mp1); // Keyboard flags
  86.    USHORT usVirtualKey = (USHORT) SHORT2FROMMP(pqmMsg->mp2); 
  87.                                           // Virtual key - if exist
  88.  
  89.    BYTE bArray[256]; // Keyboard state array
  90.    /* Check current keyboard state - also possible to check
  91.       by using the KC_xxxxxx flags: */
  92.    WinSetKeyboardStateTable(HWND_DESKTOP, bArray, FALSE);  
  93.    BOOL bAltGraf = (bArray[VK_ALT] & 0x80);
  94.    BOOL bShift = (bArray[VK_SHIFT] & 0x80);
  95.    BOOL bControl = (bArray[VK_CTRL] & 0x80);
  96.  
  97.    UCHAR chScanCode = CHAR4FROMMP(pqmMsg->mp1); // Get scancode
  98.  
  99.    if ((chScanCode == 0x3e)  && (bControl) && (!(fsKeyFlags & KC_KEYUP))) {
  100.         // Ctrl-F4 to toggle between using/not using Arne Anka:
  101.         DosBeep(1000, 100);
  102.         UseArneAnka = !UseArneAnka;
  103.         return;
  104.    } /* endif */
  105.    if (!UseArneAnka) return;
  106.  
  107.    // Return if the keyboard message is from a OS/2 or DOS window:
  108.    char szClassName[100];
  109.    WinQueryClassName(pqmMsg->hwnd, 100, (PCH) szClassName);
  110.    szClassName[100] = '\0';
  111.    if (strcmp(szClassName, "Shield") == 0) {
  112.       return; 
  113.    } /* endif */
  114.  
  115.    if (fsKeyFlags & KC_VIRTUALKEY) return; // Don't handle virtual keys
  116.  
  117.    if (chScanCode == 86) {
  118.       // Special key for Swedish keyboard - not present on US keyboards:
  119.       UCHAR uch;
  120.       if (bAltGraf) {
  121.          uch = '|';
  122.       } else if (bShift) {
  123.          uch = '>';
  124.       } else {
  125.          uch = '<';
  126.       }
  127.       fsKeyFlags |= KC_CHAR;
  128.       fsKeyFlags &= ~(KC_ALT | KC_SHIFT);
  129.       pqmMsg->mp1 = MPFROM2SHORT(fsKeyFlags,
  130.                               SHORT2FROMMP(pqmMsg->mp1));
  131.       pqmMsg->mp2 = MPFROM2SHORT(uch,
  132.                               SHORT2FROMMP(pqmMsg->mp2));
  133.       return;
  134.    } /* endif */
  135.  
  136.    if (chScanCode > 0x40) return; // Don't handle numeric keypad etc.
  137.  
  138.    USHORT fsCharacter = (USHORT) SHORT1FROMMP(pqmMsg->mp2);
  139.    for (int i = 0; i < ARR_SIZE; i++) {
  140.       if (usCharArray[i].bAltGraf) {
  141.          // Check for scancode + Alt key:
  142.          if (!bAltGraf) continue;
  143.          if (usCharArray[i].ucScanCode == chScanCode) {
  144.             fsKeyFlags |= KC_CHAR;
  145.             fsKeyFlags &= ~KC_ALT;
  146.             pqmMsg->mp1 = MPFROM2SHORT(
  147.                               fsKeyFlags,
  148.                               SHORT2FROMMP(pqmMsg->mp1));
  149.             pqmMsg->mp2 = MPFROM2SHORT(usCharArray[i].chTo,
  150.                               SHORT2FROMMP(pqmMsg->mp2));
  151.             return;
  152.          } // if
  153.  
  154.       } else {
  155.          // Check for US char and transform to foreign char:
  156.          if (bAltGraf) continue;
  157.          if (fsCharacter == usCharArray[i].chFrom) {
  158.             pqmMsg->mp2 = MPFROM2SHORT(usCharArray[i].chTo,
  159.                               SHORT2FROMMP(pqmMsg->mp2));
  160.             return;
  161.          } // if
  162.       }
  163.    } // for
  164. }
  165.  
  166. BOOL EXPENTRY MLSwitchHook(HAB habAnchor, PQMSG pqmMsg, ULONG fs)
  167. // Hook entry:
  168. {
  169.    switch (pqmMsg->msg) {
  170.       case WM_CHAR:
  171.          CharProc(pqmMsg);
  172.          break;
  173.    } // switch
  174.  
  175.    return FALSE;
  176. }
  177.  
  178.  
  179. BOOL EXPENTRY ArneAInit(HAB hab)
  180. // Init av DLL:
  181. {
  182.    HMODULE hmModule;
  183.    if (DosQueryModuleHandle(DLL_NAME, &hmModule)) {
  184.       return FALSE; // Failed to get handle
  185.    } // if
  186.  
  187.    // Set system hook:
  188.    WinSetHook(hab,
  189.               NULLHANDLE,
  190.               HK_INPUT,
  191.               (PFN)MLSwitchHook,
  192.               hmModule);
  193.  
  194.    UseArneAnka = TRUE; // Use Arne Anka by default
  195.    return TRUE;
  196. }
  197.