Atlas - SDL_BApp.h

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