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