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