summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/core/haiku
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/core/haiku')
-rw-r--r--contrib/SDL-3.2.8/src/core/haiku/SDL_BApp.h426
-rw-r--r--contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.cc182
-rw-r--r--contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.h40
3 files changed, 648 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/core/haiku/SDL_BApp.h b/contrib/SDL-3.2.8/src/core/haiku/SDL_BApp.h
new file mode 100644
index 0000000..cbd3e3a
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/haiku/SDL_BApp.h
@@ -0,0 +1,426 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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
diff --git a/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.cc b/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.cc
new file mode 100644
index 0000000..350f7f3
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.cc
@@ -0,0 +1,182 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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#include "SDL_internal.h"
22
23#ifdef SDL_PLATFORM_HAIKU
24
25// Handle the BeApp specific portions of the application
26
27#include <AppKit.h>
28#include <storage/AppFileInfo.h>
29#include <storage/Path.h>
30#include <storage/Entry.h>
31#include <storage/File.h>
32#include <unistd.h>
33#include <memory>
34
35#include "SDL_BApp.h" // SDL_BLooper class definition
36#include "SDL_BeApp.h"
37
38#include "../../video/haiku/SDL_BWin.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44#include "../../thread/SDL_systhread.h"
45
46// Flag to tell whether or not the Be application and looper are active or not
47static int SDL_BeAppActive = 0;
48static SDL_Thread *SDL_AppThread = NULL;
49SDL_BLooper *SDL_Looper = NULL;
50
51
52// Default application signature
53const char *SDL_signature = "application/x-SDL-executable";
54
55
56// Create a descendant of BApplication
57class SDL_BApp : public BApplication {
58public:
59 SDL_BApp(const char* signature) :
60 BApplication(signature) {
61 }
62
63
64 virtual ~SDL_BApp() {
65 }
66
67
68 virtual void RefsReceived(BMessage* message) {
69 entry_ref entryRef;
70 for (int32 i = 0; message->FindRef("refs", i, &entryRef) == B_OK; i++) {
71 BPath referencePath = BPath(&entryRef);
72 SDL_SendDropFile(NULL, NULL, referencePath.Path());
73 }
74 return;
75 }
76};
77
78
79static int StartBeApp(void *unused)
80{
81 std::unique_ptr<BApplication> App;
82
83 (void)unused;
84 // dig resources for correct signature
85 image_info info;
86 int32 cookie = 0;
87 if (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
88 BFile f(info.name, O_RDONLY);
89 if (f.InitCheck() == B_OK) {
90 BAppFileInfo app_info(&f);
91 if (app_info.InitCheck() == B_OK) {
92 char sig[B_MIME_TYPE_LENGTH];
93 if (app_info.GetSignature(sig) == B_OK) {
94 SDL_signature = strndup(sig, B_MIME_TYPE_LENGTH);
95 }
96 }
97 }
98 }
99
100 App = std::unique_ptr<BApplication>(new SDL_BApp(SDL_signature));
101
102 App->Run();
103 return 0;
104}
105
106
107static bool StartBeLooper()
108{
109 if (!be_app) {
110 SDL_AppThread = SDL_CreateThread(StartBeApp, "SDLApplication", NULL);
111 if (!SDL_AppThread) {
112 return SDL_SetError("Couldn't create BApplication thread");
113 }
114
115 do {
116 SDL_Delay(10);
117 } while ((!be_app) || be_app->IsLaunching());
118 }
119
120 SDL_Looper = new SDL_BLooper("SDLLooper");
121 SDL_Looper->Run();
122 return true;
123}
124
125
126// Initialize the Be Application, if it's not already started
127bool SDL_InitBeApp(void)
128{
129 // Create the BApplication that handles appserver interaction
130 if (SDL_BeAppActive <= 0) {
131 if (!StartBeLooper()) {
132 return false;
133 }
134
135 // Mark the application active
136 SDL_BeAppActive = 0;
137 }
138
139 // Increment the application reference count
140 ++SDL_BeAppActive;
141
142 // The app is running, and we're ready to go
143 return true;
144}
145
146// Quit the Be Application, if there's nothing left to do
147void SDL_QuitBeApp(void)
148{
149 // Decrement the application reference count
150 --SDL_BeAppActive;
151
152 // If the reference count reached zero, clean up the app
153 if (SDL_BeAppActive == 0) {
154 SDL_Looper->Lock();
155 SDL_Looper->Quit();
156 SDL_Looper = NULL;
157 if (SDL_AppThread) {
158 if (be_app != NULL) { // Not tested
159 be_app->PostMessage(B_QUIT_REQUESTED);
160 }
161 SDL_WaitThread(SDL_AppThread, NULL);
162 SDL_AppThread = NULL;
163 }
164 // be_app should now be NULL since be_app has quit
165 }
166}
167
168#ifdef __cplusplus
169}
170#endif
171
172// SDL_BApp functions
173void SDL_BLooper::ClearID(SDL_BWin *bwin) {
174 _SetSDLWindow(NULL, bwin->GetID());
175 int32 i = _GetNumWindowSlots() - 1;
176 while (i >= 0 && GetSDLWindow(i) == NULL) {
177 _PopBackWindow();
178 --i;
179 }
180}
181
182#endif // SDL_PLATFORM_HAIKU
diff --git a/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.h b/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.h
new file mode 100644
index 0000000..de5bb6b
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/haiku/SDL_BeApp.h
@@ -0,0 +1,40 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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#include "SDL_internal.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26// Handle the BeApp specific portions of the application
27
28// Initialize the Be Application, if it's not already started
29extern bool SDL_InitBeApp(void);
30
31// Quit the Be Application, if there's nothing left to do
32extern void SDL_QuitBeApp(void);
33
34// Be Application Signature
35extern const char *SDL_signature;
36
37
38#ifdef __cplusplus
39}
40#endif