diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/piece-table-internals.h | 5 | ||||
-rw-r--r-- | src/piece-table.h | 2 | ||||
-rw-r--r-- | src/tui-internals.h | 58 | ||||
-rw-r--r-- | src/tui.c | 193 | ||||
-rw-r--r-- | src/tui.h | 31 | ||||
-rw-r--r-- | src/ui.c | 74 | ||||
-rw-r--r-- | src/ui.h | 14 |
8 files changed, 307 insertions, 80 deletions
@@ -15,8 +15,14 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <stdio.h> +#include "ui.h" + int -main(int argc, char *argv[]) +main (int argc, char* argv[]) { - return 0; + ui *ui = ui_create (); + ui_loop (ui); + ui_destroy (ui); + return 0; } diff --git a/src/piece-table-internals.h b/src/piece-table-internals.h index b68df9f..2da6c53 100644 --- a/src/piece-table-internals.h +++ b/src/piece-table-internals.h @@ -15,6 +15,9 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#ifndef PIECE_TABLE_INTERNALS_H +#define PIECE_TABLE_INTERNALS_H + typedef struct { size_t used; @@ -87,3 +90,5 @@ char piece_table_index (piece_table *pt, size_t pos); void piece_table_to_string (piece_table *pt, char *buf, size_t size); size_t piece_table_length (piece_table *pt); void piece_table_optimize (piece_table *pt); + +#endif /* PIECE_TABLE_INTERNALS_H */ diff --git a/src/piece-table.h b/src/piece-table.h index ce3f21d..a26ce82 100644 --- a/src/piece-table.h +++ b/src/piece-table.h @@ -35,4 +35,4 @@ size_t piece_table_length (piece_table *pt); void piece_table_optimize (piece_table *pt); -#endif +#endif /* PIECE_TABLE_H */ diff --git a/src/tui-internals.h b/src/tui-internals.h new file mode 100644 index 0000000..c64964e --- /dev/null +++ b/src/tui-internals.h @@ -0,0 +1,58 @@ +/* parc + * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TUI_INTERNALS_H +#define TUI_INTERNALS_H + +#include <termios.h> + +typedef struct _tui { + struct termios term_old_config; + struct termios term_config; +} tui; + +typedef struct _mouse_event { + int x; + int y; +} mouse_event; + +typedef struct _scroll_event { + int x; + int y; +} scroll_event; + +typedef struct _key_event { + int key; + int modifiers; +} key_event; + +void tui_cursor_save (void); +void tui_cursor_restore (void); +void tui_mouse_enable (void); +void tui_mouse_disable (void); +void tui_cursor_move (int x, int y); +void tui_cursor_show (void); +void tui_cursor_hide (void); +void tui_screen_erase (void); +void tui_alternate_buffer_enable (void); +void tui_alternate_buffer_disable (void); +void tui_input_sequence_consume (void); +tui *tui_init (void); +void tui_deinit (tui *ui); +void tui_loop (tui *ui); + +#endif /* TUI_INTERNALS_H */ diff --git a/src/tui.c b/src/tui.c new file mode 100644 index 0000000..d4bc1aa --- /dev/null +++ b/src/tui.c @@ -0,0 +1,193 @@ +/* parc + * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/* + * XTERM escape sequence reference: + * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html + * Good summary: + * https://en.wikipedia.org/wiki/ANSI_escape_code + * + * also: + * man console_codes + */ + +#include <stdio.h> +#include <stdlib.h> +#include <termios.h> +#include <string.h> +#include "tui-internals.h" + +#define ESC '\x1b' + +const char CSI_LEAD[3] = { ESC, '[', '\0'}; + +void +tui_cursor_save (void) +{ + printf ("%ss", CSI_LEAD); + fflush (stdout); +} + +void +tui_cursor_restore (void) +{ + printf ("%su", CSI_LEAD); + fflush (stdout); +} + +void +tui_mouse_enable (void) +{ + printf ("%s?1000h", CSI_LEAD); + fflush (stdout); +} +void +tui_mouse_disable (void) +{ + printf ("%s?1000l", CSI_LEAD); + fflush (stdout); +} + +void +tui_cursor_move (int x, int y) +{ + /* + * Also valid with CSI<L>;<C>f being <L> and <C> line number and column + * number respectively. In ttys, cursor starts at 1,1 + */ + printf ("%s%d;%dH", CSI_LEAD, y, x); + fflush (stdout); +} + +void +tui_cursor_show (void) +{ + printf ("%s?25h", CSI_LEAD); + fflush (stdout); +} + +void +tui_cursor_hide (void) +{ + printf ("%s?25l", CSI_LEAD); + fflush (stdout); +} + +void +tui_screen_erase (void) +{ + printf ("%s2J", CSI_LEAD); + fflush (stdout); +} + +void +tui_alternate_buffer_enable (void) +{ + printf ("%s?1049h", CSI_LEAD); + fflush (stdout); +} + +void +tui_alternate_buffer_disable (void) +{ + printf ("%s?1049l", CSI_LEAD); + fflush (stdout); +} + +tui * +tui_create (void) +{ + tui *ui = malloc (sizeof *ui); + /* Get interface configuration to reset it later */ + tcgetattr (0, &ui->term_old_config); + + /* Get interface configuration to edit */ + tcgetattr (0, &ui->term_config); + + /* Set the new configuration */ + ui->term_config.c_lflag &= ~ (ECHO | ECHONL | ICANON | IEXTEN | ISIG); + ui->term_config.c_cc[VMIN] = 1; /* Wait until 1 character is in buffer */ + ui->term_config.c_cc[VTIME] = 0; /* Wait indefinitely */ + /* TCSANOW makes the change occur immediately */ + tcsetattr (0, TCSANOW, &ui->term_config); + tui_alternate_buffer_enable (); + tui_cursor_move (0, 0); + tui_mouse_enable (); + return ui; +} + +void +tui_destroy (tui *ui) +{ + /* Set old configuration again and exit. */ + /* If it's not set back the normal configuration of the */ + /* terminal will be broken later! */ + tcsetattr (0, TCSANOW, &ui->term_old_config); + tui_alternate_buffer_disable (); + free (ui); +} + +void +tui_input_sequence_consume (void) +{ + int c, x, y; + c = getchar (); + if (ESC == c) + { + /* TODO: parse escape sequence */ + c = getchar (); + if (c == '[') + { + c = getchar (); + switch (c) + { + case 'M': + /*https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Normal-tracking-mode*/ + /* They add 32 to the coordinates */ + c = getchar () - 32; + x = getchar () - 32; + y = getchar () - 32; + printf ("%d, %d, %d\n", c, x, y); + break; + + case 'I': + /* Gained focus */ + break; + case 'O': + /* Lost focus */ + break; + + default: + break; + } + } + } + else + ; + /* return key event */ +} + +void +tui_loop (tui *ui) +{ + char input_buf [100]; + memset (input_buf, 0, 100); + while (1) + { + tui_input_sequence_consume (); + } +} diff --git a/src/tui.h b/src/tui.h new file mode 100644 index 0000000..b231159 --- /dev/null +++ b/src/tui.h @@ -0,0 +1,31 @@ +/* parc + * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TUI_H +#define TUI_H + +typedef struct _mouse_event mouse_event; +typedef struct _scroll_event scroll_event; +typedef struct _key_event key_event; +typedef struct _tui tui; + +/* TODO: Define the interface with the outside world */ +tui *tui_create (void); +void tui_destroy (tui *ui); +void tui_loop (tui *ui); + +#endif /* TUI_H */ diff --git a/src/ui.c b/src/ui.c deleted file mode 100644 index e4fc35b..0000000 --- a/src/ui.c +++ /dev/null @@ -1,74 +0,0 @@ -/* parc - * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -/* - * XTERM escape sequence reference: - * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html - */ - -#include <stdio.h> - -#define ESC '\x1b' - -const char CSI_LEAD[3] = { ESC, '[', '\0'}; - -void -ui_cursor_move (int x, int y) -{ - /* - * Also valid with CSI<L>;<C>f being <L> and <C> line number and column - * number respectively. In ttys, cursor starts at 1,1 - */ - printf ("%s%d;%dH", CSI_LEAD, y, x); - fflush (stdout); -} - -void -ui_cursor_show (void) -{ - printf ("%s?25h", CSI_LEAD); - fflush (stdout); -} - -void -ui_cursor_hide (void) -{ - printf ("%s?25l", CSI_LEAD); - fflush (stdout); -} - -void -ui_screen_erase (void) -{ - printf ("%s2J", CSI_LEAD); - fflush (stdout); -} - - -void -ui_alternate_buffer_enable (void) -{ - printf ("%s?1049h", CSI_LEAD); - fflush (stdout); -} - -void -ui_alternate_buffer_disable (void) -{ - printf ("%s?1049l", CSI_LEAD); - fflush (stdout); -} @@ -14,7 +14,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -void ui_cursor_move (int x, int y); -void ui_alternate_buffer_enable (void); -void ui_alternate_buffer_disable (void); +#ifndef UI_H +#define UI_H + +/* TODO: Select this with IFDEFS */ +#include "tui.h" +#define ui tui +#define ui_create tui_create +#define ui_destroy tui_destroy +#define ui_loop tui_loop + +#endif /* UI_H */ |