Ignore:
Timestamp:
05/09/06 09:13:54 (18 years ago)
Author:
rchin
Message:

Fixed commit of nib file.

Added support for automatic injection into apps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/F-Script Anywhere/Source/FSAAppList.m

    r153 r219  
    2525*/
    2626
     27#include <sys/types.h>
     28#include <sys/sysctl.h>
    2729#import "FSAAppList.h"
    2830#import "FSAnywhere.h"
     
    3941NSString * const FSATableColumnIdentifier_appNameAndIcon = @"appNameAndIcon";
    4042NSString * const FSATableColumnIdentifier_checkMark = @"checkMark";
     43NSString * const FSATableColumnIdentifier_always = @"always";
    4144
    4245NSString *FSACheckMarkCharacter;
     
    5255    NULL
    5356};
     57
     58static int sysctlbyname_with_pid (const char *name, pid_t pid,
     59                                                                  void *oldp, size_t *oldlenp,
     60                                                                  void *newp, size_t newlen);
     61int is_pid_native (pid_t pid);
    5462
    5563@implementation FSAAppList
     
    8492    patchingApps = [[NSMutableSet alloc] init];
    8593    appsByPID = [[NSMutableDictionary alloc] init];
     94        if(!([[NSUserDefaults standardUserDefaults] objectForKey:@"AlwaysApps"]))
     95                alwaysApps = [[NSMutableArray array] retain];
     96        else
     97                alwaysApps = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlwaysApps"]] retain];
    8698
    8799    [[tableView tableColumnWithIdentifier: FSATableColumnIdentifier_appNameAndIcon]
     
    91103            setDataCell: [[[NSImageCell alloc] init] autorelease]];
    92104    [window setResizeIncrements: NSMakeSize(1, [tableView cellHeight])];
     105        [tableView setTarget:self];
     106        [tableView setAction:@selector(clickInTable:)];
    93107
    94108    [self update];
     
    169183        return NO;
    170184    return appContainsLibMatching([bundleExecutableLoc fileSystemRepresentation], FSACocoaFrameworks);
     185}
     186
     187-(BOOL)appIsNative:(DeVercruesseProcess *)app
     188{
     189        return is_pid_native([app pid]);
    171190}
    172191
     
    212231         ( ( [app isCocoa] && ![self appIsPEF: app]) ||
    213232           ( [app isCarbon] && [self appIsCocoa: app]))) {
    214         [cocoaApps addObject: app];
    215     }
    216     [appsByPID setObject: app forKey: [NSNumber numberWithInt: [app pid]]];
     233                if([self appIsNative:app]){
     234                        [cocoaApps addObject: app];
     235                        if(finishedLaunch){
     236                                if([alwaysApps containsObject:[app name]] && ![patchedApps containsObject:app])
     237                                        [NSApp installBundleInAppWithPID:[app pid]];
     238                        }
     239                }
     240    }
     241    [appsByPID setObject: app forKey: [NSNumber numberWithInt: [app pid]]];     
    217242}
    218243
     
    333358                return FSAEllipsisImage;
    334359        }
    335     }
     360    } else if([columnIdentifier isEqualToString:FSATableColumnIdentifier_always]){
     361                if([alwaysApps containsObject:[[cocoaApps objectAtIndex: rowIndex] name]])
     362                        return [NSNumber numberWithBool:YES];
     363                else
     364                        return [NSNumber numberWithBool:NO];
     365        }
    336366    return nil;
    337367}
     
    418448}
    419449
     450- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
     451{
     452        id anApp;
     453        id e = [cocoaApps objectEnumerator];
     454        while(anApp = [e nextObject]){
     455                if([alwaysApps containsObject:[anApp name]] && ![patchedApps containsObject:anApp]){
     456                        [NSApp installBundleInAppWithPID:[anApp pid]];
     457                }
     458        }
     459        finishedLaunch = YES;
     460}
     461
    420462- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender;
    421463{
     
    423465}
    424466
     467-(void)clickInTable:(id)sender
     468{
     469        if([sender clickedColumn] == 2){
     470                if([sender clickedRow] >= 0){
     471                        NSString *appName = [[cocoaApps objectAtIndex:[sender clickedRow]] name];
     472                        if(![alwaysApps containsObject:appName]){
     473                                [NSApp installBundleInAppWithPID:[appName pid]];
     474                                [alwaysApps addObject:appName];
     475                        } else
     476                                [alwaysApps removeObject:appName];
     477                        [[NSUserDefaults standardUserDefaults] setObject:alwaysApps forKey:@"AlwaysApps"];
     478                }
     479        }
     480}
     481
    425482@end
     483
     484static int sysctlbyname_with_pid (const char *name, pid_t pid,
     485                                                                  void *oldp, size_t *oldlenp,
     486                                                                  void *newp, size_t newlen)
     487{
     488    if (pid == 0) {
     489        if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1)  {
     490            fprintf(stderr, "sysctlbyname_with_pid(0): sysctlbyname  failed:"
     491                                        "%s\n", strerror(errno));
     492            return -1;
     493        }
     494    } else {
     495        int mib[CTL_MAXNAME];
     496        size_t len = CTL_MAXNAME;
     497        if (sysctlnametomib(name, mib, &len) == -1) {
     498            fprintf(stderr, "sysctlbyname_with_pid: sysctlnametomib  failed:"
     499                                        "%s\n", strerror(errno));
     500            return -1;
     501        }
     502        mib[len] = pid;
     503        len++;
     504        if (sysctl(mib, len, oldp, oldlenp, newp, newlen) == -1)  {
     505            fprintf(stderr, "sysctlbyname_with_pid: sysctl  failed:"
     506                    "%s\n", strerror(errno));
     507            return -1;
     508        }
     509    }
     510    return 0;
     511}
     512
     513int is_pid_native (pid_t pid)
     514{
     515    int ret = 0;
     516    size_t sz = sizeof(ret);
     517       
     518    if (sysctlbyname_with_pid("sysctl.proc_native", pid,
     519                                                          &ret, &sz, NULL, 0) == -1) {
     520                if (errno == ENOENT) {
     521            // sysctl doesn't exist, which means that this version of Mac OS
     522            // pre-dates Rosetta, so the application must be native.
     523            return 1;
     524        }
     525        fprintf(stderr, "is_pid_native: sysctlbyname_with_pid  failed:"
     526                "%s\n", strerror(errno));
     527        return -1;
     528    }
     529    return ret;
     530}       
Note: See TracChangeset for help on using the changeset viewer.