cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_BeApp.cc (3781B)


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