summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-09 15:47:26 -0700
committercflip <cflip@cflip.net>2023-01-09 15:47:26 -0700
commitc1573dbbb3e70cad8c1f94cbed5b2abf2b124ace (patch)
tree8333d29b92f12f7cbf97deb38e25875887d6b19a
parent54d8fdd290e21ae8c773b4228827ee7539637ce8 (diff)
Implement basic keyboard event handling
-rw-r--r--input.c5
-rw-r--r--input.h2
-rw-r--r--main.c2
-rw-r--r--window.c42
-rw-r--r--window.h2
5 files changed, 45 insertions, 8 deletions
diff --git a/input.c b/input.c
index 17a37f6..fac8aba 100644
--- a/input.c
+++ b/input.c
@@ -97,10 +97,9 @@ void editor_move_cursor(struct editor_state* editor, int key)
editor->cursor_x = row_length;
}
-void editor_process_keypress(struct editor_state* editor)
+void editor_process_keypress(struct editor_state* editor, int c)
{
static int quit_message = 1;
- int c = editor_read_key();
switch (c) {
case '\r':
@@ -112,8 +111,6 @@ void editor_process_keypress(struct editor_state* editor)
quit_message = 0;
return;
}
- write(STDOUT_FILENO, "\x1b[2J", 4);
- write(STDOUT_FILENO, "\x1b[H", 3);
exit(0);
break;
case CTRL_KEY('s'):
diff --git a/input.h b/input.h
index bd286b2..4b7eb05 100644
--- a/input.h
+++ b/input.h
@@ -20,6 +20,6 @@ enum editor_key {
int editor_read_key();
void editor_move_cursor(struct editor_state* editor, int key);
-void editor_process_keypress(struct editor_state* editor);
+void editor_process_keypress(struct editor_state* editor, int keycode);
#endif
diff --git a/main.c b/main.c
index 3718c41..2e6bdc4 100644
--- a/main.c
+++ b/main.c
@@ -16,7 +16,7 @@ int main(int argc, char** argv)
editor_set_status_message(&editor, "HELP: Ctrl+Q = quit, Ctrl+S = save, Ctrl+F = find");
- while (window_handle_event()) {
+ while (window_handle_event(&editor)) {
window_redraw(&editor);
// editor_refresh_screen(&editor);
// editor_process_keypress(&editor);
diff --git a/window.c b/window.c
index 81bf634..027d876 100644
--- a/window.c
+++ b/window.c
@@ -5,6 +5,7 @@
#include "buffer.h"
#include "editor.h"
#include "font.h"
+#include "input.h"
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
@@ -39,13 +40,52 @@ void window_init()
SDL_ShowWindow(window);
}
-int window_handle_event()
+int window_handle_event(struct editor_state *editor)
{
static SDL_Event e;
SDL_WaitEvent(&e);
switch (e.type) {
case SDL_QUIT:
return 0;
+ case SDL_KEYDOWN:
+ int keycode = e.key.keysym.sym;
+ switch (e.key.keysym.sym) {
+ case SDLK_BACKSPACE:
+ keycode = BACKSPACE;
+ break;
+ case SDLK_LEFT:
+ keycode = ARROW_LEFT;
+ break;
+ case SDLK_RIGHT:
+ keycode = ARROW_RIGHT;
+ break;
+ case SDLK_UP:
+ keycode = ARROW_UP;
+ break;
+ case SDLK_DOWN:
+ keycode = ARROW_DOWN;
+ break;
+ case SDLK_DELETE:
+ keycode = DELETE_KEY;
+ break;
+ case SDLK_HOME:
+ keycode = HOME_KEY;
+ break;
+ case SDLK_END:
+ keycode = END_KEY;
+ break;
+ case SDLK_PAGEUP:
+ keycode = PAGE_UP;
+ break;
+ case SDLK_PAGEDOWN:
+ keycode = PAGE_DOWN;
+ break;
+ }
+ if (e.key.keysym.mod & KMOD_CTRL) {
+ keycode = CTRL_KEY(keycode);
+ }
+ editor_process_keypress(editor, keycode);
+ break;
}
return 1;
}
diff --git a/window.h b/window.h
index 0175838..1c2e019 100644
--- a/window.h
+++ b/window.h
@@ -4,7 +4,7 @@
struct editor_state;
void window_init();
-int window_handle_event();
+int window_handle_event(struct editor_state *editor);
void window_redraw(struct editor_state *editor);
void window_destroy();