SDL_BApp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef SDL_BAPP_H
  19. #define SDL_BAPP_H
  20. #include <Path.h>
  21. #include <InterfaceKit.h>
  22. #include <LocaleRoster.h>
  23. #if SDL_VIDEO_OPENGL
  24. #include <OpenGLKit.h>
  25. #endif
  26. #include "../../video/haiku/SDL_bkeyboard.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "SDL_internal.h"
  31. /* Local includes */
  32. #include "../../events/SDL_events_c.h"
  33. #include "../../video/haiku/SDL_bframebuffer.h"
  34. #ifdef __cplusplus
  35. }
  36. #endif
  37. #include <vector>
  38. /* Forward declarations */
  39. class SDL_BWin;
  40. /* Message constants */
  41. enum ToSDL
  42. {
  43. /* Intercepted by BWindow on its way to BView */
  44. BAPP_MOUSE_MOVED,
  45. BAPP_MOUSE_BUTTON,
  46. BAPP_MOUSE_WHEEL,
  47. BAPP_KEY,
  48. BAPP_REPAINT, /* from _UPDATE_ */
  49. /* From BWindow */
  50. BAPP_MAXIMIZE, /* from B_ZOOM */
  51. BAPP_MINIMIZE,
  52. BAPP_RESTORE, /* TODO: IMPLEMENT! */
  53. BAPP_SHOW,
  54. BAPP_HIDE,
  55. BAPP_MOUSE_FOCUS, /* caused by MOUSE_MOVE */
  56. BAPP_KEYBOARD_FOCUS, /* from WINDOW_ACTIVATED */
  57. BAPP_WINDOW_CLOSE_REQUESTED,
  58. BAPP_WINDOW_MOVED,
  59. BAPP_WINDOW_RESIZED,
  60. BAPP_SCREEN_CHANGED
  61. };
  62. /* Create a descendant of BApplication */
  63. class SDL_BApp : public BApplication
  64. {
  65. public:
  66. SDL_BApp(const char *signature) : BApplication(signature)
  67. {
  68. #if SDL_VIDEO_OPENGL
  69. _current_context = NULL;
  70. #endif
  71. }
  72. virtual ~SDL_BApp()
  73. {
  74. }
  75. virtual void RefsReceived(BMessage *message)
  76. {
  77. entry_ref entryRef;
  78. for (int32 i = 0; message->FindRef("refs", i, &entryRef) == B_OK; i++) {
  79. BPath referencePath = BPath(&entryRef);
  80. SDL_SendDropFile(NULL, referencePath.Path());
  81. }
  82. return;
  83. }
  84. /* Event-handling functions */
  85. virtual void MessageReceived(BMessage *message)
  86. {
  87. /* Sort out SDL-related messages */
  88. switch (message->what) {
  89. case BAPP_MOUSE_MOVED:
  90. _HandleMouseMove(message);
  91. break;
  92. case BAPP_MOUSE_BUTTON:
  93. _HandleMouseButton(message);
  94. break;
  95. case BAPP_MOUSE_WHEEL:
  96. _HandleMouseWheel(message);
  97. break;
  98. case BAPP_KEY:
  99. _HandleKey(message);
  100. break;
  101. case BAPP_REPAINT:
  102. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_EXPOSED);
  103. break;
  104. case BAPP_MAXIMIZE:
  105. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_MAXIMIZED);
  106. break;
  107. case BAPP_MINIMIZE:
  108. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_MINIMIZED);
  109. break;
  110. case BAPP_SHOW:
  111. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_SHOWN);
  112. break;
  113. case BAPP_HIDE:
  114. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_HIDDEN);
  115. break;
  116. case BAPP_MOUSE_FOCUS:
  117. _HandleMouseFocus(message);
  118. break;
  119. case BAPP_KEYBOARD_FOCUS:
  120. _HandleKeyboardFocus(message);
  121. break;
  122. case BAPP_WINDOW_CLOSE_REQUESTED:
  123. _HandleBasicWindowEvent(message, SDL_EVENT_WINDOW_CLOSE_REQUESTED);
  124. break;
  125. case BAPP_WINDOW_MOVED:
  126. _HandleWindowMoved(message);
  127. break;
  128. case BAPP_WINDOW_RESIZED:
  129. _HandleWindowResized(message);
  130. break;
  131. case B_LOCALE_CHANGED:
  132. SDL_SendLocaleChangedEvent();
  133. break;
  134. case BAPP_SCREEN_CHANGED:
  135. /* TODO: Handle screen resize or workspace change */
  136. break;
  137. default:
  138. BApplication::MessageReceived(message);
  139. break;
  140. }
  141. }
  142. /* Window creation/destruction methods */
  143. int32 GetID(SDL_Window *win)
  144. {
  145. int32 i;
  146. for (i = 0; i < _GetNumWindowSlots(); ++i) {
  147. if (GetSDLWindow(i) == NULL) {
  148. _SetSDLWindow(win, i);
  149. return i;
  150. }
  151. }
  152. /* Expand the vector if all slots are full */
  153. if (i == _GetNumWindowSlots()) {
  154. _PushBackWindow(win);
  155. return i;
  156. }
  157. /* TODO: error handling */
  158. return 0;
  159. }
  160. /* FIXME: Bad coding practice, but I can't include SDL_BWin.h here. Is
  161. there another way to do this? */
  162. void ClearID(SDL_BWin *bwin); /* Defined in SDL_BeApp.cc */
  163. SDL_Window *GetSDLWindow(int32 winID)
  164. {
  165. return _window_map[winID];
  166. }
  167. #if SDL_VIDEO_OPENGL
  168. BGLView *GetCurrentContext()
  169. {
  170. return _current_context;
  171. }
  172. void SetCurrentContext(BGLView *newContext)
  173. {
  174. if (_current_context)
  175. _current_context->UnlockGL();
  176. _current_context = newContext;
  177. if (_current_context)
  178. _current_context->LockGL();
  179. }
  180. #endif
  181. private:
  182. /* Event management */
  183. void _HandleBasicWindowEvent(BMessage *msg, SDL_EventType sdlEventType)
  184. {
  185. SDL_Window *win;
  186. int32 winID;
  187. if (
  188. !_GetWinID(msg, &winID)) {
  189. return;
  190. }
  191. win = GetSDLWindow(winID);
  192. SDL_SendWindowEvent(win, sdlEventType, 0, 0);
  193. }
  194. void _HandleMouseMove(BMessage *msg)
  195. {
  196. SDL_Window *win;
  197. int32 winID;
  198. int32 x = 0, y = 0;
  199. if (
  200. !_GetWinID(msg, &winID) ||
  201. msg->FindInt32("x", &x) != B_OK || /* x movement */
  202. msg->FindInt32("y", &y) != B_OK /* y movement */
  203. ) {
  204. return;
  205. }
  206. win = GetSDLWindow(winID);
  207. // Simple relative mode support for mouse.
  208. if (SDL_GetMouse()->relative_mode) {
  209. int winWidth, winHeight, winPosX, winPosY;
  210. SDL_GetWindowSize(win, &winWidth, &winHeight);
  211. SDL_GetWindowPosition(win, &winPosX, &winPosY);
  212. int dx = x - (winWidth / 2);
  213. int dy = y - (winHeight / 2);
  214. SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
  215. set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
  216. if (!be_app->IsCursorHidden())
  217. be_app->HideCursor();
  218. } else {
  219. SDL_SendMouseMotion(0, win, 0, 0, (float)x, (float)y);
  220. if (SDL_CursorVisible() && be_app->IsCursorHidden())
  221. be_app->ShowCursor();
  222. }
  223. }
  224. void _HandleMouseButton(BMessage *msg)
  225. {
  226. SDL_Window *win;
  227. int32 winID;
  228. int32 button, state; /* left/middle/right, pressed/released */
  229. if (
  230. !_GetWinID(msg, &winID) ||
  231. msg->FindInt32("button-id", &button) != B_OK ||
  232. msg->FindInt32("button-state", &state) != B_OK) {
  233. return;
  234. }
  235. win = GetSDLWindow(winID);
  236. SDL_SendMouseButton(0, win, 0, state, button);
  237. }
  238. void _HandleMouseWheel(BMessage *msg)
  239. {
  240. SDL_Window *win;
  241. int32 winID;
  242. int32 xTicks, yTicks;
  243. if (
  244. !_GetWinID(msg, &winID) ||
  245. msg->FindInt32("xticks", &xTicks) != B_OK ||
  246. msg->FindInt32("yticks", &yTicks) != B_OK) {
  247. return;
  248. }
  249. win = GetSDLWindow(winID);
  250. SDL_SendMouseWheel(0, win, 0, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
  251. }
  252. void _HandleKey(BMessage *msg)
  253. {
  254. int32 scancode, state; /* scancode, pressed/released */
  255. if (
  256. msg->FindInt32("key-state", &state) != B_OK ||
  257. msg->FindInt32("key-scancode", &scancode) != B_OK) {
  258. return;
  259. }
  260. /* Make sure this isn't a repeated event (key pressed and held) */
  261. if (state == SDL_PRESSED && HAIKU_GetKeyState(scancode) == SDL_PRESSED) {
  262. return;
  263. }
  264. HAIKU_SetKeyState(scancode, state);
  265. SDL_SendKeyboardKey(0, state, HAIKU_GetScancodeFromBeKey(scancode));
  266. if (state == SDL_PRESSED && SDL_EventEnabled(SDL_EVENT_TEXT_INPUT)) {
  267. const int8 *keyUtf8;
  268. ssize_t count;
  269. if (msg->FindData("key-utf8", B_INT8_TYPE, (const void **)&keyUtf8, &count) == B_OK) {
  270. char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
  271. SDL_zeroa(text);
  272. SDL_memcpy(text, keyUtf8, count);
  273. SDL_SendKeyboardText(text);
  274. }
  275. }
  276. }
  277. void _HandleMouseFocus(BMessage *msg)
  278. {
  279. SDL_Window *win;
  280. int32 winID;
  281. bool bSetFocus; /* If false, lose focus */
  282. if (
  283. !_GetWinID(msg, &winID) ||
  284. msg->FindBool("focusGained", &bSetFocus) != B_OK) {
  285. return;
  286. }
  287. win = GetSDLWindow(winID);
  288. if (bSetFocus) {
  289. SDL_SetMouseFocus(win);
  290. } else if (SDL_GetMouseFocus() == win) {
  291. /* Only lose all focus if this window was the current focus */
  292. SDL_SetMouseFocus(NULL);
  293. }
  294. }
  295. void _HandleKeyboardFocus(BMessage *msg)
  296. {
  297. SDL_Window *win;
  298. int32 winID;
  299. bool bSetFocus; /* If false, lose focus */
  300. if (
  301. !_GetWinID(msg, &winID) ||
  302. msg->FindBool("focusGained", &bSetFocus) != B_OK) {
  303. return;
  304. }
  305. win = GetSDLWindow(winID);
  306. if (bSetFocus) {
  307. SDL_SetKeyboardFocus(win);
  308. } else if (SDL_GetKeyboardFocus() == win) {
  309. /* Only lose all focus if this window was the current focus */
  310. SDL_SetKeyboardFocus(NULL);
  311. }
  312. }
  313. void _HandleWindowMoved(BMessage *msg)
  314. {
  315. SDL_Window *win;
  316. int32 winID;
  317. int32 xPos, yPos;
  318. /* Get the window id and new x/y position of the window */
  319. if (
  320. !_GetWinID(msg, &winID) ||
  321. msg->FindInt32("window-x", &xPos) != B_OK ||
  322. msg->FindInt32("window-y", &yPos) != B_OK) {
  323. return;
  324. }
  325. win = GetSDLWindow(winID);
  326. SDL_SendWindowEvent(win, SDL_EVENT_WINDOW_MOVED, xPos, yPos);
  327. }
  328. void _HandleWindowResized(BMessage *msg)
  329. {
  330. SDL_Window *win;
  331. int32 winID;
  332. int32 w, h;
  333. /* Get the window id ]and new x/y position of the window */
  334. if (
  335. !_GetWinID(msg, &winID) ||
  336. msg->FindInt32("window-w", &w) != B_OK ||
  337. msg->FindInt32("window-h", &h) != B_OK) {
  338. return;
  339. }
  340. win = GetSDLWindow(winID);
  341. SDL_SendWindowEvent(win, SDL_EVENT_WINDOW_RESIZED, w, h);
  342. }
  343. bool _GetWinID(BMessage *msg, int32 *winID)
  344. {
  345. return msg->FindInt32("window-id", winID) == B_OK;
  346. }
  347. /* Vector functions: Wraps vector stuff in case we need to change
  348. implementation */
  349. void _SetSDLWindow(SDL_Window *win, int32 winID)
  350. {
  351. _window_map[winID] = win;
  352. }
  353. int32 _GetNumWindowSlots()
  354. {
  355. return _window_map.size();
  356. }
  357. void _PopBackWindow()
  358. {
  359. _window_map.pop_back();
  360. }
  361. void _PushBackWindow(SDL_Window *win)
  362. {
  363. _window_map.push_back(win);
  364. }
  365. /* Members */
  366. std::vector<SDL_Window *> _window_map; /* Keeps track of SDL_Windows by index-id */
  367. #if SDL_VIDEO_OPENGL
  368. BGLView *_current_context;
  369. #endif
  370. };
  371. #endif