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