X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=demo%2Fboxshell.c;h=4a53642a1ab4ec507354480268c94b1f96a6f86f;hb=41252ec885c4abf052d086683f69edfc83fc5f0a;hp=e0040a5ae76460a4cd1a72e85d6ed51829f1e107;hpb=8ae25c5388a9b4c1f201ef3463967635d387ea92;p=apps%2Fmadtty.git diff --git a/demo/boxshell.c b/demo/boxshell.c index e0040a5..4a53642 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -2,120 +2,135 @@ * and lets the user interact with it. * * To compile: - * gcc -o boxshell boxshell.c $(rote-config --cflags --libs) + * gcc -o boxshell boxshell.c $(pkg-config madtty --cflags --libs) */ #include #include #include +#include #include static unsigned char getout = 0; -void sigchld(int signo) { getout = 1; } -int my_custom_handler(RoteTerm *rt, const char *es); - -int screen_w, screen_h; -WINDOW *term_win; - -int main() { - RoteTerm *rt; - int i, j, ch; - - signal(SIGCHLD, sigchld); - - initscr(); - noecho(); - start_color(); - raw(); - nodelay(stdscr, TRUE); /* prevents getch() from blocking; rather - * it will return ERR when there is no - * keypress available */ - - keypad(stdscr, TRUE); /* necessary to use rote_vt_keypress */ - getmaxyx(stdscr, screen_h, screen_w); - - /* initialize the color pairs the way rote_vt_draw expects it. You might - * initialize them differently, but in that case you would need - * to supply a custom conversion function for rote_vt_draw to - * call when setting attributes. The idea of this "default" mapping - * is to map (fg,bg) to the color pair bg * 8 + 7 - fg. This way, - * the pair (white,black) ends up mapped to 0, which means that - * it does not need a color pair (since it is the default). Since - * there are only 63 available color pairs (and 64 possible fg/bg - * combinations), we really have to save 1 pair by assigning no pair - * to the combination white/black. */ - for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) - if (i != 7 || j != 0) - init_pair(j*8+7-i, i, j); - - /* paint the screen blue */ - attrset(COLOR_PAIR(32)); - for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' '); - refresh(); - - /* create a window with a frame */ - term_win = newwin(22,72,1,4); - wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */ - wborder(term_win, 0, 0, 0, 0, 0, 0, 0, 0); - mvwprintw(term_win, 0, 27, " Term In a Box "); - wrefresh(term_win); - - /* create the terminal and have it run bash */ - rt = rote_vt_create(20, 70); - rote_vt_forkpty(rt, "/bin/bash --login"); - - /* add a sample custom escape sequence handler... say we want to handle - * the sequence, say, \e{N}, which will change the application's background - * color to N (where N is a number between 0 and 7). */ - rote_vt_install_handler(rt, my_custom_handler); - - /* keep reading keypresses from the user and passing them to the terminal; - * also, redraw the terminal to the window at each iteration */ - ch = '\0'; - while (!getout) { - rote_vt_draw(rt, term_win, 1, 1, NULL); - wrefresh(term_win); - - ch = getch(); - if (ch != ERR) - rote_vt_keypress(rt, ch); /* pass the keypress for handling */ - } - - endwin(); - return 0; +static int screen_w, screen_h; +static WINDOW *term_win; + +void sigchld(int signo __attribute__((unused))) +{ + getout = 1; } +int my_custom_handler(RoteTerm *rt __attribute__((unused)), const char *es) +{ + int color; + int i, j; -int my_custom_handler(RoteTerm *rt, const char *es) { - int color; - int i, j; + /* if the escape sequence does not begin with '{', we give up */ + if (*es != '{') return ROTE_HANDLERESULT_NOWAY; - /* if the escape sequence does not begin with '{', we give up */ - if (*es != '{') return ROTE_HANDLERESULT_NOWAY; + /* ok, now we know it begins with '{'. Now, if it does not end with '}', + * it is not yet complete */ + if (es[strlen(es)-1] != '}') return ROTE_HANDLERESULT_NOTYET; - /* ok, now we know it begins with '{'. Now, if it does not end with '}', - * it is not yet complete */ - if (es[strlen(es)-1] != '}') return ROTE_HANDLERESULT_NOTYET; + /* ok, the sequence is complete */ + color = atoi(es + 1); + if (color < 0 || color > 7) return false; /* don't recognize it */ - /* ok, the sequence is complete */ - color = atoi(es + 1); - if (color < 0 || color > 7) return false; /* don't recognize it */ + /* paint the background with that color */ + attrset(COLOR_PAIR(color * 8)); + move(0, 0); + for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' '); + touchwin(stdscr); + refresh(); - /* paint the background with that color */ - attrset(COLOR_PAIR(color * 8)); - move(0, 0); - for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' '); - touchwin(stdscr); - refresh(); + /* touch term_win to force it to do a full redraw next time */ + touchwin(term_win); - /* touch term_win to force it to do a full redraw next time */ - touchwin(term_win); + /* and redraw the terminal window */ + wrefresh(term_win); - /* and redraw the terminal window */ - wrefresh(term_win); - - /* escape sequence was handled ok */ - return ROTE_HANDLERESULT_OK; + /* escape sequence was handled ok */ + return ROTE_HANDLERESULT_OK; } +int main(int argc, char *argv[]) +{ + RoteTerm *rt; + int i, j, ch, w, h; + + signal(SIGCHLD, sigchld); + + w = 80; + h = 50; + if (argc > 1) { + char *p = argv[1]; + w = strtol(p, &p, 10); + if (*p++ == 'x') + h = strtol(p, &p, 10); + } + + initscr(); + noecho(); + start_color(); + raw(); + nodelay(stdscr, TRUE); /* prevents getch() from blocking; rather + * it will return ERR when there is no + * keypress available */ + + keypad(stdscr, TRUE); /* necessary to use rote_vt_keypress */ + getmaxyx(stdscr, screen_h, screen_w); + + /* initialize the color pairs the way rote_vt_draw expects it. You might + * initialize them differently, but in that case you would need + * to supply a custom conversion function for rote_vt_draw to + * call when setting attributes. The idea of this "default" mapping + * is to map (fg, bg) to the color pair bg * 8 + 7 - fg. This way, + * the pair (white, black) ends up mapped to 0, which means that + * it does not need a color pair (since it is the default). Since + * there are only 63 available color pairs (and 64 possible fg/bg + * combinations), we really have to save 1 pair by assigning no pair + * to the combination white/black. */ + for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) + if (i != 7 || j != 0) + init_pair(j*8+7-i, i, j); + + /* paint the screen blue */ + attrset(COLOR_PAIR(32)); + for (i = 0; i < screen_h; i++) + for (j = 0; j < screen_w; j++) + addch(' '); + refresh(); + + /* create a window with a frame */ + term_win = newwin(h + 2, w + 2, 1, 2); + wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */ + wborder(term_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); + mvwprintw(term_win, 0, 27, " Term In a Box "); + wrefresh(term_win); + + /* create the terminal and have it run bash */ + rt = rote_vt_create(h, w); + rote_vt_forkpty(rt, "/bin/bash --login"); + + /* add a sample custom escape sequence handler... say we want to handle + * the sequence, say, \e{N}, which will change the application's background + * color to N (where N is a number between 0 and 7). */ + rote_vt_install_handler(rt, my_custom_handler); + + /* keep reading keypresses from the user and passing them to the terminal; + * also, redraw the terminal to the window at each iteration */ + ch = '\0'; + while (!getout) { + usleep(10000); + rote_vt_draw(rt, term_win, 1, 1, NULL); + wrefresh(term_win); + + ch = getch(); + if (ch != ERR) + rote_vt_keypress(rt, ch); /* pass the keypress for handling */ + } + + endwin(); + return 0; +}