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)
14 #include <sys/ioctl.h>
19 #include <madtty/madtty.h>
21 static int getout = 0, sigwinch = 0;
22 static int screen_w, screen_h;
23 static WINDOW *term_win;
25 void handler(int signo)
42 signal(SIGCHLD, handler);
43 signal(SIGWINCH, handler);
46 getmaxyx(stdscr, screen_h, screen_w);
48 /* create a window with a frame */
49 term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
50 rt = madtty_create(screen_h - 2, screen_w -2);
52 const char *path = getenv("SHELL") ?: "/bin/sh";
53 const char *args[] = { path, "--login", NULL};
55 madtty_forkpty(rt, path, args);
58 /* keep reading keypresses from the user and passing them to the terminal;
59 * also, redraw the terminal to the window at each iteration */
62 struct timeval tv = { 0, 1000 * 1000 / 50 };
67 FD_SET(rt->pty, &rfds);
69 if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
70 if (FD_ISSET(rt->pty, &rfds)) {
77 int fd, cols = -1, rows = -1;
80 if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
81 if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
88 rows = atoi(getenv("LINES") ?: "24");
91 cols = atoi(getenv("COLUMNS") ?: "80");
94 resizeterm(rows, cols);
95 madtty_resize(rt, rows - 2, cols - 2);
96 wresize(term_win, rows - 2, cols - 2);
101 while ((ch = getch()) != ERR) {
102 madtty_keypress(rt, ch); /* pass the keypress for handling */
107 madtty_draw(rt, term_win, 0, 0);