summaryrefslogtreecommitdiff
path: root/libterm/term.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-01-07 21:00:59 +0900
committerFreeArtMan <dos21h@gmail.com>2015-01-07 21:00:59 +0900
commit03e459e7dff84c44644b1eccc0e00b73d846fe2a (patch)
tree00269e56bdf53906396199e22dcabe8bbee9592f /libterm/term.c
parent0ab31d63699467d72f09327171b735ed2152bf2c (diff)
downloadmicrobbs-03e459e7dff84c44644b1eccc0e00b73d846fe2a.tar.gz
microbbs-03e459e7dff84c44644b1eccc0e00b73d846fe2a.zip
Small fixes about loging and libterm
Diffstat (limited to 'libterm/term.c')
-rw-r--r--libterm/term.c114
1 files changed, 87 insertions, 27 deletions
diff --git a/libterm/term.c b/libterm/term.c
index 9a4e231..1aa7afa 100644
--- a/libterm/term.c
+++ b/libterm/term.c
@@ -26,26 +26,33 @@ enum KEY_ACTION{
#define T_ESC "\x1b"
+//setup initial terminal stuff
+//this coulc act differently, becouse allways there is
+//different terminal setting that should be default
int term_init( term_screen *term )
{
int ret=0;
- if ( !isatty(STDIN_FILENO) ) goto exit_error;
+ memset( term, 0, sizeof( term_screen ));
+
+ if ( !isatty(STDIN_FILENO) )
+ {
+ ERROR("isatty failed\n")
+ goto exit_error;
+ }
//!!!!!!
//register atexit
term->ifd = STDIN_FILENO;
term->ofd = STDOUT_FILENO;
- if (term_set_raw_mode( term ) == -1 ) goto exit_error;
+ //if you whant raw mode then you should set it man
+ if ( tcgetattr( term->ifd, &term->orig_i ) == -1 ) goto exit_error;
+ term->raw_i = term->orig_i;
- term->term_col = term_get_maxcol( term );//not good
- if (term->term_col == -1) ret = -1;
+ //if (term_set_raw_mode( term ) == -1 ) goto exit_error;
- term->term_row = term_get_maxrow( term );//not good
- if (term->term_row == -1) ret = -1;
-
- term->mode = SCREEN_MODE_80x24;
+ term->mode = SCREEN_MODE_80x24;
return ret;
@@ -54,7 +61,9 @@ exit_error:
return -1;
}
-
+//get maximal number of columns setting up cursor to 999 column
+//and getting on with place terminal have placed cursor and getting
+//column on with terminal putted cursor
int term_get_maxcol( term_screen *ts )
{
int ret=-1;
@@ -87,7 +96,8 @@ exit_error:
}
-
+//try to setup far away line after that read position where
+//terminal have putted cursor and read line of that postion
int term_get_maxrow( term_screen *ts )
{
int ret=-1;
@@ -123,6 +133,8 @@ exit_error:
return -1;
}
+
+
int term_cur_pos( term_screen *ts )
{
int ret=-1;
@@ -182,16 +194,56 @@ exit_error:
return -1;
}
-
-int term_cur_set_c( term_screen *ts )
+//set cursor column position
+int term_cur_set_c( term_screen *ts, unsigned int pc )
{
+ int ret = 0;
+ int cur_r;
-}
+ /* go to right marging and get position */
+ if ( (cur_r = term_cur_pos_r( ts )) == -1 )
+ goto exit_error;
+ /* set position */
+ {
+ char buf[32];
+ int l;
+
+ snprintf( buf, 32, T_ESC "[%d;%dH", cur_r, pc);
+ l = strlen( buf );
+ if ( write( ts->ofd, buf, l ) != l)
+ goto exit_error;
+ }
+
+ return ret;
+exit_error:
+ return -1;
+}
-int term_cur_set_r( term_screen *ts )
+//set cursor row/line position
+int term_cur_set_r( term_screen *ts, unsigned int pr )
{
+ int ret = 0;
+ int cur_c;
+
+ /* go to right marging and get position */
+ if ( (cur_c = term_cur_pos_c( ts )) == -1 )
+ goto exit_error;
+ /* set position */
+ {
+ char buf[32];
+ int l;
+
+ snprintf( buf, 32, T_ESC "[%d;%dH", pr, cur_c);
+ l = strlen( buf );
+ if ( write( ts->ofd, buf, l ) != l)
+ goto exit_error;
+ }
+
+ return ret;
+exit_error:
+ return -1;
}
int term_set_speed( term_screen *ts )
@@ -200,6 +252,8 @@ int term_set_speed( term_screen *ts )
return ret;
}
+
+//clean terminal with escape command
int term_clr_scr( term_screen *ts )
{
int ret = 0;
@@ -209,33 +263,39 @@ int term_clr_scr( term_screen *ts )
return ret;
}
-
+//set terminal default input/output behavior
int term_set_raw_mode( term_screen *ts )
{
int ret = 0;
- if ( tcgetattr( ts->ifd, &ts->orig ) == -1 ) goto exit_error;
-
+ if ( tcgetattr( ts->ifd, &ts->orig_i ) == -1 )
+ {
+ ERROR("Cannot get input terminal attributes\n");
+ goto exit_error;
+ }
- ts->raw = ts->orig; /* modify the original mode */
+ ts->raw_i = ts->orig_i; /* 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);
+ ts->raw_i.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/* output modes - disable post raw */
- ts->raw.c_oflag &= ~(OPOST);
+ ts->raw_i.c_oflag &= ~(OPOST);
/* control modes - set 8 bit chars */
- ts->raw.c_cflag |= (CS8);
+ ts->raw_i.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);
+ ts->raw_i.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 */
+ ts->raw_i.c_cc[VMIN] = 1;
+ ts->raw_i.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;
-
+ if (tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i) < 0)
+ {
+ ERROR("Cannot set new terminal input attribures\n");
+ goto exit_error;
+ }
return ret;
@@ -249,5 +309,5 @@ exit_error:
void term_set_orig_mode( term_screen *ts )
{
- tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig );
+ tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig_i );
} \ No newline at end of file