Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add definitions for Wincon.GetConsoleScreenBufferInfo() #1190

Closed
wants to merge 19 commits into from
Closed

Conversation

rednoah
Copy link
Contributor

@rednoah rednoah commented May 10, 2020

Add the missing definitions for the GetConsoleScreenBufferInfo function of the Windows Console API:
https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo

Copy link
Contributor

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments inline. Can you add a test case to Kernel32ConsoleTest exercising this method?


@Override
public String toString() {
return String.format("COORD[%s,%s]", X, Y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X and Y are primitives, so should the %s be %d here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote this, I thought surely Java was more efficient knowing it had an integer value here. In fact, the way you have it simply invokes Short.toString(X) while the %d version goes through a bunch of conversions to BigInteger, extracting a long from that, and finally calling Long.toString(), although I don't know if any of those steps are eventually optimized out...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal rule is to avoid %d because you will unexpectedly get different results depending on Locale.getDefault().

e.g.

Locale.setDefault(new Locale("ar"))
def s = String.format("%d,%d,%d", 1, 2, 3)
System.out.println(s)
// prints "١,٢,٣" (and not "1,2,3" as one would expect)

Copy link
Contributor Author

@rednoah rednoah May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't ask me how I know this. Those are many ours of my life I won't get back. ٣ doesn't match \d and can't be parsed as number, and Windows CMD won't print it because it's not ASCII either, and the issue will only affect users in certain with uncommon rare locales so you won't ever be able to reproduce the issue with a debugger on a typical developer machine that's set on en_US.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable!


@Override
public String toString() {
return String.format("SMALL_RECT[%s,%s,%s,%s]", Left, Top, Right, Bottom);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here on %d vs %s. Also just my opinion, but while (X,Y) is standard for coordinates, I'm not sure there's a generally accepted standard order of values for a "rectangle". I've seen (left,top,width,height) for example, so this would be more clear with L=%d,T=%d,R=%d,B=%d or possibly combining left/top and right/bottom as a coordinate pair (%d,%d)(%d,%d).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added toString() for the sake of completeness. I don't actually use them in my code. I'm happy to make any changes you see fit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just offering suggestions!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. I switched to () because the other classes use that as well.

@rednoah
Copy link
Contributor Author

rednoah commented May 11, 2020

Is it ok to just have a dummy test in Kernel32ConsoleTest that just checks if the method is called and fails?

Because we'll need a CMD window (i.e. System.console() != null) for this method to return something meaningful (e.g. won't work when running via Eclipse).

@rednoah
Copy link
Contributor Author

rednoah commented May 11, 2020

I've added a @Test @Ignore inline with another test that is disabled because it only works when running with a CMD window.

Copy link
Member

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you looks sane to me. I left one inline comment about a potential optimisation.

Before merging, please add an entry to the features section of the CHANGES.md file. Please follow the examples set in the existing entries. Thank you.


public COORD dwSize;
public COORD dwCursorPosition;
public WORD wAttributes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COORD and SMALL_RECT both map to java shorts. It would be an option to also map wAttributes to java short. If I read the documentation correctly it is a set of shorts ORed together, so the sign is not relevant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought similar when I looked at it, but decided the plain structure interpretation (dwSize.X and dwSize.Y) would be preferred over an int with bitshifting/bitmasking getters to access the underlying values. About the only thing I might change in this mapping is that wAttributes as a short would more directly map to the WINAPI enum values it represents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er... that's what you said. I misread it the first time.

@rednoah
Copy link
Contributor Author

rednoah commented May 13, 2020

I'm adding bindings for a few additional Windows Console API functions. Please don't merge anything else, so we can all do it in one pull later on.

* ReadConsoleInput
* GetNumberOfConsoleInputEvents
* WriteConsole
@rednoah
Copy link
Contributor Author

rednoah commented May 13, 2020

Done. All that just so we can capture RESIZE events and print Emoji. 👯

…b/platform/src/com/sun/jna/platform/win32/WinDef.java:1852: 'case' child has incorrect indentation level 12, expected level should be 16. [Indentation]
Copy link
Member

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only had a quick glance, but I noticed, that WORD could be mapped to a java short, DWORD can be mapped to a int and BOOL could be mapped to boolean. DWORD and WORD are only interesting IMHO when the unsigned nature is important and trumps the easier handling of integer in java. The corresponding ByReference types exist and I would use these.

@rednoah
Copy link
Contributor Author

rednoah commented May 13, 2020

I saw lots of code in the platform package to use those WinDef classes. It does help with making the JNA definition look exactly the same as the Windows API references.

@matthiasblaesing
Copy link
Member

Yes, but it also makes it create more garbadge (more wrapper classes) and harder to use as you need to wrap each number. This is also not consistent, the COORD and SMALL_RECT structs are both mapped to java shorts.

As said, there are reasons to prefer the wrappers, but I don't see many. For an enum like value like EventType for example, that is created from constants or ORed from constants you don't gain anything.

@rednoah
Copy link
Contributor Author

rednoah commented May 14, 2020

Reasonable enough. I'll switch to native types and run a few tests.

@rednoah
Copy link
Contributor Author

rednoah commented May 15, 2020

/home/travis/build/java-native-access/jna/build.xml:402: javac doesn't support the "nativeheaderdir" attribute seems to be unrelated to my changes.

@matthiasblaesing
Copy link
Member

Please rebase this PR onto current master, @dbwiddis fixed a stability issue, that was caused by wrong assumptions about the apache mirror system.

matthiasblaesing and others added 11 commits May 16, 2020 14:14
Fix statvfs filesystem - it is unstable in docker environments
Remove invalid GetTcpStatistics test
As of 2020-05-13, there are two versions of Apache Ant 1.9.x binaries available for download.  The regexp parsing incorrectly resolves to a string containing both versions, separated by a newline character, and the download fails.   This change limits the grep to only the last line, so the latest 1.9.x version will be downloaded.
Only fetch the latest ant version in Travis script
Add getTokenPrimaryGroup to Advapi32Util
@rednoah
Copy link
Contributor Author

rednoah commented May 16, 2020

Mmhhh. Now these changes appear in my pull request as well...

@dbwiddis
Copy link
Contributor

dbwiddis commented May 16, 2020

That's an annoying "feature" of github. The steps here should fix it (basically: add a dummy file, commit, push, rebase to previous commit before you added the file, force push).

@rednoah
Copy link
Contributor Author

rednoah commented May 16, 2020

Didn't work. Nevermind. I'll just fork again and apply a patch and create a new clean pull request.

@rednoah rednoah closed this May 16, 2020
@rednoah
Copy link
Contributor Author

rednoah commented May 16, 2020

👉 #1194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants