jdk/src/jdk.internal.le/windows/native/lible/WindowsTerminal.cpp
author vinnie
Mon, 15 Aug 2016 14:25:51 +0100
changeset 40276 2217f830ca7b
parent 31530 aa87d96a02b1
child 43107 b232ec246f2b
permissions -rw-r--r--
8163503: PKCS12 keystore cannot store non-X.509 certificates Reviewed-by: weijun, xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31530
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     1
/*
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     4
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    10
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    16
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    20
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    23
 * questions.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    24
 */
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    25
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    26
#include "jni.h"
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    27
#include "jni_util.h"
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    28
#include "jvm.h"
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    29
#include "jdk_internal_jline_WindowsTerminal.h"
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    30
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    31
#include <stdlib.h>
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    32
#include <Wincon.h>
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    33
#include <Winuser.h>
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    34
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    35
static jclass recordClass;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    36
static jmethodID recordConstructor;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    37
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    38
JNIEXPORT void JNICALL Java_jdk_internal_jline_WindowsTerminal_initIDs
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    39
  (JNIEnv *env, jclass) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    40
    jclass cls = env->FindClass("jdk/internal/jline/WindowsTerminal$KEY_EVENT_RECORD");
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    41
    CHECK_NULL(cls);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    42
    recordClass = (jclass) env->NewGlobalRef(cls);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    43
    CHECK_NULL(recordClass);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    44
    recordConstructor = env->GetMethodID(cls, "<init>", "(ZCIII)V");
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    45
    CHECK_NULL(recordConstructor);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    46
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    47
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    48
JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getConsoleMode
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    49
  (JNIEnv *, jobject) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    50
    HANDLE hStdIn;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    51
    if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    52
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    53
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    54
    DWORD fdwMode;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    55
    if (! GetConsoleMode(hStdIn, &fdwMode)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    56
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    57
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    58
    return fdwMode;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    59
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    60
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    61
JNIEXPORT void JNICALL Java_jdk_internal_jline_WindowsTerminal_setConsoleMode
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    62
  (JNIEnv *, jobject, jint mode) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    63
    HANDLE hStdIn;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    64
    if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    65
        return ;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    66
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    67
    DWORD fdwMode = mode;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    68
    SetConsoleMode(hStdIn, fdwMode);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    69
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    70
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    71
JNIEXPORT jobject JNICALL Java_jdk_internal_jline_WindowsTerminal_readKeyEvent
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    72
  (JNIEnv *env, jobject) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    73
    HANDLE hStdIn;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    74
    if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    75
        return NULL;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    76
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    77
    INPUT_RECORD record;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    78
    DWORD n;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    79
    while (TRUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    80
        if (ReadConsoleInput(hStdIn, &record, 1, &n) == 0) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    81
            return NULL;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    82
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    83
        if (record.EventType == KEY_EVENT) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    84
            jclass clazz = env->FindClass("jdk/internal/jline/WindowsTerminal$KEY_EVENT_RECORD");
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    85
            jmethodID constr = env->GetMethodID(clazz, "<init>", "(ZCIII)V");
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    86
            return env->NewObject(recordClass,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    87
                                  recordConstructor,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    88
                                  record.Event.KeyEvent.bKeyDown,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    89
                                  record.Event.KeyEvent.uChar.UnicodeChar,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    90
                                  record.Event.KeyEvent.dwControlKeyState,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    91
                                  record.Event.KeyEvent.wVirtualKeyCode,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    92
                                  record.Event.KeyEvent.wRepeatCount);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    93
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    94
        continue;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    95
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    96
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    97
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    98
JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getConsoleOutputCodepage
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    99
  (JNIEnv *, jobject) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   100
    return GetConsoleCP();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   101
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   102
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   103
JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getWindowsTerminalWidth
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   104
  (JNIEnv *, jobject) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   105
    HANDLE hStdIn;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   106
    if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   107
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   108
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   109
    CONSOLE_SCREEN_BUFFER_INFO info;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   110
    if (! GetConsoleScreenBufferInfo(hStdIn, &info)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   111
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   112
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   113
    return info.dwSize.X;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   114
}
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   115
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   116
JNIEXPORT jint JNICALL Java_jdk_internal_jline_WindowsTerminal_getWindowsTerminalHeight
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   117
  (JNIEnv *, jobject) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   118
    HANDLE hStdIn;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   119
    if ((hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   120
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   121
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   122
    CONSOLE_SCREEN_BUFFER_INFO info;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   123
    if (! GetConsoleScreenBufferInfo(hStdIn, &info)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   124
        return -1;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   125
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   126
    return info.dwSize.Y;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   127
}