Showing the changes to src/core/util_console.cpp (1 of 181 files). Show all
Unified · Split
| @@ -0,0 +1,210 @@ | |||
| 1 | /*---------------------------------------------------------------------------- | ||
| 2 | ChucK Concurrent, On-the-fly Audio Programming Language | ||
| 3 | Compiler and Virtual Machine | ||
| 4 | |||
| 5 | Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved. | ||
| 6 | http://chuck.stanford.edu/ | ||
| 7 | http://chuck.cs.princeton.edu/ | ||
| 8 | |||
| 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. | ||
| 13 | |||
| 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. | ||
| 18 | |||
| 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 | -----------------------------------------------------------------------------*/ | ||
| 24 | |||
| 25 | //----------------------------------------------------------------------------- | ||
| 26 | // file: util_console.cpp | ||
| 27 | // desc: utility for console I/O | ||
| 28 | // | ||
| 29 | // author: Spencer Salazar (spencer@ccrma.stanford.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 | ||
| 41 | |||
| 42 | char * io_readline( const char * prompt ) | ||
| 43 | { | ||
| 44 | #ifdef __USE_READLINE__ | ||
| 45 | |||
| 46 | // call the real readline | ||
| 47 | return readline( prompt ); | ||
| 48 | |||
| 49 | #else | ||
| 50 | |||
| 51 | // insert our hack | ||
| 52 | char * buf=(char *)malloc( CONSOLE_INPUT_BUFFER_SIZE * sizeof(char) ); | ||
| 53 | char * result; | ||
| 54 | |||
| 55 | fputs( prompt, stdout ); | ||
| 56 | |||
| 57 | result = fgets( buf, CONSOLE_INPUT_BUFFER_SIZE, stdin ); | ||
| 58 | |||
| 59 | if( result == NULL ) | ||
| 60 | { | ||
| 61 | free( buf ); | ||
| 62 | return NULL; | ||
| 63 | } | ||
| 64 | |||
| 65 | for( int i=0; i < CONSOLE_INPUT_BUFFER_SIZE; i++ ) | ||
| 66 | if(buf[i] == '\n' ) | ||
| 67 | { | ||
| 68 | buf[i] = 0; | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | |||
| 72 | return buf; | ||
| 73 | |||
| 74 | #endif | ||
| 75 | } | ||
| 76 | |||
| 77 | void io_addhistory( const char * addme ) | ||
| 78 | { | ||
| 79 | #ifdef __USE_READLINE__ | ||
| 80 | |||
| 81 | add_history( addme ); | ||
| 82 | |||
| 83 | #else | ||
| 84 | |||
| 85 | //do nothing | ||
| 86 | |||
| 87 | #endif | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 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 | ||
| 102 | |||
| 103 | #include <unistd.h> | ||
| 104 | #include <sys/ioctl.h> | ||
| 105 | #else | ||
| 106 | #include <conio.h> | ||
| 107 | #endif | ||
| 108 | |||
| 109 | |||
| 110 | // global | ||
| 111 | static t_CKINT g_c; | ||
| 112 | static t_CKBOOL g_init; | ||
| 113 | |||
| 114 | |||
| 115 | // on entering mode | ||
| 116 | t_CKBOOL kb_initscr() | ||
| 117 | { | ||
| 118 | if( g_init ) return FALSE; | ||
| 119 | |||
| 120 | #ifndef __PLATFORM_WIN32__ | ||
| 121 | |||
| 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 | ||
| 129 | { | ||
| 130 | EM_log( CK_LOG_SEVERE, "(kbhit disabled): standard input not a tty!"); | ||
| 131 | return FALSE; | ||
| 132 | } | ||
| 133 | |||
| 134 | // log | ||
| 135 | EM_log( CK_LOG_INFO, "starting kb hit immediate mode..." ); | ||
| 136 | |||
| 137 | g_save = term; | ||
| 138 | |||
| 139 | term.c_lflag &= ~ICANON; | ||
| 140 | term.c_lflag &= ~ECHO; | ||
| 141 | |||
| 142 | term.c_cc[VMIN] = 0; | ||
| 143 | term.c_cc[VTIME]=0; | ||
| 144 | |||
| 145 | #ifdef __PLATFORM_MACOSX__ | ||
| 146 | ioctl( 0, TIOCSETA, &term ); | ||
| 147 | #else | ||
| 148 | ioctl( 0, TCSETA, &term ); | ||
| 149 | #endif | ||
| 150 | |||
| 151 | #endif | ||
| 152 | |||
| 153 | g_init = TRUE; | ||
| 154 | return TRUE; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | // on exit | ||
| 159 | void kb_endwin() | ||
| 160 | { | ||
| 161 | if( !g_init ) return; | ||
| 162 | |||
| 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 | ||
| 170 | |||
| 171 | g_init = FALSE; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | // hit | ||
| 176 | t_CKINT kb_hit() | ||
| 177 | { | ||
| 178 | #ifndef __PLATFORM_WIN32__ | ||
| 179 | int ifkeyin; | ||
| 180 | char c; | ||
| 181 | ifkeyin = read( 0, &c, 1 ); | ||
| 182 | g_c = (t_CKINT)c; | ||
| 183 | |||
| 184 | // log | ||
| 185 | EM_log( CK_LOG_FINE, "kb hit! %i : %c", ifkeyin, c ); | ||
| 186 | |||
| 187 | return (ifkeyin); | ||
| 188 | #else | ||
| 189 | return (t_CKINT)kbhit(); | ||
| 190 | #endif | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | // get | ||
| 195 | t_CKINT kb_getch() | ||
| 196 | { | ||
| 197 | #ifndef __PLATFORM_WIN32__ | ||
| 198 | return g_c; | ||
| 199 | #else | ||
| 200 | return (t_CKINT)::getch(); | ||
| 201 | #endif | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | // ready | ||
| 206 | t_CKBOOL kb_ready() | ||
| 207 | { | ||
| 208 | return g_init; | ||
| 209 | } | ||
| 210 | |||