diff options
Diffstat (limited to 'libterm')
-rw-r--r-- | libterm/Makefile | 6 | ||||
-rw-r--r-- | libterm/print_utils.c | 122 | ||||
-rw-r--r-- | libterm/print_utils.h | 13 | ||||
-rw-r--r-- | libterm/screen_modes.c | 3 | ||||
-rw-r--r-- | libterm/screen_modes.h | 15 | ||||
-rw-r--r-- | libterm/term.c | 54 | ||||
-rw-r--r-- | libterm/term.h | 22 |
7 files changed, 235 insertions, 0 deletions
diff --git a/libterm/Makefile b/libterm/Makefile new file mode 100644 index 0000000..a592109 --- /dev/null +++ b/libterm/Makefile @@ -0,0 +1,6 @@ + +make: + gcc -c term.c + gcc -c screen_modes.c + gcc -c print_utils.c + ld -r term.o screen_modes.o print_utils.o -o libterm.o diff --git a/libterm/print_utils.c b/libterm/print_utils.c new file mode 100644 index 0000000..9d4bac0 --- /dev/null +++ b/libterm/print_utils.c @@ -0,0 +1,122 @@ +#include "print_utils.h" + +int term_fprint( screen_mode_e mode, FILE *f ) +{ + int ret=-1; + if (f == NULL) + return -1; + + switch ( mode ) + { + case SCREEN_MODE_80x24: + { + /* + const int m_x=80,m_y=24; + int x=0,y=0; + int fret=1; + char c; + while ((fret = fread(&c,1,1,f)) == 1) + { + if ( c != '\n' ) + { + if ( (x < m_x) && (y < m_y) ) + { + putc( c ); + } + x+=1; + } else if ( c == '\n' ) + { + x = 0; + y += 1; + } + } + */ + } + break; + default: + printf("Unknown screen mode\n"); + } + + return ret; +} + + +//print data to terminal starting from x,y +//return 0x0000yyyy0000xxxx, current stopped position +int term_print( term_screen *ts, const char *buf, size_t size, + int init_x, int init_y ) +{ + int posx=0, posy=0; + int ret=-1; + if ( buf == NULL ) + { + return -1; + } + + if ( size <= 0 ) + { + return -1; + } + + //calculate position + //fix for diff modes also needed + if ( ts->term_col > 80) + { + posx = (ts->term_col-80)/2; + } + if (ts->term_row > 24) + { + posy = (ts->term_row-24)/2; + } + + switch ( ts->mode ) + { + case SCREEN_MODE_80x24: + { + int m_x=80, m_y=24; + int x=init_x, y=init_y; + char c; + int i,j; + + + if (( init_x == 0 ) && (init_y == 0)) + { + for (i=0; i<posx;i++) + printf(" "); + } + + //draw image according to mode + for (i=0; i<size; i++) + { + c = buf[i]; + if ( c == '\n' ) + { + printf("\n"); + //set + for (j=0; j<posx; j++) + printf(" "); + x = 0; + y += 1; + //printf("asdasdsad\n"); + } else if ( c != '\n' ) + { + if ( (x < m_x ) && (y < m_y) ) + { + printf("%c",c); + } + x += 1; + } + } + ret = x&0x0000ffff | ((y&0x0000ffff)<<16); + //printf( "%08x\n", ret ); + } + break; + default: + printf("Unknown mode\n"); + } + + return ret; +} + + + diff --git a/libterm/print_utils.h b/libterm/print_utils.h new file mode 100644 index 0000000..6665262 --- /dev/null +++ b/libterm/print_utils.h @@ -0,0 +1,13 @@ +#ifndef __LIBTERM_PRINT_UTILS_H +#define __LIBTERM_PRINT_UTILS_H + +#include <stdio.h> +#include <stdlib.h> + +#include "screen_modes.h" +#include "term.h" + +int term_fprint( screen_mode_e, FILE* ); +int term_print( term_screen*, const char*, size_t, int, int ); + +#endif diff --git a/libterm/screen_modes.c b/libterm/screen_modes.c new file mode 100644 index 0000000..709b332 --- /dev/null +++ b/libterm/screen_modes.c @@ -0,0 +1,3 @@ +#include "screen_modes.h" + + diff --git a/libterm/screen_modes.h b/libterm/screen_modes.h new file mode 100644 index 0000000..73a66a6 --- /dev/null +++ b/libterm/screen_modes.h @@ -0,0 +1,15 @@ +#ifndef __LIBTERM_SCREEN_MODES_H +#define __LIBTERM_SCREEN_MODES_H + +typedef enum +{ + SCREEN_MODE_NONE=0, + SCREEN_MODE_80x24 +} screen_mode_e; + +typedef struct term_screen_mode +{ + screen_mode_e mode; +} term_screen_mode; + +#endif diff --git a/libterm/term.c b/libterm/term.c new file mode 100644 index 0000000..5d2a44f --- /dev/null +++ b/libterm/term.c @@ -0,0 +1,54 @@ +#include "term.h" + +int term_init_data( term_screen *term ) +{ + int ret=0; + + term->term_col = term_get_col(); + if (term->term_col == -1) ret = -1; + + term->term_row = term_get_row(); + if (term->term_row == -1) ret = -1; + + term->mode = SCREEN_MODE_80x24; + + return ret; +} + + +int term_get_col( ) +{ + int ret=-1; + int fret=-1; + + struct winsize w; + + + fret = ioctl(0, TIOCGWINSZ, &w ); + if ( fret == 0 ) + { + return w.ws_col; + } + + + return -1; +} + + +int term_get_row( ) +{ + int ret=-1; + int fret=-1; + + struct winsize w; + + + fret = ioctl(0, TIOCGWINSZ, &w ); + if ( fret == 0 ) + { + return w.ws_row; + } + + return -1; +} + diff --git a/libterm/term.h b/libterm/term.h new file mode 100644 index 0000000..4565571 --- /dev/null +++ b/libterm/term.h @@ -0,0 +1,22 @@ +#ifndef __LIBTERM_TERM_H +#define __LIBTERM_TERM_H + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <sys/ioctl.h> + +#include "screen_modes.h" + +typedef struct term_screen +{ + 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( ); + +#endif |