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 <sys/ioctl.h>
20 #include <madtty/madtty.h>
22 static int getout = 0, sigwinch = 0;
23 static int screen_w, screen_h;
24 static WINDOW *term_win;
25 static struct timeval const slice = { 0, 1000 * 1000 / 100 };
27 void handler(int signo)
39 static struct timeval timeval_add(struct timeval a, struct timeval b)
41 int usec = a.tv_usec + b.tv_usec;
43 while (usec > 1000 * 1000) {
51 static int is_expired(struct timeval now, struct timeval expiry)
53 return now.tv_sec > expiry.tv_sec
54 || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
63 signal(SIGCHLD, handler);
64 signal(SIGWINCH, handler);
66 setlocale(LC_ALL, "");
71 nodelay(stdscr, TRUE);
76 getmaxyx(stdscr, screen_h, screen_w);
78 /* create a window with a frame */
79 term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
80 rt = madtty_create(screen_h - 2, screen_w -2);
82 const char *path = getenv("SHELL") ?: "/bin/sh";
83 const char *args[] = { path, "--login", NULL};
85 madtty_forkpty(rt, path, args, &pty);
88 /* keep reading keypresses from the user and passing them to the terminal;
89 * also, redraw the terminal to the window at each iteration */
90 gettimeofday(&next, NULL);
92 struct timeval tv = { 0, 1000 * 1000 / 100 };
100 if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) {
101 if (FD_ISSET(pty, &rfds)) {
108 int fd, cols = -1, rows = -1;
111 if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
112 if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
119 rows = atoi(getenv("LINES") ?: "24");
122 cols = atoi(getenv("COLUMNS") ?: "80");
125 resizeterm(rows, cols);
126 madtty_resize(rt, rows - 2, cols - 2);
127 wresize(term_win, rows - 2, cols - 2);
132 while ((ch = getch()) != ERR) {
133 madtty_keypress(rt, ch); /* pass the keypress for handling */
137 gettimeofday(&tv, NULL);
138 if (dirty && is_expired(tv, next)) {
139 madtty_draw(rt, term_win, 0, 0);
142 next = timeval_add(tv, slice);