main.c revision 666c1a44cabae77b4a63312ded6341ad94ad0b07
/*
Platypus - create MacOS X application bundles that execute scripts
This is the executable that goes into Platypus apps
Copyright (C) 2003 Sveinbjorn Thordarson <sveinbt@hi.is>
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
main.c - main program file
*/
///////////////////////////////////////
// Includes
///////////////////////////////////////
// Apple stuff
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Authorization.h>
#include <Security/AuthorizationTags.h>
// Unix stuff
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
///////////////////////////////////////
// Definitions
///////////////////////////////////////
#pragma mark Definitions
// name length limits
#define kMaxPathLength 1024
// names of files bundled with app
#define kScriptFileName "script"
#define kOpenDocFileName "openDoc"
// custom carbon events
#define kEventClassRedFatalAlert 911
#define kEventKindX11Failed 911
#define kEventKindFCCacheFailed 912
//maximum arguments the script accepts
#define kMaxArgumentsToScript 252
///////////////////////////////////////
// Prototypes
///////////////////////////////////////
#pragma mark Prototypes
static void GetParameters(void);
static char* GetScript(void);
static char* GetOpenDoc(void);
static short DoesFileExist(char *path);
static OSStatus FixFCCache(void);
///////////////////////////////////////
// Globals
///////////////////////////////////////
// process id of forked process
// thread id of threads that start scripts
// indicator of whether the script has completed executing
short taskDone = true;
// execution parameters
char scriptPath[kMaxPathLength];
char openDocPath[kMaxPathLength];
//arguments to the script
char *fileArgs[kMaxArgumentsToScript];
short numArgs = 0;
extern char **environ;
#pragma mark -
///////////////////////////////////////
// Program entrance point
///////////////////////////////////////
{
InitCursor();
//install Apple Event handlers
0, false);
0, false);
0, false);
"\pError initing Apple Event handlers.");
//create the menu bar
"\pError loading MenuBar.nib.");
GetParameters(); //load data from files containing exec settings
RunApplicationEventLoop(); //Run the event loop
return 0;
}
#pragma mark -
static void RequestUserAttention(void)
{
notificationRequest->nmIcon = 0;
notificationRequest->nmSound = 0;
}
static void ShowFirstStartWarningDialog(void)
{
params.helpButton = false;
"\pWhile Inkscape is open, its windows can be displayed or hidden by displaying or hiding the X11 application.\n\nThe first time this version of Inkscape is run it may take several minutes before the main window is displayed while font caches are built.",
}
//////////////////////////////////
// Handler for when fontconfig caches need to be generated
//////////////////////////////////
{
// Bounce Inkscape Dock icon
// Need to show warning to the user, then carry on.
// Note that we've seen the warning.
system("test -d \"$HOME/.inkscape\" || mkdir \"$HOME/.inkscape\"; "
"touch \"$HOME/.inkscape/.fccache-new\"");
// Rerun now.
ExitToShell();
return noErr;
}
{
while ((bytesToRead > 0))
{
if (bytesRead > 0)
{
bytesToRead -= bytesRead;
}
else if (bytesRead == 0)
{
// Reached EOF.
break;
}
else if (bytesRead == -1)
{
{
// Try again.
continue;
}
return 0;
}
}
return bytesRead;
}
/////////////////////////////////////
// Code to run fc-cache on first run
/////////////////////////////////////
static OSStatus FixFCCache (void)
{
int fdConnToChild = 0;
int status;
char commandStr[] = "/usr/X11R6/bin/fc-cache";
// Run fc-cache
{
{
0
}
};
{
1,
};
if (err == errAuthorizationSuccess)
{
if (err == errAuthorizationSuccess)
{
// Unfortunately, AuthorizationExecuteWithPrivileges
// does not return the process ID associated with the
// process it runs. The best solution we have it to
// try and get the process ID from the file descriptor.
// This is based on example code from Apple's
// MoreAuthSample.
// Try an get the process ID of the fc-cache command
bytesChildPID = sizeof(childPID);
if (bytesRead != bytesChildPID)
{
// If we can't get it the best alternative
// is to wait for any child to finish.
}
if (fileConnToChild != NULL) {
}
// Wait for child process to finish.
}
}
return err;
}
///////////////////////////////////
// Execution thread starts here
///////////////////////////////////
{
taskDone = false;
}
}
else ExitToShell();
return 0;
}
///////////////////////////////////
// Open additional documents thread starts here
///////////////////////////////////
{
return 0;
}
///////////////////////////////////////
// Run a script via the system command
///////////////////////////////////////
{
int status, i;
// Generate the array of argument strings before we do any executing
else if (*pid == 0) { //child process started
}
}
#pragma mark -
///////////////////////////////////////
// This function loads all the neccesary settings
// from config files in the Resources folder
///////////////////////////////////////
static void GetParameters (void)
{
char *str;
RedFatalAlert("\pInitialization Error",
"\pError getting script from application bundle.");
RedFatalAlert("\pInitialization Error",
"\pError getting openDoc from application bundle.");
}
///////////////////////////////////////
// Get path to the script in Resources folder
///////////////////////////////////////
static char* GetScript (void)
{
char *path;
//get CF URL for script
return NULL;
//Get file reference from Core Foundation URL
//dispose of the CF variables
//convert FSRef to FSSpec
//create path string
return path;
}
///////////////////////////////////////
// Gets the path to openDoc in Resources folder
///////////////////////////////////////
static char* GetOpenDoc (void)
{
char *path;
//get CF URL for openDoc
return NULL;
//Get file reference from Core Foundation URL
//dispose of the CF variables
//convert FSRef to FSSpec
//create path string
return path;
}
#pragma mark -
/////////////////////////////////////
// Load menu bar from nib
/////////////////////////////////////
{
return noErr;
}
#pragma mark -
///////////////////////////////////////
// Generate path string from FSSpec record
///////////////////////////////////////
{
//create file reference from file spec
// and then convert the FSRef to a path
}
////////////////////////////////////////
// Standard red error alert, then exit application
////////////////////////////////////////
{
ExitToShell();
}
///////////////////////////////////////
// Determines whether file exists at path or not
///////////////////////////////////////
static short DoesFileExist (char *path)
{
return true;
}
#pragma mark -
///////////////////////////////////////
// Apple Event handler for Quit i.e. from
// the dock or Application menu item
///////////////////////////////////////
{
}
ExitToShell();
return noErr;
}
/////////////////////////////////////
// Handler for docs dragged on app icon
/////////////////////////////////////
{
short i;
long count, actualSize;
char path[kMaxPathLength];
//Read the AppleEvent
&fileSpecList);
//get fsspec from apple event
{
//get path from file spec
kMaxPathLength))) return err;
if (numArgs == kMaxArgumentsToScript) break;
}
else return err;
}
return err;
}
///////////////////////////////
// Handler for clicking on app icon
///////////////////////////////
{
// the app has been opened without any items dragged on to it
return noErr;
}
{
// Use Internet Config to hand the URL to the appropriate application, as
// set by the user in the Internet Preferences pane.
// Applications creator code:
{
long start = 0;
// Don't bother testing return value (error); launched application will
// report problems.
ICStop( icInstance );
}
}
//////////////////////////////////
// Handler for when X11 fails to start
//////////////////////////////////
{
const char *getX11 = "\pGet X11 for Panther";
params.helpButton = false;
"\pInkscape.app requires Apple's X11, which is freely downloadable from Apple's website for Panther (10.3.x) users and available as an optional install from the installation DVD for Tiger (10.4.x) users.\n\nPlease install X11 and restart Inkscape.",
if (itemHit == kAlertStdAlertCancelButton)
{
}
ExitToShell();
return noErr;
}