2 JPC-RR: A x86 PC Hardware Emulator
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 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
;
41 import java
.awt
.event
.*;
44 public class JoystickInput
implements ActionListener
, Plugin
46 private JFrame window
;
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");
65 GridLayout layout
= new GridLayout(0, 4);
66 panel
= new JPanel(layout
);
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
]);
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()
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));
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);
121 //This runs entierely in UI thread.
124 public boolean systemShutdown()
126 //OK to proceed with JVM shutdown.
130 public void pcStarting()
135 public void pcStopping()
137 if(pluginManager
.isShuttingDown())
138 return; //Too much of deadlock risk.
140 if(!SwingUtilities
.isEventDispatchThread())
142 SwingUtilities
.invokeAndWait(new Thread() { public void run() { JoystickInput
.this.resetButtons(); }});
143 } catch(Exception e
) {
149 public void reconnect(org
.jpc
.emulator
.PC pc
)
152 joy
= (Joystick
)pc
.getComponent(Joystick
.class);
156 pcStopping(); //Do the equivalent actions.
159 public void actionPerformed(ActionEvent evt
)
164 String command
= evt
.getActionCommand();
168 if(command
.startsWith("A")) {
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")) {
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");