1 /****************************************************************************
2 * Copyright (c) 2007-2017,2018 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.53 2018/05/12 15:11:16 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
35 #include <test.priv.h>
36 #include <popup_msg.h>
37 #include <parse_rgb.h>
41 #include <sys/types.h>
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
49 # include <sys/time.h>
58 #define MyMarker ACS_DIAMOND
63 static bool use_init = FALSE;
64 static bool keep_dumps = FALSE;
67 /* In HPUX curses, cchar_t is opaque; other implementations are not */
69 BaseChar(cchar_t data)
71 wchar_t my_wchar[CCHARW_MAX];
75 if (getcchar(&data, my_wchar, &my_attr, &my_pair, NULL) == OK)
82 fexists(const char *name)
85 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
96 cleanup(char *files[])
101 for (n = 0; files[n] != 0; ++n) {
108 load_screen(char *filename)
113 if ((result = scr_init(filename)) != ERR)
114 result = scr_restore(filename);
116 result = scr_set(filename);
122 * scr_restore() or scr_set() operates on curscr. If we read a character using
123 * getch() that will refresh stdscr, wiping out the result. To avoid that,
124 * copy the data back from curscr to stdscr.
129 overwrite(curscr, stdscr);
134 show_what(int color, int which, int last)
143 printw("Color %d. Saved %d of %d (? for help)", color, which, last + 1);
145 now = time((time_t *) 0);
146 mytime = ctime(&now);
147 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
148 if (isspace(UChar(mytime[n]))) {
154 mvprintw(0, (COLS - n - 2), " %s", mytime);
162 get_command(int color, int which, int last)
169 show_what(color, which, last);
177 dump_screen(char **files, int color, int which, int last, bool use_colors)
179 #if USE_WIDEC_SUPPORT
183 char *filename = files[which];
188 show_what(color, ++which, last);
189 if (scr_dump(filename) == ERR) {
191 printf("Cannot write screen-dump %s\n", filename);
193 ExitProgram(EXIT_SUCCESS);
197 int pair = 1 + (which % MAX_ANSI);
199 * Change the background color, to make it more obvious. But that
200 * changes the existing text-color. Copy the old values from the
201 * currently displayed screen.
203 bkgd((chtype) COLOR_PAIR(pair));
204 for (cy = 1; cy < LINES; ++cy) {
205 for (cx = 0; cx < COLS; ++cx) {
206 wmove(curscr, cy, cx);
207 wmove(stdscr, cy, cx);
208 #if USE_WIDEC_SUPPORT
209 if (win_wch(curscr, &mycc) != ERR) {
210 myxx = wcwidth(BaseChar(mycc));
212 wadd_wchnstr(stdscr, &mycc, 1);
217 waddch(stdscr, winch(curscr));
229 static const char *msgs[] =
231 "You are now in the screen-editor, which allows you to make some",
232 "lines on the screen, as well as save copies of the screen to a",
237 " n run the screen-loader to show the saved screens",
238 " <space> dump a screen",
240 " a toggle between '#' and graphic symbol for drawing",
241 " c change color drawn by line to next in palette",
242 " h,j,k,l or arrows to move around the screen, drawing",
245 popup_msg(stdscr, msgs);
251 static const char *msgs[] =
253 "You are now in the screen-loader, which allows you to view",
254 "the dumped/restored screens.",
258 " <space> load the next screen",
259 " <backspace> load the previous screen",
262 popup_msg(stdscr, msgs);
268 static const char *msg[] =
270 "Usage: savescreen [-r] files",
273 " -f file fill/initialize screen using text from this file",
274 " -i use scr_init/scr_restore rather than scr_set",
275 " -k keep the restored dump-files rather than removing them",
276 " -r replay the screen-dump files"
279 for (n = 0; n < SIZEOF(msg); ++n) {
280 fprintf(stderr, "%s\n", msg[n]);
282 ExitProgram(EXIT_FAILURE);
286 main(int argc, char *argv[])
291 bool use_colors = FALSE;
292 bool replaying = FALSE;
296 #if USE_WIDEC_SUPPORT
302 static const wchar_t mywc[2] =
304 bool using_rgb = FALSE;
307 setlocale(LC_ALL, "");
309 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
329 files = argv + optind;
330 last = argc - optind - 1;
333 while (last >= 0 && !fexists(files[last]))
340 keypad(stdscr, TRUE);
343 if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
344 static const struct {
346 } table[MAX_ANSI] = {
347 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
352 DATA(MAGENTA, WHITE),
353 DATA(MAGENTA, BLACK),
363 * Discounting color-pair 0 (no color), make the next 8 color pairs
364 * useful for leaving a visually distinct trail of characters on the
367 for (n = 0; n < MAX_ANSI; ++n) {
368 init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
371 * After that, use color pairs for constructing a test-pattern, e.g.,
372 * imitating xterm's scripts.
376 for (n = 0; n < COLORS; ++n)
377 init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
379 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
381 int r_max, g_max, b_max;
383 if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
384 int rows = LINES - 1;
386 int b_delta = (b_max / rows);
387 int r_delta = (r_max / cols);
388 int g_delta = (g_max / cols);
393 while (row++ < rows) {
397 while (col++ < cols) {
398 int color = (((r * (g_max + 1)) + g) * (b_max + 1)
400 #if HAVE_INIT_EXTENDED_COLOR
401 init_extended_pair(pair, color, color);
403 init_pair(pair, color, color);
415 if ((fill_by == 0) && !replaying) {
417 * Originally (before wide-characters) ncurses supported 16 colors.
419 if (COLORS >= 16 && COLORS <= 256) {
420 mvprintw(2, 0, "System colors:\n");
421 for (n = 0; n < 16; ++n) {
423 addch((chtype) (' ' | COLOR_PAIR(pair)));
424 addch((chtype) (' ' | COLOR_PAIR(pair)));
425 if (((n + 1) % 8) == 0)
430 * Even with ncurses, you need wide-character support to have more
433 #if USE_WIDEC_SUPPORT
436 } else if (COLORS == 256) {
441 cube1 = cube0 + (cube * cube * cube);
444 printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
445 for (g = 0; g < cube; g++) {
446 for (r = 0; r < cube; r++) {
447 for (b = 0; b < cube; b++) {
450 + (r * cube * cube) + (g * cube) + b;
451 setcchar(&mycc, mywc, 0, (short) pair, NULL);
460 printw("Grayscale ramp:\n");
461 for (n = cube1; n < COLORS; ++n) {
463 setcchar(&mycc, mywc, 0, (short) pair, NULL);
467 } else if ((COLORS > 256) && using_rgb) {
468 int rows = LINES - 1;
474 while (row++ < rows) {
476 while (col++ < cols) {
477 setcchar(&mycc, mywc, 0, (short) pair, &pair);
490 FILE *fp = fopen(fill_by, "r");
494 while ((ch = fgetc(fp)) != EOF) {
495 if (addch(UChar(ch)) == ERR) {
502 while (addch(' ') != ERR) {
509 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
510 ExitProgram(EXIT_FAILURE);
517 * Use the last file as the initial/current screen.
521 printf("No screen-dumps given\n");
522 ExitProgram(EXIT_FAILURE);
526 if (load_screen(files[which]) == ERR) {
528 printf("Cannot load screen-dump %s\n", files[which]);
529 ExitProgram(EXIT_FAILURE);
533 while (!done && (ch = getch()) != ERR) {
537 * If we got a "next" here, skip to the final screen before
538 * moving to the next process.
567 } else if (scr_restore(files[which]) == ERR) {
569 printf("Cannot load screen-dump %s\n", files[which]);
571 ExitProgram(EXIT_FAILURE);
582 bool dirty = use_colors || (fill_by != 0);
585 switch (get_command(color, which, last)) {
587 if (dirty && files[which]) {
588 dump_screen(files, color, which, last, use_colors);
598 if (dump_screen(files, color, which, last, use_colors)) {
599 which = (which + 1) % MAX_ANSI;
627 altchars = !altchars;
631 color = (color + 1) % MAX_ANSI;
642 chtype attr = A_REVERSE;
643 chtype ch2 = (altchars ? MyMarker : '#');
645 attr |= (chtype) COLOR_PAIR(color);
655 ExitProgram(EXIT_SUCCESS);
660 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
662 printf("This program requires the screen-dump functions\n");
663 ExitProgram(EXIT_FAILURE);