even better
[apps/madtty.git] / demo / boxshell.c
1 /* Just a simple example program that creates a terminal in a frame
2  * and lets the user interact with it.
3  *
4  * To compile:
5  *    gcc -o boxshell boxshell.c $(rote-config --cflags --libs)
6  */
7
8 #include <ncurses.h>
9 #include <stdio.h>
10 #include <signal.h>
11
12 #include <madtty/madtty.h>
13
14 static unsigned char getout = 0;
15 void sigchld(int signo) { getout = 1; }
16 int my_custom_handler(RoteTerm *rt, const char *es);
17    
18 int screen_w, screen_h;
19 WINDOW *term_win;
20
21 int main() {
22    RoteTerm *rt;
23    int i, j, ch;
24
25    signal(SIGCHLD, sigchld);
26
27    initscr();
28    noecho();
29    start_color();
30    raw();
31    nodelay(stdscr, TRUE);       /* prevents getch() from blocking; rather
32                                  * it will return ERR when there is no
33                                  * keypress available */
34
35    keypad(stdscr, TRUE);        /* necessary to use rote_vt_keypress */
36    getmaxyx(stdscr, screen_h, screen_w);
37
38    /* initialize the color pairs the way rote_vt_draw expects it. You might
39     * initialize them differently, but in that case you would need
40     * to supply a custom conversion function for rote_vt_draw to
41     * call when setting attributes. The idea of this "default" mapping
42     * is to map (fg,bg) to the color pair bg * 8 + 7 - fg. This way,
43     * the pair (white,black) ends up mapped to 0, which means that
44     * it does not need a color pair (since it is the default). Since
45     * there are only 63 available color pairs (and 64 possible fg/bg
46     * combinations), we really have to save 1 pair by assigning no pair
47     * to the combination white/black. */
48    for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
49       if (i != 7 || j != 0)
50          init_pair(j*8+7-i, i, j);
51
52    /* paint the screen blue */
53    attrset(COLOR_PAIR(32));
54    for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' ');
55    refresh();
56
57    /* create a window with a frame */
58    term_win = newwin(22,72,1,4);
59    wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */
60    wborder(term_win, 0, 0, 0, 0, 0, 0, 0, 0);
61    mvwprintw(term_win, 0, 27, " Term In a Box ");
62    wrefresh(term_win);
63
64    /* create the terminal and have it run bash */
65    rt = rote_vt_create(20, 70);
66    rote_vt_forkpty(rt, "/bin/bash --login");
67
68    /* add a sample custom escape sequence handler... say we want to handle
69     * the sequence, say, \e{N}, which will change the application's background
70     * color to N (where N is a number between 0 and 7). */
71    rote_vt_install_handler(rt, my_custom_handler);
72
73    /* keep reading keypresses from the user and passing them to the terminal;
74     * also, redraw the terminal to the window at each iteration */
75    ch = '\0';
76    while (!getout) {
77       rote_vt_draw(rt, term_win, 1, 1, NULL);
78       wrefresh(term_win);
79       
80       ch = getch();
81       if (ch != ERR) 
82          rote_vt_keypress(rt, ch); /* pass the keypress for handling */
83    }
84
85    endwin();
86    return 0;
87 }
88
89
90 int my_custom_handler(RoteTerm *rt, const char *es) {
91    int color;
92    int i, j;
93
94    /* if the escape sequence does not begin with '{', we give up */
95    if (*es != '{') return ROTE_HANDLERESULT_NOWAY;
96
97    /* ok, now we know it begins with '{'. Now, if it does not end with '}',
98     * it is not yet complete */
99    if (es[strlen(es)-1] != '}') return ROTE_HANDLERESULT_NOTYET;
100
101    /* ok, the sequence is complete */
102    color = atoi(es + 1);
103    if (color < 0 || color > 7) return false; /* don't recognize it */
104
105    /* paint the background with that color */
106    attrset(COLOR_PAIR(color * 8));
107    move(0, 0);
108    for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' ');
109    touchwin(stdscr);
110    refresh();
111
112    /* touch term_win to force it to do a full redraw next time */
113    touchwin(term_win);
114
115    /* and redraw the terminal window */
116    wrefresh(term_win);
117    
118    /* escape sequence was handled ok */
119    return ROTE_HANDLERESULT_OK;
120 }
121