Detect screen dimensions _before_ attaching to the server.
[citadel.git] / citadel / textclient / screen.c
index 67cd2f506e56890c4637ff79552f6dff544464ff..c79d693fd8e1599fba5e534fd5b816a83a96640e 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdarg.h>
 #include <ctype.h>
 #include <sys/types.h>
+#include <sys/ioctl.h>
 #include "sysdep.h"
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
@@ -44,17 +45,39 @@ char status_line[1024] = "     ";
 /* the default paginator prompt will be replaced by the server's prompt when we learn it */
 char *moreprompt = " -- more -- ";
 
-extern int screenheight;
-extern int screenwidth;
+int screenheight = 24;
+int screenwidth = 80;
 int lines_printed = 0;
 int cols_printed = 0;
 
 extern int rc_ansi_color;
 extern int rc_prompt_control;
-extern void check_screen_dims(void);
-
 void do_keepalive(void);
 
+/*
+ * Attempt to discover the screen dimensions. 
+ * WARNING: This is sometimes called from a signal handler.
+ */
+void check_screen_dims(void)
+{
+#ifdef TIOCGWINSZ
+       struct {
+               unsigned short height;  /* rows */
+               unsigned short width;   /* columns */
+               unsigned short xpixels;
+               unsigned short ypixels; /* pixels */
+       } xwinsz;
+
+       if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
+               if (xwinsz.height)
+                       screenheight = (int) xwinsz.height;
+               if (xwinsz.width)
+                       screenwidth = (int) xwinsz.width;
+       }
+#endif
+}
+
+
 /*
  * Initialize the screen
  */
@@ -222,7 +245,6 @@ RETSIGTYPE scr_winch(int signum)
        /* if we receive this signal, we must be running
         * in a terminal that supports resizing.
         */
-       have_xterm = 1;
        caught_sigwinch = 1;
        check_screen_dims();
        signal(SIGWINCH, scr_winch);