2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
23 #include <sys/ioctl.h>
24 #include <glib/gstdio.h>
28 static volatile sig_atomic_t recv_sigwinch
= 0;
29 static gsize max_width
= 0;
31 static void _sigwinch(int signo
)
36 void sigwinch_setup(struct sigaction
*san
, struct sigaction
*sao
)
38 san
->sa_handler
= _sigwinch
;
39 sigemptyset(&san
->sa_mask
);
42 sigaction(SIGWINCH
, NULL
, sao
);
44 if (sao
->sa_handler
!= SIG_IGN
)
45 sigaction(SIGWINCH
, san
, NULL
);
48 void sigwinch_reset(const struct sigaction
*sao
)
50 if (sao
->sa_handler
!= SIG_IGN
)
51 sigaction(SIGWINCH
, sao
, NULL
);
54 static gsize
_get_term_width()
57 if (ioctl(fileno(stderr
), TIOCGWINSZ
, &w
) <0)
62 gsize
sigwinch_term_spaceleft(const gsize len
)
64 static const gsize default_term_width
= 80;
65 if (max_width
==0 || recv_sigwinch
==1)
67 max_width
= _get_term_width();
69 max_width
= default_term_width
;
71 return (max_width
- len
-1);
74 /* vim: set ts=2 sw=2 tw=72 expandtab: */