1 /* This demo test some of the slsmg features. */
2 #include "config.h"
3 
4 #include <stdio.h>
5 #ifdef HAVE_STDLIB_H
6 # include <stdlib.h>
7 #endif
8 
9 #include <string.h>
10 #include <slang.h>
11 
12 #include "demolib.c"
13 
14 static void menu_loop (void);
15 static int select_menu_item (int i);
16 static void init_colors (void);
17 
main(int argc,char ** argv)18 int main (int argc, char **argv)
19 {
20    if (-1 == demolib_init_terminal (1, 1))
21      return 1;
22 
23    init_colors ();
24 
25    (void) SLtt_set_mouse_mode (1, 0);
26 
27    if (argc <= 1)
28      menu_loop ();
29 
30    do
31      {
32 	argc--;
33 	argv++;
34 
35 	if (-1 == select_menu_item (atoi (*argv)))
36 	  menu_loop ();
37      }
38    while (argc > 1);
39 
40    demolib_exit (0);
41    return 1;
42 }
43 
quit(void)44 static void quit (void)
45 {
46    demolib_exit (0);
47 }
48 
49 static void bce_color_test (void);
50 static void color_test (void);
51 static void color_test1 (void);
52 static void alt_char_test (void);
53 static void esc_seq_test (void);
54 static void ansi_esc_seq_test (void);
55 static void line_test (void);
56 static void mouse_test (void);
57 static void low_level_test (void);
58 static void box_test (void);
59 static void draw_symbols_test (void);
60 static void lr_corner_test (void);
61 static void mono_test (void);
62 static void mono_attr_test (void);
63 static void wrapped_string_test (void);
64 
65 typedef struct
66 {
67    char *name;
68    void (*funct) (void);
69 }
70 Menu_Type;
71 
72 static Menu_Type Root_Menu [] =
73 {
74      {"Color Test", color_test},
75      {"Another Color Test", color_test1},
76      {"BCE Color Test", bce_color_test},
77      {"Alt charset test", alt_char_test},
78      {"Drawing Symbols", draw_symbols_test},
79      {"Key Escape Sequence Report", esc_seq_test},
80 #ifdef IBMPC_SYSTEM
81      {"ANSI Key Escape Sequence Report", ansi_esc_seq_test},
82 #endif
83      {"Line Drawing Test", line_test},
84      {"Test Mouse", mouse_test},
85      {"Box Test", box_test},
86      {"Write to Lower Right Corner Test", lr_corner_test},
87      {"Write Wrapped String Test", wrapped_string_test},
88      {"Test Low Level Functions", low_level_test},
89      {"Test monochrome functions", mono_test},
90      {"Test monochrome attributes", mono_attr_test},
91      {"Quit", quit},
92      {NULL, NULL}
93 };
94 
95 Menu_Type *Current_Menu = Root_Menu;
96 
print_menu(void)97 static void print_menu (void)
98 {
99    int i;
100    int row;
101    Menu_Type *menu;
102 
103    menu = Current_Menu;
104 
105    SLsig_block_signals ();
106 
107    SLsmg_cls ();
108 
109    row = 2;
110    i = 1;
111    while (menu->name != NULL)
112      {
113 	SLsmg_gotorc (row, 3);
114 	SLsmg_printf ("%2X. %s", i, menu->name);
115 	menu++;
116 	row++;
117 	i++;
118      }
119 
120    row = 0;
121    SLsmg_gotorc (row, 1);
122    SLsmg_write_string ("Choose number:");
123 
124    SLsmg_refresh ();
125    SLsig_unblock_signals ();
126 }
127 
select_menu_item(int num)128 static int select_menu_item (int num)
129 {
130    int i = 1;
131    Menu_Type *m = Current_Menu;
132 
133    while (m->name != NULL)
134      {
135 	if (i == num)
136 	  {
137 	     (*m->funct) ();
138 	     return 0;
139 	  }
140 	i++;
141 	m++;
142      }
143 
144    return -1;
145 }
146 
menu_loop(void)147 static void menu_loop (void)
148 {
149    int ch;
150 
151    print_menu ();
152 
153    while (1)
154      {
155 	ch = SLkp_getkey ();
156 
157 	if ((ch == 'q') || (ch == 'Q'))
158 	  quit ();
159 
160 	if ((ch >= '0')
161 	    && (ch <= '9'))
162 	  ch -= '0';
163 	else if ((ch >= 'A') && (ch <= 'Z'))
164 	  ch = 10 + (ch - 'A');
165 	else
166 	  ch = 10 + (ch - 'a');
167 
168 	if (-1 == select_menu_item (ch))
169 	  {
170 	     SLtt_beep ();
171 	     continue;
172 	  }
173 	print_menu ();
174      }
175 }
176 
write_centered_string(char * s,int row)177 static void write_centered_string (char *s, int row)
178 {
179    unsigned int len;
180    int col;
181 
182    if (s == NULL)
183      return;
184 
185    len = strlen (s);
186 
187    /* Want 2 * col + len == SLtt_Screen_Rows */
188    if (len >= (unsigned int) SLtt_Screen_Cols) col = 0;
189    else col = (SLtt_Screen_Cols - (int)len) / 2;
190 
191    SLsmg_gotorc (row, col);
192    SLsmg_write_string (s);
193 }
194 
pre_test(char * title)195 static void pre_test (char *title)
196 {
197    SLsig_block_signals ();
198    SLsmg_cls ();
199    write_centered_string (title, 0);
200 }
201 
post_test(void)202 static void post_test (void)
203 {
204    write_centered_string ("Press any key to return.", SLtt_Screen_Rows - 1);
205    SLsmg_refresh ();
206    SLsig_unblock_signals ();
207    (void) SLkp_getkey ();
208 }
209 
210 /* Various tests */
211 
212 #define NUM_COLORS 16
213 static char *Colors [NUM_COLORS] =
214 {
215    "black/default",
216    "red",
217    "green",
218    "brown",
219    "blue",
220    "magenta",
221    "cyan",
222    "lightgray",
223    "gray",
224    "brightred",
225    "brightgreen",
226    "yellow",
227    "brightblue",
228    "brightmagenta",
229    "brightcyan",
230    "white"
231 };
232 
233 #define MONO_UNDERLINE_COLOR	1
234 #define MONO_BOLD_COLOR		2
235 #define MONO_REVVIDEO_COLOR	3
236 #define MONO_BOLDULINE_COLOR	4
237 #define MONO_REVULINE_COLOR	5
238 
set_mono_color(int obj,SLtt_Char_Type mask)239 static void set_mono_color (int obj, SLtt_Char_Type mask)
240 {
241    SLtt_set_color (obj, NULL, "lightgray", "blue");
242    SLtt_add_color_attribute (obj, mask);
243    SLtt_set_mono (obj, NULL, mask);
244 }
245 
init_mono_colors(void)246 static void init_mono_colors (void)
247 {
248    set_mono_color (MONO_UNDERLINE_COLOR, SLTT_ULINE_MASK);
249    set_mono_color (MONO_REVVIDEO_COLOR, SLTT_REV_MASK);
250    set_mono_color (MONO_BOLDULINE_COLOR, SLTT_BOLD_MASK|SLTT_ULINE_MASK);
251    set_mono_color (MONO_BOLD_COLOR, SLTT_BOLD_MASK);
252    set_mono_color (MONO_REVVIDEO_COLOR, SLTT_REV_MASK);
253    set_mono_color (MONO_REVULINE_COLOR, SLTT_REV_MASK|SLTT_ULINE_MASK);
254 }
255 
init_colors(void)256 static void init_colors (void)
257 {
258    int i;
259    char *fg, *bg;
260 
261    fg = "black";
262    SLtt_set_color (1, NULL, fg, "default");
263    SLtt_set_color (1+NUM_COLORS, NULL, "default", "fg");
264    for (i = 1; i < NUM_COLORS; i++)
265      {
266 	bg = Colors[i];
267 	SLtt_set_color (i + 1, NULL, fg, bg);
268 	SLtt_set_color (i + 1 + NUM_COLORS, NULL, bg, fg);
269      }
270 }
271 
box_test(void)272 static void box_test (void)
273 {
274    char *msg = "This is a box with changing background";
275    int r, c, dr, dc;
276    int color;
277 
278    pre_test ("Box Test");
279 
280    dr = 8;
281    dc = 4 + strlen (msg);
282    r = SLtt_Screen_Rows / 2 - dr/2;
283    c = SLtt_Screen_Cols / 2 - dc/2;
284 
285    SLsmg_set_color (1);
286    SLsmg_set_char_set (1);
287    SLsmg_fill_region (r + 1, c + 1, dr - 2, dc - 2, SLSMG_CKBRD_CHAR);
288    SLsmg_set_char_set (0);
289    SLsmg_set_color (0);
290    SLsmg_gotorc (r + dr/2, c + 2); SLsmg_write_string (msg);
291    SLsmg_draw_box (r, c, dr, dc);
292 
293    SLsmg_refresh ();
294 
295    color = 2;
296    while (0 == SLang_input_pending (10))
297      {
298 	SLsmg_set_color_in_region (color, r, c, dr, dc);
299 	SLsmg_refresh ();
300 	color++;
301 	color = color % NUM_COLORS;
302      }
303    post_test ();
304 }
305 
check_color_support(void)306 static int check_color_support (void)
307 {
308    if (SLtt_Use_Ansi_Colors)
309      return 0;
310 
311    pre_test ("Your terminal does not provide color support.");
312    post_test ();
313    return -1;
314 }
315 
bce_color_test(void)316 static void bce_color_test (void)
317 {
318    int row;
319 
320    if (-1 == check_color_support ())
321      return;
322 
323    pre_test ("Background Color Erase Test");
324 
325    SLtt_set_color (0, NULL, "lightgray", "blue");
326 
327    row = SLtt_Screen_Rows/2;
328    SLsmg_set_color (0);
329    SLsmg_gotorc (row, 1);
330    SLsmg_write_string ("The screen background should be blue.");
331 
332    row += 2;
333    SLsmg_gotorc (row++, 1);
334    SLsmg_write_string ("****If the screen update is slow, then your terminal");
335    SLsmg_gotorc (row++, 1);
336    SLsmg_write_string ("    does not support background-color-erase.");
337    SLsmg_set_color (0);
338    post_test ();
339 }
340 
lr_corner_test(void)341 static void lr_corner_test (void)
342 {
343    pre_test ("Write to Lower Right Corner Test");
344 
345    SLsmg_gotorc (SLtt_Screen_Rows-1, SLtt_Screen_Cols-1);
346    SLsmg_write_string ("Ç");
347 
348    write_centered_string ("A 'Ç' should be in the lower-right corner. (assuming UTF-8)",
349 			  SLtt_Screen_Rows/2);
350 
351    SLsmg_refresh ();
352    SLsig_unblock_signals ();
353    (void) SLkp_getkey ();
354 }
355 
color_test(void)356 static void color_test (void)
357 {
358    int color;
359    int row;
360 
361    if (-1 == check_color_support ())
362      return;
363 
364    pre_test ("Color Test");
365 
366    row = 1;
367 
368    color = 0;
369    while (row < SLtt_Screen_Rows - 1)
370      {
371 	color = color % NUM_COLORS;
372 
373 	SLsmg_gotorc (row, 0);
374 	SLsmg_set_color (color+1+NUM_COLORS);
375 	SLsmg_write_string (Colors[color]);
376 	color++;
377 	SLsmg_set_color (color);
378 	SLsmg_erase_eol ();
379 	row++;
380      }
381 
382    SLsmg_set_color (0);
383    post_test ();
384 }
385 
color_test1(void)386 static void color_test1 (void)
387 {
388    int color;
389    int r0, r1;
390    int c0, c1;
391    unsigned int dr0, dr1, dc0, dc1;
392 
393    if (-1 == check_color_support ())
394      return;
395 
396    pre_test ("Another Color Test");
397 
398    r0 = 1;
399    r1 = SLtt_Screen_Rows / 2;
400    dr0 = r1;
401    dr1 = SLtt_Screen_Rows;
402 
403    c0 = 0;
404    c1 = SLtt_Screen_Cols / 2;
405    dc0 = c1;
406    dc1 = SLtt_Screen_Cols;
407 
408    color = 0;
409    do
410      {
411 	SLsmg_gotorc (r1, 0);
412 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
413 	SLsmg_write_string ("                          ");
414 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
415 	SLsmg_write_string ("X");
416 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
417 	SLsmg_erase_eol ();
418 	SLsmg_refresh ();
419      }
420    while (0 == SLang_input_pending (30));
421    SLang_flush_input ();
422 
423    color = 0;
424    do
425      {
426 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
427 	SLsmg_fill_region (r0, c0, dr0, dc0, ' ');
428 
429 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
430 	SLsmg_fill_region (r0, c1, dr0, dc1, ' ');
431 
432 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
433 	SLsmg_fill_region (r1, c0, dr1, dc0, ' ');
434 
435 	SLsmg_set_color (color); color++; color = color % NUM_COLORS;
436 	SLsmg_fill_region (r1, c1, dr1, dc1, ' ');
437 
438 	SLsmg_refresh ();
439      }
440    while (0 == SLang_input_pending (30));
441 
442    SLsmg_set_color (0);
443    post_test ();
444 }
445 
alt_char_test(void)446 static void alt_char_test (void)
447 {
448    int row, col;
449    SLwchar_Type ch;
450 
451    pre_test ("Alternate Charset Test");
452 
453    row = SLtt_Screen_Rows / 2 - 2;
454    col = 0;
455    for (ch = 32; ch < 128; ch++)
456      {
457 	SLsmg_gotorc (row, col);
458 	SLsmg_write_char (ch);
459 	SLsmg_gotorc (row + 1, col);
460 	SLsmg_set_char_set (1);
461 	SLsmg_write_char (ch);
462 	SLsmg_set_char_set (0);
463 	col++;
464 
465 	if (col > 40)
466 	  {
467 	     col = 0;
468 	     row += 4;
469 	  }
470      }
471 
472    post_test ();
473 }
474 
475 typedef struct
476 {
477    char *name;
478    unsigned char value;
479 }
480 Draw_Symbols_Type;
481 
482 static Draw_Symbols_Type Draw_Symbols [] =
483 {
484      {"SLSMG_HLINE_CHAR",		SLSMG_HLINE_CHAR},
485      {"SLSMG_VLINE_CHAR",		SLSMG_VLINE_CHAR},
486      {"SLSMG_ULCORN_CHAR",		SLSMG_ULCORN_CHAR},
487      {"SLSMG_URCORN_CHAR",		SLSMG_URCORN_CHAR},
488      {"SLSMG_LLCORN_CHAR",		SLSMG_LLCORN_CHAR},
489      {"SLSMG_LRCORN_CHAR",		SLSMG_LRCORN_CHAR},
490      {"SLSMG_RTEE_CHAR",		SLSMG_RTEE_CHAR},
491      {"SLSMG_LTEE_CHAR",		SLSMG_LTEE_CHAR},
492      {"SLSMG_UTEE_CHAR",		SLSMG_UTEE_CHAR},
493      {"SLSMG_DTEE_CHAR",		SLSMG_DTEE_CHAR},
494      {"SLSMG_PLUS_CHAR",		SLSMG_PLUS_CHAR},
495      {"SLSMG_CKBRD_CHAR",		SLSMG_CKBRD_CHAR},
496      {"SLSMG_DIAMOND_CHAR",		SLSMG_DIAMOND_CHAR},
497      {"SLSMG_DEGREE_CHAR",		SLSMG_DEGREE_CHAR},
498      {"SLSMG_PLMINUS_CHAR",		SLSMG_PLMINUS_CHAR},
499      {"SLSMG_BULLET_CHAR",		SLSMG_BULLET_CHAR},
500      {"SLSMG_LARROW_CHAR",		SLSMG_LARROW_CHAR},
501      {"SLSMG_RARROW_CHAR",		SLSMG_RARROW_CHAR},
502      {"SLSMG_DARROW_CHAR",		SLSMG_DARROW_CHAR},
503      {"SLSMG_UARROW_CHAR",		SLSMG_UARROW_CHAR},
504      {"SLSMG_BOARD_CHAR",		SLSMG_BOARD_CHAR},
505      {"SLSMG_BLOCK_CHAR",		SLSMG_BLOCK_CHAR},
506      {NULL, 0}
507 };
508 
draw_symbols_test(void)509 static void draw_symbols_test (void)
510 {
511    int row, col;
512    Draw_Symbols_Type *d;
513 
514    pre_test ("Alternate Charset Test");
515 
516    row = 3;
517    col = 3;
518    d = Draw_Symbols;
519    while (d->name != NULL)
520      {
521 	SLsmg_gotorc (row, col);
522 	SLsmg_set_char_set (1);
523 	SLsmg_write_char (d->value);
524 	SLsmg_set_char_set (0);
525 	SLsmg_printf (":%s", d->name);
526 	col += 40;
527 	if (col >= 80)
528 	  {
529 	     col = 3;
530 	     row++;
531 	  }
532 	d++;
533      }
534    post_test ();
535 }
536 
line_test(void)537 static void line_test (void)
538 {
539    int row, col;
540    pre_test ("Line Test");
541 
542    row = 4;
543    col = 2;
544    SLsmg_gotorc (row, col);
545    SLsmg_draw_hline (10);
546    SLsmg_write_string ("Result of SLsmg_draw_hline(10)");
547    SLsmg_draw_vline (5);
548    SLsmg_write_string ("Result of SLsmg_draw_vline(5)");
549 
550    post_test ();
551 }
552 
do_esc_seq_test(char * testname)553 static void do_esc_seq_test (char *testname)
554 {
555    int row;
556    unsigned char ch;
557    unsigned char buf[80], *b;
558 
559    pre_test (testname);
560 
561    while (1)
562      {
563 	row = SLtt_Screen_Rows / 2;
564 
565 	SLsmg_gotorc (row, 0);
566 	SLsmg_write_string ("Press key: (RETURN quits)");
567 	SLsmg_refresh ();
568 
569 	ch = SLang_getkey ();
570 	SLang_ungetkey (ch);
571 	if (ch == '\r')
572 	  break;
573 
574 	SLsmg_gotorc (row+1, 0);
575 	SLsmg_write_string ("Key returned \"");
576 
577 	b = buf;
578 	do
579 	  {
580 	     ch = SLang_getkey ();
581 	     if (ch < ' ')
582 	       {
583 		  *b++ = '^';
584 		  ch += '@';
585 		  *b++ = ch;
586 	       }
587 	     else if (ch >= 127)
588 	       {
589 		  sprintf ((char *) b, "\\x%02X", ch);
590 		  b += strlen ((char *) b);
591 	       }
592 	     else if ((ch == '"') || (ch == '\\'))
593 	       {
594 		  *b++ = '\\';
595 		  *b++ = ch;
596 	       }
597 	     else *b++ = ch;
598 	  }
599 	while (SLang_input_pending (3) > 0);
600 	*b++ = '"';
601 	*b = 0;
602 	SLsmg_write_string ((char *) buf);
603 	SLsmg_erase_eol ();
604 	SLsmg_refresh ();
605      }
606 
607    post_test ();
608 }
609 
esc_seq_test(void)610 static void esc_seq_test (void)
611 {
612 #ifdef IBMPC_SYSTEM
613    SLgetkey_map_to_ansi (0);
614 #endif
615    do_esc_seq_test ("Escape Sequence Report");
616 }
617 
618 #ifdef IBMPC_SYSTEM
ansi_esc_seq_test(void)619 static void ansi_esc_seq_test (void)
620 {
621    SLgetkey_map_to_ansi (1);
622    do_esc_seq_test ("ANSI Escape Sequence Report");
623 }
624 #endif
625 
mouse_test(void)626 static void mouse_test (void)
627 {
628    int row;
629    int b, x, y;
630 
631    pre_test ("Mouse Test");
632 
633    row = SLtt_Screen_Rows / 2;
634 
635    SLsmg_gotorc (row, 0);
636    SLsmg_write_string ("Click Mouse: ");
637    SLsmg_refresh ();
638 
639    if ((27 != SLang_getkey ())
640        || ('[' != SLang_getkey ())
641        || ('M' != SLang_getkey ()))
642      {
643 	SLsmg_gotorc (row, 0);
644 	SLsmg_write_string ("That did not appear to be a mouse escape sequence");
645 	post_test ();
646 	return;
647      }
648 
649    b = SLang_getkey () - ' ';
650    x = SLang_getkey () - ' ';
651    y = SLang_getkey () - ' ';
652 
653    SLsmg_gotorc (row, 0);      SLsmg_printf ("Button: %d     ", b);
654    SLsmg_gotorc (row + 1, 0);  SLsmg_printf ("Column: %d", x);
655    SLsmg_gotorc (row + 2, 0);  SLsmg_printf ("   Row: %d", y);
656 
657    post_test ();
658 }
659 
mono_test_internal(int uac,char * msg)660 static void mono_test_internal (int uac, char *msg)
661 {
662    int row;
663    int c;
664 
665    c = SLtt_Use_Ansi_Colors;
666    SLsmg_suspend_smg ();
667    SLtt_Use_Ansi_Colors = uac;
668    SLsmg_resume_smg ();
669 
670    init_mono_colors ();
671 
672    SLsmg_normal_video ();
673    SLsmg_cls ();
674 
675    pre_test ("Mono Test");
676 
677    row = 2;
678    SLsmg_gotorc (row, 0);
679    SLsmg_write_string (msg);
680 
681    row = SLtt_Screen_Rows / 2;
682 
683    SLsmg_gotorc (row, 0);
684    SLsmg_set_color (MONO_UNDERLINE_COLOR);
685    SLsmg_write_string ("This line should be completely underlined");
686    SLsmg_erase_eol ();
687 
688    row++;
689    SLsmg_gotorc (row, 0);
690    SLsmg_set_color (MONO_REVVIDEO_COLOR);
691    SLsmg_write_string ("This line should be in reverse video");
692 
693    row++;
694    SLsmg_gotorc (row, 0);
695    SLsmg_set_color (MONO_BOLD_COLOR);
696    SLsmg_write_string ("This line should appear in bold");
697 
698    row++;
699    SLsmg_gotorc (row, 0);
700    SLsmg_set_color (MONO_BOLDULINE_COLOR);
701    SLsmg_write_string ("This line should appear underlined and in bold");
702 
703    row++;
704    SLsmg_gotorc (row, 0);
705    SLsmg_set_color (MONO_REVULINE_COLOR);
706    SLsmg_write_string ("This line should appear completely underlined in reverse video");
707    SLsmg_erase_eol ();
708 
709    SLsmg_refresh ();
710    SLsmg_set_color (0);
711    post_test ();
712    SLtt_Use_Ansi_Colors = c;
713    init_colors ();
714 }
715 
mono_test(void)716 static void mono_test (void)
717 {
718    mono_test_internal (0, "This test uses SLtt_Use_Ansi_Colors=0, assuming a monochrome terminal");
719    mono_test_internal (1, "This test uses SLtt_Use_Ansi_Colors=1, assuming a color terminal");
720 }
721 
mono_attr_test(void)722 static void mono_attr_test (void)
723 {
724    int row;
725    int col;
726    int c, i;
727    int num_chars;
728 #define NUM_MONO_COLORS 6
729    static int colors [NUM_MONO_COLORS] =
730      {
731 	MONO_REVULINE_COLOR, MONO_BOLD_COLOR, MONO_REVVIDEO_COLOR,
732 	MONO_BOLDULINE_COLOR, MONO_REVULINE_COLOR
733      };
734    pre_test ("Mono Attr Test");
735 
736    init_mono_colors ();
737    c = 0, i = 0;
738    num_chars = NUM_MONO_COLORS;
739    col = 0;
740    do
741      {
742 	SLsmg_set_color (0);
743 	SLsmg_gotorc (2, 0); SLsmg_erase_eos ();
744 	for (row = 2; row < SLtt_Screen_Rows - 2; row++)
745 	  {
746 	     col = col % SLtt_Screen_Cols;
747 	     while (col < SLtt_Screen_Cols)
748 	       {
749 		  SLsmg_gotorc (row, col);
750 		  c = c % NUM_MONO_COLORS;
751 		  SLsmg_set_color (colors[c]);
752 		  SLsmg_write_char ('A' + i); col++;
753 		  SLsmg_set_color (colors[row % NUM_MONO_COLORS]);
754 		  SLsmg_write_string ("**********");
755 		  col += 10;
756 		  i = (i + 1) % num_chars;
757 		  /* if (i == 0) col += 9; */
758 		  c++;
759 	       }
760 	     col = 0;
761 	  }
762 	SLsmg_gotorc (0,0);
763 	SLsmg_refresh ();
764      }
765    while (0 == SLang_input_pending (10));
766 
767    init_colors ();
768    post_test ();
769 }
770 
wrapped_string_test(void)771 static void wrapped_string_test (void)
772 {
773    int row, col;
774    unsigned int dr, dc;
775    char *str;
776 
777    dc = 15;
778    dr = 10;
779    row = (SLtt_Screen_Rows - (int)dr)/2;
780    col = (SLtt_Screen_Cols - (int)dc)/2;
781 
782    pre_test ("Wrapped-string test");
783 
784    str = "This is a string that should be wrapped in a 12x15 cell.\n\
785 It even contains a\n\
786 couple of newline chacters\n\
787 for fun.";
788 
789    SLsmg_fill_region (row-1, col-1, dr+2, dc+2, '.');
790    SLsmg_draw_box (row-1, col-1, dr+2, dc+2);
791    SLsmg_write_wrapped_string ((SLuchar_Type *)str, row, col, dr, dc, 1);
792 
793    post_test ();
794 
795    if (SLutf8_is_utf8_mode ())
796      {
797 	unsigned int len, i;
798 	static char *long_strings[2] =
799 	  {
800 	     "พระปกเกศกองบู๊กู้ขึ้นใหม่\
801 สององค์ไซร้โง่เขลาเบาปัญญา\
802 บ้านเมืองจึงวิปริตเป็นนักหนา\
803 หมายจะฆ่ามดชั่วตัวสำคัญ\
804 รับหมาป่าเข้ามาเลยอาสัญ\
805 ใช้สาวนั้นเป็นชนวนชื่นชวนใจ\
806 ฤๅหาใครค้ำชูกู้บร",
807 	     "(CJK)を選択しる違いを知りたければ\
808 を設|ctrls:\x01\x02\x84|定した状態とを選択した状態の両方で"
809 	  };
810 	static char *short_strings[2] =
811 	  {
812 	     "รั\tบ\tหมาป่าเข้ามาเลยอาสัญ\xAB",
813 	     "定した状態\xAB"
814 	  };
815 
816 	for (i = 0; i < 2; i++)
817 	  {
818 	     str = long_strings[i];
819 
820 	     pre_test ("Wrapped-string test (UTF-8)");
821 	     SLsmg_fill_region (row-1, col-1, dr+2, dc+2, '.');
822 	     SLsmg_draw_box (row-1, col-1, dr+2, dc+2);
823 	     SLsmg_write_wrapped_string ((SLuchar_Type *)str, row, col, dr, dc, 1);
824 	     post_test ();
825 
826 	     pre_test ("SLsmg_strwidth test (UTF-8)");
827 	     col = 5;
828 	     str = short_strings[i];
829 	     SLsmg_cls ();
830 	     SLsmg_gotorc (row, col);
831 	     len = SLsmg_strwidth ((SLuchar_Type*)str, (SLuchar_Type*)str + strlen (str));
832 	     SLsmg_write_string (str);
833 	     SLsmg_gotorc (row+1, col+len);
834 	     SLsmg_write_string ("^---End of above line should be here");
835 	     post_test ();
836 
837 	     pre_test ("SLsmg_write_nstring test (UTF-8)");
838 	     str = long_strings[i];
839 	     SLsmg_gotorc (row+2, col);
840 	     len = 15;
841 	     SLsmg_write_nstring (str, len);
842 	     SLsmg_write_string ("X");
843 	     SLsmg_gotorc (row+3, col+len+1);
844 	     SLsmg_write_string ("^---End of the line with an X should be here");
845 	     post_test ();
846 	  }
847      }
848 }
849 
low_level_test(void)850 static void low_level_test (void)
851 {
852    int mid, bot;
853    int r;
854 
855    if (SLtt_Term_Cannot_Scroll)
856      {
857 	pre_test ("Sorry!  Your terminal lacks scrolling capability.");
858 	post_test ();
859 	return;
860      }
861 
862    if (-1 == SLsmg_suspend_smg ())
863      SLang_exit_error ("SLsmg_suspend_smg failed");
864 
865    if (-1 == SLtt_init_video ())
866      SLang_exit_error ("SLang_init_video failed");
867 
868    SLtt_get_screen_size ();
869 
870    mid = SLtt_Screen_Rows/2;
871    bot = SLtt_Screen_Rows - 1;
872 
873    SLtt_cls ();
874    SLtt_goto_rc (0, 0);
875    SLtt_write_string ("The following set of tests are designed to test the display system.");
876    SLtt_goto_rc (1, 0);
877    SLtt_write_string ("There should be a line of text in the middle and one at the bottom.");
878    SLtt_goto_rc (mid, 0);
879    SLtt_write_string ("This line is in the middle.");
880    SLtt_goto_rc (bot, 0);
881    SLtt_write_string ("This line is at the bottom.");
882 
883    SLtt_goto_rc (2, 0);
884    SLtt_write_string ("Press return now.");
885    SLtt_flush_output ();
886    SLang_flush_input ();
887    (void) SLang_getkey ();
888 
889    SLtt_goto_rc (2, 0);
890    SLtt_write_string ("The middle row should slowly move down next the bottom and then back up.");
891    SLtt_goto_rc (mid - 1, 0);
892    SLtt_write_string ("This line should not move.");
893    SLtt_goto_rc (mid + 1, 0);
894    SLtt_write_string ("This line should vanish at the bottom");
895    SLtt_goto_rc (mid + 1, SLtt_Screen_Cols - 5);
896    SLtt_write_string ("End->");
897    SLtt_flush_output ();
898 
899    SLtt_set_scroll_region (mid, bot - 1);
900 
901    r = (bot - mid) - 1;
902    while (r > 0)
903      {
904 	(void) SLang_input_pending (2); /* 3/10 sec delay */
905 	SLtt_goto_rc (0,0);		       /* relative to scroll region */
906 	SLtt_reverse_index (1);
907 	SLtt_flush_output ();
908 	r--;
909      }
910 
911    r = (bot - mid) - 1;
912    while (r > 0)
913      {
914 	(void) SLang_input_pending (2); /* 3/10 sec delay */
915 	SLtt_goto_rc (0,0);		       /* relative to scroll region */
916 	SLtt_delete_nlines (1);
917 	SLtt_flush_output ();
918 	r--;
919      }
920 
921    SLtt_reset_scroll_region ();
922    SLtt_goto_rc (mid - 1, 0);
923    SLtt_write_string ("Now the bottom will come up and clear the lines below");
924 
925    SLtt_set_scroll_region (mid, bot);
926    r = (bot - mid) + 1;
927    while (r > 0)
928      {
929 	(void) SLang_input_pending (2); /* 3/10 sec delay */
930 	SLtt_goto_rc (0,0);		       /* relative to scroll region */
931 	SLtt_delete_nlines (1);
932 	SLtt_flush_output ();
933 	r--;
934      }
935 
936    SLtt_reset_scroll_region ();
937    SLtt_goto_rc (3,0);
938    SLtt_write_string ("This line will go down and vanish");
939    SLtt_set_scroll_region (3, mid - 2);
940 
941    r = ((mid - 2) - 3) + 1;
942    while (r > 0)
943      {
944 	(void) SLang_input_pending (3); /* 3/10 sec delay */
945 	SLtt_goto_rc (0,0);		       /* relative to scroll region */
946 	SLtt_reverse_index (1);
947 	SLtt_flush_output ();
948 	r--;
949      }
950 
951    SLtt_reset_scroll_region ();
952    SLtt_set_scroll_region (1,1); SLtt_goto_rc (0,0);
953    SLtt_delete_nlines (1);
954    SLtt_reset_scroll_region ();
955    SLtt_set_scroll_region (2,2); SLtt_goto_rc (0,0);
956    SLtt_reverse_index (1);
957    SLtt_reset_scroll_region ();
958 
959    SLtt_goto_rc (1, 10);
960    SLtt_write_string ("Press Any Key To Continue.");
961    SLtt_flush_output ();
962    r = 15;
963    if (0 == SLtt_Term_Cannot_Insert) while (r)
964      {
965 	r--;
966 	SLtt_goto_rc (1, 0);
967 	SLtt_begin_insert ();
968 	SLtt_putchar (' ');
969 	SLtt_end_insert ();
970 	SLtt_flush_output ();
971 	SLang_input_pending (2);
972      }
973 
974    SLang_flush_input ();
975    (void) SLang_getkey ();
976 
977    SLtt_reset_video ();
978    SLsmg_resume_smg ();
979 }
980 
981