*** empty log message ***
[chuck-blob.git] / v2 / util_console.cpp
blob4fdb9196abce3cd73fc6e5a25cb1d69a5c95ef62
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: util_console.cpp
27 // desc: ...
29 // author: Spencer Salazar (ssalazar@princeton.edu)
30 // date: Autumn 2005
31 //-----------------------------------------------------------------------------
32 #include "util_console.h"
33 #include "chuck_errmsg.h"
34 #include <stdio.h>
35 #ifdef __USE_READLINE__
36 #include <readline/readline.h>
37 #else
38 #include <stdlib.h>
39 #define CONSOLE_INPUT_BUFFER_SIZE 255
40 #endif
42 char * io_readline( const char * prompt )
44 #ifdef __USE_READLINE__
46 // call the real readline
47 return readline( prompt );
49 #else
51 // insert our hack
52 char * buf=(char *)malloc( CONSOLE_INPUT_BUFFER_SIZE * sizeof(char) );
53 char * result;
55 fputs( prompt, stdout );
57 result = fgets( buf, CONSOLE_INPUT_BUFFER_SIZE, stdin );
59 if( result == NULL )
61 free( buf );
62 return NULL;
65 for( int i=0; i < CONSOLE_INPUT_BUFFER_SIZE; i++ )
66 if(buf[i] == '\n' )
68 buf[i] = 0;
69 break;
72 return buf;
74 #endif
77 void io_addhistory( const char * addme )
79 #ifdef __USE_READLINE__
81 add_history( addme );
83 #else
85 //do nothing
87 #endif
91 // code thanks to Luke Lin (wdlin@CCCA.NCTU.edu.tw)
92 // kb hit
93 #ifndef __PLATFORM_WIN32__
94 #include <string.h>
95 #ifdef __PLATFORM_MACOSX__
96 #include <termios.h>
97 static struct termios g_save;
98 #else
99 #include <termio.h>
100 static struct termio g_save;
101 #endif
103 #include <unistd.h>
104 #include <sys/ioctl.h>
105 #else
106 #include <conio.h>
107 #endif
110 // global
111 static t_CKINT g_c;
112 static t_CKBOOL g_init;
115 // on entering mode
116 t_CKBOOL kb_initscr()
118 if( g_init ) return FALSE;
120 #ifndef __PLATFORM_WIN32__
122 #ifdef __PLATFORM_MACOSX__
123 struct termios term;
124 if( ioctl( 0, TIOCGETA, &term ) == -1 )
125 #else
126 struct termio term;
127 if( ioctl( 0, TCGETA, &term ) == -1 )
128 #endif
130 EM_log( CK_LOG_SEVERE, "(kbhit disabled): standard input not a tty!");
131 return FALSE;
134 // log
135 EM_log( CK_LOG_INFO, "starting kb hit immediate mode..." );
137 g_save = term;
139 term.c_lflag &= ~ICANON;
140 term.c_lflag &= ~ECHO;
142 term.c_cc[VMIN] = 0;
143 term.c_cc[VTIME]=0;
145 #ifdef __PLATFORM_MACOSX__
146 ioctl( 0, TIOCSETA, &term );
147 #else
148 ioctl( 0, TCSETA, &term );
149 #endif
151 #endif
153 g_init = TRUE;
154 return TRUE;
158 // on exit
159 void kb_endwin()
161 if( !g_init ) return;
163 #ifndef __PLATFORM_WIN32__
164 #ifdef __PLATFORM_MACOSX__
165 ioctl( 0, TIOCSETA, &g_save );
166 #else
167 ioctl( 0, TCSETA, &g_save );
168 #endif
169 #endif
171 g_init = FALSE;
175 // hit
176 t_CKINT kb_hit()
178 #ifndef __PLATFORM_WIN32__
179 int ifkeyin;
180 char c;
181 ifkeyin = read( 0, &c, 1 );
182 g_c = (t_CKINT)c;
184 // log
185 EM_log( CK_LOG_FINE, "kb hit! %i : %c", ifkeyin, c );
187 return (ifkeyin);
188 #else
189 return (t_CKINT)kbhit();
190 #endif
194 // get
195 t_CKINT kb_getch()
197 #ifndef __PLATFORM_WIN32__
198 return g_c;
199 #else
200 return (t_CKINT)::getch();
201 #endif
205 // ready
206 t_CKBOOL kb_ready()
208 return g_init;