src/main/beos/SDL_BeApp.cc
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 2859 99210400e8b9
child 5262 b530ef003506
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1403
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Handle the BeApp specific portions of the application */
slouken@0
    25
slouken@0
    26
#include <AppKit.h>
slouken@0
    27
#include <storage/Path.h>
slouken@0
    28
#include <storage/Entry.h>
slouken@0
    29
#include <unistd.h>
slouken@0
    30
slouken@0
    31
#include "SDL_BeApp.h"
slouken@0
    32
#include "SDL_thread.h"
slouken@0
    33
#include "SDL_timer.h"
slouken@0
    34
#include "SDL_error.h"
slouken@0
    35
slouken@0
    36
/* Flag to tell whether or not the Be application is active or not */
slouken@0
    37
int SDL_BeAppActive = 0;
slouken@0
    38
static SDL_Thread *SDL_AppThread = NULL;
slouken@0
    39
slouken@1895
    40
static int
slouken@1895
    41
StartBeApp(void *unused)
slouken@0
    42
{
slouken@1895
    43
    BApplication *App;
slouken@0
    44
slouken@1895
    45
    App = new BApplication("application/x-SDL-executable");
slouken@0
    46
slouken@1895
    47
    App->Run();
slouken@1895
    48
    delete App;
slouken@1895
    49
    return (0);
slouken@0
    50
}
slouken@0
    51
slouken@0
    52
/* Initialize the Be Application, if it's not already started */
slouken@1895
    53
int
slouken@1895
    54
SDL_InitBeApp(void)
slouken@0
    55
{
slouken@1895
    56
    /* Create the BApplication that handles appserver interaction */
slouken@1895
    57
    if (SDL_BeAppActive <= 0) {
slouken@1895
    58
        SDL_AppThread = SDL_CreateThread(StartBeApp, NULL);
slouken@1895
    59
        if (SDL_AppThread == NULL) {
slouken@1895
    60
            SDL_SetError("Couldn't create BApplication thread");
slouken@1895
    61
            return (-1);
slouken@1895
    62
        }
slouken@0
    63
slouken@1895
    64
        /* Change working to directory to that of executable */
slouken@1895
    65
        app_info info;
slouken@1895
    66
        if (B_OK == be_app->GetAppInfo(&info)) {
slouken@1895
    67
            entry_ref ref = info.ref;
slouken@1895
    68
            BEntry entry;
slouken@1895
    69
            if (B_OK == entry.SetTo(&ref)) {
slouken@1895
    70
                BPath path;
slouken@1895
    71
                if (B_OK == path.SetTo(&entry)) {
slouken@1895
    72
                    if (B_OK == path.GetParent(&path)) {
slouken@1895
    73
                        chdir(path.Path());
slouken@1895
    74
                    }
slouken@1895
    75
                }
slouken@1895
    76
            }
slouken@1895
    77
        }
slouken@0
    78
slouken@1895
    79
        do {
slouken@1895
    80
            SDL_Delay(10);
slouken@2735
    81
        } while ((be_app == NULL) || be_app->IsLaunching());
slouken@0
    82
slouken@1895
    83
        /* Mark the application active */
slouken@1895
    84
        SDL_BeAppActive = 0;
slouken@1895
    85
    }
slouken@1895
    86
slouken@1895
    87
    /* Increment the application reference count */
slouken@1895
    88
    ++SDL_BeAppActive;
slouken@1895
    89
slouken@1895
    90
    /* The app is running, and we're ready to go */
slouken@1895
    91
    return (0);
slouken@0
    92
}
slouken@0
    93
slouken@0
    94
/* Quit the Be Application, if there's nothing left to do */
slouken@1895
    95
void
slouken@1895
    96
SDL_QuitBeApp(void)
slouken@0
    97
{
slouken@1895
    98
    /* Decrement the application reference count */
slouken@1895
    99
    --SDL_BeAppActive;
slouken@0
   100
slouken@1895
   101
    /* If the reference count reached zero, clean up the app */
slouken@1895
   102
    if (SDL_BeAppActive == 0) {
slouken@1895
   103
        if (SDL_AppThread != NULL) {
slouken@1895
   104
            if (be_app != NULL) {       /* Not tested */
slouken@1895
   105
                be_app->PostMessage(B_QUIT_REQUESTED);
slouken@1895
   106
            }
slouken@1895
   107
            SDL_WaitThread(SDL_AppThread, NULL);
slouken@1895
   108
            SDL_AppThread = NULL;
slouken@1895
   109
        }
slouken@1895
   110
        /* be_app should now be NULL since be_app has quit */
slouken@1895
   111
    }
slouken@0
   112
}
slouken@1895
   113
slouken@1895
   114
/* vi: set ts=4 sw=4 expandtab: */