7c9512d423fc57c9e48a0fb14a37333c692ad1f2
[apps/madtty.git] / demo / boxshell.c
1 /* Just a simple example program that creates a terminal in a frame
2  * and lets the user interact with it.
3  *
4  * To compile:
5  *    gcc -o boxshell boxshell.c $(pkg-config madtty --cflags --libs)
6  */
7
8 #include <ncurses.h>
9
10 #include <signal.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/ioctl.h>
14 #include <sys/time.h>
15 #include <termios.h>
16 #include <time.h>
17
18 #include <madtty/madtty.h>
19
20 static unsigned char getout = 0;
21 static int screen_w, screen_h;
22 static WINDOW *term_win;
23
24 void handler(int signo)
25 {
26     switch (signo) {
27       case SIGCHLD:
28         getout = 1;
29         break;
30       case SIGWINCH:
31         break;
32     }
33 }
34
35 int main(void)
36 {
37     madtty_t *rt;
38     int dirty = 0;
39
40     signal(SIGCHLD, handler);
41     signal(SIGWINCH, handler);
42
43     madtty_initialize();
44     getmaxyx(stdscr, screen_h, screen_w);
45
46     /* create a window with a frame */
47     term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
48     rt = madtty_create(screen_h - 2, screen_w - 2);
49     {
50         const char *path = getenv("SHELL") ?: "/bin/sh";
51         const char *args[] = { path, "--login", NULL};
52
53         madtty_forkpty(rt, path, args);
54     }
55
56     /* keep reading keypresses from the user and passing them to the terminal;
57      * also, redraw the terminal to the window at each iteration */
58     while (!getout) {
59         fd_set rfds;
60         struct timeval tv = { 0, 1000 * 1000 / 50 };
61         int ch;
62
63         FD_ZERO(&rfds);
64         FD_SET(rt->pty, &rfds);
65
66         if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
67             if (FD_ISSET(rt->pty, &rfds)) {
68                 madtty_process(rt);
69                 dirty = 1;
70             }
71         }
72
73         while ((ch = getch()) != ERR) {
74 #if 0
75             if (ch == KEY_F(3)) {
76                 struct winsize ws = {
77                     .ws_row = --rt->rows,
78                     .ws_col = --rt->cols,
79                 };
80
81                 erase();
82                 ioctl(rt->pty, TIOCSWINSZ, &ws);
83                 wresize(term_win, rt->rows, rt->cols);
84             }
85 #endif
86             madtty_keypress(rt, ch); /* pass the keypress for handling */
87             dirty = 1;
88         }
89
90         if (dirty) {
91             madtty_draw(rt, term_win, 0, 0);
92             wrefresh(term_win);
93             dirty = 0;
94         }
95     }
96
97     endwin();
98     return 0;
99 }