/
notepad.c
63 lines (54 loc) · 1.5 KB
/
notepad.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
#include <stdint.h>
#include <stdbool.h>
#include "notepad.h"
#include "terminal.h"
#include "keyboard.h"
//charsDown is for keeping track of the keys that are pressed down in order to show a mash message Not the most efficient way, but
//I didn't want to include a hashtable
static bool charsDown[256];
static const uint8_t MAX_KEYS_DOWN = 6;
static bool relaxMsg = false;
static uint8_t totalDown = 0;
static void onPrintableKeyDown(char c){
if(totalDown < MAX_KEYS_DOWN){
terminalPutChar(c);
}
}
static void onKeyDown(char code){
//we get this event when a key is held down, so this prevents us from overcounting keys that are held down.
if(!charsDown[(uint32_t) code]){
charsDown[(uint32_t) code] = true;
++totalDown;
}
if(totalDown >= MAX_KEYS_DOWN){
if(!relaxMsg){
terminalWriteString(" Relax. It's going to be ok. ");
relaxMsg = true;
}
}
else{
relaxMsg = false;
switch (code){
case KEY_RETURN:
terminalNewline();
break;
case KEY_BACKSPACE:
terminalBackspace();
break;
}
}
}
static void onKeyUp(char code){
charsDown[(uint32_t) code] = false;
//preventing an overflow. We could get an onKeyUp if it didn't have focus.
if(totalDown > 0){
--totalDown;
}
}
void notepad_init(){
terminalInitialize(VGA_COLOR_LIGHT_GREY, VGA_COLOR_RED);
terminalWriteString("You should type stuff> ");
registerKeyboardCallback(PRINTABLE_KEY_DOWN_EVENT, onPrintableKeyDown);
registerKeyboardCallback(KEY_DOWN_EVENT, onKeyDown);
registerKeyboardCallback(KEY_UP_EVENT, onKeyUp);
}