]> ncurses.scripts.mit.edu Git - ncurses.git/blob - tack/sync.c
ncurses 5.0
[ncurses.git] / tack / sync.c
1 /*
2 ** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
3 ** 
4 ** This file is part of TACK.
5 ** 
6 ** TACK is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2, or (at your option)
9 ** any later version.
10 ** 
11 ** TACK is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ** GNU General Public License for more details.
15 ** 
16 ** You should have received a copy of the GNU General Public License
17 ** along with TACK; see the file COPYING.  If not, write to
18 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ** Boston, MA 02111-1307, USA.
20 */
21
22 #include <tack.h>
23 #include <time.h>
24
25 MODULE_ID("$Id: sync.c,v 1.1 1998/01/10 00:31:07 tom Exp $")
26
27 /* terminal-synchronization and performance tests */
28
29 static void sync_home(struct test_list *, int *, int *);
30 static void sync_lines(struct test_list *, int *, int *);
31 static void sync_clear(struct test_list *, int *, int *);
32 static void sync_summary(struct test_list *, int *, int *);
33
34 struct test_list sync_test_list[] = {
35         {MENU_NEXT, 0, 0, 0, "b) baud rate test", sync_home, 0},
36         {MENU_NEXT, 0, 0, 0, "l) scroll performance", sync_lines, 0},
37         {MENU_NEXT, 0, 0, 0, "c) clear screen performance", sync_clear, 0},
38         {MENU_NEXT, 0, 0, 0, "p) summary of results", sync_summary, 0},
39         {0, 0, 0, 0, txt_longer_test_time, longer_test_time, 0},
40         {0, 0, 0, 0, txt_shorter_test_time, shorter_test_time, 0},
41         {MENU_LAST, 0, 0, 0, 0, 0, 0}
42 };
43
44 struct test_menu sync_menu = {
45         0, 'n', 0,
46         "Performance tests", "perf", "n) run standard tests",
47         sync_test, sync_test_list, 0, 0, 0
48 };
49
50 int tty_can_sync;               /* TRUE if tty_sync_error() returned FALSE */
51 int tty_newline_rate;           /* The number of newlines per second */
52 int tty_clear_rate;             /* The number of clear-screens per second */
53 int tty_cps;                    /* The number of characters per second */
54
55 #define TTY_ACK_SIZE 64
56
57 int ACK_terminator;             /* terminating ACK character */
58 int ACK_length;                 /* length of ACK string */
59 const char *tty_ENQ;            /* enquire string */
60 char tty_ACK[TTY_ACK_SIZE];     /* ACK response, set by tty_sync_error() */
61
62 /*****************************************************************************
63  *
64  * Terminal synchronization.
65  *
66  *      These functions handle the messy business of enq-ack handshaking
67  *      for timing purposes.
68  *
69  *****************************************************************************/
70
71 int
72 tty_sync_error(void)
73 {
74         int ch, trouble, ack;
75
76         trouble = FALSE;
77         for (;;) {
78                 tt_putp(tty_ENQ);       /* send ENQ */
79                 ch = getnext(STRIP_PARITY);
80                 event_start(TIME_SYNC); /* start the timer */
81
82                 /*
83                    The timer doesn't start until we get the first character.
84                    After that I expect to get the remaining characters of
85                    the acknowledge string in a short period of time.  If
86                    that is not true then these characters are coming from
87                    the user and we need to send the ENQ sequence out again.
88                 */
89                 for (ack = 0; ; ) {
90                         if (ack < TTY_ACK_SIZE - 2) {
91                                 tty_ACK[ack] = ch;
92                                 tty_ACK[ack + 1] = '\0';
93                         }
94                         if (ch == ACK_terminator) {
95                                 return trouble;
96                         }
97                         if (++ack >= ACK_length) {
98                                 return trouble;
99                         }
100                         ch = getnext(STRIP_PARITY);
101                         if (event_time(TIME_SYNC) > 400000) {
102                                 break;
103                         }
104                 }
105
106                 set_attr(0);    /* just in case */
107                 put_crlf();
108                 if (trouble) {
109                         /* The terminal won't sync.  Life is not good. */
110                         return TRUE;
111                 }
112                 put_str(" -- sync -- ");
113                 trouble = TRUE;
114         }
115 }
116
117 /*
118 **      flush_input()
119 **
120 **      Throw away any output.
121 */
122 void 
123 flush_input(void)
124 {
125         if (tty_can_sync == SYNC_TESTED && ACK_terminator >= 0) {
126                 (void) tty_sync_error();
127         } else {
128                 spin_flush();
129         }
130 }
131
132 /*
133 **      probe_enq_ok()
134 **
135 **      does the terminal do enq/ack handshaking?
136 */
137 static void 
138 probe_enq_ok(void)
139 {
140         int tc, len, ulen;
141
142         put_str("Testing ENQ/ACK, standby...");
143         fflush(stdout);
144         can_test("u8 u9", FLAG_TESTED);
145
146         tty_ENQ = user9 ? user9 : "\005";
147         tc_putp(tty_ENQ);
148         event_start(TIME_SYNC); /* start the timer */
149         read_key(tty_ACK, TTY_ACK_SIZE - 1);
150
151         if (event_time(TIME_SYNC) > 400000 || tty_ACK[0] == '\0') {
152                 /* These characters came from the user.  Sigh. */
153                 tty_can_sync = SYNC_FAILED;
154                 ptext("\nThis program expects the ENQ sequence to be");
155                 ptext(" answered with the ACK character.  This will help");
156                 ptext(" the program reestablish synchronization when");
157                 ptextln(" the terminal is overrun with data.");
158                 ptext("\nENQ sequence from (u9): ");
159                 putln(expand(tty_ENQ));
160                 ptext("ACK recieved: ");
161                 putln(expand(tty_ACK));
162                 len = user8 ? strlen(user8) : 0;
163                 sprintf(temp, "Length of ACK %d.  Expected length of ACK %d.",
164                         (int) strlen(tty_ACK), len);
165                 ptextln(temp);
166                 if (len) {
167                         temp[0] = user8[len - 1];
168                         temp[1] = '\0';
169                         ptext("Terminating character found in (u8): ");
170                         putln(expand(temp));
171                 }
172                 return;
173         }
174
175         tty_can_sync = SYNC_TESTED;
176         if ((len = strlen(tty_ACK)) == 1) {
177                 /* single character acknowledge string */
178                 ACK_terminator = tty_ACK[0];
179                 ACK_length = 4096;
180                 return;
181         }
182         tc = tty_ACK[len - 1];
183         if (user8) {
184                 ulen = strlen(user8);
185                 if (tc == user8[ulen - 1]) {
186                         /* ANSI style acknowledge string */
187                         ACK_terminator = tc;
188                         ACK_length = 4096;
189                         return;
190                 }
191         }
192         /* fixed length acknowledge string */
193         ACK_length = len;
194         ACK_terminator = -2;
195 }
196
197 /*
198 **      verify_time()
199 **
200 **      verify that the time tests are ready to run.
201 **      If the baud rate is not set then compute it.
202 */
203 void
204 verify_time(void)
205 {
206         int status, ch;
207
208         if (tty_can_sync == SYNC_FAILED) {
209                 return;
210         }
211         probe_enq_ok();
212         put_crlf();
213         if (tty_can_sync == SYNC_TESTED) {
214                 put_crlf();
215                 if (ACK_terminator >= 0) {
216                         ptext("ACK terminating character: ");
217                         temp[0] = ACK_terminator;
218                         temp[1] = '\0';
219                         ptextln(expand(temp));
220                 } else {
221                         sprintf(temp, "Fixed length ACK, %d characters",
222                                 ACK_length);
223                         ptextln(temp);
224                 }
225         }
226         if (tty_baud_rate == 0) {
227                 sync_home(&sync_test_list[0], &status, &ch);
228         }
229 }
230
231 /*****************************************************************************
232  *
233  * Terminal performance tests
234  *
235  *      Find out how fast the terminal can:
236  *        1) accept characters
237  *        2) scroll the screen
238  *        3) clear the screen
239  *
240  *****************************************************************************/
241
242 /*
243 **      sync_home(test_list, status, ch)
244 **
245 **      Baudrate test
246 */
247 void
248 sync_home(
249         struct test_list *t,
250         int *state,
251         int *ch)
252 {
253         int j, k;
254         unsigned long rate;
255
256         if (!cursor_home && !cursor_address && !row_address) {
257                 ptext("Terminal can not home cursor.  ");
258                 generic_done_message(t, state, ch);
259                 return;
260         }
261         if (skip_pad_test(t, state, ch,
262                 "(home) Start baudrate search")) {
263                 return;
264         }
265         pad_test_startup(1);
266         do {
267                 go_home();
268                 for (j = 1; j < lines; j++) {
269                         for (k = 0; k < columns; k++) {
270                                 if (k & 0xF) {
271                                         put_this(letter);
272                                 } else {
273                                         put_this('.');
274                                 }
275                         }
276                         SLOW_TERMINAL_EXIT;
277                 }
278                 NEXT_LETTER;
279         } while(still_testing());
280         pad_test_shutdown(t, auto_right_margin == 0);
281         /* note:  tty_frame_size is the real framesize times two.
282            This takes care of half bits. */
283         rate = (tx_cps * tty_frame_size) >> 1;
284         if (rate > tty_baud_rate) {
285                 tty_baud_rate = rate;
286         }
287         if (tx_cps > tty_cps) {
288                 tty_cps = tx_cps;
289         }
290         sprintf(temp, "%d characters per second.  Baudrate %d  ", tx_cps, j);
291         ptext(temp);
292         generic_done_message(t, state, ch);
293 }
294
295 /*
296 **      sync_lines(test_list, status, ch)
297 **
298 **      How many newlines/second?
299 */
300 static void
301 sync_lines(
302         struct test_list *t,
303         int *state,
304         int *ch)
305 {
306         int j;
307
308         if (skip_pad_test(t, state, ch,
309                 "(nel) Start scroll performance test")) {
310                 return;
311         }
312         pad_test_startup(0);
313         repeats = 100;
314         do {
315                 sprintf(temp, "%d", test_complete);
316                 put_str(temp);
317                 put_newlines(repeats);
318         } while(still_testing());
319         pad_test_shutdown(t, 0);
320         j = sliding_scale(tx_count[0], 1000000, usec_run_time);
321         if (j > tty_newline_rate) {
322                 tty_newline_rate = j;
323         }
324         sprintf(temp, "%d linefeeds per second.  ", j);
325         ptext(temp);
326         generic_done_message(t, state, ch);
327 }
328
329 /*
330 **      sync_clear(test_list, status, ch)
331 **
332 **      How many clear-screens/second?
333 */
334 static void
335 sync_clear(
336         struct test_list *t,
337         int *state,
338         int *ch)
339 {
340         int j;
341
342         if (!clear_screen) {
343                 ptext("Terminal can not clear-screen.  ");
344                 generic_done_message(t, state, ch);
345                 return;
346         }
347         if (skip_pad_test(t, state, ch,
348                 "(clear) Start clear-screen performance test")) {
349                 return;
350         }
351         pad_test_startup(0);
352         repeats = 20;
353         do {
354                 sprintf(temp, "%d", test_complete);
355                 put_str(temp);
356                 for (j = 0; j < repeats; j++) {
357                         put_clear();
358                 }
359         } while(still_testing());
360         pad_test_shutdown(t, 0);
361         j = sliding_scale(tx_count[0], 1000000, usec_run_time);
362         if (j > tty_clear_rate) {
363                 tty_clear_rate = j;
364         }
365         sprintf(temp, "%d clear-screens per second.  ", j);
366         ptext(temp);
367         generic_done_message(t, state, ch);
368 }
369
370 /*
371 **      sync_symmary(test_list, status, ch)
372 **
373 **      Print out the test results.
374 */
375 static void
376 sync_summary(
377         struct test_list *t,
378         int *state,
379         int *ch)
380 {
381         char size[32];
382
383         put_crlf();
384         ptextln("Terminal  size    characters/sec linefeeds/sec  clears/sec");
385         sprintf(size, "%dx%d", columns, lines);
386         sprintf(temp, "%-10s%-11s%11d   %11d %11d", tty_basename, size,
387                 tty_cps, tty_newline_rate, tty_clear_rate);
388         ptextln(temp);
389         generic_done_message(t, state, ch);
390 }
391
392 /*
393 **      sync_test(menu)
394 **
395 **      Run at the beginning of the pad tests and function key tests
396 */
397 void
398 sync_test(
399         struct test_menu *menu)
400 {
401         control_init();
402         if (tty_can_sync == SYNC_NOT_TESTED) {
403                 verify_time();
404         }
405         if (menu->menu_title) {
406                 put_crlf();
407                 ptextln(menu->menu_title);
408         }
409 }
410
411 /*
412 **      sync_handshake(test_list, status, ch)
413 **
414 **      Test or retest the ENQ/ACK handshake
415 */
416 void
417 sync_handshake(
418         struct test_list *t GCC_UNUSED,
419         int *state GCC_UNUSED,
420         int *ch GCC_UNUSED)
421 {
422         tty_can_sync = SYNC_NOT_TESTED;
423         verify_time();
424 }