ReactOS 0.4.16-dev-297-gc569aee
kdterminal.c File Reference
#include <ntoskrnl.h>
#include "kd.h"
#include "kdterminal.h"
Include dependency graph for kdterminal.c:

Go to the source code of this file.

Macros

#define KdbpGetCharKeyboard(ScanCode)   KdbpTryGetCharKeyboard((ScanCode), 0)
 
#define KdbpGetCharSerial()   KdbpTryGetCharSerial(0)
 

Functions

CHAR KdbpTryGetCharKeyboard (PULONG ScanCode, ULONG Retry)
 
CHAR KdbpTryGetCharSerial (_In_ ULONG Retry)
 
VOID KdbpSendCommandSerial (_In_ PCSTR Command)
 
BOOLEAN KdpInitTerminal (VOID)
 Initializes the controlling terminal.
 
BOOLEAN KdpUpdateTerminalSize (_Out_ PSIZE TermSize)
 
VOID KdpFlushTerminalInput (VOID)
 Flushes terminal input (either serial or PS/2).
 
CHAR KdpReadTermKey (_Out_ PULONG ScanCode)
 Reads one character from the terminal. This function returns a scan code even when reading is done from a serial terminal.
 

Variables

ULONG KdbDebugState = 0
 
SIZE KdTermSize = {0,0}
 
BOOLEAN KdTermConnected = FALSE
 
BOOLEAN KdTermSerial = FALSE
 
BOOLEAN KdTermReportsSize = TRUE
 
static CHAR KdTermNextKey = ANSI_NULL
 

Macro Definition Documentation

◆ KdbpGetCharKeyboard

#define KdbpGetCharKeyboard (   ScanCode)    KdbpTryGetCharKeyboard((ScanCode), 0)

Definition at line 15 of file kdterminal.c.

◆ KdbpGetCharSerial

#define KdbpGetCharSerial ( )    KdbpTryGetCharSerial(0)

Definition at line 19 of file kdterminal.c.

Function Documentation

◆ KdbpSendCommandSerial()

VOID KdbpSendCommandSerial ( _In_ PCSTR  Command)

Definition at line 17 of file kdserial.c.

19{
20 while (*Command)
22}
VOID NTAPI KdPortPutByteEx(PCPPORT PortInformation, UCHAR ByteToSend)
CPPORT SerialPortInfo
Definition: kdio.c:40
Definition: shell.h:41

Referenced by KdpInitTerminal(), and KdpUpdateTerminalSize().

◆ KdbpTryGetCharKeyboard()

CHAR KdbpTryGetCharKeyboard ( PULONG  ScanCode,
ULONG  Retry 
)

Definition at line 104 of file kdps2kbd.c.

105{
106 static byte_t last_key = 0;
107 static byte_t shift = 0;
108 char c;
109 BOOLEAN KeepRetrying = (Retry == 0);
110
111 while (KeepRetrying || Retry-- > 0)
112 {
113 while (kbd_read_status() & KBD_STAT_OBF)
114 {
115 byte_t scancode;
116
117 scancode = kbd_read_input();
118
119 /* check for SHIFT-keys */
120 if (((scancode & 0x7F) == 42) || ((scancode & 0x7F) == 54))
121 {
122 shift = !(scancode & 0x80);
123 continue;
124 }
125
126 /* ignore all other RELEASED-codes */
127 if (scancode & 0x80)
128 {
129 last_key = 0;
130 }
131 else if (last_key != scancode)
132 {
133 //printf("kbd: %d, %d, %c\n", scancode, last_key, keyb_layout[shift][scancode]);
134 last_key = scancode;
135 c = keyb_layout[shift][scancode];
136 *ScanCode = scancode;
137
138 if (c > 0)
139 return c;
140 }
141 }
142 }
143
144 return -1;
145}
unsigned char BOOLEAN
UINT ScanCode
Definition: VirtualKey.c:24
_In_ PSCSI_REQUEST_BLOCK _Out_ NTSTATUS _Inout_ BOOLEAN * Retry
Definition: classpnp.h:312
const GLubyte * c
Definition: glext.h:8905
#define KBD_STAT_OBF
Definition: kdps2kbd.c:22
#define kbd_read_status()
Definition: kdps2kbd.c:36
static unsigned char keyb_layout[2][128]
Definition: kdps2kbd.c:38
UCHAR byte_t
Definition: kdps2kbd.c:57
#define kbd_read_input()
Definition: kdps2kbd.c:35
#define c
Definition: ke_i.h:80
#define shift
Definition: input.c:1755

Referenced by KdpFlushTerminalInput(), and KdpReadTermKey().

◆ KdbpTryGetCharSerial()

CHAR KdbpTryGetCharSerial ( _In_ ULONG  Retry)

Definition at line 25 of file kdserial.c.

27{
28 CHAR Result = -1;
29
30 if (Retry == 0)
32 else
33 while (!KdPortGetByteEx(&SerialPortInfo, (PUCHAR)&Result) && Retry-- > 0);
34
35 return Result;
36}
BOOLEAN NTAPI KdPortGetByteEx(PCPPORT PortInformation, PUCHAR ByteReceived)
unsigned char * PUCHAR
Definition: typedefs.h:53
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
char CHAR
Definition: xmlstorage.h:175

Referenced by KdpFlushTerminalInput(), KdpInitTerminal(), KdpReadTermKey(), and KdpUpdateTerminalSize().

◆ KdpFlushTerminalInput()

VOID KdpFlushTerminalInput ( VOID  )

Flushes terminal input (either serial or PS/2).

Definition at line 197 of file kdterminal.c.

198{
201 {
202 while (KdbpTryGetCharSerial(1) != -1);
203 }
204 else
205 {
207 while (KdbpTryGetCharKeyboard(&ScanCode, 1) != -1);
208 }
209}
CHAR KdbpTryGetCharKeyboard(PULONG ScanCode, ULONG Retry)
Definition: kdps2kbd.c:104
static CHAR KdTermNextKey
Definition: kdterminal.c:38
ULONG KdbDebugState
Definition: kdterminal.c:32
CHAR KdbpTryGetCharSerial(_In_ ULONG Retry)
Definition: kdserial.c:25
@ KD_DEBUG_KDSERIAL
Definition: kdterminal.h:36
#define ANSI_NULL
uint32_t ULONG
Definition: typedefs.h:59

Referenced by KdIoReadLine(), KdpInitTerminal(), and KdpUpdateTerminalSize().

◆ KdpInitTerminal()

BOOLEAN KdpInitTerminal ( VOID  )

Initializes the controlling terminal.

Returns
TRUE if the controlling terminal is serial and detected as being connected, or FALSE if not.

Definition at line 51 of file kdterminal.c.

52{
53 /* Determine whether the controlling terminal is a serial terminal:
54 * serial output is enabled *and* KDSERIAL is set (i.e. user input
55 * through serial). */
57#if 0
58 // Old logic where KDSERIAL also enables serial output.
61#else
62 // New logic where KDSERIAL does not necessarily enable serial output.
65#endif
66
67 /* Flush the input buffer */
69
70 if (KdTermSerial)
71 {
73
74 /* Enable line-wrap */
75 KdbpSendCommandSerial("\x1b[?7h");
76
77 /*
78 * Query terminal type.
79 * Historically it was done with CTRL-E ('\x05'), however nowadays
80 * terminals respond to it with an empty (or a user-configurable)
81 * string. Instead, use the VT52-compatible 'ESC Z' sequence or the
82 * VT100-compatible 'ESC[c' one.
83 */
84 KdbpSendCommandSerial("\x1b[c");
86
87 Length = 0;
88 for (;;)
89 {
90 /* Verify we get an answer, but don't care about it */
91 if (KdbpTryGetCharSerial(5000) == -1)
92 break;
93 ++Length;
94 }
95
96 /* Terminal is connected (TRUE) or not connected (FALSE) */
97 KdTermConnected = (Length > 0);
98 }
99 else
100 {
101 /* Terminal is not serial, assume it's *not* connected */
103 }
104 return KdTermConnected;
105}
#define FALSE
Definition: types.h:117
KDP_DEBUG_MODE KdpDebugMode
Definition: kdio.c:46
VOID KdpFlushTerminalInput(VOID)
Flushes terminal input (either serial or PS/2).
Definition: kdterminal.c:197
BOOLEAN KdTermConnected
Definition: kdterminal.c:34
VOID KdbpSendCommandSerial(_In_ PCSTR Command)
Definition: kdserial.c:17
BOOLEAN KdTermSerial
Definition: kdterminal.c:35
#define KeStallExecutionProcessor(MicroSeconds)
Definition: precomp.h:27
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
UCHAR Screen
Definition: kd.h:100
UCHAR Serial
Definition: kd.h:101

Referenced by KdbpPagerInternal().

◆ KdpReadTermKey()

CHAR KdpReadTermKey ( _Out_ PULONG  ScanCode)

Reads one character from the terminal. This function returns a scan code even when reading is done from a serial terminal.

Definition at line 217 of file kdterminal.c.

219{
220 CHAR Key;
221
222 *ScanCode = 0;
223
225 {
228 if (Key == KEY_ESC) /* ESC */
229 {
231 if (Key == '[')
232 {
234 switch (Key)
235 {
236 case 'A':
238 break;
239 case 'B':
241 break;
242 case 'C':
243 break;
244 case 'D':
245 break;
246 }
247 }
248 }
249 }
250 else
251 {
254 }
255
256 /* Check for return */
257 if (Key == '\r')
258 {
259 /*
260 * We might need to discard the next '\n' which most clients
261 * should send after \r. Wait a bit to make sure we receive it.
262 */
264
267 else
269
270 if (KdTermNextKey == '\n' || KdTermNextKey == -1) /* \n or no response at all */
272 }
273
274 return Key;
275}
#define KdbpGetCharSerial()
Definition: kdterminal.c:19
#define KdbpGetCharKeyboard(ScanCode)
Definition: kdterminal.c:15
#define KEY_SCAN_DOWN
Definition: kdterminal.h:15
#define KEY_SCAN_UP
Definition: kdterminal.h:14
#define KEY_ESC
Definition: keycodes.h:49

Referenced by KdbpPagerInternal(), and KdIoReadLine().

◆ KdpUpdateTerminalSize()

BOOLEAN KdpUpdateTerminalSize ( _Out_ PSIZE  TermSize)

Definition at line 108 of file kdterminal.c.

110{
111 static CHAR Buffer[128];
112 CHAR c;
113 LONG NumberOfCols = -1; // Or initialize to TermSize->cx ??
114 LONG NumberOfRows = -1; // Or initialize to TermSize->cy ??
115
116 /* Retrieve the size of the controlling terminal only when it is serial */
118 {
119 /* Flush the input buffer */
121
122 /* Try to query the terminal size. A reply looks like "\x1b[8;24;80t" */
124 KdbpSendCommandSerial("\x1b[18t");
126
127 c = KdbpTryGetCharSerial(5000);
128 if (c == KEY_ESC)
129 {
130 c = KdbpTryGetCharSerial(5000);
131 if (c == '[')
132 {
133 ULONG Length = 0;
134 for (;;)
135 {
136 c = KdbpTryGetCharSerial(5000);
137 if (c == -1)
138 break;
139
140 Buffer[Length++] = c;
141 if (isalpha(c) || Length >= (sizeof(Buffer) - 1))
142 break;
143 }
145
146 if (Buffer[0] == '8' && Buffer[1] == ';')
147 {
148 SIZE_T i;
149 for (i = 2; (i < Length) && (Buffer[i] != ';'); i++);
150
151 if (Buffer[i] == ';')
152 {
153 Buffer[i++] = ANSI_NULL;
154
155 /* Number of rows is now at Buffer + 2
156 * and number of columns at Buffer + i */
157 NumberOfRows = strtoul(Buffer + 2, NULL, 0);
158 NumberOfCols = strtoul(Buffer + i, NULL, 0);
160 }
161 }
162 }
163 /* Clear further characters */
164 while (KdbpTryGetCharSerial(5000) != -1);
165 }
166 }
167
168 if (NumberOfCols <= 0)
169 {
170 /* Set the number of columns to the default */
172 NumberOfCols = (SCREEN_WIDTH / 8 /*BOOTCHAR_WIDTH*/);
173 else
174 NumberOfCols = 80;
175 }
176 if (NumberOfRows <= 0)
177 {
178 /* Set the number of rows to the default */
180 NumberOfRows = (SCREEN_HEIGHT / (13 /*BOOTCHAR_HEIGHT*/ + 1));
181 else
182 NumberOfRows = 24;
183 }
184
185 TermSize->cx = NumberOfCols;
186 TermSize->cy = NumberOfRows;
187
188 // KdIoPrintf("Cols/Rows: %dx%d\n", TermSize->cx, TermSize->cy);
189
190 return KdTermReportsSize;
191}
#define isalpha(c)
Definition: acclib.h:74
UINT32 strtoul(const char *String, char **Terminator, UINT32 Base)
Definition: utclib.c:696
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
BOOLEAN KdTermReportsSize
Definition: kdterminal.c:36
#define SCREEN_WIDTH
Definition: pc98video.c:27
#define SCREEN_HEIGHT
Definition: pc98video.c:28
long LONG
Definition: pedump.c:60
ULONG_PTR SIZE_T
Definition: typedefs.h:80

Referenced by KdbpPagerInternal().

Variable Documentation

◆ KdbDebugState

◆ KdTermConnected

BOOLEAN KdTermConnected = FALSE

Definition at line 34 of file kdterminal.c.

Referenced by KdbpPagerInternal(), KdpInitTerminal(), and KdpUpdateTerminalSize().

◆ KdTermNextKey

CHAR KdTermNextKey = ANSI_NULL
static

Definition at line 38 of file kdterminal.c.

Referenced by KdpFlushTerminalInput(), and KdpReadTermKey().

◆ KdTermReportsSize

BOOLEAN KdTermReportsSize = TRUE

Definition at line 36 of file kdterminal.c.

Referenced by KdpUpdateTerminalSize().

◆ KdTermSerial

BOOLEAN KdTermSerial = FALSE

Definition at line 35 of file kdterminal.c.

Referenced by KdpInitTerminal(), and KdpUpdateTerminalSize().

◆ KdTermSize

SIZE KdTermSize = {0,0}

Definition at line 33 of file kdterminal.c.

Referenced by KdbpPagerInternal().