Release 960521
[wine] / misc / keyboard.c
1 /*
2 static char RCSId[] = "$Id: keyboard.c,v 1.2 1993/09/13 18:52:02 scott Exp $";
3 static char Copyright[] = "Copyright  Scott A. Laird, Erik Bos  1993, 1994";
4 */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include "windows.h"
11 #include "keyboard.h"
12 #include "stddebug.h"
13 /* #define DEBUG_KEYBOARD */
14 #include "debug.h"
15
16 int ToAscii(WORD wVirtKey, WORD wScanCode, LPSTR lpKeyState, 
17         LPVOID lpChar, WORD wFlags) 
18 {
19         int i;
20
21         dprintf_keyboard(stddeb,"ToAscii (%d,%d)\n",wVirtKey, wScanCode);
22
23         /* FIXME: codepage is broken */
24
25         for (i = 0 ; i != KeyTableSize ; i++) 
26                 if (KeyTable[i].virtualkey == wVirtKey)  
27                  {
28                    dprintf_keyboard(stddeb,"\t\tchar = %s\n", KeyTable[i].name);
29                    if( isprint(KeyTable[i].ASCII) || isspace(KeyTable[i].ASCII) )
30                      {
31                         *(BYTE*)lpChar = KeyTable[i].ASCII;
32                         *(((BYTE*)lpChar) + 1) = 0;
33
34                         if( isalpha( *(BYTE*)lpChar ) )
35                           if( (lpKeyState[VK_CAPITAL]<0 && !lpKeyState[VK_SHIFT]) ||
36                               (!lpKeyState[VK_CAPITAL] && lpKeyState[VK_SHIFT]<0) )
37                               *(BYTE*)lpChar = toupper( *(BYTE*)lpChar );
38                           else
39                               *(BYTE*)lpChar = tolower( *(BYTE*)lpChar );
40
41                         return 1;
42                      }
43                  }
44
45         *(BYTE*)lpChar = 0;
46         return 0;
47 }
48
49 DWORD OemKeyScan(WORD wOemChar)
50 {
51     dprintf_keyboard(stddeb,"*OemKeyScan (%d)\n",wOemChar);
52
53         return wOemChar;
54 }
55
56 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
57  * for the current keyboard. */
58
59 WORD VkKeyScan(WORD cChar)
60 {
61         int i;
62         
63         dprintf_keyboard(stddeb,"VkKeyScan (%d)\n",cChar);
64         
65         for (i = 0 ; i != KeyTableSize ; i++) 
66                 if (KeyTable[i].ASCII == cChar)
67                         return KeyTable[i].virtualkey;
68
69         return -1;
70 }
71
72 int GetKeyboardType(int nTypeFlag)
73 {
74   dprintf_keyboard(stddeb,"GetKeyboardType(%d)\n",nTypeFlag);
75   switch(nTypeFlag)
76     {
77     case 0:      /* Keyboard type */
78       return 4;    /* AT-101 */
79       break;
80     case 1:      /* Keyboard Subtype */
81       return 0;    /* There are no defined subtypes */
82       break;
83     case 2:      /* Number of F-keys */
84       return 12;   /* We're doing an 101 for now, so return 12 F-keys */
85       break;
86     default:     
87       fprintf(stderr, "Unknown type on GetKeyboardType\n");
88       return 0;    /* The book says 0 here, so 0 */
89     }
90 }
91
92 /* MapVirtualKey translates keycodes from one format to another. */
93
94 WORD MapVirtualKey(WORD wCode, WORD wMapType)
95 {
96         int i;
97         
98         switch(wMapType) {
99                 case 0:
100                         for (i = 0 ; i != KeyTableSize ; i++) 
101                                 if (KeyTable[i].virtualkey == wCode) 
102                                         return KeyTable[i].scancode;
103                         return 0;
104
105                 case 1:
106                         for (i = 0 ; i != KeyTableSize ; i++) 
107                                 if (KeyTable[i].scancode == wCode) 
108                                         return KeyTable[i].virtualkey;
109                         return 0;
110
111                 case 2:
112                         for (i = 0 ; i != KeyTableSize ; i++) 
113                                 if (KeyTable[i].virtualkey == wCode) 
114                                         return KeyTable[i].ASCII;
115                         return 0;
116
117                 default: 
118                         fprintf(stderr, "MapVirtualKey: unknown wMapType!\n");
119                         return 0;       
120         }
121         return 0;
122 }
123
124 int GetKbCodePage(void)
125 {
126         dprintf_keyboard(stddeb,"GetKbCodePage()\n");
127         return 850;
128 }
129
130 int GetKeyNameText(LONG lParam, LPSTR lpBuffer, int nSize)
131 {
132         int i;
133         
134         dprintf_keyboard(stddeb,"GetKeyNameText(%ld,<ptr>,%d)\n",lParam,nSize);
135
136         lParam >>= 16;
137         lParam &= 0xff;
138
139         for (i = 0 ; i != KeyTableSize ; i++) 
140                 if (KeyTable[i].scancode == lParam)  {
141                         lstrcpyn(lpBuffer, KeyTable[i].name, nSize);
142                         return strlen(lpBuffer);
143                 }
144
145         *lpBuffer = 0;
146         return 0;
147 }