11
11
import java .awt .MouseInfo ;
12
12
import java .awt .Point ;
13
13
import java .awt .Rectangle ;
14
- import java .util .Arrays ;
15
14
import java .util .NoSuchElementException ;
16
15
import java .util .concurrent .Executors ;
17
16
import java .util .concurrent .atomic .AtomicReference ;
18
- import java .util .function .Consumer ;
19
17
import java .util .logging .Level ;
20
18
21
- import com .sun .jna .platform .mac .CoreFoundation .CFArrayRef ;
22
- import com .sun .tools .attach .VirtualMachine ;
23
- import com .sun .tools .attach .VirtualMachineDescriptor ;
24
19
import 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 ;
30
21
import vavi .games .input .listener .GamepadInputEventListener .Context ;
22
+ import vavi .games .input .robot .Key ;
23
+ import vavi .games .input .robot .RococaRobot ;
31
24
import vavi .util .Debug ;
32
25
import vavi .util .event .GenericEvent ;
33
26
51
44
import static org .rococoa .carbon .CarbonCoreLibrary .kVK_Space ;
52
45
import static org .rococoa .cocoa .coregraphics .CoreGraphicsLibrary .kCGMouseButtonLeft ;
53
46
import 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 ;
57
48
58
49
59
50
/**
@@ -66,15 +57,35 @@ public class MinecraftListener extends GamepadAdapter {
66
57
67
58
private static final String bundleId = "com.mojang.Minecraft" ;
68
59
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
+
69
67
private long prevForBounds ;
70
68
71
69
@ Override
72
- public boolean match (NSRunningApplication a ) {
70
+ public boolean match (AppInfo a ) {
73
71
try {
74
- if (a .processIdentifier (). intValue () == getMcLauncherPid ( )) {
72
+ if (a .pid () == getPidByMainClassName ( mcLaunchers )) {
75
73
if (System .currentTimeMillis () - prevForBounds > 20 * 1000 ) {
76
74
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
+ });
78
89
}
79
90
return true ;
80
91
}
@@ -84,48 +95,6 @@ public boolean match(NSRunningApplication a) {
84
95
return false ;
85
96
}
86
97
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
-
129
98
// ----
130
99
131
100
private final RococaRobot robot = new RococaRobot ();
@@ -146,7 +115,6 @@ private static int getMcLauncherPid() {
146
115
private final AtomicReference <Rectangle > bounds = new AtomicReference <>();
147
116
148
117
/**
149
- * @before {@link #retrieveBounds}
150
118
* @after {@link #point}
151
119
*/
152
120
private void normalizePoint () {
@@ -164,32 +132,6 @@ private void normalizePoint() {
164
132
}
165
133
}
166
134
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
-
193
135
class RobotKey extends Key {
194
136
RobotKey (int code ) {
195
137
super (code , robot ::keyPress , robot ::keyRelease );
@@ -198,38 +140,7 @@ class RobotKey extends Key {
198
140
199
141
class RobotKey2 extends Key {
200
142
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 );
233
144
}
234
145
}
235
146
@@ -580,9 +491,9 @@ public void onButton14(Event e) {
580
491
@ Override
581
492
public void after () {
582
493
if (moved ) {
583
- robot .mouseMove2 (dx , dy );
494
+ robot .mouseMoveOnlyAccel (dx , dy );
584
495
normalizePoint ();
585
- robot .mouseMove0 (point .x , point .y );
496
+ robot .mouseMoveOnlyLocation (point .x , point .y );
586
497
}
587
498
}
588
499
}
0 commit comments