| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 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 dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: util_console.cpp |
| 28 | // desc: utility for console I/O |
| 29 | // |
| 30 | // author: Spencer Salazar ([email protected]) |
| 31 | // date: Autumn 2005 |
| 32 | //----------------------------------------------------------------------------- |
| 33 | #include "util_console.h" |
| 34 | #include "chuck_errmsg.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #ifdef __USE_READLINE__ |
| 38 | #include <readline/readline.h> |
| 39 | #else |
| 40 | #include <stdlib.h> |
| 41 | #define CONSOLE_INPUT_BUFFER_SIZE 255 |
| 42 | #endif |
| 43 | |
| 44 | char * io_readline( const char * prompt ) |
| 45 | { |
| 46 | #ifdef __USE_READLINE__ |
| 47 | |
| 48 | // call the real readline |
| 49 | return readline( prompt ); |
| 50 | |
| 51 | #else |
| 52 | |
| 53 | // insert our hack |
| 54 | char * buf=(char *)malloc( CONSOLE_INPUT_BUFFER_SIZE * sizeof(char) ); |
| 55 | char * result; |
| 56 | |
| 57 | fputs( prompt, stdout ); |
| 58 | |
| 59 | result = fgets( buf, CONSOLE_INPUT_BUFFER_SIZE, stdin ); |
| 60 | |
| 61 | if( result == NULL ) |
| 62 | { |
| 63 | free( buf ); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | for( int i=0; i < CONSOLE_INPUT_BUFFER_SIZE; i++ ) |
| 68 | if(buf[i] == '\n' ) |
| 69 | { |
| 70 | buf[i] = 0; |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | return buf; |
| 75 | |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | void io_addhistory( const char * addme ) |
| 80 | { |
| 81 | #ifdef __USE_READLINE__ |
| 82 | |
| 83 | add_history( addme ); |
| 84 | |
| 85 | #else |
| 86 | |
| 87 | //do nothing |
| 88 | |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // code thanks to Luke Lin ([email protected]) |
| 94 | // kb hit |
| 95 | #ifndef __PLATFORM_WINDOWS__ |
| 96 | #include <string.h> |
| 97 | #include <termios.h> |
| 98 | static struct termios g_save; |
| 99 | #include <unistd.h> |
| 100 | #include <sys/ioctl.h> |
| 101 | #else |
| 102 | #include <conio.h> |
| 103 | #endif |
| 104 | |
| 105 | |
| 106 | // global |
| 107 | static t_CKINT g_c; |
| 108 | static t_CKBOOL g_init; |
| 109 | |
| 110 | |
| 111 | // on entering mode |
| 112 | t_CKBOOL kb_initscr() |
| 113 | { |
| 114 | if( g_init ) return FALSE; |
| 115 | |
| 116 | #ifndef __PLATFORM_WINDOWS__ |
| 117 | struct termios term; |
| 118 | if( tcgetattr(0, &term) == -1 ) |
| 119 | { |
| 120 | EM_log( CK_LOG_HERALD, "(kbhit disabled): standard input not a tty!"); |
| 121 | return FALSE; |
| 122 | } |
| 123 | |
| 124 | // log |
| 125 | EM_log( CK_LOG_INFO, "starting kb hit immediate mode..." ); |
| 126 | |
| 127 | g_save = term; |
| 128 | |
| 129 | term.c_lflag &= ~ICANON; |
| 130 | term.c_lflag &= ~ECHO; |
| 131 | |
| 132 | term.c_cc[VMIN] = 0; |
| 133 | term.c_cc[VTIME]=0; |
| 134 | |
| 135 | tcsetattr( 0, TCSADRAIN, &term ); |
| 136 | #endif |
| 137 | |
| 138 | g_init = TRUE; |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | // on exit |
| 144 | void kb_endwin() |
| 145 | { |
| 146 | if( !g_init ) return; |
| 147 | |
| 148 | #ifndef __PLATFORM_WINDOWS__ |
| 149 | tcsetattr( 0, TCSADRAIN, &g_save ); |
| 150 | #endif |
| 151 | |
| 152 | g_init = FALSE; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | // hit |
| 157 | t_CKINT kb_hit() |
| 158 | { |
| 159 | #ifndef __PLATFORM_WINDOWS__ |
| 160 | long ifkeyin; |
| 161 | char c; |
| 162 | ifkeyin = read( 0, &c, 1 ); |
| 163 | g_c = (t_CKINT)c; |
| 164 | |
| 165 | // log |
| 166 | EM_log( CK_LOG_FINE, "kb hit! %i : %c", ifkeyin, c ); |
| 167 | |
| 168 | return (ifkeyin); |
| 169 | #else |
| 170 | return (t_CKINT)kbhit(); |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | |
| 175 | // get |
| 176 | t_CKINT kb_getch() |
| 177 | { |
| 178 | #ifndef __PLATFORM_WINDOWS__ |
| 179 | return g_c; |
| 180 | #else |
| 181 | return (t_CKINT)::getch(); |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // ready |
| 187 | t_CKBOOL kb_ready() |
| 188 | { |
| 189 | return g_init; |
| 190 | } |
| 191 |