PlatformLinux.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. #ifdef __linux__
  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 <X11/X.h>
  9. #include <X11/Xlib.h>
  10. #include <X11/keysym.h>
  11. #include <sys/time.h>
  12. #include <GL/glxew.h>
  13. #include <poll.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <fstream>
  20. #define TOUCH_COUNT_MAX 4
  21. #define MAX_GAMEPADS 4
  22. using namespace std;
  23. int __argc = 0;
  24. char** __argv = 0;
  25. enum GamepadAxisInfoFlags
  26. {
  27. GP_AXIS_SKIP = 0x1,
  28. GP_AXIS_IS_DPAD = 0x2,
  29. GP_AXIS_IS_NEG = 0x4,
  30. GP_AXIS_IS_XAXIS = 0x8,
  31. GP_AXIS_IS_TRIGGER = 0x10
  32. };
  33. enum GamepadAxisInfoNormalizeFunction
  34. {
  35. NEG_TO_POS,
  36. ZERO_TO_POS
  37. };
  38. struct GamepadJoystickAxisInfo
  39. {
  40. int axisIndex;
  41. unsigned int joystickIndex;
  42. unsigned long flags;
  43. int mappedPosArg;
  44. int mappedNegArg;
  45. float deadZone;
  46. GamepadAxisInfoNormalizeFunction mapFunc;
  47. };
  48. struct GamepadInfoEntry
  49. {
  50. unsigned int vendorId;
  51. unsigned int productId;
  52. const char* productName;
  53. unsigned int numberOfJS;
  54. unsigned int numberOfAxes;
  55. unsigned int numberOfButtons;
  56. unsigned int numberOfTriggers;
  57. GamepadJoystickAxisInfo* axes;
  58. long* buttons;
  59. };
  60. struct ConnectedGamepadDevInfo
  61. {
  62. dev_t deviceId;
  63. gameplay::GamepadHandle fd;
  64. const GamepadInfoEntry& gamepadInfo;
  65. };
  66. struct timespec __timespec;
  67. static double __timeStart;
  68. static double __timeAbsolute;
  69. static bool __vsync = WINDOW_VSYNC;
  70. static bool __mouseCaptured = false;
  71. static float __mouseCapturePointX = 0;
  72. static float __mouseCapturePointY = 0;
  73. static bool __multiSampling = false;
  74. static bool __cursorVisible = true;
  75. static Display* __display;
  76. static Window __window;
  77. static int __windowSize[2];
  78. static GLXContext __context;
  79. static Atom __atomWmDeleteWindow;
  80. static list<ConnectedGamepadDevInfo> __connectedGamepads;
  81. // Gets the gameplay::Keyboard::Key enumeration constant that corresponds to the given X11 key symbol.
  82. static gameplay::Keyboard::Key getKey(KeySym sym)
  83. {
  84. switch (sym)
  85. {
  86. case XK_Sys_Req:
  87. return gameplay::Keyboard::KEY_SYSREQ;
  88. case XK_Break:
  89. return gameplay::Keyboard::KEY_BREAK;
  90. case XK_Menu :
  91. return gameplay::Keyboard::KEY_MENU;
  92. case XK_KP_Enter:
  93. return gameplay::Keyboard::KEY_KP_ENTER;
  94. case XK_Pause:
  95. return gameplay::Keyboard::KEY_PAUSE;
  96. case XK_Scroll_Lock:
  97. return gameplay::Keyboard::KEY_SCROLL_LOCK;
  98. case XK_Print:
  99. return gameplay::Keyboard::KEY_PRINT;
  100. case XK_Escape:
  101. return gameplay::Keyboard::KEY_ESCAPE;
  102. case XK_BackSpace:
  103. return gameplay::Keyboard::KEY_BACKSPACE;
  104. case XK_Tab:
  105. return gameplay::Keyboard::KEY_TAB;
  106. case XK_Return:
  107. return gameplay::Keyboard::KEY_RETURN;
  108. case XK_Caps_Lock:
  109. return gameplay::Keyboard::KEY_CAPS_LOCK;
  110. case XK_Shift_L:
  111. case XK_Shift_R:
  112. return gameplay::Keyboard::KEY_SHIFT;
  113. case XK_Control_L:
  114. case XK_Control_R:
  115. return gameplay::Keyboard::KEY_CTRL;
  116. case XK_Alt_L:
  117. case XK_Alt_R:
  118. return gameplay::Keyboard::KEY_ALT;
  119. case XK_Hyper_L:
  120. case XK_Hyper_R:
  121. return gameplay::Keyboard::KEY_HYPER;
  122. case XK_Insert:
  123. return gameplay::Keyboard::KEY_INSERT;
  124. case XK_Home:
  125. return gameplay::Keyboard::KEY_HOME;
  126. case XK_Page_Up:
  127. return gameplay::Keyboard::KEY_PG_UP;
  128. case XK_Delete:
  129. return gameplay::Keyboard::KEY_DELETE;
  130. case XK_End:
  131. return gameplay::Keyboard::KEY_END;
  132. case XK_Page_Down:
  133. return gameplay::Keyboard::KEY_PG_DOWN;
  134. case XK_Left:
  135. return gameplay::Keyboard::KEY_LEFT_ARROW;
  136. case XK_Right:
  137. return gameplay::Keyboard::KEY_RIGHT_ARROW;
  138. case XK_Up:
  139. return gameplay::Keyboard::KEY_UP_ARROW;
  140. case XK_Down:
  141. return gameplay::Keyboard::KEY_DOWN_ARROW;
  142. case XK_Num_Lock:
  143. return gameplay::Keyboard::KEY_NUM_LOCK;
  144. case XK_KP_Add:
  145. return gameplay::Keyboard::KEY_KP_PLUS;
  146. case XK_KP_Subtract:
  147. return gameplay::Keyboard::KEY_KP_MINUS;
  148. case XK_KP_Multiply:
  149. return gameplay::Keyboard::KEY_KP_MULTIPLY;
  150. case XK_KP_Divide:
  151. return gameplay::Keyboard::KEY_KP_DIVIDE;
  152. case XK_KP_Home:
  153. return gameplay::Keyboard::KEY_KP_HOME;
  154. case XK_KP_Up:
  155. return gameplay::Keyboard::KEY_KP_UP;
  156. case XK_KP_Page_Up:
  157. return gameplay::Keyboard::KEY_KP_PG_UP;
  158. case XK_KP_Left:
  159. return gameplay::Keyboard::KEY_KP_LEFT;
  160. case XK_KP_5:
  161. return gameplay::Keyboard::KEY_KP_FIVE;
  162. case XK_KP_Right:
  163. return gameplay::Keyboard::KEY_KP_RIGHT;
  164. case XK_KP_End:
  165. return gameplay::Keyboard::KEY_KP_END;
  166. case XK_KP_Down:
  167. return gameplay::Keyboard::KEY_KP_DOWN;
  168. case XK_KP_Page_Down:
  169. return gameplay::Keyboard::KEY_KP_PG_DOWN;
  170. case XK_KP_Insert:
  171. return gameplay::Keyboard::KEY_KP_INSERT;
  172. case XK_KP_Delete:
  173. return gameplay::Keyboard::KEY_KP_DELETE;
  174. case XK_F1:
  175. return gameplay::Keyboard::KEY_F1;
  176. case XK_F2:
  177. return gameplay::Keyboard::KEY_F2;
  178. case XK_F3:
  179. return gameplay::Keyboard::KEY_F3;
  180. case XK_F4:
  181. return gameplay::Keyboard::KEY_F4;
  182. case XK_F5:
  183. return gameplay::Keyboard::KEY_F5;
  184. case XK_F6:
  185. return gameplay::Keyboard::KEY_F6;
  186. case XK_F7:
  187. return gameplay::Keyboard::KEY_F7;
  188. case XK_F8:
  189. return gameplay::Keyboard::KEY_F8;
  190. case XK_F9:
  191. return gameplay::Keyboard::KEY_F9;
  192. case XK_F10:
  193. return gameplay::Keyboard::KEY_F10;
  194. case XK_F11:
  195. return gameplay::Keyboard::KEY_F11;
  196. case XK_F12:
  197. return gameplay::Keyboard::KEY_F12;
  198. case XK_KP_Space:
  199. case XK_space:
  200. return gameplay::Keyboard::KEY_SPACE;
  201. case XK_parenright:
  202. return gameplay::Keyboard::KEY_RIGHT_PARENTHESIS;
  203. case XK_0:
  204. return gameplay::Keyboard::KEY_ZERO;
  205. case XK_exclam:
  206. return gameplay::Keyboard::KEY_EXCLAM;
  207. case XK_1:
  208. return gameplay::Keyboard::KEY_ONE;
  209. case XK_at:
  210. return gameplay::Keyboard::KEY_AT;
  211. case XK_2:
  212. return gameplay::Keyboard::KEY_TWO;
  213. case XK_numbersign:
  214. return gameplay::Keyboard::KEY_NUMBER;
  215. case XK_3:
  216. return gameplay::Keyboard::KEY_THREE;
  217. case XK_dollar:
  218. return gameplay::Keyboard::KEY_DOLLAR;
  219. case XK_4:
  220. return gameplay::Keyboard::KEY_FOUR;
  221. case XK_percent:
  222. case XK_asciicircum :
  223. return gameplay::Keyboard::KEY_CIRCUMFLEX;
  224. return gameplay::Keyboard::KEY_PERCENT;
  225. case XK_5:
  226. return gameplay::Keyboard::KEY_FIVE;
  227. case XK_6:
  228. return gameplay::Keyboard::KEY_SIX;
  229. case XK_ampersand:
  230. return gameplay::Keyboard::KEY_AMPERSAND;
  231. case XK_7:
  232. return gameplay::Keyboard::KEY_SEVEN;
  233. case XK_asterisk:
  234. return gameplay::Keyboard::KEY_ASTERISK;
  235. case XK_8:
  236. return gameplay::Keyboard::KEY_EIGHT;
  237. case XK_parenleft:
  238. return gameplay::Keyboard::KEY_LEFT_PARENTHESIS;
  239. case XK_9:
  240. return gameplay::Keyboard::KEY_NINE;
  241. case XK_equal:
  242. return gameplay::Keyboard::KEY_EQUAL;
  243. case XK_plus:
  244. return gameplay::Keyboard::KEY_PLUS;
  245. case XK_less:
  246. return gameplay::Keyboard::KEY_LESS_THAN;
  247. case XK_comma:
  248. return gameplay::Keyboard::KEY_COMMA;
  249. case XK_underscore:
  250. return gameplay::Keyboard::KEY_UNDERSCORE;
  251. case XK_minus:
  252. return gameplay::Keyboard::KEY_MINUS;
  253. case XK_greater:
  254. return gameplay::Keyboard::KEY_GREATER_THAN;
  255. case XK_period:
  256. return gameplay::Keyboard::KEY_PERIOD;
  257. case XK_colon:
  258. return gameplay::Keyboard::KEY_COLON;
  259. case XK_semicolon:
  260. return gameplay::Keyboard::KEY_SEMICOLON;
  261. case XK_question:
  262. return gameplay::Keyboard::KEY_QUESTION;
  263. case XK_slash:
  264. return gameplay::Keyboard::KEY_SLASH;
  265. case XK_grave:
  266. return gameplay::Keyboard::KEY_GRAVE;
  267. case XK_asciitilde:
  268. return gameplay::Keyboard::KEY_TILDE;
  269. case XK_braceleft:
  270. return gameplay::Keyboard::KEY_LEFT_BRACE;
  271. case XK_bracketleft:
  272. return gameplay::Keyboard::KEY_LEFT_BRACKET;
  273. case XK_bar:
  274. return gameplay::Keyboard::KEY_BAR;
  275. case XK_backslash:
  276. return gameplay::Keyboard::KEY_BACK_SLASH;
  277. case XK_braceright:
  278. return gameplay::Keyboard::KEY_RIGHT_BRACE;
  279. case XK_bracketright:
  280. return gameplay::Keyboard::KEY_RIGHT_BRACKET;
  281. case XK_quotedbl:
  282. return gameplay::Keyboard::KEY_QUOTE;
  283. case XK_apostrophe:
  284. return gameplay::Keyboard::KEY_APOSTROPHE;
  285. case XK_EuroSign:
  286. return gameplay::Keyboard::KEY_EURO;
  287. case XK_sterling:
  288. return gameplay::Keyboard::KEY_POUND;
  289. case XK_yen:
  290. return gameplay::Keyboard::KEY_YEN;
  291. case XK_periodcentered:
  292. return gameplay::Keyboard::KEY_MIDDLE_DOT;
  293. case XK_A:
  294. return gameplay::Keyboard::KEY_CAPITAL_A;
  295. case XK_a:
  296. return gameplay::Keyboard::KEY_A;
  297. case XK_B:
  298. return gameplay::Keyboard::KEY_CAPITAL_B;
  299. case XK_b:
  300. return gameplay::Keyboard::KEY_B;
  301. case XK_C:
  302. return gameplay::Keyboard::KEY_CAPITAL_C;
  303. case XK_c:
  304. return gameplay::Keyboard::KEY_C;
  305. case XK_D:
  306. return gameplay::Keyboard::KEY_CAPITAL_D;
  307. case XK_d:
  308. return gameplay::Keyboard::KEY_D;
  309. case XK_E:
  310. return gameplay::Keyboard::KEY_CAPITAL_E;
  311. case XK_e:
  312. return gameplay::Keyboard::KEY_E;
  313. case XK_F:
  314. return gameplay::Keyboard::KEY_CAPITAL_F;
  315. case XK_f:
  316. return gameplay::Keyboard::KEY_F;
  317. case XK_G:
  318. return gameplay::Keyboard::KEY_CAPITAL_G;
  319. case XK_g:
  320. return gameplay::Keyboard::KEY_G;
  321. case XK_H:
  322. return gameplay::Keyboard::KEY_CAPITAL_H;
  323. case XK_h:
  324. return gameplay::Keyboard::KEY_H;
  325. case XK_I:
  326. return gameplay::Keyboard::KEY_CAPITAL_I;
  327. case XK_i:
  328. return gameplay::Keyboard::KEY_I;
  329. case XK_J:
  330. return gameplay::Keyboard::KEY_CAPITAL_J;
  331. case XK_j:
  332. return gameplay::Keyboard::KEY_J;
  333. case XK_K:
  334. return gameplay::Keyboard::KEY_CAPITAL_K;
  335. case XK_k:
  336. return gameplay::Keyboard::KEY_K;
  337. case XK_L:
  338. return gameplay::Keyboard::KEY_CAPITAL_L;
  339. case XK_l:
  340. return gameplay::Keyboard::KEY_L;
  341. case XK_M:
  342. return gameplay::Keyboard::KEY_CAPITAL_M;
  343. case XK_m:
  344. return gameplay::Keyboard::KEY_M;
  345. case XK_N:
  346. return gameplay::Keyboard::KEY_CAPITAL_N;
  347. case XK_n:
  348. return gameplay::Keyboard::KEY_N;
  349. case XK_O:
  350. return gameplay::Keyboard::KEY_CAPITAL_O;
  351. case XK_o:
  352. return gameplay::Keyboard::KEY_O;
  353. case XK_P:
  354. return gameplay::Keyboard::KEY_CAPITAL_P;
  355. case XK_p:
  356. return gameplay::Keyboard::KEY_P;
  357. case XK_Q:
  358. return gameplay::Keyboard::KEY_CAPITAL_Q;
  359. case XK_q:
  360. return gameplay::Keyboard::KEY_Q;
  361. case XK_R:
  362. return gameplay::Keyboard::KEY_CAPITAL_R;
  363. case XK_r:
  364. return gameplay::Keyboard::KEY_R;
  365. case XK_S:
  366. return gameplay::Keyboard::KEY_CAPITAL_S;
  367. case XK_s:
  368. return gameplay::Keyboard::KEY_S;
  369. case XK_T:
  370. return gameplay::Keyboard::KEY_CAPITAL_T;
  371. case XK_t:
  372. return gameplay::Keyboard::KEY_T;
  373. case XK_U:
  374. return gameplay::Keyboard::KEY_CAPITAL_U;
  375. case XK_u:
  376. return gameplay::Keyboard::KEY_U;
  377. case XK_V:
  378. return gameplay::Keyboard::KEY_CAPITAL_V;
  379. case XK_v:
  380. return gameplay::Keyboard::KEY_V;
  381. case XK_W:
  382. return gameplay::Keyboard::KEY_CAPITAL_W;
  383. case XK_w:
  384. return gameplay::Keyboard::KEY_W;
  385. case XK_X:
  386. return gameplay::Keyboard::KEY_CAPITAL_X;
  387. case XK_x:
  388. return gameplay::Keyboard::KEY_X;
  389. case XK_Y:
  390. return gameplay::Keyboard::KEY_CAPITAL_Y;
  391. case XK_y:
  392. return gameplay::Keyboard::KEY_Y;
  393. case XK_Z:
  394. return gameplay::Keyboard::KEY_CAPITAL_Z;
  395. case XK_z:
  396. return gameplay::Keyboard::KEY_Z;
  397. default:
  398. return gameplay::Keyboard::KEY_NONE;
  399. }
  400. }
  401. /**
  402. * Returns the unicode value for the given keycode or zero if the key is not a valid printable character.
  403. */
  404. static int getUnicode(gameplay::Keyboard::Key key)
  405. {
  406. switch (key)
  407. {
  408. case gameplay::Keyboard::KEY_BACKSPACE:
  409. return 0x0008;
  410. case gameplay::Keyboard::KEY_TAB:
  411. return 0x0009;
  412. case gameplay::Keyboard::KEY_RETURN:
  413. case gameplay::Keyboard::KEY_KP_ENTER:
  414. return 0x000A;
  415. case gameplay::Keyboard::KEY_ESCAPE:
  416. return 0x001B;
  417. case gameplay::Keyboard::KEY_SPACE:
  418. case gameplay::Keyboard::KEY_EXCLAM:
  419. case gameplay::Keyboard::KEY_QUOTE:
  420. case gameplay::Keyboard::KEY_NUMBER:
  421. case gameplay::Keyboard::KEY_DOLLAR:
  422. case gameplay::Keyboard::KEY_PERCENT:
  423. case gameplay::Keyboard::KEY_CIRCUMFLEX:
  424. case gameplay::Keyboard::KEY_AMPERSAND:
  425. case gameplay::Keyboard::KEY_APOSTROPHE:
  426. case gameplay::Keyboard::KEY_LEFT_PARENTHESIS:
  427. case gameplay::Keyboard::KEY_RIGHT_PARENTHESIS:
  428. case gameplay::Keyboard::KEY_ASTERISK:
  429. case gameplay::Keyboard::KEY_PLUS:
  430. case gameplay::Keyboard::KEY_COMMA:
  431. case gameplay::Keyboard::KEY_MINUS:
  432. case gameplay::Keyboard::KEY_PERIOD:
  433. case gameplay::Keyboard::KEY_SLASH:
  434. case gameplay::Keyboard::KEY_ZERO:
  435. case gameplay::Keyboard::KEY_ONE:
  436. case gameplay::Keyboard::KEY_TWO:
  437. case gameplay::Keyboard::KEY_THREE:
  438. case gameplay::Keyboard::KEY_FOUR:
  439. case gameplay::Keyboard::KEY_FIVE:
  440. case gameplay::Keyboard::KEY_SIX:
  441. case gameplay::Keyboard::KEY_SEVEN:
  442. case gameplay::Keyboard::KEY_EIGHT:
  443. case gameplay::Keyboard::KEY_NINE:
  444. case gameplay::Keyboard::KEY_COLON:
  445. case gameplay::Keyboard::KEY_SEMICOLON:
  446. case gameplay::Keyboard::KEY_LESS_THAN:
  447. case gameplay::Keyboard::KEY_EQUAL:
  448. case gameplay::Keyboard::KEY_GREATER_THAN:
  449. case gameplay::Keyboard::KEY_QUESTION:
  450. case gameplay::Keyboard::KEY_AT:
  451. case gameplay::Keyboard::KEY_CAPITAL_A:
  452. case gameplay::Keyboard::KEY_CAPITAL_B:
  453. case gameplay::Keyboard::KEY_CAPITAL_C:
  454. case gameplay::Keyboard::KEY_CAPITAL_D:
  455. case gameplay::Keyboard::KEY_CAPITAL_E:
  456. case gameplay::Keyboard::KEY_CAPITAL_F:
  457. case gameplay::Keyboard::KEY_CAPITAL_G:
  458. case gameplay::Keyboard::KEY_CAPITAL_H:
  459. case gameplay::Keyboard::KEY_CAPITAL_I:
  460. case gameplay::Keyboard::KEY_CAPITAL_J:
  461. case gameplay::Keyboard::KEY_CAPITAL_K:
  462. case gameplay::Keyboard::KEY_CAPITAL_L:
  463. case gameplay::Keyboard::KEY_CAPITAL_M:
  464. case gameplay::Keyboard::KEY_CAPITAL_N:
  465. case gameplay::Keyboard::KEY_CAPITAL_O:
  466. case gameplay::Keyboard::KEY_CAPITAL_P:
  467. case gameplay::Keyboard::KEY_CAPITAL_Q:
  468. case gameplay::Keyboard::KEY_CAPITAL_R:
  469. case gameplay::Keyboard::KEY_CAPITAL_S:
  470. case gameplay::Keyboard::KEY_CAPITAL_T:
  471. case gameplay::Keyboard::KEY_CAPITAL_U:
  472. case gameplay::Keyboard::KEY_CAPITAL_V:
  473. case gameplay::Keyboard::KEY_CAPITAL_W:
  474. case gameplay::Keyboard::KEY_CAPITAL_X:
  475. case gameplay::Keyboard::KEY_CAPITAL_Y:
  476. case gameplay::Keyboard::KEY_CAPITAL_Z:
  477. case gameplay::Keyboard::KEY_LEFT_BRACKET:
  478. case gameplay::Keyboard::KEY_BACK_SLASH:
  479. case gameplay::Keyboard::KEY_RIGHT_BRACKET:
  480. case gameplay::Keyboard::KEY_UNDERSCORE:
  481. case gameplay::Keyboard::KEY_GRAVE:
  482. case gameplay::Keyboard::KEY_A:
  483. case gameplay::Keyboard::KEY_B:
  484. case gameplay::Keyboard::KEY_C:
  485. case gameplay::Keyboard::KEY_D:
  486. case gameplay::Keyboard::KEY_E:
  487. case gameplay::Keyboard::KEY_F:
  488. case gameplay::Keyboard::KEY_G:
  489. case gameplay::Keyboard::KEY_H:
  490. case gameplay::Keyboard::KEY_I:
  491. case gameplay::Keyboard::KEY_J:
  492. case gameplay::Keyboard::KEY_K:
  493. case gameplay::Keyboard::KEY_L:
  494. case gameplay::Keyboard::KEY_M:
  495. case gameplay::Keyboard::KEY_N:
  496. case gameplay::Keyboard::KEY_O:
  497. case gameplay::Keyboard::KEY_P:
  498. case gameplay::Keyboard::KEY_Q:
  499. case gameplay::Keyboard::KEY_R:
  500. case gameplay::Keyboard::KEY_S:
  501. case gameplay::Keyboard::KEY_T:
  502. case gameplay::Keyboard::KEY_U:
  503. case gameplay::Keyboard::KEY_V:
  504. case gameplay::Keyboard::KEY_W:
  505. case gameplay::Keyboard::KEY_X:
  506. case gameplay::Keyboard::KEY_Y:
  507. case gameplay::Keyboard::KEY_Z:
  508. case gameplay::Keyboard::KEY_LEFT_BRACE:
  509. case gameplay::Keyboard::KEY_BAR:
  510. case gameplay::Keyboard::KEY_RIGHT_BRACE:
  511. case gameplay::Keyboard::KEY_TILDE:
  512. return key;
  513. default:
  514. return 0;
  515. }
  516. }
  517. // Included here to avoid the naming conflict between KEY_* defined in input.h and the ones defined in gameplay/Keyboard.h
  518. #include <linux/joystick.h>
  519. namespace gameplay
  520. {
  521. extern void print(const char* format, ...)
  522. {
  523. GP_ASSERT(format);
  524. va_list argptr;
  525. va_start(argptr, format);
  526. vfprintf(stderr, format, argptr);
  527. va_end(argptr);
  528. }
  529. Platform::Platform(Game* game) : _game(game)
  530. {
  531. }
  532. Platform::~Platform()
  533. {
  534. }
  535. Platform* Platform::create(Game* game)
  536. {
  537. GP_ASSERT(game);
  538. FileSystem::setResourcePath("./");
  539. Platform* platform = new Platform(game);
  540. // Get the display and initialize
  541. __display = XOpenDisplay(NULL);
  542. if (__display == NULL)
  543. {
  544. perror("XOpenDisplay");
  545. return NULL;
  546. }
  547. // Get the window configuration values
  548. const char *title = NULL;
  549. int __x = 0, __y = 0, __width = 1280, __height = 800, __samples = 0;
  550. bool fullscreen = false;
  551. if (game->getConfig())
  552. {
  553. Properties* config = game->getConfig()->getNamespace("window", true);
  554. if (config)
  555. {
  556. // Read window title.
  557. title = config->getString("title");
  558. // Read window rect.
  559. int x = config->getInt("x");
  560. int y = config->getInt("y");
  561. int width = config->getInt("width");
  562. int height = config->getInt("height");
  563. int samples = config->getInt("samples");
  564. fullscreen = config->getBool("fullscreen");
  565. if (fullscreen && width == 0 && height == 0)
  566. {
  567. // Use the screen resolution if fullscreen is true but width and height were not set in the config
  568. int screen_num = DefaultScreen(__display);
  569. width = DisplayWidth(__display, screen_num);
  570. height = DisplayHeight(__display, screen_num);
  571. }
  572. if (x != 0) __x = x;
  573. if (y != 0) __y = y;
  574. if (width != 0) __width = width;
  575. if (height != 0) __height = height;
  576. if (samples != 0) __samples = samples;
  577. }
  578. }
  579. // GLX version
  580. GLint majorGLX, minorGLX = 0;
  581. glXQueryVersion(__display, &majorGLX, &minorGLX);
  582. if (majorGLX == 1 && minorGLX < 2)
  583. {
  584. perror("GLX 1.2 or greater is required.");
  585. XCloseDisplay(__display);
  586. return NULL;
  587. }
  588. else
  589. {
  590. printf( "GLX version: %d.%d\n", majorGLX , minorGLX);
  591. }
  592. // Get the GLX Functions
  593. glXCreateContextAttribsARB = (GLXContext(*)(Display* dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list))glXGetProcAddressARB((GLubyte*)"glXCreateContextAttribsARB");
  594. glXChooseFBConfig = (GLXFBConfig*(*)(Display *dpy, int screen, const int *attrib_list, int *nelements))glXGetProcAddressARB((GLubyte*)"glXChooseFBConfig");
  595. glXGetVisualFromFBConfig = (XVisualInfo*(*)(Display *dpy, GLXFBConfig config))glXGetProcAddressARB((GLubyte*)"glXGetVisualFromFBConfig");
  596. glXGetFBConfigAttrib = (int(*)(Display *dpy, GLXFBConfig config, int attribute, int *value))glXGetProcAddressARB((GLubyte*)"glXGetFBConfigAttrib");
  597. glXSwapIntervalEXT = (void(*)(Display* dpy, GLXDrawable drawable, int interval))glXGetProcAddressARB((GLubyte*)"glXSwapIntervalEXT");
  598. glXSwapIntervalMESA = (int(*)(unsigned int interval))glXGetProcAddressARB((GLubyte*)"glXSwapIntervalMESA");
  599. // Get the configs
  600. int configAttribs[] =
  601. {
  602. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  603. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  604. GLX_X_RENDERABLE, True,
  605. GLX_DEPTH_SIZE, 24,
  606. GLX_STENCIL_SIZE, 8,
  607. GLX_RED_SIZE, 8,
  608. GLX_GREEN_SIZE, 8,
  609. GLX_BLUE_SIZE, 8,
  610. GLX_DOUBLEBUFFER, True,
  611. GLX_SAMPLE_BUFFERS, __samples > 0 ? 1 : 0,
  612. GLX_SAMPLES, __samples,
  613. 0
  614. };
  615. __multiSampling = __samples > 0;
  616. GLXFBConfig* configs;
  617. int configCount = 0;
  618. configs = glXChooseFBConfig(__display, DefaultScreen(__display), configAttribs, &configCount);
  619. if ( configCount == 0 || configs == 0 )
  620. {
  621. perror( "glXChooseFBConfig" );
  622. return NULL;
  623. }
  624. // Create the windows
  625. XVisualInfo* visualInfo;
  626. visualInfo = glXGetVisualFromFBConfig(__display, configs[0]);
  627. XSetWindowAttributes winAttribs;
  628. long eventMask;
  629. eventMask = ExposureMask | VisibilityChangeMask | StructureNotifyMask |
  630. KeyPressMask | KeyReleaseMask | PointerMotionMask |
  631. ButtonPressMask | ButtonReleaseMask |
  632. EnterWindowMask | LeaveWindowMask;
  633. winAttribs.event_mask = eventMask;
  634. winAttribs.border_pixel = 0;
  635. winAttribs.bit_gravity = StaticGravity;
  636. winAttribs.colormap = XCreateColormap(__display, RootWindow(__display, visualInfo->screen), visualInfo->visual, AllocNone);
  637. GLint winMask;
  638. winMask = CWBorderPixel | CWBitGravity | CWEventMask| CWColormap;
  639. __window = XCreateWindow(__display, DefaultRootWindow(__display), __x, __y, __width, __height, 0,
  640. visualInfo->depth, InputOutput, visualInfo->visual, winMask,
  641. &winAttribs);
  642. // Tell the window manager that it should send the delete window notification through ClientMessage
  643. __atomWmDeleteWindow = XInternAtom(__display, "WM_DELETE_WINDOW", False);
  644. XSetWMProtocols(__display, __window, &__atomWmDeleteWindow, 1);
  645. XMapWindow(__display, __window);
  646. // Send fullscreen atom message to the window; most window managers respect WM_STATE messages
  647. // Note: fullscreen mode will use native desktop resolution and won't care about width/height specified
  648. if (fullscreen)
  649. {
  650. XEvent xev;
  651. Atom atomWm_state = XInternAtom(__display, "_NET_WM_STATE", False);
  652. Atom atomFullscreen = XInternAtom(__display, "_NET_WM_STATE_FULLSCREEN", False);
  653. memset(&xev, 0, sizeof(xev));
  654. xev.type = ClientMessage;
  655. xev.xclient.window = __window;
  656. xev.xclient.message_type = atomWm_state;
  657. xev.xclient.format = 32;
  658. xev.xclient.data.l[0] = 1;
  659. xev.xclient.data.l[1] = atomFullscreen;
  660. xev.xclient.data.l[2] = 0;
  661. XSendEvent(__display, DefaultRootWindow(__display), false, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
  662. }
  663. XStoreName(__display, __window, title ? title : "");
  664. __context = glXCreateContext(__display, visualInfo, NULL, True);
  665. if (!__context)
  666. {
  667. perror("glXCreateContext");
  668. return NULL;
  669. }
  670. glXMakeCurrent(__display, __window, __context);
  671. // Use OpenGL 2.x with GLEW
  672. glewExperimental = GL_TRUE;
  673. GLenum glewStatus = glewInit();
  674. if (glewStatus != GLEW_OK)
  675. {
  676. perror("glewInit");
  677. return NULL;
  678. }
  679. // GL Version
  680. int versionGL[2] = {-1, -1};
  681. glGetIntegerv(GL_MAJOR_VERSION, versionGL);
  682. glGetIntegerv(GL_MINOR_VERSION, versionGL + 1);
  683. printf("GL version: %d.%d\n", versionGL[0], versionGL[1]);
  684. // TODO: Get this workings
  685. if (glXSwapIntervalEXT)
  686. glXSwapIntervalEXT(__display, __window, __vsync ? 1 : 0);
  687. else if(glXSwapIntervalMESA)
  688. glXSwapIntervalMESA(__vsync ? 1 : 0);
  689. return platform;
  690. }
  691. void cleanupX11()
  692. {
  693. if (__display)
  694. {
  695. glXMakeCurrent(__display, None, NULL);
  696. if (__context)
  697. glXDestroyContext(__display, __context);
  698. if (__window)
  699. XDestroyWindow(__display, __window);
  700. XCloseDisplay(__display);
  701. }
  702. }
  703. double timespec2millis(struct timespec *a)
  704. {
  705. GP_ASSERT(a);
  706. return (1000.0 * a->tv_sec) + (0.000001 * a->tv_nsec);
  707. }
  708. void updateWindowSize()
  709. {
  710. GP_ASSERT(__display);
  711. GP_ASSERT(__window);
  712. XWindowAttributes windowAttrs;
  713. XGetWindowAttributes(__display, __window, &windowAttrs);
  714. __windowSize[0] = windowAttrs.width;
  715. __windowSize[1] = windowAttrs.height;
  716. }
  717. // Will need to be dynamic, also should be handled in Gamepad class
  718. static const GamepadInfoEntry gamepadLookupTable[] =
  719. {
  720. {0x0,0x0,"GENERIC XBOX360",2,6,20,2,
  721. (GamepadJoystickAxisInfo[]) {
  722. {0,0,GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  723. {1,0,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  724. {2,1,GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  725. {3,1,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  726. {4,2,GP_AXIS_IS_TRIGGER,0,0,2240,ZERO_TO_POS},
  727. {5,2,GP_AXIS_IS_TRIGGER,1,0,2240,ZERO_TO_POS},
  728. {-1,0,0,0,0,0,NEG_TO_POS}
  729. },
  730. (long[]) {
  731. -1,
  732. -1,
  733. -1,
  734. -1,
  735. -1,
  736. Gamepad::BUTTON_UP,
  737. Gamepad::BUTTON_DOWN,
  738. Gamepad::BUTTON_LEFT,
  739. Gamepad::BUTTON_RIGHT,
  740. Gamepad::BUTTON_MENU2,
  741. Gamepad::BUTTON_MENU1,
  742. Gamepad::BUTTON_L3,
  743. Gamepad::BUTTON_R3,
  744. Gamepad::BUTTON_L1,
  745. Gamepad::BUTTON_R1,
  746. Gamepad::BUTTON_MENU3,
  747. Gamepad::BUTTON_A,
  748. Gamepad::BUTTON_B,
  749. Gamepad::BUTTON_X,
  750. Gamepad::BUTTON_Y
  751. }
  752. },
  753. {0x79,0x6,"DragonRise Inc. Generic USB Joystick",2,7,12,0,
  754. (GamepadJoystickAxisInfo[]) {
  755. {0,1, GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  756. {1,1,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  757. {2,0,GP_AXIS_SKIP,0,0,2240,NEG_TO_POS},
  758. {3,0,GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  759. {4,0,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  760. {5,2,GP_AXIS_IS_DPAD, Gamepad::BUTTON_RIGHT, Gamepad::BUTTON_LEFT,2240,NEG_TO_POS},
  761. {6,2,GP_AXIS_IS_DPAD, Gamepad::BUTTON_DOWN, Gamepad::BUTTON_UP,2240,NEG_TO_POS},
  762. {-1,0,0,0,0,0,NEG_TO_POS}
  763. },
  764. (long[]) {
  765. Gamepad::BUTTON_Y,
  766. Gamepad::BUTTON_B,
  767. Gamepad::BUTTON_A,
  768. Gamepad::BUTTON_X,
  769. Gamepad::BUTTON_L1,
  770. Gamepad::BUTTON_R1,
  771. Gamepad::BUTTON_L2,
  772. Gamepad::BUTTON_R2,
  773. Gamepad::BUTTON_MENU1,
  774. Gamepad::BUTTON_MENU2,
  775. Gamepad::BUTTON_L3,
  776. Gamepad::BUTTON_R3,
  777. }
  778. },
  779. {0x54c,0x268,"Sony Corp. Batoh Device / PlayStation 3 Controller",2,27,19,2,
  780. (GamepadJoystickAxisInfo[]) {
  781. {0,0,GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  782. {1,0,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  783. {2,1,GP_AXIS_IS_XAXIS,0,0,2240,NEG_TO_POS},
  784. {3,1,GP_AXIS_IS_NEG,0,0,2240,NEG_TO_POS},
  785. {12,1,GP_AXIS_IS_TRIGGER,0,0,2240,ZERO_TO_POS},
  786. {13,2,GP_AXIS_IS_TRIGGER,1,0,2240,ZERO_TO_POS},
  787. {-1,0,0,0,0,0,NEG_TO_POS}
  788. },
  789. (long[]) {
  790. Gamepad::BUTTON_MENU1,
  791. Gamepad::BUTTON_L3,
  792. Gamepad::BUTTON_R3,
  793. Gamepad::BUTTON_MENU2,
  794. Gamepad::BUTTON_UP,
  795. Gamepad::BUTTON_RIGHT,
  796. Gamepad::BUTTON_DOWN,
  797. Gamepad::BUTTON_LEFT,
  798. Gamepad::BUTTON_L2, //Use Trigger Instead of BUTTON_L2? or both should be called
  799. Gamepad::BUTTON_R2, //Use Trigger Instead of BUTTON_R2? or both should be called
  800. Gamepad::BUTTON_L1,
  801. Gamepad::BUTTON_R1,
  802. Gamepad::BUTTON_Y,
  803. Gamepad::BUTTON_B,
  804. Gamepad::BUTTON_A,
  805. Gamepad::BUTTON_X,
  806. Gamepad::BUTTON_MENU3,
  807. -1,
  808. -1
  809. }
  810. }
  811. };
  812. bool isGamepadDevRegistered(dev_t devId)
  813. {
  814. for(list<ConnectedGamepadDevInfo>::iterator it = __connectedGamepads.begin(); it != __connectedGamepads.end();++it)
  815. {
  816. if(devId == (*it).deviceId) return true;
  817. }
  818. return false;
  819. }
  820. void closeGamepad(const ConnectedGamepadDevInfo& gamepadDevInfo)
  821. {
  822. ::close(gamepadDevInfo.fd);
  823. }
  824. void unregisterGamepad(GamepadHandle handle)
  825. {
  826. for(list<ConnectedGamepadDevInfo>::iterator it = __connectedGamepads.begin(); it != __connectedGamepads.end();++it)
  827. {
  828. if(handle == (*it).fd)
  829. {
  830. closeGamepad(*it);
  831. __connectedGamepads.erase(it);
  832. return;
  833. }
  834. }
  835. }
  836. void closeAllGamepads()
  837. {
  838. for(list<ConnectedGamepadDevInfo>::iterator it = __connectedGamepads.begin(); it != __connectedGamepads.end();++it)
  839. {
  840. closeGamepad(*it);
  841. __connectedGamepads.erase(it);
  842. }
  843. }
  844. const GamepadInfoEntry& getGamepadMappedInfo(unsigned int vendorId, unsigned int productId, unsigned int numberOfAxes, unsigned int numberOfButtons)
  845. {
  846. for(int i=0;i<sizeof(gamepadLookupTable)/sizeof(GamepadInfoEntry);i++)
  847. {
  848. const GamepadInfoEntry& curEntry = gamepadLookupTable[i];
  849. if(curEntry.vendorId == vendorId && curEntry.productId == productId)
  850. {
  851. return curEntry;
  852. }
  853. }
  854. for(int i=0;i<sizeof(gamepadLookupTable)/sizeof(GamepadInfoEntry);i++)
  855. {
  856. const GamepadInfoEntry& curEntry = gamepadLookupTable[i];
  857. if(curEntry.vendorId == 0 && curEntry.productId == 0 && curEntry.numberOfAxes == numberOfAxes && curEntry.numberOfButtons == numberOfButtons)
  858. {
  859. return curEntry;
  860. }
  861. }
  862. return gamepadLookupTable[0];
  863. }
  864. const GamepadInfoEntry& getGamepadMappedInfo(const GamepadHandle handle)
  865. {
  866. GP_ASSERT(handle >= 0);
  867. for(list<ConnectedGamepadDevInfo>::iterator it = __connectedGamepads.begin(); it != __connectedGamepads.end();++it)
  868. {
  869. if(handle == (*it).fd)
  870. {
  871. return it->gamepadInfo;
  872. }
  873. }
  874. GP_WARN("Gamepad not connected but yet trying to get its data. Falling back to generic one.");
  875. return gamepadLookupTable[0];
  876. }
  877. const GamepadJoystickAxisInfo* tryGetGamepadMappedAxisInfo(const GamepadInfoEntry& gpinfo, unsigned int axisNumber)
  878. {
  879. if(axisNumber >= 0 && axisNumber < gpinfo.numberOfAxes)
  880. {
  881. int i = 0;
  882. while(true)
  883. {
  884. const GamepadJoystickAxisInfo* curAxisInfo = &gpinfo.axes[i++];
  885. if(curAxisInfo->axisIndex == axisNumber)
  886. return curAxisInfo;
  887. else if(curAxisInfo->axisIndex < 0)
  888. return NULL;
  889. }
  890. }
  891. return NULL;
  892. }
  893. bool tryGetGamepadMappedButton(const GamepadInfoEntry& gpinfo, unsigned long btnNumber, long& outMap)
  894. {
  895. if(btnNumber >= 0 && btnNumber < gpinfo.numberOfButtons )
  896. {
  897. if(gpinfo.buttons[btnNumber] >= 0)
  898. {
  899. outMap = gpinfo.buttons[btnNumber];
  900. return true;
  901. }
  902. else
  903. {
  904. return false;
  905. }
  906. }
  907. GP_WARN("Unmapped gamepad button: %u.",btnNumber);
  908. return false;
  909. }
  910. unsigned int readIntegerGamepadIdPropery(const char* sysFSIdPath, const char* propertyName)
  911. {
  912. unsigned int ret = 0;
  913. try {
  914. ifstream propStream;
  915. propStream.open((string(sysFSIdPath) + propertyName).c_str(),ifstream::in);
  916. propStream >> std::hex >> ret;
  917. propStream.close();
  918. } catch (exception e) {
  919. GP_WARN("Could not read propery from SysFS for Gamepad: %s", propertyName);
  920. }
  921. return ret;
  922. }
  923. bool isBlackListed(unsigned int vendorId, unsigned int productId)
  924. {
  925. switch(vendorId)
  926. {
  927. case 0x0e0f: //virtual machine devices
  928. if(productId == 0x0003) // Virtual Mouse
  929. return true;
  930. }
  931. return false;
  932. }
  933. void handleConnectedGamepad(dev_t devId, const char* devPath, const char* sysFSIdPath)
  934. {
  935. GP_ASSERT(devPath);
  936. unsigned int vendorId =readIntegerGamepadIdPropery(sysFSIdPath,"vendor");
  937. unsigned int productId =readIntegerGamepadIdPropery(sysFSIdPath,"product");
  938. if(isBlackListed(vendorId,productId)) return;
  939. GamepadHandle handle = ::open(devPath,O_RDONLY | O_NONBLOCK);
  940. if(handle < 0)
  941. {
  942. GP_WARN("Could not open Gamepad device.");
  943. return;
  944. }
  945. if(!(fcntl(handle, F_GETFL) != -1 || errno != EBADF))
  946. return;
  947. char axesNum, btnsNum, name[256];
  948. ioctl(handle, JSIOCGNAME(256), name);
  949. ioctl (handle, JSIOCGAXES, &axesNum);
  950. ioctl (handle, JSIOCGBUTTONS, &btnsNum);
  951. const GamepadInfoEntry& gpInfo = getGamepadMappedInfo(vendorId,productId,(unsigned int)axesNum,(unsigned int)btnsNum);
  952. unsigned int numJS = gpInfo.numberOfJS;
  953. unsigned int numTR = gpInfo.numberOfTriggers;
  954. Platform::gamepadEventConnectedInternal(handle,btnsNum,numJS,numTR,vendorId,productId,"",name);
  955. ConnectedGamepadDevInfo info = {devId,handle,gpInfo};
  956. __connectedGamepads.push_back(info);
  957. }
  958. static float normalizeJoystickAxis(int axisValue, int deadZone, bool zeroToOne)
  959. {
  960. int absAxisValue = 0;
  961. if(zeroToOne)
  962. absAxisValue = (axisValue + 32767) / 2.0;
  963. else
  964. absAxisValue = abs(axisValue);
  965. if (absAxisValue < deadZone)
  966. {
  967. return 0.0f;
  968. }
  969. else
  970. {
  971. int maxVal = 0;
  972. int value = 0;
  973. if(!zeroToOne)
  974. {
  975. value = axisValue;
  976. if (value < 0)
  977. {
  978. value = -1;
  979. maxVal = 32768;
  980. }
  981. else if (value > 0)
  982. {
  983. value = 1;
  984. maxVal = 32767;
  985. }
  986. else
  987. {
  988. return 0.0f;
  989. }
  990. }
  991. else
  992. {
  993. value = 1;
  994. maxVal = 32767;
  995. }
  996. float ret = value * (absAxisValue - deadZone) / (float)(maxVal - deadZone);
  997. return ret;
  998. }
  999. }
  1000. void enumGamepads()
  1001. {
  1002. const int maxDevs = 16;
  1003. const char* devPathFormat = "/dev/input/js%u";
  1004. const char* sysfsPathFormat = "/sys/class/input/js%u/device/id/";
  1005. char curDevPath[20];
  1006. for(int i=0;i<maxDevs;i++)
  1007. {
  1008. sprintf(curDevPath,devPathFormat,i);
  1009. struct stat gpstat;
  1010. if(::stat(curDevPath,&gpstat) == 0)
  1011. {
  1012. dev_t devid = gpstat.st_rdev;
  1013. if(!isGamepadDevRegistered(devid))
  1014. {
  1015. char cursysFSPath[35];
  1016. sprintf(cursysFSPath,sysfsPathFormat,i);
  1017. handleConnectedGamepad(devid,curDevPath,cursysFSPath);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. void gamepadHandlingLoop()
  1023. {
  1024. enumGamepads();
  1025. }
  1026. int Platform::enterMessagePump()
  1027. {
  1028. GP_ASSERT(_game);
  1029. updateWindowSize();
  1030. static bool shiftDown = false;
  1031. static bool capsOn = false;
  1032. static XEvent evt;
  1033. // Get the initial time.
  1034. clock_gettime(CLOCK_REALTIME, &__timespec);
  1035. __timeStart = timespec2millis(&__timespec);
  1036. __timeAbsolute = 0L;
  1037. // Run the game.
  1038. _game->run();
  1039. // Setup select for message handling (to allow non-blocking)
  1040. int x11_fd = ConnectionNumber(__display);
  1041. pollfd xpolls[1];
  1042. xpolls[0].fd = x11_fd;
  1043. xpolls[0].events = POLLIN|POLLPRI;
  1044. // Message loop.
  1045. while (true)
  1046. {
  1047. poll( xpolls, 1, 16 );
  1048. // handle all pending events in one block
  1049. while (XPending(__display))
  1050. {
  1051. XNextEvent(__display, &evt);
  1052. switch (evt.type)
  1053. {
  1054. case ClientMessage:
  1055. {
  1056. // Handle destroy window message correctly
  1057. if (evt.xclient.data.l[0] == __atomWmDeleteWindow)
  1058. {
  1059. _game->exit();
  1060. }
  1061. }
  1062. break;
  1063. case DestroyNotify :
  1064. {
  1065. cleanupX11();
  1066. exit(0);
  1067. }
  1068. break;
  1069. case Expose:
  1070. {
  1071. updateWindowSize();
  1072. }
  1073. break;
  1074. case KeyPress:
  1075. {
  1076. KeySym sym = XLookupKeysym(&evt.xkey, (evt.xkey.state & shiftDown) ? 1 : 0);
  1077. //TempSym needed because XConvertCase operates on two keysyms: One lower and the other upper, we are only interested in the upper case
  1078. KeySym tempSym;
  1079. if (capsOn && !shiftDown)
  1080. XConvertCase(sym, &tempSym, &sym);
  1081. Keyboard::Key key = getKey(sym);
  1082. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_PRESS, key);
  1083. if (key == Keyboard::KEY_CAPS_LOCK)
  1084. capsOn = !capsOn;
  1085. if (key == Keyboard::KEY_SHIFT)
  1086. shiftDown = true;
  1087. if (int character = getUnicode(key))
  1088. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, character);
  1089. }
  1090. break;
  1091. case KeyRelease:
  1092. {
  1093. //detect and drop repeating keystrokes (no other way to do this using the event interface)
  1094. XEvent next;
  1095. if ( XPending(__display) )
  1096. {
  1097. XPeekEvent(__display,&next);
  1098. if ( next.type == KeyPress
  1099. && next.xkey.time == evt.xkey.time
  1100. && next.xkey.keycode == evt.xkey.keycode )
  1101. {
  1102. XNextEvent(__display,&next);
  1103. continue;
  1104. }
  1105. }
  1106. KeySym sym = XLookupKeysym(&evt.xkey, 0);
  1107. Keyboard::Key key = getKey(sym);
  1108. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_RELEASE, key);
  1109. if (key == Keyboard::KEY_SHIFT)
  1110. shiftDown = false;
  1111. }
  1112. break;
  1113. case ButtonPress:
  1114. {
  1115. gameplay::Mouse::MouseEvent mouseEvt;
  1116. switch (evt.xbutton.button)
  1117. {
  1118. case 1:
  1119. mouseEvt = gameplay::Mouse::MOUSE_PRESS_LEFT_BUTTON;
  1120. break;
  1121. case 2:
  1122. mouseEvt = gameplay::Mouse::MOUSE_PRESS_MIDDLE_BUTTON;
  1123. break;
  1124. case 3:
  1125. mouseEvt = gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON;
  1126. break;
  1127. case 4:
  1128. case 5:
  1129. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_WHEEL,
  1130. evt.xbutton.x, evt.xbutton.y,
  1131. evt.xbutton.button == Button4 ? 1 : -1);
  1132. break;
  1133. default:
  1134. break;
  1135. }
  1136. if (!gameplay::Platform::mouseEventInternal(mouseEvt, evt.xbutton.x, evt.xbutton.y, 0))
  1137. {
  1138. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_PRESS, evt.xbutton.x, evt.xbutton.y, 0, true);
  1139. }
  1140. }
  1141. break;
  1142. case ButtonRelease:
  1143. {
  1144. gameplay::Mouse::MouseEvent mouseEvt;
  1145. switch (evt.xbutton.button)
  1146. {
  1147. case 1:
  1148. mouseEvt = gameplay::Mouse::MOUSE_RELEASE_LEFT_BUTTON;
  1149. break;
  1150. case 2:
  1151. mouseEvt = gameplay::Mouse::MOUSE_RELEASE_MIDDLE_BUTTON;
  1152. break;
  1153. case 3:
  1154. mouseEvt = gameplay::Mouse::MOUSE_RELEASE_RIGHT_BUTTON;
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. if (!gameplay::Platform::mouseEventInternal(mouseEvt, evt.xbutton.x, evt.xbutton.y, 0))
  1160. {
  1161. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_RELEASE, evt.xbutton.x, evt.xbutton.y, 0, true);
  1162. }
  1163. }
  1164. break;
  1165. case MotionNotify:
  1166. {
  1167. int x = evt.xmotion.x;
  1168. int y = evt.xmotion.y;
  1169. if (__mouseCaptured)
  1170. {
  1171. if (x == __mouseCapturePointX && y == __mouseCapturePointY)
  1172. {
  1173. // Discard the first MotionNotify following capture
  1174. // since it contains bogus x,y data.
  1175. break;
  1176. }
  1177. // Convert to deltas
  1178. x -= __mouseCapturePointX;
  1179. y -= __mouseCapturePointY;
  1180. // Warp mouse back to center of screen.
  1181. XWarpPointer(__display, None, __window, 0, 0, 0, 0, __mouseCapturePointX, __mouseCapturePointY);
  1182. }
  1183. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_MOVE, x, y, 0))
  1184. {
  1185. if (evt.xmotion.state & Button1Mask)
  1186. {
  1187. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0, true);
  1188. }
  1189. }
  1190. }
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. }
  1196. gamepadHandlingLoop();
  1197. if (_game)
  1198. {
  1199. // Game state will be uninitialized if game was closed through Game::exit()
  1200. if (_game->getState() == Game::UNINITIALIZED)
  1201. break;
  1202. _game->frame();
  1203. }
  1204. glXSwapBuffers(__display, __window);
  1205. }
  1206. cleanupX11();
  1207. return 0;
  1208. }
  1209. void Platform::signalShutdown()
  1210. {
  1211. }
  1212. bool Platform::canExit()
  1213. {
  1214. return true;
  1215. }
  1216. unsigned int Platform::getDisplayWidth()
  1217. {
  1218. return __windowSize[0];
  1219. }
  1220. unsigned int Platform::getDisplayHeight()
  1221. {
  1222. return __windowSize[1];
  1223. }
  1224. double Platform::getAbsoluteTime()
  1225. {
  1226. clock_gettime(CLOCK_REALTIME, &__timespec);
  1227. double now = timespec2millis(&__timespec);
  1228. __timeAbsolute = now - __timeStart;
  1229. return __timeAbsolute;
  1230. }
  1231. void Platform::setAbsoluteTime(double time)
  1232. {
  1233. __timeAbsolute = time;
  1234. }
  1235. bool Platform::isVsync()
  1236. {
  1237. return __vsync;
  1238. }
  1239. void Platform::setVsync(bool enable)
  1240. {
  1241. if (glXSwapIntervalEXT)
  1242. glXSwapIntervalEXT(__display, __window, __vsync ? 1 : 0);
  1243. else if(glXSwapIntervalMESA)
  1244. glXSwapIntervalMESA(__vsync ? 1 : 0);
  1245. __vsync = enable;
  1246. }
  1247. void Platform::swapBuffers()
  1248. {
  1249. glXSwapBuffers(__display, __window);
  1250. }
  1251. void Platform::sleep(long ms)
  1252. {
  1253. usleep(ms * 1000);
  1254. }
  1255. void Platform::setMultiSampling(bool enabled)
  1256. {
  1257. if (enabled == __multiSampling)
  1258. {
  1259. return;
  1260. }
  1261. //todo
  1262. __multiSampling = enabled;
  1263. }
  1264. bool Platform::isMultiSampling()
  1265. {
  1266. return __multiSampling;
  1267. }
  1268. void Platform::setMultiTouch(bool enabled)
  1269. {
  1270. // not supported
  1271. }
  1272. bool Platform::isMultiTouch()
  1273. {
  1274. false;
  1275. }
  1276. bool Platform::hasAccelerometer()
  1277. {
  1278. return false;
  1279. }
  1280. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1281. {
  1282. GP_ASSERT(pitch);
  1283. GP_ASSERT(roll);
  1284. *pitch = 0;
  1285. *roll = 0;
  1286. }
  1287. void Platform::getSensorValues(float* accelX, float* accelY, float* accelZ, float* gyroX, float* gyroY, float* gyroZ)
  1288. {
  1289. if (accelX)
  1290. {
  1291. *accelX = 0;
  1292. }
  1293. if (accelY)
  1294. {
  1295. *accelY = 0;
  1296. }
  1297. if (accelZ)
  1298. {
  1299. *accelZ = 0;
  1300. }
  1301. if (gyroX)
  1302. {
  1303. *gyroX = 0;
  1304. }
  1305. if (gyroY)
  1306. {
  1307. *gyroY = 0;
  1308. }
  1309. if (gyroZ)
  1310. {
  1311. *gyroZ = 0;
  1312. }
  1313. }
  1314. void Platform::getArguments(int* argc, char*** argv)
  1315. {
  1316. if (argc)
  1317. *argc = __argc;
  1318. if (argv)
  1319. *argv = __argv;
  1320. }
  1321. bool Platform::hasMouse()
  1322. {
  1323. return true;
  1324. }
  1325. void Platform::setMouseCaptured(bool captured)
  1326. {
  1327. if (captured != __mouseCaptured)
  1328. {
  1329. if (captured)
  1330. {
  1331. // Hide the cursor and warp it to the center of the screen
  1332. __mouseCapturePointX = getDisplayWidth() / 2;
  1333. __mouseCapturePointY = getDisplayHeight() / 2;
  1334. setCursorVisible(false);
  1335. XWarpPointer(__display, None, __window, 0, 0, 0, 0, __mouseCapturePointX, __mouseCapturePointY);
  1336. }
  1337. else
  1338. {
  1339. // Restore cursor
  1340. XWarpPointer(__display, None, __window, 0, 0, 0, 0, __mouseCapturePointX, __mouseCapturePointY);
  1341. setCursorVisible(true);
  1342. }
  1343. __mouseCaptured = captured;
  1344. }
  1345. }
  1346. bool Platform::isMouseCaptured()
  1347. {
  1348. return __mouseCaptured;
  1349. }
  1350. void Platform::setCursorVisible(bool visible)
  1351. {
  1352. if (visible != __cursorVisible)
  1353. {
  1354. if (visible)
  1355. {
  1356. Cursor invisibleCursor;
  1357. Pixmap bitmapNoData;
  1358. XColor black;
  1359. static char noData[] = {0, 0, 0, 0, 0, 0, 0, 0};
  1360. black.red = black.green = black.blue = 0;
  1361. bitmapNoData = XCreateBitmapFromData(__display, __window, noData, 8, 8);
  1362. invisibleCursor = XCreatePixmapCursor(__display, bitmapNoData, bitmapNoData, &black, &black, 0, 0);
  1363. XDefineCursor(__display, __window, invisibleCursor);
  1364. XFreeCursor(__display, invisibleCursor);
  1365. XFreePixmap(__display, bitmapNoData);
  1366. }
  1367. else
  1368. {
  1369. XUndefineCursor(__display, __window);
  1370. }
  1371. XFlush(__display);
  1372. __cursorVisible = visible;
  1373. }
  1374. }
  1375. bool Platform::isCursorVisible()
  1376. {
  1377. return __cursorVisible;
  1378. }
  1379. void Platform::displayKeyboard(bool display)
  1380. {
  1381. // not supported
  1382. }
  1383. void Platform::shutdownInternal()
  1384. {
  1385. closeAllGamepads();
  1386. Game::getInstance()->shutdown();
  1387. }
  1388. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1389. {
  1390. return false;
  1391. }
  1392. void Platform::registerGesture(Gesture::GestureEvent evt)
  1393. {
  1394. }
  1395. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1396. {
  1397. }
  1398. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1399. {
  1400. return false;
  1401. }
  1402. void Platform::pollGamepadState(Gamepad* gamepad)
  1403. {
  1404. GP_ASSERT(gamepad);
  1405. struct js_event jevent;
  1406. const GamepadInfoEntry& gpInfo = getGamepadMappedInfo(gamepad->_handle);
  1407. while (read(gamepad->_handle, &jevent, sizeof(struct js_event)) > 0)
  1408. {
  1409. switch (jevent.type)
  1410. {
  1411. case JS_EVENT_BUTTON:
  1412. case JS_EVENT_BUTTON | JS_EVENT_INIT:
  1413. {
  1414. long curMappingIndex = -1;
  1415. if(tryGetGamepadMappedButton(gpInfo, jevent.number, curMappingIndex))
  1416. {
  1417. unsigned int buttons = 0;
  1418. if (jevent.value)
  1419. buttons |= (1 << curMappingIndex);
  1420. else
  1421. buttons &= ~(1 << curMappingIndex);
  1422. gamepad->setButtons(buttons);
  1423. }
  1424. break;
  1425. }
  1426. case JS_EVENT_AXIS:
  1427. case JS_EVENT_AXIS | JS_EVENT_INIT:
  1428. {
  1429. if(jevent.number < gpInfo.numberOfAxes)
  1430. {
  1431. const GamepadJoystickAxisInfo* jsInfo = tryGetGamepadMappedAxisInfo(gpInfo,jevent.number);
  1432. if(jsInfo)
  1433. {
  1434. float val = normalizeJoystickAxis(jevent.value,jsInfo->deadZone,jsInfo->mapFunc == ZERO_TO_POS);
  1435. if(!(jsInfo->flags & GP_AXIS_SKIP))
  1436. {
  1437. if((jsInfo->flags & GP_AXIS_IS_NEG))
  1438. val = -1.0f * val;
  1439. bool not_js_axis = false;
  1440. if((jsInfo->flags & GP_AXIS_IS_DPAD))
  1441. {
  1442. unsigned int buttons = 0;
  1443. if(jevent.value != 0)
  1444. buttons |= (1 << (jevent.value > 0 ? jsInfo->mappedPosArg : jsInfo->mappedNegArg));
  1445. else
  1446. {
  1447. buttons &= ~(1 << jsInfo->mappedPosArg);
  1448. buttons &= ~(1 << jsInfo->mappedNegArg);
  1449. }
  1450. gamepad->setButtons(buttons);
  1451. not_js_axis = true;
  1452. }
  1453. if((jsInfo->flags & GP_AXIS_IS_TRIGGER))
  1454. {
  1455. gamepad->setTriggerValue(jsInfo->mappedPosArg, val);
  1456. not_js_axis = true;
  1457. }
  1458. if(!not_js_axis)
  1459. {
  1460. Vector2 jsVals;
  1461. gamepad->getJoystickValues(jsInfo->joystickIndex,&jsVals);
  1462. if(jsInfo->flags & GP_AXIS_IS_XAXIS)
  1463. jsVals.x = val;
  1464. else
  1465. jsVals.y = val;
  1466. gamepad->setJoystickValue(jsInfo->joystickIndex,jsVals.x,jsVals.y);
  1467. }
  1468. }
  1469. }
  1470. }
  1471. }
  1472. break;
  1473. default:
  1474. GP_WARN("unhandled gamepad event: %x\n", jevent.type);
  1475. }
  1476. }
  1477. if(errno == ENODEV)
  1478. {
  1479. unregisterGamepad(gamepad->_handle);
  1480. gamepadEventDisconnectedInternal(gamepad->_handle);
  1481. }
  1482. }
  1483. bool Platform::launchURL(const char* url)
  1484. {
  1485. if (url == NULL || *url == '\0')
  1486. return false;
  1487. int len = strlen(url);
  1488. char* cmd = new char[11 + len];
  1489. sprintf(cmd, "xdg-open %s", url);
  1490. int r = system(cmd);
  1491. SAFE_DELETE_ARRAY(cmd);
  1492. return (r == 0);
  1493. }
  1494. std::string Platform::displayFileDialog(size_t mode, const char* title, const char* filterDescription, const char* filterExtensions, const char* initialDirectory)
  1495. {
  1496. return "";
  1497. }
  1498. }
  1499. #endif