PlatformLinux.cpp 60 KB

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