diff --git a/src/Makefile b/src/Makefile index 12aeae7..b93660a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,16 +1,19 @@ TARGET=editor CC=gcc -OBJ = main.o draw.o term.o +OBJ = main.o term.o draw.o controls.o $(TARGET): $(OBJ) mkdir -p ../build $(CC) -o $(TARGET) $(OBJ) -g mv $(TARGET) ../build/ -main.o: term.h draw.h -term.o: term.h draw.h -draw.o: term.h draw.h +main.o: headers/term.h headers/draw.h headers/controls.h +term.o: headers/term.h +draw.o: headers/term.h headers/draw.h +controls.o: headers/controls.h headers/__defines.h headers/draw.h headers/term.h + + .PHONY: clean clean: diff --git a/src/controls.c b/src/controls.c new file mode 100644 index 0000000..2b5c7e4 --- /dev/null +++ b/src/controls.c @@ -0,0 +1,96 @@ +#include + +#include "headers/__defines.h" +#include "headers/controls.h" +#include "headers/draw.h" +#include "headers/term.h" + +extern editor_t editor; +extern screen_buffer_t screen_buffer; + +int parse_input_char(char input_char) +{ + if (INSERT_MODE == editor.control_type) { + if (ESC == input_char) { + editor.control_type = COMMAND_MODE; + return 0; + } + + // put char + + return 0; + } + + switch(input_char) { + /** INSERT MODE KEYS START **/ + case 'a': + move_cursor(RIGHT); + case 'i': + editor.control_type = INSERT_MODE; + break; + /** INSERT MODE KEYS END **/ + + /** MOVEMENT KEYS START **/ + + case 'h': + move_cursor(LEFT); + break; + case 'j': + move_cursor(DOWN); + break; + case 'k': + move_cursor(UP); + break; + case 'l': + move_cursor(RIGHT); + break; + + case 'g': // TODO: move this to 'gg' + move_cursor(HOME); + break; + case 'G': + // want to move to the end of the buffer + break; + + case '0': + move_cursor(BEGIN_LINE); + break; + + + /** MOVEMENT KEYS END **/ + default: + return -1; + } + return 0; +} + +void move_cursor(movement_t dir) +{ + switch(dir) { + case LEFT: + move_cursor_pos(--editor.cx, editor.cy); + break; + case RIGHT: + move_cursor_pos(++editor.cx, editor.cy); + break; + case UP: + move_cursor_pos(editor.cx, --editor.cy); + break; + case DOWN: + move_cursor_pos(editor.cx, ++editor.cy); + break; + case HOME: + move_cursor_pos(1, 1); + editor.cx = 1; + editor.cy = 1; + break; + case BEGIN_LINE: + move_cursor_pos(1, editor.cy); + editor.cx = 1; + break; + default: + fprintf(stderr, "move_cursor had an invalid input, what the hell.\n"); + exit(EXIT_FAILURE); + } +} + diff --git a/src/draw.c b/src/draw.c index 6327485..22768ec 100644 --- a/src/draw.c +++ b/src/draw.c @@ -2,8 +2,8 @@ #include #include -#include "draw.h" -#include "term.h" +#include "headers/draw.h" +#include "headers/term.h" #define MAX_LEN 256 @@ -17,13 +17,11 @@ extern screen_buffer_t screen_buffer; void refresh_screen(void) { /** - * TODO: need to move the cursor back at the end - * also need to draw the cusor coords at the bottom + * TODO: * and need to implement controls */ int cur_x, cur_y; - char* move_cursor; cur_x = editor.cx; cur_y = editor.cy; @@ -36,10 +34,9 @@ void refresh_screen(void) screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN); draw_editor_rows(); + draw_cursor_pos(); - move_cursor_pos(&move_cursor, cur_x, cur_y); - screen_buffer_append(&screen_buffer, move_cursor, strlen(move_cursor)); - free(move_cursor); + move_cursor_pos(cur_x, cur_y); screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN); @@ -53,11 +50,21 @@ void draw_editor_rows(void) screen_buffer_append(&screen_buffer, "~\r\n", 3); } - -void move_cursor_pos(char** out, int x, int y) +void draw_cursor_pos(void) { - *out = malloc(sizeof(char) * MAX_LEN); - snprintf(*out, MAX_LEN, "\e[%d;%dH", x, y); + char* pos = malloc(sizeof(char) * MAX_LEN); + snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy); + screen_buffer_append(&screen_buffer, pos, strlen(pos)); + free(pos); +} + +void move_cursor_pos(int x, int y) +{ + // TODO: add check for boundaries + char* out = malloc(sizeof(char) * MAX_LEN); + snprintf(out, MAX_LEN, "\e[%d;%dH", y, x); + screen_buffer_append(&screen_buffer, out, strlen(out)); + free(out); } #undef MAX_LEN diff --git a/src/headers/__defines.h b/src/headers/__defines.h new file mode 100644 index 0000000..e6f3827 --- /dev/null +++ b/src/headers/__defines.h @@ -0,0 +1,8 @@ +#ifndef __DEFINES_H +#define __DEFINES_H + +typedef enum { + ESC = 0x1B, +} key_code_t; + +#endif diff --git a/src/headers/controls.h b/src/headers/controls.h new file mode 100644 index 0000000..1c38d7c --- /dev/null +++ b/src/headers/controls.h @@ -0,0 +1,15 @@ +#ifndef CONTROLS_H +#define CONTROLS_H + +typedef enum { + UP, DOWN, + LEFT, RIGHT, + HOME, + BEGIN_LINE, +} movement_t; + +int parse_input_char(char in); + +void move_cursor(movement_t dir); + +#endif diff --git a/src/draw.h b/src/headers/draw.h similarity index 74% rename from src/draw.h rename to src/headers/draw.h index 1b2553c..52168d0 100644 --- a/src/draw.h +++ b/src/headers/draw.h @@ -19,17 +19,14 @@ void refresh_screen(void); void draw_editor_rows(void); +void draw_cursor_pos(void); /** * # move_cursor_pos * - * - char** out - pointer to char* which will be malloc'd and have the formatted string placed into * - int x - x pos * - int y - y pos - * - * out must be free'd */ -void move_cursor_pos(char** out, int x, int y); - +void move_cursor_pos(int x, int y); #endif diff --git a/src/term.h b/src/headers/term.h similarity index 94% rename from src/term.h rename to src/headers/term.h index 311fa76..8360177 100644 --- a/src/term.h +++ b/src/headers/term.h @@ -4,8 +4,14 @@ #ifndef TERM_H #define TERM_H +typedef enum { + COMMAND_MODE, + INSERT_MODE, +} control_type_t; + typedef struct { struct termios term_settings; + control_type_t control_type; size_t cols, rows; int cx, cy; // cursor pos } editor_t; diff --git a/src/main.c b/src/main.c index 8b5ff3b..7e8002c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ -#include #include -#include "draw.h" -#include "term.h" +#include "headers/controls.h" +#include "headers/draw.h" +#include "headers/term.h" editor_t editor; screen_buffer_t screen_buffer; @@ -20,6 +20,8 @@ int main(void) read(STDIN_FILENO, &c, 1); if (c == '\004') break; + else + parse_input_char(c); refresh_screen(); } diff --git a/src/term.c b/src/term.c index ff5121a..5275fb4 100644 --- a/src/term.c +++ b/src/term.c @@ -6,7 +6,7 @@ #include -#include "term.h" +#include "headers/term.h" /** BEGIN extern section **/ @@ -107,8 +107,10 @@ void setup_terminal(void) exit(EXIT_FAILURE); } - editor.cx = 0; - editor.cy = 0; + editor.control_type = COMMAND_MODE; + + editor.cx = 1; + editor.cy = 1; editor.cols = win.ws_col; editor.rows = win.ws_row;