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.21 2015/03/07 21:55:35 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;
56 fexists(const char *name)
59 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
70 cleanup(char *files[])
74 for (n = 0; files[n] != 0; ++n) {
80 load_screen(char *filename)
85 if ((result = scr_init(filename)) != ERR)
86 result = scr_restore(filename);
88 result = scr_set(filename);
94 * scr_restore() or scr_set() operates on curscr. If we read a character using
95 * getch() that will refresh stdscr, wiping out the result. To avoid that,
96 * copy the data back from curscr to stdscr.
101 overwrite(curscr, stdscr);
106 show_what(int which, int last)
115 printw("Saved %d of %d (? for help)", which, last + 1);
117 now = time((time_t *) 0);
118 mytime = ctime(&now);
119 for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
120 if (isspace(UChar(mytime[n]))) {
126 mvprintw(0, (COLS - n - 2), " %s", mytime);
134 get_command(int which, int last)
141 show_what(which, last);
149 show_help(const char **help)
151 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
156 for (n = 0; help[n] != 0; ++n) {
157 wmove(mywin, 1 + n, 2);
158 wprintw(mywin, "%.*s", COLS - 4, help[n]);
169 static const char *msgs[] =
171 "You are now in the screen-editor, which allows you to make some",
172 "lines on the screen, as well as save copies of the screen to a",
177 " n run the screen-loader to show the saved screens",
178 " <space> dump a screen",
180 " a toggle between '#' and graphic symbol for drawing",
181 " c change color drawn by line to next in palette",
182 " h,j,k,l or arrows to move around the screen, drawing",
190 static const char *msgs[] =
192 "You are now in the screen-loader, which allows you to view",
193 "the dumped/restored screens.",
197 " <space> load the next screen",
198 " <backspace> load the previous screen",
206 static const char *msg[] =
208 "Usage: savescreen [-r] files",
211 " -i use scr_init/scr_restore rather than scr_set",
212 " -r replay the screen-dump files"
215 for (n = 0; n < SIZEOF(msg); ++n) {
216 fprintf(stderr, "%s\n", msg[n]);
218 ExitProgram(EXIT_FAILURE);
222 main(int argc, char *argv[])
227 bool replaying = FALSE;
231 while ((ch = getopt(argc, argv, "ir")) != -1) {
245 files = argv + optind;
246 last = argc - optind - 1;
249 while (last >= 0 && !fexists(files[last]))
256 keypad(stdscr, TRUE);
264 * Assume pairs is the square of colors, and assign pairs going down
265 * so that there is minimal conflict with the background color (which
266 * counts up). The intent is just to show how color pair values are
267 * saved and restored.
269 for (pair = 0; pair < COLOR_PAIRS; ++pair) {
270 color = (short) (pair % (COLORS - 1));
271 init_pair(pair, COLOR_WHITE - color, color);
278 * Use the last file as the initial/current screen.
282 printf("No screen-dumps given\n");
283 ExitProgram(EXIT_FAILURE);
287 if (load_screen(files[which]) == ERR) {
289 printf("Cannot load screen-dump %s\n", files[which]);
290 ExitProgram(EXIT_FAILURE);
294 while (!done && (ch = getch()) != ERR) {
298 * If we got a "next" here, skip to the final screen before
299 * moving to the next process.
328 } else if (scr_restore(files[which]) == ERR) {
330 printf("Cannot load screen-dump %s\n", files[which]);
332 ExitProgram(EXIT_FAILURE);
345 switch (get_command(which, last)) {
355 if (files[which] != 0) {
356 show_what(which + 1, last);
357 if (scr_dump(files[which]) == ERR) {
359 printf("Cannot write screen-dump %s\n", files[which]);
367 short pair = (short) (which % COLOR_PAIRS);
369 * Change the background color, to make it more
370 * obvious. But that changes the existing text-color.
371 * Copy the old values from the currently displayed
374 bkgd((chtype) COLOR_PAIR(pair));
375 for (cy = 1; cy < LINES; ++cy) {
376 for (cx = 0; cx < COLS; ++cx) {
377 wmove(curscr, cy, cx);
378 wmove(stdscr, cy, cx);
379 waddch(stdscr, winch(curscr));
408 altchars = !altchars;
411 color = (color + 1) % COLORS;
421 attr_t attr = (A_REVERSE | COLOR_PAIR(color * COLORS));
422 chtype ch2 = (altchars ? ACS_DIAMOND : '#');
430 ExitProgram(EXIT_SUCCESS);
435 main(int argc, char *argv[])
437 printf("This program requires the screen-dump functions\n");
438 ExitProgram(EXIT_FAILURE);