more refactoring
[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 #include <stdio.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <sys/time.h>
13 #include <time.h>
14
15 #include <madtty/madtty.h>
16
17 static unsigned char getout = 0;
18 static int screen_w, screen_h;
19 static WINDOW *term_win;
20
21 void sigchld(int signo __attribute__((unused)))
22 {
23     getout = 1;
24 }
25
26 int main(int argc, char *argv[])
27 {
28     struct timeval next = { 0, 0 };
29     madtty_t *rt;
30     int i, j, ch, w, h, pos;
31     char buf[BUFSIZ];
32
33     signal(SIGCHLD, sigchld);
34
35     w = 80;
36     h = 40;
37     if (argc > 1) {
38         char *p = argv[1];
39         w = strtol(p, &p, 10);
40         if (*p++ == 'x')
41             h = strtol(p, &p, 10);
42     }
43
44     madtty_initialize();
45     getmaxyx(stdscr, screen_h, screen_w);
46
47     /* paint the screen blue */
48     attrset(COLOR_PAIR(004));
49     for (i = 0; i < screen_h; i++)
50         for (j = 0; j < screen_w; j++)
51             addch(' ');
52     refresh();
53
54     /* create a window with a frame */
55     term_win = newwin(h, w, 2, 3);
56     mvwprintw(term_win, 0, 27, " Term In a Box ");
57     wrefresh(term_win);
58
59     rt = madtty_create(h, w);
60     {
61         const char *path = getenv("SHELL") ?: "/bin/sh";
62         const char *args[] = { path, "--login", NULL};
63
64         madtty_forkpty(rt, path, args);
65     }
66
67     /* keep reading keypresses from the user and passing them to the terminal;
68      * also, redraw the terminal to the window at each iteration */
69     ch = '\0';
70     pos = 0;
71     while (!getout) {
72         fd_set rfds;
73         struct timeval tv = { 0 , 1000 }, t;
74
75         FD_ZERO(&rfds);
76         FD_SET(rt->pty, &rfds);
77
78         if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
79             int nb;
80
81             nb = madtty_read(rt, buf + pos, sizeof(buf) - pos);
82             if (nb <= 0)
83                 continue;
84             pos += nb;
85
86             nb = madtty_inject(rt, buf, pos);
87             if (nb <= 0)
88                 continue;
89             memmove(buf, buf + nb, pos - nb);
90             pos -= nb;
91         }
92
93         while ((ch = getch()) != ERR) {
94             madtty_keypress(rt, ch); /* pass the keypress for handling */
95         }
96
97         gettimeofday(&t, NULL);
98         if (timercmp(&t, &next, >=)) {
99             madtty_draw(rt, term_win, 0, 0);
100             wrefresh(term_win);
101             gettimeofday(&next, NULL);
102             next.tv_usec += 1000 * 1000 / 50;
103             if (next.tv_usec > 1000 * 1000) {
104                 next.tv_usec -= 1000 * 1000;
105                 next.tv_sec++;
106             }
107         }
108     }
109
110     endwin();
111     return 0;
112 }