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)))
28 struct timeval next = { 0, 0 };
33 signal(SIGCHLD, sigchld);
36 getmaxyx(stdscr, screen_h, screen_w);
38 /* create a window with a frame */
39 term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
40 rt = madtty_create(screen_h - 2, screen_w - 2);
42 const char *path = getenv("SHELL") ?: "/bin/sh";
43 const char *args[] = { path, "--login", NULL};
45 madtty_forkpty(rt, path, args);
48 /* keep reading keypresses from the user and passing them to the terminal;
49 * also, redraw the terminal to the window at each iteration */
53 struct timeval tv = { 0 , 1000 }, t;
57 FD_SET(rt->pty, &rfds);
59 if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
60 if (madtty_process(rt))
64 while ((ch = getch()) != ERR) {
65 madtty_keypress(rt, ch); /* pass the keypress for handling */
68 gettimeofday(&t, NULL);
69 if (timercmp(&t, &next, >=)) {
70 madtty_draw(rt, term_win, 0, 0);
72 gettimeofday(&next, NULL);
73 next.tv_usec += 1000 * 1000 / 50;
74 if (next.tv_usec > 1000 * 1000) {
75 next.tv_usec -= 1000 * 1000;