glue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. Copyright (c) 2014-2022 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include "SDL.h"
  19. #include "SDL_events.h"
  20. #include "SDL_keyboard.h"
  21. #include <brl.mod/blitz.mod/blitz.h>
  22. #include <brl.mod/event.mod/event.h> //event enums
  23. #include <brl.mod/keycodes.mod/keycodes.h> //keycode enums
  24. /* System stuff */
  25. #define TOUCH_MULTIPLIER 10000
  26. static int mouse_button_map[] = {0, 1, 3, 2, 4, 5};
  27. int brl_event_EmitEvent( BBObject *event );
  28. BBObject *brl_event_CreateEvent( int id,BBObject *source,int data,int mods,int x,int y,BBObject *extra );
  29. int sdl_sdlsystem_TSDLSystemDriver__eventFilter(BBObject * userdata, int eventType);
  30. BBObject * sdl_sdlsystem_TSDLMultiGesture__getGesture(BBLONG touchId, int x, int y, float dTheta, float dDist, int numFingers);
  31. void sdl_sdlsystem_TSDLMultiGesture__freeGesture(BBObject * gesture);
  32. void bbSDLSystemEmitEvent( int id,BBObject *source,int data,int mods,int x,int y,BBObject *extra ){
  33. BBObject *event=brl_event_CreateEvent( id,source,data,mods,x,y,extra );
  34. brl_event_EmitEvent( event );
  35. }
  36. int mapkey(SDL_Keycode keycode);
  37. int mapmods(int keymods);
  38. int bmx_SDL_GetDisplayWidth(int display) {
  39. SDL_DisplayMode mode;
  40. SDL_GetCurrentDisplayMode(display, &mode);
  41. return mode.w;
  42. }
  43. int bmx_SDL_GetDisplayHeight(int display) {
  44. SDL_DisplayMode mode;
  45. SDL_GetCurrentDisplayMode(display, &mode);
  46. return mode.h;
  47. }
  48. int bmx_SDL_GetDisplayDepth(int display) {
  49. SDL_DisplayMode mode;
  50. SDL_GetCurrentDisplayMode(display, &mode);
  51. return (mode.format >> 8) & 0xFF;
  52. }
  53. int bmx_SDL_GetDisplayhertz(int display) {
  54. SDL_DisplayMode mode;
  55. SDL_GetCurrentDisplayMode(display, &mode);
  56. return mode.refresh_rate;
  57. }
  58. static void bmx_SDL_EmitSDLEvent( SDL_Event *event, BBObject *source ) {
  59. int data = 0;
  60. int mods = 0;
  61. int i = 0;
  62. switch (event->type) {
  63. case SDL_QUIT:
  64. bbSDLSystemEmitEvent(BBEVENT_APPTERMINATE, source, 0, 0, 0, 0, &bbNullObject);
  65. return;
  66. case SDL_KEYDOWN:
  67. i = 0;
  68. // some keys are not raised as text input events..
  69. // so we will push them ourselves.
  70. switch (event->key.keysym.sym) {
  71. case SDLK_BACKSPACE:
  72. i = 0x08;
  73. break;
  74. case SDLK_DELETE:
  75. i = 0x7f;
  76. break;
  77. case SDLK_RETURN:
  78. case SDLK_RETURN2:
  79. case SDLK_KP_ENTER:
  80. i = 0x0d;
  81. break;
  82. case SDLK_ESCAPE:
  83. i = 0x1b;
  84. break;
  85. }
  86. // intentional fall-through...
  87. case SDL_KEYUP:
  88. data = mapkey(event->key.keysym.sym);
  89. mods = mapmods(event->key.keysym.mod);
  90. // only generate for keys we support
  91. if (data || i) {
  92. if (event->key.repeat) {
  93. bbSDLSystemEmitEvent( BBEVENT_KEYREPEAT,source,data,mods,0,0,&bbNullObject );
  94. if (i) {
  95. bbSDLSystemEmitEvent( BBEVENT_KEYCHAR,source,i,0,0,0,&bbNullObject );
  96. }
  97. return;
  98. }
  99. bbSDLSystemEmitEvent( (event->type == SDL_KEYDOWN) ? BBEVENT_KEYDOWN : BBEVENT_KEYUP,source,data,mods,0,0,&bbNullObject );
  100. if (i) {
  101. bbSDLSystemEmitEvent( BBEVENT_KEYCHAR,source,i,0,0,0,&bbNullObject );
  102. }
  103. }
  104. return;
  105. break;
  106. case SDL_TEXTINPUT:
  107. {
  108. BBString * s = bbStringFromUTF8String(event->text.text);
  109. while (i < s->length) {
  110. bbSDLSystemEmitEvent( BBEVENT_KEYCHAR,source,s->buf[i],0,0,0,&bbNullObject );
  111. i++;
  112. }
  113. return;
  114. }
  115. case SDL_MOUSEMOTION:
  116. bbSDLSystemEmitEvent( BBEVENT_MOUSEMOVE,source,0,0,event->motion.x,event->motion.y,&bbNullObject );
  117. return;
  118. case SDL_MOUSEBUTTONDOWN:
  119. case SDL_MOUSEBUTTONUP:
  120. bbSDLSystemEmitEvent( (event->type == SDL_MOUSEBUTTONDOWN) ? BBEVENT_MOUSEDOWN : BBEVENT_MOUSEUP,source,mouse_button_map[event->button.button],0,event->button.x,event->button.y,&bbNullObject );
  121. return;
  122. case SDL_MOUSEWHEEL:
  123. bbSDLSystemEmitEvent( BBEVENT_MOUSEWHEEL,source,(event->wheel.y < 0) ? -1 : 1,0,0,0,&bbNullObject );
  124. return;
  125. case SDL_USEREVENT:
  126. switch (event->user.code) {
  127. case BBEVENT_TIMERTICK:
  128. bbSDLSystemEmitEvent( BBEVENT_TIMERTICK,event->user.data1,((int*)event->user.data2)[0],0,0,0,&bbNullObject );
  129. free(event->user.data2);
  130. return;
  131. case 0x802:
  132. brl_event_EmitEvent( event->user.data1 );
  133. return;
  134. }
  135. case SDL_FINGERMOTION:
  136. {
  137. bbSDLSystemEmitEvent( BBEVENT_TOUCHMOVE, source, event->tfinger.fingerId, 0, event->tfinger.x * TOUCH_MULTIPLIER, event->tfinger.y * TOUCH_MULTIPLIER, &bbNullObject);
  138. return;
  139. }
  140. case SDL_FINGERDOWN:
  141. case SDL_FINGERUP:
  142. {
  143. bbSDLSystemEmitEvent( (event->type == SDL_FINGERDOWN) ? BBEVENT_TOUCHDOWN : BBEVENT_TOUCHUP, source, event->tfinger.fingerId, 0, event->tfinger.x * TOUCH_MULTIPLIER, event->tfinger.y * TOUCH_MULTIPLIER, &bbNullObject );
  144. return;
  145. }
  146. case SDL_MULTIGESTURE:
  147. {
  148. int x = event->mgesture.x * TOUCH_MULTIPLIER;
  149. int y = event->mgesture.y * TOUCH_MULTIPLIER;
  150. BBObject * gesture = sdl_sdlsystem_TSDLMultiGesture__getGesture(event->mgesture.touchId, x, y, event->mgesture.dTheta, event->mgesture.dDist, event->mgesture.numFingers);
  151. bbSDLSystemEmitEvent(BBEVENT_MULTIGESTURE, source, event->mgesture.touchId, 0, x, y, gesture);
  152. sdl_sdlsystem_TSDLMultiGesture__freeGesture(gesture);
  153. return;
  154. }
  155. case SDL_WINDOWEVENT:
  156. switch (event->window.event) {
  157. case SDL_WINDOWEVENT_FOCUS_GAINED:
  158. bbSDLSystemEmitEvent(BBEVENT_APPRESUME, source, 0, 0, 0, 0, &bbNullObject);
  159. return;
  160. case SDL_WINDOWEVENT_FOCUS_LOST:
  161. bbSDLSystemEmitEvent(BBEVENT_APPSUSPEND, source, 0, 0, 0, 0, &bbNullObject);
  162. return;
  163. case SDL_WINDOWEVENT_RESIZED:
  164. bbSDLSystemEmitEvent(BBEVENT_WINDOWSIZE, source, event->window.windowID, 0, event->window.data1, event->window.data2, &bbNullObject);
  165. return;
  166. case SDL_WINDOWEVENT_MOVED:
  167. bbSDLSystemEmitEvent(BBEVENT_WINDOWMOVE, source, event->window.windowID, 0, event->window.data1, event->window.data2, &bbNullObject);
  168. return;
  169. case SDL_WINDOWEVENT_ENTER:
  170. bbSDLSystemEmitEvent(BBEVENT_MOUSEENTER, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  171. return;
  172. case SDL_WINDOWEVENT_LEAVE:
  173. bbSDLSystemEmitEvent(BBEVENT_MOUSELEAVE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  174. return;
  175. case SDL_WINDOWEVENT_MINIMIZED:
  176. bbSDLSystemEmitEvent(BBEVENT_WINDOWMINIMIZE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  177. return;
  178. case SDL_WINDOWEVENT_MAXIMIZED:
  179. bbSDLSystemEmitEvent(BBEVENT_WINDOWMAXIMIZE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  180. return;
  181. case SDL_WINDOWEVENT_RESTORED:
  182. bbSDLSystemEmitEvent(BBEVENT_WINDOWRESTORE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  183. return;
  184. case SDL_WINDOWEVENT_CLOSE:
  185. bbSDLSystemEmitEvent(BBEVENT_WINDOWCLOSE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  186. return;
  187. case SDL_WINDOWEVENT_DISPLAY_CHANGED:
  188. bbSDLSystemEmitEvent(BBEVENT_WINDOWDISPLAYCHANGE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  189. return;
  190. case SDL_WINDOWEVENT_ICCPROF_CHANGED:
  191. bbSDLSystemEmitEvent(BBEVENT_WINDOWICCPROFCHANGE, source, event->window.windowID, 0, 0, 0, &bbNullObject);
  192. return;
  193. }
  194. case SDL_DISPLAYEVENT:
  195. switch (event->display.event) {
  196. case SDL_DISPLAYEVENT_ORIENTATION:
  197. bbSDLSystemEmitEvent(BBEVENT_DISPLAYORIENTATION, source, event->display.display, 0, 0, 0, &bbNullObject);
  198. return;
  199. case SDL_DISPLAYEVENT_CONNECTED:
  200. bbSDLSystemEmitEvent(BBEVENT_DISPLAYCONNECT, source, event->display.display, 0, 0, 0, &bbNullObject);
  201. return;
  202. case SDL_DISPLAYEVENT_DISCONNECTED:
  203. bbSDLSystemEmitEvent(BBEVENT_DISPLAYDISCONNECT, source, event->display.display, 0, 0, 0, &bbNullObject);
  204. return;
  205. case SDL_DISPLAYEVENT_MOVED:
  206. bbSDLSystemEmitEvent(BBEVENT_DISPLAYMOVED, source, event->display.display, 0, 0, 0, &bbNullObject);
  207. return;
  208. }
  209. }
  210. }
  211. typedef void (*EventCallback)(void * userdata, SDL_Event *event, int *ignoreKeyboard, int *ignoreMouse);
  212. typedef struct EventCallbackNode {
  213. EventCallback cb;
  214. void * userdata;
  215. int priority;
  216. struct EventCallbackNode *next;
  217. } EventCallbackNode;
  218. static EventCallbackNode *bmx_eventCallbacks = NULL;
  219. void bmx_SDL_AddEventCallback(EventCallback callback, void *userdata, int priority) {
  220. EventCallbackNode *node = malloc(sizeof(EventCallbackNode));
  221. node->cb = callback;
  222. node->userdata = userdata;
  223. node->priority = priority;
  224. // empty list or higher than head?
  225. if (!bmx_eventCallbacks || priority > bmx_eventCallbacks->priority) {
  226. node->next = bmx_eventCallbacks;
  227. bmx_eventCallbacks = node;
  228. }
  229. else {
  230. // find the insertion point
  231. EventCallbackNode *cur = bmx_eventCallbacks;
  232. while (cur->next && cur->next->priority >= priority) {
  233. cur = cur->next;
  234. }
  235. node->next = cur->next;
  236. cur->next = node;
  237. }
  238. }
  239. void bmx_SDL_RemoveEventCallback(EventCallback callback) {
  240. EventCallbackNode **walk = &bmx_eventCallbacks;
  241. while (*walk) {
  242. if ((*walk)->cb == callback) {
  243. EventCallbackNode *toFree = *walk;
  244. *walk = toFree->next;
  245. free(toFree);
  246. return; // stop after first match
  247. }
  248. walk = &((*walk)->next);
  249. }
  250. }
  251. static int isKeyboardEvent(const SDL_Event *event) {
  252. return event->type == SDL_KEYDOWN
  253. || event->type == SDL_KEYUP
  254. || event->type == SDL_TEXTINPUT
  255. || event->type == SDL_TEXTEDITING;
  256. }
  257. static int isMouseEvent(const SDL_Event *event) {
  258. return event->type == SDL_MOUSEBUTTONDOWN
  259. || event->type == SDL_MOUSEBUTTONUP
  260. || event->type == SDL_MOUSEMOTION
  261. || event->type == SDL_MOUSEWHEEL
  262. || event->type == SDL_FINGERMOTION
  263. || event->type == SDL_FINGERDOWN
  264. || event->type == SDL_FINGERUP;
  265. }
  266. static void bmx_SDL_handleEvent(SDL_Event * event) {
  267. if (bmx_eventCallbacks) {
  268. int eatKeyboard = 0;
  269. int eatMouse = 0;
  270. int keyboardEvent = isKeyboardEvent(event);
  271. int mouseEvent = isMouseEvent(event);
  272. for (EventCallbackNode *n = bmx_eventCallbacks; n; n = n->next) {
  273. // if already eaten, skip this handler
  274. if (eatKeyboard && keyboardEvent) continue;
  275. if (eatMouse && mouseEvent) continue;
  276. int callbackAteKeyboard = 0;
  277. int callbackAteMouse = 0;
  278. n->cb(n->userdata, event, &callbackAteKeyboard, &callbackAteMouse);
  279. // once any handler “eats” a keyboard/mouse event,
  280. // lower-priority ones will be skipped above
  281. if (callbackAteKeyboard) eatKeyboard = 1;
  282. if (callbackAteMouse) eatMouse = 1;
  283. }
  284. if ((keyboardEvent && eatKeyboard) || (mouseEvent && eatMouse)) {
  285. return; // event has been handled, no need to emit
  286. }
  287. bmx_SDL_EmitSDLEvent(event, &bbNullObject);
  288. } else {
  289. bmx_SDL_EmitSDLEvent(event, &bbNullObject);
  290. }
  291. }
  292. void bmx_SDL_Poll() {
  293. SDL_Event event;
  294. while (SDL_PollEvent(&event)) {
  295. bmx_SDL_handleEvent(&event);
  296. }
  297. }
  298. void bmx_SDL_WaitEvent() {
  299. SDL_Event event;
  300. if (SDL_WaitEvent(&event)) {
  301. bmx_SDL_handleEvent(&event);
  302. }
  303. }
  304. int mapkey(SDL_Keycode keycode) {
  305. switch(keycode) {
  306. case SDLK_BACKSPACE:
  307. return KEY_BACKSPACE;
  308. case SDLK_TAB:
  309. return KEY_TAB;
  310. case SDLK_RETURN:
  311. return KEY_ENTER;
  312. case SDLK_ESCAPE:
  313. return KEY_ESC;
  314. case SDLK_SPACE:
  315. return KEY_SPACE;
  316. case SDLK_PAGEUP:
  317. return KEY_PAGEUP;
  318. case SDLK_PAGEDOWN:
  319. return KEY_PAGEDOWN;
  320. case SDLK_END:
  321. return KEY_END;
  322. case SDLK_HOME:
  323. return KEY_HOME;
  324. case SDLK_LEFT:
  325. return KEY_LEFT;
  326. case SDLK_UP:
  327. return KEY_UP;
  328. case SDLK_RIGHT:
  329. return KEY_RIGHT;
  330. case SDLK_DOWN:
  331. return KEY_DOWN;
  332. case SDLK_INSERT:
  333. return KEY_INSERT;
  334. case SDLK_DELETE:
  335. return KEY_DELETE;
  336. case SDLK_0:
  337. return KEY_0;
  338. case SDLK_1:
  339. return KEY_1;
  340. case SDLK_2:
  341. return KEY_2;
  342. case SDLK_3:
  343. return KEY_3;
  344. case SDLK_4:
  345. return KEY_4;
  346. case SDLK_5:
  347. return KEY_5;
  348. case SDLK_6:
  349. return KEY_6;
  350. case SDLK_7:
  351. return KEY_7;
  352. case SDLK_8:
  353. return KEY_8;
  354. case SDLK_9:
  355. return KEY_9;
  356. case SDLK_a:
  357. return KEY_A;
  358. case SDLK_b:
  359. return KEY_B;
  360. case SDLK_c:
  361. return KEY_C;
  362. case SDLK_d:
  363. return KEY_D;
  364. case SDLK_e:
  365. return KEY_E;
  366. case SDLK_f:
  367. return KEY_F;
  368. case SDLK_g:
  369. return KEY_G;
  370. case SDLK_h:
  371. return KEY_H;
  372. case SDLK_i:
  373. return KEY_I;
  374. case SDLK_j:
  375. return KEY_J;
  376. case SDLK_k:
  377. return KEY_K;
  378. case SDLK_l:
  379. return KEY_L;
  380. case SDLK_m:
  381. return KEY_M;
  382. case SDLK_n:
  383. return KEY_N;
  384. case SDLK_o:
  385. return KEY_O;
  386. case SDLK_p:
  387. return KEY_P;
  388. case SDLK_q:
  389. return KEY_Q;
  390. case SDLK_r:
  391. return KEY_R;
  392. case SDLK_s:
  393. return KEY_S;
  394. case SDLK_t:
  395. return KEY_T;
  396. case SDLK_u:
  397. return KEY_U;
  398. case SDLK_v:
  399. return KEY_V;
  400. case SDLK_w:
  401. return KEY_W;
  402. case SDLK_x:
  403. return KEY_X;
  404. case SDLK_y:
  405. return KEY_Y;
  406. case SDLK_z:
  407. return KEY_Z;
  408. case SDLK_LGUI:
  409. return KEY_LSYS;
  410. case SDLK_RGUI:
  411. return KEY_RSYS;
  412. case SDLK_KP_0:
  413. return KEY_NUM0;
  414. case SDLK_KP_1:
  415. return KEY_NUM1;
  416. case SDLK_KP_2:
  417. return KEY_NUM2;
  418. case SDLK_KP_3:
  419. return KEY_NUM3;
  420. case SDLK_KP_4:
  421. return KEY_NUM4;
  422. case SDLK_KP_5:
  423. return KEY_NUM5;
  424. case SDLK_KP_6:
  425. return KEY_NUM6;
  426. case SDLK_KP_7:
  427. return KEY_NUM7;
  428. case SDLK_KP_8:
  429. return KEY_NUM8;
  430. case SDLK_KP_9:
  431. return KEY_NUM9;
  432. case SDLK_KP_MULTIPLY:
  433. return KEY_NUMMULTIPLY;
  434. case SDLK_KP_PLUS:
  435. return KEY_NUMADD;
  436. case SDLK_KP_EQUALS:
  437. return KEY_NUMSLASH;
  438. case SDLK_KP_MINUS:
  439. return KEY_NUMSUBTRACT;
  440. case SDLK_KP_PERIOD:
  441. return KEY_NUMDECIMAL;
  442. case SDLK_KP_DIVIDE:
  443. return KEY_NUMDIVIDE;
  444. case SDLK_KP_ENTER:
  445. return KEY_ENTER;
  446. case SDLK_F1:
  447. return KEY_F1;
  448. case SDLK_F2:
  449. return KEY_F2;
  450. case SDLK_F3:
  451. return KEY_F3;
  452. case SDLK_F4:
  453. return KEY_F4;
  454. case SDLK_F5:
  455. return KEY_F5;
  456. case SDLK_F6:
  457. return KEY_F6;
  458. case SDLK_F7:
  459. return KEY_F7;
  460. case SDLK_F8:
  461. return KEY_F8;
  462. case SDLK_F9:
  463. return KEY_F9;
  464. case SDLK_F10:
  465. return KEY_F10;
  466. case SDLK_F11:
  467. return KEY_F11;
  468. case SDLK_F12:
  469. return KEY_F12;
  470. case SDLK_LSHIFT:
  471. return KEY_LSHIFT;
  472. case SDLK_RSHIFT:
  473. return KEY_RSHIFT;
  474. case SDLK_LCTRL:
  475. return KEY_LCONTROL;
  476. case SDLK_RCTRL:
  477. return KEY_RCONTROL;
  478. case SDLK_LALT:
  479. return KEY_LALT;
  480. case SDLK_RALT:
  481. return KEY_RALT;
  482. case SDLK_AC_BACK:
  483. return KEY_BROWSER_BACK;
  484. case SDLK_AC_FORWARD:
  485. return KEY_BROWSER_FORWARD;
  486. case SDLK_AC_HOME:
  487. return KEY_BROWSER_HOME;
  488. case SDLK_AC_REFRESH:
  489. return KEY_BROWSER_REFRESH;
  490. case SDLK_AC_SEARCH:
  491. return KEY_BROWSER_SEARCH;
  492. case SDLK_AC_STOP:
  493. return KEY_BROWSER_STOP;
  494. case SDLK_BACKQUOTE:
  495. return KEY_TILDE;
  496. case SDLK_MINUS:
  497. return KEY_MINUS;
  498. case SDLK_EQUALS:
  499. return KEY_EQUALS;
  500. case SDLK_LEFTBRACKET:
  501. return KEY_OPENBRACKET;
  502. case SDLK_RIGHTBRACKET:
  503. return KEY_CLOSEBRACKET;
  504. case SDLK_BACKSLASH:
  505. return KEY_BACKSLASH;
  506. case SDLK_SEMICOLON:
  507. return KEY_SEMICOLON;
  508. case SDLK_QUOTE:
  509. return KEY_QUOTES;
  510. case SDLK_COMMA:
  511. return KEY_COMMA;
  512. case SDLK_PERIOD:
  513. return KEY_PERIOD;
  514. case SDLK_SLASH:
  515. return KEY_SLASH;
  516. }
  517. // we don't have a mapping
  518. return 0;
  519. }
  520. int mapmods(int keymods) {
  521. int mod = 0;
  522. if (keymods & KMOD_SHIFT) {
  523. mod |= MODIFIER_SHIFT;
  524. }
  525. if (keymods & KMOD_CTRL) {
  526. mod |= MODIFIER_CONTROL;
  527. }
  528. if (keymods & KMOD_ALT) {
  529. mod |= MODIFIER_OPTION;
  530. }
  531. if (keymods & KMOD_GUI) {
  532. mod |= MODIFIER_SYSTEM;
  533. }
  534. return mod;
  535. }
  536. int bmx_SDL_ShowSimpleMessageBox(BBString * text, BBString * appTitle, int serious) {
  537. int flags = (serious) ? SDL_MESSAGEBOX_WARNING : SDL_MESSAGEBOX_INFORMATION;
  538. char * t = bbStringToUTF8String(appTitle);
  539. char * s = bbStringToUTF8String(text);
  540. int ret = SDL_ShowSimpleMessageBox(flags, t, s, NULL);
  541. bbMemFree(s);
  542. bbMemFree(t);
  543. return ret;
  544. }
  545. int bmx_SDL_ShowMessageBox_confirm(BBString * text, BBString * appTitle, int serious) {
  546. char * t = bbStringToUTF8String(appTitle);
  547. char * s = bbStringToUTF8String(text);
  548. SDL_MessageBoxButtonData buttons[] = {
  549. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "no" },
  550. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" }
  551. };
  552. SDL_MessageBoxData messageboxdata = {
  553. (serious) ? SDL_MESSAGEBOX_WARNING : SDL_MESSAGEBOX_INFORMATION,
  554. NULL, t, s, SDL_arraysize(buttons), buttons, NULL
  555. };
  556. bbMemFree(s);
  557. bbMemFree(t);
  558. int buttonid;
  559. SDL_ShowMessageBox(&messageboxdata, &buttonid);
  560. if (buttonid == 1) {
  561. return 1;
  562. } else {
  563. return 0;
  564. }
  565. }
  566. int bmx_SDL_ShowMessageBox_proceed(BBString * text, BBString * appTitle, int serious) {
  567. char * t = bbStringToUTF8String(appTitle);
  568. char * s = bbStringToUTF8String(text);
  569. SDL_MessageBoxButtonData buttons[] = {
  570. { 0, 0, "no" },
  571. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" },
  572. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "cancel" }
  573. };
  574. SDL_MessageBoxData messageboxdata = {
  575. (serious) ? SDL_MESSAGEBOX_WARNING : SDL_MESSAGEBOX_INFORMATION,
  576. NULL, t, s, SDL_arraysize(buttons), buttons, NULL
  577. };
  578. bbMemFree(s);
  579. bbMemFree(t);
  580. int buttonid;
  581. SDL_ShowMessageBox(&messageboxdata, &buttonid);
  582. switch (buttonid) {
  583. case 0: return 0;
  584. case 1: return 1;
  585. }
  586. return -1;
  587. }
  588. int bmx_SDL_EventFilter(void * userdata, SDL_Event * event) {
  589. switch (event->type) {
  590. case SDL_APP_TERMINATING:
  591. case SDL_APP_LOWMEMORY:
  592. case SDL_APP_WILLENTERBACKGROUND:
  593. case SDL_APP_DIDENTERBACKGROUND:
  594. case SDL_APP_WILLENTERFOREGROUND:
  595. case SDL_APP_DIDENTERFOREGROUND:
  596. return sdl_sdlsystem_TSDLSystemDriver__eventFilter(userdata, event->type);
  597. }
  598. return 1;
  599. }
  600. void bmx_SDL_SetEventFilter(BBObject * obj) {
  601. SDL_SetEventFilter(bmx_SDL_EventFilter, obj);
  602. }