1 /****************************************************************************
2 * Copyright (c) 2007-2018,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: savescreen.c,v 1.55 2019/01/20 15:59:28 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
36 #include <test.priv.h>
37 #include <popup_msg.h>
38 #include <parse_rgb.h>
42 #include <sys/types.h>
48 #define MyMarker ACS_DIAMOND
53 static bool use_init = FALSE;
54 static bool keep_dumps = FALSE;
57 /* In HPUX curses, cchar_t is opaque; other implementations are not */
59 BaseChar(cchar_t data)
61 wchar_t my_wchar[CCHARW_MAX];
65 if (getcchar(&data, my_wchar, &my_attr, &my_pair, NULL) == OK)
72 fexists(const char *name)
75 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
86 cleanup(char *files[])
91 for (n = 0; files[n] != 0; ++n) {
98 load_screen(char *filename)
103 if ((result = scr_init(filename)) != ERR)
104 result = scr_restore(filename);
106 result = scr_set(filename);
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.
119 overwrite(curscr, stdscr);
124 show_what(int color, int which, int last)
133 printw("Color %d. Saved %d of %d (? for help)", color, which, last + 1);
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]))) {
144 mvprintw(0, (COLS - n - 2), " %s", mytime);
152 get_command(int color, int which, int last)
159 show_what(color, which, last);
167 dump_screen(char **files, int color, int which, int last, bool use_colors)
169 #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 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",
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"
269 for (n = 0; n < SIZEOF(msg); ++n) {
270 fprintf(stderr, "%s\n", msg[n]);
272 ExitProgram(EXIT_FAILURE);
276 main(int argc, char *argv[])
281 bool use_colors = FALSE;
282 bool replaying = FALSE;
286 #if USE_WIDEC_SUPPORT
292 static const wchar_t mywc[2] =
294 bool using_rgb = FALSE;
297 setlocale(LC_ALL, "");
299 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
319 files = argv + optind;
320 last = argc - optind - 1;
323 while (last >= 0 && !fexists(files[last]))
330 keypad(stdscr, TRUE);
333 if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
334 static const struct {
336 } table[MAX_ANSI] = {
337 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
342 DATA(MAGENTA, WHITE),
343 DATA(MAGENTA, BLACK),
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
357 for (n = 0; n < MAX_ANSI; ++n) {
358 init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
361 * After that, use color pairs for constructing a test-pattern, e.g.,
362 * imitating xterm's scripts.
366 for (n = 0; n < COLORS; ++n)
367 init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
369 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
371 int r_max, g_max, b_max;
373 if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
374 int rows = LINES - 1;
376 int b_delta = (b_max / rows);
377 int r_delta = (r_max / cols);
378 int g_delta = (g_max / cols);
383 while (row++ < rows) {
387 while (col++ < cols) {
388 int color = (((r * (g_max + 1)) + g) * (b_max + 1)
390 #if USE_EXTENDED_COLOR
391 init_extended_pair(pair, color, color);
393 init_pair(pair, color, color);
405 if ((fill_by == 0) && !replaying) {
407 * Originally (before wide-characters) ncurses supported 16 colors.
409 if (COLORS >= 16 && COLORS <= 256) {
410 mvprintw(2, 0, "System colors:\n");
411 for (n = 0; n < 16; ++n) {
413 addch((chtype) (' ' | COLOR_PAIR(pair)));
414 addch((chtype) (' ' | COLOR_PAIR(pair)));
415 if (((n + 1) % 8) == 0)
420 * Even with ncurses, you need wide-character support to have more
423 #if USE_WIDEC_SUPPORT
426 } else if (COLORS == 256) {
431 cube1 = cube0 + (cube * cube * cube);
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++) {
440 + (r * cube * cube) + (g * cube) + b;
441 setcchar(&mycc, mywc, 0, (short) pair, NULL);
450 printw("Grayscale ramp:\n");
451 for (n = cube1; n < COLORS; ++n) {
453 setcchar(&mycc, mywc, 0, (short) pair, NULL);
457 } else if ((COLORS > 256) && using_rgb) {
458 int rows = LINES - 1;
464 while (row++ < rows) {
466 while (col++ < cols) {
467 setcchar(&mycc, mywc, 0, (short) pair, &pair);
480 FILE *fp = fopen(fill_by, "r");
484 while ((ch = fgetc(fp)) != EOF) {
485 if (addch(UChar(ch)) == ERR) {
492 while (addch(' ') != ERR) {
499 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
500 ExitProgram(EXIT_FAILURE);
507 * Use the last file as the initial/current screen.
511 printf("No screen-dumps given\n");
512 ExitProgram(EXIT_FAILURE);
516 if (load_screen(files[which]) == ERR) {
518 printf("Cannot load screen-dump %s\n", files[which]);
519 ExitProgram(EXIT_FAILURE);
523 while (!done && (ch = getch()) != ERR) {
527 * If we got a "next" here, skip to the final screen before
528 * moving to the next process.
557 } else if (scr_restore(files[which]) == ERR) {
559 printf("Cannot load screen-dump %s\n", files[which]);
561 ExitProgram(EXIT_FAILURE);
572 bool dirty = use_colors || (fill_by != 0);
575 switch (get_command(color, which, last)) {
577 if (dirty && files[which]) {
578 dump_screen(files, color, which, last, use_colors);
588 if (dump_screen(files, color, which, last, use_colors)) {
589 which = (which + 1) % MAX_ANSI;
617 altchars = !altchars;
621 color = (color + 1) % MAX_ANSI;
632 chtype attr = A_REVERSE;
633 chtype ch2 = (altchars ? MyMarker : '#');
635 attr |= (chtype) COLOR_PAIR(color);
645 ExitProgram(EXIT_SUCCESS);
650 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
652 printf("This program requires the screen-dump functions\n");
653 ExitProgram(EXIT_FAILURE);