]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/savescreen.c
ncurses 6.0 - patch 20180106
[ncurses.git] / test / savescreen.c
1 /****************************************************************************
2  * Copyright (c) 2007-2017,2018 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: savescreen.c,v 1.50 2018/01/07 02:24:03 tom Exp $
30  *
31  * Demonstrate save/restore functions from the curses library.
32  * Thomas Dickey - 2007/7/14
33  */
34
35 #include <test.priv.h>
36 #include <popup_msg.h>
37
38 #if HAVE_SCR_DUMP
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
45 # include <time.h>
46 #else
47 # if HAVE_SYS_TIME_H
48 #  include <sys/time.h>
49 # else
50 #  include <time.h>
51 # endif
52 #endif
53
54 #if defined(__hpux)
55 #define MyMarker 'X'
56 #else
57 #define MyMarker ACS_DIAMOND
58 #endif
59
60 #define MAX_ANSI 8
61
62 static bool use_init = FALSE;
63 static bool keep_dumps = FALSE;
64
65 #if USE_WIDEC_SUPPORT
66 /* In HPUX curses, cchar_t is opaque; other implementations are not */
67 static wchar_t
68 BaseChar(cchar_t data)
69 {
70     wchar_t my_wchar[CCHARW_MAX];
71     attr_t my_attr;
72     short my_pair;
73     getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
74     return my_wchar[0];
75 }
76 #endif
77
78 static int
79 fexists(const char *name)
80 {
81     struct stat sb;
82     return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
83 }
84
85 static void
86 setup_next(void)
87 {
88     curs_set(1);
89     reset_shell_mode();
90 }
91
92 static void
93 cleanup(char *files[])
94 {
95     int n;
96
97     if (!keep_dumps) {
98         for (n = 0; files[n] != 0; ++n) {
99             unlink(files[n]);
100         }
101     }
102 }
103
104 static int
105 load_screen(char *filename)
106 {
107     int result;
108
109     if (use_init) {
110         if ((result = scr_init(filename)) != ERR)
111             result = scr_restore(filename);
112     } else {
113         result = scr_set(filename);
114     }
115     return result;
116 }
117
118 /*
119  * scr_restore() or scr_set() operates on curscr.  If we read a character using
120  * getch() that will refresh stdscr, wiping out the result.  To avoid that,
121  * copy the data back from curscr to stdscr.
122  */
123 static void
124 after_load(void)
125 {
126     overwrite(curscr, stdscr);
127     doupdate();
128 }
129
130 static void
131 show_what(int color, int which, int last)
132 {
133     int y, x, n;
134     time_t now;
135     char *mytime;
136
137     getyx(stdscr, y, x);
138
139     move(0, 0);
140     printw("Color %d.  Saved %d of %d (? for help)", color, which, last + 1);
141
142     now = time((time_t *) 0);
143     mytime = ctime(&now);
144     for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
145         if (isspace(UChar(mytime[n]))) {
146             mytime[n] = '\0';
147         } else {
148             break;
149         }
150     }
151     mvprintw(0, (COLS - n - 2), " %s", mytime);
152
153     move(y, x);
154
155     refresh();
156 }
157
158 static int
159 get_command(int color, int which, int last)
160 {
161     int ch;
162
163     timeout(50);
164
165     do {
166         show_what(color, which, last);
167         ch = getch();
168     } while (ch == ERR);
169
170     return ch;
171 }
172
173 static int
174 dump_screen(char **files, int color, int which, int last, bool use_colors)
175 {
176 #if USE_WIDEC_SUPPORT
177     cchar_t mycc;
178     int myxx;
179 #endif
180     char *filename = files[which];
181     bool dumped = FALSE;
182
183     if (filename != 0) {
184         dumped = TRUE;
185         show_what(color, ++which, last);
186         if (scr_dump(filename) == ERR) {
187             endwin();
188             printf("Cannot write screen-dump %s\n", filename);
189             cleanup(files);
190             ExitProgram(EXIT_SUCCESS);
191         }
192         if (use_colors) {
193             int cx, cy;
194             int pair = 1 + (which % MAX_ANSI);
195             /*
196              * Change the background color, to make it more obvious.  But that
197              * changes the existing text-color.  Copy the old values from the
198              * currently displayed screen.
199              */
200             bkgd((chtype) COLOR_PAIR(pair));
201             for (cy = 1; cy < LINES; ++cy) {
202                 for (cx = 0; cx < COLS; ++cx) {
203                     wmove(curscr, cy, cx);
204                     wmove(stdscr, cy, cx);
205 #if USE_WIDEC_SUPPORT
206                     if (win_wch(curscr, &mycc) != ERR) {
207                         myxx = wcwidth(BaseChar(mycc));
208                         if (myxx > 0) {
209                             wadd_wchnstr(stdscr, &mycc, 1);
210                             cx += (myxx - 1);
211                         }
212                     }
213 #else
214                     waddch(stdscr, winch(curscr));
215 #endif
216                 }
217             }
218         }
219     }
220     return dumped;
221 }
222
223 static void
224 editor_help(void)
225 {
226     static const char *msgs[] =
227     {
228         "You are now in the screen-editor, which allows you to make some",
229         "lines on the screen, as well as save copies of the screen to a",
230         "temporary file",
231         "",
232         "Keys:",
233         "   q           quit",
234         "   n           run the screen-loader to show the saved screens",
235         "   <space>     dump a screen",
236         "",
237         "   a           toggle between '#' and graphic symbol for drawing",
238         "   c           change color drawn by line to next in palette",
239         "   h,j,k,l or arrows to move around the screen, drawing",
240         0
241     };
242     popup_msg(stdscr, msgs);
243 }
244
245 static void
246 replay_help(void)
247 {
248     static const char *msgs[] =
249     {
250         "You are now in the screen-loader, which allows you to view",
251         "the dumped/restored screens.",
252         "",
253         "Keys:",
254         "   q           quit",
255         "   <space>     load the next screen",
256         "   <backspace> load the previous screen",
257         0
258     };
259     popup_msg(stdscr, msgs);
260 }
261
262 static void
263 usage(void)
264 {
265     static const char *msg[] =
266     {
267         "Usage: savescreen [-r] files",
268         "",
269         "Options:",
270         " -f file  fill/initialize screen using text from this file",
271         " -i       use scr_init/scr_restore rather than scr_set",
272         " -k       keep the restored dump-files rather than removing them",
273         " -r       replay the screen-dump files"
274     };
275     unsigned n;
276     for (n = 0; n < SIZEOF(msg); ++n) {
277         fprintf(stderr, "%s\n", msg[n]);
278     }
279     ExitProgram(EXIT_FAILURE);
280 }
281
282 int
283 main(int argc, char *argv[])
284 {
285     int ch;
286     int which = 0;
287     int last;
288     bool use_colors = FALSE;
289     bool replaying = FALSE;
290     bool done = FALSE;
291     char **files;
292     char *fill_by = 0;
293 #if USE_WIDEC_SUPPORT
294     int r, g, b;
295     int cube = 0;
296     int cube0 = 16;
297     int cube1;
298     cchar_t mycc;
299     static const wchar_t mywc[2] =
300     {L'#', 0};
301     bool using_rgb = FALSE;
302 #endif
303
304     setlocale(LC_ALL, "");
305
306     while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
307         switch (ch) {
308         case 'f':
309             fill_by = optarg;
310             break;
311         case 'i':
312             use_init = TRUE;
313             break;
314         case 'k':
315             keep_dumps = TRUE;
316             break;
317         case 'r':
318             replaying = TRUE;
319             break;
320         default:
321             usage();
322             break;
323         }
324     }
325
326     files = argv + optind;
327     last = argc - optind - 1;
328
329     if (replaying) {
330         while (last >= 0 && !fexists(files[last]))
331             --last;
332     }
333
334     initscr();
335     cbreak();
336     noecho();
337     keypad(stdscr, TRUE);
338     curs_set(0);
339
340     if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
341         static const struct {
342             int fg, bg;
343         } table[MAX_ANSI] = {
344 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
345             DATA(RED, WHITE),
346                 DATA(GREEN, WHITE),
347                 DATA(YELLOW, BLACK),
348                 DATA(BLUE, WHITE),
349                 DATA(MAGENTA, WHITE),
350                 DATA(MAGENTA, BLACK),
351                 DATA(CYAN, WHITE),
352                 DATA(CYAN, BLACK),
353 #undef DATA
354         };
355         int n;
356         int pair = 1;
357
358         use_colors = TRUE;
359         /*
360          * Discounting color-pair 0 (no color), make the next 8 color pairs
361          * useful for leaving a visually distinct trail of characters on the
362          * screen.
363          */
364         for (n = 0; n < MAX_ANSI; ++n) {
365             init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
366         }
367         /*
368          * After that, use color pairs for constructing a test-pattern, e.g.,
369          * imitating xterm's scripts.
370          */
371         if (fill_by == 0) {
372             if (COLORS <= 256) {
373                 for (n = 0; n < COLORS; ++n)
374                     init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
375             }
376 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
377             else if (tigetflag("RGB") > 0) {
378                 int rows = LINES - 1;
379                 int cols = COLS - 1;
380                 /* FIXME: test all 3 types of capability */
381                 int b_max = 255;
382                 int r_max = 255;
383                 int g_max = 255;
384                 int b_delta = (b_max / rows);
385                 int r_delta = (r_max / cols);
386                 int g_delta = (g_max / cols);
387                 int row = 0;
388
389                 b = 0;
390                 using_rgb = TRUE;
391                 while (row++ < rows) {
392                     int col = 0;
393                     r = 0;
394                     g = g_max;
395                     while (col++ < cols) {
396                         int color = (((r * (g_max + 1)) + g) * (b_max + 1)
397                                      + b + MAX_ANSI);
398 #if HAVE_INIT_EXTENDED_COLOR
399                         init_extended_pair(pair, color, color);
400 #else
401                         init_pair(pair, color, color);
402 #endif
403                         pair++;
404                         r += r_delta;
405                         g -= g_delta;
406                     }
407                     b += b_delta;
408                 }
409             }
410 #endif
411         }
412         if ((fill_by == 0) && !replaying) {
413             /*
414              * Originally (before wide-characters) ncurses supported 16 colors.
415              */
416             if (COLORS >= 16 && COLORS <= 256) {
417                 mvprintw(2, 0, "System colors:\n");
418                 for (n = 0; n < 16; ++n) {
419                     pair = n + MAX_ANSI;
420                     addch(' ' | COLOR_PAIR(pair));
421                     addch(' ' | COLOR_PAIR(pair));
422                     if (((n + 1) % 8) == 0)
423                         addch('\n');
424                 }
425             }
426             /*
427              * Even with ncurses, you need wide-character support to have more
428              * than 16 colors.
429              */
430 #if USE_WIDEC_SUPPORT
431             if (COLORS == 88) {
432                 cube = 4;
433             } else if (COLORS == 256) {
434                 cube = 6;
435             }
436             if (cube != 0) {
437                 cube0 = 16;
438                 cube1 = cube0 + (cube * cube * cube);
439
440                 addch('\n');
441                 printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
442                 for (g = 0; g < cube; g++) {
443                     for (r = 0; r < cube; r++) {
444                         for (b = 0; b < cube; b++) {
445                             pair = MAX_ANSI
446                                 + 16
447                                 + (r * cube * cube) + (g * cube) + b;
448                             setcchar(&mycc, mywc, 0, (short) pair, NULL);
449                             add_wch(&mycc);
450                             add_wch(&mycc);
451                         }
452                         addch(' ');
453                     }
454                     addch('\n');
455                 }
456                 addch('\n');
457                 printw("Grayscale ramp:\n");
458                 for (n = cube1; n < COLORS; ++n) {
459                     pair = n + MAX_ANSI;
460                     setcchar(&mycc, mywc, 0, (short) pair, NULL);
461                     add_wch(&mycc);
462                     add_wch(&mycc);
463                 }
464             } else if ((COLORS > 256) && using_rgb) {
465                 int rows = LINES - 1;
466                 int cols = COLS - 1;
467                 int row = 0;
468
469                 b = 0;
470                 pair = MAX_ANSI;
471                 while (row++ < rows) {
472                     int col = 0;
473                     while (col++ < cols) {
474                         setcchar(&mycc, mywc, 0, (short) pair, &pair);
475                         add_wch(&mycc);
476                         ++pair;
477                     }
478                     addch('\n');
479                 }
480                 addch('\n');
481             }
482 #endif
483         }
484     }
485
486     if (fill_by != 0) {
487         FILE *fp = fopen(fill_by, "r");
488         if (fp != 0) {
489             bool filled = FALSE;
490             move(1, 0);
491             while ((ch = fgetc(fp)) != EOF) {
492                 if (addch(UChar(ch)) == ERR) {
493                     filled = TRUE;
494                     break;
495                 }
496             }
497             fclose(fp);
498             if (!filled) {
499                 while (addch(' ') != ERR) {
500                     ;
501                 }
502             }
503             move(0, 0);
504         } else {
505             exit_curses();
506             fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
507             ExitProgram(EXIT_FAILURE);
508         }
509     }
510
511     if (replaying) {
512
513         /*
514          * Use the last file as the initial/current screen.
515          */
516         if (last < 0) {
517             exit_curses();
518             printf("No screen-dumps given\n");
519             ExitProgram(EXIT_FAILURE);
520         }
521
522         which = last;
523         if (load_screen(files[which]) == ERR) {
524             exit_curses();
525             printf("Cannot load screen-dump %s\n", files[which]);
526             ExitProgram(EXIT_FAILURE);
527         }
528         after_load();
529
530         while (!done && (ch = getch()) != ERR) {
531             switch (ch) {
532             case 'n':
533                 /*
534                  * If we got a "next" here, skip to the final screen before
535                  * moving to the next process.
536                  */
537                 setup_next();
538                 which = last;
539                 done = TRUE;
540                 break;
541             case 'q':
542                 cleanup(files);
543                 done = TRUE;
544                 break;
545             case KEY_BACKSPACE:
546             case '\b':
547                 if (--which < 0)
548                     which = last;
549                 break;
550             case ' ':
551                 if (++which > last)
552                     which = 0;
553                 break;
554             case HELP_KEY_1:
555                 replay_help();
556                 break;
557             default:
558                 beep();
559                 continue;
560             }
561
562             if (ch == 'q') {
563                 ;
564             } else if (scr_restore(files[which]) == ERR) {
565                 endwin();
566                 printf("Cannot load screen-dump %s\n", files[which]);
567                 cleanup(files);
568                 ExitProgram(EXIT_FAILURE);
569             } else {
570                 wrefresh(curscr);
571             }
572         }
573         endwin();
574     } else {
575         int y = 0;
576         int x = 0;
577         int color = 0;
578         int altchars = 0;
579         bool dirty = use_colors || (fill_by != 0);
580
581         while (!done) {
582             switch (get_command(color, which, last)) {
583             case 'n':
584                 if (dirty && files[which]) {
585                     dump_screen(files, color, which, last, use_colors);
586                 }
587                 setup_next();
588                 done = TRUE;
589                 break;
590             case 'q':
591                 cleanup(files);
592                 done = TRUE;
593                 break;
594             case ' ':
595                 if (dump_screen(files, color, which, last, use_colors)) {
596                     which = (which + 1) % MAX_ANSI;
597                     dirty = FALSE;
598                 } else {
599                     setup_next();
600                     done = TRUE;
601                 }
602                 break;
603             case KEY_LEFT:
604             case 'h':
605                 if (--x < 0)
606                     x = COLS - 1;
607                 break;
608             case KEY_DOWN:
609             case 'j':
610                 if (++y >= LINES)
611                     y = 1;
612                 break;
613             case KEY_UP:
614             case 'k':
615                 if (--y < 1)
616                     y = LINES - 1;
617                 break;
618             case KEY_RIGHT:
619             case 'l':
620                 if (++x >= COLS)
621                     x = 0;
622                 break;
623             case 'a':
624                 altchars = !altchars;
625                 break;
626             case 'c':
627                 if (use_colors) {
628                     color = (color + 1) % MAX_ANSI;
629                 }
630                 break;
631             case HELP_KEY_1:
632                 editor_help();
633                 break;
634             default:
635                 beep();
636                 continue;
637             }
638             if (!done) {
639                 chtype attr = A_REVERSE;
640                 chtype ch2 = (altchars ? MyMarker : '#');
641                 if (use_colors) {
642                     attr |= COLOR_PAIR(color);
643                 }
644                 move(y, x);
645                 AddCh(ch2 | attr);
646                 move(y, x);
647                 dirty = TRUE;
648             }
649         }
650         endwin();
651     }
652     ExitProgram(EXIT_SUCCESS);
653 }
654
655 #else
656 int
657 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
658 {
659     printf("This program requires the screen-dump functions\n");
660     ExitProgram(EXIT_FAILURE);
661 }
662 #endif