]> code.citadel.org Git - citadel.git/blob - citadel/screen.c
I made some changes that I hope will help the lagging input problem
[citadel.git] / citadel / screen.c
1 /* $Id$ */
2
3 /*
4  * Handle full-screen curses stuff
5  */
6
7 #include "sysdep.h"
8 #ifdef HAVE_CURSES_H
9 #include <curses.h>
10 #endif
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #ifdef VW_PRINTW_IN_CURSES
17 #define _vwprintw vw_printw
18 #else
19 /* Ancient curses implementations, this needs testing. Anybody got XENIX? */
20 #define _vwprintw vwprintw
21 #endif
22 #ifndef HAVE_SNPRINTF
23 #include "snprintf.h"
24 #endif
25 #include "citadel.h"
26 #include "commands.h"
27 #include "screen.h"
28
29 #ifdef HAVE_CURSES_H
30 static SCREEN *myscreen = NULL;
31 static WINDOW *mainwindow = NULL;
32 static WINDOW *statuswindow = NULL;
33
34 char rc_screen;
35 char arg_screen;
36
37 extern int screenheight;
38 extern int screenwidth;
39 extern int rc_ansi_color;
40 extern void check_screen_dims(void);
41 #endif
42
43 void do_keepalive(void);
44
45
46 /*
47  * status_line() is a convenience function for writing a "typical"
48  * status line to the window.
49  */
50 void status_line(const char *humannode, const char *bbs_city,
51                  const char *room_name, int secure, int newmailcount)
52 {
53 #ifdef HAVE_CURSES_H
54         if (statuswindow) {
55                 if (secure) {
56                         sln_printf("Encrypted ");
57                         waddch(statuswindow, ACS_VLINE);
58                         waddch(statuswindow, ' ');
59                 }
60                 if (room_name)
61                         sln_printf("%s on ", room_name);
62                 if (humannode)
63                         sln_printf("%s ", humannode);
64                 if (newmailcount > -1) {
65                         waddch(statuswindow, ACS_VLINE);
66                         sln_printf(" Mail: %d new ", newmailcount);
67                 }
68                 sln_printf("\n");
69         }
70 #endif /* HAVE_CURSES_H */
71 }
72
73
74 /*
75  * Initialize the screen.  If newterm() fails, myscreen will be NULL and
76  * further handlers will assume we should be in line mode.
77  */
78 void screen_new(void)
79 {
80 #ifdef HAVE_CURSES_H
81         if (arg_screen != RC_NO && rc_screen != RC_NO)
82                 myscreen = newterm(NULL, stdout, stdin);
83         if (myscreen) {
84                 cbreak();
85                 noecho();
86                 nonl();
87                 intrflush(stdscr, FALSE);
88                 keypad(stdscr, TRUE);
89                 /* Setup all our colors */
90                 start_color();
91                 if (rc_ansi_color)
92                         enable_color = 1;
93                 init_pair(1+DIM_BLACK, COLOR_BLACK, COLOR_BLACK);
94                 init_pair(1+DIM_RED, COLOR_RED, COLOR_BLACK);
95                 init_pair(1+DIM_GREEN, COLOR_GREEN, COLOR_BLACK);
96                 init_pair(1+DIM_YELLOW, COLOR_YELLOW, COLOR_BLACK);
97                 init_pair(1+DIM_BLUE, COLOR_BLUE, COLOR_BLACK);
98                 init_pair(1+DIM_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
99                 init_pair(1+DIM_CYAN, COLOR_CYAN, COLOR_BLACK);
100                 init_pair(1+DIM_WHITE, COLOR_WHITE, COLOR_BLACK);
101                 init_pair(17, COLOR_WHITE, COLOR_BLUE);
102         } else
103 #endif /* HAVE_CURSES_H */
104         {
105                 send_ansi_detect();
106                 look_for_ansi();
107                 cls(0);
108                 color(1+DIM_WHITE);
109         }
110         screen_set();
111         windows_new();
112 }
113
114
115 /*
116  * Kill the screen completely (used at exit).  It is safe to call this
117  * function more than once.
118  */
119 void screen_delete(void)
120 {
121         windows_delete();
122         screen_reset();
123 #ifdef HAVE_CURSES_H
124         if (myscreen)
125                 delscreen(myscreen);
126         myscreen = NULL;
127 #endif
128 }
129
130
131 /*
132  * Set screen/IO parameters, e.g. at start of program or return from external
133  * program run.
134  */
135 int screen_set(void)
136 {
137 #ifdef HAVE_CURSES_H
138         if (myscreen) {
139                 set_term(myscreen);
140                 wrefresh(curscr);
141                 return 1;
142         }
143 #endif /* HAVE_CURSES_H */
144         return 0;
145 }
146
147
148 /*
149  * Reset screen/IO parameters, e.g. at exit or fork of external program.
150  */
151 int screen_reset(void)
152 {
153 #ifdef HAVE_CURSES_H
154         if (myscreen) {
155                 endwin();
156                 return 1;
157         }
158 #endif /* HAVE_CURSES_H */
159         return 0;
160 }
161
162
163 /*
164  * scr_printf() outputs to the main window (or screen if not in curses)
165  */
166 int scr_printf(char *fmt, ...)
167 {
168         va_list ap;
169         register int retval;
170
171         va_start(ap, fmt);
172 #ifdef HAVE_CURSES_H
173         if (mainwindow) {
174                 retval = _vwprintw(mainwindow, fmt, ap);
175         } else
176 #endif
177                 retval = vprintf(fmt, ap);
178         va_end(ap);
179         return retval;
180 }
181
182
183 /*
184  * err_printf() outputs to error status window (or stderr if not in curses)
185  */
186 int err_printf(char *fmt, ...)
187 {
188         va_list ap;
189         register int retval;
190
191         va_start(ap, fmt);
192 #ifdef HAVE_CURSES_H
193         if (mainwindow) {               /* FIXME: direct to error window */
194                 retval = _vwprintw(mainwindow, fmt, ap);
195                 if (fmt[strlen(fmt) - 1] == '\n')
196                         wrefresh(mainwindow);
197         } else
198 #endif
199                 retval = vfprintf(stderr, fmt, ap);
200         va_end(ap);
201         return retval;
202 }
203
204
205 /*
206  * sln_printf() outputs to error status window (or stderr if not in curses)
207  */
208 int sln_printf(char *fmt, ...)
209 {
210         va_list ap;
211         register int retval;
212 #ifdef HAVE_CURSES_H
213         static char buf[4096];
214 #endif
215
216         va_start(ap, fmt);
217 #ifdef HAVE_CURSES_H
218         if (statuswindow) {
219                 register char *i;
220                 
221                 retval = vsnprintf(buf, 4096, fmt, ap);
222                 for (i = buf; *i; i++) {
223                         if (*i == '\r' || *i == '\n')
224                                 wclrtoeol(statuswindow);
225                         sln_putc(*i);
226                         if (*i == '\r' || *i == '\n') {
227                                 wrefresh(statuswindow);
228                                 mvwinch(statuswindow, 0, 0);
229                         }
230                 }
231         } else
232 #endif
233                 retval = vprintf(fmt, ap);
234         va_end(ap);
235         return retval;
236 }
237
238
239 /*
240  * sln_printf_if() outputs to status window, no output if not in curses
241  */
242 int sln_printf_if(char *fmt, ...)
243 {
244         register int retval = 1;
245 #ifdef HAVE_CURSES_H
246         static char buf[4096];
247         va_list ap;
248
249         va_start(ap, fmt);
250         if (statuswindow) {
251                 register char *i;
252                 
253                 retval = vsnprintf(buf, 4096, fmt, ap);
254                 for (i = buf; *i; i++) {
255                         if (*i == '\r' || *i == '\n')
256                                 wclrtoeol(statuswindow);
257                         sln_putc(*i);
258                         if (*i == '\r' || *i == '\n') {
259                                 wrefresh(statuswindow);
260                                 mvwinch(statuswindow, 0, 0);
261                         }
262                 }
263         }
264         va_end(ap);
265 #endif
266         return retval;
267 }
268
269
270 int scr_getc(void)
271 {
272 #ifdef HAVE_CURSES_H
273     /* This looks scary, the program will hang if mainwindow is null */
274         if (mainwindow)
275                 return wgetch(mainwindow);
276 #endif
277         return getchar();
278 }
279 int scr_blockread(void)
280   {
281     int a;
282     wtimeout(mainwindow, S_KEEPALIVE); 
283     while (1)
284       {
285         do_keepalive();
286         a = wgetch(mainwindow); /* will block for food */
287         if (a != ERR)
288           break;
289         /* a = scr_getc(); */
290       }
291     return a;
292   }
293
294 /*
295  * scr_putc() outputs a single character
296  */
297 int scr_putc(int c)
298 {
299 #ifdef HAVE_CURSES_H
300         if (mainwindow)
301                 return ((waddch(mainwindow, c) == OK) ? c : EOF);
302 #endif
303         return putc(c, stdout);
304 }
305
306
307 int sln_putc(int c)
308 {
309 #ifdef HAVE_CURSES_H
310         if (statuswindow)
311                 return ((waddch(statuswindow, c) == OK) ? c : EOF);
312 #endif
313         return putc(c, stdout);
314 }
315
316
317 int sln_putc_if(int c)
318 {
319 #ifdef HAVE_CURSES_H
320         if (statuswindow)
321                 return ((waddch(statuswindow, c) == OK) ? c : EOF);
322 #endif
323         return 1;
324 }
325
326
327 /*
328  * scr_color() sets the window color for mainwindow
329  */
330 int scr_color(int colornum)
331 {
332 #ifdef HAVE_CURSES_H
333         if (mainwindow) {
334                 wcolor_set(mainwindow, 1 + (colornum & 7), NULL);
335                 if (colornum & 8) {
336                         wattron(mainwindow, A_BOLD);
337                 } else {
338                         wattroff(mainwindow, A_BOLD);
339                 }
340                 return 1;
341         }
342 #endif
343         return 0;
344 }
345
346
347 void scr_flush(void)
348 {
349 #ifdef HAVE_CURSES_H
350         if (mainwindow)
351                 wrefresh(mainwindow);
352         else
353 #endif
354                 fflush(stdout);
355 }
356
357
358 void err_flush(void)
359 {
360 #ifdef HAVE_CURSES_H
361         if (mainwindow)         /* FIXME: error status window needed */
362                 wrefresh(mainwindow);
363         else
364 #endif
365                 fflush(stderr);
366 }
367
368
369 void sln_flush(void)
370 {
371 #ifdef HAVE_CURSES_H
372         if (statuswindow)
373                 wrefresh(statuswindow);
374         else
375 #endif
376                 fflush(stdout);
377 }
378
379
380 int scr_set_windowsize(void)
381 {
382 #ifdef HAVE_CURSES_H
383         int y, x;
384
385         if (mainwindow) {
386                 getmaxyx(mainwindow, y, x);
387                 screenheight = y;
388                 screenwidth = x;
389                 return 1;
390         }
391 #endif /* HAVE_CURSES_H */
392         return 0;
393 }
394
395
396 /*
397  * scr_winch() handles window size changes from SIGWINCH
398  * resizes all our windows for us
399  */
400 RETSIGTYPE scr_winch(void)
401 {
402 #ifdef HAVE_CURSES_H
403         /* FIXME: not implemented */
404 #endif
405         check_screen_dims();
406 }
407
408
409 /*
410  * Initialize the window(s) we will be using.
411  */
412 void windows_new(void)
413 {
414 #ifdef HAVE_CURSES_H
415         register int x, y;
416
417         if (myscreen) {
418                 getmaxyx(stdscr, y, x);
419                 mainwindow = newwin(y - 1, x, 0, 0);
420                 screenwidth = x;
421                 screenheight = y - 1;
422                 immedok(mainwindow, FALSE);
423                 leaveok(mainwindow, FALSE);
424                 scrollok(mainwindow, TRUE);
425                 statuswindow = newwin(1, x, y - 1, 0);
426                 wbkgdset(statuswindow, COLOR_PAIR(17));
427                 werase(statuswindow);
428                 immedok(statuswindow, FALSE);
429                 leaveok(statuswindow, FALSE);
430                 scrollok(statuswindow, FALSE);
431                 wrefresh(statuswindow);
432         }
433 #else /* HAVE_CURSES_H */
434
435 #endif /* HAVE_CURSES_H */
436 }
437
438
439 /*
440  * Deinitialize the window(s) we were using (at exit).
441  */
442 void windows_delete(void)
443 {
444 #ifdef HAVE_CURSES_H
445         if (mainwindow)
446                 delwin(mainwindow);
447         mainwindow = NULL;
448         if (statuswindow)
449                 delwin(statuswindow);
450         statuswindow = NULL;
451 #else /* HAVE_CURSES_H */
452
453 #endif /* HAVE_CURSES_H */
454 }