#include "../../SDL_internal.h"
#if defined(__HAIKU__)
#include <AppKit.h>
#include <storage/AppFileInfo.h>
#include <storage/Path.h>
#include <storage/Entry.h>
#include <storage/File.h>
#include <unistd.h>
#include "SDL_BApp.h"
#include "SDL_BeApp.h"
#include "SDL_timer.h"
#include "SDL_error.h"
#include "../../video/haiku/SDL_BWin.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "../../thread/SDL_systhread.h"
static int SDL_BeAppActive = 0;
static SDL_Thread *SDL_AppThread = NULL;
const char *signature = "application/x-SDL-executable";
static int
StartBeApp(void *unused)
{
BApplication *App;
image_info info;
int32 cookie = 0;
if (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
BFile f(info.name, O_RDONLY);
if (f.InitCheck() == B_OK) {
BAppFileInfo app_info(&f);
if (app_info.InitCheck() == B_OK) {
char sig[B_MIME_TYPE_LENGTH];
if (app_info.GetSignature(sig) == B_OK)
signature = strndup(sig, B_MIME_TYPE_LENGTH);
}
}
}
App = new SDL_BApp(signature);
App->Run();
delete App;
return (0);
}
int
SDL_InitBeApp(void)
{
if (SDL_BeAppActive <= 0) {
SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
if (SDL_AppThread == NULL) {
return SDL_SetError("Couldn't create BApplication thread");
}
app_info info;
if (B_OK == be_app->GetAppInfo(&info)) {
entry_ref ref = info.ref;
BEntry entry;
if (B_OK == entry.SetTo(&ref)) {
BPath path;
if (B_OK == path.SetTo(&entry)) {
if (B_OK == path.GetParent(&path)) {
chdir(path.Path());
}
}
}
}
do {
SDL_Delay(10);
} while ((be_app == NULL) || be_app->IsLaunching());
SDL_BeAppActive = 0;
}
++SDL_BeAppActive;
return (0);
}
void
SDL_QuitBeApp(void)
{
--SDL_BeAppActive;
if (SDL_BeAppActive == 0) {
if (SDL_AppThread != NULL) {
if (be_app != NULL) {
be_app->PostMessage(B_QUIT_REQUESTED);
}
SDL_WaitThread(SDL_AppThread, NULL);
SDL_AppThread = NULL;
}
}
}
#ifdef __cplusplus
}
#endif
void SDL_BApp::ClearID(SDL_BWin *bwin) {
_SetSDLWindow(NULL, bwin->GetID());
int32 i = _GetNumWindowSlots() - 1;
while(i >= 0 && GetSDLWindow(i) == NULL) {
_PopBackWindow();
--i;
}
}
#endif