Add emuname for telling apart multiple emulators
[jpcrr.git] / org / jpc / plugins / JoystickInput.java
blob0e00cb4d6377d3748f5bb7af8121dda6ffb53b6b
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009-2010 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.plugins;
32 import org.jpc.modules.Joystick;
33 import org.jpc.pluginsbase.Plugins;
34 import org.jpc.pluginsbase.Plugin;
35 import static org.jpc.Misc.errorDialog;
36 import static org.jpc.Misc.moveWindow;
37 import org.jpc.Misc;
39 import javax.swing.*;
40 import java.io.*;
41 import java.awt.event.*;
42 import java.awt.*;
44 public class JoystickInput implements ActionListener, Plugin
46 private JFrame window;
47 private JPanel panel;
48 private org.jpc.modules.Joystick joy;
49 private Plugins pluginManager;
50 private int nativeWidth, nativeHeight;
51 private JTextField[] axisInput;
52 private JLabel[] axisValue;
53 private JButton[] updateAxis;
54 private JToggleButton[] buttons;
56 public void eci_joystickinput_setwinpos(Integer x, Integer y)
58 moveWindow(window, x.intValue(), y.intValue(), nativeWidth, nativeHeight);
61 public JoystickInput(Plugins _pluginManager)
63 pluginManager = _pluginManager;
64 window = new JFrame("Joystick input" + Misc.emuname);
65 GridLayout layout = new GridLayout(0, 4);
66 panel = new JPanel(layout);
67 window.add(panel);
69 axisInput = new JTextField[4];
70 axisValue = new JLabel[4];
71 updateAxis = new JButton[4];
72 buttons = new JToggleButton[4];
74 for(int i = 0; i < 4; i++) {
75 axisValue[i] = new JLabel("<N/A> ");
76 panel.add(axisValue[i]);
77 axisInput[i] = new JTextField("10000", 10);
78 panel.add(axisInput[i]);
79 updateAxis[i] = new JButton("Update");
80 updateAxis[i].setActionCommand("A" + i);
81 updateAxis[i].setEnabled(false);
82 updateAxis[i].addActionListener(this);
83 panel.add(updateAxis[i]);
84 buttons[i] = new JToggleButton("Button");
85 buttons[i].setActionCommand("B" + i);
86 buttons[i].addActionListener(this);
87 buttons[i].setSelected(false);
88 buttons[i].setEnabled(false);
89 panel.add(buttons[i]);
92 window.pack();
93 window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
94 Dimension d = window.getSize();
95 nativeWidth = d.width;
96 nativeHeight = d.height;
97 window.setVisible(true);
100 public void resetButtons()
102 if(joy != null) {
103 for(int i = 0; i < 4; i++) {
104 axisValue[i].setText("" + joy.axisHoldTime(i, true));
105 updateAxis[i].setEnabled(true);
106 buttons[i].setEnabled(true);
107 buttons[i].setSelected(joy.buttonState(i, true));
109 } else {
110 for(int i = 0; i < 4; i++) {
111 axisValue[i].setText("<N/A>");
112 updateAxis[i].setEnabled(false);
113 buttons[i].setSelected(false);
114 buttons[i].setEnabled(false);
119 public void main()
121 //This runs entierely in UI thread.
124 public boolean systemShutdown()
126 //OK to proceed with JVM shutdown.
127 return true;
130 public void pcStarting()
132 //Not interested.
135 public void pcStopping()
137 if(pluginManager.isShuttingDown())
138 return; //Too much of deadlock risk.
140 if(!SwingUtilities.isEventDispatchThread())
141 try {
142 SwingUtilities.invokeAndWait(new Thread() { public void run() { JoystickInput.this.resetButtons(); }});
143 } catch(Exception e) {
145 else
146 resetButtons();
149 public void reconnect(org.jpc.emulator.PC pc)
151 if(pc != null) {
152 joy = (Joystick)pc.getComponent(Joystick.class);
153 } else {
154 joy = null;
156 pcStopping(); //Do the equivalent actions.
159 public void actionPerformed(ActionEvent evt)
161 if(joy == null)
162 return;
164 String command = evt.getActionCommand();
165 if(command == null)
166 return;
168 if(command.startsWith("A")) {
169 try {
170 int i = Integer.parseInt(command.substring(1));
171 joy.setAxis(i, Long.parseLong(axisInput[i].getText()));
172 } catch(Exception e) {
173 errorDialog(new IOException("Can't set joystick Axis"), "Can't send event", null, "Dismiss");
176 if(command.startsWith("B")) {
177 try {
178 int i = Integer.parseInt(command.substring(1));
179 joy.setButton(i, buttons[i].isSelected());
180 } catch(Exception e) {
181 errorDialog(new IOException("Can't set joystick Button"), "Can't send event", null, "Dismiss");
185 resetButtons();