a1d746e2c0627c4d18ccb9615a076ca62a6e910c
[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 <locale.h>
10 #include <stdio.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <sys/time.h>
14 #include <time.h>
15
16 #include <madtty/madtty.h>
17
18 static unsigned char getout = 0;
19 static int screen_w, screen_h;
20 static WINDOW *term_win;
21
22 void sigchld(int signo __attribute__((unused)))
23 {
24     getout = 1;
25 }
26
27 int main(int argc, char *argv[])
28 {
29     struct timeval next = { 0, 0 };
30     RoteTerm *rt;
31     int i, j, ch, w, h, pos;
32     char buf[BUFSIZ];
33
34     signal(SIGCHLD, sigchld);
35
36     w = 80;
37     h = 40;
38     if (argc > 1) {
39         char *p = argv[1];
40         w = strtol(p, &p, 10);
41         if (*p++ == 'x')
42             h = strtol(p, &p, 10);
43     }
44
45     setlocale(LC_ALL, "");
46     initscr();
47     noecho();
48     start_color();
49     raw();
50     nodelay(stdscr, TRUE);       /* prevents getch() from blocking; rather
51                                   * it will return ERR when there is no
52                                   * keypress available */
53
54     keypad(stdscr, TRUE);        /* necessary to use rote_vt_keypress */
55     getmaxyx(stdscr, screen_h, screen_w);
56
57     /* initialize the color pairs the way rote_vt_draw expects it. You might
58      * initialize them differently, but in that case you would need
59      * to supply a custom conversion function for rote_vt_draw to
60      * call when setting attributes. The idea of this "default" mapping
61      * is to map (fg, bg) to the color pair bg * 8 + 7 - fg. This way,
62      * the pair (white, black) ends up mapped to 0, which means that
63      * it does not need a color pair (since it is the default). Since
64      * there are only 63 available color pairs (and 64 possible fg/bg
65      * combinations), we really have to save 1 pair by assigning no pair
66      * to the combination white/black. */
67     for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
68         if (i != 7 || j != 0)
69             init_pair(j*8+7-i, i, j);
70
71     /* paint the screen blue */
72     attrset(COLOR_PAIR(32));
73     for (i = 0; i < screen_h; i++)
74         for (j = 0; j < screen_w; j++)
75             addch(' ');
76     refresh();
77
78     /* create a window with a frame */
79     term_win = newwin(h + 2, w + 2, 2, 3);
80     wborder(term_win, '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0');
81     mvwprintw(term_win, 0, 27, " Term In a Box ");
82     wrefresh(term_win);
83
84     rt = rote_vt_create(h, w);
85     {
86         const char *path = getenv("SHELL") ?: "/bin/sh";
87         const char *args[] = { path, "--login", NULL};
88
89         rote_vt_forkpty(rt, path, args);
90     }
91
92     /* keep reading keypresses from the user and passing them to the terminal;
93      * also, redraw the terminal to the window at each iteration */
94     ch = '\0';
95     pos = 0;
96     while (!getout) {
97         fd_set rfds;
98         struct timeval tv = { 0 , 1000 }, t;
99
100         FD_ZERO(&rfds);
101         FD_SET(rt->pty, &rfds);
102
103         if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
104             int nb;
105
106             nb = rote_vt_read(rt, buf + pos, sizeof(buf) - pos);
107             if (nb <= 0)
108                 continue;
109             pos += nb;
110
111             nb = rote_vt_inject(rt, buf, pos);
112             if (nb <= 0)
113                 continue;
114             memmove(buf, buf + nb, pos - nb);
115             pos -= nb;
116         }
117
118         while ((ch = getch()) != ERR) {
119             rote_vt_keypress(rt, ch); /* pass the keypress for handling */
120         }
121
122         gettimeofday(&t, NULL);
123         if (timercmp(&t, &next, >=)) {
124             rote_vt_draw(rt, term_win, 1, 1);
125             wrefresh(term_win);
126             gettimeofday(&next, NULL);
127             next.tv_usec += 1000 * 1000 / 100;
128             if (next.tv_usec > 1000 * 1000) {
129                 next.tv_usec -= 1000 * 1000;
130                 next.tv_sec++;
131             }
132         }
133     }
134
135     endwin();
136     return 0;
137 }