1111import java .awt .MouseInfo ;
1212import java .awt .Point ;
1313import java .awt .Rectangle ;
14- import java .util .Arrays ;
1514import java .util .NoSuchElementException ;
1615import java .util .concurrent .Executors ;
1716import java .util .concurrent .atomic .AtomicReference ;
18- import java .util .function .Consumer ;
1917import java .util .logging .Level ;
2018
21- import com .sun .jna .platform .mac .CoreFoundation .CFArrayRef ;
22- import com .sun .tools .attach .VirtualMachine ;
23- import com .sun .tools .attach .VirtualMachineDescriptor ;
2419import net .java .games .input .Event ;
25- import org .rococoa .Rococoa ;
26- import org .rococoa .cocoa .appkit .NSRunningApplication ;
27- import org .rococoa .cocoa .coregraphics .RococaRobot ;
28- import org .rococoa .cocoa .foundation .NSDictionary ;
29- import org .rococoa .cocoa .foundation .NSString ;
20+ import vavi .games .input .listener .GamepadInputEventListener .AppInfo ;
3021import vavi .games .input .listener .GamepadInputEventListener .Context ;
22+ import vavi .games .input .robot .Key ;
23+ import vavi .games .input .robot .RococaRobot ;
3124import vavi .util .Debug ;
3225import vavi .util .event .GenericEvent ;
3326
5144import static org .rococoa .carbon .CarbonCoreLibrary .kVK_Space ;
5245import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .kCGMouseButtonLeft ;
5346import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .kCGMouseButtonRight ;
54- import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .kCGNullWindowID ;
55- import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .kCGWindowListOptionOnScreenOnly ;
56- import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .library ;
47+ import static vavi .games .input .helper .JavaVMAppInfo .getPidByMainClassName ;
5748
5849
5950/**
@@ -66,15 +57,35 @@ public class MinecraftListener extends GamepadAdapter {
6657
6758 private static final String bundleId = "com.mojang.Minecraft" ;
6859
60+ /** minecraft launchers descriptor#dusplayName */
61+ private static final String [] mcLaunchers = {
62+ "net.minecraft.client.main.Main" , // mc launcher -> original
63+ "net.fabricmc.loader.impl.launch.knot.KnotClient" , // mc launcher -> fabric
64+ "org.prismlauncher.EntryPoint" // prism launcher
65+ };
66+
6967 private long prevForBounds ;
7068
7169 @ Override
72- public boolean match (NSRunningApplication a ) {
70+ public boolean match (AppInfo a ) {
7371 try {
74- if (a .processIdentifier (). intValue () == getMcLauncherPid ( )) {
72+ if (a .pid () == getPidByMainClassName ( mcLaunchers )) {
7573 if (System .currentTimeMillis () - prevForBounds > 20 * 1000 ) {
7674 prevForBounds = System .currentTimeMillis ();
77- Executors .newSingleThreadScheduledExecutor ().submit (() -> retrieveBounds (a ));
75+ Executors .newSingleThreadScheduledExecutor ().submit (() -> {
76+ Rectangle b = a .bounds ();
77+ if (b != null ) {
78+ Rectangle r = bounds .get ();
79+ if (r == null ) {
80+ bounds .set (b );
81+ } else {
82+ r .setBounds (b );
83+ }
84+ //Debug.println("minecraft window found: " + r);
85+ // } else {
86+ //Debug.println("no minecraft window found.");
87+ }
88+ });
7889 }
7990 return true ;
8091 }
@@ -84,48 +95,6 @@ public boolean match(NSRunningApplication a) {
8495 return false ;
8596 }
8697
87- /** @after {@link #bounds} */
88- private void retrieveBounds (NSRunningApplication a ) {
89- CFArrayRef array = library .CGWindowListCopyWindowInfo (kCGWindowListOptionOnScreenOnly , kCGNullWindowID );
90- //Debug.println("windows: " + array.getCount());
91- for (int i = 0 ; i < array .getCount (); i ++) {
92- NSDictionary dic = Rococoa .toNSDictionary (array .getValueAtIndex (i ));
93- if (Integer .parseInt (dic .get (NSString .stringWithString ("kCGWindowOwnerPID" )).toString ()) == a .processIdentifier ().intValue ()) {
94- NSDictionary rect = Rococoa .cast (dic .get (NSString .stringWithString ("kCGWindowBounds" )), NSDictionary .class );
95- int x = Integer .parseInt (rect .get (NSString .stringWithString ("X" )).toString ());
96- int y = Integer .parseInt (rect .get (NSString .stringWithString ("Y" )).toString ());
97- int width = Integer .parseInt (rect .get (NSString .stringWithString ("Width" )).toString ());
98- int height = Integer .parseInt (rect .get (NSString .stringWithString ("Height" )).toString ());
99- Rectangle r = bounds .get ();
100- if (r == null ) {
101- bounds .set (new Rectangle (x , y , width , height ));
102- } else {
103- r .setBounds (x , y , width , height );
104- }
105- //Debug.println("minecraft window found: " + r);
106- return ;
107- }
108- }
109- //Debug.println("no minecraft window found.");
110- }
111-
112- /** minecraft launchers descriptor#dusplayName */
113- private static final String [] mcLaunchers = {
114- "net.minecraft.client.main.Main" , // mc launcher -> original
115- "net.fabricmc.loader.impl.launch.knot.KnotClient" , // mc launcher -> fabric
116- "org.prismlauncher.EntryPoint" // prism launcher
117- };
118-
119- /** minecraft launchers pid which displayName contains one of those */
120- private static int getMcLauncherPid () {
121- for (VirtualMachineDescriptor descriptor : VirtualMachine .list ()) {
122- if (Arrays .asList (mcLaunchers ).contains (descriptor .displayName ())) {
123- return Integer .decode (descriptor .id ());
124- }
125- }
126- throw new NoSuchElementException ("minecraft might not run" );
127- }
128-
12998 // ----
13099
131100 private final RococaRobot robot = new RococaRobot ();
@@ -146,7 +115,6 @@ private static int getMcLauncherPid() {
146115 private final AtomicReference <Rectangle > bounds = new AtomicReference <>();
147116
148117 /**
149- * @before {@link #retrieveBounds}
150118 * @after {@link #point}
151119 */
152120 private void normalizePoint () {
@@ -164,32 +132,6 @@ private void normalizePoint() {
164132 }
165133 }
166134
167- static class Key {
168- final int code ;
169- final Consumer <Integer > pressAction ;
170- final Consumer <Integer > releaseAction ;
171- boolean pressed ;
172-
173- public Key (int code , Consumer <Integer > pressAction , Consumer <Integer > releaseAction ) {
174- this .code = code ;
175- this .pressAction = pressAction ;
176- this .releaseAction = releaseAction ;
177- }
178-
179- void press () {
180- if (!pressed ) {
181- pressAction .accept (code );
182- pressed = true ;
183- }
184- }
185- void release () {
186- if (pressed ) {
187- releaseAction .accept (code );
188- pressed = false ;
189- }
190- }
191- }
192-
193135 class RobotKey extends Key {
194136 RobotKey (int code ) {
195137 super (code , robot ::keyPress , robot ::keyRelease );
@@ -198,38 +140,7 @@ class RobotKey extends Key {
198140
199141 class RobotKey2 extends Key {
200142 RobotKey2 (int code ) {
201- super (code , robot ::keyPress2 , robot ::keyRelease2 );
202- }
203- }
204-
205- @ FunctionalInterface
206- interface TriConsumer <T , U , V > {
207- void accept (T t , U u , V v );
208- }
209-
210- static class KeyWithCoordinate {
211- final int code ;
212- final TriConsumer <Integer , Integer , Integer > pressActionWithCoordinat ;
213- final TriConsumer <Integer , Integer , Integer > releaseActionWithCoordinat ;
214- boolean pressed ;
215-
216- public KeyWithCoordinate (int code , TriConsumer <Integer , Integer , Integer > pressActionWithCoordinate , TriConsumer <Integer , Integer , Integer > releaseActionWithCoordinate ) {
217- this .code = code ;
218- this .pressActionWithCoordinat = pressActionWithCoordinate ;
219- this .releaseActionWithCoordinat = releaseActionWithCoordinate ;
220- }
221-
222- void press (int x , int y ) {
223- if (!pressed ) {
224- pressActionWithCoordinat .accept (code , x , y );
225- pressed = true ;
226- }
227- }
228- void release (int x , int y ) {
229- if (pressed ) {
230- releaseActionWithCoordinat .accept (code , x , y );
231- pressed = false ;
232- }
143+ super (code , robot ::keyPressRaw , robot ::keyReleaseRaw );
233144 }
234145 }
235146
@@ -580,9 +491,9 @@ public void onButton14(Event e) {
580491 @ Override
581492 public void after () {
582493 if (moved ) {
583- robot .mouseMove2 (dx , dy );
494+ robot .mouseMoveOnlyAccel (dx , dy );
584495 normalizePoint ();
585- robot .mouseMove0 (point .x , point .y );
496+ robot .mouseMoveOnlyLocation (point .x , point .y );
586497 }
587498 }
588499}
0 commit comments