1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "java_io_Console.h"
  30 
  31 #include <stdlib.h>
  32 #include <Wincon.h>
  33 
  34 static HANDLE hStdOut = INVALID_HANDLE_VALUE;
  35 static HANDLE hStdIn = INVALID_HANDLE_VALUE;
  36 JNIEXPORT jboolean JNICALL
  37 Java_java_io_Console_istty(JNIEnv *env, jclass cls)
  38 {
  39     if (hStdIn == INVALID_HANDLE_VALUE &&
  40         (hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  41         return JNI_FALSE;
  42     }
  43     if (hStdOut == INVALID_HANDLE_VALUE &&
  44         (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  45         return JNI_FALSE;
  46     }
  47     if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
  48         GetFileType(hStdOut) != FILE_TYPE_CHAR)
  49         return JNI_FALSE;
  50     return JNI_TRUE;
  51 }
  52 
  53 JNIEXPORT jstring JNICALL
  54 Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
  55 {
  56     char buf[64];
  57     int cp = GetConsoleCP();
  58     if (cp >= 874 && cp <= 950)
  59         sprintf(buf, "ms%d", cp);
  60     else
  61         sprintf(buf, "cp%d", cp);
  62     return JNU_NewStringPlatform(env, buf);
  63 }
  64 
  65 JNIEXPORT jboolean JNICALL
  66 Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)
  67 {
  68     DWORD fdwMode;
  69     jboolean old;
  70     if (! GetConsoleMode(hStdIn, &fdwMode)) {
  71         JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
  72         return !on;
  73     }
  74     old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
  75     if (on) {
  76         fdwMode |= ENABLE_ECHO_INPUT;
  77     } else {
  78         fdwMode &= ~ENABLE_ECHO_INPUT;
  79     }
  80     if (! SetConsoleMode(hStdIn, fdwMode)) {
  81         JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");
  82     }
  83     return old;
  84 }
  85 
  86 
  87 JNIEXPORT void JNICALL
  88 Java_java_io_Console_initIDs(JNIEnv *env, jclass cls) 
  89 {
  90 }
  91 
  92 
  93 JNIEXPORT jboolean JNICALL
  94 Java_java_io_Console_open(JNIEnv *env, jobject this)
  95 {
  96     if (INVALID_HANDLE_VALUE == hStdIn &&
  97         INVALID_HANDLE_VALUE == (hStdIn = GetStdHandle(STD_INPUT_HANDLE))) {
  98         JNU_ThrowIOExceptionWithLastError(env, "Open Console input failed");
  99     } 
 100     if (INVALID_HANDLE_VALUE == hStdOut &&
 101         INVALID_HANDLE_VALUE == (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE))) {
 102         JNU_ThrowIOExceptionWithLastError(env, "Open Console output failed");
 103     } 
 104     return TRUE;
 105 }
 106 
 107 JNIEXPORT void JNICALL
 108 Java_java_io_Console_close(JNIEnv *env, jobject this)
 109 { 
 110     //TBD, should we close these handles?
 111 }
 112 
 113 JNIEXPORT void JNICALL
 114 Java_java_io_Console_rawOnEchoOff(JNIEnv *env, 
 115                                       jobject this, 
 116                                       jboolean onoff)
 117 {
 118     DWORD fdwMode;
 119     if (! GetConsoleMode(hStdIn, &fdwMode)) {
 120         JNU_ThrowIOExceptionWithLastError(env, "Get Console mode failed");
 121     }
 122     if (onoff) {
 123         fdwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
 124     } else {
 125         fdwMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT; 
 126     }
 127     if (! SetConsoleMode(hStdIn, fdwMode)) {
 128         JNU_ThrowIOExceptionWithLastError(env, "Set Console mode failed");
 129     }
 130 }
 131 
 132 JNIEXPORT jint JNICALL
 133 Java_java_io_Console_killToEOS(JNIEnv *env, jobject this) {
 134     CONSOLE_SCREEN_BUFFER_INFO csbi;
 135     DWORD dwCharsSize;
 136     DWORD dwCharsWritten;
 137     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 138         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 139     }
 140     dwCharsSize = csbi.dwSize.X * (csbi.dwSize.Y - csbi.dwCursorPosition.Y)
 141         + csbi.dwCursorPosition.X;
 142     if (FillConsoleOutputCharacterA(hStdOut, ' ', 
 143                                     dwCharsSize,
 144                                     csbi.dwCursorPosition,
 145                                     &dwCharsWritten) == 0 ||
 146         FillConsoleOutputAttribute(hStdOut, csbi.wAttributes,
 147                                    dwCharsSize,
 148                                    csbi.dwCursorPosition,
 149                                    &dwCharsWritten) == 0) {
 150         JNU_ThrowIOExceptionWithLastError(env, "Console killToEOS, failed");
 151     }
 152     return 0;
 153 }
 154 
 155 static void moveXY(JNIEnv *env, int dx, int dy) {
 156     CONSOLE_SCREEN_BUFFER_INFO csbi;
 157     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 158         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 159     }
 160     csbi.dwCursorPosition.X += dx;
 161     csbi.dwCursorPosition.Y += dy;
 162     if (SetConsoleCursorPosition(hStdOut, csbi.dwCursorPosition) == 0) {
 163         JNU_ThrowIOExceptionWithLastError(env, "Console set cursole pos failed");
 164     }
 165 }
 166 
 167 JNIEXPORT void JNICALL
 168 Java_java_io_Console_moveLeft(JNIEnv *env, jobject this) {
 169     moveXY(env, -1, 0);
 170 }
 171 
 172 JNIEXPORT void JNICALL
 173 Java_java_io_Console_moveRight(JNIEnv *env, jobject this) {
 174     moveXY(env, 1, 0);
 175 }
 176 
 177 JNIEXPORT void JNICALL
 178 Java_java_io_Console_moveUp(JNIEnv *env, jobject this) {
 179     moveXY(env, 0, -1);
 180 }
 181 
 182 JNIEXPORT void JNICALL
 183 Java_java_io_Console_moveDown(JNIEnv *env, jobject this) {
 184     moveXY(env, 0, 1);
 185 }
 186 
 187 JNIEXPORT void JNICALL
 188 Java_java_io_Console_moveXY(JNIEnv *env, jobject this, jint x, jint y) {
 189     COORD xy;
 190     xy.X = (SHORT)x;
 191     xy.Y = (SHORT)y;
 192     if (SetConsoleCursorPosition(hStdOut, xy) == 0) {
 193         JNU_ThrowIOExceptionWithLastError(env, "Console set cursole pos failed");
 194     }
 195 }
 196 
 197 JNIEXPORT jint JNICALL
 198 Java_java_io_Console_getXY(JNIEnv *env, jobject this) {
 199     CONSOLE_SCREEN_BUFFER_INFO csbi;
 200     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 201         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 202     }
 203     return (csbi.dwCursorPosition.X << 16) & 0xff00 
 204         | csbi.dwCursorPosition.Y & 0xff;
 205 }
 206 
 207 JNIEXPORT jint JNICALL
 208 Java_java_io_Console_getWidth(JNIEnv *env, jobject this) {
 209     CONSOLE_SCREEN_BUFFER_INFO csbi;
 210     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 211         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 212     }
 213     return csbi.dwSize.X;
 214 }
 215 
 216 JNIEXPORT jint JNICALL
 217 Java_java_io_Console_getHeight(JNIEnv *env, jobject this) {
 218     CONSOLE_SCREEN_BUFFER_INFO csbi;
 219     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 220         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 221     }
 222     return csbi.dwSize.Y;
 223 }
 224 
 225 JNIEXPORT jint JNICALL
 226 Java_java_io_Console_readChar(JNIEnv *env, jobject this) {
 227     INPUT_RECORD record;
 228     DWORD n;
 229     while (TRUE) {
 230         if (ReadConsoleInputA(hStdIn, &record, 1, &n) == 0) {
 231             JNU_ThrowIOExceptionWithLastError(env, "Console read failed");
 232         }
 233         if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
 234              int vk = record.Event.KeyEvent.wVirtualKeyCode;
 235              if (vk >= 0x25 && vk <= 0x28) {
 236                  switch (vk) {
 237                      case 0x25 /*VK_LEFT */: return 2;
 238                      case 0x27 /*VK_RIGHT*/: return 6;
 239                      case 0x26 /*VK_UP   */: return 16;
 240                      case 0x28 /*VK_DOWN */: return 14;
 241                  }
 242              } 
 243              if (MapVirtualKey(vk, 2) != 0) 
 244                  return record.Event.KeyEvent.uChar.AsciiChar & 0xff;
 245         } 
 246         continue;
 247     }
 248 
 249 }
 250 
 251 JNIEXPORT jint JNICALL
 252 Java_java_io_Console_widthOf(JNIEnv *env, 
 253                               jobject this, 
 254                               jbyteArray bytes,
 255                               jint len) 
 256 {
 257     return len;
 258     /*
 259     int nwc = 0;
 260     int width = 0;
 261     cs[len] = 0;
 262     jbyte * cs = (*env)->GetByteArrayElements(env, bytes, NULL);    
 263     nwc = mbstowcs(NULL, (const char*)cs, 0);
 264     if (nwc > pwcsBufLen) {
 265         free(pwcsBuf);
 266         pwcsBufLen = nwc * 2;
 267         pwcsBuf = malloc(nwc *sizeof(wchar_t));
 268     }
 269     mbstowcs(pwcsBuf, (const char*)cs, pwcsBufLen);
 270     width = wcswidth(pwcsBuf, nwc);
 271     (*env)->ReleaseByteArrayElements(env, bytes, cs, 0);    
 272     return width;
 273     */
 274 }
 275 
 276 JNIEXPORT void JNICALL
 277 Java_java_io_Console_writeMBChar(JNIEnv *env, 
 278                                jobject this, 
 279                                jbyteArray bytes,
 280                                jint len) 
 281 {
 282     jbyte * cs = (*env)->GetByteArrayElements(env, bytes, NULL);    
 283     int n = 0;
 284     cs[len] = 0;
 285     if (WriteConsoleA(hStdOut, (const VOID*)cs, len, &n, NULL) == 0) {
 286         JNU_ThrowIOExceptionWithLastError(env, "Console write failed");
 287     }
 288     (*env)->ReleaseByteArrayElements(env, bytes, cs, 0);    
 289 
 290 }
 291 
 292 JNIEXPORT void JNICALL
 293 Java_java_io_Console_writeSBChar(JNIEnv *env, jobject this, jbyte ch) {
 294     int n = 0;
 295     char cs[2];
 296     cs[0] = ch & 0xff; cs[1] = 0;
 297     if (WriteConsole(hStdOut, (const VOID*)cs, 1, &n, NULL) == 0) {
 298         JNU_ThrowIOExceptionWithLastError(env, "Console write failed");
 299     }
 300 }
 301 
 302 JNIEXPORT void JNICALL
 303 Java_java_io_Console_nl(JNIEnv *env, jobject this) {
 304     int n = 0;
 305     if (WriteConsole(hStdOut, (const VOID*)"\n", 1, &n, NULL) == 0) {
 306         JNU_ThrowIOExceptionWithLastError(env, "Console write failed");
 307     }
 308 }
 309 
 310 JNIEXPORT void JNICALL
 311 Java_java_io_Console_cr(JNIEnv *env, jobject this) {
 312     CONSOLE_SCREEN_BUFFER_INFO csbi;
 313     if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0) {
 314         JNU_ThrowIOExceptionWithLastError(env, "Console get info failed");
 315     }
 316     csbi.dwCursorPosition.X = 0;
 317     if (SetConsoleCursorPosition(hStdOut, csbi.dwCursorPosition) == 0) {
 318         JNU_ThrowIOExceptionWithLastError(env, "Console set cursole pos failed");
 319     }
 320     /*
 321     int n = 0;
 322     if (WriteConsole(hStdOut, (const VOID*)"\r", 1, &n, NULL) == 0) {
 323         JNU_ThrowIOExceptionWithLastError(env, "Console write failed");
 324     }
 325     */
 326 }
 327 
 328 
 329