1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
* io.c -- dv1394d client demo input/output
* Copyright (C) 2002-2003 Ushodaya Enterprises Limited
* Author: Charles Yates <charles.yates@pandora.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* System header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <unistd.h>
/* Application header files */
#include "io.h"
char *chomp( char *input )
{
if ( input != NULL )
{
int length = strlen( input );
if ( length && input[ length - 1 ] == '\n' )
input[ length - 1 ] = '\0';
if ( length > 1 && input[ length - 2 ] == '\r' )
input[ length - 2 ] = '\0';
}
return input;
}
char *trim( char *input )
{
if ( input != NULL )
{
int length = strlen( input );
int first = 0;
while ( first < length && isspace( input[ first ] ) )
first ++;
memmove( input, input + first, length - first + 1 );
length = length - first;
while ( length > 0 && isspace( input[ length - 1 ] ) )
input[ -- length ] = '\0';
}
return input;
}
char *strip_quotes( char *input )
{
if ( input != NULL )
{
char * ptr = strrchr( input, '\"' );
if ( ptr != NULL )
* ptr = '\0';
if ( input[ 0 ] == '\"' )
strcpy( input, input + 1 );
}
return input;
}
char *get_string( char *output, int maxlength, char *use )
{
char * value = NULL;
strcpy( output, use );
if ( trim( chomp( fgets( output, maxlength, stdin ) ) ) != NULL )
{
if ( !strcmp( output, "" ) )
strcpy( output, use );
value = output;
}
return value;
}
int *get_int( int *output, int use )
{
int * value = NULL;
char temp[ 132 ];
*output = use;
if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
{
if ( strcmp( temp, "" ) )
* output = atoi( temp );
value = output;
}
return value;
}
/** This stores the previous settings
*/
static struct termios oldtty;
static int mode = 0;
/** This is called automatically on application exit to restore the
previous tty settings.
*/
void term_exit( void )
{
if ( mode == 1 )
{
tcsetattr( 0, TCSANOW, &oldtty );
mode = 0;
}
}
/** Init terminal so that we can grab keys without blocking.
*/
void term_init( )
{
struct termios tty;
if ( isatty( fileno( stdin ) ) )
{
tcgetattr( 0, &tty );
oldtty = tty;
tty.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );
tty.c_oflag |= OPOST;
tty.c_lflag &= ~( ECHO | ECHONL | ICANON | IEXTEN );
tty.c_cflag &= ~( CSIZE | PARENB );
tty.c_cflag |= CS8;
tty.c_cc[ VMIN ] = 1;
tty.c_cc[ VTIME ] = 0;
tcsetattr( 0, TCSANOW, &tty );
mode = 1;
atexit( term_exit );
}
else
{
fcntl( fileno( stdin ), F_SETFL, O_NONBLOCK );
}
}
/** Check for a keypress without blocking infinitely.
Returns: ASCII value of keypress or -1 if no keypress detected.
*/
int term_read( )
{
int n = 1;
unsigned char ch;
struct timeval tv;
fd_set rfds;
FD_ZERO( &rfds );
FD_SET( 0, &rfds );
tv.tv_sec = 0;
tv.tv_usec = 125000;
n = select( 1, &rfds, NULL, NULL, &tv );
if ( n > 0 )
{
n = read( 0, &ch, 1 );
if ( isatty( fileno( stdin ) ) )
tcflush( 0, TCIFLUSH );
if ( n == 1 )
return ch;
return n;
}
return -1;
}
char get_keypress( )
{
char value = '\0';
int pressed = 0;
fflush( stderr );
term_init( );
while ( ( pressed = term_read( ) ) == -1 )
;
term_exit( );
value = ( char ) pressed;
return value;
}
void wait_for_any_key( char *message )
{
if ( message == NULL )
printf( "Press any key to continue: " );
else
printf( "%s", message );
get_keypress( );
printf( "\n\n" );
}
void beep( )
{
printf( "%c", 7 );
fflush( stderr );
}
|