summaryrefslogtreecommitdiff
path: root/libterm
diff options
context:
space:
mode:
Diffstat (limited to 'libterm')
-rw-r--r--libterm/debug.h69
-rw-r--r--libterm/term.c233
-rw-r--r--libterm/term.h27
3 files changed, 308 insertions, 21 deletions
diff --git a/libterm/debug.h b/libterm/debug.h
new file mode 100644
index 0000000..a8bf211
--- /dev/null
+++ b/libterm/debug.h
@@ -0,0 +1,69 @@
+#ifndef __RB_DEBUG_UTILS_H
+#define __RB_DEBUG_UTILS_H
+
+//what about kprintf?
+
+//config options
+#define PRINTF printf
+#define COLORIZE
+#define PRINT_LINENUM
+#define PRINT_FILENAME
+#define PRINT_DEBUG
+
+
+//use color
+#ifdef COLORIZE
+ #define D_COLOR "1;32m"
+ #define D_COLOR_S "\033[" D_COLOR
+ #define D_COLOR_E "\033[0m"
+ #define E_COLOR "1;31m"
+ #define E_COLOR_S "\033[" E_COLOR
+ #define E_COLOR_E "\033[0m"
+#else
+ #define D_COLOR
+ #define D_COLOR_S
+ #define D_COLOR_E
+ #define E_COLOR
+ #define E_COLOR_S
+ #define E_COLOR_E
+#endif
+
+//print debug line
+#ifdef PRINT_LINENUM
+ #define PRINT_LINE_F "LINE:%d "
+ #define PRINT_LINE_D __LINE__
+#else
+ #define PRINT_LINE_F ""
+ #define PRINT_LINE_D ""
+#endif
+
+//print
+#ifdef PRINT_FILENAME
+ #define PRINT_FILE_F "FILE:%s "
+ #define PRINT_FILE_D __FILE__
+#else
+ #define PRINT_FILE_F ""
+ #define PRINT_FILE_D ""
+#endif
+
+//print debug string
+#ifdef PRINT_DEBUG
+ #define PRINT_DEBUG_F "Debug: "
+#else
+ #define PRINT_DEBUG_F ""
+#endif
+
+#define PRINT( format, args ... ) PRINTF( D_COLOR_S PRINT_DEBUG_F \
+ PRINT_FILE_F PRINT_LINE_F format D_COLOR_E, PRINT_FILE_D, \
+ PRINT_LINE_D, ##args);
+
+#define ERROR( format, args ... ) PRINTF( E_COLOR_S PRINT_DEBUG_F \
+ PRINT_FILE_F PRINT_LINE_F format E_COLOR_E, PRINT_FILE_D, \
+ PRINT_LINE_D, ##args);
+
+#define PNL() PRINT("\n");
+
+#define ENL() ERROR("\n");
+
+
+#endif
diff --git a/libterm/term.c b/libterm/term.c
index 5d2a44f..9a4e231 100644
--- a/libterm/term.c
+++ b/libterm/term.c
@@ -1,54 +1,253 @@
#include "term.h"
-int term_init_data( term_screen *term )
+#include "debug.h"
+
+enum KEY_ACTION{
+ KEY_NULL = 0, /* NULL */
+ CTRL_A = 1, /* Ctrl+a */
+ CTRL_B = 2, /* Ctrl-b */
+ CTRL_C = 3, /* Ctrl-c */
+ CTRL_D = 4, /* Ctrl-d */
+ CTRL_E = 5, /* Ctrl-e */
+ CTRL_F = 6, /* Ctrl-f */
+ CTRL_H = 8, /* Ctrl-h */
+ TAB = 9, /* Tab */
+ CTRL_K = 11, /* Ctrl+k */
+ CTRL_L = 12, /* Ctrl+l */
+ ENTER = 13, /* Enter */
+ CTRL_N = 14, /* Ctrl-n */
+ CTRL_P = 16, /* Ctrl-p */
+ CTRL_T = 20, /* Ctrl-t */
+ CTRL_U = 21, /* Ctrl+u */
+ CTRL_W = 23, /* Ctrl+w */
+ ESC = 27, /* Escape */
+ BACKSPACE = 127 /* Backspace */
+};
+
+#define T_ESC "\x1b"
+
+int term_init( term_screen *term )
{
int ret=0;
- term->term_col = term_get_col();
+ if ( !isatty(STDIN_FILENO) ) goto exit_error;
+ //!!!!!!
+ //register atexit
+
+ term->ifd = STDIN_FILENO;
+ term->ofd = STDOUT_FILENO;
+
+ if (term_set_raw_mode( term ) == -1 ) goto exit_error;
+
+ term->term_col = term_get_maxcol( term );//not good
if (term->term_col == -1) ret = -1;
- term->term_row = term_get_row();
+ term->term_row = term_get_maxrow( term );//not good
if (term->term_row == -1) ret = -1;
term->mode = SCREEN_MODE_80x24;
return ret;
+
+exit_error:
+ errno = ENOTTY;
+ return -1;
}
-int term_get_col( )
+int term_get_maxcol( term_screen *ts )
{
int ret=-1;
- int fret=-1;
+ int orig_c;
+ int cur_c;
+
+ /* get initial cursor position */
+ if ( (orig_c = term_cur_pos_c( ts )) == -1 )
+ goto exit_error;
+
+
+ /* go to right marging and get position */
+ if ( write( ts->ofd, T_ESC "[999C", 6 ) != 6 )
+ goto exit_error;
+ if ( (cur_c = term_cur_pos_c( ts )) == -1 )
+ goto exit_error;
+ ret = cur_c;
+
+ /* restore position */
+ {
+ char buf[32];
+ snprintf( buf, 32, T_ESC "[" "%d" "D", cur_c-orig_c);
+ write( ts->ofd, buf, strlen(buf) );
+ }
+
+ return ret;
+
+exit_error:
+ return -1;
+
+}
+
+
+int term_get_maxrow( term_screen *ts )
+{
+ int ret=-1;
+ int orig_r;
+ int cur_r;
+
+ /* get initial cursor position */
+ if ( (orig_r = term_cur_pos_r( ts )) == -1 )
+ goto exit_error;
+
+
+ /* go to right marging and get position */
+ if ( write( ts->ofd, T_ESC "[999B", 6 ) != 6 )
+ goto exit_error;
+ if ( (cur_r = term_cur_pos_r( ts )) == -1 )
+ goto exit_error;
+ ret = cur_r;
+
+ //PNL();
+ /* restore position */
+ {
+ char buf[32];
+ snprintf( buf, 32, T_ESC "[" "%d" "A", cur_r-orig_r);
+ write( ts->ofd, buf, strlen(buf) );
+ }
+
+ //PNL();
+
+ return ret;
+
+exit_error:
+ //PNL();
+ return -1;
+}
+
+int term_cur_pos( term_screen *ts )
+{
+ int ret=-1;
+ return ret;
+}
- struct winsize w;
+int term_cur_pos_c( term_screen *ts )
+{
+ unsigned int i;
+ int row, col;
+ char buf[32];
+ if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error;
- fret = ioctl(0, TIOCGWINSZ, &w );
- if ( fret == 0 )
+ i = 0;
+ while (i < sizeof(buf)-1)
{
- return w.ws_col;
+ if ( read( ts->ifd, buf+i,1 ) != 1 ) break;
+ if (buf[i] == 'R') break;
+ i++;
}
+ buf[i] = '\0';
+ /* Parse terminal response */
+ if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error;
+ if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error;
+ return col;
+exit_error:
return -1;
}
-int term_get_row( )
+int term_cur_pos_r( term_screen *ts )
{
- int ret=-1;
- int fret=-1;
-
- struct winsize w;
+ unsigned int i;
+ int row, col;
+ char buf[32];
+ if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error;
- fret = ioctl(0, TIOCGWINSZ, &w );
- if ( fret == 0 )
+ i = 0;
+ while (i < sizeof(buf)-1)
{
- return w.ws_row;
+ if ( read( ts->ifd, buf+i,1 ) != 1 ) break;
+ if (buf[i] == 'R') break;
+ i++;
}
+ buf[i] = '\0';
+ /* Parse terminal response */
+ if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error;
+ if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error;
+
+ return row;
+exit_error:
return -1;
}
+
+int term_cur_set_c( term_screen *ts )
+{
+
+}
+
+
+int term_cur_set_r( term_screen *ts )
+{
+
+}
+
+int term_set_speed( term_screen *ts )
+{
+ int ret = -1;
+ return ret;
+}
+
+int term_clr_scr( term_screen *ts )
+{
+ int ret = 0;
+
+ if ( write( ts->ofd, T_ESC "[H" T_ESC "[2J", 7 ) <= 0 );;
+
+ return ret;
+}
+
+
+int term_set_raw_mode( term_screen *ts )
+{
+ int ret = 0;
+
+ if ( tcgetattr( ts->ifd, &ts->orig ) == -1 ) goto exit_error;
+
+
+ ts->raw = ts->orig; /* modify the original mode */
+ /* input modes: no break, no CR to NL, no parity check, no strip char,
+ * no start/stop output control. */
+ ts->raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+ /* output modes - disable post raw */
+ ts->raw.c_oflag &= ~(OPOST);
+ /* control modes - set 8 bit chars */
+ ts->raw.c_cflag |= (CS8);
+ /* local modes - choing off, canonical off, no extended functions,
+ * no signal chars (^Z,^C) */
+ ts->raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
+ /* control chars - set return condition: min number of bytes and timer.
+ * We want read to return every single byte, without timeout. */
+ ts->raw.c_cc[VMIN] = 1;
+ ts->raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
+
+ /* put terminal in raw mode after flushing */
+ if (tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw) < 0) goto exit_error;
+
+
+ return ret;
+
+exit_error:
+ errno = ENOTTY;
+ return -1;
+
+}
+
+
+
+void term_set_orig_mode( term_screen *ts )
+{
+ tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig );
+} \ No newline at end of file
diff --git a/libterm/term.h b/libterm/term.h
index 4565571..010ed1b 100644
--- a/libterm/term.h
+++ b/libterm/term.h
@@ -1,22 +1,41 @@
#ifndef __LIBTERM_TERM_H
#define __LIBTERM_TERM_H
+#include <termios.h>
+#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
+#include <errno.h>
#include <string.h>
-
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/types.h>
#include <sys/ioctl.h>
+#include <unistd.h>
#include "screen_modes.h"
+
typedef struct term_screen
{
+ int ifd, ofd;
+ struct termios orig;
+ struct termios raw;
screen_mode_e mode;
int term_col, term_row;
} term_screen;
-int term_init_data( term_screen* );
-int term_get_col( );
-int term_get_row( );
+int term_init( term_screen* );
+int term_get_maxcol( term_screen* );
+int term_get_maxrow( term_screen* );
+int term_cur_pos( term_screen* );
+int term_cur_pos_c( term_screen* );
+int term_cur_pos_r( term_screen* );
+int term_cur_set_c( term_screen* );
+int term_cur_set_r( term_screen* );
+int term_set_speed( term_screen* );
+int term_clr_scr( term_screen* );
+int term_set_raw_mode( term_screen* );
+void term_set_orig_mode( term_screen* );
#endif