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
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: util_console.cpp
29 // author: Spencer Salazar (ssalazar@princeton.edu)
31 //-----------------------------------------------------------------------------
32 #include "util_console.h"
33 #include "chuck_errmsg.h"
35 #ifdef __USE_READLINE__
36 #include <readline/readline.h>
39 #define CONSOLE_INPUT_BUFFER_SIZE 255
42 char * io_readline( const char * prompt
)
44 #ifdef __USE_READLINE__
46 // call the real readline
47 return readline( prompt
);
52 char * buf
=(char *)malloc( CONSOLE_INPUT_BUFFER_SIZE
* sizeof(char) );
55 fputs( prompt
, stdout
);
57 result
= fgets( buf
, CONSOLE_INPUT_BUFFER_SIZE
, stdin
);
65 for( int i
=0; i
< CONSOLE_INPUT_BUFFER_SIZE
; i
++ )
77 void io_addhistory( const char * addme
)
79 #ifdef __USE_READLINE__
91 // code thanks to Luke Lin (wdlin@CCCA.NCTU.edu.tw)
93 #ifndef __PLATFORM_WIN32__
95 #ifdef __PLATFORM_MACOSX__
97 static struct termios g_save
;
100 static struct termio g_save
;
104 #include <sys/ioctl.h>
112 static t_CKBOOL g_init
;
116 t_CKBOOL
kb_initscr()
118 if( g_init
) return FALSE
;
120 #ifndef __PLATFORM_WIN32__
122 #ifdef __PLATFORM_MACOSX__
124 if( ioctl( 0, TIOCGETA
, &term
) == -1 )
127 if( ioctl( 0, TCGETA
, &term
) == -1 )
130 EM_log( CK_LOG_SEVERE
, "(kbhit disabled): standard input not a tty!");
135 EM_log( CK_LOG_INFO
, "starting kb hit immediate mode..." );
139 term
.c_lflag
&= ~ICANON
;
140 term
.c_lflag
&= ~ECHO
;
145 #ifdef __PLATFORM_MACOSX__
146 ioctl( 0, TIOCSETA
, &term
);
148 ioctl( 0, TCSETA
, &term
);
161 if( !g_init
) return;
163 #ifndef __PLATFORM_WIN32__
164 #ifdef __PLATFORM_MACOSX__
165 ioctl( 0, TIOCSETA
, &g_save
);
167 ioctl( 0, TCSETA
, &g_save
);
178 #ifndef __PLATFORM_WIN32__
181 ifkeyin
= read( 0, &c
, 1 );
185 EM_log( CK_LOG_FINE
, "kb hit! %i : %c", ifkeyin
, c
);
189 return (t_CKINT
)kbhit();
197 #ifndef __PLATFORM_WIN32__
200 return (t_CKINT
)::getch();