src/main/haiku/SDL_BeApp.cc
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Sep 2018 11:49:25 -0700
changeset 12201 8bdc4d340419
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
Fixed whitespace
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@0
    22
icculus@7981
    23
#if defined(__HAIKU__)
slouken@6044
    24
slouken@0
    25
/* Handle the BeApp specific portions of the application */
slouken@0
    26
slouken@0
    27
#include <AppKit.h>
icculus@10983
    28
#include <storage/AppFileInfo.h>
slouken@0
    29
#include <storage/Path.h>
slouken@0
    30
#include <storage/Entry.h>
icculus@10983
    31
#include <storage/File.h>
slouken@0
    32
#include <unistd.h>
slouken@0
    33
slouken@12201
    34
#include "SDL_BApp.h"   /* SDL_BApp class definition */
slouken@0
    35
#include "SDL_BeApp.h"
slouken@0
    36
#include "SDL_timer.h"
slouken@0
    37
#include "SDL_error.h"
slouken@0
    38
icculus@7981
    39
#include "../../video/haiku/SDL_BWin.h"
nathanheisey@5916
    40
nathanheisey@5922
    41
#ifdef __cplusplus
nathanheisey@5922
    42
extern "C" {
nathanheisey@5922
    43
#endif
icculus@10149
    44
icculus@10149
    45
#include "../../thread/SDL_systhread.h"
icculus@10149
    46
slouken@0
    47
/* Flag to tell whether or not the Be application is active or not */
philipp@11059
    48
static int SDL_BeAppActive = 0;
slouken@0
    49
static SDL_Thread *SDL_AppThread = NULL;
slouken@0
    50
slouken@1895
    51
static int
slouken@1895
    52
StartBeApp(void *unused)
slouken@0
    53
{
slouken@1895
    54
    BApplication *App;
slouken@0
    55
slouken@12201
    56
    // default application signature
slouken@12201
    57
    const char *signature = "application/x-SDL-executable";
slouken@12201
    58
    // dig resources for correct signature
slouken@12201
    59
    image_info info;
slouken@12201
    60
    int32 cookie = 0;
slouken@12201
    61
    if (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
slouken@12201
    62
        BFile f(info.name, O_RDONLY);
slouken@12201
    63
        if (f.InitCheck() == B_OK) {
slouken@12201
    64
            BAppFileInfo app_info(&f);
slouken@12201
    65
            if (app_info.InitCheck() == B_OK) {
slouken@12201
    66
                char sig[B_MIME_TYPE_LENGTH];
slouken@12201
    67
                if (app_info.GetSignature(sig) == B_OK)
slouken@12201
    68
                    signature = strndup(sig, B_MIME_TYPE_LENGTH);
slouken@12201
    69
            }
slouken@12201
    70
        }
slouken@12201
    71
    }
icculus@10983
    72
slouken@12201
    73
    App = new SDL_BApp(signature);
slouken@0
    74
slouken@1895
    75
    App->Run();
slouken@1895
    76
    delete App;
slouken@1895
    77
    return (0);
slouken@0
    78
}
slouken@0
    79
slouken@0
    80
/* Initialize the Be Application, if it's not already started */
slouken@1895
    81
int
slouken@1895
    82
SDL_InitBeApp(void)
slouken@0
    83
{
slouken@1895
    84
    /* Create the BApplication that handles appserver interaction */
slouken@1895
    85
    if (SDL_BeAppActive <= 0) {
icculus@10146
    86
        SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
slouken@1895
    87
        if (SDL_AppThread == NULL) {
icculus@7037
    88
            return SDL_SetError("Couldn't create BApplication thread");
slouken@1895
    89
        }
slouken@0
    90
icculus@7987
    91
        /* Change working directory to that of executable */
slouken@1895
    92
        app_info info;
slouken@1895
    93
        if (B_OK == be_app->GetAppInfo(&info)) {
slouken@1895
    94
            entry_ref ref = info.ref;
slouken@1895
    95
            BEntry entry;
slouken@1895
    96
            if (B_OK == entry.SetTo(&ref)) {
slouken@1895
    97
                BPath path;
slouken@1895
    98
                if (B_OK == path.SetTo(&entry)) {
slouken@1895
    99
                    if (B_OK == path.GetParent(&path)) {
slouken@1895
   100
                        chdir(path.Path());
slouken@1895
   101
                    }
slouken@1895
   102
                }
slouken@1895
   103
            }
slouken@1895
   104
        }
slouken@0
   105
slouken@1895
   106
        do {
slouken@1895
   107
            SDL_Delay(10);
slouken@2735
   108
        } while ((be_app == NULL) || be_app->IsLaunching());
slouken@0
   109
slouken@1895
   110
        /* Mark the application active */
slouken@1895
   111
        SDL_BeAppActive = 0;
slouken@1895
   112
    }
slouken@1895
   113
slouken@1895
   114
    /* Increment the application reference count */
slouken@1895
   115
    ++SDL_BeAppActive;
slouken@1895
   116
slouken@1895
   117
    /* The app is running, and we're ready to go */
slouken@1895
   118
    return (0);
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
/* Quit the Be Application, if there's nothing left to do */
slouken@1895
   122
void
slouken@1895
   123
SDL_QuitBeApp(void)
slouken@0
   124
{
slouken@1895
   125
    /* Decrement the application reference count */
slouken@1895
   126
    --SDL_BeAppActive;
slouken@0
   127
slouken@1895
   128
    /* If the reference count reached zero, clean up the app */
slouken@1895
   129
    if (SDL_BeAppActive == 0) {
slouken@1895
   130
        if (SDL_AppThread != NULL) {
slouken@1895
   131
            if (be_app != NULL) {       /* Not tested */
slouken@1895
   132
                be_app->PostMessage(B_QUIT_REQUESTED);
slouken@1895
   133
            }
slouken@1895
   134
            SDL_WaitThread(SDL_AppThread, NULL);
slouken@1895
   135
            SDL_AppThread = NULL;
slouken@1895
   136
        }
slouken@1895
   137
        /* be_app should now be NULL since be_app has quit */
slouken@1895
   138
    }
slouken@0
   139
}
slouken@1895
   140
nathanheisey@5922
   141
#ifdef __cplusplus
nathanheisey@5922
   142
}
nathanheisey@5922
   143
#endif
nathanheisey@5922
   144
nathanheisey@5916
   145
/* SDL_BApp functions */
nathanheisey@5916
   146
void SDL_BApp::ClearID(SDL_BWin *bwin) {
slouken@12201
   147
    _SetSDLWindow(NULL, bwin->GetID());
slouken@12201
   148
    int32 i = _GetNumWindowSlots() - 1;
slouken@12201
   149
    while(i >= 0 && GetSDLWindow(i) == NULL) {
slouken@12201
   150
        _PopBackWindow();
slouken@12201
   151
        --i;
slouken@12201
   152
    }
nathanheisey@5916
   153
}
slouken@6044
   154
icculus@7981
   155
#endif /* __HAIKU__ */
slouken@6044
   156
slouken@6044
   157
/* vi: set ts=4 sw=4 expandtab: */