PlatformLinux.cpp 59 KB

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