src/main/beos/SDL_BeApp.cc
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Mar 2002 19:55:32 +0000
changeset 292 eadc0746dfaf
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
Added SDL_LockRect() and SDL_UnlockRect()
Incorporated XFree86 extension libraries into the source
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Handle the BeApp specific portions of the application */
slouken@0
    29
slouken@0
    30
#include <AppKit.h>
slouken@0
    31
#include <storage/Path.h>
slouken@0
    32
#include <storage/Entry.h>
slouken@0
    33
#include <stdlib.h>
slouken@0
    34
#include <unistd.h>
slouken@0
    35
slouken@0
    36
#include "SDL_BeApp.h"
slouken@0
    37
#include "SDL_thread.h"
slouken@0
    38
#include "SDL_timer.h"
slouken@0
    39
#include "SDL_error.h"
slouken@0
    40
slouken@0
    41
/* Flag to tell whether or not the Be application is active or not */
slouken@0
    42
int SDL_BeAppActive = 0;
slouken@0
    43
static SDL_Thread *SDL_AppThread = NULL;
slouken@0
    44
slouken@0
    45
static int StartBeApp(void *unused)
slouken@0
    46
{
slouken@0
    47
	BApplication *App;
slouken@0
    48
slouken@0
    49
	App = new BApplication("application/x-SDL-executable");
slouken@0
    50
slouken@0
    51
	App->Run();
slouken@0
    52
	delete App;
slouken@0
    53
	return(0);
slouken@0
    54
}
slouken@0
    55
slouken@0
    56
/* Initialize the Be Application, if it's not already started */
slouken@0
    57
int SDL_InitBeApp(void)
slouken@0
    58
{
slouken@0
    59
	/* Create the BApplication that handles appserver interaction */
slouken@0
    60
	if ( SDL_BeAppActive <= 0 ) {
slouken@0
    61
		SDL_AppThread = SDL_CreateThread(StartBeApp, NULL);
slouken@0
    62
		if ( SDL_AppThread == NULL ) {
slouken@0
    63
			SDL_SetError("Couldn't create BApplication thread");
slouken@0
    64
			return(-1);
slouken@0
    65
		}
slouken@0
    66
		
slouken@0
    67
		/* Check if we started from Terminal or Tracker... */
slouken@0
    68
		/* Based on code posted to BeDevTalk by Marco Nelissen */
slouken@0
    69
		char *cmd = getenv("_"); 
slouken@0
    70
		if(!(cmd == NULL || strlen(cmd) < 7) && 
slouken@0
    71
			(!strcmp(cmd + strlen(cmd) - 7 , "Tracker"))) { 
slouken@0
    72
	
slouken@0
    73
			/* Change working to directory to that of executable */
slouken@0
    74
			app_info info;
slouken@0
    75
			if (B_OK == be_app->GetAppInfo(&info)) {
slouken@0
    76
				entry_ref ref = info.ref;
slouken@0
    77
				BEntry entry;
slouken@0
    78
				if (B_OK == entry.SetTo(&ref)) {
slouken@0
    79
					BPath path;
slouken@0
    80
					if (B_OK == path.SetTo(&entry)) {
slouken@0
    81
						if (B_OK == path.GetParent(&path)) {
slouken@0
    82
							chdir(path.Path());
slouken@0
    83
						}
slouken@0
    84
					}
slouken@0
    85
				}
slouken@0
    86
			}	
slouken@0
    87
		} /* else started from Terminal */
slouken@0
    88
		
slouken@0
    89
		do {
slouken@0
    90
			SDL_Delay(10);
slouken@0
    91
		} while ( (be_app == NULL) || be_app->IsLaunching() );
slouken@0
    92
slouken@0
    93
		/* Mark the application active */
slouken@0
    94
		SDL_BeAppActive = 0;
slouken@0
    95
	}
slouken@0
    96
slouken@0
    97
	/* Increment the application reference count */
slouken@0
    98
	++SDL_BeAppActive;
slouken@0
    99
slouken@0
   100
	/* The app is running, and we're ready to go */
slouken@0
   101
	return(0);
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
/* Quit the Be Application, if there's nothing left to do */
slouken@0
   105
void SDL_QuitBeApp(void)
slouken@0
   106
{
slouken@0
   107
	/* Decrement the application reference count */
slouken@0
   108
	--SDL_BeAppActive;
slouken@0
   109
slouken@0
   110
	/* If the reference count reached zero, clean up the app */
slouken@0
   111
	if ( SDL_BeAppActive == 0 ) {
slouken@0
   112
		if ( SDL_AppThread != NULL ) {
slouken@0
   113
			if ( be_app != NULL ) { /* Not tested */
slouken@0
   114
				be_app->PostMessage(B_QUIT_REQUESTED);
slouken@0
   115
			}
slouken@0
   116
			SDL_WaitThread(SDL_AppThread, NULL);
slouken@0
   117
			SDL_AppThread = NULL;
slouken@0
   118
		}
slouken@0
   119
		/* be_app should now be NULL since be_app has quit */
slouken@0
   120
	}
slouken@0
   121
}