-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkl.c
711 lines (662 loc) · 22.1 KB
/
kl.c
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include "kl.h"
#include "ka.h"
#include "km.h"
#include "lm.h"
#include "dk.h"
#include "kn.h"
#include "scancodes.h"
#include "stdafx.h"
#ifndef NOGUI
# include "ui.h"
#endif // NOGUI
/* kl.c -- the low level keyboard hook
*
* Allows for key presses and releases to be intercepted,
* and then either be blocked, passed, or altered.
* Requires Windows 95.
* The interception is toggled, and when off, the program does nothing.
*
* The alteration is performed using the lookup table,
* which is a compilation of global, language-specific,
* and application-specific tables.
*
* This file also contains all the table manipulation functions.
*
* */
bool KL_active = false;
HHOOK KL_handle;
KM KL_km_shift;
KM KL_km_control;
KM KL_km_alt;
KM KL_km_win;
KM KL_km_l3;
KM KL_km_l5;
KLY KL_kly;
/* vk/sc-to-wchar tables */
typedef WCHAR KLVKW[2][VK_COUNT];
typedef WCHAR KLSCW[2][SC_COUNT];
KLVKW KL_vkw;
KLSCW KL_scw;
UCHAR KL_phys[SC_COUNT];
UCHAR KL_phys_mods[SC_COUNT];
VK KL_mods_vks[SC_COUNT];
bool KL_dk_in_effect;
void KL_activate() {
KL_handle = SetWindowsHookEx(WH_KEYBOARD_LL, KL_proc, OS_current_module_handle(), 0);
if (KL_handle == NULL) {
puts("\n\nSetWindowsHookEx failed!");
OS_print_last_error();
puts("\n");
}
KL_active = true;
#ifndef NOGUI
UI_TR_update();
#endif // NOGUI
}
void KL_deactivate() {
UnhookWindowsHookEx(KL_handle);
KL_active = false;
#ifndef NOGUI
UI_TR_update();
#endif // NOGUI
}
void KL_toggle() {
if (KL_active) {
KL_deactivate();
} else {
KL_activate();
}
}
static bool KL_received_anything_in_this_window;
void KL_on_task_switch(void) {
/* A key might have been pressed in a non-Administrator window,
* and released in an Administrator one.
* If running without Administrator rights, this records the wrong key state.
* This, in turn, makes the code faking the ctrl/alt/shift state for vk sending do
* (depress,SendInput,press) of one or more of these modifiers instead of
* (press,SendInput,depress).
* */
printf("\n received anything in the last window: %d\n",
(int)KL_received_anything_in_this_window);
if (!KL_received_anything_in_this_window) {
ZeroBuf(KL_phys);
KM_reset(&KL_km_shift);
KM_reset(&KL_km_control);
KM_reset(&KL_km_alt);
KM_reset(&KL_km_win);
KM_reset(&KL_km_l3);
KM_reset(&KL_km_l5);
}
KL_received_anything_in_this_window = false;
}
void KL_dk_on_sc(SC sc, int level) {
printf("{dk sc%03x}", sc);
WCHAR wc = ((unsigned short)(sc) >= KPN) ? 0 : KL_scw[level % 2][sc];
printf("{dk l%d '%c'}", level % 2, (char)wc);
if (wc) { KL_dk_in_effect = DK_on_char(wc); }
else { KL_dk_in_effect = 0; }
}
void KL_dk_on_vk(VK vk, int level, BOOL need_shift) {
if (need_shift) {
level = 1;
}
printf("{dk vk%02x}", vk);
WCHAR wc = KL_vkw[level % 2][(unsigned char)(vk)];
printf("{dk l%d '%c'}", level % 2, (char)wc);
if (wc) { KL_dk_in_effect = DK_on_char(wc); }
else { KL_dk_in_effect = 0; }
}
void KL_dk_on_wchar(WCHAR wc) {
printf("{dk u%04x}", wc);
if (wc) { KL_dk_in_effect = DK_on_char(wc); }
else { KL_dk_in_effect = 0; }
}
void KL_dk_send_wchar(WCHAR wc) {
KL_dk_in_effect = 0;
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wVk = 0;
inp.ki.dwFlags = KEYEVENTF_UNICODE;
inp.ki.dwExtraInfo = 0;
inp.ki.wScan = wc;
inp.ki.time = GetTickCount();
SendInput(1, &inp, sizeof(INPUT));;
}
#define duch() (down ? '_' : '^')
#define frch() (faked ? 'F' : 'R')
#define RawThisEvent() 0
#define StopThisEvent() 1
#define PassThisEvent() CallNextHookEx(NULL, aCode, wParam, lParam)
LRESULT CALLBACK KL_proc(int aCode, WPARAM wParam, LPARAM lParam) {
if (aCode != HC_ACTION) {
printf("\n{not_hc_action %d %lx %lx}", aCode, (long)wParam, (long)lParam);
return PassThisEvent();
}
/* Gather the values of interest */
PKBDLLHOOKSTRUCT ev = (PKBDLLHOOKSTRUCT) lParam;
DWORD flags = ev->flags;
SC sc = (SC) ev->scanCode;
//VK vk = (VK) ev->vkCode;
bool down = (wParam != WM_KEYUP && wParam != WM_SYSKEYUP);
// non-physical key events:
// injected key presses (generated by programs - keybd_event(), SendInput()),
// their (non-injected) release counterparts,
// fake shift presses and releases by driver accompanying numpad keys
// (so that numlock'ed keys are independent of shift state yet have the same 2 levels),
// LControl press/release by OS (the window system) triggered by AltGr RAlt event
// Only check here for injected presses and corresponding releases.
bool faked;
faked = (flags & LLKHF_INJECTED || (!(KL_phys[sc]) && !down));
if (!faked) { printf("\n"); }
printf("{sc%03xvk%02lx%c%c}", sc, ev->vkCode, frch(), duch());
if (!faked) {
SC sc_ext = (flags & LLKHF_EXTENDED) ? (sc | SC_EXTENDED_BIT) : sc;
printf(" %s %s\n ", KN_sc_to_str(sc_ext), (down ? "down" : "up"));
(void) sc_ext;
}
/* Track the modifiers state: shift, control, alt, win, level3, level5
* (the latter two are not present in the OS)
* */
if (!faked) {
KL_phys[sc] = down;
BYTE mod = KL_phys_mods[sc];
if (mod) {
switch (mod) {
case MOD_SHIFT:
printf("{Shift%c} ", duch());
KM_shift_event(&KL_km_shift, down, sc);
break;
case MOD_CONTROL:
printf("{Ctrl%c} ", duch());
KM_shift_event(&KL_km_control, down, sc);
break;
case MOD_ALT:
printf("{Alt%c} ", duch());
KM_shift_event(&KL_km_alt, down, sc);
break;
case MOD_WIN:
printf("{Win%c} ", duch());
KM_shift_event(&KL_km_win, down, sc);
break;
case KLM_PHYS_TEMP:
KL_phys_mods[sc] = 0;
goto mods_end;
}
return PassThisEvent();
} else {
KM_nonmod_event(&KL_km_shift, down, sc);
KM_nonmod_event(&KL_km_l3, down, sc);
}
}
mods_end:
/* Set the 'extended' bit in scancode if it should be set */
if (flags & LLKHF_EXTENDED) {
sc |= SC_EXTENDED_BIT;
}
/* Do not process any simulated key events,
* or events which most likely are hardware-dependent
* (such as multimedia buttons, power buttons, etc.);
* just let them through.
* */
if (faked || sc >= KPN) {
if (!faked) {
printf("{sc%03lx,vk%02lx%c} ", ev->scanCode, ev->vkCode, duch());
}
return PassThisEvent();
}
KL_received_anything_in_this_window = true;
/* Compute the layout level currently in effect
* (using the modifier tracking data) */
int lv = 0;
if (KL_km_l3.in_effect) {
lv = 2;
} else if (KL_km_l5.in_effect) {
lv = 4;
}
if (KL_km_shift.in_effect) {
lv += 1;
}
/* Acquire the key binding (the alteration to be performed) */
LK lk = KL_kly[lv][sc];
printf(" l%d,b%x", lv, lk.binding);
/* If any of control, alt, or win is currently in effect,
* alter the key being send using the special when-a-modifier-is-in-effect
* lookup table, except when this would cancel a key action.
* */
if (lv <= 1 && (KL_km_alt.in_effect || KL_km_control.in_effect || KL_km_win.in_effect)) {
VK vk = KL_mods_vks[sc];
/* the when-modifier alteration */
if (vk && !(lk.active && (lk.mods & KLM_KA))) {
keybd_event(vk, 0, (down ? 0 : KEYEVENTF_KEYUP), 0);
printf(" SendVK%c(%02x=%c)", duch(), vk, vk);
return StopThisEvent();
}
}
/* Processing of key when in dead key sequence */
#define MaybeDeadKeyVK() \
if (KL_dk_in_effect) { \
if (down) { KL_dk_on_vk(ev->vkCode, lv, lk.mods & MOD_SHIFT); }; \
return StopThisEvent(); \
};
/* If there is no alteration ... */
if (!lk.active) {
printf(" na%c ", duch());
if (lv < 2) { /* ... just let the event fire when none or shift is in effect */
printf("lv1");
MaybeDeadKeyVK();
return PassThisEvent();
} else if (lv < 4) { /* ... when level3 is in effect ... */
printf("lv3");
MaybeDeadKeyVK();
return StopThisEvent();
/* ... block the event if level5 is in effect */
} else {
printf("lv5");
return StopThisEvent();
}
}
#undef MaybeDeadKeyVK
/* If there is an alteration,
* determine its type:
* scancode (KLM_SC),
* UCS-2 character (KLM_WCHAR),
* key action from ka.c (KLM_KA),
* virtual keycode (none of the above)
* */
UCHAR mods = lk.mods;
if (mods == KLM_SC) { /* ... if scancode, just alter the scancode */
printf("%csc%03lx=%02x ", duch(), ev->scanCode, lk.binding);
/* processing of scan code when in dead key sequence */
if (KL_dk_in_effect) {
if (down) { KL_dk_on_sc(lk.binding, lv); };
} else {
keybd_event(0, lk.binding, KEYEVENTF_SCANCODE | (down ? 0 : KEYEVENTF_KEYUP), 0);
}
return StopThisEvent();
} else if (mods == KLM_WCHAR) { /* ... if a character, SendInput it */
if (down) {
WCHAR wc = lk.binding;
printf(" send U+%04x ", wc);
/* processing of character when in dead key sequence */
if (KL_dk_in_effect) {
KL_dk_on_wchar(wc);
} else {
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wVk = 0;
inp.ki.dwFlags = KEYEVENTF_UNICODE;
inp.ki.dwExtraInfo = 0;
inp.ki.wScan = wc;
inp.ki.time = GetTickCount();
SendInput(1, &inp, sizeof(INPUT));
}
}
} else if (mods & KLM_KA) { /* ... if key action, call it */
printf(" ka_call ka%d(%d){", lk.binding, down);
if (lk.binding < KA_dkn_count) {
KL_dk_in_effect = true;
}
KA_call(lk.binding, down, sc);
printf("}%c", duch());
}
/* ... if virtual key code, temporarily bring shift, control and alt
* into the state required by alteration, and send the virtual key code.
* */
/* processing of virtual keycode when in dead key sequence */
else if (KL_dk_in_effect) {
if (down) { KL_dk_on_vk(lk.binding, lv, lk.mods & MOD_SHIFT); };
}
else {
bool shift_was_down = KL_km_shift.in_effect;
bool need_shift = (mods & MOD_SHIFT);
char mod_shift = (KL_km_shift.in_effect ? (need_shift ? 0 : -1) : (need_shift ? 1 : 0)), mod_shift0 = mod_shift;
char mod_control = (((mods & MOD_CONTROL) && !KL_km_control.in_effect) ? 1 : 0), mod_control0 = mod_control;
char mod_alt = (((mods & MOD_ALT) && !KL_km_alt.in_effect) ? 1 : 0), mod_alt0 = mod_alt;
int mods_count = (mod_shift & 1) + mod_control + mod_alt;
if (!mods_count) {
printf(" evt vk%02x%c ", lk.binding, duch());
keybd_event(lk.binding, sc, (down ? 0 : KEYEVENTF_KEYUP), 0);
} else {
int inps_count = 1 + mods_count * 2;
INPUT inps[7];
int i, tick_count = GetTickCount();
printf(" send%d vk%02x[%d%d%d]%c", inps_count, lk.binding, mod_shift, mod_control, mod_alt, duch());
fori (i, 0, inps_count) {
VK vk1 = lk.binding;
DWORD flags = 0;
if (mod_shift) {
if (mod_shift > 0 && !need_shift && !shift_was_down) {
inps_count--;
continue;
}
printf("+%d-", mod_shift);
flags = (mod_shift > 0 ? 0 : KEYEVENTF_KEYUP);
mod_shift = 0;
vk1 = VK_LSHIFT;
} else if (mod_control) {
printf("^");
flags = (mod_control > 0 ? 0 : KEYEVENTF_KEYUP);
mod_control = 0;
vk1 = VK_LCONTROL;
} else if (mod_alt) {
printf("!");
flags = (mod_alt > 0 ? 0 : KEYEVENTF_KEYUP);
mod_alt = 0;
vk1 = VK_RMENU;
} else {
printf("-");
mod_shift = -mod_shift0;
mod_control = -mod_control0;
mod_alt = -mod_alt0;
vk1 = lk.binding;
flags = (down ? 0 : KEYEVENTF_KEYUP);
}
INPUT *inp = &(inps[i]);
inp->type = INPUT_KEYBOARD;
inp->ki.wVk = vk1;
inp->ki.dwFlags = flags;
inp->ki.dwExtraInfo = 0;
inp->ki.wScan = sc;
inp->ki.time = tick_count;
}
SendInput(inps_count, inps, sizeof(INPUT));
}
}
return StopThisEvent();
}
#undef RawThisEvent
#undef StopThisEvent
#undef PassThisEvent
KLY *KL_bind_kly = &KL_kly;
void KL_bind(SC sc, UINT lvl, UINT mods, SC binding) {
LK lk;
SC binding1 = binding;
UINT lvl1 = lvl+1;
if (mods & KLM_SC) {
binding1 = OS_sc_to_vk(binding);
}
printf("bind sc%03x[%d]:", sc, lvl1);
if (mods & KLM_WCHAR) {
printf("u%04x ", binding);
} else if (mods & KLM_KA) {
printf("ka%d ", binding);
} else {
if (mods & KLM_SC) {
printf("sc%03x=>vk%02x ", binding, binding1);
} else {
printf("vk%02x ", binding);
}
}
if (!(lvl1 % 2) && !(mods & KLM_WCHAR) && !(mods & KLM_KA)) {
printf("+(%x)", mods);
mods |= MOD_SHIFT;
}
lk.active = true;
lk.mods = mods;
lk.binding = binding1;
(*KL_bind_kly)[lvl][sc] = lk;
}
void KL_temp_sc(SC sc, SC mods, SC binding) {
if (mods == KLM_KA) {
printf("t sc%03x=ka%d ", sc, binding);
} else if (mods == KLM_SC) {
printf("t sc%03x=%02x ", sc, binding);
} else {
printf("t sc%03x={%02x/%02x} ", sc, binding, mods);
}
LK lk = { true, (UCHAR)mods, binding };
KL_kly[0][sc] = lk;
KL_kly[1][sc] = lk;
}
typedef struct {
bool compiled;
// Primary Language ID
LANGID lang;
// Whether defines VKs for while modifiers are in effect
bool vks_lang;
// Bindings
KLY kly;
// vk/sc-to-wchar tables
KLVKW vkw;
KLSCW scw;
} KLC;
size_t KL_klcs_size = 0;
size_t KL_klcs_count = 0;
KLC *KL_klcs;
void KL_add_lang(LANGID lang) {
if ((KL_klcs_count+=1) > KL_klcs_size) {
KL_klcs = (KLC*) realloc(KL_klcs, (KL_klcs_size *= 1.5) * sizeof(KLC));
}
KLC *klc = KL_klcs + KL_klcs_count - 1;
klc->compiled = false;
klc->lang = lang;
klc->vks_lang = false;
}
KLY *KL_lang_to_kly(LANGID lang) {
UINT i;
fori(i, 0, KL_klcs_count) {
if (KL_klcs[i].lang == lang)
return &(KL_klcs[i].kly);
}
return NULL;
}
KLC *KL_lang_to_klc(LANGID lang) {
UINT i;
printf("klcs_count:%d ", (int)KL_klcs_count);
fori(i, 0, KL_klcs_count) {
printf("klc.lang:%x ", KL_klcs[i].lang);
if (KL_klcs[i].lang == lang)
return &(KL_klcs[i]);
}
return NULL;
}
LANGID KL_vks_lang = LANG_NEUTRAL;
BYTE KL_vksc_kbdstate[256];
void KL_compile_klc(KLC *klc) {
if (klc->lang == LANG_NEUTRAL)
return;
HKL cur_hkl = GetKeyboardLayout(0);
ActivateKeyboardLayout(LM_langid_to_hkl(klc->lang), 0);
KLY *kly = &(klc->kly);
CopyMemory(KL_kly, KL_lang_to_kly(LANG_NEUTRAL), sizeof(KLY));
bool vks_lang = klc->vks_lang;
int lv, sc;
fori(lv, 0, KLVN) {
fori(sc, 0, KPN) {
LK lk = (*kly)[lv][sc];
if (lk.active) {
printf("sc%03x:%d ", sc, lv+1);
KL_kly[lv][sc] = lk;
}
}
}
fori (lv, 0, KLVN) {
fori (sc, 0, KPN) {
LK *p_lk = &(KL_kly[lv][sc]), lk = *p_lk;
if (lk.active) {
//printf("a(%03x:%d:%x/%x)", sc, lv+1, lk.binding, lk.mods);
if (lk.mods & KLM_WCHAR) {
WCHAR w = lk.binding;
printf("sc%03x'%c':%d->", sc, w, lv+1);
KP kp = OS_wchar_to_vk(w);
if (kp.vk != 0xFF) {
printf("vk%02x/%d", kp.vk, kp.mods);
bool same_sc = (kp.mods == KLM_SC && kp.sc == sc);
bool same_vk = (lv <= 1 && kp.mods == (MOD_SHIFT * (lv % 2)) && kp.vk == OS_sc_to_vk(sc));
if (same_sc || same_vk) {
lk.active = 0;
lk.binding = 0;
lk.mods = 0;
} else {
if (lv == 0 && vks_lang && !(kp.mods & KLM_SC)) {
printf("_M");
KL_mods_vks[sc] = kp.vk;
}
lk.mods = kp.mods;
lk.binding = kp.vk;
}
} else {
printf("u%04x", w);
}
printf(";");
}
*p_lk = lk;
}
}
}
void *to_wc_cache = OS_ToUnicodeThroghVkKeyScan_new_cache();
fori (lv, 0, 2) {
BOOL shift_down = lv % 2;
/* Set the shift state according to level */
if (shift_down) {
KL_vksc_kbdstate[VK_SHIFT] = 1;
KL_vksc_kbdstate[VK_LSHIFT] = 1;
KL_vksc_kbdstate[VK_RSHIFT] = 1;
} else {
KL_vksc_kbdstate[VK_SHIFT] = 0;
KL_vksc_kbdstate[VK_LSHIFT] = 0;
KL_vksc_kbdstate[VK_RSHIFT] = 0;
}
int vk;
printf("\n");
fori (vk, 0, VK_COUNT) {
WCHAR wc = 0;
/* Translate virtual keycode and shift state into UCS-2 character.
* Do not use ToUnicode(), as it ignores shift state.
* */
// WCHAR buf[4] = { 0, 0, 0, 0 };
// int n = ToUnicode(vk, 0, KL_vksc_kbdstate, buf, lenof(buf), 0);
// if (n == 1) { wc = buf[0]; };
wc = OS_ToUnicodeThroghVkKeyScan(to_wc_cache, vk, shift_down);
printf("[vk%02x%cu%02x]", vk, lv ? '+' : ' ', wc);
KL_vkw[lv][vk] = wc;
}
printf("\n");
fori (sc, 0, KPN) {
WCHAR wc = 0;
/* Translate scancode and shift state into UCS-2 character. */
VK vk = OS_sc_to_vk(sc);
// WCHAR buf[4] = { 0, 0, 0, 0 };
// int n = ToUnicode(vk, 0, KL_vksc_kbdstate, buf, lenof(buf), 0);
// if (n == 1) { wc = buf[0]; }
wc = OS_ToUnicodeThroghVkKeyScan(to_wc_cache, vk, shift_down);
printf("[sc%03x=>vk%02x%cu%02x]", sc, vk, lv ? '+' : ' ', wc);
KL_scw[lv][sc] = wc;
}
}
free(to_wc_cache);
CopyMemory(kly, KL_kly, sizeof(KLY));
CopyMemory(&(klc->vkw), KL_vkw, sizeof(KLVKW));
CopyMemory(&(klc->scw), KL_scw, sizeof(KLSCW));
klc->compiled = true;
ActivateKeyboardLayout(cur_hkl, 0);
}
void KL_activate_lang(LANGID lang) {
printf("lang %04x ", lang);
KLC *lang_klc = KL_lang_to_klc(lang);
if (lang_klc == NULL) {
printf("no such lang! ");
return;
}
if (lang_klc->compiled) {
CopyMemory(KL_kly, lang_klc->kly, sizeof(KLY));
CopyMemory(KL_vkw, lang_klc->vkw, sizeof(KLVKW));
CopyMemory(KL_scw, lang_klc->scw, sizeof(KLSCW));
} else {
printf("compile ");
KL_compile_klc(lang_klc);
}
puts("");
}
void KL_set_bind_lang(LANGID lang) {
KLY *kly = KL_lang_to_kly(lang);
if (kly == NULL) {
KL_add_lang(lang);
kly = KL_lang_to_kly(lang);
}
KL_bind_kly = kly;
}
void KL_set_vks_lang(LANGID lang) {
KL_vks_lang = lang;
}
void KL_define_one_vk(VK vk, KLY *kly) {
SC sc = OS_vk_to_sc(vk);
if (!sc) {
return;
}
LK lk = (*kly)[0][sc];
if (lk.active && lk.mods == 0) {
vk = lk.binding;
}
printf(" DefineVK(%02x, sc%03x)", vk, sc);
KL_mods_vks[sc] = vk;
}
void KL_define_vks() {
KLC *klc = KL_lang_to_klc(KL_vks_lang);
KLY *kly = &(klc->kly);
klc->vks_lang = true;
if (!klc->compiled) {
KL_compile_klc(klc);
}
int vk;
// 0..9
fori (vk, 0x30, 0x3A) {
KL_define_one_vk(vk, kly);
}
// A..Z
fori (vk, 0x41, 0x5B) {
KL_define_one_vk(vk, kly);
}
// Tilde, comma, period, brackets, semicolon, quote, slash
KL_define_one_vk(VK_OEM_1, kly);
KL_define_one_vk(VK_OEM_2, kly);
KL_define_one_vk(VK_OEM_3, kly);
KL_define_one_vk(VK_OEM_4, kly);
KL_define_one_vk(VK_OEM_5, kly);
KL_define_one_vk(VK_OEM_6, kly);
KL_define_one_vk(VK_OEM_7, kly);
KL_define_one_vk(VK_OEM_8, kly);
}
void KL_bind_init() {
KL_bind_kly = KL_lang_to_kly(LANG_NEUTRAL);
}
void KL_init() {
ZeroBuf(KL_kly);
ZeroBuf(KL_phys);
KL_klcs = (KLC*)calloc((KL_klcs_size = 4), sizeof(KLC));
KL_received_anything_in_this_window = false;
KM_init(&KL_km_shift);
KM_init(&KL_km_control);
KM_init(&KL_km_alt);
KM_init(&KL_km_l3);
KM_init(&KL_km_l5);
KL_add_lang(LANG_NEUTRAL);
KL_bind_kly = KL_lang_to_kly(LANG_NEUTRAL);
UINT sc;
fori (sc, 0, lenof(KL_phys_mods)) {
VK vk = OS_sc_to_vk(sc);
UCHAR mod = 0;
switch (vk) {
case VK_SHIFT: case VK_LSHIFT: case VK_RSHIFT:
mod = MOD_SHIFT;
break;
case VK_CONTROL: case VK_LCONTROL: case VK_RCONTROL:
mod = MOD_CONTROL;
break;
case VK_MENU: case VK_LMENU: case VK_RMENU:
mod = MOD_ALT;
break;
case VK_LWIN: case VK_RWIN:
mod = MOD_WIN;
break;
}
if (mod) {
printf("[mods] sc%03x => vk%02x, mod %x\t", sc, vk, mod);
}
KL_phys_mods[sc] = mod;
}
#define mod_vk(mod, vk) do { sc = OS_vk_to_sc(vk); if (sc) { printf("[mods] sc%03x => vk%02x, mod %x\t", sc, vk, mod); KL_phys_mods[sc] = mod; }; } while (0)
mod_vk(MOD_WIN, VK_LWIN);
mod_vk(MOD_WIN, VK_RWIN);
#undef mod_vk
}