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 };
31 signal(SIGCHLD, sigchld);
34 getmaxyx(stdscr, screen_h, screen_w);
36 /* create a window with a frame */
37 term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
38 rt = madtty_create(screen_h - 2, screen_w - 2);
40 const char *path = getenv("SHELL") ?: "/bin/sh";
41 const char *args[] = { path, "--login", NULL};
43 madtty_forkpty(rt, path, args);
46 /* keep reading keypresses from the user and passing them to the terminal;
47 * also, redraw the terminal to the window at each iteration */
50 struct timeval tv = { 0 , 1000 }, t;
54 FD_SET(rt->pty, &rfds);
56 if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
57 if (madtty_process(rt))
61 while ((ch = getch()) != ERR) {
62 madtty_keypress(rt, ch); /* pass the keypress for handling */
65 gettimeofday(&t, NULL);
66 if (timercmp(&t, &next, >=)) {
67 madtty_draw(rt, term_win, 0, 0);
69 gettimeofday(&next, NULL);
70 next.tv_usec += 1000 * 1000 / 50;
71 if (next.tv_usec > 1000 * 1000) {
72 next.tv_usec -= 1000 * 1000;