/* Curses Wrapper Functions: CDisplay *CWInit(int argc, char *argv[]) void CWShutdown(CDisplay *display) Window CWCreateWindow( CDisplay *display, Window parent, int x, int y, unsigned int width, unsigned int height ) unsigned int CWRootWidth(CDisplay *display) unsigned int CWRootHeight(CDisplay *display) void CWMoveCursor( CDisplay *display, Window w, int x, int y ) void CWDrawCharacter( CDisplay *display, Window w, int x, int y, char c ) void CWDrawString( CDisplay *display, Window w, char *string ) void CWDrawStringPosition( CDisplay *display, Window w, int x, int y, char *string ) void CWDrawStringPositionLimited( CDisplay *display, Window w, int x, int y, char *string, int limit ) void CWClearArea( CDisplay *display, Window w, int x, int y, unsigned int width, unsigned int height ) int CGetNextEvent( CDisplay *display, CEvent *event ) void CWSync( CDisplay *display, unsigned int options ) --- */ #include #include #include #include #include #include "cursesw.h" /* * Initializes the curses library, returns pointer to * curses display structure or NULL on failure. */ CDisplay *CWInit(int argc, char *argv[]) { char *strptr; CDisplay *display; WINDOW *w; SCREEN *screen; /* Allocate display structure. */ display = (CDisplay *)calloc(1, sizeof(CDisplay)); if(display == NULL) return(NULL); /* Initialize curses library. */ w = initscr(); if(w == NULL) { free(display); return(NULL); } /* Set root window ID. */ display->root_win = (Window)w; /* Get width and height of root window. */ display->root_width = CWRootWidth(display); display->root_height = CWRootHeight(display); /* Set screen. */ /* strptr = getenv("TERM"); if(strptr != NULL) { screen = newterm( strptr, stdout, stdin ); } */ /* Move cursor to root window. */ display->cursor_window = display->root_win; display->cursor_x = 0; display->cursor_y = 0; /* Turn off echo. */ noecho(); /* Enter cbreak mode. */ cbreak(); /* Allow line feed. */ nonl(); /* No hang on input */ nodelay(stdscr, TRUE); /* Enable keypad and cursor keys. */ keypad(stdscr, TRUE); /* Insert and delete key use is ok. */ idcok(stdscr, TRUE); return(display); } /* * Shutsdown curses library and deallocates display structure. */ void CWShutdown(CDisplay *display) { if(display == NULL) return; clrtoeol(); /* Clear bottom line. */ refresh(); /* Flush out everything. */ endwin(); /* Curses clean up */ /* Deallocate display structure. */ free(display); return; } /* * Creates a new window, can return None on error. */ Window CWCreateWindow( CDisplay *display, Window parent, int x, int y, unsigned int width, unsigned int height ) { return( (Window)subwin( (WINDOW *)parent, height, width, y, x ) ); } /* * Macros to fetch width and height of root window. */ unsigned int CWRootWidth(CDisplay *display) { unsigned int width; char *strptr; return(COLS); strptr = getenv("COLUMNS"); if(strptr == NULL) width = DefaultRootWidth; else width = atoi(strptr); return(width); } unsigned int CWRootHeight(CDisplay *display) { unsigned int height; char *strptr; return(LINES); strptr = getenv("LINES"); if(strptr == NULL) height = DefaultRootHeight; else height = atoi(strptr); return(height); } /* * Moves the cursor position to the Window w and to a specified * position. */ void CWMoveCursor( CDisplay *display, Window w, int x, int y ) { if((display == NULL) || (w == None) ) return; /* Move cursor to window and specified position. */ wmove( (WINDOW *)w, y, x ); /* Update current cursor location. */ display->cursor_window = w; display->cursor_x = x; display->cursor_y = y; return; } /* * Draws a character at the specified position on the Window * w. */ void CWDrawCharacter( CDisplay *display, Window w, int x, int y, char c ) { if((display == NULL) || (w == None) ) return; mvwaddch( (WINDOW *)w, y, x, c ); return; } /* * Draws a string at the current cursor position on the Window * w. * * If there are any '%' characters in the string, then each one * must be doubled. Example "Progress is 52%% complete.". */ void CWDrawString( CDisplay *display, Window w, char *string ) { int len; if((display == NULL) || (w == None) || (string == NULL) ) return; /* Get length of string. */ len = strlen(string); /* Draw string. */ wprintw( (WINDOW *)w, string ); /* Update current cursor location. */ display->cursor_window = w; display->cursor_x += len; return; } /* * Draws a string at the specified cursor position on the Window * w. * * If there are any '%' characters in the string, then each one * must be doubled. Example "Progress is 52%% complete.". */ void CWDrawStringPosition( CDisplay *display, Window w, int x, int y, char *string ) { int i, xc; if((display == NULL) || (w == None) || (string == NULL) ) return; for(xc = x, i = 0; string[i] != '\0'; xc++, i++) mvwaddch( (WINDOW *)w, y, xc, string[i] ); /* Update current cursor location. */ display->cursor_window = w; display->cursor_x = x + i; display->cursor_y = y; return; } /* * Same as CWDrawStringPosition() except that it will not draw * more than the number of characters specified by limit. */ void CWDrawStringPositionLimited( CDisplay *display, Window w, int x, int y, char *string, int limit ) { int i, xc; if((display == NULL) || (w == None) || (string == NULL) ) return; for(xc = x, i = 0; string[i] != '\0'; xc++, i++) { if(i >= limit) break; mvwaddch( (WINDOW *)w, y, xc, string[i] ); } /* Update current cursor location. */ display->cursor_window = w; display->cursor_x = x + i; display->cursor_y = y; return; } /* * Puts ' ' characters to the specified area. The specified * width and height must be atleast one or greater. */ void CWClearArea( CDisplay *display, Window w, int x, int y, unsigned int width, unsigned int height ) { int xc, yc, wc, hc; char c = ' '; if((display == NULL) || (w == None) || (width == 0) || (height == 0) ) return; wc = x + (int)width; hc = y + (int)height; for(yc = y; yc < hc; yc++) { for(xc = x; xc < wc; xc++) mvwaddch( (WINDOW *)w, yc, xc, c ); } /* Update current cursor location. */ display->cursor_window = w; display->cursor_x = x + (int)width; display->cursor_y = y + (int)height; return; } /* * Fetches the next event, returns positive if it got an event, * or 0 if there were no events or an error occured. */ int CGetNextEvent( CDisplay *display, CEvent *event ) { int got_event = 0; int c; char text[256]; if((display == NULL) || (event == NULL) ) return(got_event); /* Check for CKeyPress. */ c = getch(); if(c != ERR) { event->type = CKeyPress; event->ckey.key = c; got_event++; } refresh(); /* Flush out everything. */ return(got_event); } /* * Syncronizes the curses library. */ void CWSync( CDisplay *display, unsigned int options ) { if(display == NULL) return; refresh(); return; }