1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 2006-2017,2018 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: savescreen.c,v 1.62 2022/12/10 23:23:27 tom Exp $
32 * Demonstrate save/restore functions from the curses library.
33 * Thomas Dickey - 2007/7/14
37 #include <test.priv.h>
38 #include <popup_msg.h>
39 #include <parse_rgb.h>
43 #include <sys/types.h>
49 #define MyMarker ACS_DIAMOND
54 static bool use_init = FALSE;
55 static bool keep_dumps = FALSE;
58 /* In HPUX curses, cchar_t is opaque; other implementations are not */
60 BaseChar(cchar_t data)
62 wchar_t my_wchar[CCHARW_MAX];
66 if (getcchar(&data, my_wchar, &my_attr, &my_pair, NULL) == OK)
73 fexists(const char *name)
76 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
87 cleanup(char *files[])
92 for (n = 0; files[n] != 0; ++n) {
99 load_screen(char *filename)
104 if ((result = scr_init(filename)) != ERR)
105 result = scr_restore(filename);
107 result = scr_set(filename);
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.
120 overwrite(curscr, stdscr);
125 show_what(int color, int which, int last)
134 printw("Color %d. Saved %d of %d (? for help)", color, which, last + 1);
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]))) {
145 mvprintw(0, (COLS - n - 2), " %s", mytime);
153 get_command(int color, int which, int last)
160 show_what(color, which, last);
168 dump_screen(char **files, int color, int which, int last, bool use_colors)
170 #if USE_WIDEC_SUPPORT
173 char *filename = files[which];
178 show_what(color, ++which, last);
179 if (scr_dump(filename) == ERR) {
181 printf("Cannot write screen-dump %s\n", filename);
183 ExitProgram(EXIT_SUCCESS);
187 int pair = 1 + (which % MAX_ANSI);
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.
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 int myxx = wcwidth(BaseChar(mycc));
202 wadd_wchnstr(stdscr, &mycc, 1);
207 waddch(stdscr, winch(curscr));
219 static const char *msgs[] =
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",
227 " n run the screen-loader to show the saved screens",
228 " <space> dump a screen",
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",
235 popup_msg(stdscr, msgs);
241 static const char *msgs[] =
243 "You are now in the screen-loader, which allows you to view",
244 "the dumped/restored screens.",
248 " <space> load the next screen",
249 " <backspace> load the previous screen",
252 popup_msg(stdscr, msgs);
258 static const char *msg[] =
260 "Usage: savescreen [-r] files"
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"
270 for (n = 0; n < SIZEOF(msg); ++n) {
271 fprintf(stderr, "%s\n", msg[n]);
273 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
280 main(int argc, char *argv[])
285 bool use_colors = FALSE;
286 bool replaying = FALSE;
290 #if USE_WIDEC_SUPPORT
292 static const wchar_t mywc[2] =
296 setlocale(LC_ALL, "");
298 while ((ch = getopt(argc, argv, OPTS_COMMON "f:ikr")) != -1) {
314 ExitProgram(EXIT_SUCCESS);
316 usage(ch == OPTS_USAGE);
321 files = argv + optind;
322 last = argc - optind - 1;
325 while (last >= 0 && !fexists(files[last]))
332 keypad(stdscr, TRUE);
335 if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
336 #if USE_WIDEC_SUPPORT
337 bool using_rgb = FALSE;
339 static const struct {
341 } table[MAX_ANSI] = {
342 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
347 DATA(MAGENTA, WHITE),
348 DATA(MAGENTA, BLACK),
358 * Discounting color-pair 0 (no color), make the next 8 color pairs
359 * useful for leaving a visually distinct trail of characters on the
362 for (n = 0; n < MAX_ANSI; ++n) {
363 init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
366 * After that, use color pairs for constructing a test-pattern, e.g.,
367 * imitating xterm's scripts.
371 for (n = 0; n < COLORS; ++n)
372 init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
374 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
376 int r_max, g_max, b_max;
378 if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
379 int rows = LINES - 1;
381 int b_delta = (b_max / rows);
382 int r_delta = (r_max / cols);
383 int g_delta = (g_max / cols);
388 while (row++ < rows) {
392 while (col++ < cols) {
393 int color = (((r * (g_max + 1)) + g) * (b_max + 1)
395 #if USE_EXTENDED_COLOR
396 init_extended_pair(pair, color, color);
398 init_pair(pair, color, color);
410 if ((fill_by == 0) && !replaying) {
411 #if USE_WIDEC_SUPPORT
415 * Originally (before wide-characters) ncurses supported 16 colors.
417 if (COLORS >= 16 && COLORS <= 256) {
418 mvprintw(2, 0, "System colors:\n");
419 for (n = 0; n < 16; ++n) {
421 addch((chtype) (' ' | COLOR_PAIR(pair)));
422 addch((chtype) (' ' | COLOR_PAIR(pair)));
423 if (((n + 1) % 8) == 0)
428 * Even with ncurses, you need wide-character support to have more
431 #if USE_WIDEC_SUPPORT
434 } else if (COLORS == 256) {
440 int cube1 = cube0 + (cube * cube * cube);
443 printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
444 for (g = 0; g < cube; g++) {
445 for (r = 0; r < cube; r++) {
446 for (b = 0; b < cube; b++) {
449 + (r * cube * cube) + (g * cube) + b;
450 setcchar(&mycc, mywc, 0, (short) pair, NULL);
459 printw("Grayscale ramp:\n");
460 for (n = cube1; n < COLORS; ++n) {
462 setcchar(&mycc, mywc, 0, (short) pair, NULL);
466 } else if ((COLORS > 256) && using_rgb) {
467 int rows = LINES - 1;
472 while (row++ < rows) {
474 while (col++ < cols) {
475 setcchar(&mycc, mywc, 0, (short) pair, &pair);
488 FILE *fp = fopen(fill_by, "r");
492 while ((ch = fgetc(fp)) != EOF) {
493 if (addch(UChar(ch)) == ERR) {
500 while (addch(' ') != ERR) {
507 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
508 ExitProgram(EXIT_FAILURE);
515 * Use the last file as the initial/current screen.
519 printf("No screen-dumps given\n");
520 ExitProgram(EXIT_FAILURE);
524 if (load_screen(files[which]) == ERR) {
526 printf("Cannot load screen-dump %s\n", files[which]);
527 ExitProgram(EXIT_FAILURE);
531 while (!done && (ch = getch()) != ERR) {
535 * If we got a "next" here, skip to the final screen before
536 * moving to the next process.
565 } else if (scr_restore(files[which]) == ERR) {
567 printf("Cannot load screen-dump %s\n", files[which]);
569 ExitProgram(EXIT_FAILURE);
580 bool dirty = use_colors || (fill_by != 0);
583 switch (get_command(color, which, last)) {
585 if (dirty && files[which]) {
586 dump_screen(files, color, which, last, use_colors);
596 if (dump_screen(files, color, which, last, use_colors)) {
597 which = (which + 1) % MAX_ANSI;
625 altchars = !altchars;
629 color = (color + 1) % MAX_ANSI;
640 chtype attr = A_REVERSE;
641 chtype ch2 = (altchars ? MyMarker : '#');
643 attr |= (chtype) COLOR_PAIR(color);
653 ExitProgram(EXIT_SUCCESS);
660 printf("This program requires the screen-dump functions\n");
661 ExitProgram(EXIT_FAILURE);