-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbd_input.cpp
More file actions
83 lines (71 loc) · 1.96 KB
/
Copy pathkbd_input.cpp
File metadata and controls
83 lines (71 loc) · 1.96 KB
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
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "kbd_input.h"
#include "vm.h"
// Pack key_code and modifiers into a 32-bit ekey
uint32_t pack_ekey(uint32_t key_code, uint32_t modifiers) {
return (modifiers & 0xFF000000) | (key_code & 0x00FFFFFF);
}
// Unpack ekey into key_code and modifiers
void unpack_ekey(uint32_t ekey, uint32_t& key_code, uint32_t& modifiers) {
modifiers = ekey & 0xFF000000;
key_code = ekey & 0x00FFFFFF;
}
// Check if key_code is printable ASCII
bool is_printable_char(uint32_t key_code) {
return key_code >= 32 && key_code <= 126;
}
// ekey_to_char: returns character code or 0
uint32_t ekey_to_char(uint32_t ekey) {
uint32_t key_code, modifiers;
unpack_ekey(ekey, key_code, modifiers);
return is_printable_char(key_code) ? key_code : 0;
}
// ekey_to_fkey: returns full function key code with modifiers, or 0
uint32_t ekey_to_fkey(uint32_t ekey) {
uint32_t key_code, modifiers;
unpack_ekey(ekey, key_code, modifiers);
return is_printable_char(key_code) ? 0 : ekey;
}
// Forth interface functions
void f_key_query() {
push(f_bool(key_available()));
}
void f_key() {
int key = get_key();
push(key);
}
void f_ekey_query() {
push(f_bool(key_available()));
}
void f_ekey() {
uint32_t ekey = get_ekey();
push(ekey);
}
void f_ekey_to_char() {
uint32_t ekey = pop();
int c = ekey_to_char(ekey);
if (c == 0) {
push(ekey);
push(F_FALSE);
}
else {
push(c);
push(F_TRUE);
}
}
void f_ekey_to_fkey() {
uint32_t ekey = pop();
int fkey = ekey_to_fkey(ekey);
if (fkey == 0) {
push(ekey);
push(F_FALSE);
}
else {
push(fkey);
push(F_TRUE);
}
}