]> git.sesse.net Git - mlt/blob - src/inigo/io.c
06acc3780b59744aa0e141a20defe5f48485f448
[mlt] / src / inigo / 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 #include <sys/time.h>
33
34 /* Application header files */
35 #include "io.h"
36
37 char *chomp( char *input )
38 {
39         if ( input != NULL )
40         {
41                 int length = strlen( input );
42                 if ( length && input[ length - 1 ] == '\n' )
43                         input[ length - 1 ] = '\0';
44                 if ( length > 1 && input[ length - 2 ] == '\r' )
45                         input[ length - 2 ] = '\0';
46         }
47         return input;
48 }
49
50 char *trim( char *input )
51 {
52         if ( input != NULL )
53         {
54                 int length = strlen( input );
55                 int first = 0;
56                 while( first < length && isspace( input[ first ] ) )
57                         first ++;
58                 memmove( input, input + first, length - first + 1 );
59                 length = length - first;
60                 while ( length > 0 && isspace( input[ length - 1 ] ) )
61                         input[ -- length ] = '\0';
62         }
63         return input;
64 }
65
66 char *strip_quotes( char *input )
67 {
68         if ( input != NULL )
69         {
70                 char *ptr = strrchr( input, '\"' );
71                 if ( ptr != NULL )
72                         *ptr = '\0';
73                 if ( input[ 0 ] == '\"' )
74                         strcpy( input, input + 1 );
75         }
76         return input;
77 }
78
79 int *get_int( int *output, int use )
80 {
81         int *value = NULL;
82         char temp[ 132 ];
83         *output = use;
84         if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
85         {
86                 if ( strcmp( temp, "" ) )
87                         *output = atoi( temp );
88                 value = output;
89         }
90         return value;
91 }
92
93 /** This stores the previous settings
94 */
95
96 static struct termios oldtty;
97 static int mode = 0;
98
99 /** This is called automatically on application exit to restore the 
100         previous tty settings.
101 */
102
103 void term_exit(void)
104 {
105         if ( mode == 1 )
106         {
107                 tcsetattr( 0, TCSANOW, &oldtty );
108                 mode = 0;
109         }
110 }
111
112 /** Init terminal so that we can grab keys without blocking.
113 */
114
115 void term_init( )
116 {
117         struct termios tty;
118
119         tcgetattr( 0, &tty );
120         oldtty = tty;
121
122         tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
123         tty.c_oflag |= OPOST;
124         tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
125         tty.c_cflag &= ~(CSIZE|PARENB);
126         tty.c_cflag |= CS8;
127         tty.c_cc[ VMIN ] = 1;
128         tty.c_cc[ VTIME ] = 0;
129     
130         tcsetattr( 0, TCSANOW, &tty );
131
132         mode = 1;
133
134         atexit( term_exit );
135 }
136
137 /** Check for a keypress without blocking infinitely.
138         Returns: ASCII value of keypress or -1 if no keypress detected.
139 */
140
141 int term_read( )
142 {
143     int n = 1;
144     unsigned char ch;
145     struct timeval tv;
146     fd_set rfds;
147
148     FD_ZERO( &rfds );
149     FD_SET( 0, &rfds );
150     tv.tv_sec = 0;
151     tv.tv_usec = 40000;
152     n = select( 1, &rfds, NULL, NULL, &tv );
153     if (n > 0) 
154         {
155         n = read( 0, &ch, 1 );
156                 tcflush( 0, TCIFLUSH );
157         if (n == 1)
158             return ch;
159         return n;
160     }
161     return -1;
162 }
163
164 char get_keypress( )
165 {
166         char value = '\0';
167         int pressed = 0;
168
169         fflush( stdout );
170
171         term_init( );
172         while ( ( pressed = term_read( ) ) == -1 ) ;
173         term_exit( );
174
175         value = (char)pressed;
176
177         return value;
178 }
179
180 void wait_for_any_key( char *message )
181 {
182         if ( message == NULL )
183                 printf( "Press any key to continue: " );
184         else
185                 printf( "%s", message );
186
187         get_keypress( );
188
189         printf( "\n\n" );
190 }
191
192 void beep( )
193 {
194         printf( "%c", 7 );
195         fflush( stdout );
196 }