From: hackbard Date: Fri, 7 May 2004 11:43:24 +0000 (+0000) Subject: added ncurses based display system (minimal and still full of bugs) :o X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=da2c28e700faec39bab322b103fe0298c52fbda7;p=my-code%2Fivac.git added ncurses based display system (minimal and still full of bugs) :o --- diff --git a/src/Makefile b/src/Makefile index 0acc428..396ebc8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,9 +3,9 @@ INCLUDEDIR = /usr/include CFLAGS = -DDEBUG -O3 -Wall -LIBS = +LIBS = -lncurses -OBJS = network.o event.o input.o +OBJS = network.o event.o input.o display.o ivac: $(OBJS) $(CC) -o $@ $(OBJS) ivac.c $(LIBS) diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..9390fbc --- /dev/null +++ b/src/display.c @@ -0,0 +1,37 @@ +/* display.c -- display management stuff + * + * hackbard@hackdaworld.dyndns.org + * + */ + +#include "display.h" + +int display_init(t_display *display) { + + puts("[display] initializing display ..."); + + initscr(); + raw(); + noecho(); + keypad(stdscr,TRUE); + getmaxyx(stdscr,display->max_y,display->max_x); + + return D_SUCCESS; +} + +int display_refresh(t_display *display) { + + getmaxyx(stdscr,display->max_y,display->max_x); + + return D_SUCCESS; +} + +int display_shutdown(t_display *display) { + + noraw(); + echo(); + + puts("[display] shutdown"); + + return D_SUCCESS; +} diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..7ec779e --- /dev/null +++ b/src/display.h @@ -0,0 +1,22 @@ +/* display.h -- display headers */ + +/* includes */ +#include +#include + +/* defines */ +#define D_SUCCESS 1 +#define D_ERROR -1 + +/* display specific variables */ +typedef struct s_display { + int max_x; + int max_y; + int x; + int y; +} t_display; + +/* function prototypes */ +int display_init(t_display *display); +int display_refresh(t_display *display); +int display_shutdown(t_display *display); diff --git a/src/ivac.c b/src/ivac.c index 70d1a35..82b9a7c 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -31,6 +31,7 @@ int main(int argc,char **argv) { /* set username (futur: read from config or entered later) */ strcpy(ivac.username,"hackbard"); + memset(&ivac.console[0][0],0,IVAC_CONSOLE_LEN*IVAC_CONSOLE_STRING_LEN); /* set capabilities (futur: set by check routines) */ ivac.g_cap=NETWORK; @@ -62,8 +63,11 @@ int main(int argc,char **argv) { event_math(ivac.net.l_fd,&(ivac.event),READ,ADD); event_math(0,&(ivac.event),READ,ADD); + /* display init */ + display_init(&(ivac.display)); + /* display */ - ivac_display(&(ivac)); + ivac_display(&(ivac.display)); /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */ event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb); @@ -78,6 +82,7 @@ int ivac_shutdown(t_ivac *ivac) { network_shutdown(&(ivac->net)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); + display_shutdown(&(ivac->display)); return SUCCESS; } @@ -177,7 +182,7 @@ int ivac_event_cb(t_event *event,void *ptr) { input_get_event(&(ivac->input),ivac_parse_command,ivac); /* display ivac gui */ - ivac_display(ivac); + ivac_display_content(ivac); return SUCCESS; } @@ -192,74 +197,183 @@ int ivac_regular_cb(t_event *event,void *ptr) { int ivac_parse_command(t_input *input,void *ptr) { t_ivac *ivac; - int channel; + int channel,i; + int len; + char *data,valid=0; + char c_str[IVAC_CONSOLE_STRING_LEN]; ivac=(t_ivac *)ptr; + data=input->content; + i=input->c_count-1; + memset(c_str,0,IVAC_CONSOLE_STRING_LEN); /* parse command routines */ - if(input->content[input->c_count-1]=='\n') { + if((data[i]=='\r') || (data[i]=='\n')) { + + /* parse commands */ + switch(data[0]) { + case '/': + len=strlen(data+1); + if(len==4) { + /* len 4 commands */ + if(!strncmp(data+1,"quit",4)) { + valid=1; + sprintf(c_str,"ivac shutdown\n"); + ivac_add_to_monitor(ivac,c_str); + ivac_shutdown(ivac); + } + } + break; + default: + break; + } + + if(!valid) { + snprintf(c_str,IVAC_CONSOLE_STRING_LEN-1,"unknown command: '%s...'\n", + data); + ivac_add_to_monitor(ivac,c_str); + } + /* delete content buffer + reset counter */ memset(input->content,0,input->c_count); input->c_count=0; + } return SUCCESS; } -int ivac_display_head(void) { +int ivac_display_head(t_display *display) { + + int x,y; + + move(0,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(1,0,"##"); + mvaddstr(1,(display->max_x-4)/2-4,"- ivac -"); + mvaddstr(1,(display->max_x-2),"##"); + move(2,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); + + return SUCCESS; +} + +int ivac_display_box(t_display *display) { + + int x,y; - /* 23 x 80 */ - int column,line; + for(y=IVAC_PROMPT_LEN;ymax_y-IVAC_PROMPT_LEN;y++) { + mvaddch(y,0,'#'); + mvaddch(y,display->max_x-1,'#'); + } - for(column=0;columnmax_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0); + for(x=0;xmax_x;x++) addch('#'); + + return SUCCESS; +} - int column,line; +int ivac_display_console_content(t_ivac *ivac) { - for(column=0;columninput.c_count;column++) - printf("%c",ivac->input.content[column-12]); - for(column=12+ivac->input.c_count;columnconsole[y]); + move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2); + for(x=0;xconsole[y][x]); + } + refresh(); return SUCCESS; } -int ivac_display(t_ivac *ivac) { +int ivac_display_prompt(t_display *display) { - int line,column; + int x,y; - /* display head */ - ivac_display_head(); + move(display->max_y-3,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(display->max_y-2,0,"## command: "); + mvaddstr(display->max_y-2,display->max_x-2,"##"); + move(display->max_y-1,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); + + return SUCCESS; +} - /* build content of middle part + display */ - for(line=3;lineinput.c_count==0) { + move(ivac->display.max_y-2,12); + for(x=12;xdisplay.max_x-1;x++) addch(' '); } + for(x=0;xinput.c_count;x++) + mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]); + refresh(); + + return SUCCESS; +} + +int ivac_display(t_display *display) { + + int x,y; + + /* display head */ + ivac_display_head(display); + + /* display box */ + ivac_display_box(display); + + /* display console */ + ivac_display_console(display); + /* display command prompt */ - ivac_display_prompt(ivac); + ivac_display_prompt(display); + + return SUCCESS; +} + +int ivac_display_content(t_ivac *ivac) { + + /* display box content */ + ivac_display_box_content(ivac); + + /* display console content */ + ivac_display_console_content(ivac); + + /* display prompt content */ + ivac_display_prompt_content(ivac); + + return SUCCESS; +} + +int ivac_add_to_monitor(t_ivac *ivac,char *msg) { + + int i; + + for(i=0;iconsole[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN); + memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN); + ivac_display_console_content(ivac); return SUCCESS; } diff --git a/src/ivac.h b/src/ivac.h index bd427f0..9fb5263 100644 --- a/src/ivac.h +++ b/src/ivac.h @@ -13,6 +13,7 @@ #include "network.h" #include "event.h" #include "input.h" +#include "display.h" /* defines */ #define CHAR_USERNAME 32 @@ -34,8 +35,11 @@ #define VIDEO (1<<1) #define DUPLEX (1<<2) -#define COLUMN 80 -#define LINE 24 +#define IVAC_CONSOLE_STRING_LEN 64 + +#define IVAC_HEAD_LEN 3 +#define IVAC_PROMPT_LEN 3 +#define IVAC_CONSOLE_LEN 8 /* variables */ typedef struct s_challenger { @@ -51,7 +55,9 @@ typedef struct s_ivac { t_net net; t_event event; t_input input; + t_display display; t_challenger challenger[MAX_CONNECTIONS]; + char console[IVAC_CONSOLE_LEN][IVAC_CONSOLE_STRING_LEN]; } t_ivac; /* function prototypes */ @@ -61,8 +67,15 @@ int ivac_receive_info(int channel,t_ivac *ivac); int ivac_event_cb(t_event *event,void *ptr); int ivac_regular_cb(t_event *event,void *ptr); int ivac_parse_command(t_input *input,void *ptr); -int ivac_display_head(void); -int ivac_display_prompt(t_ivac *ivac); -int ivac_display(t_ivac *ivac); +int ivac_display_head(t_display *display); +int ivac_display_box(t_display *display); +int ivac_display_box_content(t_ivac *ivac); +int ivac_display_console(t_display *display); +int ivac_display_console_content(t_ivac *ivac); +int ivac_display_prompt(t_display *display); +int ivac_display_prompt_content(t_ivac *ivac); +int ivac_display(t_display *display); +int ivac_display_content(t_ivac *ivac); +int ivac_add_to_monitor(t_ivac *ivac,char *msg); #endif