PlatformLinux.cpp 46 KB

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