< prev index next >

src/java.base/windows/native/libjava/Console_md.c

Print this page
rev 51515 : 8209937: Enhance java.io.Console - provide methods to query console width and height
Contributed-by: christoph.langer@sap.com, matthias.baesken@sap.com
   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 {


   1 /*
   2  * Copyright (c) 2005, 2018, 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 #include <stdlib.h>
  31 #include <Wincon.h>
  32 
  33 static HANDLE hStdOut = INVALID_HANDLE_VALUE;
  34 static HANDLE hStdIn = INVALID_HANDLE_VALUE;
  35 
  36 JNIEXPORT jint JNICALL
  37 Java_java_io_Console_width(JNIEnv *env, jobject this)
  38 {
  39     CONSOLE_SCREEN_BUFFER_INFO csbi;
  40 
  41     if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
  42         if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) {
  43             return -1;
  44         }
  45     }
  46 
  47     return csbi.dwSize.X;
  48 }
  49 
  50 JNIEXPORT jint JNICALL
  51 Java_java_io_Console_height(JNIEnv *env, jobject this)
  52 {
  53     CONSOLE_SCREEN_BUFFER_INFO csbi;
  54 
  55     if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
  56         if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) {
  57             return -1;
  58         }
  59     }
  60 
  61     return csbi.dwSize.Y;
  62 }
  63 
  64 JNIEXPORT jboolean JNICALL
  65 Java_java_io_Console_istty(JNIEnv *env, jclass cls)
  66 {
  67     if (hStdIn == INVALID_HANDLE_VALUE &&
  68         (hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  69         return JNI_FALSE;
  70     }
  71     if (hStdOut == INVALID_HANDLE_VALUE &&
  72         (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  73         return JNI_FALSE;
  74     }
  75     if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
  76         GetFileType(hStdOut) != FILE_TYPE_CHAR)
  77         return JNI_FALSE;
  78     return JNI_TRUE;
  79 }
  80 
  81 JNIEXPORT jstring JNICALL
  82 Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
  83 {


< prev index next >