FreeRDP
Loading...
Searching...
No Matches
TestScancode.c
1#include <winpr/input.h>
2
3static bool testKeyboardType(enum WINPR_KBD_TYPE type, DWORD flag)
4{
5 for (DWORD x = 0; x < 0x100; x++)
6 {
7 const DWORD vk = GetVirtualKeyCodeFromVirtualScanCode(x | flag, type);
8
9 /* > 127 is out of range */
10 if (x > 127)
11 {
12 if (vk != VK_NONE)
13 return false;
14 }
15
16 const DWORD sc = GetVirtualScanCodeFromVirtualKeyCode(vk | flag, type);
17 if (sc != x)
18 {
19 if ((sc != 0) && (vk != VK_NONE))
20 {
21 DWORD buffer[256] = WINPR_C_ARRAY_INIT;
22 const DWORD count = GetVirtualScanCodesFromVirtualKeyCode(vk | flag, type, buffer,
23 ARRAYSIZE(buffer));
24
25 bool isOK = false;
26 for (DWORD i = 0; i < count; i++)
27 {
28 const DWORD cur = buffer[i];
29 if (cur == sc)
30 {
31 isOK = true;
32 break;
33 }
34 }
35 if (!isOK)
36 {
37 printf("[%" PRIu32 "]: got %" PRIu32 ", expected %" PRIu32, type, sc, x);
38 return false;
39 }
40 }
41 else if ((sc == 0) && (vk == VK_NONE))
42 {
43 }
44 else
45 {
46 printf("[%" PRIu32 "]: got %" PRIu32 ", expected %" PRIu32, type, sc, x);
47 return false;
48 }
49 }
50 }
51 return true;
52}
53
54static bool testKeyboardTypes(enum WINPR_KBD_TYPE type)
55{
56 if (!testKeyboardType(type, 0))
57 return false;
58 if (!testKeyboardType(type, KBDMULTIVK))
59 return false;
60 return testKeyboardType(type, KBDEXT);
61}
62
63static bool testVKToKeycode(WINPR_KEYCODE_TYPE type)
64{
65
66 for (DWORD x = 0; x <= 0x100; x++)
67 {
68 const DWORD vkc = GetVirtualKeyCodeFromKeycode(x, type);
69 const DWORD kc = GetKeycodeFromVirtualKeyCode(vkc, type);
70 if (x != kc)
71 {
72 if (!((kc == 0) && (vkc == VK_NONE)))
73 {
74 DWORD keycodes[0x100] = WINPR_C_ARRAY_INIT;
75 const DWORD count =
76 GetKeycodesFromVirtualKeyCode(vkc, type, keycodes, ARRAYSIZE(keycodes));
77 bool rc = false;
78 for (DWORD y = 0; y < count; y++)
79 {
80 if (keycodes[y] == x)
81 rc = true;
82 }
83 if (!rc)
84 return false;
85 }
86 }
87 }
88 return true;
89}
90
91static bool testVKToKeycodes(void)
92{
93 const WINPR_KEYCODE_TYPE types[] = { WINPR_KEYCODE_TYPE_NONE, WINPR_KEYCODE_TYPE_APPLE,
94 WINPR_KEYCODE_TYPE_EVDEV, WINPR_KEYCODE_TYPE_XKB };
95
96 for (size_t x = 0; x < ARRAYSIZE(types); x++)
97 {
98 const WINPR_KEYCODE_TYPE type = types[x];
99 if (!testVKToKeycode(type))
100 return false;
101 }
102 return true;
103}
104
105int TestScancode(WINPR_ATTR_UNUSED int argc, WINPR_ATTR_UNUSED char* argv[])
106{
107 if (!testVKToKeycodes())
108 return -23;
109
110 const enum WINPR_KBD_TYPE types[] = { WINPR_KBD_TYPE_IBM_PC_XT, WINPR_KBD_TYPE_OLIVETTI_ICO,
111 WINPR_KBD_TYPE_IBM_PC_AT, WINPR_KBD_TYPE_IBM_ENHANCED,
112 WINPR_KBD_TYPE_NOKIA_1050, WINPR_KBD_TYPE_NOKIA_9140,
113 WINPR_KBD_TYPE_JAPANESE, WINPR_KBD_TYPE_KOREAN };
114
115 int rc = 0;
116 for (size_t x = 0; x < ARRAYSIZE(types); x++)
117 {
118 const enum WINPR_KBD_TYPE type = types[x];
119 if (!testKeyboardTypes(type))
120 rc--;
121 }
122 return rc;
123}