diff options
Diffstat (limited to 'libraries/ecore/src/lib/ecore_psl1ght/ecore_psl1ght.c')
-rw-r--r-- | libraries/ecore/src/lib/ecore_psl1ght/ecore_psl1ght.c | 804 |
1 files changed, 804 insertions, 0 deletions
diff --git a/libraries/ecore/src/lib/ecore_psl1ght/ecore_psl1ght.c b/libraries/ecore/src/lib/ecore_psl1ght/ecore_psl1ght.c new file mode 100644 index 0000000..64cef9f --- /dev/null +++ b/libraries/ecore/src/lib/ecore_psl1ght/ecore_psl1ght.c | |||
@@ -0,0 +1,804 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include <config.h> | ||
3 | #endif | ||
4 | |||
5 | #include <sysutil/video.h> | ||
6 | #include <sysutil/sysutil.h> | ||
7 | #include <sysmodule/sysmodule.h> | ||
8 | #include <io/pad.h> | ||
9 | #include <io/mouse.h> | ||
10 | #include <io/kb.h> | ||
11 | #include <io/camera.h> | ||
12 | #include <io/move.h> | ||
13 | #include <sys/process.h> | ||
14 | |||
15 | #include "moveutil.h" | ||
16 | |||
17 | #include "Eina.h" | ||
18 | #include "Ecore_Psl1ght.h" | ||
19 | #include "Ecore_Input.h" | ||
20 | #include "Ecore.h" | ||
21 | #include "ecore_psl1ght_private.h" | ||
22 | #include "ecore_private.h" | ||
23 | #include "Ecore_Psl1ght_Keys.h" | ||
24 | |||
25 | /* Allocate 1MB stack to avoid overflows */ | ||
26 | SYS_PROCESS_PARAM(1001, 0x100000); | ||
27 | |||
28 | int _ecore_psl1ght_log_dom = -1; | ||
29 | |||
30 | EAPI int ECORE_PSL1GHT_EVENT_KEY_MODIFIERS = 0; | ||
31 | EAPI int ECORE_PSL1GHT_EVENT_GOT_FOCUS = 0; | ||
32 | EAPI int ECORE_PSL1GHT_EVENT_LOST_FOCUS = 0; | ||
33 | EAPI int ECORE_PSL1GHT_EVENT_EXPOSE = 0; | ||
34 | |||
35 | static int _ecore_psl1ght_init_count = 0; | ||
36 | static int window_width = 0; | ||
37 | static int window_height = 0; | ||
38 | /* Mouse support */ | ||
39 | static int mouse_connected = FALSE; | ||
40 | static u8 mouse_buttons = 0; | ||
41 | static int mouse_x = 0; | ||
42 | static int mouse_y = 0; | ||
43 | /* Keyboard support */ | ||
44 | static int keyboard_connected = FALSE; | ||
45 | static KbLed keyboard_leds = {{0}}; | ||
46 | static KbMkey keyboard_mods = {{0}}; | ||
47 | static u16 keyboard_old_key = 0; | ||
48 | /* Pad support */ | ||
49 | static padData pad_data; | ||
50 | static int pad_old_x = 0; | ||
51 | static int pad_old_o = 0; | ||
52 | /* Move support */ | ||
53 | static int move_connected = FALSE; | ||
54 | static moveContext *move_context = NULL; | ||
55 | u16 move_buttons = 0; | ||
56 | |||
57 | static void xmb_event_handler(u64 status, u64 param, void *user_data); | ||
58 | |||
59 | /** | ||
60 | * @defgroup Ecore_Psl1ght_Library_Group PSL1GHT Library Functions | ||
61 | * | ||
62 | * Functions used to set up and shut down the Ecore_Psl1ght functions. | ||
63 | */ | ||
64 | |||
65 | /** | ||
66 | * Sets up the Ecore_Psl1ght library. | ||
67 | * @param name device target name | ||
68 | * @return @c 0 on failure. Otherwise, the number of times the library has | ||
69 | * been initialised without being shut down. | ||
70 | * @ingroup Ecore_PSL1GHT_Library_Group | ||
71 | */ | ||
72 | EAPI int | ||
73 | ecore_psl1ght_init(const char *name __UNUSED__) | ||
74 | { | ||
75 | videoState state; | ||
76 | videoResolution resolution; | ||
77 | int ret, camera_loaded, gem_loaded; | ||
78 | |||
79 | if (++_ecore_psl1ght_init_count != 1) | ||
80 | return _ecore_psl1ght_init_count; | ||
81 | _ecore_psl1ght_log_dom = eina_log_domain_register | ||
82 | ("ecore_psl1ght", ECORE_PSL1GHT_DEFAULT_LOG_COLOR); | ||
83 | if (_ecore_psl1ght_log_dom < 0) | ||
84 | { | ||
85 | EINA_LOG_ERR("Impossible to create a log domain for the Ecore PSL1GHT module."); | ||
86 | return --_ecore_psl1ght_init_count; | ||
87 | } | ||
88 | if (!ecore_event_init()) | ||
89 | { | ||
90 | eina_log_domain_unregister(_ecore_psl1ght_log_dom); | ||
91 | _ecore_psl1ght_log_dom = -1; | ||
92 | return --_ecore_psl1ght_init_count; | ||
93 | } | ||
94 | |||
95 | if (videoGetState (0, 0, &state) == 0 && | ||
96 | videoGetResolution (state.displayMode.resolution, &resolution) == 0) | ||
97 | { | ||
98 | ecore_psl1ght_resolution_set (resolution.width, resolution.height); | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | ecore_event_shutdown(); | ||
103 | eina_log_domain_unregister(_ecore_psl1ght_log_dom); | ||
104 | _ecore_psl1ght_log_dom = -1; | ||
105 | return --_ecore_psl1ght_init_count; | ||
106 | } | ||
107 | |||
108 | /* Pad support */ | ||
109 | ioPadInit (7); | ||
110 | /* Mouse support */ | ||
111 | ioMouseInit(2); | ||
112 | mouse_buttons = 0; | ||
113 | mouse_x = 0; | ||
114 | mouse_y = 0; | ||
115 | |||
116 | /* Keyboard support */ | ||
117 | ioKbInit(2); | ||
118 | keyboard_leds._KbLedU.leds = 0; | ||
119 | keyboard_mods._KbMkeyU.mkeys = 0; | ||
120 | |||
121 | /* Initialize Move */ | ||
122 | move_context = NULL; | ||
123 | move_buttons = 0; | ||
124 | |||
125 | camera_loaded = !sysModuleIsLoaded (SYSMODULE_CAMERA); | ||
126 | if (!camera_loaded) | ||
127 | ret = sysModuleLoad (SYSMODULE_CAMERA); | ||
128 | else | ||
129 | ret = 0; | ||
130 | if (ret == 0) | ||
131 | { | ||
132 | gem_loaded = !sysModuleIsLoaded (SYSMODULE_GEM); | ||
133 | if (!gem_loaded) | ||
134 | ret = sysModuleLoad (SYSMODULE_GEM); | ||
135 | if (ret == 0) | ||
136 | { | ||
137 | move_context = initMove (); | ||
138 | } | ||
139 | else { | ||
140 | if (gem_loaded) | ||
141 | sysModuleUnload (SYSMODULE_CAMERA); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | sysUtilRegisterCallback (SYSUTIL_EVENT_SLOT0, xmb_event_handler, NULL); | ||
146 | |||
147 | ECORE_PSL1GHT_EVENT_GOT_FOCUS = ecore_event_type_new(); | ||
148 | ECORE_PSL1GHT_EVENT_LOST_FOCUS = ecore_event_type_new(); | ||
149 | ECORE_PSL1GHT_EVENT_EXPOSE = ecore_event_type_new(); | ||
150 | ECORE_PSL1GHT_EVENT_KEY_MODIFIERS = ecore_event_type_new(); | ||
151 | |||
152 | mouse_x = 0; | ||
153 | mouse_y = 0; | ||
154 | |||
155 | return _ecore_psl1ght_init_count; | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Shuts down the Ecore_Psl1ght library. | ||
160 | * @return @c The number of times the system has been initialised without | ||
161 | * being shut down. | ||
162 | * @ingroup Ecore_PSL1GHT_Library_Group | ||
163 | */ | ||
164 | EAPI int | ||
165 | ecore_psl1ght_shutdown(void) | ||
166 | { | ||
167 | if (--_ecore_psl1ght_init_count != 0) | ||
168 | return _ecore_psl1ght_init_count; | ||
169 | |||
170 | ecore_event_shutdown(); | ||
171 | eina_log_domain_unregister(_ecore_psl1ght_log_dom); | ||
172 | _ecore_psl1ght_log_dom = -1; | ||
173 | |||
174 | ECORE_PSL1GHT_EVENT_GOT_FOCUS = 0; | ||
175 | ECORE_PSL1GHT_EVENT_LOST_FOCUS = 0; | ||
176 | ECORE_PSL1GHT_EVENT_EXPOSE = 0; | ||
177 | ECORE_PSL1GHT_EVENT_KEY_MODIFIERS = 0; | ||
178 | |||
179 | ioPadEnd(); | ||
180 | ioMouseEnd(); | ||
181 | ioKbEnd(); | ||
182 | |||
183 | if (move_context) | ||
184 | { | ||
185 | endMove (move_context); | ||
186 | move_context = NULL; | ||
187 | sysModuleUnload (SYSMODULE_CAMERA); | ||
188 | sysModuleUnload (SYSMODULE_GEM); | ||
189 | } | ||
190 | |||
191 | sysUtilUnregisterCallback(SYSUTIL_EVENT_SLOT0); | ||
192 | |||
193 | return _ecore_psl1ght_init_count; | ||
194 | } | ||
195 | |||
196 | static unsigned int | ||
197 | _ecore_psl1ght_get_time(void) | ||
198 | { | ||
199 | return (unsigned int)((unsigned long long) | ||
200 | (ecore_time_get() * 1000.0) & 0xffffffff); | ||
201 | } | ||
202 | |||
203 | static unsigned int | ||
204 | _ecore_psl1ght_get_modifiers(void) | ||
205 | { | ||
206 | unsigned int modifiers = 0; | ||
207 | |||
208 | if (keyboard_mods._KbMkeyU._KbMkeyS.r_shift || | ||
209 | keyboard_mods._KbMkeyU._KbMkeyS.l_shift) | ||
210 | modifiers |= ECORE_EVENT_MODIFIER_SHIFT; | ||
211 | if (keyboard_mods._KbMkeyU._KbMkeyS.r_ctrl || | ||
212 | keyboard_mods._KbMkeyU._KbMkeyS.l_ctrl) | ||
213 | modifiers |= ECORE_EVENT_MODIFIER_CTRL; | ||
214 | if (keyboard_mods._KbMkeyU._KbMkeyS.r_alt || | ||
215 | keyboard_mods._KbMkeyU._KbMkeyS.l_alt) | ||
216 | modifiers |= ECORE_EVENT_MODIFIER_ALT; | ||
217 | if (keyboard_mods._KbMkeyU._KbMkeyS.r_win || | ||
218 | keyboard_mods._KbMkeyU._KbMkeyS.l_win) | ||
219 | modifiers |= ECORE_EVENT_MODIFIER_WIN; | ||
220 | |||
221 | if (keyboard_leds._KbLedU._KbLedS.num_lock) | ||
222 | modifiers |= ECORE_EVENT_LOCK_NUM; | ||
223 | if (keyboard_leds._KbLedU._KbLedS.caps_lock) | ||
224 | modifiers |= ECORE_EVENT_LOCK_CAPS; | ||
225 | if (keyboard_leds._KbLedU._KbLedS.scroll_lock) | ||
226 | modifiers |= ECORE_EVENT_LOCK_SCROLL; | ||
227 | |||
228 | return modifiers; | ||
229 | } | ||
230 | |||
231 | static void | ||
232 | _ecore_psl1ght_key_modifiers(KbMkey *mods, KbLed *leds) | ||
233 | { | ||
234 | Ecore_Psl1ght_Event_Key_Modifiers *ev; | ||
235 | Eina_Bool emit = EINA_FALSE; | ||
236 | |||
237 | ev = malloc(sizeof(Ecore_Psl1ght_Event_Key_Modifiers)); | ||
238 | if (!ev) return; | ||
239 | |||
240 | if (mods->_KbMkeyU._KbMkeyS.l_shift != | ||
241 | keyboard_mods._KbMkeyU._KbMkeyS.l_shift || | ||
242 | mods->_KbMkeyU._KbMkeyS.r_shift != | ||
243 | keyboard_mods._KbMkeyU._KbMkeyS.r_shift) | ||
244 | { | ||
245 | emit = EINA_TRUE; | ||
246 | ev->shift_changed = EINA_TRUE; | ||
247 | ev->shift = mods->_KbMkeyU._KbMkeyS.r_shift | | ||
248 | mods->_KbMkeyU._KbMkeyS.l_shift; | ||
249 | } | ||
250 | if (mods->_KbMkeyU._KbMkeyS.l_ctrl != | ||
251 | keyboard_mods._KbMkeyU._KbMkeyS.l_ctrl || | ||
252 | mods->_KbMkeyU._KbMkeyS.r_ctrl != | ||
253 | keyboard_mods._KbMkeyU._KbMkeyS.r_ctrl) | ||
254 | { | ||
255 | emit = EINA_TRUE; | ||
256 | ev->ctrl_changed = EINA_TRUE; | ||
257 | ev->ctrl = mods->_KbMkeyU._KbMkeyS.r_ctrl | | ||
258 | mods->_KbMkeyU._KbMkeyS.l_ctrl; | ||
259 | } | ||
260 | if (mods->_KbMkeyU._KbMkeyS.l_alt != | ||
261 | keyboard_mods._KbMkeyU._KbMkeyS.l_alt || | ||
262 | mods->_KbMkeyU._KbMkeyS.r_alt != | ||
263 | keyboard_mods._KbMkeyU._KbMkeyS.r_alt) | ||
264 | { | ||
265 | emit = EINA_TRUE; | ||
266 | ev->alt_changed = EINA_TRUE; | ||
267 | ev->alt = mods->_KbMkeyU._KbMkeyS.r_alt | | ||
268 | mods->_KbMkeyU._KbMkeyS.l_alt; | ||
269 | } | ||
270 | if (mods->_KbMkeyU._KbMkeyS.l_win != | ||
271 | keyboard_mods._KbMkeyU._KbMkeyS.l_win || | ||
272 | mods->_KbMkeyU._KbMkeyS.r_win != | ||
273 | keyboard_mods._KbMkeyU._KbMkeyS.r_win) | ||
274 | { | ||
275 | emit = EINA_TRUE; | ||
276 | ev->win_changed = EINA_TRUE; | ||
277 | ev->win = mods->_KbMkeyU._KbMkeyS.r_win | | ||
278 | mods->_KbMkeyU._KbMkeyS.l_win; | ||
279 | } | ||
280 | keyboard_mods = *mods; | ||
281 | |||
282 | if (leds->_KbLedU._KbLedS.num_lock != | ||
283 | keyboard_leds._KbLedU._KbLedS.num_lock) | ||
284 | { | ||
285 | emit = EINA_TRUE; | ||
286 | ev->num_lock_changed = EINA_TRUE; | ||
287 | ev->num_lock = leds->_KbLedU._KbLedS.num_lock; | ||
288 | } | ||
289 | if (leds->_KbLedU._KbLedS.caps_lock != | ||
290 | keyboard_leds._KbLedU._KbLedS.caps_lock) | ||
291 | { | ||
292 | emit = EINA_TRUE; | ||
293 | ev->caps_lock_changed = EINA_TRUE; | ||
294 | ev->caps_lock = leds->_KbLedU._KbLedS.caps_lock; | ||
295 | } | ||
296 | if (leds->_KbLedU._KbLedS.scroll_lock != | ||
297 | keyboard_leds._KbLedU._KbLedS.scroll_lock) | ||
298 | { | ||
299 | emit = EINA_TRUE; | ||
300 | ev->scroll_lock_changed = EINA_TRUE; | ||
301 | ev->scroll_lock = leds->_KbLedU._KbLedS.scroll_lock; | ||
302 | } | ||
303 | keyboard_leds = *leds; | ||
304 | |||
305 | if (emit) | ||
306 | { | ||
307 | ev->timestamp = _ecore_psl1ght_get_time (); | ||
308 | ev->modifiers = _ecore_psl1ght_get_modifiers(); | ||
309 | ecore_event_add(ECORE_PSL1GHT_EVENT_KEY_MODIFIERS, ev, NULL, NULL); | ||
310 | } | ||
311 | else | ||
312 | { | ||
313 | free(ev); | ||
314 | } | ||
315 | } | ||
316 | |||
317 | static void | ||
318 | unicodeToUtf8(u16 w, char *utf8buf) | ||
319 | { | ||
320 | unsigned char *utf8s = (unsigned char *)utf8buf; | ||
321 | |||
322 | if ( w < 0x0080 ) | ||
323 | { | ||
324 | utf8s[0] = ( unsigned char )w; | ||
325 | utf8s[1] = 0; | ||
326 | } | ||
327 | else if ( w < 0x0800 ) | ||
328 | { | ||
329 | utf8s[0] = 0xc0 | ((w) >> 6); | ||
330 | utf8s[1] = 0x80 | ((w) & 0x3f); | ||
331 | utf8s[2] = 0; | ||
332 | } | ||
333 | else { | ||
334 | utf8s[0] = 0xe0 | ((w) >> 12); | ||
335 | utf8s[1] = 0x80 | (((w) >> 6) & 0x3f); | ||
336 | utf8s[2] = 0x80 | ((w) & 0x3f); | ||
337 | utf8s[3] = 0; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | static Ecore_Event_Key * | ||
342 | _ecore_psl1ght_event_key(u16 key) | ||
343 | { | ||
344 | Ecore_Event_Key *ev; | ||
345 | char utf8[4]; | ||
346 | u16 utf16; | ||
347 | unsigned int i; | ||
348 | |||
349 | ev = malloc(sizeof(Ecore_Event_Key)); | ||
350 | if (!ev) return NULL; | ||
351 | |||
352 | ev->timestamp = _ecore_psl1ght_get_time (); | ||
353 | ev->window = 0; | ||
354 | ev->event_window = 0; | ||
355 | ev->modifiers = _ecore_psl1ght_get_modifiers(); | ||
356 | |||
357 | printf ("Key is %X\n", key); | ||
358 | key &= ~KB_KEYPAD; | ||
359 | for (i = 0; i < sizeof(keystable) / sizeof(struct _ecore_psl1ght_keys_s); ++i) | ||
360 | if (keystable[i].code == key) | ||
361 | { | ||
362 | ev->keyname = keystable[i].name; | ||
363 | ev->key = keystable[i].name; | ||
364 | ev->string = keystable[i].compose; | ||
365 | ev->compose = keystable[i].compose; | ||
366 | |||
367 | printf ("Found key '%s' in the table\n", ev->keyname); | ||
368 | return ev; | ||
369 | } | ||
370 | |||
371 | utf16 = ioKbCnvRawCode (KB_MAPPING_101, keyboard_mods, keyboard_leds, key); | ||
372 | unicodeToUtf8(utf16, utf8); | ||
373 | printf ("Converting to utf16 : %X - utf8 : %s\n", utf16, utf8); | ||
374 | ev->keyname = ev->key = ev->string = ev->compose = strdup (utf8); | ||
375 | |||
376 | return ev; | ||
377 | } | ||
378 | |||
379 | static void | ||
380 | _ecore_psl1ght_mouse_move(s32 x_axis, s32 y_axis) | ||
381 | { | ||
382 | Ecore_Event_Mouse_Move *ev; | ||
383 | |||
384 | ev = malloc(sizeof(Ecore_Event_Mouse_Move)); | ||
385 | if (!ev) return; | ||
386 | |||
387 | mouse_x += x_axis; | ||
388 | mouse_y += y_axis; | ||
389 | if (mouse_x < 0) mouse_x = 0; | ||
390 | if (mouse_y < 0) mouse_y = 0; | ||
391 | if (mouse_x > window_width) mouse_x = window_width; | ||
392 | if (mouse_y > window_height) mouse_y = window_height; | ||
393 | |||
394 | ev->window = 0; | ||
395 | ev->root_window = 0; | ||
396 | ev->event_window = 0; | ||
397 | ev->same_screen = 0; | ||
398 | ev->timestamp = _ecore_psl1ght_get_time (); | ||
399 | ev->modifiers = _ecore_psl1ght_get_modifiers (); | ||
400 | ev->x = ev->root.x = mouse_x; | ||
401 | ev->y = ev->root.x = mouse_y; | ||
402 | |||
403 | ev->multi.device = 0; | ||
404 | ev->multi.radius = ev->multi.radius_x = ev->multi.radius_y = 0; | ||
405 | ev->multi.pressure = ev->multi.angle = 0; | ||
406 | ev->multi.x = ev->multi.y = ev->multi.root.x = ev->multi.root.y = 0; | ||
407 | |||
408 | ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL); | ||
409 | } | ||
410 | |||
411 | static void | ||
412 | _ecore_psl1ght_mouse_button(int button, int pressed) | ||
413 | { | ||
414 | Ecore_Event_Mouse_Button *ev; | ||
415 | static unsigned int previous_timestamp = 0; | ||
416 | |||
417 | ev = malloc(sizeof(Ecore_Event_Mouse_Button)); | ||
418 | if (!ev) return; | ||
419 | |||
420 | ev->window = 0; | ||
421 | ev->root_window = 0; | ||
422 | ev->event_window = 0; | ||
423 | ev->same_screen = 0; | ||
424 | ev->timestamp = _ecore_psl1ght_get_time (); | ||
425 | ev->modifiers = _ecore_psl1ght_get_modifiers (); | ||
426 | ev->buttons = button; | ||
427 | if (ev->timestamp - previous_timestamp <= 500) | ||
428 | ev->double_click = 1; | ||
429 | ev->triple_click = 0; | ||
430 | previous_timestamp = ev->timestamp; | ||
431 | |||
432 | ev->x = ev->root.x = mouse_x; | ||
433 | ev->y = ev->root.y = mouse_y; | ||
434 | ev->multi.device = 0; | ||
435 | ev->multi.radius = ev->multi.radius_x = ev->multi.radius_y = 0; | ||
436 | ev->multi.pressure = ev->multi.angle = 0; | ||
437 | ev->multi.x = ev->multi.y = ev->multi.root.x = ev->multi.root.y = 0; | ||
438 | |||
439 | if (pressed) | ||
440 | ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL); | ||
441 | else | ||
442 | ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL); | ||
443 | } | ||
444 | |||
445 | static void | ||
446 | _ecore_psl1ght_mouse_wheel(s8 wheel, s8 tilt) | ||
447 | { | ||
448 | Ecore_Event_Mouse_Wheel *ev; | ||
449 | |||
450 | ev = malloc(sizeof(Ecore_Event_Mouse_Wheel)); | ||
451 | if (!ev) return; | ||
452 | |||
453 | ev->timestamp = _ecore_psl1ght_get_time (); | ||
454 | ev->window = 0; | ||
455 | ev->event_window = 0; | ||
456 | ev->modifiers = _ecore_psl1ght_get_modifiers (); | ||
457 | ev->direction = 0; | ||
458 | ev->z = wheel; | ||
459 | |||
460 | ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL); | ||
461 | } | ||
462 | |||
463 | #define PAD_STICK_DEADZONE 0x20 | ||
464 | |||
465 | static void | ||
466 | _ecore_psl1ght_poll_joypad(void) | ||
467 | { | ||
468 | padInfo padinfo; | ||
469 | int i; | ||
470 | |||
471 | /**/ | ||
472 | /* Check mouse events */ | ||
473 | ioPadGetInfo (&padinfo); | ||
474 | |||
475 | for (i = 0; i < 4; i++) /* Ignore the move */ | ||
476 | { | ||
477 | if (padinfo.status[i]) | ||
478 | { | ||
479 | int analog_h, analog_v; | ||
480 | |||
481 | if (ioPadGetData (i, &pad_data) != 0) | ||
482 | continue; | ||
483 | analog_h = pad_data.ANA_L_H - 0x80; | ||
484 | analog_v = pad_data.ANA_L_V - 0x80; | ||
485 | |||
486 | if (analog_h > PAD_STICK_DEADZONE) | ||
487 | analog_h -= PAD_STICK_DEADZONE; | ||
488 | else if (analog_h < -PAD_STICK_DEADZONE) | ||
489 | analog_h += PAD_STICK_DEADZONE; | ||
490 | else | ||
491 | analog_h = 0; | ||
492 | analog_h /= 10; | ||
493 | |||
494 | if (analog_v > PAD_STICK_DEADZONE) | ||
495 | analog_v -= PAD_STICK_DEADZONE; | ||
496 | else if (analog_v < -PAD_STICK_DEADZONE) | ||
497 | analog_v += PAD_STICK_DEADZONE; | ||
498 | else | ||
499 | analog_v = 0; | ||
500 | analog_v /= 10; | ||
501 | |||
502 | if (analog_h != 0 || analog_v != 0) | ||
503 | _ecore_psl1ght_mouse_move (analog_h, analog_v); | ||
504 | |||
505 | if (pad_old_x != pad_data.BTN_CROSS) | ||
506 | _ecore_psl1ght_mouse_button (1, pad_data.BTN_CROSS); | ||
507 | if (pad_old_o != pad_data.BTN_CIRCLE) | ||
508 | _ecore_psl1ght_mouse_button (3, pad_data.BTN_CIRCLE); | ||
509 | |||
510 | pad_old_x = pad_data.BTN_CROSS; | ||
511 | pad_old_o = pad_data.BTN_CIRCLE; | ||
512 | |||
513 | //pad_buttons = paddata.buttons; | ||
514 | } | ||
515 | } | ||
516 | } | ||
517 | |||
518 | static void | ||
519 | _ecore_psl1ght_poll_mouse(void) | ||
520 | { | ||
521 | mouseInfo mouseinfo; | ||
522 | u32 i; | ||
523 | |||
524 | /**/ | ||
525 | /* Check mouse events */ | ||
526 | ioMouseGetInfo(&mouseinfo); | ||
527 | |||
528 | if (mouseinfo.status[0] == 1 && !mouse_connected) // Connected | ||
529 | { | ||
530 | mouse_connected = TRUE; | ||
531 | mouse_buttons = 0; | ||
532 | |||
533 | // Old events in the queue are discarded | ||
534 | ioMouseClearBuf(0); | ||
535 | } | ||
536 | else if (mouseinfo.status[0] != 1 && mouse_connected) // Disconnected | ||
537 | { | ||
538 | mouse_connected = FALSE; | ||
539 | mouse_buttons = 0; | ||
540 | } | ||
541 | |||
542 | if (mouse_connected) | ||
543 | { | ||
544 | mouseDataList datalist; | ||
545 | |||
546 | ioMouseGetDataList(0, &datalist); | ||
547 | |||
548 | for (i = 0; i < datalist.count; i++) | ||
549 | { | ||
550 | u8 old_left = mouse_buttons & 1; | ||
551 | u8 new_left = datalist.list[i].buttons & 1; | ||
552 | u8 old_right = mouse_buttons & 2; | ||
553 | u8 new_right = datalist.list[i].buttons & 2; | ||
554 | u8 old_middle = mouse_buttons & 4; | ||
555 | u8 new_middle = datalist.list[i].buttons & 4; | ||
556 | |||
557 | if (datalist.list[i].x_axis != 0 || | ||
558 | datalist.list[i].y_axis != 0) | ||
559 | _ecore_psl1ght_mouse_move (datalist.list[i].x_axis, | ||
560 | datalist.list[i].y_axis); | ||
561 | |||
562 | if (old_left != new_left) | ||
563 | _ecore_psl1ght_mouse_button (1, new_left); | ||
564 | if (old_middle != new_middle) | ||
565 | _ecore_psl1ght_mouse_button (2, new_middle); | ||
566 | if (old_right != new_right) | ||
567 | _ecore_psl1ght_mouse_button (3, new_right); | ||
568 | |||
569 | if (datalist.list[i].wheel != 0) | ||
570 | _ecore_psl1ght_mouse_wheel (datalist.list[i].wheel, | ||
571 | datalist.list[i].tilt); | ||
572 | |||
573 | mouse_buttons = datalist.list[i].buttons; | ||
574 | } | ||
575 | } | ||
576 | } | ||
577 | |||
578 | static void | ||
579 | _ecore_psl1ght_poll_move(void) | ||
580 | { | ||
581 | int i; | ||
582 | u16 new_buttons; | ||
583 | static int t_pressed = 0; | ||
584 | static int calibrated = 0; | ||
585 | static float prev_x = 0; | ||
586 | static float prev_y = 0; | ||
587 | static int gyro = 0; | ||
588 | float x, y, z; | ||
589 | |||
590 | /* Check move events */ | ||
591 | processMove (move_context); | ||
592 | new_buttons = move_context->state.paddata.buttons & (~move_buttons); | ||
593 | move_buttons = move_context->state.paddata.buttons; | ||
594 | |||
595 | moveGet3DPosition (move_context, &x, &y, &z); | ||
596 | //printf ("Move 3D position is : %f, %f, %f\n", x,y,z); | ||
597 | |||
598 | switch (new_buttons) { | ||
599 | case 1: | ||
600 | gyro = !gyro; | ||
601 | break; | ||
602 | |||
603 | case 4: | ||
604 | // Move button | ||
605 | printf ("Calibrating\n"); | ||
606 | gemCalibrate (0); | ||
607 | calibrated = 1; | ||
608 | break; | ||
609 | |||
610 | case 8: | ||
611 | // start button | ||
612 | _ecore_psl1ght_mouse_move ((window_width / 2) - mouse_x, (window_height / 2) - mouse_y); | ||
613 | break; | ||
614 | } | ||
615 | |||
616 | if (calibrated) | ||
617 | { | ||
618 | float x_axis, y_axis; | ||
619 | |||
620 | if (gyro) | ||
621 | { | ||
622 | gemInertialState gem_inert; | ||
623 | |||
624 | gemGetInertialState (0, 0, 0, &gem_inert); | ||
625 | x_axis = -vec_array (gem_inert.gyro, 1) * 25; | ||
626 | y_axis = -vec_array (gem_inert.gyro, 0) * 25; | ||
627 | if (abs (x_axis) > 2 || abs (y_axis) > 2) | ||
628 | _ecore_psl1ght_mouse_move (x_axis, y_axis); | ||
629 | } | ||
630 | else { | ||
631 | x_axis = (x - prev_x) * 2.5; | ||
632 | y_axis = -(y - prev_y) * 2.5; | ||
633 | prev_x = x; | ||
634 | prev_y = y; | ||
635 | _ecore_psl1ght_mouse_move (x_axis, y_axis); | ||
636 | } | ||
637 | |||
638 | if (!t_pressed && (move_buttons & 0x2)) | ||
639 | _ecore_psl1ght_mouse_button (1, 1); | ||
640 | else if (t_pressed && (move_buttons & 0x2) == 0) | ||
641 | _ecore_psl1ght_mouse_button (1, 0); | ||
642 | t_pressed = move_buttons & 0x2; | ||
643 | } | ||
644 | } | ||
645 | |||
646 | static void | ||
647 | _ecore_psl1ght_poll_keyboard(void) | ||
648 | { | ||
649 | KbInfo kbInfo; | ||
650 | int i; | ||
651 | |||
652 | /* Check keyboard events */ | ||
653 | ioKbGetInfo(&kbInfo); | ||
654 | |||
655 | if (kbInfo.status[0] == 1 && !keyboard_connected) | ||
656 | { | ||
657 | /* Connected */ | ||
658 | keyboard_connected = true; | ||
659 | |||
660 | // Old events in the queue are discarded | ||
661 | ioKbClearBuf(0); | ||
662 | keyboard_leds._KbLedU.leds = 0; | ||
663 | keyboard_mods._KbMkeyU.mkeys = 0; | ||
664 | keyboard_old_key = 0; | ||
665 | |||
666 | // Set raw keyboard code types to get scan codes | ||
667 | ioKbSetCodeType(0, KB_CODETYPE_RAW); | ||
668 | ioKbSetReadMode(0, KB_RMODE_INPUTCHAR); | ||
669 | } | ||
670 | else if (kbInfo.status[0] != 1 && keyboard_connected) | ||
671 | { | ||
672 | /* Disconnected keyboard */ | ||
673 | keyboard_connected = FALSE; | ||
674 | } | ||
675 | |||
676 | if (keyboard_connected) | ||
677 | { | ||
678 | KbData Keys; | ||
679 | |||
680 | // Read data from the keyboard buffer | ||
681 | if (ioKbRead(0, &Keys) == 0 && Keys.nb_keycode > 0) | ||
682 | { | ||
683 | Ecore_Event_Key *ev = NULL; | ||
684 | |||
685 | _ecore_psl1ght_key_modifiers (&Keys.mkey, &Keys.led); | ||
686 | |||
687 | if (Keys.nb_keycode == 0 && keyboard_old_key != 0) | ||
688 | { | ||
689 | ev = _ecore_psl1ght_event_key (keyboard_old_key); | ||
690 | if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL); | ||
691 | } | ||
692 | for (i = 0; i < Keys.nb_keycode; i++) | ||
693 | { | ||
694 | if (Keys.keycode[i] != keyboard_old_key) | ||
695 | { | ||
696 | if (Keys.keycode[i] != 0) | ||
697 | { | ||
698 | ev = _ecore_psl1ght_event_key (Keys.keycode[i]); | ||
699 | if (ev) | ||
700 | ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, | ||
701 | NULL, NULL); | ||
702 | } | ||
703 | else | ||
704 | { | ||
705 | ev = _ecore_psl1ght_event_key (keyboard_old_key); | ||
706 | if (ev) | ||
707 | ecore_event_add(ECORE_EVENT_KEY_UP, ev, | ||
708 | NULL, NULL); | ||
709 | } | ||
710 | keyboard_old_key = Keys.keycode[0]; | ||
711 | } | ||
712 | } | ||
713 | } | ||
714 | } | ||
715 | } | ||
716 | |||
717 | static void | ||
718 | xmb_event_handler(u64 status, u64 param, void *user_data) | ||
719 | { | ||
720 | printf ("Received event %lX\n", status); | ||
721 | if (status == SYSUTIL_EXIT_GAME) | ||
722 | { | ||
723 | ecore_main_loop_quit(); | ||
724 | } | ||
725 | else if (status == SYSUTIL_MENU_OPEN) | ||
726 | { | ||
727 | } | ||
728 | else if (status == SYSUTIL_MENU_CLOSE) | ||
729 | { | ||
730 | } | ||
731 | else if (status == SYSUTIL_DRAW_BEGIN) | ||
732 | { | ||
733 | } | ||
734 | else if (status == SYSUTIL_DRAW_END) | ||
735 | { | ||
736 | } | ||
737 | } | ||
738 | |||
739 | EAPI void | ||
740 | ecore_psl1ght_poll_events(void) | ||
741 | { | ||
742 | _ecore_psl1ght_poll_joypad (); | ||
743 | _ecore_psl1ght_poll_mouse (); | ||
744 | if (move_context) | ||
745 | _ecore_psl1ght_poll_move (); | ||
746 | _ecore_psl1ght_poll_keyboard (); | ||
747 | |||
748 | sysUtilCheckCallback (); | ||
749 | } | ||
750 | |||
751 | EAPI void | ||
752 | ecore_psl1ght_resolution_set(int width, int height) | ||
753 | { | ||
754 | window_width = width; | ||
755 | window_height = height; | ||
756 | if (mouse_x > window_width) mouse_x = window_width; | ||
757 | if (mouse_y > window_height) mouse_y = window_height; | ||
758 | } | ||
759 | |||
760 | EAPI void | ||
761 | ecore_psl1ght_screen_resolution_get(int *w, int *h) | ||
762 | { | ||
763 | videoState state; | ||
764 | videoResolution resolution; | ||
765 | |||
766 | /* Get the state of the display */ | ||
767 | if (videoGetState (0, 0, &state) == 0 && | ||
768 | videoGetResolution (state.displayMode.resolution, &resolution) == 0) | ||
769 | { | ||
770 | if (w) *w = resolution.width; | ||
771 | if (h) *h = resolution.height; | ||
772 | } | ||
773 | else { | ||
774 | if (w) *w = 0; | ||
775 | if (h) *h = 0; | ||
776 | } | ||
777 | } | ||
778 | |||
779 | EAPI void | ||
780 | ecore_psl1ght_optimal_screen_resolution_get(int *w, int *h) | ||
781 | { | ||
782 | videoDeviceInfo info; | ||
783 | videoResolution res; | ||
784 | int area = 720 * 480; | ||
785 | int mode_area; | ||
786 | int i; | ||
787 | |||
788 | if (w) *w = 720; | ||
789 | if (h) *h = 480; | ||
790 | |||
791 | videoGetDeviceInfo(0, 0, &info); | ||
792 | |||
793 | for (i = 0; i < info.availableModeCount; i++) { | ||
794 | videoGetResolution (info.availableModes[i].resolution, &res); | ||
795 | mode_area = res.width * res.height; | ||
796 | if (mode_area > area) | ||
797 | { | ||
798 | area = mode_area; | ||
799 | if (w) *w = res.width; | ||
800 | if (h) *h = res.height; | ||
801 | } | ||
802 | } | ||
803 | } | ||
804 | |||