XInput.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.util.*;
16:
17:
18: public final class XInput extends Thread {
19:
20:
21:
22:
23:
24: protected Gamepad[] gamepads;
25: protected ArrayList<GamepadListener> gamepadListeners;
26: protected boolean polling;
27:
28:
29:
30: public XInput () {
31:
32: gamepads = new Gamepad[4];
33:
34: for (int index = 0; index < 4; index++) {
35: gamepads[index] = new Gamepad (index);
36: }
37:
38: gamepadListeners = new ArrayList<GamepadListener> ();
39:
40: polling = true;
41: this.start ();
42: }
43:
44:
45:
46: public void end () {
47:
48: if (polling) {
49: polling = false;
50: if (this.isAlive ()) {
51: this.interrupt ();
52: try {
53: this.join ();
54: } catch (InterruptedException ie) {
55: }
56: }
57: }
58:
59: for (int index = 0; index < 4; index++) {
60: gamepads[index].close ();
61: }
62: }
63:
64:
65:
66:
67: @Override public void run () {
68: while (polling) {
69: boolean available = false;
70: int counter = 0;
71: for (int index = 0; index < 4; index++) {
72: Gamepad gamepad = gamepads[index];
73: if (counter == 0 || gamepad.isAvailable ()) {
74: available = available || gamepad.isAvailable ();
75: if (gamepad.getState () &&
76: gamepadListeners.size () != 0) {
77: if (gamepad.isConnected ()) {
78: for (GamepadListener listener : gamepadListeners) {
79: listener.connected (gamepad);
80: }
81: } else if (gamepad.isDisconnected ()) {
82: for (GamepadListener listener : gamepadListeners) {
83: listener.disconnected (gamepad);
84: }
85: }
86: if (gamepad.isAvailable ()) {
87: int buttonMasks = gamepad.getPressedButtonMasks ();
88: if (buttonMasks != 0) {
89: for (GamepadListener listener : gamepadListeners) {
90: listener.buttonPressed (gamepad, buttonMasks);
91: }
92: }
93: buttonMasks = gamepad.getReleasedButtonMasks ();
94: if (buttonMasks != 0) {
95: for (GamepadListener listener : gamepadListeners) {
96: listener.buttonReleased (gamepad, buttonMasks);
97: }
98: }
99: if (gamepad.isLeftStickMovedX ()) {
100: for (GamepadListener listener : gamepadListeners) {
101: listener.leftStickMovedX (gamepad);
102: }
103: }
104: if (gamepad.isLeftStickMovedY ()) {
105: for (GamepadListener listener : gamepadListeners) {
106: listener.leftStickMovedY (gamepad);
107: }
108: }
109: if (gamepad.isLeftTriggerMoved ()) {
110: for (GamepadListener listener : gamepadListeners) {
111: listener.leftTriggerMoved (gamepad);
112: }
113: }
114: if (gamepad.isRightStickMovedX ()) {
115: for (GamepadListener listener : gamepadListeners) {
116: listener.rightStickMovedX (gamepad);
117: }
118: }
119: if (gamepad.isRightStickMovedY ()) {
120: for (GamepadListener listener : gamepadListeners) {
121: listener.rightStickMovedY (gamepad);
122: }
123: }
124: if (gamepad.isRightTriggerMoved ()) {
125: for (GamepadListener listener : gamepadListeners) {
126: listener.rightTriggerMoved (gamepad);
127: }
128: }
129: }
130: }
131: }
132: }
133: try {
134: if (available) {
135: counter = (counter + 1) & 31;
136: Thread.sleep (10L);
137: } else {
138: counter = 0;
139: Thread.sleep (320L);
140: }
141: } catch (InterruptedException ie) {
142: return;
143: }
144: }
145: }
146:
147:
148:
149:
150:
151:
152: public static final int UP_BIT = 0;
153: public static final int DOWN_BIT = 1;
154: public static final int LEFT_BIT = 2;
155: public static final int RIGHT_BIT = 3;
156: public static final int START_BIT = 4;
157: public static final int BACK_BIT = 5;
158: public static final int LSTICK_BIT = 6;
159: public static final int RSTICK_BIT = 7;
160: public static final int LB_BIT = 8;
161: public static final int RB_BIT = 9;
162: public static final int A_BIT = 12;
163: public static final int B_BIT = 13;
164: public static final int X_BIT = 14;
165: public static final int Y_BIT = 15;
166: public static final int LSUP_BIT = 16;
167: public static final int LSDOWN_BIT = 17;
168: public static final int LSLEFT_BIT = 18;
169: public static final int LSRIGHT_BIT = 19;
170: public static final int RSUP_BIT = 20;
171: public static final int RSDOWN_BIT = 21;
172: public static final int RSLEFT_BIT = 22;
173: public static final int RSRIGHT_BIT = 23;
174: public static final int LTRIGGER_BIT = 24;
175: public static final int RTRIGGER_BIT = 25;
176: public static final int BUTTONS = 26;
177:
178: public static final int UP_MASK = 1 << UP_BIT;
179: public static final int DOWN_MASK = 1 << DOWN_BIT;
180: public static final int LEFT_MASK = 1 << LEFT_BIT;
181: public static final int RIGHT_MASK = 1 << RIGHT_BIT;
182: public static final int START_MASK = 1 << START_BIT;
183: public static final int BACK_MASK = 1 << BACK_BIT;
184: public static final int LSTICK_MASK = 1 << LSTICK_BIT;
185: public static final int RSTICK_MASK = 1 << RSTICK_BIT;
186: public static final int LB_MASK = 1 << LB_BIT;
187: public static final int RB_MASK = 1 << RB_BIT;
188: public static final int A_MASK = 1 << A_BIT;
189: public static final int B_MASK = 1 << B_BIT;
190: public static final int X_MASK = 1 << X_BIT;
191: public static final int Y_MASK = 1 << Y_BIT;
192: public static final int LSUP_MASK = 1 << LSUP_BIT;
193: public static final int LSDOWN_MASK = 1 << LSDOWN_BIT;
194: public static final int LSLEFT_MASK = 1 << LSLEFT_BIT;
195: public static final int LSRIGHT_MASK = 1 << LSRIGHT_BIT;
196: public static final int RSUP_MASK = 1 << RSUP_BIT;
197: public static final int RSDOWN_MASK = 1 << RSDOWN_BIT;
198: public static final int RSLEFT_MASK = 1 << RSLEFT_BIT;
199: public static final int RSRIGHT_MASK = 1 << RSRIGHT_BIT;
200: public static final int LTRIGGER_MASK = 1 << LTRIGGER_BIT;
201: public static final int RTRIGGER_MASK = 1 << RTRIGGER_BIT;
202:
203: public static final String BIT_TO_TEXT[] = new String[] {
204: "UP", "DOWN", "LEFT", "RIGHT", "START", "BACK", "LSTICK", "RSTICK",
205: "LB", "RB", "(10)", "(11)", "A", "B", "X", "Y",
206: "LSUP", "LSDOWN", "LSLEFT", "LSRIGHT", "RSUP", "RSDOWN", "RSLEFT", "RSRIGHT",
207: "LTRIGGER", "RTRIGGER",
208: };
209:
210:
211:
212:
213:
214: public int getButtonMasks (int index) {
215: return gamepads[index].getButtonMasks ();
216: }
217:
218:
219:
220:
221:
222: public int getLeftStickX (int index) {
223: return gamepads[index].getLeftStickX ();
224: }
225:
226:
227:
228:
229:
230: public int getLeftStickY (int index) {
231: return gamepads[index].getLeftStickY ();
232: }
233:
234:
235:
236:
237:
238: public int getLeftTrigger (int index) {
239: return gamepads[index].getLeftTrigger ();
240: }
241:
242:
243:
244:
245:
246: public int getRightStickX (int index) {
247: return gamepads[index].getRightStickX ();
248: }
249:
250:
251:
252:
253:
254: public int getRightStickY (int index) {
255: return gamepads[index].getRightStickY ();
256: }
257:
258:
259:
260:
261:
262: public int getRightTrigger (int index) {
263: return gamepads[index].getRightTrigger ();
264: }
265:
266:
267:
268:
269:
270: public boolean isAvailable (int index) {
271: return gamepads[index].isAvailable ();
272: }
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316: public void setThresholdOfLeftStick (int index, int thresholdOfLeftStick) {
317: gamepads[index].setThresholdOfLeftStick (thresholdOfLeftStick);
318: }
319:
320:
321:
322:
323:
324: public void setThresholdOfRightStick (int index, int thresholdOfRightStick) {
325: gamepads[index].setThresholdOfRightStick (thresholdOfRightStick);
326: }
327:
328:
329:
330:
331:
332: public void setThresholdOfLeftTrigger (int index, int thresholdOfLeftTrigger) {
333: gamepads[index].setThresholdOfLeftTrigger (thresholdOfLeftTrigger);
334: }
335:
336:
337:
338:
339:
340: public void setThresholdOfRightTrigger (int index, int thresholdOfRightTrigger) {
341: gamepads[index].setThresholdOfRightTrigger (thresholdOfRightTrigger);
342: }
343:
344:
345:
346:
347:
348:
349: public interface GamepadListener {
350: public void connected (Gamepad gamepad);
351: public void disconnected (Gamepad gamepad);
352: public void buttonPressed (Gamepad gamepad, int buttonMasks);
353: public void buttonReleased (Gamepad gamepad, int buttonMasks);
354: public void leftStickMovedX (Gamepad gamepad);
355: public void leftStickMovedY (Gamepad gamepad);
356: public void leftTriggerMoved (Gamepad gamepad);
357: public void rightStickMovedX (Gamepad gamepad);
358: public void rightStickMovedY (Gamepad gamepad);
359: public void rightTriggerMoved (Gamepad gamepad);
360: }
361:
362:
363: public static class GamepadAdapter implements GamepadListener {
364: @Override public void connected (Gamepad gamepad) {
365: }
366: @Override public void disconnected (Gamepad gamepad) {
367: }
368: @Override public void buttonPressed (Gamepad gamepad, int buttonMasks) {
369: }
370: @Override public void buttonReleased (Gamepad gamepad, int buttonMasks) {
371: }
372: @Override public void leftStickMovedX (Gamepad gamepad) {
373: }
374: @Override public void leftStickMovedY (Gamepad gamepad) {
375: }
376: @Override public void leftTriggerMoved (Gamepad gamepad) {
377: }
378: @Override public void rightStickMovedX (Gamepad gamepad) {
379: }
380: @Override public void rightStickMovedY (Gamepad gamepad) {
381: }
382: @Override public void rightTriggerMoved (Gamepad gamepad) {
383: }
384: }
385:
386:
387:
388:
389: public void addGamepadListener (GamepadListener listener) {
390: if (listener != null && !gamepadListeners.contains (listener)) {
391: gamepadListeners.add (listener);
392: }
393: }
394:
395:
396:
397:
398: public void removeGamepadListener (GamepadListener listener) {
399: gamepadListeners.remove (listener);
400: }
401:
402:
403:
404: public void removeGamepadListeners () {
405: gamepadListeners.clear ();
406: }
407:
408:
409:
410: public GamepadListener[] getGamepadListeners () {
411: return gamepadListeners.toArray (new GamepadListener[gamepadListeners.size ()]);
412: }
413:
414:
415:
416:
417: public static class Gamepad implements AutoCloseable {
418:
419: private long xiwork;
420:
421:
422:
423:
424: public Gamepad (int index) {
425: open (index);
426: }
427:
428:
429:
430: @Override public native void close ();
431:
432:
433:
434:
435: public native int getButtonMasks ();
436:
437:
438:
439:
440: public native int getIndex ();
441:
442:
443:
444:
445: public native int getLeftStickX ();
446:
447:
448:
449:
450: public native int getLeftStickY ();
451:
452:
453:
454:
455: public native int getLeftTrigger ();
456:
457:
458:
459:
460: public native int getPressedButtonMasks ();
461:
462:
463:
464:
465: public native int getReleasedButtonMasks ();
466:
467:
468:
469:
470: public native int getRightStickX ();
471:
472:
473:
474:
475: public native int getRightStickY ();
476:
477:
478:
479:
480: public native int getRightTrigger ();
481:
482:
483:
484:
485: public native boolean getState ();
486:
487:
488:
489:
490: public native boolean isAvailable ();
491:
492:
493:
494:
495: public native boolean isConnected ();
496:
497:
498:
499:
500: public native boolean isDisconnected ();
501:
502:
503:
504:
505: public native boolean isLeftStickMovedX ();
506:
507:
508:
509:
510: public native boolean isLeftStickMovedY ();
511:
512:
513:
514:
515: public native boolean isLeftTriggerMoved ();
516:
517:
518:
519:
520: public native boolean isRightStickMovedX ();
521:
522:
523:
524:
525: public native boolean isRightStickMovedY ();
526:
527:
528:
529:
530: public native boolean isRightTriggerMoved ();
531:
532:
533:
534:
535: private native void open (int index);
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567: public native void setThresholdOfLeftStick (int thresholdOfLeftStick);
568:
569:
570:
571:
572:
573: public native void setThresholdOfRightStick (int thresholdOfRightStick);
574:
575:
576:
577:
578:
579: public native void setThresholdOfLeftTrigger (int thresholdOfLeftTrigger);
580:
581:
582:
583:
584:
585: public native void setThresholdOfRightTrigger (int thresholdOfRightTrigger);
586:
587: }
588:
589:
590:
591: }