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