Espresso 0.0.2a
This commit is contained in:
@ -31,6 +31,14 @@
|
||||
|
||||
#define RETURN_STI() do { _sti_asm(); return; } while (0)
|
||||
|
||||
|
||||
#define GETS_BUFFER_SIZE 256
|
||||
|
||||
static char gets_buffer[GETS_BUFFER_SIZE];
|
||||
volatile char* gets_string = gets_buffer;
|
||||
volatile int32_t gets_capacity = GETS_BUFFER_SIZE;
|
||||
volatile int32_t gets_length = 0;
|
||||
|
||||
bool ps2keyboard_initialized = false;
|
||||
|
||||
/* State for shift key */
|
||||
@ -50,11 +58,8 @@ static volatile bool gets_finished = false;
|
||||
|
||||
volatile unsigned char current_char;
|
||||
volatile char* current_string = NULL;
|
||||
volatile char* gets_string = NULL;
|
||||
volatile int32_t current_length = 0;
|
||||
volatile int32_t gets_length = 0;
|
||||
volatile int32_t capacity = 0;
|
||||
volatile int32_t gets_capacity = 0;
|
||||
volatile uint16_t current_key;
|
||||
|
||||
volatile ps2_hook_t* hooks = NULL;
|
||||
@ -88,7 +93,7 @@ void keyboard_init(void)
|
||||
|
||||
outb(0x64, 0xAE); /* Enable keyboard interface (often optional, but here for safety) */
|
||||
|
||||
hooks = (ps2_hook_t*) malloc(sizeof(ps2_hook_t) * hook_count);
|
||||
hooks = NULL;
|
||||
|
||||
ps2keyboard_initialized = true;
|
||||
|
||||
@ -251,143 +256,95 @@ char* get_string(void)
|
||||
return current_string;
|
||||
}
|
||||
|
||||
static inline void print_(void)
|
||||
{
|
||||
static char ch = 'a';
|
||||
|
||||
int r = 0, c = 0;
|
||||
|
||||
terminal_get_cursor(&r, &c);
|
||||
terminal_set_cursor((uint16_t) 30, (uint16_t) 0);
|
||||
printf("%c", ch);
|
||||
terminal_set_cursor((uint16_t) r, (uint16_t) c);
|
||||
|
||||
ch++;
|
||||
}
|
||||
|
||||
char* kbd_gets(void)
|
||||
{
|
||||
gets_called = true;
|
||||
gets_finished = false;
|
||||
|
||||
while (gets_finished != true)
|
||||
gets_length = 0;
|
||||
gets_string[0] = '\0';
|
||||
|
||||
while (!gets_finished)
|
||||
{
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
char* result = strdup(gets_string); /* Could be NULL, that is checked below */
|
||||
|
||||
free(gets_string);
|
||||
|
||||
gets_string = NULL;
|
||||
gets_capacity = 0;
|
||||
|
||||
_cli_asm();
|
||||
|
||||
char* result = strdup((char*) gets_string);
|
||||
|
||||
gets_length = 0;
|
||||
gets_string[0] = '\0';
|
||||
gets_called = false;
|
||||
|
||||
return result == NULL ? "" : result;
|
||||
|
||||
_sti_asm();
|
||||
|
||||
return result ? result : "";
|
||||
}
|
||||
|
||||
void gets_append_char(unsigned char c)
|
||||
{
|
||||
_cli_asm();
|
||||
|
||||
if (!gets_string && gets_capacity > 0)
|
||||
if (!gets_called)
|
||||
{
|
||||
printf("[keyboard] ERROR: gets_string is NULL but capacity > 0!\n");
|
||||
RETURN_STI();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (c == KEY_ARROW_UP || c == KEY_ARROW_DOWN || c == KEY_ARROW_RIGHT || c == KEY_ARROW_LEFT)
|
||||
{
|
||||
gets_string[1] = '\0';
|
||||
|
||||
char tc = '\0';
|
||||
|
||||
switch (c)
|
||||
if (GETS_BUFFER_SIZE >= 2)
|
||||
{
|
||||
case KEY_ARROW_UP:
|
||||
tc = KEY_UP;
|
||||
break;
|
||||
case KEY_ARROW_DOWN:
|
||||
tc = KEY_DOWN;
|
||||
break;
|
||||
case KEY_ARROW_RIGHT:
|
||||
tc = KEY_RIGHT;
|
||||
break;
|
||||
case KEY_ARROW_LEFT:
|
||||
tc = KEY_LEFT;
|
||||
break;
|
||||
default:
|
||||
tc = '\0';
|
||||
break;
|
||||
gets_string[0] = (char)c;
|
||||
gets_string[1] = '\0';
|
||||
}
|
||||
|
||||
gets_string[0] = tc;
|
||||
|
||||
|
||||
gets_finished = true;
|
||||
|
||||
RETURN_STI();
|
||||
return;
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
/* The returned string must/will not have newlines in it, so we return here after setting gets_finished to 'true'. */
|
||||
|
||||
gets_finished = true;
|
||||
|
||||
printf("\n");
|
||||
|
||||
RETURN_STI();
|
||||
return;
|
||||
}
|
||||
else if (c == 27) /* ASCII escape, here it's used to cancel input. */
|
||||
else if (c == 27)
|
||||
{
|
||||
gets_finished = true;
|
||||
gets_string[0] = '\0';
|
||||
|
||||
RETURN_STI();
|
||||
gets_finished = true;
|
||||
return;
|
||||
}
|
||||
else if (c == '\b')
|
||||
{
|
||||
if (gets_length < 1)
|
||||
if (gets_length > 0)
|
||||
{
|
||||
RETURN_STI();
|
||||
gets_length--;
|
||||
gets_string[gets_length] = '\0';
|
||||
printf("\b \b");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
gets_length--;
|
||||
if (gets_length < GETS_BUFFER_SIZE - 1)
|
||||
{
|
||||
gets_string[gets_length++] = (char)c;
|
||||
gets_string[gets_length] = '\0';
|
||||
|
||||
printf("\b \b");
|
||||
|
||||
RETURN_STI();
|
||||
printf("%c", c);
|
||||
}
|
||||
|
||||
if ((gets_length) >= gets_capacity)
|
||||
{
|
||||
int new_capacity = (gets_capacity == 0) ? 64 : gets_capacity * 2;
|
||||
|
||||
char* new_str = (char*) malloc(new_capacity);
|
||||
|
||||
if (!new_str)
|
||||
{
|
||||
RETURN_STI();
|
||||
}
|
||||
|
||||
if (gets_string)
|
||||
{
|
||||
for (int i = 0; i < gets_length; i++)
|
||||
{
|
||||
new_str[i] = gets_string[i];
|
||||
}
|
||||
|
||||
new_str[gets_length] = '\0';
|
||||
|
||||
free(gets_string);
|
||||
}
|
||||
|
||||
gets_string = new_str;
|
||||
gets_capacity = new_capacity;
|
||||
}
|
||||
|
||||
if (!gets_string)
|
||||
{
|
||||
RETURN_STI();
|
||||
}
|
||||
|
||||
gets_string[gets_length] = (char) c;
|
||||
gets_length++;
|
||||
gets_string[gets_length] = '\0';
|
||||
|
||||
printf("%c", c);
|
||||
|
||||
RETURN_STI();
|
||||
}
|
||||
|
||||
|
||||
void append_char(char c)
|
||||
{
|
||||
_cli_asm();
|
||||
|
||||
Reference in New Issue
Block a user