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