1 /*
   2  * Copyright (c) 2015, 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 "jvm.h"
  28 #include "jdk_internal_jline_WindowsTerminal.h"
  29 
  30 #include <stdlib.h>
  31 #include <Wincon.h>
  32 #include <Winuser.h>
  33 
  34 JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getConsoleMode
  35   (JNIEnv *, jobject) {
  36     HANDLE hStdIn;
  37     if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  38         return -1;
  39     }
  40     DWORD fdwMode;
  41     if (! GetConsoleMode(hStdIn, &fdwMode)) {
  42         return -1;
  43     }
  44     return fdwMode;
  45 }
  46 
  47 JNIEXPORT void JNICALL Java_jdk_internal_jline_WindowsTerminal_setConsoleMode
  48   (JNIEnv *, jobject, jint mode) {
  49     HANDLE hStdIn;
  50     if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  51         return ;
  52     }
  53     DWORD fdwMode = mode;
  54     SetConsoleMode(hStdIn, fdwMode);
  55 }
  56 
  57 JNIEXPORT jobject JNICALL Java_jdk_internal_jline_WindowsTerminal_readKeyEvent
  58   (JNIEnv *env, jobject) {
  59     HANDLE hStdIn;
  60     if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  61         return NULL;
  62     }
  63     INPUT_RECORD record;
  64     DWORD n;
  65     while (TRUE) {
  66         if (ReadConsoleInputA(hStdIn, &record, 1, &n) == 0) {
  67             return NULL;
  68         }
  69         if (record.EventType == KEY_EVENT) {
  70             jclass clazz = env->FindClass("jdk/internal/jline/WindowsTerminal$KEY_EVENT_RECORD");
  71             jmethodID constr = env->GetMethodID(clazz, "<init>", "(ZCIII)V");
  72             return env->NewObject(clazz,
  73                                      constr,
  74                                      record.Event.KeyEvent.bKeyDown,
  75                                      record.Event.KeyEvent.uChar.UnicodeChar,
  76                                      record.Event.KeyEvent.dwControlKeyState,
  77                                      record.Event.KeyEvent.wVirtualKeyCode,
  78                                      record.Event.KeyEvent.wRepeatCount);
  79         }
  80         continue;
  81     }
  82 }
  83 
  84 JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getConsoleOutputCodepage
  85   (JNIEnv *, jobject) {
  86     return GetConsoleCP();
  87 }
  88 
  89 JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getWindowsTerminalWidth
  90   (JNIEnv *, jobject) {
  91     HANDLE hStdIn;
  92     if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  93         return -1;
  94     }
  95     CONSOLE_SCREEN_BUFFER_INFO info;
  96     if (! GetConsoleScreenBufferInfo(hStdIn, &info)) {
  97         return -1;
  98     }
  99     return info.dwSize.X;
 100 }
 101 
 102 JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getWindowsTerminalHeight
 103   (JNIEnv *, jobject) {
 104     HANDLE hStdIn;
 105     if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
 106         return -1;
 107     }
 108     CONSOLE_SCREEN_BUFFER_INFO info;
 109     if (! GetConsoleScreenBufferInfo(hStdIn, &info)) {
 110         return -1;
 111     }
 112     return info.dwSize.Y;
 113 }