Skip to content

Commit b22f85a

Browse files
rednoahmatthiasblaesing
authored andcommitted
Add additional Wincon bindings
* GetConsoleScreenBufferInfo * ReadConsoleInput * WriteConsole
1 parent 2afa6da commit b22f85a

3 files changed

Lines changed: 236 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Features
1414
* [#1178](https://github.com/java-native-access/jna/pull/1178): Add `c.s.j.p.win32.IPHlpAPI#GetTcpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetUdpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetTcpStatisticsEx` and `c.s.j.p.win32.IPHlpAPI#GetUdpStatisticsEx` - [@dbwiddis](https://github.com/dbwiddis).
1515
* [#1191](https://github.com/java-native-access/jna/pull/1191): Add `c.s.j.p.win32.Advapi32Util#getTokenPrimaryGroup` - [@dbwiddis](https://github.com/dbwiddis).
1616
* [#1182](https://github.com/java-native-access/jna/pull/1182): Add `toString` to classes extending `c.s.j.ptr.ByReference` - [@dbwiddis](https://github.com/dbwiddis).
17+
* [#1194](https://github.com/java-native-access/jna/pull/1194): Add `GetConsoleScreenBufferInfo`, `ReadConsoleInput` and `WriteConsole` with associated structures to `c.s.j.p.win32.Wincon` - [@rednoah](https://github.com/rednoah).
1718

1819
Bug Fixes
1920
---------

contrib/platform/src/com/sun/jna/platform/win32/Wincon.java

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
package com.sun.jna.platform.win32;
2525

2626
import com.sun.jna.Native;
27+
import com.sun.jna.Structure;
28+
import com.sun.jna.Structure.FieldOrder;
29+
import com.sun.jna.Union;
2730
import com.sun.jna.platform.win32.WinDef.HWND;
31+
import com.sun.jna.platform.win32.WinDef.LPVOID;
2832
import com.sun.jna.platform.win32.WinNT.HANDLE;
2933
import com.sun.jna.ptr.IntByReference;
3034

@@ -182,6 +186,9 @@ public interface Wincon {
182186
int ENABLE_INSERT_MODE=0x0020;
183187
int ENABLE_QUICK_EDIT_MODE=0x0040;
184188
int ENABLE_EXTENDED_FLAGS=0x0080;
189+
int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
190+
int DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
191+
int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
185192

186193
/* If the hConsoleHandle parameter is a screen buffer handle, the mode
187194
* can be one or more of the following values
@@ -249,4 +256,181 @@ public interface Wincon {
249256
* @see <a href="https://msdn.microsoft.com/en-us/library/ms686050(v=vs.85).aspx">SetConsoleTitle documentation</a>
250257
*/
251258
boolean SetConsoleTitle(String lpConsoleTitle);
259+
260+
/**
261+
* Retrieves information about the specified console screen buffer.
262+
* @param hConsoleOutput A handle to the console screen buffer.
263+
* @param lpConsoleScreenBufferInfo A pointer to a CONSOLE_SCREEN_BUFFER_INFO structure that receives the console screen buffer information.
264+
* @return {@code true} if successful - if {@code false} then use
265+
* {@code GetLastError()} to get extended error information
266+
* @see <a href="https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo">GetConsoleScreenBufferInfo documentation</a>
267+
*/
268+
boolean GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
269+
270+
/**
271+
* Reads data from a console input buffer and removes it from the buffer.
272+
* @param hConsoleInput A handle to the console input buffer.
273+
* @param lpBuffer A pointer to an array of INPUT_RECORD structures that receives the input buffer data.
274+
* @param nLength The size of the array pointed to by the lpBuffer parameter, in array elements.
275+
* @param lpNumberOfEventsRead A pointer to a variable that receives the number of input records read.
276+
* @return {@code true} if successful - if {@code false} then use
277+
* {@code GetLastError()} to get extended error information
278+
* @see <a href="https://docs.microsoft.com/en-us/windows/console/readconsoleinput">ReadConsoleInput documentation</a>
279+
*/
280+
boolean ReadConsoleInput(HANDLE hConsoleInput, INPUT_RECORD[] lpBuffer, int nLength, IntByReference lpNumberOfEventsRead);
281+
282+
/**
283+
* Writes a character string to a console screen buffer beginning at the current cursor location.
284+
* @param hConsoleOutput A handle to the console screen buffer.
285+
* @param lpBuffer A pointer to a buffer that contains characters to be written to the console screen buffer.
286+
* @param nNumberOfCharsToWrite The number of characters to be written.
287+
* @param lpNumberOfCharsWritten A pointer to a variable that receives the number of characters actually written.
288+
* @param lpReserved Reserved; must be NULL.
289+
* @return {@code true} if successful - if {@code false} then use
290+
* {@code GetLastError()} to get extended error information
291+
* @see <a href="https://docs.microsoft.com/en-us/windows/console/writeconsole">WriteConsole documentation</a>
292+
*/
293+
boolean WriteConsole(HANDLE hConsoleOutput, String lpBuffer, int nNumberOfCharsToWrite, IntByReference lpNumberOfCharsWritten, LPVOID lpReserved);
294+
295+
/**
296+
* COORD structure
297+
*/
298+
@FieldOrder({ "X", "Y" })
299+
public static class COORD extends Structure {
300+
301+
public short X;
302+
public short Y;
303+
304+
@Override
305+
public String toString() {
306+
return String.format("COORD(%s,%s)", X, Y);
307+
}
308+
}
309+
310+
/**
311+
* SMALL_RECT structure
312+
*/
313+
@FieldOrder({ "Left", "Top", "Right", "Bottom" })
314+
public static class SMALL_RECT extends Structure {
315+
316+
public short Left;
317+
public short Top;
318+
public short Right;
319+
public short Bottom;
320+
321+
@Override
322+
public String toString() {
323+
return String.format("SMALL_RECT(%s,%s)(%s,%s)", Left, Top, Right, Bottom);
324+
}
325+
}
326+
327+
/**
328+
* CONSOLE_SCREEN_BUFFER_INFO structure
329+
*/
330+
@FieldOrder({ "dwSize", "dwCursorPosition", "wAttributes", "srWindow", "dwMaximumWindowSize" })
331+
public static class CONSOLE_SCREEN_BUFFER_INFO extends Structure {
332+
333+
public COORD dwSize;
334+
public COORD dwCursorPosition;
335+
public short wAttributes;
336+
public SMALL_RECT srWindow;
337+
public COORD dwMaximumWindowSize;
338+
339+
@Override
340+
public String toString() {
341+
return String.format("CONSOLE_SCREEN_BUFFER_INFO(%s,%s,%s,%s,%s)", dwSize, dwCursorPosition, wAttributes, srWindow, dwMaximumWindowSize);
342+
}
343+
}
344+
345+
/**
346+
* INPUT_RECORD structure
347+
*/
348+
@FieldOrder({ "EventType", "Event" })
349+
public static class INPUT_RECORD extends Structure {
350+
351+
public static final short KEY_EVENT = 0x01;
352+
public static final short MOUSE_EVENT = 0x02;
353+
public static final short WINDOW_BUFFER_SIZE_EVENT = 0x04;
354+
355+
public short EventType;
356+
public Event Event;
357+
358+
public static class Event extends Union {
359+
public KEY_EVENT_RECORD KeyEvent;
360+
public MOUSE_EVENT_RECORD MouseEvent;
361+
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
362+
}
363+
364+
@Override
365+
public void read() {
366+
super.read();
367+
switch (EventType) {
368+
case KEY_EVENT:
369+
Event.setType("KeyEvent");
370+
break;
371+
case MOUSE_EVENT:
372+
Event.setType("MouseEvent");
373+
break;
374+
case WINDOW_BUFFER_SIZE_EVENT:
375+
Event.setType("WindowBufferSizeEvent");
376+
break;
377+
}
378+
Event.read();
379+
}
380+
381+
@Override
382+
public String toString() {
383+
return String.format("INPUT_RECORD(%s)", EventType);
384+
}
385+
}
386+
387+
/**
388+
* KEY_EVENT_RECORD structure
389+
*/
390+
@FieldOrder({ "bKeyDown", "wRepeatCount", "wVirtualKeyCode", "wVirtualScanCode", "uChar", "dwControlKeyState" })
391+
public static class KEY_EVENT_RECORD extends Structure {
392+
393+
public boolean bKeyDown;
394+
public short wRepeatCount;
395+
public short wVirtualKeyCode;
396+
public short wVirtualScanCode;
397+
public char uChar;
398+
public int dwControlKeyState;
399+
400+
@Override
401+
public String toString() {
402+
return String.format("KEY_EVENT_RECORD(%s,%s,%s,%s,%s,%s)", bKeyDown, wRepeatCount, wVirtualKeyCode, wVirtualKeyCode, wVirtualScanCode, uChar, dwControlKeyState);
403+
}
404+
}
405+
406+
/**
407+
* MOUSE_EVENT_RECORD structure
408+
*/
409+
@FieldOrder({ "dwMousePosition", "dwButtonState", "dwControlKeyState", "dwEventFlags" })
410+
public static class MOUSE_EVENT_RECORD extends Structure {
411+
412+
public COORD dwMousePosition;
413+
public int dwButtonState;
414+
public int dwControlKeyState;
415+
public int dwEventFlags;
416+
417+
@Override
418+
public String toString() {
419+
return String.format("MOUSE_EVENT_RECORD(%s,%s,%s,%s)", dwMousePosition, dwButtonState, dwControlKeyState, dwEventFlags);
420+
}
421+
}
422+
423+
/**
424+
* WINDOW_BUFFER_SIZE_RECORD structure
425+
*/
426+
@FieldOrder({ "dwSize" })
427+
public static class WINDOW_BUFFER_SIZE_RECORD extends Structure {
428+
429+
public COORD dwSize;
430+
431+
@Override
432+
public String toString() {
433+
return String.format("WINDOW_BUFFER_SIZE_RECORD(%s)", dwSize);
434+
}
435+
}
252436
}

contrib/platform/test/com/sun/jna/platform/win32/Kernel32ConsoleTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.sun.jna.Pointer;
3131
import com.sun.jna.platform.win32.WinDef.HWND;
3232
import com.sun.jna.platform.win32.WinNT.HANDLE;
33+
import com.sun.jna.platform.win32.Wincon.CONSOLE_SCREEN_BUFFER_INFO;
34+
import com.sun.jna.platform.win32.Wincon.INPUT_RECORD;
3335
import com.sun.jna.ptr.IntByReference;
3436
import org.junit.Assume;
3537

@@ -142,4 +144,53 @@ public void testGetConsoleOriginalTitle() {
142144
}
143145
}
144146
}
147+
148+
@Test
149+
public void testGetConsoleScreenBufferInfo() {
150+
HANDLE hConsoleOutput = INSTANCE.GetStdHandle(Wincon.STD_OUTPUT_HANDLE);
151+
CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo = new CONSOLE_SCREEN_BUFFER_INFO();
152+
153+
if (System.console() == null) {
154+
assertFalse(INSTANCE.GetConsoleScreenBufferInfo(hConsoleOutput, lpConsoleScreenBufferInfo));
155+
} else {
156+
assertCallSucceeded("GetConsoleScreenBufferInfo", INSTANCE.GetConsoleScreenBufferInfo(hConsoleOutput, lpConsoleScreenBufferInfo));
157+
}
158+
}
159+
160+
@Test
161+
public void testReadConsoleInput() {
162+
HANDLE hConsoleInput = INSTANCE.GetStdHandle(Wincon.STD_INPUT_HANDLE);
163+
INPUT_RECORD[] lpBuffer = new INPUT_RECORD[1];
164+
IntByReference lpNumberOfEventsRead = new IntByReference();
165+
166+
if (System.console() == null) {
167+
assertFalse(INSTANCE.ReadConsoleInput(hConsoleInput, lpBuffer, lpBuffer.length, lpNumberOfEventsRead));
168+
} else {
169+
assertCallSucceeded("ReadConsoleInput", INSTANCE.ReadConsoleInput(hConsoleInput, lpBuffer, lpBuffer.length, lpNumberOfEventsRead));
170+
}
171+
}
172+
173+
@Test
174+
public void testGetNumberOfConsoleInputEvents() {
175+
HANDLE hConsoleInput = INSTANCE.GetStdHandle(Wincon.STD_INPUT_HANDLE);
176+
IntByReference lpcNumberOfEvents = new IntByReference();
177+
178+
if (System.console() == null) {
179+
assertFalse(INSTANCE.GetNumberOfConsoleInputEvents(hConsoleInput, lpcNumberOfEvents));
180+
} else {
181+
assertCallSucceeded("GetNumberOfConsoleInputEvents", INSTANCE.GetNumberOfConsoleInputEvents(hConsoleInput, lpcNumberOfEvents));
182+
}
183+
}
184+
185+
@Test
186+
public void testWriteConsole() {
187+
HANDLE hConsoleOutput = INSTANCE.GetStdHandle(Wincon.STD_OUTPUT_HANDLE);
188+
String lpBuffer = "WriteConsole";
189+
190+
if (System.console() == null) {
191+
assertFalse(INSTANCE.WriteConsole(hConsoleOutput, lpBuffer, lpBuffer.length(), null, null));
192+
} else {
193+
assertCallSucceeded("WriteConsole", INSTANCE.WriteConsole(hConsoleOutput, lpBuffer, lpBuffer.length(), null, null));
194+
}
195+
}
145196
}

0 commit comments

Comments
 (0)