remove the handler thing, that's really not the way to do that.
[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 main(int argc, char *argv[])
25 {
26     RoteTerm *rt;
27     int i, j, ch, w, h;
28
29     signal(SIGCHLD, sigchld);
30
31     w = 80;
32     h = 50;
33     if (argc > 1) {
34         char *p = argv[1];
35         w = strtol(p, &p, 10);
36         if (*p++ == 'x')
37             h = strtol(p, &p, 10);
38     }
39
40     initscr();
41     noecho();
42     start_color();
43     raw();
44     nodelay(stdscr, TRUE);       /* prevents getch() from blocking; rather
45                                   * it will return ERR when there is no
46                                   * keypress available */
47
48     keypad(stdscr, TRUE);        /* necessary to use rote_vt_keypress */
49     getmaxyx(stdscr, screen_h, screen_w);
50
51     /* initialize the color pairs the way rote_vt_draw expects it. You might
52      * initialize them differently, but in that case you would need
53      * to supply a custom conversion function for rote_vt_draw to
54      * call when setting attributes. The idea of this "default" mapping
55      * is to map (fg, bg) to the color pair bg * 8 + 7 - fg. This way,
56      * the pair (white, black) ends up mapped to 0, which means that
57      * it does not need a color pair (since it is the default). Since
58      * there are only 63 available color pairs (and 64 possible fg/bg
59      * combinations), we really have to save 1 pair by assigning no pair
60      * to the combination white/black. */
61     for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
62         if (i != 7 || j != 0)
63             init_pair(j*8+7-i, i, j);
64
65     /* paint the screen blue */
66     attrset(COLOR_PAIR(32));
67     for (i = 0; i < screen_h; i++)
68         for (j = 0; j < screen_w; j++)
69             addch(' ');
70     refresh();
71
72     /* create a window with a frame */
73     term_win = newwin(h + 2, w + 2, 1, 2);
74     wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */
75     wborder(term_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
76     mvwprintw(term_win, 0, 27, " Term In a Box ");
77     wrefresh(term_win);
78
79     /* create the terminal and have it run bash */
80     rt = rote_vt_create(h, w);
81     rote_vt_forkpty(rt, "/bin/bash --login");
82
83     /* keep reading keypresses from the user and passing them to the terminal;
84      * also, redraw the terminal to the window at each iteration */
85     ch = '\0';
86     while (!getout) {
87         usleep(10000);
88         rote_vt_draw(rt, term_win, 1, 1, NULL);
89         wrefresh(term_win);
90
91         ch = getch();
92         if (ch != ERR) 
93             rote_vt_keypress(rt, ch); /* pass the keypress for handling */
94     }
95
96     endwin();
97     return 0;
98 }