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);
75 madtty_init_vt100_graphics();
77 getmaxyx(stdscr, screen_h, screen_w);
79 /* create a window with a frame */
80 term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
81 rt = madtty_create(screen_h - 2, screen_w -2);
83 const char *path = getenv("SHELL") ?: "/bin/sh";
84 const char *args[] = { path, "--login", NULL};
86 madtty_forkpty(rt, path, args, &pty);
89 /* keep reading keypresses from the user and passing them to the terminal;
90 * also, redraw the terminal to the window at each iteration */
91 gettimeofday(&next, NULL);
93 struct timeval tv = { 0, 1000 * 1000 / 100 };
101 if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) {
102 if (FD_ISSET(pty, &rfds)) {
109 int fd, cols = -1, rows = -1;
112 if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
113 if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
120 rows = atoi(getenv("LINES") ?: "24");
123 cols = atoi(getenv("COLUMNS") ?: "80");
126 resizeterm(rows, cols);
127 madtty_resize(rt, rows - 2, cols - 2);
128 wresize(term_win, rows - 2, cols - 2);
133 while ((ch = getch()) != ERR) {
134 madtty_keypress(rt, ch); /* pass the keypress for handling */
138 gettimeofday(&tv, NULL);
139 if (dirty && is_expired(tv, next)) {
140 madtty_draw(rt, term_win, 0, 0);
143 next = timeval_add(tv, slice);