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)
15 #include <madtty/madtty.h>
17 static unsigned char getout = 0;
18 static int screen_w, screen_h;
19 static WINDOW *term_win;
21 void sigchld(int signo __attribute__((unused)))
26 int main(int argc, char *argv[])
28 struct timeval next = { 0, 0 };
30 int i, j, ch, w, h, pos;
33 signal(SIGCHLD, sigchld);
39 w = strtol(p, &p, 10);
41 h = strtol(p, &p, 10);
45 getmaxyx(stdscr, screen_h, screen_w);
47 /* paint the screen blue */
48 attrset(COLOR_PAIR(004));
49 for (i = 0; i < screen_h; i++)
50 for (j = 0; j < screen_w; j++)
54 /* create a window with a frame */
55 term_win = newwin(h, w, 2, 3);
56 mvwprintw(term_win, 0, 27, " Term In a Box ");
59 rt = madtty_create(h, w);
61 const char *path = getenv("SHELL") ?: "/bin/sh";
62 const char *args[] = { path, "--login", NULL};
64 madtty_forkpty(rt, path, args);
67 /* keep reading keypresses from the user and passing them to the terminal;
68 * also, redraw the terminal to the window at each iteration */
73 struct timeval tv = { 0 , 1000 }, t;
76 FD_SET(rt->pty, &rfds);
78 if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
81 nb = madtty_read(rt, buf + pos, sizeof(buf) - pos);
86 nb = madtty_inject(rt, buf, pos);
89 memmove(buf, buf + nb, pos - nb);
93 while ((ch = getch()) != ERR) {
94 madtty_keypress(rt, ch); /* pass the keypress for handling */
97 gettimeofday(&t, NULL);
98 if (timercmp(&t, &next, >=)) {
99 madtty_draw(rt, term_win, 0, 0);
101 gettimeofday(&next, NULL);
102 next.tv_usec += 1000 * 1000 / 50;
103 if (next.tv_usec > 1000 * 1000) {
104 next.tv_usec -= 1000 * 1000;