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.52 2018/02/03 23:18:50 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];
74 getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
80 fexists(const char *name)
83 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
94 cleanup(char *files[])
99 for (n = 0; files[n] != 0; ++n) {
106 load_screen(char *filename)
111 if ((result = scr_init(filename)) != ERR)
112 result = scr_restore(filename);
114 result = scr_set(filename);
120 * scr_restore() or scr_set() operates on curscr. If we read a character using
121 * getch() that will refresh stdscr, wiping out the result. To avoid that,
122 * copy the data back from curscr to stdscr.
127 overwrite(curscr, stdscr);
132 show_what(int color, int which, int last)
141 printw("Color %d. Saved %d of %d (? for help)", color, which, last + 1);
143 now = time((time_t *) 0);
144 mytime = ctime(&now);
145 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
146 if (isspace(UChar(mytime[n]))) {
152 mvprintw(0, (COLS - n - 2), " %s", mytime);
160 get_command(int color, int which, int last)
167 show_what(color, which, last);
175 dump_screen(char **files, int color, int which, int last, bool use_colors)
177 #if USE_WIDEC_SUPPORT
181 char *filename = files[which];
186 show_what(color, ++which, last);
187 if (scr_dump(filename) == ERR) {
189 printf("Cannot write screen-dump %s\n", filename);
191 ExitProgram(EXIT_SUCCESS);
195 int pair = 1 + (which % MAX_ANSI);
197 * Change the background color, to make it more obvious. But that
198 * changes the existing text-color. Copy the old values from the
199 * currently displayed screen.
201 bkgd((chtype) COLOR_PAIR(pair));
202 for (cy = 1; cy < LINES; ++cy) {
203 for (cx = 0; cx < COLS; ++cx) {
204 wmove(curscr, cy, cx);
205 wmove(stdscr, cy, cx);
206 #if USE_WIDEC_SUPPORT
207 if (win_wch(curscr, &mycc) != ERR) {
208 myxx = wcwidth(BaseChar(mycc));
210 wadd_wchnstr(stdscr, &mycc, 1);
215 waddch(stdscr, winch(curscr));
227 static const char *msgs[] =
229 "You are now in the screen-editor, which allows you to make some",
230 "lines on the screen, as well as save copies of the screen to a",
235 " n run the screen-loader to show the saved screens",
236 " <space> dump a screen",
238 " a toggle between '#' and graphic symbol for drawing",
239 " c change color drawn by line to next in palette",
240 " h,j,k,l or arrows to move around the screen, drawing",
243 popup_msg(stdscr, msgs);
249 static const char *msgs[] =
251 "You are now in the screen-loader, which allows you to view",
252 "the dumped/restored screens.",
256 " <space> load the next screen",
257 " <backspace> load the previous screen",
260 popup_msg(stdscr, msgs);
266 static const char *msg[] =
268 "Usage: savescreen [-r] files",
271 " -f file fill/initialize screen using text from this file",
272 " -i use scr_init/scr_restore rather than scr_set",
273 " -k keep the restored dump-files rather than removing them",
274 " -r replay the screen-dump files"
277 for (n = 0; n < SIZEOF(msg); ++n) {
278 fprintf(stderr, "%s\n", msg[n]);
280 ExitProgram(EXIT_FAILURE);
284 main(int argc, char *argv[])
289 bool use_colors = FALSE;
290 bool replaying = FALSE;
294 #if USE_WIDEC_SUPPORT
300 static const wchar_t mywc[2] =
302 bool using_rgb = FALSE;
305 setlocale(LC_ALL, "");
307 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
327 files = argv + optind;
328 last = argc - optind - 1;
331 while (last >= 0 && !fexists(files[last]))
338 keypad(stdscr, TRUE);
341 if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
342 static const struct {
344 } table[MAX_ANSI] = {
345 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
350 DATA(MAGENTA, WHITE),
351 DATA(MAGENTA, BLACK),
361 * Discounting color-pair 0 (no color), make the next 8 color pairs
362 * useful for leaving a visually distinct trail of characters on the
365 for (n = 0; n < MAX_ANSI; ++n) {
366 init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
369 * After that, use color pairs for constructing a test-pattern, e.g.,
370 * imitating xterm's scripts.
374 for (n = 0; n < COLORS; ++n)
375 init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
377 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
379 int r_max, g_max, b_max;
381 if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
382 int rows = LINES - 1;
384 int b_delta = (b_max / rows);
385 int r_delta = (r_max / cols);
386 int g_delta = (g_max / cols);
391 while (row++ < rows) {
395 while (col++ < cols) {
396 int color = (((r * (g_max + 1)) + g) * (b_max + 1)
398 #if HAVE_INIT_EXTENDED_COLOR
399 init_extended_pair(pair, color, color);
401 init_pair(pair, color, color);
413 if ((fill_by == 0) && !replaying) {
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) {
439 cube1 = cube0 + (cube * cube * cube);
442 printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
443 for (g = 0; g < cube; g++) {
444 for (r = 0; r < cube; r++) {
445 for (b = 0; b < cube; b++) {
448 + (r * cube * cube) + (g * cube) + b;
449 setcchar(&mycc, mywc, 0, (short) pair, NULL);
458 printw("Grayscale ramp:\n");
459 for (n = cube1; n < COLORS; ++n) {
461 setcchar(&mycc, mywc, 0, (short) pair, NULL);
465 } else if ((COLORS > 256) && using_rgb) {
466 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);
658 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
660 printf("This program requires the screen-dump functions\n");
661 ExitProgram(EXIT_FAILURE);