1 /****************************************************************************
2 * Copyright (c) 2007-2011,2015 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.23 2015/03/21 21:35:00 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
35 #include <test.priv.h>
39 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
53 static bool use_init = FALSE;
54 static bool keep_dumps = FALSE;
57 fexists(const char *name)
60 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
71 cleanup(char *files[])
76 for (n = 0; files[n] != 0; ++n) {
83 load_screen(char *filename)
88 if ((result = scr_init(filename)) != ERR)
89 result = scr_restore(filename);
91 result = scr_set(filename);
97 * scr_restore() or scr_set() operates on curscr. If we read a character using
98 * getch() that will refresh stdscr, wiping out the result. To avoid that,
99 * copy the data back from curscr to stdscr.
104 overwrite(curscr, stdscr);
109 show_what(int which, int last)
118 printw("Saved %d of %d (? for help)", which, last + 1);
120 now = time((time_t *) 0);
121 mytime = ctime(&now);
122 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
123 if (isspace(UChar(mytime[n]))) {
129 mvprintw(0, (COLS - n - 2), " %s", mytime);
137 get_command(int which, int last)
144 show_what(which, last);
152 show_help(const char **help)
154 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
159 for (n = 0; help[n] != 0; ++n) {
160 wmove(mywin, 1 + n, 2);
161 wprintw(mywin, "%.*s", COLS - 4, help[n]);
172 static const char *msgs[] =
174 "You are now in the screen-editor, which allows you to make some",
175 "lines on the screen, as well as save copies of the screen to a",
180 " n run the screen-loader to show the saved screens",
181 " <space> dump a screen",
183 " a toggle between '#' and graphic symbol for drawing",
184 " c change color drawn by line to next in palette",
185 " h,j,k,l or arrows to move around the screen, drawing",
193 static const char *msgs[] =
195 "You are now in the screen-loader, which allows you to view",
196 "the dumped/restored screens.",
200 " <space> load the next screen",
201 " <backspace> load the previous screen",
209 static const char *msg[] =
211 "Usage: savescreen [-r] files",
214 " -i use scr_init/scr_restore rather than scr_set",
215 " -k keep the restored dump-files rather than removing them",
216 " -r replay the screen-dump files"
219 for (n = 0; n < SIZEOF(msg); ++n) {
220 fprintf(stderr, "%s\n", msg[n]);
222 ExitProgram(EXIT_FAILURE);
226 main(int argc, char *argv[])
231 bool replaying = FALSE;
235 setlocale(LC_ALL, "");
237 while ((ch = getopt(argc, argv, "ikr")) != -1) {
254 files = argv + optind;
255 last = argc - optind - 1;
258 while (last >= 0 && !fexists(files[last]))
265 keypad(stdscr, TRUE);
273 * Assume pairs is the square of colors, and assign pairs going down
274 * so that there is minimal conflict with the background color (which
275 * counts up). The intent is just to show how color pair values are
276 * saved and restored.
278 for (pair = 0; pair < COLOR_PAIRS; ++pair) {
279 color = (short) (pair % (COLORS - 1));
280 init_pair(pair, (short) (COLOR_WHITE - color), color);
287 * Use the last file as the initial/current screen.
291 printf("No screen-dumps given\n");
292 ExitProgram(EXIT_FAILURE);
296 if (load_screen(files[which]) == ERR) {
298 printf("Cannot load screen-dump %s\n", files[which]);
299 ExitProgram(EXIT_FAILURE);
303 while (!done && (ch = getch()) != ERR) {
307 * If we got a "next" here, skip to the final screen before
308 * moving to the next process.
337 } else if (scr_restore(files[which]) == ERR) {
339 printf("Cannot load screen-dump %s\n", files[which]);
341 ExitProgram(EXIT_FAILURE);
354 switch (get_command(which, last)) {
364 if (files[which] != 0) {
365 show_what(which + 1, last);
366 if (scr_dump(files[which]) == ERR) {
368 printf("Cannot write screen-dump %s\n", files[which]);
376 short pair = (short) (which % COLOR_PAIRS);
378 * Change the background color, to make it more
379 * obvious. But that changes the existing text-color.
380 * Copy the old values from the currently displayed
383 bkgd((chtype) COLOR_PAIR(pair));
384 for (cy = 1; cy < LINES; ++cy) {
385 for (cx = 0; cx < COLS; ++cx) {
386 wmove(curscr, cy, cx);
387 wmove(stdscr, cy, cx);
388 waddch(stdscr, winch(curscr));
417 altchars = !altchars;
420 color = (color + 1) % COLORS;
430 attr_t attr = (A_REVERSE | COLOR_PAIR(color * COLORS));
431 chtype ch2 = (altchars ? ACS_DIAMOND : '#');
439 ExitProgram(EXIT_SUCCESS);
444 main(int argc, char *argv[])
446 printf("This program requires the screen-dump functions\n");
447 ExitProgram(EXIT_FAILURE);