Browse Source

Added ability for commands to accept keyboard input

Stephen Downward 9 months ago
parent
commit
61b787a982
2 changed files with 71 additions and 38 deletions
  1. 68 37
      util/terminal.c
  2. 3 1
      util/terminal.h

+ 68 - 37
util/terminal.c

@ -83,13 +83,35 @@ char cmd_history[50][512]; //First element holds key_buffer when we press up
83 83
u8 cmd_his_len = 0;
84 84
char key_buffer[512];
85 85
86
void keyboard_handler(char key) {
86
void (*ext_handler)(char key);
87
void (*special_handler)(enum SPECIAL key);
87 88
89
void register_keyboard_handler(void (*handler)(char key), void(*special)(enum SPECIAL key))
90
{
91
	ext_handler = handler;
92
	special_handler = special;
93
}
94
95
void remove_keyboard_handler()
96
{
97
	ext_handler = 0;
98
	special_handler = 0;
99
}
100
101
void keyboard_handler(char key)
102
{
88 103
	//Print character
89 104
	char printstr[] = {key, 0};
90 105
	print(printstr);
91 106
92
	append(key_buffer, key);
107
	if(!ext_handler)
108
	{
109
		append(key_buffer, key);
110
	}
111
	else
112
	{
113
		ext_handler(key);
114
	}
93 115
}
94 116
95 117
u8 cmd_his_index = 0;
@ -104,52 +126,61 @@ void goto_cmd_history()
104 126
}
105 127
106 128
void keyboard_handler_special(enum SPECIAL key) {
107
	if (key == BACKSPACE)
129
	//Note: we check for ext_handler, not special_handler, because we don't want the terminal
130
	//to still act like a terminal when a cmd program is running.
131
	if(!ext_handler)
108 132
	{
109
		if (strlen(key_buffer) > 0)
133
		if (key == BACKSPACE)
110 134
		{
111
			backspace(key_buffer);
112
			delete_last();
113
		}
114
	}
115
	else if (key == ENTER)
116
	{
117
		cmd_his_index = 0;
118
		//Shift the cmd_history array down
119
		for (int i = 48; i >= 1; i--)
120
		{
121
			strcpy(cmd_history[i + 1], cmd_history[i]);
135
			if (strlen(key_buffer) > 0)
136
			{
137
				backspace(key_buffer);
138
				delete_last();
139
			}
122 140
		}
123
		cmd_his_len++;
124
		strcpy(cmd_history[1], key_buffer);
125
126
		newline();
127
		if(!parse_command(key_buffer))
141
		else if (key == ENTER)
128 142
		{
129
			println("Command not found.");
143
			cmd_his_index = 0;
144
			//Shift the cmd_history array down
145
			for (int i = 48; i >= 1; i--)
146
			{
147
				strcpy(cmd_history[i + 1], cmd_history[i]);
148
			}
149
			cmd_his_len++;
150
			strcpy(cmd_history[1], key_buffer);
151
			
152
			newline();
153
			if(!parse_command(key_buffer))
154
			{
155
				println("Command not found.");
156
			}
157
			key_buffer[0] = '\0';
158
			print("> ");
130 159
		}
131
		key_buffer[0] = '\0';
132
		print("> ");
133
	}
134
	else if (key == ARW_UP)
135
	{
136
		if (cmd_his_index==0)
160
		else if (key == ARW_UP)
137 161
		{
138
			strcpy(cmd_history[0], key_buffer);
162
			if (cmd_his_index==0)
163
			{
164
				strcpy(cmd_history[0], key_buffer);
165
			}
166
			if (cmd_his_index < cmd_his_len)
167
			{
168
				cmd_his_index++;
169
				goto_cmd_history();
170
			}
139 171
		}
140
		if (cmd_his_index < cmd_his_len)
172
		else if (key == ARW_DOWN)
141 173
		{
142
			cmd_his_index++;
143
			goto_cmd_history();
174
			if (cmd_his_index > 0)
175
			{
176
				cmd_his_index--;
177
				goto_cmd_history();
178
			}
144 179
		}
145 180
	}
146
	else if (key == ARW_DOWN)
181
	else if(special_handler)
147 182
	{
148
		if (cmd_his_index > 0)
149
		{
150
			cmd_his_index--;
151
			goto_cmd_history();
152
		}
183
		special_handler(key);
153 184
	}
154 185
}
155 186

+ 3 - 1
util/terminal.h

@ -16,5 +16,7 @@ void register_command(char *cmd, void (*handler)(char *));
16 16
int parse_command(char *linein);
17 17
void keyboard_handler(char key);
18 18
void keyboard_handler_special(enum SPECIAL key);
19
19
void register_keyboard_handler(void (*handler)(char key), void (*special)(enum SPECIAL key));
20
void remove_keyboard_handler();
21
void delete_last();
20 22
#endif