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.28 2015/08/22 22:40:22 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 " -f file fill/initialize screen using text from this file",
215 " -i use scr_init/scr_restore rather than scr_set",
216 " -k keep the restored dump-files rather than removing them",
217 " -r replay the screen-dump files"
220 for (n = 0; n < SIZEOF(msg); ++n) {
221 fprintf(stderr, "%s\n", msg[n]);
223 ExitProgram(EXIT_FAILURE);
227 main(int argc, char *argv[])
232 bool replaying = FALSE;
236 #if USE_WIDEC_SUPPORT
241 setlocale(LC_ALL, "");
243 while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
263 files = argv + optind;
264 last = argc - optind - 1;
267 while (last >= 0 && !fexists(files[last]))
274 keypad(stdscr, TRUE);
282 * Assume pairs is the square of colors, and assign pairs going down
283 * so that there is minimal conflict with the background color (which
284 * counts up). The intent is just to show how color pair values are
285 * saved and restored.
287 for (pair = 0; pair < COLOR_PAIRS; ++pair) {
288 color = (short) (pair % (COLORS - 1));
289 init_pair(pair, (short) (COLOR_WHITE - color), color);
294 FILE *fp = fopen(fill_by, "r");
298 while ((ch = fgetc(fp)) != EOF) {
299 if (addch(UChar(ch)) == ERR) {
306 while (addch(' ') != ERR) {
313 fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
314 ExitProgram(EXIT_FAILURE);
321 * Use the last file as the initial/current screen.
325 printf("No screen-dumps given\n");
326 ExitProgram(EXIT_FAILURE);
330 if (load_screen(files[which]) == ERR) {
332 printf("Cannot load screen-dump %s\n", files[which]);
333 ExitProgram(EXIT_FAILURE);
337 while (!done && (ch = getch()) != ERR) {
341 * If we got a "next" here, skip to the final screen before
342 * moving to the next process.
371 } else if (scr_restore(files[which]) == ERR) {
373 printf("Cannot load screen-dump %s\n", files[which]);
375 ExitProgram(EXIT_FAILURE);
388 switch (get_command(which, last)) {
398 if (files[which] != 0) {
399 show_what(which + 1, last);
400 if (scr_dump(files[which]) == ERR) {
402 printf("Cannot write screen-dump %s\n", files[which]);
410 short pair = (short) (which % COLOR_PAIRS);
412 * Change the background color, to make it more
413 * obvious. But that changes the existing text-color.
414 * Copy the old values from the currently displayed
417 bkgd((chtype) COLOR_PAIR(pair));
418 for (cy = 1; cy < LINES; ++cy) {
419 for (cx = 0; cx < COLS; ++cx) {
420 wmove(curscr, cy, cx);
421 wmove(stdscr, cy, cx);
422 #if USE_WIDEC_SUPPORT
423 if (win_wch(curscr, &mycc) != ERR) {
424 myxx = wcwidth(mycc.chars[0]);
426 wadd_wchnstr(stdscr, &mycc, 1);
431 waddch(stdscr, winch(curscr));
461 altchars = !altchars;
464 color = (color + 1) % COLORS;
474 attr_t attr = (A_REVERSE | (attr_t) COLOR_PAIR(color * COLORS));
475 chtype ch2 = (altchars ? ACS_DIAMOND : '#');
483 ExitProgram(EXIT_SUCCESS);
488 main(int argc, char *argv[])
490 printf("This program requires the screen-dump functions\n");
491 ExitProgram(EXIT_FAILURE);