Home | History | Annotate | Line # | Download | only in EXAMPLES
ex1.c revision 1.4.18.2
      1 /*	$NetBSD: ex1.c,v 1.4.18.2 2007/01/31 10:08:47 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 <stdlib.h>
     46 #include <string.h>
     47 #include <signal.h>
     48 #include <locale.h>
     49 #include <assert.h>
     50 
     51 #define YSIZE 4
     52 #define XSIZE 30
     53 
     54 void quit( int );
     55 
     56 main()
     57 {
     58 	int i, j, c, n = 0, ac = 0;
     59 	size_t len;
     60 	char id[100];
     61 	FILE *fp;
     62 	char *s;
     63     cchar_t cc, ncc, cstr[ 128 ], icc, icstr[ 128 ], bcc;
     64     int wc_on = 0, wgc_on = 0;
     65     char mbs[] = "";
     66     char mbstr[] = " (Liji)";
     67     wchar_t wstr[ 128 ], owstr[ 4 ], gwstr[ 128 ], iwstr[ 128 ];
     68     int wslen = 0;
     69     wint_t wc;
     70     char nostr[ 128 ];
     71     attr_t al[ 15 ] = { WA_BLINK,
     72                         WA_BOLD,
     73                         WA_DIM,
     74                         WA_LOW,
     75                         WA_TOP,
     76                         WA_INVIS,
     77                         WA_HORIZONTAL,
     78                         WA_VERTICAL,
     79                         WA_LEFT,
     80                         WA_RIGHT,
     81                         WA_PROTECT,
     82                         WA_REVERSE,
     83                         WA_STANDOUT,
     84                         WA_UNDERLINE };
     85 
     86     fprintf( stderr, "Current locale: %s\n", setlocale(LC_ALL, ""));
     87     if (( wslen =  mbstowcs( &cc.vals[ 0 ], mbs, strlen( mbs ))) < 0 ) {
     88         fprintf( stderr, "mbstowcs() failed\n" );
     89         return -1;
     90     }
     91     fprintf( stderr, "WC string length: %d\n", wslen );
     92     fprintf( stderr, "WC width: %d\n", wcwidth( cc.vals[ 0 ]));
     93     cc.elements = ncc.elements = 8;
     94     cc.attributes = ncc.attributes = 0;
     95     ncc.vals[ 0 ] = 0xda00;
     96     cc.vals[ 1 ] = ncc.vals[ 1 ] = 0xda01;
     97     cc.vals[ 2 ] = ncc.vals[ 2 ] = 0xda02;
     98     cc.vals[ 3 ] = ncc.vals[ 3 ] = 0xda03;
     99     cc.vals[ 4 ] = ncc.vals[ 4 ] = 0xda04;
    100     cc.vals[ 5 ] = ncc.vals[ 5 ] = 0xda05;
    101     cc.vals[ 6 ] = ncc.vals[ 6 ] = 0xda06;
    102     cc.vals[ 7 ] = ncc.vals[ 7 ] = 0xda07;
    103 
    104     if (( wslen =  mbstowcs( wstr, mbstr, strlen( mbstr ))) < 0 ) {
    105         fprintf( stderr, "mbstowcs() failed\n" );
    106         return -1;
    107     }
    108 
    109     for ( i = 0; i < wslen; i++ ) {
    110         cstr[ i ].vals[ 0 ] = wstr[ i ];
    111     }
    112     cstr[ wslen ].vals[ 0 ] = 0;
    113 
    114     bcc.elements = 8;
    115     bcc.attributes = 0;
    116     bcc.vals[ 0 ] = L'_';
    117     bcc.vals[ 1 ] = 0xda01;
    118     bcc.vals[ 2 ] = 0xda02;
    119     bcc.vals[ 3 ] = 0xda03;
    120     bcc.vals[ 4 ] = 0xda04;
    121     bcc.vals[ 5 ] = 0xda05;
    122     bcc.vals[ 6 ] = 0xda06;
    123     bcc.vals[ 7 ] = 0xda07;
    124 
    125 	initscr();			/* Always call initscr() first */
    126 	signal(SIGINT, quit);		/* Make sure wou have a 'cleanup' fn */
    127 	crmode();			/* We want cbreak mode */
    128 	noecho();			/* We want to have control of chars */
    129 	delwin(stdscr);			/* Create our own stdscr */
    130 	stdscr = newwin(YSIZE, XSIZE, 1, 1);
    131 	flushok(stdscr, TRUE);		/* Enable flushing of stdout */
    132 	scrollok(stdscr, TRUE);		/* Enable scrolling */
    133 	erase();			/* Initially, clear the screen */
    134 
    135 	standout();
    136 	move(0,0);
    137 	while (1) {
    138         if ( !wgc_on ) {
    139 		    c = getchar();
    140 		    switch(c) {
    141 		        case 'q':		/* Quit on 'q' */
    142 			        quit( 0 );
    143 			        break;
    144                 case 'p':
    145                     keypad( stdscr, TRUE );
    146                     break;
    147                 case 'P':
    148                     keypad( stdscr, FALSE );
    149                     break;
    150                 case 'g':
    151                     wgc_on = 1;
    152                     echo();
    153                     break;
    154                 case 'b':
    155                     get_wstr( gwstr );
    156                     move( 1, 0 );
    157                     addstr( "Input:" );
    158                     addwstr( gwstr );
    159                     refresh();
    160                     break;
    161                 case 'h':
    162                     move( 0, 0 );
    163                     in_wch( &icc );
    164                     move( 1, 0 );
    165                     add_wch( &icc );
    166                     refresh();
    167                     break;
    168                 case 'y':
    169                     move( 0, 0 );
    170                     in_wchstr( icstr );
    171                     move( 1, 0 );
    172                     add_wchstr( icstr );
    173                     refresh();
    174                     break;
    175                 case 'u':
    176                     move( 0, 0 );
    177                     inwstr( iwstr );
    178                     move( 1, 0 );
    179                     addwstr( iwstr );
    180                     refresh();
    181                     break;
    182                 case 'i':
    183                     move( 0, 0 );
    184                     hline_set( &cc, 20 );
    185                     move( 0, 0 );
    186                     vline_set( &cc, 20 );
    187                     refresh();
    188                     break;
    189                 case 'o':
    190                     clrtobot();
    191                     refresh();
    192                     break;
    193 		        case 's':		/* Go into standout mode on 's' */
    194 			        standout();
    195 			        break;
    196 		        case 'e':		/* Exit standout mode on 'e' */
    197 			        standend();
    198 			        break;
    199 		        case 'r':		/* Force a refresh on 'r' */
    200 			        wrefresh(curscr);
    201 			        break;
    202 		        case 'w':		/* Turn on/off add_wch() tests */
    203                     wc_on = 1 - wc_on;
    204                     break;
    205                 case 'd':
    206                     add_wchstr(( const cchar_t *)&cstr );
    207                     refresh();
    208                     break;
    209                 case 'c':
    210                     addwstr(( const wchar_t *)&wstr );
    211                     refresh();
    212                     break;
    213                 case 'z':
    214                     move( 0, 1 );
    215                     if ( wc_on )
    216                         add_wch( &cc );
    217                     else
    218                         addch( c );
    219                     refresh();
    220                     break;
    221                 case 'x':
    222                     move( 0, 3 );
    223                     if ( wc_on )
    224                         add_wch( &cc );
    225                     else
    226                         addch( c );
    227                     refresh();
    228                     break;
    229                 case 'n':
    230                     add_wch( &ncc );
    231                     refresh();
    232                     break;
    233                 case 'm':
    234                     //border( 0, 0, 0, 0, 0, 0, 0, 0 );
    235                     border_set( NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
    236                     refresh();
    237                     break;
    238                 case 'j':
    239                     box_set( stdscr, &cc, &cc );
    240                     refresh();
    241                     break;
    242                 case 'k':
    243                     erase();
    244                     refresh();
    245                     break;
    246                 case '1':
    247                     move( 0, 0 );
    248                     clrtoeol();
    249                     refresh();
    250                     break;
    251                 case '2':
    252                     move( 0, 0 );
    253                     sprintf( nostr, "Orig:%x", al[ ac ]);
    254                     addstr( nostr );
    255                     ac = ( ac + 1 ) % 16;
    256                     bcc.attributes = al[ ac ];
    257                     bkgrnd( &bcc );
    258                     move( 1, 0 );
    259                     sprintf( nostr, "New:%x", al[ ac ]);
    260                     //addstr( nostr );
    261                     insstr( nostr );
    262                     refresh();
    263                     break;
    264                 case 'v':
    265                     if ( wc_on )
    266                         ins_wch( &cc );
    267                     else
    268                         insch( c );
    269                     refresh();
    270                     break;
    271                 case 'f':
    272                     ins_wstr(( const wchar_t *)&wstr );
    273                     refresh();
    274                     break;
    275                 case 't':
    276                     for ( i = 0; i < 4; i++ ) {
    277                         owstr[ i ] = wstr[ i + 5 ];
    278                         wstr[ i + 5 ] = i + 0xda05;
    279                     }
    280                     ins_wstr(( const wchar_t *)&wstr );
    281                     refresh();
    282                     for ( i = 0; i < 4; i++ )
    283                         wstr[ i + 5 ] = owstr[ i ];
    284                     break;
    285 		        default:		/* By default output the character */
    286                     if ( wc_on )
    287                         add_wch( &cc );
    288                     else {
    289                         if ( c < 0x7F )
    290                             addch( c );
    291                         else {
    292                             addstr( keyname( c ));
    293                         }
    294                     }
    295                     refresh();
    296 		    }
    297         } else {
    298             get_wch( &wc );
    299             switch ( wc ) {
    300                 case L'w':
    301                     wgc_on = 0;
    302                     noecho();
    303                     break;
    304                 case L'q':
    305                     quit( 0 );
    306                     break;
    307                 case L't':
    308                     notimeout( stdscr, TRUE );
    309                     break;
    310                 case L'T':
    311                     notimeout( stdscr, FALSE );
    312                     break;
    313                 case L'd':
    314                     nodelay( stdscr, TRUE );
    315                     break;
    316                 case L'D':
    317                     nodelay( stdscr, FALSE );
    318                     break;
    319                 case L'p':
    320                     keypad( stdscr, TRUE );
    321                     break;
    322                 case L'P':
    323                     keypad( stdscr, FALSE );
    324                     break;
    325                 default:
    326                     break;
    327             }
    328         }
    329 	}
    330 }
    331 
    332 void quit( int sig )
    333 {
    334 	erase();		/* Terminate by erasing the screen */
    335 	refresh();
    336 	endwin();		/* Always end with endwin() */
    337 	delwin(curscr);		/* Return storage */
    338 	delwin(stdscr);
    339 	putchar('\n');
    340 	exit( sig );
    341 }
    342 
    343