1 /* Just a simple example program that creates a terminal in a frame
2 * and lets the user interact with it.
5 * gcc -o boxshell boxshell.c $(pkg-config madtty --cflags --libs)
16 #include <madtty/madtty.h>
18 static unsigned char getout = 0;
19 static int screen_w, screen_h;
20 static WINDOW *term_win;
22 void sigchld(int signo __attribute__((unused)))
27 int main(int argc, char *argv[])
29 struct timeval next = { 0, 0 };
31 int i, j, ch, w, h, pos;
34 signal(SIGCHLD, sigchld);
40 w = strtol(p, &p, 10);
42 h = strtol(p, &p, 10);
45 setlocale(LC_ALL, "");
50 nodelay(stdscr, TRUE); /* prevents getch() from blocking; rather
51 * it will return ERR when there is no
52 * keypress available */
54 keypad(stdscr, TRUE); /* necessary to use rote_vt_keypress */
55 getmaxyx(stdscr, screen_h, screen_w);
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++)
69 init_pair(j*8+7-i, i, j);
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++)
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 ");
84 rt = rote_vt_create(h, w);
86 const char *path = getenv("SHELL") ?: "/bin/sh";
87 const char *args[] = { path, "--login", NULL};
89 rote_vt_forkpty(rt, path, args);
92 /* keep reading keypresses from the user and passing them to the terminal;
93 * also, redraw the terminal to the window at each iteration */
98 struct timeval tv = { 0 , 1000 }, t;
101 FD_SET(rt->pty, &rfds);
103 if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
106 nb = rote_vt_read(rt, buf + pos, sizeof(buf) - pos);
111 nb = rote_vt_inject(rt, buf, pos);
114 memmove(buf, buf + nb, pos - nb);
118 while ((ch = getch()) != ERR) {
119 rote_vt_keypress(rt, ch); /* pass the keypress for handling */
122 gettimeofday(&t, NULL);
123 if (timercmp(&t, &next, >=)) {
124 rote_vt_draw(rt, term_win, 1, 1);
126 gettimeofday(&next, NULL);
127 next.tv_usec += 1000 * 1000 / 100;
128 if (next.tv_usec > 1000 * 1000) {
129 next.tv_usec -= 1000 * 1000;