1 /****************************************************************************
2 * Copyright (c) 2007-2015,2017 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.32 2017/04/15 17:33: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>
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
60 static bool use_init = FALSE;
61 static bool keep_dumps = FALSE;
64 /* In HPUX curses, cchar_t is opaque; other implementations are not */
66 BaseChar(cchar_t data)
68 wchar_t my_wchar[sizeof(cchar_t)];
71 getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
77 fexists(const char *name)
80 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
91 cleanup(char *files[])
96 for (n = 0; files[n] != 0; ++n) {
103 load_screen(char *filename)
108 if ((result = scr_init(filename)) != ERR)
109 result = scr_restore(filename);
111 result = scr_set(filename);
117 * scr_restore() or scr_set() operates on curscr. If we read a character using
118 * getch() that will refresh stdscr, wiping out the result. To avoid that,
119 * copy the data back from curscr to stdscr.
124 overwrite(curscr, stdscr);
129 show_what(int which, int last)
138 printw("Saved %d of %d (? for help)", which, last + 1);
140 now = time((time_t *) 0);
141 mytime = ctime(&now);
142 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
143 if (isspace(UChar(mytime[n]))) {
149 mvprintw(0, (COLS - n - 2), " %s", mytime);
157 get_command(int which, int last)
164 show_what(which, last);
174 static const char *msgs[] =
176 "You are now in the screen-editor, which allows you to make some",
177 "lines on the screen, as well as save copies of the screen to a",
182 " n run the screen-loader to show the saved screens",
183 " <space> dump a screen",
185 " a toggle between '#' and graphic symbol for drawing",
186 " c change color drawn by line to next in palette",
187 " h,j,k,l or arrows to move around the screen, drawing",
190 popup_msg(stdscr, msgs);
196 static const char *msgs[] =
198 "You are now in the screen-loader, which allows you to view",
199 "the dumped/restored screens.",
203 " <space> load the next screen",
204 " <backspace> load the previous screen",
207 popup_msg(stdscr, msgs);
213 static const char *msg[] =
215 "Usage: savescreen [-r] files",
218 " -f file fill/initialize screen using text from this file",
219 " -i use scr_init/scr_restore rather than scr_set",
220 " -k keep the restored dump-files rather than removing them",
221 " -r replay the screen-dump files"
224 for (n = 0; n < SIZEOF(msg); ++n) {
225 fprintf(stderr, "%s\n", msg[n]);
227 ExitProgram(EXIT_FAILURE);
231 main(int argc, char *argv[])
236 bool replaying = FALSE;
240 #if USE_WIDEC_SUPPORT
245 setlocale(LC_ALL, "");
247 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
267 files = argv + optind;
268 last = argc - optind - 1;
271 while (last >= 0 && !fexists(files[last]))
278 keypad(stdscr, TRUE);
286 * Assume pairs is the square of colors, and assign pairs going down
287 * so that there is minimal conflict with the background color (which
288 * counts up). The intent is just to show how color pair values are
289 * saved and restored.
291 for (pair = 0; pair < COLOR_PAIRS; ++pair) {
292 color = (short) (pair % (COLORS - 1));
293 init_pair(pair, (short) (COLOR_WHITE - color), color);
298 FILE *fp = fopen(fill_by, "r");
302 while ((ch = fgetc(fp)) != EOF) {
303 if (addch(UChar(ch)) == ERR) {
310 while (addch(' ') != ERR) {
317 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
318 ExitProgram(EXIT_FAILURE);
325 * Use the last file as the initial/current screen.
329 printf("No screen-dumps given\n");
330 ExitProgram(EXIT_FAILURE);
334 if (load_screen(files[which]) == ERR) {
336 printf("Cannot load screen-dump %s\n", files[which]);
337 ExitProgram(EXIT_FAILURE);
341 while (!done && (ch = getch()) != ERR) {
345 * If we got a "next" here, skip to the final screen before
346 * moving to the next process.
375 } else if (scr_restore(files[which]) == ERR) {
377 printf("Cannot load screen-dump %s\n", files[which]);
379 ExitProgram(EXIT_FAILURE);
392 switch (get_command(which, last)) {
402 if (files[which] != 0) {
403 show_what(which + 1, last);
404 if (scr_dump(files[which]) == ERR) {
406 printf("Cannot write screen-dump %s\n", files[which]);
414 short pair = (short) (which % COLOR_PAIRS);
416 * Change the background color, to make it more
417 * obvious. But that changes the existing text-color.
418 * Copy the old values from the currently displayed
421 bkgd((chtype) COLOR_PAIR(pair));
422 for (cy = 1; cy < LINES; ++cy) {
423 for (cx = 0; cx < COLS; ++cx) {
424 wmove(curscr, cy, cx);
425 wmove(stdscr, cy, cx);
426 #if USE_WIDEC_SUPPORT
427 if (win_wch(curscr, &mycc) != ERR) {
428 myxx = wcwidth(BaseChar(mycc));
430 wadd_wchnstr(stdscr, &mycc, 1);
435 waddch(stdscr, winch(curscr));
465 altchars = !altchars;
468 color = (color + 1) % COLORS;
478 attr_t attr = (A_REVERSE | (attr_t) COLOR_PAIR(color * COLORS));
479 chtype ch2 = (altchars ? MyMarker : '#');
487 ExitProgram(EXIT_SUCCESS);
492 main(int argc, char *argv[])
494 printf("This program requires the screen-dump functions\n");
495 ExitProgram(EXIT_FAILURE);