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