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.50 2018/01/07 02:24:03 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>
40 #include <sys/types.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
48 # include <sys/time.h>
57 #define MyMarker ACS_DIAMOND
62 static bool use_init = FALSE;
63 static bool keep_dumps = FALSE;
66 /* In HPUX curses, cchar_t is opaque; other implementations are not */
68 BaseChar(cchar_t data)
70 wchar_t my_wchar[CCHARW_MAX];
73 getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
79 fexists(const char *name)
82 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
93 cleanup(char *files[])
98 for (n = 0; files[n] != 0; ++n) {
105 load_screen(char *filename)
110 if ((result = scr_init(filename)) != ERR)
111 result = scr_restore(filename);
113 result = scr_set(filename);
119 * scr_restore() or scr_set() operates on curscr. If we read a character using
120 * getch() that will refresh stdscr, wiping out the result. To avoid that,
121 * copy the data back from curscr to stdscr.
126 overwrite(curscr, stdscr);
131 show_what(int color, int which, int last)
140 printw("Color %d. Saved %d of %d (? for help)", color, which, last + 1);
142 now = time((time_t *) 0);
143 mytime = ctime(&now);
144 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
145 if (isspace(UChar(mytime[n]))) {
151 mvprintw(0, (COLS - n - 2), " %s", mytime);
159 get_command(int color, int which, int last)
166 show_what(color, which, last);
174 dump_screen(char **files, int color, int which, int last, bool use_colors)
176 #if USE_WIDEC_SUPPORT
180 char *filename = files[which];
185 show_what(color, ++which, last);
186 if (scr_dump(filename) == ERR) {
188 printf("Cannot write screen-dump %s\n", filename);
190 ExitProgram(EXIT_SUCCESS);
194 int pair = 1 + (which % MAX_ANSI);
196 * Change the background color, to make it more obvious. But that
197 * changes the existing text-color. Copy the old values from the
198 * currently displayed screen.
200 bkgd((chtype) COLOR_PAIR(pair));
201 for (cy = 1; cy < LINES; ++cy) {
202 for (cx = 0; cx < COLS; ++cx) {
203 wmove(curscr, cy, cx);
204 wmove(stdscr, cy, cx);
205 #if USE_WIDEC_SUPPORT
206 if (win_wch(curscr, &mycc) != ERR) {
207 myxx = wcwidth(BaseChar(mycc));
209 wadd_wchnstr(stdscr, &mycc, 1);
214 waddch(stdscr, winch(curscr));
226 static const char *msgs[] =
228 "You are now in the screen-editor, which allows you to make some",
229 "lines on the screen, as well as save copies of the screen to a",
234 " n run the screen-loader to show the saved screens",
235 " <space> dump a screen",
237 " a toggle between '#' and graphic symbol for drawing",
238 " c change color drawn by line to next in palette",
239 " h,j,k,l or arrows to move around the screen, drawing",
242 popup_msg(stdscr, msgs);
248 static const char *msgs[] =
250 "You are now in the screen-loader, which allows you to view",
251 "the dumped/restored screens.",
255 " <space> load the next screen",
256 " <backspace> load the previous screen",
259 popup_msg(stdscr, msgs);
265 static const char *msg[] =
267 "Usage: savescreen [-r] files",
270 " -f file fill/initialize screen using text from this file",
271 " -i use scr_init/scr_restore rather than scr_set",
272 " -k keep the restored dump-files rather than removing them",
273 " -r replay the screen-dump files"
276 for (n = 0; n < SIZEOF(msg); ++n) {
277 fprintf(stderr, "%s\n", msg[n]);
279 ExitProgram(EXIT_FAILURE);
283 main(int argc, char *argv[])
288 bool use_colors = FALSE;
289 bool replaying = FALSE;
293 #if USE_WIDEC_SUPPORT
299 static const wchar_t mywc[2] =
301 bool using_rgb = FALSE;
304 setlocale(LC_ALL, "");
306 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
326 files = argv + optind;
327 last = argc - optind - 1;
330 while (last >= 0 && !fexists(files[last]))
337 keypad(stdscr, TRUE);
340 if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
341 static const struct {
343 } table[MAX_ANSI] = {
344 #define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
349 DATA(MAGENTA, WHITE),
350 DATA(MAGENTA, BLACK),
360 * Discounting color-pair 0 (no color), make the next 8 color pairs
361 * useful for leaving a visually distinct trail of characters on the
364 for (n = 0; n < MAX_ANSI; ++n) {
365 init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
368 * After that, use color pairs for constructing a test-pattern, e.g.,
369 * imitating xterm's scripts.
373 for (n = 0; n < COLORS; ++n)
374 init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
376 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
377 else if (tigetflag("RGB") > 0) {
378 int rows = LINES - 1;
380 /* FIXME: test all 3 types of capability */
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);
412 if ((fill_by == 0) && !replaying) {
414 * Originally (before wide-characters) ncurses supported 16 colors.
416 if (COLORS >= 16 && COLORS <= 256) {
417 mvprintw(2, 0, "System colors:\n");
418 for (n = 0; n < 16; ++n) {
420 addch(' ' | COLOR_PAIR(pair));
421 addch(' ' | COLOR_PAIR(pair));
422 if (((n + 1) % 8) == 0)
427 * Even with ncurses, you need wide-character support to have more
430 #if USE_WIDEC_SUPPORT
433 } else if (COLORS == 256) {
438 cube1 = cube0 + (cube * cube * cube);
441 printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
442 for (g = 0; g < cube; g++) {
443 for (r = 0; r < cube; r++) {
444 for (b = 0; b < cube; b++) {
447 + (r * cube * cube) + (g * cube) + b;
448 setcchar(&mycc, mywc, 0, (short) pair, NULL);
457 printw("Grayscale ramp:\n");
458 for (n = cube1; n < COLORS; ++n) {
460 setcchar(&mycc, mywc, 0, (short) pair, NULL);
464 } else if ((COLORS > 256) && using_rgb) {
465 int rows = LINES - 1;
471 while (row++ < rows) {
473 while (col++ < cols) {
474 setcchar(&mycc, mywc, 0, (short) pair, &pair);
487 FILE *fp = fopen(fill_by, "r");
491 while ((ch = fgetc(fp)) != EOF) {
492 if (addch(UChar(ch)) == ERR) {
499 while (addch(' ') != ERR) {
506 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
507 ExitProgram(EXIT_FAILURE);
514 * Use the last file as the initial/current screen.
518 printf("No screen-dumps given\n");
519 ExitProgram(EXIT_FAILURE);
523 if (load_screen(files[which]) == ERR) {
525 printf("Cannot load screen-dump %s\n", files[which]);
526 ExitProgram(EXIT_FAILURE);
530 while (!done && (ch = getch()) != ERR) {
534 * If we got a "next" here, skip to the final screen before
535 * moving to the next process.
564 } else if (scr_restore(files[which]) == ERR) {
566 printf("Cannot load screen-dump %s\n", files[which]);
568 ExitProgram(EXIT_FAILURE);
579 bool dirty = use_colors || (fill_by != 0);
582 switch (get_command(color, which, last)) {
584 if (dirty && files[which]) {
585 dump_screen(files, color, which, last, use_colors);
595 if (dump_screen(files, color, which, last, use_colors)) {
596 which = (which + 1) % MAX_ANSI;
624 altchars = !altchars;
628 color = (color + 1) % MAX_ANSI;
639 chtype attr = A_REVERSE;
640 chtype ch2 = (altchars ? MyMarker : '#');
642 attr |= COLOR_PAIR(color);
652 ExitProgram(EXIT_SUCCESS);
657 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
659 printf("This program requires the screen-dump functions\n");
660 ExitProgram(EXIT_FAILURE);