summaryrefslogtreecommitdiff
path: root/libterm
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
parent0ab31d63699467d72f09327171b735ed2152bf2c (diff)
downloadmicrobbs-03e459e7dff84c44644b1eccc0e00b73d846fe2a.tar.gz
microbbs-03e459e7dff84c44644b1eccc0e00b73d846fe2a.zip
Small fixes about loging and libterm
Diffstat (limited to 'libterm')
-rw-r--r--libterm/examples/Makefile8
-rw-r--r--libterm/examples/detect_resize.c63
-rw-r--r--libterm/examples/detect_screen_size.c39
-rw-r--r--libterm/examples/filtered_input.c63
-rw-r--r--libterm/examples/invisible_input.c67
-rw-r--r--libterm/examples/restore_screen.c24
-rw-r--r--libterm/term.c114
-rw-r--r--libterm/term.h8
8 files changed, 355 insertions, 31 deletions
diff --git a/libterm/examples/Makefile b/libterm/examples/Makefile
new file mode 100644
index 0000000..64009bd
--- /dev/null
+++ b/libterm/examples/Makefile
@@ -0,0 +1,8 @@
+CC=gcc
+
+make:
+ $(CC) detect_screen_size.c -o detect_screen_size ../libterm.o -I../
+ $(CC) detect_resize.c -o detect_resize ../libterm.o -I../
+ $(CC) filtered_input.c -o filtered_input ../libterm.o -I../
+ $(CC) invisible_input.c -o invisisble_input ../libterm.o -I../
+ $(CC) restore_screen.c -o restore_screen ../libterm.o -I../ \ No newline at end of file
diff --git a/libterm/examples/detect_resize.c b/libterm/examples/detect_resize.c
new file mode 100644
index 0000000..27c88c6
--- /dev/null
+++ b/libterm/examples/detect_resize.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+
+int main()
+{
+ int c;
+ int max_r, max_c;
+
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ term_clr_scr( &ts );
+
+ {
+ char buf[32];
+ int c;
+ c = term_get_maxcol( &ts );
+ snprintf( buf, 32, "MaxCol:%d\n", c );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+
+ {
+ char buf[32];
+ int r;
+ r = term_get_maxrow( &ts );
+ snprintf( buf, 32, "MaxRow:%d\n", r );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+
+ while ( (c = getchar() ) != 'q' )
+ {
+ term_clr_scr( &ts );
+
+ {
+ char buf[32];
+ max_c = term_get_maxcol( &ts );
+ snprintf( buf, 32, "MaxCol:%d\n", max_c );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+
+ {
+ char buf[32];
+ max_r = term_get_maxrow( &ts );
+ snprintf( buf, 32, "MaxRow:%d\n", max_r );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+ {
+ term_cur_set_r( &ts, max_r );
+ term_cur_set_c( &ts, max_c );
+ printf("+");
+ }
+ }
+
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
+ return 0;
+} \ No newline at end of file
diff --git a/libterm/examples/detect_screen_size.c b/libterm/examples/detect_screen_size.c
new file mode 100644
index 0000000..f309253
--- /dev/null
+++ b/libterm/examples/detect_screen_size.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+
+int main()
+{
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ term_clr_scr( &ts );
+
+ {
+ char buf[32];
+ int c;
+ c = term_get_maxcol( &ts );
+ snprintf( buf, 32, "MaxCol:%d\n", c );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+
+ {
+ char buf[32];
+ int r;
+ r = term_get_maxrow( &ts );
+ snprintf( buf, 32, "MaxRow:%d\n", r );
+ write( ts.ofd, buf, strlen(buf) );
+ }
+
+
+ sleep(3);
+
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
+ return 0;
+} \ No newline at end of file
diff --git a/libterm/examples/filtered_input.c b/libterm/examples/filtered_input.c
new file mode 100644
index 0000000..170e474
--- /dev/null
+++ b/libterm/examples/filtered_input.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+
+int main()
+{
+ int c;
+ int new_c=0, new_r=0, old_r=0, old_c=0;
+ const int in_size = 32;
+ int counter=0;
+ char in_buf[in_size];
+
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ term_clr_scr( &ts );
+
+ new_c = term_get_maxcol( &ts );
+ new_r = term_get_maxrow( &ts );
+ old_r = new_r;
+ old_c = new_c;
+
+ memset( in_buf, 0, in_size );
+
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, old_r);
+ printf("Your name is?: %s", in_buf);
+ while ( (c = getchar() ) != 13 )
+ {
+ if ( isalpha(c) )
+ {
+ if ( counter < in_size-1 )
+ {
+ in_buf[counter] = c;
+ counter++;
+ }
+ } else if ( c == 127 )
+ {
+ if ( counter > 0)
+ {
+ in_buf[counter-1] = 0x0;
+ counter--;
+ }
+ } else
+ printf("%d",c);
+ term_clr_scr( &ts );
+ new_r = term_get_maxrow( &ts );
+ if ( old_r != new_r ) old_r = new_r;
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, old_r);
+ printf("Your name is?: %s", in_buf);
+
+ }
+
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
+ return 0;
+} \ No newline at end of file
diff --git a/libterm/examples/invisible_input.c b/libterm/examples/invisible_input.c
new file mode 100644
index 0000000..5fadfb3
--- /dev/null
+++ b/libterm/examples/invisible_input.c
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+
+int main()
+{
+ int c;
+ int new_c=0, new_r=0, old_r=0, old_c=0;
+ const int in_size = 32;
+ int counter=0;
+ int i=0;
+ char in_buf[in_size];
+
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ term_clr_scr( &ts );
+
+ new_c = term_get_maxcol( &ts );
+ new_r = term_get_maxrow( &ts );
+ old_r = new_r;
+ old_c = new_c;
+
+ memset( in_buf, 0, in_size );
+
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, old_r);
+ printf("Your name is?: %s", in_buf);
+ while ( (c = getchar() ) != 13 )
+ {
+ if ( isalpha(c) )
+ {
+ if ( counter < in_size-1 )
+ {
+ in_buf[counter] = c;
+ counter++;
+ }
+ } else if ( c == 127 )
+ {
+ if ( counter > 0)
+ {
+ in_buf[counter-1] = 0x0;
+ counter--;
+ }
+ } else
+ printf("%d",c);
+ term_clr_scr( &ts );
+ new_r = term_get_maxrow( &ts );
+ if ( old_r != new_r ) old_r = new_r;
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, old_r);
+ printf("Your name is?: ");
+ for ( i=0; i<counter;i++) printf("*");
+
+ }
+
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
+ printf("Hidden input was: %s\n", in_buf );
+
+ return 0;
+} \ No newline at end of file
diff --git a/libterm/examples/restore_screen.c b/libterm/examples/restore_screen.c
new file mode 100644
index 0000000..5323d78
--- /dev/null
+++ b/libterm/examples/restore_screen.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+#include <debug.h>
+
+int main()
+{
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+
+ PRINT("asd\n");
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+ PRINT("asd\n");
+
+ printf("set mode\n");
+
+ sleep(1);
+
+ term_set_orig_mode( &ts );
+
+ return 0;
+} \ No newline at end of file
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
diff --git a/libterm/term.h b/libterm/term.h
index 010ed1b..b290a43 100644
--- a/libterm/term.h
+++ b/libterm/term.h
@@ -19,8 +19,8 @@
typedef struct term_screen
{
int ifd, ofd;
- struct termios orig;
- struct termios raw;
+ struct termios orig_i, orig_o;
+ struct termios raw_i, raw_o;
screen_mode_e mode;
int term_col, term_row;
} term_screen;
@@ -31,8 +31,8 @@ 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_cur_set_c( term_screen*, unsigned int );
+int term_cur_set_r( term_screen*, unsigned int );
int term_set_speed( term_screen* );
int term_clr_scr( term_screen* );
int term_set_raw_mode( term_screen* );