-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
270 lines (225 loc) · 6.16 KB
/
input.c
File metadata and controls
270 lines (225 loc) · 6.16 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
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
/* KallistiOS ##version##
input.c
Copyright (C) 2002 Megan Potter
Adapted from Kosh, Copyright (C) 2000 Jordan DeLong
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <kos/limits.h>
#include <kos/thread.h>
#include <kos/sem.h>
#include <dc/maple/keyboard.h>
#include "conio.h"
/* This module defines a conio input system, if you want to use it. */
/* the buffer for input */
static struct {
char text[CONIO_INPUT_BUFFSIZE];
unsigned int pos; /* pos in the buffer, not the screen */
} input_buffer;
/* the state var */
enum {
INPUT_PROMPT, /* print the prompt, etc */
INPUT_READCOMM, /* read and edit a command line */
INPUT_COMMAND /* call a command based on input_buffer.text */
} input_state = INPUT_PROMPT;
/***********************************************************************/
/* Default input callback system: queue up lines of text
and return them using a semaphore for flow control. */
typedef struct cb_sem_data_str {
char line[256];
struct cb_sem_data_str *next;
} cb_sem_data_t;
static volatile cb_sem_data_t *cb_queue;
static semaphore_t cb_sem, cb_mutex;
static volatile int cb_dead;
static void input_cb_init(void) {
sem_init(&cb_sem, 0);
sem_init(&cb_mutex, 1);
cb_queue = NULL;
cb_dead = 0;
}
static void cb_default(const char *str) {
cb_sem_data_t *t;
t = malloc(sizeof(cb_sem_data_t));
sem_wait(&cb_mutex);
strncpy(t->line, str, 255); t->line[255] = '\0';
t->next = (cb_sem_data_t *)cb_queue;
cb_queue = t;
sem_signal(&cb_mutex);
sem_signal(&cb_sem);
}
static void input_cb_shutdown(void) {
cb_sem_data_t *t, *n;
int i;
/* Make sure any clients waiting on getline go through */
cb_dead = 1;
cb_default("");
/* This is pretty damned hacky but it should do the job */
for (i=0; i<5; i++)
thd_pass();
sem_destroy(&cb_mutex);
sem_destroy(&cb_sem);
for (t=(cb_sem_data_t *)cb_queue; t; t = n) {
n = t->next;
free(t);
}
cb_queue = NULL;
}
int conio_input_getline(int block, char *dst, int dstcnt) {
cb_sem_data_t *t, *l;
/* assert_msg( block != 0, "non-blocking I/O not supported yet" ); */
if (!block && cb_queue == NULL)
return -1;
/* Did we quit already? */
if (cb_dead)
return -1;
/* Wait for some input to be ready */
if (block > 0) {
if (sem_wait_timed(&cb_sem, block) < 0)
return -1;
} else {
if (sem_wait(&cb_sem) < 0)
return -1;
}
/* Grab the mutex and retrieve the line */
sem_wait(&cb_mutex);
assert( cb_queue != NULL );
for (l=NULL, t=(cb_sem_data_t *)cb_queue; t->next; t=t->next)
l=t;
assert( t->next == NULL );
if (l != NULL) {
assert( l->next == t );
l->next = NULL;
} else {
cb_queue = NULL;
}
sem_signal(&cb_mutex);
strncpy(dst, t->line, dstcnt-1);
dst[dstcnt-1] = '\0';
free(t);
return 0;
}
/***********************************************************************/
/* Input callbacks */
static conio_input_callback_t input_cb = NULL;
void conio_input_callback(conio_input_callback_t cb) {
if (cb == NULL)
input_cb = cb_default;
else
input_cb = cb;
}
/***********************************************************************/
/* add text to the input buff and putch it */
static void input_insertbuff(int ch) {
int len;
len = strlen(input_buffer.text);
if (len >= CONIO_INPUT_BUFFSIZE - 1)
return;
/* our str */
memmove(&input_buffer.text[input_buffer.pos + 1], &input_buffer.text[input_buffer.pos],
len - input_buffer.pos + 1);
/* the virtscr */
if (conio_cursor.row * CONIO_NUM_COLS + conio_cursor.col + len - input_buffer.pos + 1
>= CONIO_NUM_COLS * CONIO_NUM_ROWS)
conio_scroll();
memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col + 1],
&conio_virtscr[conio_cursor.row][conio_cursor.col],
len - input_buffer.pos + 1);
input_buffer.text[input_buffer.pos] = ch;
input_buffer.pos++;
if (conio_ttymode != CONIO_TTY_STDIO)
conio_putch(ch);
return;
}
/* remove the char at input_buffer.pos from the buffer, and reflect the changes on the virtscr */
static void input_delchar_buff(void) {
int len;
len = strlen(input_buffer.text);
if (len < 1)
return;
/* our str */
memmove(&input_buffer.text[input_buffer.pos], &input_buffer.text[input_buffer.pos + 1],
len - input_buffer.pos + 1);
input_buffer.text[len] = '\0';
/* the virtscr */
memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col],
&conio_virtscr[conio_cursor.row][conio_cursor.col + 1],
len - input_buffer.pos + 1);
}
/* print the prompt out, clear input buffer */
static void input_prompt(void) {
input_buffer.pos = 0;
*input_buffer.text = '\0';
/* conio_putstr("> "); */
input_state = INPUT_READCOMM;
}
/* reading commands from the prompt */
static void input_readcomm(void) {
int key;
key = conio_check_getch();
switch (key) {
case -1:
return;
case KBD_KEY_DEL << 8:
input_delchar_buff();
break;
case 8: /* backspace */
if (input_buffer.pos > 0) {
conio_deadvance_cursor();
input_buffer.pos--;
input_delchar_buff();
}
break;
case '\r':
input_state = INPUT_COMMAND;
conio_putch('\n');
break;
case KBD_KEY_LEFT << 8: /* left */
if (input_buffer.pos > 0) {
input_buffer.pos--;
conio_deadvance_cursor();
}
break;
case KBD_KEY_RIGHT << 8:
if (input_buffer.pos < strlen(input_buffer.text)) {
input_buffer.pos++;
conio_advance_cursor();
}
break;
case KBD_KEY_HOME << 8:
for (; input_buffer.pos > 0; input_buffer.pos--)
conio_deadvance_cursor();
break;
case KBD_KEY_END << 8:
for (; input_buffer.pos < strlen(input_buffer.text); input_buffer.pos++)
conio_advance_cursor();
break;
default:
if (isascii(key))
input_insertbuff(key);
}
}
/* read command line, try to execute builtin commands, otherwise externals (this coming soon) */
static void input_command(void) {
input_state = INPUT_PROMPT;
if (input_cb)
input_cb(input_buffer.text);
}
void conio_input_init(void) {
input_cb_init();
input_cb = cb_default;
}
void conio_input_shutdown(void) {
input_cb = NULL;
input_cb_shutdown();
}
/* our exported input function, called once per frame. */
void conio_input_frame(void) {
switch (input_state) {
case INPUT_PROMPT: input_prompt(); break;
case INPUT_READCOMM: input_readcomm(); break;
case INPUT_COMMAND: input_command(); break;
}
}