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