Home | History | Annotate | Line # | Download | only in EXAMPLES
ex1.c revision 1.4.18.1
      1 /*	$NetBSD: ex1.c,v 1.4.18.1 2007/01/21 11:38:59 blymn Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #ifndef lint
     34 static char copyright[] =
     35 "@(#) Copyright (c) 1992, 1993\n\
     36 	The Regents of the University of California.  All rights reserved.\n";
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 static char sccsid[] = "@(#)ex1.c	8.1 (Berkeley) 6/4/93";
     41 #endif /* not lint */
     42 #include <sys/types.h>
     43 #include <curses.h>
     44 #include <stdio.h>
     45 #include <signal.h>
     46 #include <locale.h>
     47 #include <assert.h>
     48 
     49 #define YSIZE 4
     50 #define XSIZE 30
     51 
     52 void quit( int );
     53 
     54 main()
     55 {
     56 	int i, j, c, n = 0, ac = 0;
     57 	size_t len;
     58 	char id[100];
     59 	FILE *fp;
     60 	char *s;
     61     cchar_t cc, ncc, cstr[ 128 ], icc, icstr[ 128 ], bcc;
     62     int wc_on = 0, wgc_on = 0;
     63     char mbs[] = "";
     64     char mbstr[] = " (Liji)";
     65     wchar_t wstr[ 128 ], owstr[ 4 ], gwstr[ 128 ], iwstr[ 128 ];
     66     int wslen = 0;
     67     wint_t wc;
     68     char nostr[ 128 ];
     69     attr_t al[ 15 ] = { WA_BLINK,
     70                         WA_BOLD,
     71                         WA_DIM,
     72                         WA_LOW,
     73                         WA_TOP,
     74                         WA_INVIS,
     75                         WA_HORIZONTAL,
     76                         WA_VERTICAL,
     77                         WA_LEFT,
     78                         WA_RIGHT,
     79                         WA_PROTECT,
     80                         WA_REVERSE,
     81                         WA_STANDOUT,
     82                         WA_UNDERLINE };
     83 
     84     fprintf( stderr, "Current locale: %s\n", setlocale(LC_ALL, ""));
     85     if (( wslen =  mbstowcs( &cc.vals[ 0 ], mbs, strlen( mbs ))) < 0 ) {
     86         fprintf( stderr, "mbstowcs() failed\n" );
     87         return -1;
     88     }
     89     fprintf( stderr, "WC string length: %d\n", wslen );
     90     fprintf( stderr, "WC width: %d\n", wcwidth( cc.vals[ 0 ]));
     91     cc.elements = ncc.elements = 8;
     92     cc.attributes = ncc.attributes = 0;
     93     ncc.vals[ 0 ] = 0xda00;
     94     cc.vals[ 1 ] = ncc.vals[ 1 ] = 0xda01;
     95     cc.vals[ 2 ] = ncc.vals[ 2 ] = 0xda02;
     96     cc.vals[ 3 ] = ncc.vals[ 3 ] = 0xda03;
     97     cc.vals[ 4 ] = ncc.vals[ 4 ] = 0xda04;
     98     cc.vals[ 5 ] = ncc.vals[ 5 ] = 0xda05;
     99     cc.vals[ 6 ] = ncc.vals[ 6 ] = 0xda06;
    100     cc.vals[ 7 ] = ncc.vals[ 7 ] = 0xda07;
    101 
    102     if (( wslen =  mbstowcs( &wstr, mbstr, strlen( mbstr ))) < 0 ) {
    103         fprintf( stderr, "mbstowcs() failed\n" );
    104         return -1;
    105     }
    106 
    107     for ( i = 0; i < wslen; i++ ) {
    108         cstr[ i ].vals[ 0 ] = wstr[ i ];
    109     }
    110     cstr[ wslen ].vals[ 0 ] = 0;
    111 
    112     bcc.elements = 8;
    113     bcc.attributes = 0;
    114     bcc.vals[ 0 ] = L'_';
    115     bcc.vals[ 1 ] = 0xda01;
    116     bcc.vals[ 2 ] = 0xda02;
    117     bcc.vals[ 3 ] = 0xda03;
    118     bcc.vals[ 4 ] = 0xda04;
    119     bcc.vals[ 5 ] = 0xda05;
    120     bcc.vals[ 6 ] = 0xda06;
    121     bcc.vals[ 7 ] = 0xda07;
    122 
    123 	initscr();			/* Always call initscr() first */
    124 	signal(SIGINT, quit);		/* Make sure wou have a 'cleanup' fn */
    125 	crmode();			/* We want cbreak mode */
    126 	noecho();			/* We want to have control of chars */
    127 	delwin(stdscr);			/* Create our own stdscr */
    128 	stdscr = newwin(YSIZE, XSIZE, 1, 1);
    129 	flushok(stdscr, TRUE);		/* Enable flushing of stdout */
    130 	scrollok(stdscr, TRUE);		/* Enable scrolling */
    131 	erase();			/* Initially, clear the screen */
    132 
    133 	standout();
    134 	move(0,0);
    135 	while (1) {
    136         if ( !wgc_on ) {
    137 		    c = getchar();
    138 		    switch(c) {
    139 		        case 'q':		/* Quit on 'q' */
    140 			        quit( 0 );
    141 			        break;
    142                 case 'p':
    143                     keypad( stdscr, TRUE );
    144                     break;
    145                 case 'P':
    146                     keypad( stdscr, FALSE );
    147                     break;
    148                 case 'g':
    149                     wgc_on = 1;
    150                     echo();
    151                     break;
    152                 case 'b':
    153                     get_wstr( gwstr );
    154                     move( 1, 0 );
    155                     addstr( "Input:" );
    156                     addwstr( gwstr );
    157                     refresh();
    158                     break;
    159                 case 'h':
    160                     move( 0, 0 );
    161                     in_wch( &icc );
    162                     move( 1, 0 );
    163                     add_wch( &icc );
    164                     refresh();
    165                     break;
    166                 case 'y':
    167                     move( 0, 0 );
    168                     in_wchstr( icstr );
    169                     move( 1, 0 );
    170                     add_wchstr( icstr );
    171                     refresh();
    172                     break;
    173                 case 'u':
    174                     move( 0, 0 );
    175                     inwstr( iwstr );
    176                     move( 1, 0 );
    177                     addwstr( iwstr );
    178                     refresh();
    179                     break;
    180                 case 'i':
    181                     move( 0, 0 );
    182                     hline_set( &cc, 20 );
    183                     move( 0, 0 );
    184                     vline_set( &cc, 20 );
    185                     refresh();
    186                     break;
    187                 case 'o':
    188                     clrtobot();
    189                     refresh();
    190                     break;
    191 		        case 's':		/* Go into standout mode on 's' */
    192 			        standout();
    193 			        break;
    194 		        case 'e':		/* Exit standout mode on 'e' */
    195 			        standend();
    196 			        break;
    197 		        case 'r':		/* Force a refresh on 'r' */
    198 			        wrefresh(curscr);
    199 			        break;
    200 		        case 'w':		/* Turn on/off add_wch() tests */
    201                     wc_on = 1 - wc_on;
    202                     break;
    203                 case 'd':
    204                     add_wchstr(( const cchar_t *)&cstr );
    205                     refresh();
    206                     break;
    207                 case 'c':
    208                     addwstr(( const wchar_t *)&wstr );
    209                     refresh();
    210                     break;
    211                 case 'z':
    212                     move( 0, 1 );
    213                     if ( wc_on )
    214                         add_wch( &cc );
    215                     else
    216                         addch( c );
    217                     refresh();
    218                     break;
    219                 case 'x':
    220                     move( 0, 3 );
    221                     if ( wc_on )
    222                         add_wch( &cc );
    223                     else
    224                         addch( c );
    225                     refresh();
    226                     break;
    227                 case 'n':
    228                     add_wch( &ncc );
    229                     refresh();
    230                     break;
    231                 case 'm':
    232                     //border( 0, 0, 0, 0, 0, 0, 0, 0 );
    233                     border_set( NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
    234                     refresh();
    235                     break;
    236                 case 'j':
    237                     box_set( stdscr, &cc, &cc );
    238                     refresh();
    239                     break;
    240                 case 'k':
    241                     erase();
    242                     refresh();
    243                     break;
    244                 case '1':
    245                     move( 0, 0 );
    246                     clrtoeol();
    247                     refresh();
    248                     break;
    249                 case '2':
    250                     move( 0, 0 );
    251                     sprintf( nostr, "Orig:%x", al[ ac ]);
    252                     addstr( nostr );
    253                     ac = ( ac + 1 ) % 16;
    254                     bcc.attributes = al[ ac ];
    255                     bkgrnd( &bcc );
    256                     move( 1, 0 );
    257                     sprintf( nostr, "New:%x", al[ ac ]);
    258                     //addstr( nostr );
    259                     insstr( nostr );
    260                     refresh();
    261                     break;
    262                 case 'v':
    263                     if ( wc_on )
    264                         ins_wch( &cc );
    265                     else
    266                         insch( c );
    267                     refresh();
    268                     break;
    269                 case 'f':
    270                     ins_wstr(( const wchar_t *)&wstr );
    271                     refresh();
    272                     break;
    273                 case 't':
    274                     for ( i = 0; i < 4; i++ ) {
    275                         owstr[ i ] = wstr[ i + 5 ];
    276                         wstr[ i + 5 ] = i + 0xda05;
    277                     }
    278                     ins_wstr(( const wchar_t *)&wstr );
    279                     refresh();
    280                     for ( i = 0; i < 4; i++ )
    281                         wstr[ i + 5 ] = owstr[ i ];
    282                     break;
    283 		        default:		/* By default output the character */
    284                     if ( wc_on )
    285                         add_wch( &cc );
    286                     else {
    287                         if ( c < 0x7F )
    288                             addch( c );
    289                         else {
    290                             addstr( keyname( c ));
    291                         }
    292                     }
    293                     refresh();
    294 		    }
    295         } else {
    296             get_wch( &wc );
    297             switch ( wc ) {
    298                 case L'w':
    299                     wgc_on = 0;
    300                     noecho();
    301                     break;
    302                 case L'q':
    303                     quit( 0 );
    304                     break;
    305                 case L't':
    306                     notimeout( stdscr, TRUE );
    307                     break;
    308                 case L'T':
    309                     notimeout( stdscr, FALSE );
    310                     break;
    311                 case L'd':
    312                     nodelay( stdscr, TRUE );
    313                     break;
    314                 case L'D':
    315                     nodelay( stdscr, FALSE );
    316                     break;
    317                 case L'p':
    318                     keypad( stdscr, TRUE );
    319                     break;
    320                 case L'P':
    321                     keypad( stdscr, FALSE );
    322                     break;
    323                 default:
    324                     break;
    325             }
    326         }
    327 	}
    328 }
    329 
    330 void quit( int sig )
    331 {
    332 	erase();		/* Terminate by erasing the screen */
    333 	refresh();
    334 	endwin();		/* Always end with endwin() */
    335 	delwin(curscr);		/* Return storage */
    336 	delwin(stdscr);
    337 	putchar('\n');
    338 	exit( sig );
    339 }
    340 
    341