gcc says those values are out of bounds
[apps/madtty.git] / demo / boxshell.c
index fa7d2ee..15fbc41 100644 (file)
@@ -8,6 +8,7 @@
 #include <ncurses.h>
 
 #include <fcntl.h>
+#include <locale.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
@@ -21,6 +22,7 @@
 static int getout = 0, sigwinch = 0;
 static int screen_w, screen_h;
 static WINDOW *term_win;
+static struct timeval const slice = { 0, 1000 * 1000 / 100 };
 
 void handler(int signo)
 {
@@ -34,15 +36,44 @@ void handler(int signo)
     }
 }
 
+static struct timeval timeval_add(struct timeval a, struct timeval b)
+{
+    int usec = a.tv_usec + b.tv_usec;
+    a.tv_sec += b.tv_sec;
+    while (usec > 1000 * 1000) {
+        a.tv_sec += 1;
+        usec -= 1000 * 1000;
+    }
+    a.tv_usec = usec;
+    return a;
+}
+
+static int is_expired(struct timeval now, struct timeval expiry)
+{
+    return now.tv_sec > expiry.tv_sec
+        || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
+}
+
 int main(void)
 {
     madtty_t *rt;
-    int dirty = 0;
+    int dirty = 0, pty;
+    struct timeval next;
 
     signal(SIGCHLD, handler);
     signal(SIGWINCH, handler);
 
-    madtty_initialize();
+    setlocale(LC_ALL, "");
+    initscr();
+    start_color();
+    noecho();
+    raw();
+    nodelay(stdscr, TRUE);
+    keypad(stdscr, TRUE);
+    curs_set(0);
+    ESCDELAY=50;
+    madtty_init_vt100_graphics();
+    madtty_init_colors();
     getmaxyx(stdscr, screen_h, screen_w);
 
     /* create a window with a frame */
@@ -52,22 +83,23 @@ int main(void)
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
 
-        madtty_forkpty(rt, path, args);
+        madtty_forkpty(rt, path, args, &pty);
     }
 
     /* keep reading keypresses from the user and passing them to the terminal;
      * also, redraw the terminal to the window at each iteration */
+    gettimeofday(&next, NULL);
     while (!getout) {
+        struct timeval tv = { 0, 1000 * 1000 / 100 };
         fd_set rfds;
-        struct timeval tv = { 0, 1000 * 1000 / 50 };
         int ch;
 
         FD_ZERO(&rfds);
         FD_SET(0, &rfds);
-        FD_SET(rt->pty, &rfds);
+        FD_SET(pty, &rfds);
 
-        if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
-            if (FD_ISSET(rt->pty, &rfds)) {
+        if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) {
+            if (FD_ISSET(pty, &rfds)) {
                 madtty_process(rt);
                 dirty = 1;
             }
@@ -103,10 +135,12 @@ int main(void)
             dirty = 1;
         }
 
-        if (dirty) {
+        gettimeofday(&tv, NULL);
+        if (dirty && is_expired(tv, next)) {
             madtty_draw(rt, term_win, 0, 0);
             wrefresh(term_win);
             dirty = 0;
+            next = timeval_add(tv, slice);
         }
     }