mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-06-01 06:44:27 +08:00
Use a BHandler attached to the application instead of a BLooper
When there is already a BApplication, SDL cannot start its own. In a previous version, it instead started a separate looper. This results in some extra complexity as there is now yet another thread to manage (in addition to the main thread, the application thread, and the window threads). Instead, create a BHandler and attach it to the existing BApplication, which allows it to receive messages in the already existing application thread.
This commit is contained in:
committed by
Adrien Destugues
parent
d9836d15e7
commit
47312cf0f3
@@ -49,7 +49,7 @@ extern "C" {
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
class SDL_BLooper;
|
class SDL_BHandler;
|
||||||
class SDL_BWin;
|
class SDL_BWin;
|
||||||
|
|
||||||
/* Message constants */
|
/* Message constants */
|
||||||
@@ -76,21 +76,21 @@ enum ToSDL
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
extern "C" SDL_BLooper *SDL_Looper;
|
extern "C" SDL_BHandler *SDL_Handler;
|
||||||
|
|
||||||
|
|
||||||
/* Create a descendant of BLooper */
|
/* Create a descendant of BHandler */
|
||||||
class SDL_BLooper : public BLooper
|
class SDL_BHandler : public BHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SDL_BLooper(const char* name) : BLooper(name)
|
SDL_BHandler(const char* name) : BHandler(name)
|
||||||
{
|
{
|
||||||
#ifdef SDL_VIDEO_OPENGL
|
#ifdef SDL_VIDEO_OPENGL
|
||||||
_current_context = NULL;
|
_current_context = NULL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~SDL_BLooper()
|
virtual ~SDL_BHandler()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ class SDL_BLooper : public BLooper
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
BLooper::MessageReceived(message);
|
BHandler::MessageReceived(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
#include <storage/File.h>
|
#include <storage/File.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "SDL_BApp.h" /* SDL_BLooper class definition */
|
#include "SDL_BApp.h" /* SDL_BHandler class definition */
|
||||||
#include "SDL_BeApp.h"
|
#include "SDL_BeApp.h"
|
||||||
#include "SDL_timer.h"
|
#include "SDL_timer.h"
|
||||||
#include "SDL_error.h"
|
#include "SDL_error.h"
|
||||||
@@ -47,7 +47,7 @@ extern "C" {
|
|||||||
/* Flag to tell whether or not the Be application and looper are active or not */
|
/* Flag to tell whether or not the Be application and looper are active or not */
|
||||||
static int SDL_BeAppActive = 0;
|
static int SDL_BeAppActive = 0;
|
||||||
static SDL_Thread *SDL_AppThread = NULL;
|
static SDL_Thread *SDL_AppThread = NULL;
|
||||||
SDL_BLooper *SDL_Looper = NULL;
|
SDL_BHandler *SDL_Handler = NULL;
|
||||||
|
|
||||||
|
|
||||||
/* Default application signature */
|
/* Default application signature */
|
||||||
@@ -137,8 +137,11 @@ static int StartBeLooper()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Looper = new SDL_BLooper("SDLLooper");
|
SDL_Handler = new SDL_BHandler("SDLHandler");
|
||||||
SDL_Looper->Run();
|
bool locked = be_app->Lock();
|
||||||
|
be_app->AddHandler(SDL_Handler);
|
||||||
|
if (locked)
|
||||||
|
be_app->Unlock();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,9 +172,6 @@ void SDL_QuitBeApp(void)
|
|||||||
|
|
||||||
/* If the reference count reached zero, clean up the app */
|
/* If the reference count reached zero, clean up the app */
|
||||||
if (SDL_BeAppActive == 0) {
|
if (SDL_BeAppActive == 0) {
|
||||||
SDL_Looper->Lock();
|
|
||||||
SDL_Looper->Quit();
|
|
||||||
SDL_Looper = NULL;
|
|
||||||
if (SDL_AppThread) {
|
if (SDL_AppThread) {
|
||||||
if (be_app != NULL) { /* Not tested */
|
if (be_app != NULL) { /* Not tested */
|
||||||
be_app->PostMessage(B_QUIT_REQUESTED);
|
be_app->PostMessage(B_QUIT_REQUESTED);
|
||||||
@@ -188,7 +188,7 @@ void SDL_QuitBeApp(void)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* SDL_BApp functions */
|
/* SDL_BApp functions */
|
||||||
void SDL_BLooper::ClearID(SDL_BWin *bwin) {
|
void SDL_BHandler::ClearID(SDL_BWin *bwin) {
|
||||||
_SetSDLWindow(NULL, bwin->GetID());
|
_SetSDLWindow(NULL, bwin->GetID());
|
||||||
int32 i = _GetNumWindowSlots() - 1;
|
int32 i = _GetNumWindowSlots() - 1;
|
||||||
while (i >= 0 && GetSDLWindow(i) == NULL) {
|
while (i >= 0 && GetSDLWindow(i) == NULL) {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ extern "C" {
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
class SDL_BLooper;
|
class SDL_BHandler;
|
||||||
class SDL_BWin;
|
class SDL_BWin;
|
||||||
|
|
||||||
/* Message constants */
|
/* Message constants */
|
||||||
@@ -76,21 +76,21 @@ enum ToSDL
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
extern "C" SDL_BLooper *SDL_Looper;
|
extern "C" SDL_BHandler *SDL_Handler;
|
||||||
|
|
||||||
|
|
||||||
/* Create a descendant of BLooper */
|
/* Create a descendant of BHandler */
|
||||||
class SDL_BLooper : public BLooper
|
class SDL_BHandler : public BHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SDL_BLooper(const char* name) : BLooper(name)
|
SDL_BHandler(const char* name) : BHandler(name)
|
||||||
{
|
{
|
||||||
#ifdef SDL_VIDEO_OPENGL
|
#ifdef SDL_VIDEO_OPENGL
|
||||||
_current_context = NULL;
|
_current_context = NULL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~SDL_BLooper()
|
virtual ~SDL_BHandler()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ class SDL_BLooper : public BLooper
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
BLooper::MessageReceived(message);
|
BHandler::MessageReceived(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,8 +125,8 @@ class SDL_BWin : public BWindow
|
|||||||
|
|
||||||
#ifdef SDL_VIDEO_OPENGL
|
#ifdef SDL_VIDEO_OPENGL
|
||||||
if (_SDL_GLView) {
|
if (_SDL_GLView) {
|
||||||
if (SDL_Looper->GetCurrentContext() == _SDL_GLView)
|
if (SDL_Handler->GetCurrentContext() == _SDL_GLView)
|
||||||
SDL_Looper->SetCurrentContext(NULL);
|
SDL_Handler->SetCurrentContext(NULL);
|
||||||
if (_SDL_GLView == _cur_view)
|
if (_SDL_GLView == _cur_view)
|
||||||
RemoveChild(_SDL_GLView);
|
RemoveChild(_SDL_GLView);
|
||||||
_SDL_GLView = NULL;
|
_SDL_GLView = NULL;
|
||||||
@@ -209,8 +209,8 @@ class SDL_BWin : public BWindow
|
|||||||
{
|
{
|
||||||
Lock();
|
Lock();
|
||||||
if (_SDL_GLView != NULL) {
|
if (_SDL_GLView != NULL) {
|
||||||
if (SDL_Looper->GetCurrentContext() == _SDL_GLView)
|
if (SDL_Handler->GetCurrentContext() == _SDL_GLView)
|
||||||
SDL_Looper->SetCurrentContext(NULL);
|
SDL_Handler->SetCurrentContext(NULL);
|
||||||
_SDL_GLView = NULL;
|
_SDL_GLView = NULL;
|
||||||
UpdateCurrentView();
|
UpdateCurrentView();
|
||||||
// _SDL_GLView deleted by HAIKU_GL_DeleteContext
|
// _SDL_GLView deleted by HAIKU_GL_DeleteContext
|
||||||
@@ -572,7 +572,7 @@ class SDL_BWin : public BWindow
|
|||||||
if (keyUtf8 != NULL) {
|
if (keyUtf8 != NULL) {
|
||||||
msg.AddData("key-utf8", B_INT8_TYPE, (const void *)keyUtf8, len);
|
msg.AddData("key-utf8", B_INT8_TYPE, (const void *)keyUtf8, len);
|
||||||
}
|
}
|
||||||
SDL_Looper->PostMessage(&msg);
|
be_app->PostMessage(&msg, SDL_Handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _RepaintEvent()
|
void _RepaintEvent()
|
||||||
@@ -584,7 +584,7 @@ class SDL_BWin : public BWindow
|
|||||||
void _PostWindowEvent(BMessage &msg)
|
void _PostWindowEvent(BMessage &msg)
|
||||||
{
|
{
|
||||||
msg.AddInt32("window-id", _id);
|
msg.AddInt32("window-id", _id);
|
||||||
SDL_Looper->PostMessage(&msg);
|
be_app->PostMessage(&msg, SDL_Handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Command methods (functions called upon by SDL) */
|
/* Command methods (functions called upon by SDL) */
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
|
|||||||
return (SDL_BWin *)(window->driverdata);
|
return (SDL_BWin *)(window->driverdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
|
static SDL_INLINE SDL_BHandler *_GetBeLooper() {
|
||||||
return SDL_Looper;
|
return SDL_Handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
int HAIKU_CreateWindowFramebuffer(_THIS, SDL_Window * window,
|
int HAIKU_CreateWindowFramebuffer(_THIS, SDL_Window * window,
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
|
|||||||
return (SDL_BWin *)(window->driverdata);
|
return (SDL_BWin *)(window->driverdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
|
static SDL_INLINE SDL_BHandler *_GetBeLooper() {
|
||||||
return SDL_Looper;
|
return SDL_Handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_INLINE display_mode * _ExtractBMode(SDL_DisplayMode *mode) {
|
static SDL_INLINE display_mode * _ExtractBMode(SDL_DisplayMode *mode) {
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
|
|||||||
return (SDL_BWin *)(window->driverdata);
|
return (SDL_BWin *)(window->driverdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
|
static SDL_INLINE SDL_BHandler *_GetBeLooper() {
|
||||||
return SDL_Looper;
|
return SDL_Handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Passing a NULL path means load pointers from the application */
|
/* Passing a NULL path means load pointers from the application */
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
|
|||||||
return (SDL_BWin *)(window->driverdata);
|
return (SDL_BWin *)(window->driverdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_INLINE SDL_BLooper *_GetBeLooper() {
|
static SDL_INLINE SDL_BHandler *_GetBeLooper() {
|
||||||
return SDL_Looper;
|
return SDL_Handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _InitWindow(_THIS, SDL_Window *window) {
|
static int _InitWindow(_THIS, SDL_Window *window) {
|
||||||
|
|||||||
Reference in New Issue
Block a user