1 /****************************************************************************
2 * Copyright (c) 2007 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.10 2007/07/21 17:57:37 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
35 #include <test.priv.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
48 static bool use_init = FALSE;
58 cleanup(char *files[])
62 for (n = 0; files[n] != 0; ++n) {
68 load_screen(char *filename)
73 if ((result = scr_init(filename)) != ERR)
74 result = scr_restore(filename);
76 result = scr_set(filename);
82 * scr_restore() or scr_set() operates on curscr. If we read a character using
83 * getch() that will refresh stdscr, wiping out the result. To avoid that,
84 * copy the data back from curscr to stdscr.
89 overwrite(curscr, stdscr);
94 show_what(int which, int last)
97 time_t now = time((time_t *) 0);
102 printw("Saved %d of %d - %s", which, last + 1, ctime(&now));
110 get_command(int which, int last)
117 show_what(which, last);
127 static const char *msg[] =
129 "Usage: savescreen [-r] files",
132 " -i use scr_init/scr_restore rather than scr_set",
133 " -r replay the screen-dump files"
136 for (n = 0; n < SIZEOF(msg); ++n) {
137 fprintf(stderr, "%s\n", msg[n]);
139 ExitProgram(EXIT_FAILURE);
143 main(int argc, char *argv[])
148 bool replaying = FALSE;
152 while ((ch = getopt(argc, argv, "ir")) != -1) {
169 keypad(stdscr, TRUE);
173 for (ch = 0; ch < COLOR_PAIRS; ++ch) {
174 short pair = ch % COLOR_PAIRS;
175 init_pair(pair, COLOR_WHITE, ch % COLORS);
179 files = argv + optind;
180 last = argc - optind - 1;
184 * Use the last file as the initial/current screen.
188 printf("No screen-dumps given\n");
189 ExitProgram(EXIT_FAILURE);
193 if (load_screen(files[which]) == ERR) {
195 printf("Cannot load screen-dump %s\n", files[which]);
196 ExitProgram(EXIT_FAILURE);
200 while (!done && (ch = getch()) != ERR) {
204 * If we got a "next" here, skip to the final screen before
205 * moving to the next process.
232 } else if (scr_restore(files[which]) == ERR) {
234 printf("Cannot load screen-dump %s\n", files[which]);
236 ExitProgram(EXIT_FAILURE);
246 printw("Use h,j,k,l or arrows to move around the screen\n");
247 printw("Press 'q' to quit, ' ' to dump a screen\n");
248 printw("When the last screen has been dumped, press 'n' to run the\n");
249 printw("screen-loader. That allows only 'q', backspace and ' ' for\n");
250 printw("stepping through the dumped/restored screens.\n");
254 switch (ch = get_command(which, last)) {
265 if (files[which] != 0) {
266 show_what(which + 1, last);
267 if (scr_dump(files[which]) == ERR) {
269 printf("Cannot write screen-dump %s\n", files[which]);
276 short pair = which % COLOR_PAIRS;
277 bkgd(COLOR_PAIR(pair));
305 time_t now = time((time_t *) 0);
310 addch('#' | A_REVERSE);
315 ExitProgram(EXIT_SUCCESS);