Add Lua function to obtain keyboard key state at input edge
[jpcrr.git] / org / jpc / luaextensions / InputDevices.java
blob361f44fb84125c8cbfa705b6873649bb24db1912
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.luaextensions;
32 import mnj.lua.*;
34 import org.jpc.modules.Joystick;
35 import org.jpc.emulator.peripheral.Keyboard;
37 import org.jpc.plugins.LuaPlugin;
39 //Locking this class is used for preventing termination and when terminating.
40 public class InputDevices extends LuaPlugin.LuaResource
42 public void destroy()
46 private InputDevices(LuaPlugin plugin)
48 super(plugin);
51 public static int luaCB_keypressed(Lua l, LuaPlugin plugin)
53 if(l.type(1) != Lua.TNUMBER) {
54 l.error("Unexpected types to keypressed");
55 return 0;
57 Keyboard key = (Keyboard)plugin.getComponent(Keyboard.class);
58 if(key != null)
59 l.pushBoolean(key.getKeyExecStatus((byte)l.checkNumber(1)));
60 else
61 l.pushBoolean(false);
62 return 1;
65 public static int luaCB_keypressed_edge(Lua l, LuaPlugin plugin)
67 if(l.type(1) != Lua.TNUMBER) {
68 l.error("Unexpected types to keypressed");
69 return 0;
71 Keyboard key = (Keyboard)plugin.getComponent(Keyboard.class);
72 if(key != null)
73 l.pushBoolean(key.getKeyStatus((byte)l.checkNumber(1)));
74 else
75 l.pushBoolean(false);
76 return 1;
79 public static int luaCB_keyboard_leds(Lua l, LuaPlugin plugin)
81 Keyboard key = (Keyboard)plugin.getComponent(Keyboard.class);
82 if(key != null) {
83 int status = key.getLEDStatus();
84 if(status < 0) {
85 l.pushBoolean(false);
86 return 1;
87 } else {
88 l.pushBoolean((status & 2) != 0);
89 l.pushBoolean((status & 4) != 0);
90 l.pushBoolean((status & 1) != 0);
91 return 3;
93 } else {
94 l.pushNil();
95 return 1;
99 public static int luaCB_joystick_state(Lua l, LuaPlugin plugin)
101 Joystick joy = (Joystick)plugin.getComponent(Joystick.class);
103 if(joy != null) {
104 l.push(new Double(joy.axisHoldTime(0, false)));
105 l.push(new Double(joy.axisHoldTime(1, false)));
106 l.push(new Double(joy.axisHoldTime(2, false)));
107 l.push(new Double(joy.axisHoldTime(3, false)));
108 l.pushBoolean(joy.buttonState(0, false));
109 l.pushBoolean(joy.buttonState(1, false));
110 l.pushBoolean(joy.buttonState(2, false));
111 l.pushBoolean(joy.buttonState(3, false));
112 return 8;
113 } else {
114 l.pushNil();
115 return 1;
119 public static int luaCB_mouse_state(Lua l, LuaPlugin plugin)
121 Keyboard kbd = (Keyboard)plugin.getComponent(Keyboard.class);
122 if(kbd != null) {
123 l.push(new Double(kbd.getMouseXPendingMotion()));
124 l.push(new Double(kbd.getMouseYPendingMotion()));
125 l.push(new Double(kbd.getMouseZPendingMotion()));
126 int b = kbd.getMouseButtonStatus();
127 l.pushBoolean((b & 1) != 0);
128 l.pushBoolean((b & 2) != 0);
129 l.pushBoolean((b & 4) != 0);
130 l.pushBoolean((b & 8) != 0);
131 l.pushBoolean((b & 16) != 0);
132 return 8;
133 } else {
134 l.pushNil();
135 return 1;