|
| 1 | +/* conio.c |
| 2 | + * Author: Pragma Systems, Inc. <www.pragmasys.com> |
| 3 | + * Contribution by Pragma Systems, Inc. for Microsoft openssh win32 port |
| 4 | + * Copyright (c) 2011, 2015 Pragma Systems, Inc. |
| 5 | + * All rights reserved |
| 6 | + * |
| 7 | + * Inserts data into Windows Console Input. WriteToConsole() API implemented. |
| 8 | + * |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice. |
| 13 | + * 2. Binaries produced provide no direct or implied warranties or any |
| 14 | + * guarantee of performance or suitability. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <stdlib.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +#include <windows.h> |
| 22 | + |
| 23 | +#include <console.h> |
| 24 | +#include <conio.h> |
| 25 | + |
| 26 | +COORD lastCursorLoc = { 0, 0 }; |
| 27 | +BYTE KeyboardState[256]; |
| 28 | +INPUT_RECORD srec; |
| 29 | +DWORD dwGlobalConsoleMode ; |
| 30 | + |
| 31 | +int WriteToConsole(HANDLE fd, unsigned char *buf, size_t len, size_t *dwWritten, void *flag) |
| 32 | +{ |
| 33 | + static KEY_EVENT_RECORD *pkey; |
| 34 | + static KEY_EVENT_RECORD *pkey2; |
| 35 | + static INPUT_RECORD irec[2]; |
| 36 | + static BOOL bInitKeyboard = TRUE; |
| 37 | + size_t ctr; |
| 38 | + int rc; |
| 39 | + DWORD dwRecords; |
| 40 | + DWORD vkey; |
| 41 | + BOOL bNeedToWait = TRUE; |
| 42 | + CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 43 | + |
| 44 | + int scr_width = 80; /* screen horizontal width, e.g. 80 */ |
| 45 | + int scr_height = 25; /* screen vertical length, e.g. 25 */ |
| 46 | + char tmpbuf[2]; |
| 47 | + int local_echo = 0; |
| 48 | + |
| 49 | + /* |
| 50 | + * Need to set pkey and pkey2 which we use below. Initialize the keyboard state table. |
| 51 | + */ |
| 52 | + if (bInitKeyboard) |
| 53 | + { |
| 54 | + GetKeyboardState(KeyboardState); |
| 55 | + bInitKeyboard = FALSE; |
| 56 | + srec.EventType = KEY_EVENT; |
| 57 | + srec.Event.KeyEvent.bKeyDown = TRUE; |
| 58 | + srec.Event.KeyEvent.wRepeatCount = 1; |
| 59 | + srec.Event.KeyEvent.wVirtualKeyCode = 0x10; |
| 60 | + srec.Event.KeyEvent.wVirtualScanCode = 0x2a; |
| 61 | + srec.Event.KeyEvent.uChar.AsciiChar = 0; |
| 62 | + srec.Event.KeyEvent.uChar.UnicodeChar = 0; |
| 63 | + srec.Event.KeyEvent.dwControlKeyState = 0x10; |
| 64 | + |
| 65 | + irec[0].EventType = KEY_EVENT; /* init key down message */ |
| 66 | + pkey = &(irec[0].Event.KeyEvent); |
| 67 | + pkey->wRepeatCount = 1; |
| 68 | + pkey->bKeyDown = TRUE; |
| 69 | + |
| 70 | + irec[1].EventType = KEY_EVENT; /* init key up message */ |
| 71 | + pkey2 = &(irec[1].Event.KeyEvent); |
| 72 | + pkey2->wRepeatCount = 1; |
| 73 | + pkey2->bKeyDown = FALSE; |
| 74 | + } |
| 75 | + |
| 76 | + // Stream mode processing |
| 77 | + if (local_echo) |
| 78 | + { |
| 79 | + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 80 | + } |
| 81 | + |
| 82 | + GetConsoleMode(fd, &dwGlobalConsoleMode); |
| 83 | + |
| 84 | + ctr = 0; |
| 85 | + while (ctr < len) |
| 86 | + { |
| 87 | + if (local_echo) |
| 88 | + { |
| 89 | + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 90 | + lastCursorLoc.Y = csbi.dwCursorPosition.Y; |
| 91 | + lastCursorLoc.X = csbi.dwCursorPosition.X; |
| 92 | + } |
| 93 | + { |
| 94 | + pkey->dwControlKeyState = 0x00000000; |
| 95 | + pkey->uChar.AsciiChar = buf[ctr]; /* next char in ascii */ |
| 96 | + |
| 97 | + mbtowc(&(pkey->uChar.UnicodeChar), (const char *)&(buf[ctr]), 1); |
| 98 | + vkey = VkKeyScan(pkey->uChar.AsciiChar); |
| 99 | + |
| 100 | + if ((BYTE)(vkey >> 8) != 0xFF) // high order word |
| 101 | + { |
| 102 | + if (vkey & 0x0100 || (KeyboardState[VK_LSHIFT] & 0x80)) /* high word gives shift, ctrl, alt status */ |
| 103 | + pkey->dwControlKeyState |= SHIFT_PRESSED; /* shift key presssed*/ |
| 104 | + if (vkey & 0x0200 || (KeyboardState[VK_LCONTROL] & 0x80)) |
| 105 | + pkey->dwControlKeyState |= LEFT_CTRL_PRESSED; /* any ctrl really*/ |
| 106 | + if ((vkey & 0x0400) || (KeyboardState[VK_LMENU] & 0x80)) |
| 107 | + pkey->dwControlKeyState |= LEFT_ALT_PRESSED; /* any ALT really*/ |
| 108 | + } |
| 109 | + if ((BYTE)vkey != 0xFF) // low order word |
| 110 | + { |
| 111 | + pkey->wVirtualKeyCode = (BYTE)vkey; |
| 112 | + pkey->wVirtualScanCode = MapVirtualKey(pkey->wVirtualKeyCode, 0); |
| 113 | + if (pkey->uChar.UnicodeChar == 0x1b) // stream mode fix for Admark ESC sequences |
| 114 | + pkey->wVirtualKeyCode = 0x00db; |
| 115 | + |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + /* we need to mimic key up and key down */ |
| 120 | + if (pkey->dwControlKeyState & 0x0100) |
| 121 | + { |
| 122 | + srec.Event.KeyEvent.bKeyDown = TRUE; |
| 123 | + srec.Event.KeyEvent.dwControlKeyState = 0x10; |
| 124 | + WriteConsoleInput(fd, &srec, 1, &dwRecords); /* write shift down */ |
| 125 | + tmpbuf[0] = irec[0].Event.KeyEvent.uChar.AsciiChar; |
| 126 | + tmpbuf[1] = '\0'; |
| 127 | + } |
| 128 | + |
| 129 | + pkey->bKeyDown = TRUE; /*since pkey is mucked by others we do it again*/ |
| 130 | + |
| 131 | + /* dup these into key up message structure from key down message */ |
| 132 | + pkey2->wVirtualKeyCode = pkey->wVirtualKeyCode; |
| 133 | + pkey2->wVirtualScanCode = pkey->wVirtualScanCode; |
| 134 | + pkey2->uChar.AsciiChar = pkey->uChar.AsciiChar; |
| 135 | + pkey2->uChar.UnicodeChar = pkey->uChar.UnicodeChar; |
| 136 | + pkey2->dwControlKeyState = pkey->dwControlKeyState; |
| 137 | + |
| 138 | + WriteConsoleInput(fd, irec, 2, &dwRecords); /* key down,up msgs */ |
| 139 | + tmpbuf[0] = irec[0].Event.KeyEvent.uChar.AsciiChar; |
| 140 | + tmpbuf[1] = '\0'; |
| 141 | + if (pkey->dwControlKeyState & 0x0100) |
| 142 | + { |
| 143 | + srec.Event.KeyEvent.bKeyDown = FALSE; |
| 144 | + srec.Event.KeyEvent.dwControlKeyState = 0x0; |
| 145 | + WriteConsoleInput(fd, &srec, 1, &dwRecords); /* write shift up */ |
| 146 | + |
| 147 | + } |
| 148 | + //if ((local_echo)) |
| 149 | + //{ |
| 150 | + // bNeedToWait = EchoInputCharacter(buf[ctr], &csbi.dwCursorPosition, dwGlobalConsoleMode); |
| 151 | + //} |
| 152 | + } |
| 153 | + ctr++; |
| 154 | + Sleep(0); |
| 155 | + } |
| 156 | + |
| 157 | + *dwWritten = len; |
| 158 | + |
| 159 | + //netflush(); |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | + |
0 commit comments