2 * psychlops_io_keyboard.cpp
3 * Psychlops Standard Library (Universal)
5 * Last Modified 2006/01/05 by Kenchi HOSOKAWA
6 * (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
10 #include "psychlops_io_hid.h"
11 #include "../ApplicationInterfaces/psychlops_code_exception.h"
12 #include "../graphic/psychlops_g_shape.h"
13 #include "../graphic/psychlops_g_canvas.h"
14 #include "../math/psychlops_math.h"
16 #define PSYCHLOPS_HID_API_PLATFORM
17 #include "../../platform/psychlops_platform_selector.h"
22 //////// Class HumanInterfaceDevice ////////
24 int HumanInterfaceDevice::pushRepeatFrames = 20, HumanInterfaceDevice::pushRepeatInterval = 3;
26 void HumanInterfaceDevice::initialize() {
27 APIHIDProperties::initialize();
28 //Keyboard::initialize();
32 void HumanInterfaceDevice::finalize() {
34 APIHIDProperties::finalize();
36 bool HumanInterfaceDevice::get(Keyboard::Key code, Keyboard::KeyState state) {
37 return Keyboard::get(code,state);
39 bool HumanInterfaceDevice::get(Mouse::Button code, Mouse::ButtonState state) {
40 return Mouse::get(code,state);
43 void HumanInterfaceDevice::wait(Keyboard::Key code, Keyboard::KeyState state) {
44 Keyboard::wait(code,state);
47 void HumanInterfaceDevice::refresh() {
52 void HumanInterfaceDevice::startListener() {
53 if(!runningHIDListner) APIHIDProperties::startListener();
54 runningHIDListner = true;
56 void HumanInterfaceDevice::stopListener() {
57 if(runningHIDListner) APIHIDProperties::stopListener();
58 runningHIDListner = false;
61 bool HumanInterfaceDevice::runningHIDListner = false;
62 HumanInterfaceDevice::States HumanInterfaceDevice::state[3];
64 typedef HumanInterfaceDevice HID;
67 HID::ButtonStateHolder::ButtonStateHolder(const Mouse::Button &t)
69 target = (Mouse::Button*)&t;
70 pressed_ = pushed_ = released_ = hadpressed_ = false;
72 void HID::ButtonStateHolder::ask()
74 hadpressed_ = pressed_;
75 pressed_ = target->pressed();
76 if(hadpressed_==false && pressed_==true) pushed_ = true;
77 if(hadpressed_==true && pressed_==false) released_ = true;
79 bool HID::ButtonStateHolder::pressed()
84 bool HID::ButtonStateHolder::pushed()
91 bool HID::ButtonStateHolder::released()
100 //////// Class Keyboard ////////
103 Keyboard::one(0),Keyboard::two(1),Keyboard::three(2),Keyboard::four(3),Keyboard::five(4),Keyboard::six(5),Keyboard::seven(6),Keyboard::eight(7),Keyboard::nine(8),Keyboard::zero(9),
104 Keyboard::a(10),Keyboard::b(11),Keyboard::c(12),Keyboard::d(13),Keyboard::e(14),Keyboard::f(15),Keyboard::g(16),Keyboard::h(17),Keyboard::i(18),Keyboard::j(19),Keyboard::k(20),Keyboard::l(21),Keyboard::m(22),Keyboard::n(23),Keyboard::o(24),Keyboard::p(25),Keyboard::q(26),Keyboard::r(27),Keyboard::s(28),Keyboard::t(29),Keyboard::u(30),Keyboard::v(31),Keyboard::w(32),Keyboard::x(33),Keyboard::y(34),Keyboard::z(35),
105 Keyboard::comma(36),Keyboard::period(37),Keyboard::slash(38),
106 Keyboard::pad0(39),Keyboard::pad1(40),Keyboard::pad2(41),Keyboard::pad3(42),Keyboard::pad4(43),Keyboard::pad5(44),Keyboard::pad6(45),Keyboard::pad7(46),Keyboard::pad8(47),Keyboard::pad9(48),
107 Keyboard::rtn(49),Keyboard::spc(50),Keyboard::esc(51),Keyboard::tab(52),Keyboard::up(53),Keyboard::down(54),Keyboard::left(55),Keyboard::right(56),
109 Keyboard::shift(100),Keyboard::shift_r(101),Keyboard::ctrl(102),Keyboard::alt(103),
111 Keyboard::hyphen(59),Keyboard::equal(60),Keyboard::backslash(61),Keyboard::caret(62),
112 Keyboard::underscore(63),Keyboard::semicolon(64),Keyboard::colon(65),Keyboard::leftbracket(66),Keyboard::rightbracket(67),Keyboard::at(68), // because these keys laid on different location in differnt keyboard-lacalization.
113 Keyboard::padcomma(69),Keyboard::padperiod(70),Keyboard::padenter(71),Keyboard::padplus(72),Keyboard::padminus(73),Keyboard::padasterisk(74),Keyboard::padslash(75),Keyboard::padequal(76),Keyboard::padclear(77), // because these keys are OS-specific.
114 Keyboard::nfer(78),Keyboard::xfer(79) // because these keys exist only on Japanese keyboard
117 Keyboard::Key::Key(const int code_) : code(code_) {}
118 bool Keyboard::Key::pressed() const {
119 return Keyboard::get(*this, Keyboard::pressed);
121 bool Keyboard::Key::pushed() const {
122 return Keyboard::get(*this, Keyboard::pushed);
124 bool Keyboard::Key::released() const {
125 return Keyboard::get(*this, Keyboard::released);
127 bool Keyboard::Key::get(KeyState state) const {
129 case Keyboard::pressed:
131 case Keyboard::released:
133 case Keyboard::pushed:
136 throw new Exception("Keyboard.get: unknown key state.");
143 void Keyboard::update() {
144 APIHIDProperties::update();
146 void Keyboard::refresh() {
147 APIHIDProperties::update();
148 APIHIDProperties::refresh_needed = true;
151 void Keyboard::refresh_internal() {
152 for(int i=0;i<128;i++) {
153 HID::state[HID::pushed].key[i] = 0;
154 HID::state[HID::released].key[i] = 0;
158 bool Keyboard::get(Key key, KeyState state) {
159 APIHIDProperties::update();
160 return APIHIDProperties::get(key, state);
164 bool Keyboard::getKey(Key key, KeyState state) {
165 return APIHIDProperties::get(key, state);
168 void Keyboard::wait(Key key, bool needrelease) {
172 if(!Input::runningInputListner) { updateKeyState(); }
173 if(get(code,pressed)) { break; }
176 if(!Input::runningInputListner) { updateKeyState(); }
178 if(!get(code,pressed)) { break; }
184 void Keyboard::allReleased() {
189 if(!Input::runningInputListner) { updateKeyState(); }
191 for(int i=0; i<4; i++ ) {
192 if(key[pressed][i]!=0) { loop=true; }
199 //////// Class Mouse ////////
201 ::Psychlops::Point Mouse::wheelDelta, Mouse::lastPoint;
203 const Mouse::Button Mouse::left(0), Mouse::right(1), Mouse::middle(2), Mouse::any(-1);
204 Mouse::Button::Button(const int code_) : code(code_) {}
205 bool Mouse::Button::pressed() const {
206 return Mouse::get(*this, Mouse::pressed);
208 bool Mouse::Button::pushed() const {
209 return Mouse::get(*this, Mouse::pushed);
211 bool Mouse::Button::released() const {
212 return Mouse::get(*this, Mouse::released);
214 bool Mouse::Button::get(ButtonState state) const {
218 case Mouse::released:
223 throw new Exception("Mouse.get: unknown key state.");
228 Mouse::POSITIONVAL::POSITIONVAL() {}
229 Mouse::POSITIONVAL::operator int() {
233 int Mouse::POSITIONVAL::operator =(int val) {
235 APIHIDProperties::setPointerPosition(uniX.val_, uniY.val_);
238 Mouse::POSITIONVAL Mouse::uniX, Mouse::uniY;
240 Mouse::POSITION::operator Psychlops::Point() {
242 return Point(uniX.val_, uniY.val_);
244 Psychlops::Point Mouse::POSITION::operator =(Psychlops::Point po) {
245 uniX.val_ = (int)Math::round(po.x);
246 uniY.val_ = (int)Math::round(po.y);
247 APIHIDProperties::setPointerPosition(uniX.val_, uniY.val_);
248 return Point(uniX.val_, uniY.val_);
250 Mouse::POSITION Mouse::uniPosition;
252 Mouse::DEFPOSITIONVALX::operator int() { return Display::the_canvas->mouse().x; }
253 int Mouse::DEFPOSITIONVALX::operator =(int val) { return (int)Display::the_canvas->mouse(Point(val, y)).x; }
254 Mouse::DEFPOSITIONVALX Mouse::x;
255 Mouse::DEFPOSITIONVALY::operator int() { return Display::the_canvas->mouse().y; }
256 int Mouse::DEFPOSITIONVALY::operator =(int val) { return (int)Display::the_canvas->mouse(Point(x, val)).y; }
257 Mouse::DEFPOSITIONVALY Mouse::y;
258 Mouse::DEFPOSITION::operator ::Psychlops::Point() { if(Display::the_canvas==0) return Point(0,0); else return Display::the_canvas->mouse(); }
259 ::Psychlops::Point Mouse::DEFPOSITION::operator =(::Psychlops::Point val) { if(Display::the_canvas==0) return Point(0,0); else return Display::the_canvas->mouse(val); }
260 Mouse::DEFPOSITION Mouse::position;
262 bool Mouse::show_local_pointer_;
263 Figure* Mouse::pointer_figure;
264 Group default_pointer_figure;
265 Rectangle hb,vb,hf,vf;
267 void Mouse::initialize() {
268 show_local_pointer_ = false;
269 default_pointer_figure.append(hb);
270 default_pointer_figure.append(vb);
271 default_pointer_figure.append(hf);
272 default_pointer_figure.append(vf);
276 void Mouse::update() {
277 APIHIDProperties::update();
279 void Mouse::refresh() {
280 APIHIDProperties::update();
281 APIHIDProperties::refresh_needed = true;
284 void Mouse::refresh_internal() {
285 for(int i=0; i<4; i++) for(int j=0; j<8; j++) HID::state[i].button[j] = false;
288 bool Mouse::get(Button button, ButtonState state) {
289 return APIHIDProperties::get(button, (Mouse::ButtonState)state);
291 ::Psychlops::Point Mouse::getWheelDelta() {
292 Point p = wheelDelta;
293 wheelDelta.set(0,0,0);
297 void Mouse::drawPointer(Point p, Drawable &target) {
299 Rectangle hb(11,3), vb(3,11), hf(9,1), vf(1,9);
300 if(show_local_pointer_) {
301 hb.centering(p).draw(Color::black);
302 vb.centering(p).draw(Color::black);
303 hf.centering(p).draw(Color::white);
304 vf.centering(p).draw(Color::white);
307 pointer_figure->centering(p).draw(target);
310 show_local_pointer_ = true;
313 show_local_pointer_ = false;
315 bool Mouse::showLocalPointer()
317 return show_local_pointer_;
320 void setDefaultPointer(int size)
322 hf.set(9*size, size).shift(-9*size/2, -size/2);
323 hf.fill = Color::white;
324 vf.set(size, 9*size).shift(-size/2, -9*size/2);
325 vf.fill = Color::white;
326 hb.set(2+9*size, 2+size).shift(-(2+9*size)/2, -(2+size)/2);
327 hb.fill = Color::black;
328 vb.set(2+size, 2+9*size).shift(-(2+size)/2, -(2+9*size)/2);
329 vb.fill = Color::black;
331 void Mouse::setPointer(int size)
333 setDefaultPointer(size);
334 pointer_figure = &default_pointer_figure;
336 void Mouse::setPointer(Figure &fig)
338 pointer_figure = &fig;
343 } /* <- namespace Psycholops */