update for HEAD-2003091401
[reactos.git] / lib / kbdus / kbdus.c
1 /*
2  * ReactOS USASCII Keyboard layout
3  * Copyright (C) 2003 ReactOS
4  * License: LGPL, see: LGPL.txt
5  * 
6  * Thanks to: http://www.barcodeman.com/altek/mule/scandoc.php
7  * and http://win.tue.nl/~aeb/linux/kbd/scancodes-1.html
8  */
9
10 #include <windows.h>
11 #include <internal/kbd.h>
12
13 #ifdef _M_IA64
14 #define ROSDATA static __declspec(allocate(".data"))
15 #else
16 #pragma data_seg(".data")
17 #define ROSDATA static
18 #endif
19
20 #define VK_EMPTY 0xff   /* The non-existent VK */
21 #define KSHIFT   0x001  /* Shift modifier */
22 #define KCTRL    0x002  /* Ctrl modifier */
23 #define KALT     0x004  /* Alt modifier */
24 #define KEXT     0x100  /* Extended key code */
25 #define KMULTI   0x200  /* Multi-key */
26 #define KSPEC    0x400  /* Special key */
27 #define KNUMP    0x800  /* Number-pad */
28 #define KNUMS    0xc00  /* Special + number pad */
29 #define KMEXT    0x300  /* Multi + ext */
30
31 /* Thanks to http://asp.flaaten.dk/pforum/keycode/keycode.htm */
32 #ifndef VK_OEM_1
33 #define VK_OEM_1  0xba
34 #endif
35 #ifndef VK_OEM_PLUS
36 #define VK_OEM_PLUS 0xbb
37 #endif
38 #ifndef VK_OEM_COMMA
39 #define VK_OEM_COMMA 0xbc
40 #endif
41 #ifndef VK_OEM_MINUS
42 #define VK_OEM_MINUS 0xbd
43 #endif
44 #ifndef VK_OEM_PERIOD
45 #define VK_OEM_PERIOD 0xbe
46 #endif
47 #ifndef VK_OEM_2
48 #define VK_OEM_2 0xbf
49 #endif
50 #ifndef VK_OEM_3
51 #define VK_OEM_3 0xc0
52 #endif
53 #ifndef VK_OEM_4
54 #define VK_OEM_4 0xdb
55 #endif
56 #ifndef VK_OEM_5
57 #define VK_OEM_5 0xdc
58 #endif
59 #ifndef VK_OEM_6
60 #define VK_OEM_6 0xdd
61 #endif
62 #ifndef VK_OEM_7
63 #define VK_OEM_7 0xde
64 #endif
65 #ifndef VK_OEM_8
66 #define VK_OEM_8 0xdf
67 #endif
68 #ifndef VK_OEM_102
69 #define VK_OEM_102 0xe1
70 #endif
71
72 ROSDATA USHORT scancode_to_vk[] = {
73   /* Numbers Row */
74   /* - 00 - */
75   /* 1 ...         2 ...         3 ...         4 ... */     
76   VK_EMPTY,     VK_ESCAPE,    '1',          '2',
77   '3',          '4',          '5',          '6',
78   '7',          '8',          '9',          '0',
79   VK_OEM_MINUS, VK_OEM_PLUS,  VK_BACK,
80   /* - 0f - */
81   /* First Letters Row */
82   VK_TAB,       'Q',          'W',          'E',
83   'R',          'T',          'U',          'I',
84   'O',          'P',          
85   VK_OEM_4,     VK_OEM_6,     VK_RETURN,
86   /* - 1c - */
87   /* Second Letters Row */
88   VK_LCONTROL,  
89   'A',          'S',          'D',          'F',
90   'G',          'H',          'J',          'K',
91   'L',          VK_OEM_1,     VK_OEM_7,     VK_OEM_3, 
92   VK_LSHIFT,    VK_OEM_5,
93   /* - 2c - */
94   /* Third letters row */
95   'Z',          'X',          'C',          'V',
96   'B',          'N',          'M',          VK_OEM_COMMA,
97   VK_OEM_PERIOD,VK_OEM_2,     VK_RSHIFT,
98   /* - 35 - */
99   /* Bottom Row */
100   VK_EMPTY,                   VK_RSHIFT | KEXT,
101   VK_MULTIPLY,                VK_LMENU,
102   ' ',                        VK_CAPITAL,
103   
104   /* - 3b - */
105   /* F-Keys */
106   VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, 
107   VK_F7, VK_F8, VK_F9, VK_F10,
108   /* - 45 - */
109   /* Locks */
110   VK_NUMLOCK | KMEXT,   
111   VK_SCROLL | KMULTI,    
112   /* - 47 - */
113   /* Number-Pad */
114   VK_HOME | KNUMS,      VK_UP | KNUMS,         VK_PRIOR | KNUMS, VK_SUBTRACT,
115   VK_LEFT | KNUMS,      VK_CLEAR | KNUMS,      VK_RIGHT | KNUMS, VK_ADD,
116   VK_END | KNUMS,       VK_DOWN | KNUMS,       VK_NEXT | KNUMS,
117   VK_INSERT | KNUMS,    VK_DELETE | KNUMS,
118   /* - 54 - */
119   /* Presumably PrtSc */
120   VK_SNAPSHOT,
121   /* - 55 - */
122   /* Oddities, and the remaining standard F-Keys */
123   VK_EMPTY,     VK_EMPTY,     VK_F11,       VK_F12,
124   /* - 59 - */
125   VK_CLEAR,     VK_EMPTY,     VK_EMPTY,     VK_EMPTY,     VK_EMPTY, /* EREOF */
126   VK_EMPTY,     VK_EMPTY,     VK_EMPTY,     VK_EMPTY,     VK_EMPTY, /* ZOOM */
127   VK_HELP,      
128   /* - 64 - */
129   /* Even more F-Keys (for example, NCR keyboards from the early 90's) */
130   VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19, VK_F20,
131   VK_F21, VK_F22, VK_F23, 
132   /* - 6f - */
133   /* Not sure who uses these codes */
134   VK_EMPTY, VK_EMPTY, VK_EMPTY,
135   /* - 72 - */
136   VK_EMPTY, VK_EMPTY, VK_EMPTY, VK_EMPTY,
137   /* - 76 - */
138   /* One more f-key */
139   VK_F24,
140   /* - 77 - */
141   VK_EMPTY, VK_EMPTY, VK_EMPTY, VK_EMPTY,
142   VK_EMPTY, VK_EMPTY, VK_EMPTY, VK_EMPTY, /* PA1 */
143   VK_EMPTY,
144   /* - 80 - */
145   0
146 };
147
148 ROSDATA VSC_VK extcode0_to_vk[] = {
149   { 0, 0 },
150 };
151
152 ROSDATA VSC_VK extcode1_to_vk[] = {
153   { 0, 0 },
154 };
155
156 #define VK_ALT 
157
158 ROSDATA VK_TO_BIT modifier_keys[] = {
159   { VK_SHIFT,   KSHIFT },
160   { VK_CONTROL, KCTRL },
161   { VK_MENU,    KALT },
162   { 0,          0 }
163 };
164
165 typedef struct _mymod {
166   PVOID mod_keys;
167   int maxmod;
168   int mod_max[4];
169 } INTERNAL_KBD_MODIFIERS;
170
171 ROSDATA INTERNAL_KBD_MODIFIERS modifier_bits[] = {
172   modifier_keys,
173   3,
174   { 0, 1, 2, 3 } /* Modifier bit order, NONE, SHIFT, CTRL, ALT */
175 };
176
177 #define NOCAPS 0
178 #define CAPS   KSHIFT /* Caps -> shift */
179
180 ROSDATA VK_TO_WCHARS2 key_to_chars_2mod[] = {
181   /* Normal vs Shifted */
182   /* The numbers */
183   { '1',         NOCAPS, '1', '!' },
184   /* Ctrl-2 generates NUL */
185   { '3',         NOCAPS, '3', '#' },
186   { '4',         NOCAPS, '4', '$' },
187   { '5',         NOCAPS, '5', '%' },
188   /* Ctrl-6 generates RS */
189   { '7',         NOCAPS, '7', '&' },
190   { '8',         NOCAPS, '8', '*' },
191   { '9',         NOCAPS, '9', '(' },
192   { '0',         NOCAPS, '0', ')' },
193   /* First letter row */
194   { 'Q',         CAPS,   'q', 'Q' },
195   { 'W',         CAPS,   'w', 'W' },
196   { 'E',         CAPS,   'e', 'E' },
197   { 'R',         CAPS,   'r', 'R' },
198   { 'T',         CAPS,   't', 'T' },
199   { 'Y',         CAPS,   'y', 'Y' },
200   { 'U',         CAPS,   'u', 'U' },
201   { 'I',         CAPS,   'i', 'I' },
202   { 'O',         CAPS,   'o', 'O' },
203   { 'P',         CAPS,   'p', 'P' },
204   /* Second letter row */
205   { 'A',         CAPS,   'a', 'A' },
206   { 'S',         CAPS,   's', 'S' },
207   { 'D',         CAPS,   'd', 'D' },
208   { 'F',         CAPS,   'f', 'F' },
209   { 'G',         CAPS,   'g', 'G' },
210   { 'H',         CAPS,   'h', 'H' },
211   { 'J',         CAPS,   'j', 'J' },
212   { 'K',         CAPS,   'k', 'K' },
213   { 'L',         CAPS,   'l', 'L' },
214   /* Third letter row */
215   { 'Z',         CAPS,   'z', 'Z' },
216   { 'X',         CAPS,   'x', 'X' },
217   { 'C',         CAPS,   'c', 'C' },
218   { 'V',         CAPS,   'v', 'V' },
219   { 'B',         CAPS,   'b', 'B' },
220   { 'N',         CAPS,   'n', 'N' },
221   { 'M',         CAPS,   'm', 'M' },
222
223   /* Specials */
224   /* Ctrl-_ generates US */
225   { VK_OEM_PLUS    ,NOCAPS, '=', '+' },
226   { VK_OEM_1       ,NOCAPS, ';', ':' },
227   { VK_OEM_7       ,NOCAPS, '\'','\"'},
228   { VK_OEM_3       ,NOCAPS, '`', '~' },
229   { VK_OEM_COMMA   ,NOCAPS, ',', '<' },
230   { VK_OEM_PERIOD  ,NOCAPS, '.', '>' },
231   { VK_OEM_2       ,NOCAPS, '/', '?' },
232   /* Keys that do not have shift states */
233   { VK_TAB     ,NOCAPS, '\t','\t'},
234   { VK_ADD     ,NOCAPS, '+', '+' },
235   { VK_SUBTRACT,NOCAPS, '-', '-' },
236   { VK_MULTIPLY,NOCAPS, '*', '*' },
237   { VK_DIVIDE  ,NOCAPS, '/', '/' },
238   { VK_ESCAPE  ,NOCAPS, '\x1b','\x1b' },
239   { VK_SPACE   ,NOCAPS, ' ', ' ' },
240   { 0, 0 }
241 };
242
243 ROSDATA VK_TO_WCHARS3 key_to_chars_3mod[] = {
244   /* Normal, Shifted, Ctrl */
245   /* Legacy (telnet-style) ascii escapes */
246   { VK_OEM_4, 0, '[', '{', 0x1b /* ESC */ },
247   { VK_OEM_6, 0, ']', '}', 0x1d /* GS */ },
248   { VK_OEM_5, 0, '\\','|', 0x1c /* FS */ },
249   { VK_RETURN,0, '\r', '\r', '\n' },
250   { 0,0 }
251 };
252
253 ROSDATA VK_TO_WCHARS4 key_to_chars_4mod[] = {
254   /* Normal, Shifted, Ctrl, C-S-x */
255   /* Legacy Ascii generators */
256   { '2', NOCAPS, '2', '@', WCH_NONE, 0 },
257   { '6', NOCAPS, '6', '^', WCH_NONE, 0x1e /* RS */ },
258   { VK_OEM_MINUS, NOCAPS, '-', '_', WCH_NONE, 0x1f /* US */ },
259   { 0, 0 }
260 };
261
262 ROSDATA VK_TO_WCHARS1 keypad_numbers[] = {
263   { VK_NUMPAD0, 0, '0' },
264   { VK_NUMPAD1, 0, '1' },
265   { VK_NUMPAD2, 0, '2' },
266   { VK_NUMPAD3, 0, '3' },
267   { VK_NUMPAD4, 0, '4' },
268   { VK_NUMPAD5, 0, '5' },
269   { VK_NUMPAD6, 0, '6' },
270   { VK_NUMPAD7, 0, '7' },
271   { VK_NUMPAD8, 0, '8' },
272   { VK_NUMPAD9, 0, '9' },
273   { VK_BACK,    0, '\010' },
274   { 0,0 }
275 };
276
277 #define vk_master(n,x) { (PVK_TO_WCHARS1)x, n, sizeof(x[0]) }
278
279 ROSDATA VK_TO_WCHAR_TABLE vk_to_wchar_master_table[] = {
280   vk_master(1,keypad_numbers),
281   vk_master(2,key_to_chars_2mod),
282   vk_master(3,key_to_chars_3mod),
283   vk_master(4,key_to_chars_4mod),
284   { 0,0,0 }
285 };
286
287 ROSDATA VSC_LPWSTR key_names[] = {
288   { 0x00, L"Error" },
289   { 0x01, L"Esc" },
290   { 0x0e, L"Backspace" },
291   { 0x0f, L"Tab" },
292   { 0x1c, L"Enter" },
293   { 0x1d, L"Ctrl" },
294   { 0x2a, L"Shift" },
295   { 0x36, L"Right Shift" },
296   { 0x37, L"Num *" },
297   { 0x38, L"Alt" },
298   { 0x39, L"Space" },
299   { 0x3a, L"Caps Lock" },
300   { 0x3b, L"F1" },
301   { 0x3c, L"F2" },
302   { 0x3d, L"F3" },
303   { 0x3e, L"F4" },
304   { 0x3f, L"F5" },
305   { 0x40, L"F6" },
306   { 0x41, L"F7" },
307   { 0x42, L"F8" },
308   { 0x43, L"F9" },
309   { 0x44, L"F10" },
310   { 0x45, L"Pause" },
311   { 0x46, L"Scroll Lock" },
312   { 0x47, L"Num 7" },
313   { 0x48, L"Num 8" },
314   { 0x49, L"Num 9" },
315   { 0x4a, L"Num -" },
316   { 0x4b, L"Num 4" },
317   { 0x4c, L"Num 5" },
318   { 0x4d, L"Num 6" },
319   { 0x4e, L"Num +" },
320   { 0x4f, L"Num 1" },
321   { 0x50, L"Num 2" },
322   { 0x51, L"Num 3" },
323   { 0x52, L"Num 0" },
324   { 0x53, L"Num Del" },
325   { 0x54, L"Sys Req" },
326   { 0x57, L"F11" },
327   { 0x58, L"F12" },
328   { 0x7c, L"F13" },
329   { 0x7d, L"F14" },
330   { 0x7e, L"F15" },
331   { 0x7f, L"F16" },
332   { 0x80, L"F17" },
333   { 0x81, L"F18" },
334   { 0x82, L"F19" },
335   { 0x83, L"F20" },
336   { 0x84, L"F21" },
337   { 0x85, L"F22" },
338   { 0x86, L"F23" },
339   { 0x87, L"F24" },
340   { 0, NULL },
341 };
342
343 ROSDATA VSC_LPWSTR extended_key_names[] = {
344   { 0x1c, L"Num Enter" },
345   { 0x1d, L"Right Ctrl" },
346   { 0x35, L"Num /" },
347   { 0x37, L"Prnt Scrn" },
348   { 0x38, L"Right Alt" },
349   { 0x45, L"Num Lock" },
350   { 0x46, L"Break" },
351   { 0x47, L"Home" },
352   { 0x48, L"Up" },
353   { 0x49, L"Page Up" },
354   { 0x4a, L"Left" },
355   { 0x4c, L"Center" },
356   { 0x4d, L"Right" },
357   { 0x4f, L"End" },
358   { 0x50, L"Down" },
359   { 0x51, L"Page Down" },
360   { 0x52, L"Insert" },
361   { 0x53, L"Delete" },
362   { 0x54, L"<ReactOS>" },
363   { 0x55, L"Help" },
364   { 0x56, L"Left Windows" },
365   { 0x5b, L"Right Windows" },
366   { 0, NULL },
367 };
368
369 /* Finally, the master table */
370 ROSDATA KBDTABLES keyboard_layout_table = {
371   /* modifier assignments */
372   (PMODIFIERS)modifier_bits,
373   
374   /* character from vk tables */
375   vk_to_wchar_master_table,
376   
377   /* diacritical marks -- currently implemented by wine code */
378   /* English doesn't have any, anyway */
379   NULL,
380
381   /* Key names */
382   (VSC_LPWSTR *)key_names,
383   (VSC_LPWSTR *)extended_key_names,
384   NULL, /* Dead key names */
385
386   /* scan code to virtual key maps */
387   scancode_to_vk,
388   sizeof(scancode_to_vk) / sizeof(scancode_to_vk[0]),
389   extcode0_to_vk,
390   extcode1_to_vk,
391
392   MAKELONG(0,1), /* Version 1.0 */
393
394   /* Ligatures -- English doesn't have any */
395   0,
396   0,
397   NULL
398 };
399
400 PKBDTABLES STDCALL KbdLayerDescriptor() {
401   return &keyboard_layout_table;
402 }