PlatformBlackBerry.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. #ifdef __QNX__
  2. #include "Base.h"
  3. #include "Platform.h"
  4. #include "FileSystem.h"
  5. #include "Game.h"
  6. #include "Form.h"
  7. #include "ScriptController.h"
  8. #include <unistd.h>
  9. #include <sys/keycodes.h>
  10. #include <screen/screen.h>
  11. #include <input/screen_helpers.h>
  12. #include <gestures/set.h>
  13. #include <gestures/swipe.h>
  14. #include <gestures/pinch.h>
  15. #include <gestures/tap.h>
  16. #include <bps/bps.h>
  17. #include <bps/event.h>
  18. #include <bps/screen.h>
  19. #include <bps/navigator.h>
  20. #include <bps/sensor.h>
  21. #include <bps/orientation.h>
  22. #include <bps/virtualkeyboard.h>
  23. #define TOUCH_COUNT_MAX 4
  24. using namespace std;
  25. struct timespec __timespec;
  26. static double __timeStart;
  27. static double __timeAbsolute;
  28. static bool __vsync = WINDOW_VSYNC;
  29. static screen_context_t __screenContext;
  30. static screen_window_t __screenWindow;
  31. static screen_event_t __screenEvent;
  32. static int __screenWindowSize[2];
  33. static bool __screenFullscreen = false;
  34. static EGLDisplay __eglDisplay = EGL_NO_DISPLAY;
  35. static EGLContext __eglContext = EGL_NO_CONTEXT;
  36. static EGLSurface __eglSurface = EGL_NO_SURFACE;
  37. static EGLConfig __eglConfig = 0;
  38. static int __orientationAngle;
  39. static bool __multiTouch = false;
  40. static float __pitch;
  41. static float __roll;
  42. static const char* __glExtensions;
  43. static struct gestures_set * __gestureSet;
  44. static bitset<3> __gestureEventsProcessed;
  45. static bool __gestureSwipeRecognized = false;
  46. PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray = NULL;
  47. PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays = NULL;
  48. PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays = NULL;
  49. PFNGLISVERTEXARRAYOESPROC glIsVertexArray = NULL;
  50. namespace gameplay
  51. {
  52. // Gets the Keyboard::Key enumeration constant that corresponds to the given QNX key code.
  53. static Keyboard::Key getKey(int qnxKeycode)
  54. {
  55. switch (qnxKeycode)
  56. {
  57. case KEYCODE_SYSREQ:
  58. return Keyboard::KEY_SYSREQ;
  59. case KEYCODE_BREAK:
  60. return Keyboard::KEY_BREAK;
  61. case KEYCODE_MENU:
  62. return Keyboard::KEY_MENU;
  63. case KEYCODE_KP_ENTER:
  64. return Keyboard::KEY_KP_ENTER;
  65. case KEYCODE_PAUSE:
  66. return Keyboard::KEY_PAUSE;
  67. case KEYCODE_SCROLL_LOCK:
  68. return Keyboard::KEY_SCROLL_LOCK;
  69. case KEYCODE_PRINT:
  70. return Keyboard::KEY_PRINT;
  71. case KEYCODE_ESCAPE:
  72. return Keyboard::KEY_ESCAPE;
  73. case KEYCODE_BACKSPACE:
  74. return Keyboard::KEY_BACKSPACE;
  75. case KEYCODE_BACK_TAB:
  76. return Keyboard::KEY_BACK_TAB;
  77. case KEYCODE_TAB:
  78. return Keyboard::KEY_TAB;
  79. case KEYCODE_RETURN:
  80. return Keyboard::KEY_RETURN;
  81. case KEYCODE_CAPS_LOCK:
  82. return Keyboard::KEY_CAPS_LOCK;
  83. case KEYCODE_LEFT_SHIFT:
  84. case KEYCODE_RIGHT_SHIFT:
  85. return Keyboard::KEY_SHIFT;
  86. case KEYCODE_LEFT_CTRL:
  87. case KEYCODE_RIGHT_CTRL:
  88. return Keyboard::KEY_CTRL;
  89. case KEYCODE_LEFT_ALT:
  90. case KEYCODE_RIGHT_ALT:
  91. return Keyboard::KEY_ALT;
  92. case KEYCODE_LEFT_HYPER:
  93. case KEYCODE_RIGHT_HYPER:
  94. return Keyboard::KEY_HYPER;
  95. case KEYCODE_INSERT:
  96. return Keyboard::KEY_INSERT;
  97. case KEYCODE_HOME:
  98. return Keyboard::KEY_HOME;
  99. case KEYCODE_PG_UP:
  100. return Keyboard::KEY_PG_UP;
  101. case KEYCODE_DELETE:
  102. return Keyboard::KEY_DELETE;
  103. case KEYCODE_END:
  104. return Keyboard::KEY_END;
  105. case KEYCODE_PG_DOWN:
  106. return Keyboard::KEY_PG_DOWN;
  107. case KEYCODE_LEFT:
  108. return Keyboard::KEY_LEFT_ARROW;
  109. case KEYCODE_RIGHT:
  110. return Keyboard::KEY_RIGHT_ARROW;
  111. case KEYCODE_UP:
  112. return Keyboard::KEY_UP_ARROW;
  113. case KEYCODE_DOWN:
  114. return Keyboard::KEY_DOWN_ARROW;
  115. case KEYCODE_NUM_LOCK:
  116. return Keyboard::KEY_NUM_LOCK;
  117. case KEYCODE_KP_PLUS:
  118. return Keyboard::KEY_KP_PLUS;
  119. case KEYCODE_KP_MINUS:
  120. return Keyboard::KEY_KP_MINUS;
  121. case KEYCODE_KP_MULTIPLY:
  122. return Keyboard::KEY_KP_MULTIPLY;
  123. case KEYCODE_KP_DIVIDE:
  124. return Keyboard::KEY_KP_DIVIDE;
  125. case KEYCODE_KP_HOME:
  126. return Keyboard::KEY_KP_HOME;
  127. case KEYCODE_KP_UP:
  128. return Keyboard::KEY_KP_UP;
  129. case KEYCODE_KP_PG_UP:
  130. return Keyboard::KEY_KP_PG_UP;
  131. case KEYCODE_KP_LEFT:
  132. return Keyboard::KEY_KP_LEFT;
  133. case KEYCODE_KP_FIVE:
  134. return Keyboard::KEY_KP_FIVE;
  135. case KEYCODE_KP_RIGHT:
  136. return Keyboard::KEY_KP_RIGHT;
  137. case KEYCODE_KP_END:
  138. return Keyboard::KEY_KP_END;
  139. case KEYCODE_KP_DOWN:
  140. return Keyboard::KEY_KP_DOWN;
  141. case KEYCODE_KP_PG_DOWN:
  142. return Keyboard::KEY_KP_PG_DOWN;
  143. case KEYCODE_KP_INSERT:
  144. return Keyboard::KEY_KP_INSERT;
  145. case KEYCODE_KP_DELETE:
  146. return Keyboard::KEY_KP_DELETE;
  147. case KEYCODE_F1:
  148. return Keyboard::KEY_F1;
  149. case KEYCODE_F2:
  150. return Keyboard::KEY_F2;
  151. case KEYCODE_F3:
  152. return Keyboard::KEY_F3;
  153. case KEYCODE_F4:
  154. return Keyboard::KEY_F4;
  155. case KEYCODE_F5:
  156. return Keyboard::KEY_F5;
  157. case KEYCODE_F6:
  158. return Keyboard::KEY_F6;
  159. case KEYCODE_F7:
  160. return Keyboard::KEY_F7;
  161. case KEYCODE_F8:
  162. return Keyboard::KEY_F8;
  163. case KEYCODE_F9:
  164. return Keyboard::KEY_F9;
  165. case KEYCODE_F10:
  166. return Keyboard::KEY_F10;
  167. case KEYCODE_F11:
  168. return Keyboard::KEY_F11;
  169. case KEYCODE_F12:
  170. return Keyboard::KEY_F12;
  171. case KEYCODE_SPACE:
  172. return Keyboard::KEY_SPACE;
  173. case KEYCODE_RIGHT_PAREN:
  174. return Keyboard::KEY_RIGHT_PARENTHESIS;
  175. case KEYCODE_ZERO:
  176. return Keyboard::KEY_ZERO;
  177. case KEYCODE_EXCLAM:
  178. return Keyboard::KEY_EXCLAM;
  179. case KEYCODE_ONE:
  180. return Keyboard::KEY_ONE;
  181. case KEYCODE_AT:
  182. return Keyboard::KEY_AT;
  183. case KEYCODE_TWO:
  184. return Keyboard::KEY_TWO;
  185. case KEYCODE_NUMBER:
  186. return Keyboard::KEY_NUMBER;
  187. case KEYCODE_THREE:
  188. return Keyboard::KEY_THREE;
  189. case KEYCODE_DOLLAR:
  190. return Keyboard::KEY_DOLLAR;
  191. case KEYCODE_FOUR:
  192. return Keyboard::KEY_FOUR;
  193. case KEYCODE_PERCENT:
  194. return Keyboard::KEY_PERCENT;
  195. case KEYCODE_FIVE:
  196. return Keyboard::KEY_FIVE;
  197. case KEYCODE_CIRCUMFLEX:
  198. return Keyboard::KEY_CIRCUMFLEX;
  199. case KEYCODE_SIX:
  200. return Keyboard::KEY_SIX;
  201. case KEYCODE_AMPERSAND:
  202. return Keyboard::KEY_AMPERSAND;
  203. case KEYCODE_SEVEN:
  204. return Keyboard::KEY_SEVEN;
  205. case KEYCODE_ASTERISK:
  206. return Keyboard::KEY_ASTERISK;
  207. case KEYCODE_EIGHT:
  208. return Keyboard::KEY_EIGHT;
  209. case KEYCODE_LEFT_PAREN:
  210. return Keyboard::KEY_LEFT_PARENTHESIS;
  211. case KEYCODE_NINE:
  212. return Keyboard::KEY_NINE;
  213. case KEYCODE_EQUAL:
  214. return Keyboard::KEY_EQUAL;
  215. case KEYCODE_PLUS:
  216. return Keyboard::KEY_PLUS;
  217. case KEYCODE_LESS_THAN:
  218. return Keyboard::KEY_LESS_THAN;
  219. case KEYCODE_COMMA:
  220. return Keyboard::KEY_COMMA;
  221. case KEYCODE_UNDERSCORE:
  222. return Keyboard::KEY_UNDERSCORE;
  223. case KEYCODE_MINUS:
  224. return Keyboard::KEY_MINUS;
  225. case KEYCODE_GREATER_THAN:
  226. return Keyboard::KEY_GREATER_THAN;
  227. case KEYCODE_PERIOD:
  228. return Keyboard::KEY_PERIOD;
  229. case KEYCODE_COLON:
  230. return Keyboard::KEY_COLON;
  231. case KEYCODE_SEMICOLON:
  232. return Keyboard::KEY_SEMICOLON;
  233. case KEYCODE_QUESTION:
  234. return Keyboard::KEY_QUESTION;
  235. case KEYCODE_SLASH:
  236. return Keyboard::KEY_SLASH;
  237. case KEYCODE_GRAVE:
  238. return Keyboard::KEY_GRAVE;
  239. case KEYCODE_TILDE:
  240. return Keyboard::KEY_TILDE;
  241. case KEYCODE_LEFT_BRACE:
  242. return Keyboard::KEY_LEFT_BRACE;
  243. case KEYCODE_LEFT_BRACKET:
  244. return Keyboard::KEY_LEFT_BRACKET;
  245. case KEYCODE_BAR:
  246. return Keyboard::KEY_BAR;
  247. case KEYCODE_BACK_SLASH:
  248. return Keyboard::KEY_BACK_SLASH;
  249. case KEYCODE_RIGHT_BRACE:
  250. return Keyboard::KEY_RIGHT_BRACE;
  251. case KEYCODE_RIGHT_BRACKET:
  252. return Keyboard::KEY_RIGHT_BRACKET;
  253. case KEYCODE_QUOTE:
  254. return Keyboard::KEY_QUOTE;
  255. case KEYCODE_APOSTROPHE:
  256. return Keyboard::KEY_APOSTROPHE;
  257. case 0x20AC:
  258. return Keyboard::KEY_EURO;
  259. case KEYCODE_POUND_SIGN:
  260. return Keyboard::KEY_POUND;
  261. case KEYCODE_YEN_SIGN:
  262. return Keyboard::KEY_YEN;
  263. case KEYCODE_MIDDLE_DOT:
  264. return Keyboard::KEY_MIDDLE_DOT;
  265. case KEYCODE_CAPITAL_A:
  266. return Keyboard::KEY_CAPITAL_A;
  267. case KEYCODE_A:
  268. return Keyboard::KEY_A;
  269. case KEYCODE_CAPITAL_B:
  270. return Keyboard::KEY_CAPITAL_B;
  271. case KEYCODE_B:
  272. return Keyboard::KEY_B;
  273. case KEYCODE_CAPITAL_C:
  274. return Keyboard::KEY_CAPITAL_C;
  275. case KEYCODE_C:
  276. return Keyboard::KEY_C;
  277. case KEYCODE_CAPITAL_D:
  278. return Keyboard::KEY_CAPITAL_D;
  279. case KEYCODE_D:
  280. return Keyboard::KEY_D;
  281. case KEYCODE_CAPITAL_E:
  282. return Keyboard::KEY_CAPITAL_E;
  283. case KEYCODE_E:
  284. return Keyboard::KEY_E;
  285. case KEYCODE_CAPITAL_F:
  286. return Keyboard::KEY_CAPITAL_F;
  287. case KEYCODE_F:
  288. return Keyboard::KEY_F;
  289. case KEYCODE_CAPITAL_G:
  290. return Keyboard::KEY_CAPITAL_G;
  291. case KEYCODE_G:
  292. return Keyboard::KEY_G;
  293. case KEYCODE_CAPITAL_H:
  294. return Keyboard::KEY_CAPITAL_H;
  295. case KEYCODE_H:
  296. return Keyboard::KEY_H;
  297. case KEYCODE_CAPITAL_I:
  298. return Keyboard::KEY_CAPITAL_I;
  299. case KEYCODE_I:
  300. return Keyboard::KEY_I;
  301. case KEYCODE_CAPITAL_J:
  302. return Keyboard::KEY_CAPITAL_J;
  303. case KEYCODE_J:
  304. return Keyboard::KEY_J;
  305. case KEYCODE_CAPITAL_K:
  306. return Keyboard::KEY_CAPITAL_K;
  307. case KEYCODE_K:
  308. return Keyboard::KEY_K;
  309. case KEYCODE_CAPITAL_L:
  310. return Keyboard::KEY_CAPITAL_L;
  311. case KEYCODE_L:
  312. return Keyboard::KEY_L;
  313. case KEYCODE_CAPITAL_M:
  314. return Keyboard::KEY_CAPITAL_M;
  315. case KEYCODE_M:
  316. return Keyboard::KEY_M;
  317. case KEYCODE_CAPITAL_N:
  318. return Keyboard::KEY_CAPITAL_N;
  319. case KEYCODE_N:
  320. return Keyboard::KEY_N;
  321. case KEYCODE_CAPITAL_O:
  322. return Keyboard::KEY_CAPITAL_O;
  323. case KEYCODE_O:
  324. return Keyboard::KEY_O;
  325. case KEYCODE_CAPITAL_P:
  326. return Keyboard::KEY_CAPITAL_P;
  327. case KEYCODE_P:
  328. return Keyboard::KEY_P;
  329. case KEYCODE_CAPITAL_Q:
  330. return Keyboard::KEY_CAPITAL_Q;
  331. case KEYCODE_Q:
  332. return Keyboard::KEY_Q;
  333. case KEYCODE_CAPITAL_R:
  334. return Keyboard::KEY_CAPITAL_R;
  335. case KEYCODE_R:
  336. return Keyboard::KEY_R;
  337. case KEYCODE_CAPITAL_S:
  338. return Keyboard::KEY_CAPITAL_S;
  339. case KEYCODE_S:
  340. return Keyboard::KEY_S;
  341. case KEYCODE_CAPITAL_T:
  342. return Keyboard::KEY_CAPITAL_T;
  343. case KEYCODE_T:
  344. return Keyboard::KEY_T;
  345. case KEYCODE_CAPITAL_U:
  346. return Keyboard::KEY_CAPITAL_U;
  347. case KEYCODE_U:
  348. return Keyboard::KEY_U;
  349. case KEYCODE_CAPITAL_V:
  350. return Keyboard::KEY_CAPITAL_V;
  351. case KEYCODE_V:
  352. return Keyboard::KEY_V;
  353. case KEYCODE_CAPITAL_W:
  354. return Keyboard::KEY_CAPITAL_W;
  355. case KEYCODE_W:
  356. return Keyboard::KEY_W;
  357. case KEYCODE_CAPITAL_X:
  358. return Keyboard::KEY_CAPITAL_X;
  359. case KEYCODE_X:
  360. return Keyboard::KEY_X;
  361. case KEYCODE_CAPITAL_Y:
  362. return Keyboard::KEY_CAPITAL_Y;
  363. case KEYCODE_Y:
  364. return Keyboard::KEY_Y;
  365. case KEYCODE_CAPITAL_Z:
  366. return Keyboard::KEY_CAPITAL_Z;
  367. case KEYCODE_Z:
  368. return Keyboard::KEY_Z;
  369. default:
  370. return Keyboard::KEY_NONE;
  371. }
  372. }
  373. /**
  374. * Returns the unicode value from the given QNX key code value.
  375. * Some non-printable characters also have corresponding unicode values, such as backspace.
  376. *
  377. * @param qnxKeyCode The keyboard key code.
  378. *
  379. * @return The unicode value or 0 if the keycode did not represent a unicode key.
  380. */
  381. static int getUnicode(int qnxKeyCode)
  382. {
  383. if (qnxKeyCode >= KEYCODE_PC_KEYS && qnxKeyCode <= UNICODE_PRIVATE_USE_AREA_LAST)
  384. {
  385. switch (qnxKeyCode)
  386. {
  387. case KEYCODE_BACKSPACE:
  388. return 0x0008;
  389. case KEYCODE_TAB:
  390. return 0x0009;
  391. case KEYCODE_KP_ENTER:
  392. case KEYCODE_RETURN:
  393. return 0x000A;
  394. case KEYCODE_ESCAPE:
  395. return 0x001B;
  396. // Win32 doesn't consider delete to be a key char.
  397. default:
  398. return 0;
  399. }
  400. }
  401. return qnxKeyCode;
  402. }
  403. extern void print(const char* format, ...)
  404. {
  405. GP_ASSERT(format);
  406. va_list argptr;
  407. va_start(argptr, format);
  408. vfprintf(stderr, format, argptr);
  409. va_end(argptr);
  410. }
  411. EGLenum checkErrorEGL(const char* msg)
  412. {
  413. GP_ASSERT(msg);
  414. static const char* errmsg[] =
  415. {
  416. "EGL function failed",
  417. "EGL is not initialized, or could not be initialized, for the specified display",
  418. "EGL cannot access a requested resource",
  419. "EGL failed to allocate resources for the requested operation",
  420. "EGL fail to access an unrecognized attribute or attribute value was passed in an attribute list",
  421. "EGLConfig argument does not name a valid EGLConfig",
  422. "EGLContext argument does not name a valid EGLContext",
  423. "EGL current surface of the calling thread is no longer valid",
  424. "EGLDisplay argument does not name a valid EGLDisplay",
  425. "EGL arguments are inconsistent",
  426. "EGLNativePixmapType argument does not refer to a valid native pixmap",
  427. "EGLNativeWindowType argument does not refer to a valid native window",
  428. "EGL one or more argument values are invalid",
  429. "EGLSurface argument does not name a valid surface configured for rendering",
  430. "EGL power management event has occurred",
  431. };
  432. EGLenum error = eglGetError();
  433. fprintf(stderr, "%s: %s\n", msg, errmsg[error - EGL_SUCCESS]);
  434. return error;
  435. }
  436. void gesture_callback(gesture_base_t* gesture, mtouch_event_t* event, void* param, int async)
  437. {
  438. switch (gesture->type)
  439. {
  440. case GESTURE_SWIPE:
  441. {
  442. if ( __gestureEventsProcessed.test(Gesture::GESTURE_SWIPE) )
  443. {
  444. gesture_swipe_t* swipe = (gesture_swipe_t*)gesture;
  445. if (!__gestureSwipeRecognized)
  446. {
  447. Game::getInstance()->gestureSwipeEvent(swipe->coords.x, swipe->coords.y, swipe->direction);
  448. __gestureSwipeRecognized = true;
  449. }
  450. }
  451. break;
  452. }
  453. case GESTURE_PINCH:
  454. {
  455. if ( __gestureEventsProcessed.test(Gesture::GESTURE_PINCH) )
  456. {
  457. gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
  458. float dist_x = (float)pinch->last_distance.x - (float)pinch->distance.x;
  459. float dist_y = (float)pinch->last_distance.y - (float)pinch->distance.y;
  460. float scale = sqrt( (dist_x * dist_x) + (dist_y * dist_y) );
  461. Game::getInstance()->gesturePinchEvent(pinch->centroid.x, pinch->centroid.y, scale);
  462. }
  463. break;
  464. }
  465. case GESTURE_TAP:
  466. {
  467. if ( __gestureEventsProcessed.test(Gesture::GESTURE_TAP) )
  468. {
  469. gesture_tap_t* tap = (gesture_tap_t*)gesture;
  470. Game::getInstance()->gestureTapEvent(tap->touch_coords.x, tap->touch_coords.y);
  471. }
  472. break;
  473. }
  474. default:
  475. break;
  476. }
  477. }
  478. #ifdef USE_BLACKBERRY_GAMEPAD
  479. static const char* __vendorStrings[] =
  480. {
  481. "SteelSeries",
  482. "Nintendo",
  483. };
  484. static const char* __productStrings[] =
  485. {
  486. "FREE",
  487. "Wii Remote",
  488. };
  489. static const int __VIDs[] = {
  490. 0x1038,
  491. 0x057e,
  492. };
  493. static const int __PIDs[] = {
  494. 0x1412,
  495. 0x0306,
  496. };
  497. static const unsigned int __knownGamepads = 2;
  498. void queryGamepad(GamepadHandle handle, int* buttonCount, int* joystickCount, int* productId, int* vendorId, char* productString, char* vendorString)
  499. {
  500. char id[128];
  501. screen_get_device_property_iv(handle, SCREEN_PROPERTY_BUTTON_COUNT, buttonCount);
  502. screen_get_device_property_cv(handle, SCREEN_PROPERTY_ID_STRING, 128, id);
  503. screen_get_device_property_cv(handle, SCREEN_PROPERTY_PRODUCT, 64, productString);
  504. screen_get_device_property_cv(handle, SCREEN_PROPERTY_VENDOR, 64, vendorString);
  505. // Check for the existence of analog sticks.
  506. int analogs[3];
  507. if (!screen_get_device_property_iv(handle, SCREEN_PROPERTY_ANALOG0, analogs))
  508. {
  509. ++(*joystickCount);
  510. }
  511. if (!screen_get_device_property_iv(handle, SCREEN_PROPERTY_ANALOG1, analogs))
  512. {
  513. ++(*joystickCount);
  514. }
  515. // ID string format: A-BBBB-CCCC-D.D
  516. // A is the device's index
  517. // BBBB is the device's Vendor ID (in hexadecimal)
  518. // CCCC is the device's Product ID (also in hexadecimal)
  519. // D.D is the device's version number
  520. char* token = strtok(id, "-");
  521. token = strtok(NULL, "-");
  522. if (token)
  523. {
  524. *vendorId = strtol(token, NULL, 16);
  525. }
  526. token = strtok(NULL, "-");
  527. if (token)
  528. {
  529. *productId = strtol(token, NULL, 16);
  530. }
  531. // For gamepads unknown to BB10,
  532. // check VID and PID against gamepads known to gameplay.
  533. if (strlen(productString) == 0 || strlen(vendorString) == 0)
  534. {
  535. for (unsigned int i = 0; i < __knownGamepads; ++i)
  536. {
  537. if (__VIDs[i] == *vendorId && __PIDs[i] == *productId)
  538. {
  539. strcpy(vendorString, __vendorStrings[i]);
  540. strcpy(productString, __productStrings[i]);
  541. }
  542. }
  543. }
  544. }
  545. void Platform::pollGamepadState(Gamepad* gamepad)
  546. {
  547. screen_get_device_property_iv(gamepad->_handle, SCREEN_PROPERTY_BUTTONS, (int*)&gamepad->_buttons);
  548. unsigned int i;
  549. for (i = 0; i < gamepad->_joystickCount; ++i)
  550. {
  551. GP_ASSERT(i < 2);
  552. int analog[3];
  553. switch (i)
  554. {
  555. case 0:
  556. screen_get_device_property_iv(gamepad->_handle, SCREEN_PROPERTY_ANALOG0, analog);
  557. break;
  558. case 1:
  559. screen_get_device_property_iv(gamepad->_handle, SCREEN_PROPERTY_ANALOG1, analog);
  560. break;
  561. }
  562. // So far we've tested two gamepads with analog sticks on BlackBerry:
  563. // the SteelSeries FREE, and the iControlPad.
  564. // Both return values between -128 and +127, with the y axis starting from
  565. // the top at -128.
  566. // 1 / 128 == 0.0078125f
  567. // 1 / 127 == 0.0078740157480315f
  568. float x = (float)analog[0];
  569. float y = -(float)analog[1];
  570. x *= (x < 0) ? 0.0078125f : 0.0078740157480315f;
  571. y *= (y > 0) ? 0.0078125f : 0.0078740157480315f;
  572. gamepad->_joysticks[i].set(x, y);
  573. }
  574. for (i = 0; i < gamepad->_triggerCount; ++i)
  575. {
  576. GP_ASSERT(i < 2);
  577. int analog[3];
  578. switch (i)
  579. {
  580. case 0:
  581. screen_get_device_property_iv(gamepad->_handle, SCREEN_PROPERTY_ANALOG0, analog);
  582. break;
  583. case 1:
  584. screen_get_device_property_iv(gamepad->_handle, SCREEN_PROPERTY_ANALOG1, analog);
  585. break;
  586. }
  587. float value = (float)analog[2] * 0.0078125f;
  588. gamepad->_triggers[i] = value;
  589. }
  590. }
  591. #else
  592. void Platform::pollGamepadState(Gamepad* gamepad)
  593. {
  594. }
  595. #endif
  596. Platform::Platform(Game* game)
  597. : _game(game)
  598. {
  599. }
  600. Platform::~Platform()
  601. {
  602. if (__eglDisplay != EGL_NO_DISPLAY)
  603. {
  604. eglMakeCurrent(__eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  605. }
  606. if (__eglSurface != EGL_NO_SURFACE)
  607. {
  608. eglDestroySurface(__eglDisplay, __eglSurface);
  609. __eglSurface = EGL_NO_SURFACE;
  610. }
  611. if (__eglContext != EGL_NO_CONTEXT)
  612. {
  613. eglDestroyContext(__eglDisplay, __eglContext);
  614. __eglContext = EGL_NO_CONTEXT;
  615. }
  616. if (__eglDisplay != EGL_NO_DISPLAY)
  617. {
  618. eglTerminate(__eglDisplay);
  619. __eglDisplay = EGL_NO_DISPLAY;
  620. }
  621. if (__screenWindow)
  622. {
  623. screen_destroy_window(__screenWindow);
  624. __screenWindow = NULL;
  625. }
  626. if (__screenEvent)
  627. {
  628. screen_destroy_event(__screenEvent);
  629. __screenEvent = NULL;
  630. }
  631. if (__screenContext)
  632. {
  633. screen_destroy_context(__screenContext);
  634. __screenContext = NULL;
  635. }
  636. }
  637. Platform* Platform::create(Game* game, void* attachToWindow)
  638. {
  639. FileSystem::setResourcePath("./app/native/");
  640. Platform* platform = new Platform(game);
  641. // Query game config
  642. int samples = 0;
  643. Properties* config = Game::getInstance()->getConfig()->getNamespace("window", true);
  644. if (config)
  645. {
  646. samples = std::max(config->getInt("samples"), 0);
  647. }
  648. __gestureSet = gestures_set_alloc();
  649. swipe_gesture_alloc(NULL, gesture_callback, __gestureSet);
  650. pinch_gesture_alloc(NULL, gesture_callback, __gestureSet);
  651. tap_gesture_alloc(NULL, gesture_callback, __gestureSet);
  652. bps_initialize();
  653. // Initialize navigator and orientation
  654. static const int SENSOR_RATE = 25000;
  655. sensor_set_rate(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, SENSOR_RATE);
  656. sensor_set_skip_duplicates(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, true);
  657. sensor_request_events(SENSOR_TYPE_AZIMUTH_PITCH_ROLL);
  658. navigator_request_events(0);
  659. navigator_rotation_lock(true);
  660. __orientationAngle = atoi(getenv("ORIENTATION"));
  661. int rc = 0;
  662. int screenFormat = SCREEN_FORMAT_RGBA8888;
  663. #ifdef __X86__
  664. int screenUsage = SCREEN_USAGE_OPENGL_ES2;
  665. #else
  666. int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2; // Physical device copy directly into physical display
  667. #endif
  668. int screenSwapInterval = WINDOW_VSYNC ? 1 : 0;
  669. int screenTransparency = SCREEN_TRANSPARENCY_NONE;
  670. char *width_str = getenv("WIDTH");
  671. char *height_str = getenv("HEIGHT");
  672. // Hard-coded to (0,0).
  673. int windowPosition[] =
  674. {
  675. 0, 0
  676. };
  677. EGLint eglConfigCount;
  678. // Hard-coded to 32-bit/OpenGL ES 2.0.
  679. // NOTE: EGL_SAMPLE_BUFFERS and EGL_SAMPLES MUST remain at the beginning of the attribute list
  680. // since they are expected to be at indices 0-3 in config fallback code later.
  681. EGLint eglConfigAttrs[] =
  682. {
  683. EGL_SAMPLE_BUFFERS, samples > 0 ? 1 : 0,
  684. EGL_SAMPLES, samples,
  685. EGL_RED_SIZE, 8,
  686. EGL_GREEN_SIZE, 8,
  687. EGL_BLUE_SIZE, 8,
  688. EGL_ALPHA_SIZE, 8,
  689. EGL_DEPTH_SIZE, 24,
  690. EGL_STENCIL_SIZE, 8,
  691. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  692. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  693. EGL_NONE
  694. };
  695. const EGLint eglContextAttrs[] =
  696. {
  697. EGL_CONTEXT_CLIENT_VERSION, 2,
  698. EGL_NONE
  699. };
  700. const EGLint eglSurfaceAttrs[] =
  701. {
  702. EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
  703. EGL_NONE
  704. };
  705. // Create the screen context.
  706. rc = screen_create_context(&__screenContext, 0);
  707. if (rc)
  708. {
  709. perror("screen_create_context");
  710. goto error;
  711. }
  712. // Create the screen window.
  713. rc = screen_create_window(&__screenWindow, __screenContext);
  714. if (rc)
  715. {
  716. perror("screen_create_window");
  717. goto error;
  718. }
  719. // Set/get any window properties.
  720. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat);
  721. if (rc)
  722. {
  723. perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)");
  724. goto error;
  725. }
  726. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage);
  727. if (rc)
  728. {
  729. perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)");
  730. goto error;
  731. }
  732. if (width_str && height_str)
  733. {
  734. __screenWindowSize[0] = atoi(width_str);
  735. __screenWindowSize[1] = atoi(height_str);
  736. }
  737. else
  738. {
  739. screen_display_t screen_display;
  740. rc = screen_get_window_property_pv(__screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display);
  741. if (rc)
  742. {
  743. perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)");
  744. goto error;
  745. }
  746. screen_display_mode_t screen_mode;
  747. rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode);
  748. if (rc)
  749. {
  750. perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)");
  751. goto error;
  752. }
  753. int size[2];
  754. rc = screen_get_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size);
  755. if (rc)
  756. {
  757. perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)");
  758. goto error;
  759. }
  760. __screenWindowSize[0] = size[0];
  761. __screenWindowSize[1] = size[1];
  762. if ((__orientationAngle == 0) || (__orientationAngle == 180))
  763. {
  764. if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) ||
  765. ((screen_mode.width < screen_mode.height) && (size[0] > size[1])))
  766. {
  767. __screenWindowSize[1] = size[0];
  768. __screenWindowSize[0] = size[1];
  769. }
  770. }
  771. else if ((__orientationAngle == 90) || (__orientationAngle == 270))
  772. {
  773. if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) ||
  774. ((screen_mode.width < screen_mode.height) && (size[0] < size[1])))
  775. {
  776. __screenWindowSize[1] = size[0];
  777. __screenWindowSize[0] = size[1];
  778. }
  779. }
  780. else
  781. {
  782. perror("Navigator returned an unexpected orientation angle.");
  783. goto error;
  784. }
  785. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_ROTATION, &__orientationAngle);
  786. if (rc)
  787. {
  788. perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)");
  789. goto error;
  790. }
  791. }
  792. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, __screenWindowSize);
  793. if (rc)
  794. {
  795. perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)");
  796. goto error;
  797. }
  798. if (windowPosition[0] != 0 || windowPosition[1] != 0)
  799. {
  800. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_POSITION, windowPosition);
  801. if (rc)
  802. {
  803. perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)");
  804. goto error;
  805. }
  806. }
  807. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency);
  808. if (rc)
  809. {
  810. perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)");
  811. goto error;
  812. }
  813. // Double buffered.
  814. rc = screen_create_window_buffers(__screenWindow, 2);
  815. if (rc)
  816. {
  817. perror("screen_create_window_buffers");
  818. goto error;
  819. }
  820. // Create screen event object.
  821. rc = screen_create_event(&__screenEvent);
  822. if (rc)
  823. {
  824. perror("screen_create_event");
  825. goto error;
  826. }
  827. // Request screen events.
  828. screen_request_events(__screenContext);
  829. // Get the EGL display and initialize.
  830. __eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  831. if (__eglDisplay == EGL_NO_DISPLAY)
  832. {
  833. perror("eglGetDisplay");
  834. goto error;
  835. }
  836. if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE)
  837. {
  838. perror("eglInitialize");
  839. goto error;
  840. }
  841. if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0)
  842. {
  843. bool success = false;
  844. while (samples)
  845. {
  846. // Try lowering the MSAA sample count until we find a supported config
  847. GP_WARN("Failed to find a valid EGL configuration with EGL samples=%d. Trying samples=%d instead.", samples, samples/2);
  848. samples /= 2;
  849. eglConfigAttrs[1] = samples > 0 ? 1 : 0;
  850. eglConfigAttrs[3] = samples;
  851. if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0)
  852. {
  853. success = true;
  854. break;
  855. }
  856. }
  857. if (!success)
  858. {
  859. checkErrorEGL("eglChooseConfig");
  860. goto error;
  861. }
  862. }
  863. __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs);
  864. if (__eglContext == EGL_NO_CONTEXT)
  865. {
  866. checkErrorEGL("eglCreateContext");
  867. goto error;
  868. }
  869. __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, __screenWindow, eglSurfaceAttrs);
  870. if (__eglSurface == EGL_NO_SURFACE)
  871. {
  872. checkErrorEGL("eglCreateWindowSurface");
  873. goto error;
  874. }
  875. if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE)
  876. {
  877. checkErrorEGL("eglMakeCurrent");
  878. goto error;
  879. }
  880. // Set vsync.
  881. eglSwapInterval(__eglDisplay, screenSwapInterval);
  882. // Initialize OpenGL ES extensions.
  883. __glExtensions = (const char*)glGetString(GL_EXTENSIONS);
  884. if (strstr(__glExtensions, "GL_OES_vertex_array_object") || strstr(__glExtensions, "GL_ARB_vertex_array_object"))
  885. {
  886. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES");
  887. glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES");
  888. glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES");
  889. glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES");
  890. }
  891. #ifdef USE_BLACKBERRY_GAMEPAD
  892. screen_device_t* screenDevs;
  893. // Discover initial gamepad devices.
  894. int count;
  895. screen_get_context_property_iv(__screenContext, SCREEN_PROPERTY_DEVICE_COUNT, &count);
  896. screenDevs = (screen_device_t*)calloc(count, sizeof(screen_device_t));
  897. screen_get_context_property_pv(__screenContext, SCREEN_PROPERTY_DEVICES, (void**)screenDevs);
  898. for (int i = 0; i < count; i++)
  899. {
  900. int type;
  901. screen_get_device_property_iv(screenDevs[i], SCREEN_PROPERTY_TYPE, &type);
  902. if (type == SCREEN_EVENT_GAMEPAD || type == SCREEN_EVENT_JOYSTICK)
  903. {
  904. int buttonCount = 0;
  905. int joystickCount = 0;
  906. int productId;
  907. int vendorId;
  908. char productString[64];
  909. char vendorString[64];
  910. queryGamepad(screenDevs[i], &buttonCount, &joystickCount, &productId, &vendorId, productString, vendorString);
  911. Platform::gamepadEventConnectedInternal(screenDevs[i], buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
  912. }
  913. }
  914. free(screenDevs);
  915. #endif
  916. return platform;
  917. error:
  918. return NULL;
  919. }
  920. /**
  921. * Convert the timespec into milliseconds.
  922. */
  923. double timespec2millis(struct timespec *a)
  924. {
  925. GP_ASSERT(a);
  926. return (1000.0 * a->tv_sec) + (0.000001 * a->tv_nsec);
  927. }
  928. /**
  929. * Fires a mouse event or a touch event on the game.
  930. * If the mouse event is not consumed, a touch event is fired instead.
  931. *
  932. * @param mouseEvent The mouse event to fire.
  933. * @param touchEvent The touch event to fire.
  934. * @param x The x position of the touch in pixels.
  935. * @param y The y position of the touch in pixels.
  936. */
  937. void mouseOrTouchEvent(Mouse::MouseEvent mouseEvent, Touch::TouchEvent touchEvent, int x, int y)
  938. {
  939. if (!gameplay::Platform::mouseEventInternal(mouseEvent, x, y, 0))
  940. {
  941. Platform::touchEventInternal(touchEvent, x, y, 0);
  942. }
  943. }
  944. int Platform::enterMessagePump()
  945. {
  946. GP_ASSERT(_game);
  947. int rc;
  948. int eventType;
  949. int flags;
  950. int value;
  951. int position[2];
  952. int domain;
  953. mtouch_event_t touchEvent;
  954. bool suspended = false;
  955. // Get the initial time.
  956. clock_gettime(CLOCK_REALTIME, &__timespec);
  957. __timeStart = timespec2millis(&__timespec);
  958. __timeAbsolute = 0L;
  959. _game->run();
  960. // Message loop.
  961. while (true)
  962. {
  963. bps_event_t* event = NULL;
  964. while (true)
  965. {
  966. rc = bps_get_event(&event, 1);
  967. GP_ASSERT(rc == BPS_SUCCESS);
  968. if (event == NULL)
  969. break;
  970. domain = bps_event_get_domain(event);
  971. if (domain == screen_get_domain())
  972. {
  973. __screenEvent = screen_event_get_event(event);
  974. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_TYPE, &eventType);
  975. switch (eventType)
  976. {
  977. case SCREEN_EVENT_MTOUCH_TOUCH:
  978. {
  979. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  980. if (__gestureEventsProcessed.any())
  981. rc = gestures_set_process_event(__gestureSet, &touchEvent, NULL);
  982. if ( !rc && (__multiTouch || touchEvent.contact_id == 0) )
  983. {
  984. gameplay::Platform::touchEventInternal(Touch::TOUCH_PRESS, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  985. }
  986. break;
  987. }
  988. case SCREEN_EVENT_MTOUCH_RELEASE:
  989. {
  990. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  991. if (__gestureEventsProcessed.any())
  992. rc = gestures_set_process_event(__gestureSet, &touchEvent, NULL);
  993. if ( !rc && (__multiTouch || touchEvent.contact_id == 0) )
  994. {
  995. gameplay::Platform::touchEventInternal(Touch::TOUCH_RELEASE, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  996. }
  997. if (__gestureSwipeRecognized)
  998. {
  999. __gestureSwipeRecognized = false;
  1000. }
  1001. break;
  1002. }
  1003. case SCREEN_EVENT_MTOUCH_MOVE:
  1004. {
  1005. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  1006. if (__gestureEventsProcessed.any())
  1007. rc = gestures_set_process_event(__gestureSet, &touchEvent, NULL);
  1008. if ( !rc && (__multiTouch || touchEvent.contact_id == 0) )
  1009. {
  1010. gameplay::Platform::touchEventInternal(Touch::TOUCH_MOVE, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  1011. }
  1012. break;
  1013. }
  1014. case SCREEN_EVENT_POINTER:
  1015. {
  1016. static int mouse_pressed = 0;
  1017. int buttons;
  1018. int wheel;
  1019. // A move event will be fired unless a button state changed.
  1020. bool move = true;
  1021. bool left_move = false;
  1022. // This is a mouse move event, it is applicable to a device with a usb mouse or simulator.
  1023. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons);
  1024. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, position);
  1025. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
  1026. // Handle left mouse. Interpret as touch if the left mouse event is not consumed.
  1027. if (buttons & SCREEN_LEFT_MOUSE_BUTTON)
  1028. {
  1029. if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON)
  1030. {
  1031. left_move = true;
  1032. }
  1033. else
  1034. {
  1035. move = false;
  1036. mouse_pressed |= SCREEN_LEFT_MOUSE_BUTTON;
  1037. mouseOrTouchEvent(Mouse::MOUSE_PRESS_LEFT_BUTTON, Touch::TOUCH_PRESS, position[0], position[1]);
  1038. }
  1039. }
  1040. else if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON)
  1041. {
  1042. move = false;
  1043. mouse_pressed &= ~SCREEN_LEFT_MOUSE_BUTTON;
  1044. mouseOrTouchEvent(Mouse::MOUSE_RELEASE_LEFT_BUTTON, Touch::TOUCH_RELEASE, position[0], position[1]);
  1045. }
  1046. // Handle right mouse.
  1047. if (buttons & SCREEN_RIGHT_MOUSE_BUTTON)
  1048. {
  1049. if ((mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) == 0)
  1050. {
  1051. move = false;
  1052. mouse_pressed |= SCREEN_RIGHT_MOUSE_BUTTON;
  1053. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_PRESS_RIGHT_BUTTON, position[0], position[1], 0);
  1054. }
  1055. }
  1056. else if (mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON)
  1057. {
  1058. move = false;
  1059. mouse_pressed &= ~SCREEN_RIGHT_MOUSE_BUTTON;
  1060. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_RELEASE_RIGHT_BUTTON, position[0], position[1], 0);
  1061. }
  1062. // Handle middle mouse.
  1063. if (buttons & SCREEN_MIDDLE_MOUSE_BUTTON)
  1064. {
  1065. if ((mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) == 0)
  1066. {
  1067. move = false;
  1068. mouse_pressed |= SCREEN_MIDDLE_MOUSE_BUTTON;
  1069. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_PRESS_MIDDLE_BUTTON, position[0], position[1], 0);
  1070. }
  1071. }
  1072. else if (mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON)
  1073. {
  1074. move = false;
  1075. mouse_pressed &= ~SCREEN_MIDDLE_MOUSE_BUTTON;
  1076. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_RELEASE_MIDDLE_BUTTON, position[0], position[1], 0);
  1077. }
  1078. // Fire a move event if none of the buttons changed.
  1079. if (left_move)
  1080. {
  1081. mouseOrTouchEvent(Mouse::MOUSE_MOVE, Touch::TOUCH_MOVE, position[0], position[1]);
  1082. }
  1083. else if (move)
  1084. {
  1085. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_MOVE, position[0], position[1], 0);
  1086. }
  1087. // Handle mouse wheel events.
  1088. if (wheel)
  1089. {
  1090. gameplay::Platform::mouseEventInternal(Mouse::MOUSE_WHEEL, position[0], position[1], -wheel);
  1091. }
  1092. break;
  1093. }
  1094. case SCREEN_EVENT_KEYBOARD:
  1095. {
  1096. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &flags);
  1097. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_KEY_SYM, &value);
  1098. gameplay::Keyboard::KeyEvent evt = (flags & KEY_DOWN) ? gameplay::Keyboard::KEY_PRESS : gameplay::Keyboard::KEY_RELEASE;
  1099. // Suppress key repeats.
  1100. if ((flags & KEY_REPEAT) == 0)
  1101. {
  1102. keyEventInternal(evt, getKey(value));
  1103. if (evt == gameplay::Keyboard::KEY_PRESS && (flags & KEY_SYM_VALID))
  1104. {
  1105. int unicode = getUnicode(value);
  1106. if (unicode)
  1107. keyEventInternal(gameplay::Keyboard::KEY_CHAR, unicode);
  1108. }
  1109. }
  1110. break;
  1111. }
  1112. #ifdef USE_BLACKBERRY_GAMEPAD
  1113. case SCREEN_EVENT_DEVICE:
  1114. {
  1115. // A device was attached or removed.
  1116. screen_device_t device;
  1117. int attached;
  1118. screen_get_event_property_pv(__screenEvent, SCREEN_PROPERTY_DEVICE, (void**)&device);
  1119. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_ATTACHED, &attached);
  1120. if (attached)
  1121. {
  1122. int type;
  1123. screen_get_device_property_iv(device, SCREEN_PROPERTY_TYPE, &type);
  1124. if (type == SCREEN_EVENT_GAMEPAD || type == SCREEN_EVENT_JOYSTICK)
  1125. {
  1126. int buttonCount = 0;
  1127. int joystickCount = 0;
  1128. int productId;
  1129. int vendorId;
  1130. char productString[64];
  1131. char vendorString[64];
  1132. queryGamepad(device, &buttonCount, &joystickCount, &productId, &vendorId, productString, vendorString);
  1133. Platform::gamepadEventConnectedInternal(device, buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. Platform::gamepadEventDisconnectedInternal(device);
  1139. }
  1140. break;
  1141. }
  1142. #endif
  1143. default:
  1144. break;
  1145. }
  1146. }
  1147. else if (domain == navigator_get_domain())
  1148. {
  1149. switch (bps_event_get_code(event))
  1150. {
  1151. case NAVIGATOR_SWIPE_DOWN:
  1152. _game->menuEvent();
  1153. break;
  1154. case NAVIGATOR_WINDOW_STATE:
  1155. {
  1156. navigator_window_state_t state = navigator_event_get_window_state(event);
  1157. switch (state)
  1158. {
  1159. case NAVIGATOR_WINDOW_FULLSCREEN:
  1160. if (!__screenFullscreen)
  1161. __screenFullscreen = true;
  1162. _game->resume();
  1163. suspended = false;
  1164. break;
  1165. case NAVIGATOR_WINDOW_THUMBNAIL:
  1166. case NAVIGATOR_WINDOW_INVISIBLE:
  1167. if (__screenFullscreen && !suspended)
  1168. {
  1169. _game->pause();
  1170. suspended = true;
  1171. }
  1172. break;
  1173. }
  1174. break;
  1175. }
  1176. case NAVIGATOR_EXIT:
  1177. // Call Game::shutdown directly, instead of Game::exit.
  1178. // We need to do this since exit() queues a request to shutdown for the
  1179. // next frame, which will never get executed because we are suspended.
  1180. _game->shutdown();
  1181. break;
  1182. }
  1183. }
  1184. else if (domain == sensor_get_domain())
  1185. {
  1186. if (bps_event_get_code(event) == SENSOR_AZIMUTH_PITCH_ROLL_READING)
  1187. {
  1188. float azimuth;
  1189. sensor_event_get_apr(event, &azimuth, &__pitch, &__roll);
  1190. }
  1191. }
  1192. }
  1193. // If we are done, then exit.
  1194. if (_game->getState() == Game::UNINITIALIZED)
  1195. break;
  1196. if (!suspended)
  1197. {
  1198. _game->frame();
  1199. // Post the new frame to the display.
  1200. // Note that there are a couple cases where eglSwapBuffers could fail
  1201. // with an error code that requires a certain level of re-initialization:
  1202. //
  1203. // 1) EGL_BAD_NATIVE_WINDOW - Called when the surface we're currently using
  1204. // is invalidated. This would require us to destroy our EGL surface,
  1205. // close our OpenKODE window, and start again.
  1206. //
  1207. // 2) EGL_CONTEXT_LOST - Power management event that led to our EGL context
  1208. // being lost. Requires us to re-create and re-initalize our EGL context
  1209. // and all OpenGL ES state.
  1210. //
  1211. // For now, if we get these, we'll simply exit.
  1212. rc = eglSwapBuffers(__eglDisplay, __eglSurface);
  1213. if (rc != EGL_TRUE)
  1214. {
  1215. _game->shutdown();
  1216. perror("eglSwapBuffers");
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. screen_stop_events(__screenContext);
  1222. bps_shutdown();
  1223. screen_destroy_context(__screenContext);
  1224. return 0;
  1225. }
  1226. void Platform::signalShutdown()
  1227. {
  1228. // nothing to do
  1229. }
  1230. bool Platform::canExit()
  1231. {
  1232. return true;
  1233. }
  1234. unsigned int Platform::getDisplayWidth()
  1235. {
  1236. return __screenWindowSize[0];
  1237. }
  1238. unsigned int Platform::getDisplayHeight()
  1239. {
  1240. return __screenWindowSize[1];
  1241. }
  1242. double Platform::getAbsoluteTime()
  1243. {
  1244. clock_gettime(CLOCK_REALTIME, &__timespec);
  1245. double now = timespec2millis(&__timespec);
  1246. __timeAbsolute = now - __timeStart;
  1247. return __timeAbsolute;
  1248. }
  1249. void Platform::setAbsoluteTime(double time)
  1250. {
  1251. __timeAbsolute = time;
  1252. }
  1253. bool Platform::isVsync()
  1254. {
  1255. return __vsync;
  1256. }
  1257. void Platform::setVsync(bool enable)
  1258. {
  1259. eglSwapInterval(__eglDisplay, enable ? 1 : 0);
  1260. __vsync = enable;
  1261. }
  1262. void Platform::swapBuffers()
  1263. {
  1264. if (__eglDisplay && __eglSurface)
  1265. eglSwapBuffers(__eglDisplay, __eglSurface);
  1266. }
  1267. void Platform::sleep(long ms)
  1268. {
  1269. usleep(ms * 1000);
  1270. }
  1271. void Platform::setMultiTouch(bool enabled)
  1272. {
  1273. __multiTouch = enabled;
  1274. }
  1275. bool Platform::isMultiTouch()
  1276. {
  1277. return __multiTouch;
  1278. }
  1279. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1280. {
  1281. GP_ASSERT(pitch);
  1282. GP_ASSERT(roll);
  1283. switch(__orientationAngle)
  1284. {
  1285. // Landscape based device adjusting for landscape game mode
  1286. case 0:
  1287. if (pitch)
  1288. *pitch = __pitch;
  1289. if (roll)
  1290. *roll = -__roll;
  1291. break;
  1292. case 180:
  1293. if (pitch)
  1294. *pitch = -__pitch;
  1295. if (roll)
  1296. *roll = __roll;
  1297. break;
  1298. // Portrait based device adjusting for landscape game mode
  1299. case 90:
  1300. if (pitch)
  1301. *pitch = -__roll;
  1302. if (roll)
  1303. *roll = -__pitch;
  1304. break;
  1305. case 270:
  1306. if (pitch)
  1307. *pitch = __roll;
  1308. if (roll)
  1309. *roll = __pitch;
  1310. break;
  1311. default:
  1312. break;
  1313. }
  1314. }
  1315. bool Platform::hasMouse()
  1316. {
  1317. // not supported
  1318. return false;
  1319. }
  1320. void Platform::setMouseCaptured(bool captured)
  1321. {
  1322. // not supported
  1323. }
  1324. bool Platform::isMouseCaptured()
  1325. {
  1326. // not supported
  1327. return false;
  1328. }
  1329. void Platform::setCursorVisible(bool visible)
  1330. {
  1331. // not supported
  1332. }
  1333. bool Platform::isCursorVisible()
  1334. {
  1335. // not supported
  1336. return false;
  1337. }
  1338. void Platform::displayKeyboard(bool display)
  1339. {
  1340. if (display)
  1341. virtualkeyboard_show();
  1342. else
  1343. virtualkeyboard_hide();
  1344. }
  1345. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1346. {
  1347. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1348. {
  1349. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1350. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1351. }
  1352. }
  1353. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1354. {
  1355. if (!Form::keyEventInternal(evt, key))
  1356. {
  1357. Game::getInstance()->keyEvent(evt, key);
  1358. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1359. }
  1360. }
  1361. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1362. {
  1363. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1364. {
  1365. return true;
  1366. }
  1367. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1368. {
  1369. return true;
  1370. }
  1371. else
  1372. {
  1373. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1374. }
  1375. }
  1376. void Platform::gamepadEventConnectedInternal(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
  1377. unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
  1378. {
  1379. Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
  1380. }
  1381. void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
  1382. {
  1383. Gamepad::remove(handle);
  1384. }
  1385. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1386. {
  1387. // All are supported no need to test the bitset
  1388. return true;
  1389. }
  1390. void Platform::registerGesture(Gesture::GestureEvent evt)
  1391. {
  1392. switch(evt)
  1393. {
  1394. case Gesture::GESTURE_ANY_SUPPORTED:
  1395. __gestureEventsProcessed.set();
  1396. break;
  1397. case Gesture::GESTURE_SWIPE:
  1398. case Gesture::GESTURE_PINCH:
  1399. case Gesture::GESTURE_TAP:
  1400. __gestureEventsProcessed.set(evt);
  1401. break;
  1402. default:
  1403. break;
  1404. }
  1405. }
  1406. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1407. {
  1408. switch(evt)
  1409. {
  1410. case Gesture::GESTURE_ANY_SUPPORTED:
  1411. __gestureEventsProcessed.reset();
  1412. break;
  1413. case Gesture::GESTURE_SWIPE:
  1414. case Gesture::GESTURE_PINCH:
  1415. case Gesture::GESTURE_TAP:
  1416. __gestureEventsProcessed.set(evt, 0);
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. }
  1422. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1423. {
  1424. return __gestureEventsProcessed.test(evt);
  1425. }
  1426. bool Platform::launchURL(const char* url)
  1427. {
  1428. if (url == NULL || *url == '\0')
  1429. return false;
  1430. return navigator_invoke(url, NULL) == BPS_SUCCESS;
  1431. }
  1432. }
  1433. #endif