2 * Screen output handling
4 * Copyright (c) 1987-2016 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
26 #include <libcitadel.h>
27 #include "citadel_ipc.h"
28 #include "citadel_decls.h"
32 int enable_status_line = 0;
33 char status_line[1024] = " ";
35 /* the default paginator prompt will be replaced by the server's prompt when we learn it */
36 char *moreprompt = " -- more -- ";
38 int screenheight = 24;
40 int lines_printed = 0;
43 extern int rc_ansi_color;
44 extern int rc_prompt_control;
45 void do_keepalive(void);
48 * Attempt to discover the screen dimensions.
49 * WARNING: This is sometimes called from a signal handler.
51 void check_screen_dims(void)
55 unsigned short height; /* rows */
56 unsigned short width; /* columns */
57 unsigned short xpixels;
58 unsigned short ypixels; /* pixels */
61 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
63 screenheight = (int) xwinsz.height;
65 screenwidth = (int) xwinsz.width;
72 * Initialize the screen
87 void ctdl_beep(void) {
95 * scr_printf() outputs to the terminal
97 int scr_printf(char *fmt, ...)
99 static char outbuf[4096]; /* static for performance -- not re-entrant -- change if needed */
105 retval = vsnprintf(outbuf, sizeof outbuf, fmt, ap);
108 len = strlen(outbuf);
109 for (i=0; i<len; ++i) {
117 * Read one character from the terminal
119 int scr_getc(int delay)
126 if (!read (0, &buf, 1))
134 * Issue the paginator prompt (more / hit any key to continue)
136 void hit_any_key(void) {
141 scr_printf("%s\r", moreprompt);
144 for (a=0; a<screenwidth; ++a) {
149 if ( (rc_prompt_control == 1) || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
150 if (b == 'q' || b == 'Q' || b == 's' || b == 'S') {
153 if (b == 'n' || b == 'N') {
158 if (b==NEXT_KEY) sigcaught = SIGINT;
159 if (b==STOP_KEY) sigcaught = SIGQUIT;
164 * Output one character to the terminal
168 /* handle tabs normally */
172 } while ((cols_printed % 8) != 0);
176 /* Output the character... */
177 if (putc(c, stdout) == EOF) {
185 else if (c == '\r') {
188 else if (isprint(c)) {
190 if ((screenwidth > 0) && (cols_printed > screenwidth)) {
196 /* How many lines output before stopping for the paginator?
197 * Depends on whether we are displaying a status line.
199 int height_offset = ( ((enable_color) && (screenwidth > 0) && (enable_status_line)) ? (3) : (2) ) ;
201 /* Ok, go check it. Stop and display the paginator prompt if necessary. */
202 if ((screenheight > 0) && (lines_printed > (screenheight-height_offset))) {
214 if ((enable_color) && (screenwidth > 0) && (enable_status_line)) {
215 if (strlen(status_line) < screenwidth) {
216 memset(&status_line[strlen(status_line)], 32, screenwidth - strlen(status_line));
218 printf("\033[s\033[1;1H\033[K\033[7m");
219 fwrite(status_line, screenwidth, 1, stdout);
220 printf("\033[27m\033[u");
226 static volatile int caught_sigwinch = 0;
230 * scr_winch() handles window size changes from SIGWINCH
231 * resizes all our windows for us
233 RETSIGTYPE scr_winch(int signum)
235 /* if we receive this signal, we must be running
236 * in a terminal that supports resizing.
240 signal(SIGWINCH, scr_winch);
246 * Display a 3270-style "wait" indicator at the bottom of the screen
248 void scr_wait_indicator(int state) {
249 int sp = (screenwidth - 2);
251 if (!enable_status_line) return;
253 if (screenwidth > 0) {
257 status_line[sp] = ' ';
259 case 1: /* Waiting */
260 status_line[sp] = 'X';
262 case 2: /* Receiving */
263 status_line[sp] = '<';
265 case 3: /* Sending */
266 status_line[sp] = '>';