OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / devices / psychlops_io_hid.cpp
1 /*
2  *  psychlops_io_keyboard.cpp
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/01/05 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9
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"
15
16 #define PSYCHLOPS_HID_API_PLATFORM
17 #include "../../platform/psychlops_platform_selector.h"
18
19 namespace Psychlops {
20
21
22 ////////        Class HumanInterfaceDevice      ////////
23
24         int HumanInterfaceDevice::pushRepeatFrames = 20, HumanInterfaceDevice::pushRepeatInterval = 3;
25
26         void HumanInterfaceDevice::initialize() {
27                 APIHIDProperties::initialize();
28                 //Keyboard::initialize();
29                 Mouse::initialize();
30                 startListener();
31         }
32         void HumanInterfaceDevice::finalize() {
33                 stopListener();
34                 APIHIDProperties::finalize();
35         }
36         bool HumanInterfaceDevice::get(Keyboard::Key code, Keyboard::KeyState state) {
37                 return Keyboard::get(code,state);
38         }
39         bool HumanInterfaceDevice::get(Mouse::Button code, Mouse::ButtonState state) {
40                 return Mouse::get(code,state);
41         }
42
43         void HumanInterfaceDevice::wait(Keyboard::Key code, Keyboard::KeyState state) {
44                 Keyboard::wait(code,state);
45         }
46
47         void HumanInterfaceDevice::refresh() {
48                 Keyboard::refresh();
49                 Mouse::refresh();
50         }
51
52         void HumanInterfaceDevice::startListener() {
53                 if(!runningHIDListner) APIHIDProperties::startListener();
54                 runningHIDListner = true;
55         }
56         void HumanInterfaceDevice::stopListener() {
57                 if(runningHIDListner) APIHIDProperties::stopListener();
58                 runningHIDListner = false;
59         }
60
61         bool HumanInterfaceDevice::runningHIDListner = false;
62         HumanInterfaceDevice::States HumanInterfaceDevice::state[3];
63
64         typedef HumanInterfaceDevice HID;
65
66
67         HID::ButtonStateHolder::ButtonStateHolder(const Mouse::Button &t)
68         {
69                 target = (Mouse::Button*)&t;
70                 pressed_ = pushed_ = released_ = hadpressed_ = false;
71         }
72         void HID::ButtonStateHolder::ask()
73         {
74                 hadpressed_ = pressed_;
75                 pressed_ = target->pressed();
76                 if(hadpressed_==false && pressed_==true) pushed_ = true;
77                 if(hadpressed_==true && pressed_==false) released_ = true;
78         }
79         bool HID::ButtonStateHolder::pressed()
80         {
81                 ask();
82                 return pressed_;
83         }
84         bool HID::ButtonStateHolder::pushed()
85         {
86                 ask();
87                 bool tmp = pushed_;
88                 pushed_ = false;
89                 return tmp;
90         }
91         bool HID::ButtonStateHolder::released()
92         {
93                 ask();
94                 bool tmp = released_;
95                 released_ = false;
96                 return tmp;
97         }
98
99
100 ////////        Class Keyboard  ////////
101
102         const Keyboard::Key
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),
108                 //      key modulator
109                 Keyboard::shift(100),Keyboard::shift_r(101),Keyboard::ctrl(102),Keyboard::alt(103),
110                 //  not recommended
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
115         ;
116
117         Keyboard::Key::Key(const int code_) : code(code_) {}
118         bool Keyboard::Key::pressed() const {
119                 return Keyboard::get(*this, Keyboard::pressed);
120         }
121         bool Keyboard::Key::pushed() const {
122                 return Keyboard::get(*this, Keyboard::pushed);
123         }
124         bool Keyboard::Key::released() const {
125                 return Keyboard::get(*this, Keyboard::released);
126         }
127         bool Keyboard::Key::get(KeyState state) const {
128                 switch(state) {
129                 case Keyboard::pressed:
130                         return pressed();
131                 case Keyboard::released:
132                         return released();
133                 case Keyboard::pushed:
134                         return pushed();
135                 default:
136                         throw new Exception("Keyboard.get: unknown key state.");
137                 }
138         }
139
140
141
142
143         void Keyboard::update() {
144                 APIHIDProperties::update();
145         }
146         void Keyboard::refresh() {
147                 APIHIDProperties::update();
148                 APIHIDProperties::refresh_needed = true;
149                 refresh_internal();
150         }
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;
155                 }
156         }
157
158         bool Keyboard::get(Key key, KeyState state) {
159                 APIHIDProperties::update();
160                 return APIHIDProperties::get(key, state);
161         }
162
163     //  obsolete
164         bool Keyboard::getKey(Key key, KeyState state) {
165             return APIHIDProperties::get(key, state);
166         }
167
168         void Keyboard::wait(Key key, bool needrelease) {
169                 exit(50);
170                 /*
171             for(;;) {
172                 if(!Input::runningInputListner) { updateKeyState(); }
173                 if(get(code,pressed)) { break; }
174             }
175             if(needrelease) {
176                 if(!Input::runningInputListner) { updateKeyState(); }
177                 for(;;) {
178                     if(!get(code,pressed)) { break; }
179                 }
180             }
181             */
182         }
183
184         void Keyboard::allReleased() {
185                 exit(50);
186                 /*
187             bool loop = true;
188             while(loop) {
189                 if(!Input::runningInputListner) { updateKeyState(); }
190                 loop = false;
191                 for(int i=0; i<4; i++ ) {
192                     if(key[pressed][i]!=0) { loop=true; }
193                 }
194             }
195             */
196         }
197
198
199 ////////        Class Mouse     ////////
200
201         ::Psychlops::Point Mouse::wheelDelta, Mouse::lastPoint;
202
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);
207         }
208         bool Mouse::Button::pushed() const {
209                 return Mouse::get(*this, Mouse::pushed);
210         }
211         bool Mouse::Button::released() const {
212                 return Mouse::get(*this, Mouse::released);
213         }
214         bool Mouse::Button::get(ButtonState state) const {
215                 switch(state) {
216                 case Mouse::pressed:
217                         return pressed();
218                 case Mouse::released:
219                         return released();
220                 case Mouse::pushed:
221                         return pushed();
222                 default:
223                         throw new Exception("Mouse.get: unknown key state.");
224                 }
225         }
226
227
228         Mouse::POSITIONVAL::POSITIONVAL() {}
229         Mouse::POSITIONVAL::operator int() {
230                 Mouse::update();
231                 return val_;
232         }
233         int Mouse::POSITIONVAL::operator =(int val) {
234                 val_ = val;
235                 APIHIDProperties::setPointerPosition(uniX.val_, uniY.val_);
236                 return val_;
237         }
238         Mouse::POSITIONVAL Mouse::uniX, Mouse::uniY;
239
240         Mouse::POSITION::operator Psychlops::Point() {
241                 Mouse::update();
242                 return Point(uniX.val_, uniY.val_);
243         }
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_);
249         }
250         Mouse::POSITION Mouse::uniPosition;
251
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;
261
262         bool Mouse::show_local_pointer_;
263         Figure* Mouse::pointer_figure;
264         Group default_pointer_figure;
265         Rectangle hb,vb,hf,vf;
266
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);
273                 setPointer(1);
274         }
275
276         void Mouse::update() {
277                 APIHIDProperties::update();
278         }
279         void Mouse::refresh() {
280                 APIHIDProperties::update();
281                 APIHIDProperties::refresh_needed = true;
282                 refresh_internal();
283         }
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;
286         }
287
288         bool Mouse::get(Button button, ButtonState state) {
289                 return APIHIDProperties::get(button, (Mouse::ButtonState)state);
290         }
291         ::Psychlops::Point Mouse::getWheelDelta() {
292                 Point p = wheelDelta;
293                 wheelDelta.set(0,0,0);
294                 return p;
295         }
296
297         void Mouse::drawPointer(Point p, Drawable &target) {
298                 /*
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);
305                 }
306                 */
307                 pointer_figure->centering(p).draw(target);
308         }
309         void Mouse::show() {
310                 show_local_pointer_ = true;
311         }
312         void Mouse::hide() {
313                 show_local_pointer_ = false;
314         }
315         bool  Mouse::showLocalPointer()
316         {
317                 return show_local_pointer_;
318         }
319
320         void setDefaultPointer(int size)
321         {
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;
330         }
331         void Mouse::setPointer(int size)
332         {
333                 setDefaultPointer(size);
334                 pointer_figure = &default_pointer_figure;
335         }
336         void Mouse::setPointer(Figure &fig)
337         {
338                 pointer_figure = &fig;
339         }
340
341
342
343 }       /*      <- namespace Psycholops         */
344