001    /**
002     * Copyright (C) 2009, Progress Software Corporation and/or its 
003     * subsidiaries or affiliates.  All rights reserved.
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.fusesource.jansi.internal;
018    
019    import static org.fusesource.jansi.internal.Kernel32.*;
020    
021    import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
022    
023    /**
024     * 
025     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
026     */
027    public class WindowsSupport {
028            
029            public static String getLastErrorMessage() {
030                    int errorCode = GetLastError();
031                    int bufferSize = 160;
032                    byte data[] = new byte[bufferSize]; 
033                    FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null);
034                    return new String(data);
035            }
036            
037        //////////////////////////////////////////////////////////////////////////
038        //
039        // The following helper methods are for jline 
040        //
041        //////////////////////////////////////////////////////////////////////////
042        
043        public static int readByte() {
044            return _getch();
045        }
046        
047        public static int getConsoleMode() {
048            long hConsole = GetStdHandle (STD_INPUT_HANDLE);
049            if (hConsole == INVALID_HANDLE_VALUE)
050                return -1;
051            int mode[] = new int[1];
052            if (GetConsoleMode (hConsole, mode)==0)
053                return -1;
054            return mode[0];
055        }
056    
057        public static void setConsoleMode(int mode) {
058            long hConsole = GetStdHandle (STD_INPUT_HANDLE);
059            if (hConsole == INVALID_HANDLE_VALUE)
060                return;
061            SetConsoleMode (hConsole, mode);
062        }
063        
064        public static int getWindowsTerminalWidth() {
065            long outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
066            CONSOLE_SCREEN_BUFFER_INFO info = new CONSOLE_SCREEN_BUFFER_INFO(); 
067            GetConsoleScreenBufferInfo (outputHandle, info);
068            return info.windowWidth();        
069        }
070    
071        public static int getWindowsTerminalHeight() {
072            long outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
073            CONSOLE_SCREEN_BUFFER_INFO info = new CONSOLE_SCREEN_BUFFER_INFO(); 
074            GetConsoleScreenBufferInfo (outputHandle, info);
075            return info.windowHeight();        
076        }
077    
078        public static KEY_EVENT_RECORD readConsoleInput() {
079            long hConsole = GetStdHandle (STD_INPUT_HANDLE);
080            if (hConsole == INVALID_HANDLE_VALUE)
081                return null;
082            return readKeyEvent(hConsole); 
083        }
084    
085    }