44fa8dfa6037e92b688778bb42b3482e3582b75a
[citadel.git] / citadel / textclient / screen.c
1 /*
2  * Screen output handling
3  */
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <stdarg.h>
11 #include <sys/types.h>
12 #include "sysdep.h"
13 #ifndef HAVE_SNPRINTF
14 #include "snprintf.h"
15 #endif
16 #include <libcitadel.h>
17 #include "citadel.h"
18 #include "citadel_ipc.h"
19 #include "citadel_decls.h"
20 #include "commands.h"
21 #include "screen.h"
22
23 char arg_screen;
24
25 extern int screenheight;
26 extern int screenwidth;
27 extern int rc_ansi_color;
28 extern void check_screen_dims(void);
29
30 void do_keepalive(void);
31
32
33 /*
34  * Initialize the screen
35  */
36 void screen_new(void)
37 {
38         send_ansi_detect();
39         look_for_ansi();
40         cls(0);
41         color(DIM_WHITE);
42 }
43
44
45 /*
46  * Kill the screen completely (used at exit).  It is safe to call this
47  * function more than once.
48  */
49 void screen_delete(void)
50 {
51         screen_reset();
52 }
53
54
55
56 /*
57  * Beep.
58  */
59 void ctdl_beep(void) {
60         putc(7, stdout);
61 }
62         
63
64
65 /*
66  * Set screen/IO parameters, e.g. at start of program or return from external
67  * program run.
68  */
69 int screen_set(void)
70 {
71         return 0;
72 }
73
74
75 /*
76  * Reset screen/IO parameters, e.g. at exit or fork of external program.
77  */
78 int screen_reset(void)
79 {
80         return 0;
81 }
82
83
84 /*
85  * scr_printf() outputs to the terminal
86  */
87 int scr_printf(char *fmt, ...)
88 {
89         va_list ap;
90         register int retval;
91
92         va_start(ap, fmt);
93         retval = vprintf(fmt, ap);
94         va_end(ap);
95         return retval;
96 }
97
98
99 /*
100  * err_printf() outputs to error status window (or stderr if not in curses)
101  */
102 int err_printf(char *fmt, ...)
103 {
104         va_list ap;
105         register int retval;
106
107         va_start(ap, fmt);
108         retval = vfprintf(stderr, fmt, ap);
109         va_end(ap);
110         return retval;
111 }
112
113
114 /*
115  * sln_printf() outputs to error status window (or stderr if not in curses)
116  */
117 int sln_printf(char *fmt, ...)
118 {
119         va_list ap;
120         register int retval;
121         va_start(ap, fmt);
122         retval = vprintf(fmt, ap);
123         va_end(ap);
124         return retval;
125 }
126
127
128 /*
129  * sln_printf_if() outputs to status window, no output if not in curses
130  */
131 int sln_printf_if(char *fmt, ...)
132 {
133         register int retval = 1;
134         return retval;
135 }
136
137
138 int scr_getc(int delay)
139 {
140         unsigned char buf;
141         buf = '\0';
142         if (!read (0, &buf, 1))
143                 logoff(NULL, 3);
144         return buf;
145 }
146
147 /*
148  * scr_putc() outputs a single character
149  */
150 int scr_putc(int c)
151 {
152         if (putc(c, stdout) == EOF)
153                 logoff(NULL, 3);
154         return c;
155 }
156
157
158 int sln_putc(int c)
159 {
160         return putc(c, stdout);
161 }
162
163
164 int sln_putc_if(int c)
165 {
166         return 1;
167 }
168
169
170 /*
171  * scr_color() sets the window color for mainwindow
172  */
173 int scr_color(int colornum)
174 {
175         return 0;
176 }
177
178
179 void scr_flush(void)
180 {
181         fflush(stdout);
182 }
183
184
185 void err_flush(void)
186 {
187                 fflush(stderr);
188 }
189
190
191 void sln_flush(void)
192 {
193         fflush(stdout);
194 }
195
196 static volatile int caught_sigwinch = 0;
197
198 /*
199  * this is not supposed to be called from a signal handler.
200  */
201 int scr_set_windowsize(CtdlIPC* ipc)
202 {
203         return 0;
204 }
205
206 /*
207  * scr_winch() handles window size changes from SIGWINCH
208  * resizes all our windows for us
209  */
210 RETSIGTYPE scr_winch(int signum)
211 {
212         /* if we receive this signal, we must be running
213          * in a terminal that supports resizing.
214          */
215         have_xterm = 1;
216         caught_sigwinch = 1;
217         check_screen_dims();
218         signal(SIGWINCH, scr_winch);
219 }