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