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