]> git.sesse.net Git - mlt/blob - src/melt/io.c
Let 'Q' stop melt as well.
[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 #else
33 // MinGW defines struct timespec in pthread.h
34 #include <pthread.h>
35 #endif
36 #include <unistd.h>
37 #include <sys/time.h>
38
39 /* Application header files */
40 #include "io.h"
41
42 char *chomp( char *input )
43 {
44         if ( input != NULL )
45         {
46                 int length = strlen( input );
47                 if ( length && input[ length - 1 ] == '\n' )
48                         input[ length - 1 ] = '\0';
49                 if ( length > 1 && input[ length - 2 ] == '\r' )
50                         input[ length - 2 ] = '\0';
51         }
52         return input;
53 }
54
55 char *trim( char *input )
56 {
57         if ( input != NULL )
58         {
59                 int length = strlen( input );
60                 int first = 0;
61                 while( first < length && isspace( input[ first ] ) )
62                         first ++;
63                 memmove( input, input + first, length - first + 1 );
64                 length = length - first;
65                 while ( length > 0 && isspace( input[ length - 1 ] ) )
66                         input[ -- length ] = '\0';
67         }
68         return input;
69 }
70
71 char *strip_quotes( char *input )
72 {
73         if ( input != NULL )
74         {
75                 char *ptr = strrchr( input, '\"' );
76                 if ( ptr != NULL )
77                         *ptr = '\0';
78                 if ( input[ 0 ] == '\"' )
79                         strcpy( input, input + 1 );
80         }
81         return input;
82 }
83
84 int *get_int( int *output, int use )
85 {
86         int *value = NULL;
87         char temp[ 132 ];
88         *output = use;
89         if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
90         {
91                 if ( strcmp( temp, "" ) )
92                         *output = atoi( temp );
93                 value = output;
94         }
95         return value;
96 }
97
98 /** This stores the previous settings
99 */
100
101 static struct termios oldtty;
102 static int mode = 0;
103
104 /** This is called automatically on application exit to restore the 
105         previous tty settings.
106 */
107
108 void term_exit(void)
109 {
110 #ifndef WIN32
111         if ( mode == 1 )
112         {
113                 tcsetattr( 0, TCSANOW, &oldtty );
114                 mode = 0;
115         }
116 #endif
117 }
118
119 /** Init terminal so that we can grab keys without blocking.
120 */
121
122 void term_init( )
123 {
124 #ifndef WIN32
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 #endif
144 }
145
146 /** Check for a keypress without blocking infinitely.
147         Returns: ASCII value of keypress or -1 if no keypress detected.
148 */
149
150 int term_read( )
151 {
152 #ifndef WIN32
153     int n = 1;
154     unsigned char ch;
155     struct timeval tv;
156     fd_set rfds;
157
158     FD_ZERO( &rfds );
159     FD_SET( 0, &rfds );
160     tv.tv_sec = 0;
161     tv.tv_usec = 40000;
162     n = select( 1, &rfds, NULL, NULL, &tv );
163     if (n > 0) 
164         {
165         n = read( 0, &ch, 1 );
166                 tcflush( 0, TCIFLUSH );
167         if (n == 1)
168             return ch;
169         return n;
170     }
171 #else
172         struct timespec tm = { 0, 40000 };
173         nanosleep( &tm, NULL );
174 #endif
175     return -1;
176 }
177
178 char get_keypress( )
179 {
180         char value = '\0';
181         int pressed = 0;
182
183         fflush( stdout );
184
185         term_init( );
186         while ( ( pressed = term_read( ) ) == -1 ) ;
187         term_exit( );
188
189         value = (char)pressed;
190
191         return value;
192 }
193
194 void wait_for_any_key( char *message )
195 {
196         if ( message == NULL )
197                 printf( "Press any key to continue: " );
198         else
199                 printf( "%s", message );
200
201         get_keypress( );
202
203         printf( "\n\n" );
204 }
205
206 void beep( )
207 {
208         printf( "%c", 7 );
209         fflush( stdout );
210 }