]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/savescreen.c
44d7d1ca1a6b4223b0f1b8faeb8c3ef27210f070
[ncurses.git] / test / savescreen.c
1 /****************************************************************************
2  * Copyright (c) 2007-2011,2015 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: savescreen.c,v 1.21 2015/03/07 21:55:35 tom Exp $
30  *
31  * Demonstrate save/restore functions from the curses library.
32  * Thomas Dickey - 2007/7/14
33  */
34
35 #include <test.priv.h>
36
37 #if HAVE_SCR_DUMP
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
44 # include <time.h>
45 #else
46 # if HAVE_SYS_TIME_H
47 #  include <sys/time.h>
48 # else
49 #  include <time.h>
50 # endif
51 #endif
52
53 static bool use_init = FALSE;
54
55 static int
56 fexists(const char *name)
57 {
58     struct stat sb;
59     return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
60 }
61
62 static void
63 setup_next(void)
64 {
65     curs_set(1);
66     reset_shell_mode();
67 }
68
69 static void
70 cleanup(char *files[])
71 {
72     int n;
73
74     for (n = 0; files[n] != 0; ++n) {
75         unlink(files[n]);
76     }
77 }
78
79 static int
80 load_screen(char *filename)
81 {
82     int result;
83
84     if (use_init) {
85         if ((result = scr_init(filename)) != ERR)
86             result = scr_restore(filename);
87     } else {
88         result = scr_set(filename);
89     }
90     return result;
91 }
92
93 /*
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.
97  */
98 static void
99 after_load(void)
100 {
101     overwrite(curscr, stdscr);
102     doupdate();
103 }
104
105 static void
106 show_what(int which, int last)
107 {
108     int y, x, n;
109     time_t now;
110     char *mytime;
111
112     getyx(stdscr, y, x);
113
114     move(0, 0);
115     printw("Saved %d of %d (? for help)", which, last + 1);
116
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]))) {
121             mytime[n] = '\0';
122         } else {
123             break;
124         }
125     }
126     mvprintw(0, (COLS - n - 2), " %s", mytime);
127
128     move(y, x);
129
130     refresh();
131 }
132
133 static int
134 get_command(int which, int last)
135 {
136     int ch;
137
138     timeout(50);
139
140     do {
141         show_what(which, last);
142         ch = getch();
143     } while (ch == ERR);
144
145     return ch;
146 }
147
148 static void
149 show_help(const char **help)
150 {
151     WINDOW *mywin = newwin(LINES, COLS, 0, 0);
152     int n;
153
154     box(mywin, 0, 0);
155     wmove(mywin, 1, 1);
156     for (n = 0; help[n] != 0; ++n) {
157         wmove(mywin, 1 + n, 2);
158         wprintw(mywin, "%.*s", COLS - 4, help[n]);
159     }
160     wgetch(mywin);
161     delwin(mywin);
162     touchwin(stdscr);
163     refresh();
164 }
165
166 static void
167 editor_help(void)
168 {
169     static const char *msgs[] =
170     {
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",
173         "temporary file",
174         "",
175         "Keys:",
176         "   q           quit",
177         "   n           run the screen-loader to show the saved screens",
178         "   <space>     dump a screen",
179         "",
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",
183     };
184     show_help(msgs);
185 }
186
187 static void
188 replay_help(void)
189 {
190     static const char *msgs[] =
191     {
192         "You are now in the screen-loader, which allows you to view",
193         "the dumped/restored screens.",
194         "",
195         "Keys:",
196         "   q           quit",
197         "   <space>     load the next screen",
198         "   <backspace> load the previous screen",
199     };
200     show_help(msgs);
201 }
202
203 static void
204 usage(void)
205 {
206     static const char *msg[] =
207     {
208         "Usage: savescreen [-r] files",
209         "",
210         "Options:",
211         " -i  use scr_init/scr_restore rather than scr_set",
212         " -r  replay the screen-dump files"
213     };
214     unsigned n;
215     for (n = 0; n < SIZEOF(msg); ++n) {
216         fprintf(stderr, "%s\n", msg[n]);
217     }
218     ExitProgram(EXIT_FAILURE);
219 }
220
221 int
222 main(int argc, char *argv[])
223 {
224     int ch;
225     int which = 0;
226     int last;
227     bool replaying = FALSE;
228     bool done = FALSE;
229     char **files;
230
231     while ((ch = getopt(argc, argv, "ir")) != -1) {
232         switch (ch) {
233         case 'i':
234             use_init = TRUE;
235             break;
236         case 'r':
237             replaying = TRUE;
238             break;
239         default:
240             usage();
241             break;
242         }
243     }
244
245     files = argv + optind;
246     last = argc - optind - 1;
247
248     if (replaying) {
249         while (last >= 0 && !fexists(files[last]))
250             --last;
251     }
252
253     initscr();
254     cbreak();
255     noecho();
256     keypad(stdscr, TRUE);
257     curs_set(0);
258     if (has_colors()) {
259         short pair;
260         short color;
261
262         start_color();
263         /*
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.
268          */
269         for (pair = 0; pair < COLOR_PAIRS; ++pair) {
270             color = (short) (pair % (COLORS - 1));
271             init_pair(pair, COLOR_WHITE - color, color);
272         }
273     }
274
275     if (replaying) {
276
277         /*
278          * Use the last file as the initial/current screen.
279          */
280         if (last < 0) {
281             endwin();
282             printf("No screen-dumps given\n");
283             ExitProgram(EXIT_FAILURE);
284         }
285
286         which = last;
287         if (load_screen(files[which]) == ERR) {
288             endwin();
289             printf("Cannot load screen-dump %s\n", files[which]);
290             ExitProgram(EXIT_FAILURE);
291         }
292         after_load();
293
294         while (!done && (ch = getch()) != ERR) {
295             switch (ch) {
296             case 'n':
297                 /*
298                  * If we got a "next" here, skip to the final screen before
299                  * moving to the next process.
300                  */
301                 setup_next();
302                 which = last;
303                 done = TRUE;
304                 break;
305             case 'q':
306                 cleanup(files);
307                 done = TRUE;
308                 break;
309             case KEY_BACKSPACE:
310             case '\b':
311                 if (--which < 0)
312                     which = last;
313                 break;
314             case ' ':
315                 if (++which > last)
316                     which = 0;
317                 break;
318             case '?':
319                 replay_help();
320                 break;
321             default:
322                 beep();
323                 continue;
324             }
325
326             if (ch == 'q') {
327                 ;
328             } else if (scr_restore(files[which]) == ERR) {
329                 endwin();
330                 printf("Cannot load screen-dump %s\n", files[which]);
331                 cleanup(files);
332                 ExitProgram(EXIT_FAILURE);
333             } else {
334                 wrefresh(curscr);
335             }
336         }
337         endwin();
338     } else {
339         int y = 0;
340         int x = 0;
341         int color = 0;
342         int altchars = 0;
343
344         while (!done) {
345             switch (get_command(which, last)) {
346             case 'n':
347                 setup_next();
348                 done = TRUE;
349                 break;
350             case 'q':
351                 cleanup(files);
352                 done = TRUE;
353                 break;
354             case ' ':
355                 if (files[which] != 0) {
356                     show_what(which + 1, last);
357                     if (scr_dump(files[which]) == ERR) {
358                         endwin();
359                         printf("Cannot write screen-dump %s\n", files[which]);
360                         cleanup(files);
361                         done = TRUE;
362                         break;
363                     }
364                     ++which;
365                     if (has_colors()) {
366                         int cx, cy;
367                         short pair = (short) (which % COLOR_PAIRS);
368                         /*
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
372                          * screen.
373                          */
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));
380                             }
381                         }
382                     }
383                 } else {
384                     beep();
385                 }
386                 break;
387             case KEY_LEFT:
388             case 'h':
389                 if (--x < 0)
390                     x = COLS - 1;
391                 break;
392             case KEY_DOWN:
393             case 'j':
394                 if (++y >= LINES)
395                     y = 1;
396                 break;
397             case KEY_UP:
398             case 'k':
399                 if (--y < 1)
400                     y = LINES - 1;
401                 break;
402             case KEY_RIGHT:
403             case 'l':
404                 if (++x >= COLS)
405                     x = 0;
406                 break;
407             case 'a':
408                 altchars = !altchars;
409                 break;
410             case 'c':
411                 color = (color + 1) % COLORS;
412                 break;
413             case '?':
414                 editor_help();
415                 break;
416             default:
417                 beep();
418                 continue;
419             }
420             if (!done) {
421                 attr_t attr = (A_REVERSE | COLOR_PAIR(color * COLORS));
422                 chtype ch2 = (altchars ? ACS_DIAMOND : '#');
423                 move(y, x);
424                 addch(ch2 | attr);
425                 move(y, x);
426             }
427         }
428         endwin();
429     }
430     ExitProgram(EXIT_SUCCESS);
431 }
432
433 #else
434 int
435 main(int argc, char *argv[])
436 {
437     printf("This program requires the screen-dump functions\n");
438     ExitProgram(EXIT_FAILURE);
439 }
440 #endif