PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
kbd.c
Go to the documentation of this file.
1#include "kbd.h"
2
3#include <stdbool.h>
4
5/// @todo Implement keymap files.
6
7typedef struct
8{
9 char norm;
10 char shift;
12
13typedef struct
14{
16} keymap_t;
17
18static keymap_t keymap = {
19 .map =
20 {
21 [KBD_A] = {.norm = 'a', .shift = 'A'},
22 [KBD_B] = {.norm = 'b', .shift = 'B'},
23 [KBD_C] = {.norm = 'c', .shift = 'C'},
24 [KBD_D] = {.norm = 'd', .shift = 'D'},
25 [KBD_E] = {.norm = 'e', .shift = 'E'},
26 [KBD_F] = {.norm = 'f', .shift = 'F'},
27 [KBD_G] = {.norm = 'g', .shift = 'G'},
28 [KBD_H] = {.norm = 'h', .shift = 'H'},
29 [KBD_I] = {.norm = 'i', .shift = 'I'},
30 [KBD_J] = {.norm = 'j', .shift = 'J'},
31 [KBD_K] = {.norm = 'k', .shift = 'K'},
32 [KBD_L] = {.norm = 'l', .shift = 'L'},
33 [KBD_M] = {.norm = 'm', .shift = 'M'},
34 [KBD_N] = {.norm = 'n', .shift = 'N'},
35 [KBD_O] = {.norm = 'o', .shift = 'O'},
36 [KBD_P] = {.norm = 'p', .shift = 'P'},
37 [KBD_Q] = {.norm = 'q', .shift = 'Q'},
38 [KBD_R] = {.norm = 'r', .shift = 'R'},
39 [KBD_S] = {.norm = 's', .shift = 'S'},
40 [KBD_T] = {.norm = 't', .shift = 'T'},
41 [KBD_U] = {.norm = 'u', .shift = 'U'},
42 [KBD_V] = {.norm = 'v', .shift = 'V'},
43 [KBD_W] = {.norm = 'w', .shift = 'W'},
44 [KBD_X] = {.norm = 'x', .shift = 'X'},
45 [KBD_Y] = {.norm = 'y', .shift = 'Y'},
46 [KBD_Z] = {.norm = 'z', .shift = 'Z'},
47
48 [KBD_1] = {.norm = '1', .shift = '!'},
49 [KBD_2] = {.norm = '2', .shift = '@'},
50 [KBD_3] = {.norm = '3', .shift = '#'},
51 [KBD_4] = {.norm = '4', .shift = '$'},
52 [KBD_5] = {.norm = '5', .shift = '%'},
53 [KBD_6] = {.norm = '6', .shift = '^'},
54 [KBD_7] = {.norm = '7', .shift = '&'},
55 [KBD_8] = {.norm = '8', .shift = '*'},
56 [KBD_9] = {.norm = '9', .shift = '('},
57 [KBD_0] = {.norm = '0', .shift = ')'},
58
59 [KBD_ENTER] = {.norm = '\n', .shift = '\n'},
60 [KBD_ESC] = {.norm = 0x1B, .shift = 0x1B},
61 [KBD_BACKSPACE] = {.norm = 0x08, .shift = 0x08},
62 [KBD_TAB] = {.norm = '\t', .shift = '\t'},
63 [KBD_SPACE] = {.norm = ' ', .shift = ' '},
64 [KBD_MINUS] = {.norm = '-', .shift = '_'},
65 [KBD_EQUAL] = {.norm = '=', .shift = '+'},
66 [KBD_LEFT_BRACE] = {.norm = '[', .shift = '{'},
67 [KBD_RIGHT_BRACE] = {.norm = ']', .shift = '}'},
68 [KBD_BACKSLASH] = {.norm = '\\', .shift = '|'},
69 [KBD_HASHTILDE] = {.norm = '#', .shift = '~'},
70 [KBD_SEMICOLON] = {.norm = ';', .shift = ':'},
71 [KBD_APOSTROPHE] = {.norm = '\'', .shift = '"'},
72 [KBD_GRAVE] = {.norm = '`', .shift = '~'},
73 [KBD_COMMA] = {.norm = ',', .shift = '<'},
74 [KBD_PERIOD] = {.norm = '.', .shift = '>'},
75 [KBD_SLASH] = {.norm = '/', .shift = '?'},
76
77 [KBD_KP_0] = {.norm = '0', .shift = KBD_NONE},
78 [KBD_KP_1] = {.norm = '1', .shift = KBD_NONE},
79 [KBD_KP_2] = {.norm = '2', .shift = KBD_NONE},
80 [KBD_KP_3] = {.norm = '3', .shift = KBD_NONE},
81 [KBD_KP_4] = {.norm = '4', .shift = KBD_NONE},
82 [KBD_KP_5] = {.norm = '5', .shift = KBD_NONE},
83 [KBD_KP_6] = {.norm = '6', .shift = KBD_NONE},
84 [KBD_KP_7] = {.norm = '7', .shift = KBD_NONE},
85 [KBD_KP_8] = {.norm = '8', .shift = KBD_NONE},
86 [KBD_KP_9] = {.norm = '9', .shift = KBD_NONE},
87 [KBD_KP_PERIOD] = {.norm = '.', .shift = KBD_NONE},
88 [KBD_KP_SLASH] = {.norm = '/', .shift = KBD_NONE},
89 [KBD_KP_ASTERISK] = {.norm = '*', .shift = KBD_NONE},
90 [KBD_KP_MINUS] = {.norm = '-', .shift = KBD_NONE},
91 [KBD_KP_PLUS] = {.norm = '+', .shift = KBD_NONE},
92 [KBD_KP_ENTER] = {.norm = '\n', .shift = '\n'},
93 [KBD_KP_EQUAL] = {.norm = '=', .shift = '='},
94 },
95};
96
98{
99 return code;
100}
101
103{
105 {
106 return '\0';
107 }
108
109 bool shift = mods & KBD_MOD_SHIFT;
110 if (mods & KBD_MOD_CAPS)
111 {
112 if (code >= KBD_A && code <= KBD_Z)
113 {
114 shift = !shift;
115 }
116 }
117
118 return shift ? keymap.map[code].shift : keymap.map[code].norm;
119}
int64_t y
Definition main.c:153
char kbd_ascii(keycode_t code, kbd_mods_t mods)
Definition kbd.c:102
static keymap_t keymap
Definition kbd.c:18
keycode_t kbd_translate(keycode_t code)
Definition kbd.c:97
kbd_mods_t
Keyboard modifiers type.
Definition event.h:146
@ KBD_MOD_CAPS
Caps Lock modifier.
Definition event.h:148
@ KBD_MOD_SHIFT
Shift modifier.
Definition event.h:149
keycode_t
Keyboard keycode type.
Definition kbd.h:29
@ KBD_KP_4
Keypad 4.
Definition kbd.h:128
@ KBD_2
Key 2.
Definition kbd.h:62
@ KBD_6
Key 6.
Definition kbd.h:66
@ KBD_1
Key 1.
Definition kbd.h:61
@ KBD_4
Key 4.
Definition kbd.h:64
@ KBD_K
Key K.
Definition kbd.h:44
@ KBD_SPACE
Space key.
Definition kbd.h:76
@ KBD_T
Key T.
Definition kbd.h:53
@ KBD_R
Key R.
Definition kbd.h:51
@ KBD_GRAVE
Grave accent key.
Definition kbd.h:85
@ KBD_PERIOD
Period key.
Definition kbd.h:87
@ KBD_Q
Key Q.
Definition kbd.h:50
@ KBD_MINUS
Minus key.
Definition kbd.h:77
@ KBD_M
Key M.
Definition kbd.h:46
@ KBD_APOSTROPHE
Apostrophe key.
Definition kbd.h:84
@ KBD_3
Key 3.
Definition kbd.h:63
@ KBD_J
Key J.
Definition kbd.h:43
@ KBD_KP_ASTERISK
Keypad Asterisk.
Definition kbd.h:121
@ KBD_BACKSLASH
Backslash key.
Definition kbd.h:81
@ KBD_RIGHT_BRACE
Right brace key.
Definition kbd.h:80
@ KBD_KP_1
Keypad 1.
Definition kbd.h:125
@ KBD_X
Key X.
Definition kbd.h:57
@ KBD_8
Key 8.
Definition kbd.h:68
@ KBD_S
Key S.
Definition kbd.h:52
@ KBD_I
Key I.
Definition kbd.h:42
@ KBD_H
Key H.
Definition kbd.h:41
@ KBD_TAB
Tab key.
Definition kbd.h:75
@ KBD_N
Key N.
Definition kbd.h:47
@ KBD_Z
Key Z.
Definition kbd.h:59
@ KBD_KP_7
Keypad 7.
Definition kbd.h:131
@ KBD_BACKSPACE
Backspace key.
Definition kbd.h:74
@ KBD_HASHTILDE
Hashtilde key.
Definition kbd.h:82
@ KBD_U
Key U.
Definition kbd.h:54
@ KBD_F
Key F.
Definition kbd.h:39
@ KBD_7
Key 7.
Definition kbd.h:67
@ KBD_5
Key 5.
Definition kbd.h:65
@ KBD_KP_9
Keypad 9.
Definition kbd.h:133
@ KBD_0
Key 0.
Definition kbd.h:70
@ KBD_KP_3
Keypad 3.
Definition kbd.h:127
@ KBD_KP_0
Keypad 0.
Definition kbd.h:134
@ KBD_O
Key O.
Definition kbd.h:48
@ KBD_KP_6
Keypad 6.
Definition kbd.h:130
@ KBD_D
Key D.
Definition kbd.h:37
@ KBD_ENTER
Enter key.
Definition kbd.h:72
@ KBD_V
Key V.
Definition kbd.h:55
@ KBD_KP_MINUS
Keypad Minus.
Definition kbd.h:122
@ KBD_W
Key W.
Definition kbd.h:56
@ KBD_EQUAL
Equal key.
Definition kbd.h:78
@ KBD_9
Key 9.
Definition kbd.h:69
@ KBD_KP_SLASH
Keypad Slash.
Definition kbd.h:120
@ KBD_KP_PLUS
Keypad Plus.
Definition kbd.h:123
@ KBD_KP_8
Keypad 8.
Definition kbd.h:132
@ KBD_P
Key P.
Definition kbd.h:49
@ KBD_KP_2
Keypad 2.
Definition kbd.h:126
@ KBD_KP_ENTER
Keypad Enter.
Definition kbd.h:124
@ KBD_KP_EQUAL
Keypad Equal.
Definition kbd.h:141
@ KBD_L
Key L.
Definition kbd.h:45
@ KBD_A
Key A.
Definition kbd.h:34
@ KBD_Y
Key Y.
Definition kbd.h:58
@ KBD_KP_5
Keypad 5.
Definition kbd.h:129
@ KBD_NONE
None.
Definition kbd.h:30
@ KBD_SLASH
Slash key.
Definition kbd.h:88
@ KBD_ESC
Escape key.
Definition kbd.h:73
@ KBD_C
Key C.
Definition kbd.h:36
@ KBD_B
Key B.
Definition kbd.h:35
@ KBD_G
Key G.
Definition kbd.h:40
@ KBD_E
Key E.
Definition kbd.h:38
@ KBD_KP_PERIOD
Keypad Period.
Definition kbd.h:135
@ KBD_COMMA
Comma key.
Definition kbd.h:86
@ KBD_LEFT_BRACE
Left brace key.
Definition kbd.h:79
@ KBD_SEMICOLON
Semicolon key.
Definition kbd.h:83
boot_memory_map_t * map
Definition mem.c:19
#define UINT8_MAX
Definition stdint.h:62
Definition kbd.c:8
char shift
Definition kbd.c:10
char norm
Definition kbd.c:9
Definition kbd.c:14
keymap_entry_t map[UINT8_MAX]
Definition kbd.c:15