diff options
author | FreeArtMan <dos21h@gmail.com> | 2016-05-23 21:51:44 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2016-05-23 21:51:44 +0100 |
commit | cb141140579d796ae5cafe6da52b4f0b87be6a84 (patch) | |
tree | 61e0130a1581e1ba99dabed32c82782699db064c /libterm/term_io.c | |
parent | 4baa01f8cd595cb0517d8f7f665d5be92528b6c2 (diff) | |
download | ihe-cb141140579d796ae5cafe6da52b4f0b87be6a84.tar.gz ihe-cb141140579d796ae5cafe6da52b4f0b87be6a84.zip |
Simplifying logic. Removing buffer features
Diffstat (limited to 'libterm/term_io.c')
-rw-r--r-- | libterm/term_io.c | 336 |
1 files changed, 336 insertions, 0 deletions
diff --git a/libterm/term_io.c b/libterm/term_io.c new file mode 100644 index 0000000..a1f157c --- /dev/null +++ b/libterm/term_io.c @@ -0,0 +1,336 @@ +#include "term_io.h" + +int term_fprint( screen_mode_e mode, FILE *f ) +{ + int ret=-1; + if (f == NULL) + return -1; + + ERROR("\n"); + switch ( mode ) + { + case SCREEN_MODE_80x25: + { + } + break; + default: + printf("Unknown screen mode\n"); + } + + return ret; +} + +int term_print( term_screen *ts, const char *s, size_t n) +{ + int ret=0; + + if ( ts == NULL ) + return -1; + + if ( s == NULL ) + return -1; + + if ( n < 1 ) + return -1; + + if ( n > 80*25 ) + return -1; + + ret = write( ts->ofd, s, n ); + + return ret; +} + +//print data to terminal starting from x,y +int term_print_xy( term_screen *ts, const char *buf, size_t size, + int init_column, int init_row ) +{ + int ret=-1; + if ( buf == NULL ) + { + return -1; + } + + if ( size <= 0 ) + { + return -1; + } + + switch ( ts->mode ) + { + case SCREEN_MODE_80x25: + { + + } + break; + default: + printf("Unknown mode\n"); + } + + return ret; +} + +int term_draw_hline( term_screen *ts, int pc, int pr, const int sz, char ch) +{ + int ret=0; + char buf[sz]; memset( buf, ch, sz ); + + if ( sz > 0) + { + term_cur_set_c( ts, pc ); + term_cur_set_r( ts, pr ); + write( ts->ofd, buf, sz ); + } else + { + ret = -1; + } + + return ret; +} + +//read one character from stream +int term_getc( term_screen *ts ) +{ + int ret=-1; + int fret=-1; + char buf; + + fret = read( ts->ifd, &buf, 1 ); + if ( fret == 1 ) + { + ret = buf; + } + return ret; +} + +int64_t term_getb( term_screen *ts ) +{ + int64_t ret = -1; + int fret = -1; + unsigned char buf[8]; + + fret = read( ts->ifd, buf, 8 ); + if ( fret == 1 ) + { + //printf("1\n"); + + ret = buf[0]; + } else if ( fret == 2 ) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t r = 0x0; + + r = (a1+((a2<<8)&0xff)); + + ret = r; + + } else if ( fret == 3 ) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t a3 = buf[2]&0xff; + uint32_t r=0x0; + + //printf("3\n"); + + r = (a1+((a2<<8)&0xFF00)+((a3<<16)&0xFF0000)); + + ret = r; + } else if ( fret == 4) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t a3 = buf[2]&0xff; + uint32_t a4 = buf[3]&0xff; + uint32_t r=0x0; + + //printf("4\n"); + + r = (a1+((a2<<8)&0xFF00)+((a3<<16)&0xFF0000)+((a4<<24)&(0xFF000000))); + + ret = r; + } else if ( fret == 5 ) + { + uint64_t a1 = buf[0]&0xff; + uint64_t a2 = buf[1]&0xff; + uint64_t a3 = buf[2]&0xff; + uint64_t a4 = buf[3]&0xff; + uint64_t a5 = buf[4]&0xff; + uint64_t r=0x0; + + //printf("5 %02x %02x %02x %02x %02x\n",a1,a2,a3,a4,a5); + + r = (a1+ + ((a2<<8)&0xFF00)+ + ((a3<<16)&0xFF0000)+ + ((a4<<24)&0xFF000000)+ + ((a5<<32)&0xFF00000000)); + + ret = r; + } else + { + int i; + for (i=0;i<fret;i++) + printf("!%02x",buf[fret-i-1]); + printf(" ![%d]",fret); + ret = 0; + } + + + return ret; +} + +int term_putc( term_screen *ts, char c ) +{ + int ret = 0; + int fret = -1; + + fret = write( ts->ofd, &c, 1 ); + if ( fret != 1 ) + { + ret = -1; + } + + return ret; +} + +//return amoutn fo characters readed +//on error or if nothing is imputed 0 returned +int term_readline( term_screen *ts, char *str, size_t str_size, int flag ) +{ + int ret = 0; + int max_row, max_column; + char *buf = NULL; + int buf_size = str_size-1; + int buf_curent = 0; + int menu_input=0; + char menu_cmd = 0; + int orig_row; + int orig_col; + + buf = malloc( buf_size ); + memset( buf, 0, buf_size ); + + max_row = term_get_maxrow( ts ); + max_column = term_get_maxcol( ts ); + orig_row = term_cur_get_r( ts ); + orig_col = term_cur_get_c( ts ); + + menu_cmd = 0; + buf_curent = 0; + //finsih when escape button is setted + while ( menu_cmd != TK_ESC ) + { + menu_input = term_getc( ts ); + if ( menu_input != -1 ) + { + menu_cmd = (char)menu_input; + //add to buffer any printable alpahnumeric char + if ( isalpha( menu_cmd ) && + (flag == READLINE_TEXT || flag == READLINE_ALPHA + || flag == READLINE_HIDDEN) ) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //add to buffer number 0-9 + } else if ( isdigit( menu_cmd ) && + ( flag == READLINE_TEXT || flag == READLINE_NUMBER + || flag == READLINE_HIDDEN )) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //if space allowed + } else if ( (menu_cmd == ' ') && (flag == READLINE_TEXT) ) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //remaining printable chars for READLINE_TEXT + } else if ( ispunct( menu_cmd ) && + (flag == READLINE_TEXT )) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //deleteone char from buffer with backspace + } else if ( menu_cmd == TK_BACKSPACE ) + { + if ( buf_curent > 0) + { + buf[ buf_curent ] = 0x0; + buf_curent -= 1; + } + //input ready lets finish input + } else if ( menu_cmd == TK_ENTER ) + { + ret = buf_curent; + memcpy( str, buf, buf_size ); + str[ str_size-1 ] = 0x0; + break; + //finsih input without saving result + } else if ( menu_cmd == TK_ESC ) + { + ret = -1; + } + + { + int i; + term_cur_set_r( ts, orig_row ); + term_cur_set_c( ts, orig_col ); + for (i=0;i<buf_size;i++) + { + if ( i < buf_curent ) + { + if ( (flag == READLINE_TEXT) || (flag == READLINE_ALPHA) + || (flag == READLINE_NUMBER) ) + { + term_putc( ts, buf[i] ); + } else if ( flag == READLINE_HIDDEN ) + { + term_putc( ts, '*'); + } + } else + { + term_putc( ts, ' ' ); + } + } + term_cur_set_c( ts, orig_col+buf_curent ); + term_cur_set_r( ts, orig_row ); + } + } + } + + if ( buf != NULL) + { + free( buf ); + buf = NULL; + } + + return ret; +} + +//this is to replace printf,flush code +int term_printf( term_screen *ts, const char *format, ...) +{ + int ret=0; + + va_list args; + va_start(args, format); + + vdprintf(ts->ofd, format, args); + + va_end(args); + + return ret; +} |