Skip to content

Commit 2d64f41

Browse files
authored
Haiku: use a BLooper for events, only create a BApplication when it doesn't already exist. (#7555)
1 parent cae6b44 commit 2d64f41

10 files changed

+109
-77
lines changed

src/core/haiku/SDL_BApp.h

+10-15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "C" {
4747
#include <vector>
4848

4949
/* Forward declarations */
50+
class SDL_BLooper;
5051
class SDL_BWin;
5152

5253
/* Message constants */
@@ -72,31 +73,25 @@ enum ToSDL
7273
BAPP_SCREEN_CHANGED
7374
};
7475

75-
/* Create a descendant of BApplication */
76-
class SDL_BApp : public BApplication
76+
77+
extern "C" SDL_BLooper *SDL_Looper;
78+
79+
80+
/* Create a descendant of BLooper */
81+
class SDL_BLooper : public BLooper
7782
{
7883
public:
79-
SDL_BApp(const char *signature) : BApplication(signature)
84+
SDL_BLooper(const char* name) : BLooper(name)
8085
{
8186
#ifdef SDL_VIDEO_OPENGL
8287
_current_context = NULL;
8388
#endif
8489
}
8590

86-
virtual ~SDL_BApp()
91+
virtual ~SDL_BLooper()
8792
{
8893
}
8994

90-
virtual void RefsReceived(BMessage *message)
91-
{
92-
entry_ref entryRef;
93-
for (int32 i = 0; message->FindRef("refs", i, &entryRef) == B_OK; i++) {
94-
BPath referencePath = BPath(&entryRef);
95-
SDL_SendDropFile(NULL, referencePath.Path());
96-
}
97-
return;
98-
}
99-
10095
/* Event-handling functions */
10196
virtual void MessageReceived(BMessage *message)
10297
{
@@ -167,7 +162,7 @@ class SDL_BApp : public BApplication
167162
break;
168163

169164
default:
170-
BApplication::MessageReceived(message);
165+
BLooper::MessageReceived(message);
171166
break;
172167
}
173168
}

src/core/haiku/SDL_BeApp.cc

+68-25
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <unistd.h>
3333
#include <memory>
3434

35-
#include "SDL_BApp.h" /* SDL_BApp class definition */
35+
#include "SDL_BApp.h" /* SDL_BLooper class definition */
3636
#include "SDL_BeApp.h"
3737

3838
#include "../../video/haiku/SDL_BWin.h"
@@ -43,12 +43,38 @@ extern "C" {
4343

4444
#include "../../thread/SDL_systhread.h"
4545

46-
/* Flag to tell whether or not the Be application is active or not */
46+
/* Flag to tell whether or not the Be application and looper are active or not */
4747
static int SDL_BeAppActive = 0;
4848
static SDL_Thread *SDL_AppThread = NULL;
49+
SDL_BLooper *SDL_Looper = NULL;
50+
4951

5052
/* Default application signature */
51-
const char *signature = "application/x-SDL-executable";
53+
const char *SDL_signature = "application/x-SDL-executable";
54+
55+
56+
/* Create a descendant of BApplication */
57+
class SDL_BApp : public BApplication {
58+
public:
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, referencePath.Path());
73+
}
74+
return;
75+
}
76+
};
77+
5278

5379
static int StartBeApp(void *unused)
5480
{
@@ -65,47 +91,61 @@ static int StartBeApp(void *unused)
6591
if (app_info.InitCheck() == B_OK) {
6692
char sig[B_MIME_TYPE_LENGTH];
6793
if (app_info.GetSignature(sig) == B_OK) {
68-
signature = strndup(sig, B_MIME_TYPE_LENGTH);
94+
SDL_signature = strndup(sig, B_MIME_TYPE_LENGTH);
6995
}
7096
}
7197
}
7298
}
7399

74-
App = std::unique_ptr<BApplication>(new SDL_BApp(signature));
100+
App = std::unique_ptr<BApplication>(new SDL_BApp(SDL_signature));
75101

76102
App->Run();
77103
return 0;
78104
}
79105

80-
/* Initialize the Be Application, if it's not already started */
81-
int
82-
SDL_InitBeApp(void)
106+
107+
static int
108+
StartBeLooper()
83109
{
84-
/* Create the BApplication that handles appserver interaction */
85-
if (SDL_BeAppActive <= 0) {
110+
if (!be_app) {
86111
SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
87112
if (SDL_AppThread == NULL) {
88113
return SDL_SetError("Couldn't create BApplication thread");
89114
}
90115

91-
/* Change working directory to that of executable */
92-
app_info info;
93-
if (B_OK == be_app->GetAppInfo(&info)) {
94-
entry_ref ref = info.ref;
95-
BEntry entry;
96-
if (B_OK == entry.SetTo(&ref)) {
97-
BPath path;
98-
if (B_OK == path.SetTo(&entry)) {
99-
if (B_OK == path.GetParent(&path)) {
100-
chdir(path.Path());
101-
}
116+
do {
117+
SDL_Delay(10);
118+
} while ((be_app == NULL) || be_app->IsLaunching());
119+
}
120+
121+
/* Change working directory to that of executable */
122+
app_info info;
123+
if (B_OK == be_app->GetAppInfo(&info)) {
124+
entry_ref ref = info.ref;
125+
BEntry entry;
126+
if (B_OK == entry.SetTo(&ref)) {
127+
BPath path;
128+
if (B_OK == path.SetTo(&entry)) {
129+
if (B_OK == path.GetParent(&path)) {
130+
chdir(path.Path());
102131
}
103132
}
104133
}
134+
}
105135

106-
do {
107-
SDL_Delay(10);
108-
} while ((be_app == NULL) || be_app->IsLaunching());
136+
SDL_Looper = new SDL_BLooper("SDLLooper");
137+
SDL_Looper->Run();
138+
return (0);
139+
}
140+
141+
142+
/* Initialize the Be Application, if it's not already started */
143+
int
144+
SDL_InitBeApp(void)
145+
{
146+
/* Create the BApplication that handles appserver interaction */
147+
if (SDL_BeAppActive <= 0) {
148+
StartBeLooper();
109149

110150
/* Mark the application active */
111151
SDL_BeAppActive = 0;
@@ -127,6 +167,9 @@ SDL_QuitBeApp(void)
127167

128168
/* If the reference count reached zero, clean up the app */
129169
if (SDL_BeAppActive == 0) {
170+
SDL_Looper->Lock();
171+
SDL_Looper->Quit();
172+
SDL_Looper = NULL;
130173
if (SDL_AppThread != NULL) {
131174
if (be_app != NULL) { /* Not tested */
132175
be_app->PostMessage(B_QUIT_REQUESTED);
@@ -143,7 +186,7 @@ SDL_QuitBeApp(void)
143186
#endif
144187

145188
/* SDL_BApp functions */
146-
void SDL_BApp::ClearID(SDL_BWin *bwin) {
189+
void SDL_BLooper::ClearID(SDL_BWin *bwin) {
147190
_SetSDLWindow(NULL, bwin->GetID());
148191
int32 i = _GetNumWindowSlots() - 1;
149192
while (i >= 0 && GetSDLWindow(i) == NULL) {

src/core/haiku/SDL_BeApp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern int SDL_InitBeApp(void);
3232
extern void SDL_QuitBeApp(void);
3333

3434
/* Be Application Signature*/
35-
extern const char *signature;
35+
extern const char *SDL_signature;
3636

3737

3838
#ifdef __cplusplus

src/video/haiku/SDL_BApp.h

+11-17
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "C" {
4747
#include <vector>
4848

4949
/* Forward declarations */
50+
class SDL_BLooper;
5051
class SDL_BWin;
5152

5253
/* Message constants */
@@ -72,32 +73,25 @@ enum ToSDL
7273
BAPP_SCREEN_CHANGED
7374
};
7475

75-
/* Create a descendant of BApplication */
76-
class SDL_BApp : public BApplication
76+
77+
extern "C" SDL_BLooper *SDL_Looper;
78+
79+
80+
/* Create a descendant of BLooper */
81+
class SDL_BLooper : public BLooper
7782
{
7883
public:
79-
SDL_BApp(const char *signature) : BApplication(signature)
84+
SDL_BLooper(const char* name) : BLooper(name)
8085
{
8186
#ifdef SDL_VIDEO_OPENGL
8287
_current_context = NULL;
8388
#endif
8489
}
8590

86-
virtual ~SDL_BApp()
91+
virtual ~SDL_BLooper()
8792
{
8893
}
8994

90-
virtual void RefsReceived(BMessage *message)
91-
{
92-
char filePath[512];
93-
entry_ref entryRef;
94-
for (int32 i = 0; message->FindRef("refs", i, &entryRef) == B_OK; i++) {
95-
BPath referencePath = BPath(&entryRef);
96-
SDL_SendDropFile(NULL, referencePath.Path());
97-
}
98-
return;
99-
}
100-
10195
/* Event-handling functions */
10296
virtual void MessageReceived(BMessage *message)
10397
{
@@ -168,7 +162,7 @@ class SDL_BApp : public BApplication
168162
break;
169163

170164
default:
171-
BApplication::MessageReceived(message);
165+
BLooper::MessageReceived(message);
172166
break;
173167
}
174168
}
@@ -221,7 +215,7 @@ class SDL_BApp : public BApplication
221215

222216
private:
223217
/* Event management */
224-
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType)
218+
void _HandleBasicWindowEvent(BMessage *msg, SDL_EventType sdlEventType)
225219
{
226220
SDL_Window *win;
227221
int32 winID;

src/video/haiku/SDL_BWin.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class SDL_BWin : public BWindow
124124

125125
#ifdef SDL_VIDEO_OPENGL
126126
if (_SDL_GLView) {
127-
if (((SDL_BApp *)be_app)->GetCurrentContext() == _SDL_GLView)
128-
((SDL_BApp *)be_app)->SetCurrentContext(NULL);
127+
if (SDL_Looper->GetCurrentContext() == _SDL_GLView)
128+
SDL_Looper->SetCurrentContext(NULL);
129129
if (_SDL_GLView == _cur_view)
130130
RemoveChild(_SDL_GLView);
131131
_SDL_GLView = NULL;
@@ -208,8 +208,8 @@ class SDL_BWin : public BWindow
208208
{
209209
Lock();
210210
if (_SDL_GLView != NULL) {
211-
if (((SDL_BApp *)be_app)->GetCurrentContext() == _SDL_GLView)
212-
((SDL_BApp *)be_app)->SetCurrentContext(NULL);
211+
if (SDL_Looper->GetCurrentContext() == _SDL_GLView)
212+
SDL_Looper->SetCurrentContext(NULL);
213213
_SDL_GLView = NULL;
214214
UpdateCurrentView();
215215
// _SDL_GLView deleted by HAIKU_GL_DeleteContext
@@ -571,7 +571,7 @@ class SDL_BWin : public BWindow
571571
if (keyUtf8 != NULL) {
572572
msg.AddData("key-utf8", B_INT8_TYPE, (const void *)keyUtf8, len);
573573
}
574-
be_app->PostMessage(&msg);
574+
SDL_Looper->PostMessage(&msg);
575575
}
576576

577577
void _RepaintEvent()
@@ -583,7 +583,7 @@ class SDL_BWin : public BWindow
583583
void _PostWindowEvent(BMessage &msg)
584584
{
585585
msg.AddInt32("window-id", _id);
586-
be_app->PostMessage(&msg);
586+
SDL_Looper->PostMessage(&msg);
587587
}
588588

589589
/* Command methods (functions called upon by SDL) */

src/video/haiku/SDL_bframebuffer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
3939
return (SDL_BWin *)(window->driverdata);
4040
}
4141

42-
static SDL_INLINE SDL_BApp *_GetBeApp() {
43-
return (SDL_BApp *)be_app;
42+
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
43+
return SDL_Looper;
4444
}
4545

4646
int HAIKU_CreateWindowFramebuffer(_THIS, SDL_Window * window,

src/video/haiku/SDL_bmessagebox.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ HAIKU_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
358358
// "2 BApplication objects were created. Only one is allowed."
359359
std::unique_ptr<BApplication> application;
360360
if (be_app == NULL) {
361-
application = std::unique_ptr<BApplication>(new(std::nothrow) BApplication(signature));
361+
application = std::unique_ptr<BApplication>(new(std::nothrow) BApplication(SDL_signature));
362362
if (application == NULL) {
363363
return SDL_SetError("Cannot create the BApplication object. Lack of memory?");
364364
}

src/video/haiku/SDL_bmodes.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
5252
return (SDL_BWin *)(window->driverdata);
5353
}
5454

55-
static SDL_INLINE SDL_BApp *_GetBeApp() {
56-
return (SDL_BApp *)be_app;
55+
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
56+
return SDL_Looper;
5757
}
5858

5959
static SDL_INLINE display_mode * _ExtractBMode(SDL_DisplayMode *mode) {

src/video/haiku/SDL_bopengl.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
3939
return (SDL_BWin *)(window->driverdata);
4040
}
4141

42-
static SDL_INLINE SDL_BApp *_GetBeApp() {
43-
return (SDL_BApp *)be_app;
42+
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
43+
return SDL_Looper;
4444
}
4545

4646
/* Passing a NULL path means load pointers from the application */
@@ -97,7 +97,7 @@ int HAIKU_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) {
9797
return SDL_SetError("MakeCurrent failed");
9898
}
9999
}
100-
_GetBeApp()->SetCurrentContext(glView);
100+
_GetBeLooper()->SetCurrentContext(glView);
101101
return 0;
102102
}
103103

@@ -138,7 +138,7 @@ SDL_GLContext HAIKU_GL_CreateContext(_THIS, SDL_Window * window) {
138138
}
139139
#endif
140140
bwin->CreateGLView(gl_flags);
141-
_GetBeApp()->SetCurrentContext(bwin->GetGLView());
141+
_GetBeLooper()->SetCurrentContext(bwin->GetGLView());
142142
return (SDL_GLContext)(bwin->GetGLView());
143143
}
144144

0 commit comments

Comments
 (0)