Handle terminal resizes. - irc - Unnamed repository; edit this file 'description' to name the repository.
(HTM) git clone git://vernunftzentrum.de/irc.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit c5b6ac6afa188bd107bdbfb96500182447368b05
(DIR) parent 646d0a762d1536fca7c19518d1e30a269b57cba5
(HTM) Author: Quentin Carbonneaux <qcarbonneaux@gmail.com>
Date: Sun, 11 Mar 2012 22:03:49 +0100
Handle terminal resizes.
The KEY_RESIZE feature of ncurses is not used since it is not possible
to detect if a KEY_RESIZE was queued by selecting on stdin. Hence, tinit
now installs a SIGWINCH handler which will set the winchg variable to 1.
I rely on the fact that select will be interrupted by the signal which
pops after a terminal resize to be able to redraw the screen
instantaneously.
tresize does all the job of resizing the three used curses windows.
Diffstat:
irc.c | 34 +++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)
---
(DIR) diff --git a/irc.c b/irc.c
@@ -2,6 +2,7 @@
*/
#include <assert.h>
#include <limits.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -28,7 +29,7 @@ enum { ChanLen = 64, LineLen = 512, MaxChans = 16, BufSz = 2048, LogSz = 4096 };
char nick[64];
char prefix[64];
-int quit;
+int quit, winchg;
int sfd; /* Server file descriptor. */
struct {
int x;
@@ -315,9 +316,16 @@ uparse(char *m)
}
static void
+sigwinch(int sig)
+{
+ if (sig) winchg=1;
+}
+
+static void
tinit(void)
{
setlocale(LC_ALL, "");
+ signal(SIGWINCH, sigwinch);
initscr();
raw();
noecho();
@@ -337,6 +345,19 @@ tinit(void)
}
static void
+tresize(void)
+{
+ winchg=0;
+ getmaxyx(stdscr, scr.y, scr.x);
+ if (scr.y<3 || scr.x<10) panic("Screen too small.");
+ wresize(scr.mw, scr.y-2, scr.x);
+ wresize(scr.iw, 1, scr.x);
+ wresize(scr.sw, 1, scr.x);
+ mvwin(scr.iw, scr.y-1, 1);
+ tredraw();
+}
+
+static void
tredraw(void)
{
struct Chan * const c=&chl[ch];
@@ -425,11 +446,6 @@ tgetch(void)
uparse(lb);
dirty=cu=len=0;
break;
- case KEY_RESIZE:
- getmaxyx(stdscr, scr.y, scr.x);
- if (scr.y<3 || scr.x<10) panic("Screen too small.");
- tredraw();
- return;
default:
if (c>CHAR_MAX || len>=BufSz) return; /* Skip other curses codes. */
memmove(&lb[cu+1], &lb[cu], len-cu);
@@ -476,7 +492,9 @@ main(void)
while (!quit) {
fd_set rfs, wfs;
int ret;
-
+
+ if (winchg)
+ tresize();
FD_ZERO(&wfs);
FD_ZERO(&rfs);
FD_SET(0, &rfs);
@@ -494,7 +512,7 @@ main(void)
}
if (FD_ISSET(sfd, &wfs)) {
int wr;
-
+
wr=write(sfd, outb, outp-outb);
if (wr<0) {
if (errno==EINTR) continue;