summaryrefslogtreecommitdiff
path: root/jni/mkxp.win32-binding.cpp
blob: 10da51dca04abfc7bf567e371ece0f0053432ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <SDL.h>

typedef int32_t LPARAM;
typedef uint32_t WPARAM;
#include "jni/SDL/src/video/windows/SDL_vkeys.h"
#include "jni/SDL/src/events/scancodes_windows.h"

#include "binding-util.h"

// generated with:
// curl -s 'https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes?redirectedfrom=MSDN' | \
//          grep -Eo '<strong>VK_.*</strong>.*<dt>0x[A-Z0-9]+</dt>' | perl -pe 's/<.*?>//g' | sed 's/^/#define /'
#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02
#define VK_CANCEL 0x03
#define VK_MBUTTON 0x04
#define VK_XBUTTON1 0x05
#define VK_XBUTTON2 0x06
#define VK_BACK 0x08
#define VK_TAB 0x09
#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D
#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14
#define VK_KANA 0x15
#define VK_HANGUEL 0x15
#define VK_HANGUL 0x15
#define VK_IME_ON 0x16
#define VK_JUNJA 0x17
#define VK_FINAL 0x18
#define VK_HANJA 0x19
#define VK_KANJI 0x19
#define VK_IME_OFF 0x1A
#define VK_ESCAPE 0x1B
#define VK_CONVERT 0x1C
#define VK_NONCONVERT 0x1D
#define VK_ACCEPT 0x1E
#define VK_MODECHANGE 0x1F
#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F
#define VK_LWIN 0x5B
#define VK_RWIN 0x5C
#define VK_APPS 0x5D
#define VK_SLEEP 0x5F
#define VK_NUMPAD0 0x60
#define VK_NUMPAD1 0x61
#define VK_NUMPAD2 0x62
#define VK_NUMPAD3 0x63
#define VK_NUMPAD4 0x64
#define VK_NUMPAD5 0x65
#define VK_NUMPAD6 0x66
#define VK_NUMPAD7 0x67
#define VK_NUMPAD8 0x68
#define VK_NUMPAD9 0x69
#define VK_MULTIPLY 0x6A
#define VK_ADD 0x6B
#define VK_SEPARATOR 0x6C
#define VK_SUBTRACT 0x6D
#define VK_DECIMAL 0x6E
#define VK_DIVIDE 0x6F
#define VK_F1 0x70
#define VK_F2 0x71
#define VK_F3 0x72
#define VK_F4 0x73
#define VK_F5 0x74
#define VK_F6 0x75
#define VK_F7 0x76
#define VK_F8 0x77
#define VK_F9 0x78
#define VK_F10 0x79
#define VK_F11 0x7A
#define VK_F12 0x7B
#define VK_F13 0x7C
#define VK_F14 0x7D
#define VK_F15 0x7E
#define VK_F16 0x7F
#define VK_F17 0x80
#define VK_F18 0x81
#define VK_F19 0x82
#define VK_F20 0x83
#define VK_F21 0x84
#define VK_F22 0x85
#define VK_F23 0x86
#define VK_F24 0x87
#define VK_NUMLOCK 0x90
#define VK_SCROLL 0x91
#define VK_LSHIFT 0xA0
#define VK_RSHIFT 0xA1
#define VK_LCONTROL 0xA2
#define VK_RCONTROL 0xA3
#define VK_LMENU 0xA4
#define VK_RMENU 0xA5
#define VK_BROWSER_BACK 0xA6
#define VK_BROWSER_FORWARD 0xA7
#define VK_BROWSER_REFRESH 0xA8
#define VK_BROWSER_STOP 0xA9
#define VK_BROWSER_SEARCH 0xAA
#define VK_BROWSER_FAVORITES 0xAB
#define VK_BROWSER_HOME 0xAC
#define VK_VOLUME_MUTE 0xAD
#define VK_VOLUME_DOWN 0xAE
#define VK_VOLUME_UP 0xAF
#define VK_MEDIA_NEXT_TRACK 0xB0
#define VK_MEDIA_PREV_TRACK 0xB1
#define VK_MEDIA_STOP 0xB2
#define VK_MEDIA_PLAY_PAUSE 0xB3
#define VK_LAUNCH_MAIL 0xB4
#define VK_LAUNCH_MEDIA_SELECT 0xB5
#define VK_LAUNCH_APP1 0xB6
#define VK_LAUNCH_APP2 0xB7
#define VK_OEM_1 0xBA
#define VK_OEM_PLUS 0xBB
#define VK_OEM_COMMA 0xBC
#define VK_OEM_MINUS 0xBD
#define VK_OEM_PERIOD 0xBE
#define VK_OEM_2 0xBF
#define VK_OEM_3 0xC0
#define VK_OEM_4 0xDB
#define VK_OEM_5 0xDC
#define VK_OEM_6 0xDD
#define VK_OEM_7 0xDE
#define VK_OEM_8 0xDF
#define VK_OEM_102 0xE2
#define VK_PROCESSKEY 0xE5
#define VK_PACKET 0xE7
#define VK_ATTN 0xF6
#define VK_CRSEL 0xF7
#define VK_EXSEL 0xF8
#define VK_EREOF 0xF9
#define VK_PLAY 0xFA
#define VK_ZOOM 0xFB
#define VK_NONAME 0xFC
#define VK_PA1 0xFD
#define VK_OEM_CLEAR 0xFE

// not on the website above
#define VK_OEM_NEC_EQUAL 0x92

static WPARAM
sdl_scancode_to_vkey(SDL_Scancode scancode)
{
    switch (scancode) {
       case SDL_SCANCODE_LEFT: return VK_LEFT;
       case SDL_SCANCODE_UP: return VK_UP;
       case SDL_SCANCODE_RIGHT: return VK_RIGHT;
       case SDL_SCANCODE_DOWN: return VK_DOWN;
       case SDL_SCANCODE_MODE: return VK_MODECHANGE;
       case SDL_SCANCODE_SELECT: return VK_SELECT;
       case SDL_SCANCODE_EXECUTE: return VK_EXECUTE;
       case SDL_SCANCODE_HELP: return VK_HELP;
       case SDL_SCANCODE_PAUSE: return VK_PAUSE;
       case SDL_SCANCODE_NUMLOCKCLEAR: return VK_NUMLOCK;
       case SDL_SCANCODE_F13: return VK_F13;
       case SDL_SCANCODE_F14: return VK_F14;
       case SDL_SCANCODE_F15: return VK_F15;
       case SDL_SCANCODE_F16: return VK_F16;
       case SDL_SCANCODE_F17: return VK_F17;
       case SDL_SCANCODE_F18: return VK_F18;
       case SDL_SCANCODE_F19: return VK_F19;
       case SDL_SCANCODE_F20: return VK_F20;
       case SDL_SCANCODE_F21: return VK_F21;
       case SDL_SCANCODE_F22: return VK_F22;
       case SDL_SCANCODE_F23: return VK_F23;
       case SDL_SCANCODE_F24: return VK_F24;
       case SDL_SCANCODE_KP_EQUALS: return VK_OEM_NEC_EQUAL;
       case SDL_SCANCODE_AC_BACK: return VK_BROWSER_BACK;
       case SDL_SCANCODE_AC_FORWARD: return VK_BROWSER_FORWARD;
       case SDL_SCANCODE_AC_REFRESH: return VK_BROWSER_REFRESH;
       case SDL_SCANCODE_AC_STOP: return VK_BROWSER_STOP;
       case SDL_SCANCODE_AC_SEARCH: return VK_BROWSER_SEARCH;
       case SDL_SCANCODE_AC_BOOKMARKS: return VK_BROWSER_FAVORITES;
       case SDL_SCANCODE_AC_HOME: return VK_BROWSER_HOME;
       case SDL_SCANCODE_AUDIOMUTE: return VK_VOLUME_MUTE;
       case SDL_SCANCODE_VOLUMEDOWN: return VK_VOLUME_DOWN;
       case SDL_SCANCODE_VOLUMEUP: return VK_VOLUME_UP;
       case SDL_SCANCODE_AUDIONEXT: return VK_MEDIA_NEXT_TRACK;
       case SDL_SCANCODE_AUDIOPREV: return VK_MEDIA_PREV_TRACK;
       case SDL_SCANCODE_AUDIOSTOP: return VK_MEDIA_STOP;
       case SDL_SCANCODE_AUDIOPLAY: return VK_MEDIA_PLAY_PAUSE;
       case SDL_SCANCODE_MAIL: return VK_LAUNCH_MAIL;
       case SDL_SCANCODE_MEDIASELECT: return VK_LAUNCH_MEDIA_SELECT;
       case SDL_SCANCODE_NONUSBACKSLASH: return VK_OEM_102;
       case SDL_SCANCODE_SYSREQ: return VK_ATTN;
       case SDL_SCANCODE_CRSEL: return VK_CRSEL;
       case SDL_SCANCODE_EXSEL: return VK_EXSEL;
       case SDL_SCANCODE_CLEAR: return VK_OEM_CLEAR;
       case SDL_SCANCODE_APP1: return VK_LAUNCH_APP1;
       case SDL_SCANCODE_APP2: return VK_LAUNCH_APP2;
       defaultbreak;
    }

    for (size_t i = 0; i < sizeof(windows_scancode_table) / sizeof(windows_scancode_table); ++i)
       if (windows_scancode_table[i] == scancode) return i;
    return 0;
}

RB_METHOD(user32_GetKeyboardState) {
   char *bytes;
   rb_get_args(argc, argv, "z", &bytes RB_ARG_END);

   int numKeys;
   const Uint8 *state = SDL_GetKeyboardState(&numKeys);
   for (int i = 0; i < numKeys; ++i) {
      WPARAM vkey = sdl_scancode_to_vkey(static_cast<SDL_Scancode>(i));
      assert(vkey < 256);
      bytes[vkey] = (state[i] ? -1 : 0);
   }

   return rb_fix_new(!!numKeys);
}

void
win32BindingInit(void)
{
   VALUE module = rb_define_module("Win32NativeAPI");
   _rb_define_module_function(module"user32_GetKeyboardState", user32_GetKeyboardState);
}