2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-06-28 10:34:47 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-06-28 10:34:47 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2005-06-28 10:34:47 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-06-28 10:34:47 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2005-06-28 10:34:47 +00:00
|
|
|
|
|
|
|
/* Mac-specific code to display custom icon for science application (optional)
|
2011-09-27 19:45:27 +00:00
|
|
|
adapted from code written by Bernd Machenschalk. Used with permission of the
|
2005-06-28 10:34:47 +00:00
|
|
|
Einstein@home project.
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2005-06-28 10:34:47 +00:00
|
|
|
To use this code:
|
|
|
|
1. Create a *.icns file using "/Developer/Applications/utilities/Icon Composer.app"
|
2011-09-27 19:45:27 +00:00
|
|
|
2. Convert the *.icns file to an app_icon.h file as follows: in Terminal, run
|
|
|
|
"MakeAppIcon_h <source_file> <dest_file>". (The MakeAppIcon_h command-line utility
|
2005-06-28 10:34:47 +00:00
|
|
|
is built by the Mac boinc XCode project.)
|
|
|
|
3. In the science application's main(), #include "app_icon.h" and call:
|
|
|
|
setMacIcon(argv[0], MacAppIconData, sizeof(MacAppIconData));
|
|
|
|
4. Science application must link with Carbon.framework.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
2007-09-28 02:06:30 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2005-06-28 10:34:47 +00:00
|
|
|
#include "boinc_api.h"
|
2011-03-24 09:32:02 +00:00
|
|
|
#include "filesys.h"
|
2007-09-28 02:06:30 +00:00
|
|
|
#include "common_defs.h"
|
2005-06-28 10:34:47 +00:00
|
|
|
|
|
|
|
#define RESIDICON -16455
|
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
char MacPListData[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
|
|
"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
|
|
|
|
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
|
|
|
|
"<plist version=\"1.0\">\n<dict>\n"
|
|
|
|
"\t<key>NSUIElement</key>\n\t<string>1</string>\n</dict>\n</plist>\n";
|
|
|
|
|
|
|
|
|
2005-06-28 10:34:47 +00:00
|
|
|
/* checks for an OS error, reports the line number and returns */
|
|
|
|
#define CHECK_OSERR(e) {\
|
|
|
|
if (e) {\
|
2006-02-27 12:19:38 +00:00
|
|
|
fprintf(stderr,"MacOS Error %d occured in %s line %d\n",e,__FILE__,__LINE__);\
|
2005-06-28 10:34:47 +00:00
|
|
|
return(e); } }
|
|
|
|
|
2007-09-28 02:06:30 +00:00
|
|
|
// Adds the specified resource to the file given as an argument.
|
2011-09-27 19:45:27 +00:00
|
|
|
int setMacRsrcForFile(char *filename, char *rsrcData, long rsrcSize,
|
|
|
|
OSType rsrcType, int rsrcID, ConstStringPtr rsrcName) {
|
2006-08-19 10:35:55 +00:00
|
|
|
OSErr oserr; /* stores an OS error code */
|
2007-11-16 13:27:35 +00:00
|
|
|
// FSSpec fsspec; /* FileSpec */
|
|
|
|
HFSUniStr255 forkName; /* Unicode name of resource fork "RESOURCE_FORK" */
|
2006-08-19 10:35:55 +00:00
|
|
|
FSRef fsref; /* File Reference */
|
|
|
|
FSCatalogInfo catalogInfo; /* For setting custom icon bit in Finder Info */
|
|
|
|
short rref; /* Resource Reference */
|
2005-06-28 10:34:47 +00:00
|
|
|
Handle hand;
|
2005-09-06 12:58:56 +00:00
|
|
|
int retry;
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2005-06-28 10:34:47 +00:00
|
|
|
/* get finder spec for this file */
|
2006-02-07 01:08:02 +00:00
|
|
|
CHECK_OSERR((int)FSPathMakeRef((StringPtr)filename, &fsref, NULL));
|
2007-11-16 13:27:35 +00:00
|
|
|
// CHECK_OSERR(FSGetCatalogInfo(&fsref, nil, NULL, NULL, &fsspec, NULL));
|
2005-06-28 10:34:47 +00:00
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
/* Open the resource fork for writing, create it if it does not exist.
|
2011-09-27 19:45:27 +00:00
|
|
|
On a dual-processor system, the other cpu may have the resource fork
|
2005-09-06 12:58:56 +00:00
|
|
|
open for writing, so if we fail we wait and retry.
|
|
|
|
*/
|
|
|
|
for (retry=0;retry<5;retry++) {
|
2007-11-16 13:27:35 +00:00
|
|
|
// rref = FSpOpenResFile(&fsspec, fsRdWrPerm);
|
|
|
|
rref = FSOpenResFile(&fsref, fsRdWrPerm);
|
2005-09-06 12:58:56 +00:00
|
|
|
oserr = ResError();
|
|
|
|
if (oserr == eofErr) { /* EOF, resource fork/file not found */
|
|
|
|
// If we set file type and signature to non-NULL, it makes OS mistakenly
|
|
|
|
// identify file as a classic application instead of a UNIX executable.
|
2007-11-16 13:27:35 +00:00
|
|
|
// FSpCreateResFile(&fsref, 0, 0, smRoman);
|
|
|
|
oserr = FSGetResourceForkName(&forkName);
|
|
|
|
if (oserr == noErr) {
|
|
|
|
oserr = FSCreateResourceFork(&fsref, forkName.length, forkName.unicode, 0);
|
|
|
|
}
|
2005-09-06 12:58:56 +00:00
|
|
|
oserr = ResError();
|
|
|
|
if (oserr == noErr) {
|
2007-11-16 13:27:35 +00:00
|
|
|
// rref = FSpOpenResFile(&fsspec, fsRdWrPerm);
|
|
|
|
rref = FSOpenResFile(&fsref, fsRdWrPerm);
|
2005-09-06 12:58:56 +00:00
|
|
|
oserr = ResError();
|
|
|
|
}
|
|
|
|
}
|
2007-09-28 02:06:30 +00:00
|
|
|
// We may not have permissions to set resources in debug runs
|
|
|
|
if ((oserr == noErr) || (oserr == wrPermErr) || (oserr == permErr))
|
2011-09-27 19:45:27 +00:00
|
|
|
break;
|
2005-09-06 12:58:56 +00:00
|
|
|
sleep (1);
|
|
|
|
};
|
2005-06-28 10:34:47 +00:00
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
if (oserr)
|
|
|
|
return oserr; // give up after 5 seconds
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
/* add the resource if not already present */
|
|
|
|
if (!GetResource(rsrcType, rsrcID)) { /* if resource not found */
|
|
|
|
oserr = PtrToHand(rsrcData, &hand, rsrcSize);
|
|
|
|
if (!oserr)
|
|
|
|
AddResource(hand, rsrcType, rsrcID, rsrcName);
|
2005-06-28 10:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add this to the file on disk */
|
|
|
|
CloseResFile(rref);
|
|
|
|
CHECK_OSERR(ResError());
|
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
if (rsrcType == 'icns') {
|
|
|
|
/* set custom icon flag */
|
2006-08-19 10:35:55 +00:00
|
|
|
CHECK_OSERR(FSGetCatalogInfo(&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL));
|
|
|
|
((FileInfo *)&catalogInfo.finderInfo)->finderFlags |= kHasCustomIcon;
|
|
|
|
CHECK_OSERR( FSSetCatalogInfo(&fsref, kFSCatInfoFinderInfo, &catalogInfo));
|
2005-09-06 12:58:56 +00:00
|
|
|
}
|
2005-06-28 10:34:47 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-28 09:53:10 +00:00
|
|
|
static char * PersistentFGets(char *buf, size_t buflen, FILE *f) {
|
|
|
|
char *p = buf;
|
|
|
|
size_t len = buflen;
|
|
|
|
size_t datalen = 0;
|
|
|
|
|
|
|
|
*buf = '\0';
|
|
|
|
while (datalen < (buflen - 1)) {
|
|
|
|
fgets(p, len, f);
|
|
|
|
if (feof(f)) break;
|
|
|
|
if (ferror(f) && (errno != EINTR)) break;
|
|
|
|
if (strchr(buf, '\n')) break;
|
|
|
|
datalen = strlen(buf);
|
|
|
|
p = buf + datalen;
|
|
|
|
len -= datalen;
|
|
|
|
}
|
|
|
|
return (buf[0] ? buf : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getPathToThisApp(char* pathBuf, size_t bufSize) {
|
|
|
|
FILE *f;
|
2007-09-28 02:06:30 +00:00
|
|
|
char buf[MAXPATHLEN], *c;
|
2006-02-28 09:53:10 +00:00
|
|
|
pid_t myPID = getpid();
|
|
|
|
int i;
|
2007-09-28 02:06:30 +00:00
|
|
|
struct stat stat_buf;
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2007-09-28 02:06:30 +00:00
|
|
|
strcpy(pathBuf, GRAPHICS_APP_FILENAME);
|
|
|
|
if (!stat(pathBuf, &stat_buf)) {
|
|
|
|
// stat() returns zero on success
|
|
|
|
return;
|
|
|
|
}
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2006-02-28 09:53:10 +00:00
|
|
|
*pathBuf = 0; // in case of failure
|
2011-09-27 19:45:27 +00:00
|
|
|
|
|
|
|
// Before launching this project application, the BOINC client set the
|
|
|
|
// current directory to the slot directory which contains this application
|
|
|
|
// (or the soft-link to it.) So all we need for the path to this
|
|
|
|
// application is the file name. We use the -c option so ps strips off
|
2006-02-28 09:53:10 +00:00
|
|
|
// any command-line arguments for us.
|
2007-09-28 02:06:30 +00:00
|
|
|
sprintf(buf, "ps -wcp %d -o command=", myPID);
|
2006-02-28 09:53:10 +00:00
|
|
|
f = popen(buf, "r");
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
PersistentFGets(pathBuf, bufSize, f); // Skip over line of column headings
|
|
|
|
PersistentFGets(pathBuf, bufSize, f); // Get the UNIX command which ran us
|
2007-10-12 06:22:58 +00:00
|
|
|
pclose(f);
|
2006-02-28 09:53:10 +00:00
|
|
|
|
2011-09-27 19:45:27 +00:00
|
|
|
c = strstr(pathBuf, " -");
|
2006-02-28 09:53:10 +00:00
|
|
|
if (c)
|
|
|
|
*c = 0; // Strip off any command-line arguments
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2006-02-28 09:53:10 +00:00
|
|
|
for (i=strlen(pathBuf)-1; i>=0; --i) {
|
|
|
|
if (pathBuf[i] <= ' ')
|
|
|
|
pathBuf[i] = 0; // Strip off trailing spaces, newlines, etc.
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:45:27 +00:00
|
|
|
// Adds plst resource 0 to the file given as an argument. This
|
|
|
|
// identifies the application to the OS as an NSUIElement, so
|
|
|
|
// that the application does not show in the Dock and it has no
|
2005-09-06 12:58:56 +00:00
|
|
|
// menu bar.
|
2006-02-28 09:53:10 +00:00
|
|
|
int setMacPList() {
|
2011-09-27 19:45:27 +00:00
|
|
|
int rc;
|
2007-09-28 02:06:30 +00:00
|
|
|
char path[1024], resolvedPath[1024];
|
2011-09-27 19:45:27 +00:00
|
|
|
const char plistStr[] = "Application PList";
|
|
|
|
CFStringRef plistCFStr = CFStringCreateWithCString(kCFAllocatorDefault, plistStr, kCFStringEncodingMacRoman);
|
|
|
|
ConstStringPtr rsrcName = CFStringGetPascalStringPtr(plistCFStr, kCFStringEncodingMacRoman);
|
|
|
|
if (!rsrcName) {
|
|
|
|
return -2;
|
|
|
|
}
|
2005-09-06 12:58:56 +00:00
|
|
|
|
2011-09-27 19:45:27 +00:00
|
|
|
// If resource already exists, don't call getPathToThisApp()
|
2007-10-11 01:41:33 +00:00
|
|
|
// which leaves a zombie process.
|
2011-09-27 19:45:27 +00:00
|
|
|
if (GetResource('plst', 0)) {
|
2007-10-11 01:41:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2006-02-28 09:53:10 +00:00
|
|
|
getPathToThisApp(path, sizeof(path));
|
|
|
|
if (path[0] == 0)
|
|
|
|
return -1; // Should never happen
|
2011-09-27 19:45:27 +00:00
|
|
|
|
2006-02-28 09:53:10 +00:00
|
|
|
setMacRsrcForFile(path, MacPListData, sizeof(MacPListData), 'plst', 0, rsrcName);
|
|
|
|
boinc_resolve_filename(path, resolvedPath, sizeof(resolvedPath));
|
2011-09-27 19:45:27 +00:00
|
|
|
rc = setMacRsrcForFile(resolvedPath, MacPListData, sizeof(MacPListData), 'plst', 0, rsrcName);
|
|
|
|
|
|
|
|
CFRelease(plistCFStr);
|
|
|
|
return rc;
|
2005-09-06 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-28 10:34:47 +00:00
|
|
|
// Adds icns resource to the file given as an argument.
|
2011-09-27 19:45:27 +00:00
|
|
|
// If the file is a soft link, also adds icns resource to the resolved flle.
|
2005-06-28 10:34:47 +00:00
|
|
|
// Typically called from a main() with argv[0] to attach resources to itself */
|
|
|
|
int setMacIcon(char *filename, char *iconData, long iconSize) {
|
2011-09-27 19:45:27 +00:00
|
|
|
int rc;
|
2005-09-06 12:58:56 +00:00
|
|
|
char path[1024];
|
2011-09-27 19:45:27 +00:00
|
|
|
const char iconStr[] = "Application icons";
|
|
|
|
CFStringRef iconCFStr = CFStringCreateWithCString(kCFAllocatorDefault, iconStr, kCFStringEncodingMacRoman);
|
|
|
|
ConstStringPtr rsrcName = CFStringGetPascalStringPtr(iconCFStr, kCFStringEncodingMacRoman); // FIXME: How to release this?
|
|
|
|
if (!rsrcName) {
|
|
|
|
return -2;
|
|
|
|
}
|
2005-06-28 10:34:47 +00:00
|
|
|
|
2005-09-06 12:58:56 +00:00
|
|
|
setMacRsrcForFile(filename, iconData, iconSize, 'icns', RESIDICON, rsrcName);
|
2005-06-28 10:34:47 +00:00
|
|
|
boinc_resolve_filename(filename, path, sizeof(path));
|
2011-09-27 19:45:27 +00:00
|
|
|
rc = setMacRsrcForFile(path, iconData, iconSize, 'icns', RESIDICON, rsrcName);
|
|
|
|
|
|
|
|
CFRelease(iconCFStr);
|
|
|
|
return rc;
|
2005-06-28 10:34:47 +00:00
|
|
|
}
|