Split PCMonitor into monitor window and monitor panel code
[jpcrr.git] / org / jpc / plugins / JoystickInput.java
blob5d31854e0d86e63588a491c500fcd67cff7ce5a4
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;
38 import javax.swing.*;
39 import java.io.*;
40 import java.awt.event.*;
41 import java.awt.*;
43 public class JoystickInput implements ActionListener, Plugin
45 private JFrame window;
46 private JPanel panel;
47 private org.jpc.modules.Joystick joy;
48 private Plugins pluginManager;
49 private int nativeWidth, nativeHeight;
50 private JTextField[] axisInput;
51 private JLabel[] axisValue;
52 private JButton[] updateAxis;
53 private JToggleButton[] buttons;
55 public void eci_joystickinput_setwinpos(Integer x, Integer y)
57 moveWindow(window, x.intValue(), y.intValue(), nativeWidth, nativeHeight);
60 public JoystickInput(Plugins _pluginManager)
62 pluginManager = _pluginManager;
63 window = new JFrame("Joystick input");
64 GridLayout layout = new GridLayout(0, 4);
65 panel = new JPanel(layout);
66 window.add(panel);
68 axisInput = new JTextField[4];
69 axisValue = new JLabel[4];
70 updateAxis = new JButton[4];
71 buttons = new JToggleButton[4];
73 for(int i = 0; i < 4; i++) {
74 axisValue[i] = new JLabel("<N/A> ");
75 panel.add(axisValue[i]);
76 axisInput[i] = new JTextField("10000", 10);
77 panel.add(axisInput[i]);
78 updateAxis[i] = new JButton("Update");
79 updateAxis[i].setActionCommand("A" + i);
80 updateAxis[i].setEnabled(false);
81 updateAxis[i].addActionListener(this);
82 panel.add(updateAxis[i]);
83 buttons[i] = new JToggleButton("Button");
84 buttons[i].setActionCommand("B" + i);
85 buttons[i].addActionListener(this);
86 buttons[i].setSelected(false);
87 buttons[i].setEnabled(false);
88 panel.add(buttons[i]);
91 window.pack();
92 window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
93 Dimension d = window.getSize();
94 nativeWidth = d.width;
95 nativeHeight = d.height;
96 window.setVisible(true);
99 public void resetButtons()
101 if(joy != null) {
102 for(int i = 0; i < 4; i++) {
103 axisValue[i].setText("" + joy.axisHoldTime(i, true));
104 updateAxis[i].setEnabled(true);
105 buttons[i].setEnabled(true);
106 buttons[i].setSelected(joy.buttonState(i, true));
108 } else {
109 for(int i = 0; i < 4; i++) {
110 axisValue[i].setText("<N/A>");
111 updateAxis[i].setEnabled(false);
112 buttons[i].setSelected(false);
113 buttons[i].setEnabled(false);
118 public void main()
120 //This runs entierely in UI thread.
123 public boolean systemShutdown()
125 //OK to proceed with JVM shutdown.
126 return true;
129 public void pcStarting()
131 //Not interested.
134 public void pcStopping()
136 if(pluginManager.isShuttingDown())
137 return; //Too much of deadlock risk.
139 if(!SwingUtilities.isEventDispatchThread())
140 try {
141 SwingUtilities.invokeAndWait(new Thread() { public void run() { JoystickInput.this.resetButtons(); }});
142 } catch(Exception e) {
144 else
145 resetButtons();
148 public void reconnect(org.jpc.emulator.PC pc)
150 if(pc != null) {
151 joy = (Joystick)pc.getComponent(Joystick.class);
152 } else {
153 joy = null;
155 pcStopping(); //Do the equivalent actions.
158 public void actionPerformed(ActionEvent evt)
160 if(joy == null)
161 return;
163 String command = evt.getActionCommand();
164 if(command == null)
165 return;
167 if(command.startsWith("A")) {
168 try {
169 int i = Integer.parseInt(command.substring(1));
170 joy.setAxis(i, Long.parseLong(axisInput[i].getText()));
171 } catch(Exception e) {
172 errorDialog(new IOException("Can't set joystick Axis"), "Can't send event", null, "Dismiss");
175 if(command.startsWith("B")) {
176 try {
177 int i = Integer.parseInt(command.substring(1));
178 joy.setButton(i, buttons[i].isSelected());
179 } catch(Exception e) {
180 errorDialog(new IOException("Can't set joystick Button"), "Can't send event", null, "Dismiss");
184 resetButtons();