source: trunk/Cocoa/F-Script Anywhere/Source/FSAAppList.m@ 621

Last change on this file since 621 was 412, checked in by Nicholas Riley, 17 years ago

Fewer warnings and extraneous build settings.

File size: 17.4 KB
RevLine 
[7]1//
2// FSAAppList.m
3// F-Script Anywhere
4//
5// Created by Nicholas Riley on Fri Feb 01 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9/*
[221]10
[7]11 F-Script Anywhere is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
[221]15
[7]16 F-Script Anywhere is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
[221]20
[7]21 You should have received a copy of the GNU General Public License
22 along with F-Script Anywhere; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
[221]24
25 */
[7]26
[219]27#include <sys/types.h>
28#include <sys/sysctl.h>
[412]29#import "FSAApp.h"
[153]30#import "FSAAppList.h"
[16]31#import "FSAnywhere.h"
[7]32#import "libMatch.h"
33#import "DeVercruesseProcessManager.h"
34#import "NJRLabeledImageCell.h"
35#import "NSTableView-NJRExtensions.h"
36
[16]37// for appIsPEF:
38#import <Carbon/Carbon.h>
39#import <fcntl.h>
40#import <unistd.h>
41
[7]42NSString * const FSATableColumnIdentifier_appNameAndIcon = @"appNameAndIcon";
43NSString * const FSATableColumnIdentifier_checkMark = @"checkMark";
[219]44NSString * const FSATableColumnIdentifier_always = @"always";
[7]45
46NSString *FSACheckMarkCharacter;
[16]47NSImage *FSACheckMarkImage;
[7]48
[153]49NSString *FSAEllipsisCharacter;
50NSImage *FSAEllipsisImage;
51
[7]52static const char *FSACocoaFrameworks[] = {
53 "/System/Library/Frameworks/AppKit.framework",
54 "/System/Library/Frameworks/Foundation.framework",
55 "/System/Library/Frameworks/Cocoa.framework",
56 NULL
57};
58
[219]59static int sysctlbyname_with_pid (const char *name, pid_t pid,
[221]60 void *oldp, size_t *oldlenp,
61 void *newp, size_t newlen);
[219]62int is_pid_native (pid_t pid);
63
[7]64@implementation FSAAppList
65
[16]66+ (void)initialize;
[7]67{
68 FSACheckMarkCharacter = [[NSString alloc] initWithCharacters: (const unichar *)"\x27\x13" length: 1];
[16]69 FSACheckMarkImage = [NSImage imageNamed: @"NSMenuCheckmark"];
70 if (FSACheckMarkImage == nil) {
71 FSACheckMarkImage = [[NSImage alloc] initByReferencingFile: [[NSBundle mainBundle] pathForResource: @"Fallback checkmark" ofType: @"tiff"]];
72 if (FSACheckMarkImage != nil && ![FSACheckMarkImage isValid]) {
73 [FSACheckMarkImage release];
74 FSACheckMarkImage = nil;
75 }
76 FSALog(@"Falling back to checkmark image from bundle: %@", FSACheckMarkImage);
77 }
[153]78 FSAEllipsisCharacter = [[NSString alloc] initWithCharacters: (const unichar *)"\x20\x26" length: 1];
79 FSAEllipsisImage = [[NSImage alloc] initByReferencingFile: [[NSBundle mainBundle] pathForResource: @"Ellipsis" ofType: @"tiff"]];
80 if (FSAEllipsisImage != nil && ![FSAEllipsisImage isValid]) {
81 [FSAEllipsisImage release];
82 FSAEllipsisImage = nil;
83 }
[7]84}
85
86- (void)awakeFromNib;
87{
[19]88 NSWindow *window = [tableView window];
89
[7]90 processManager = [DeVercruesseProcessManager defaultManager];
91 cocoaApps = [[NSMutableArray alloc] init];
92 patchedApps = [[NSMutableSet alloc] init];
[153]93 patchingApps = [[NSMutableSet alloc] init];
[7]94 appsByPID = [[NSMutableDictionary alloc] init];
[221]95 alwaysApps = [[NSMutableArray array] retain];
96
[7]97 [[tableView tableColumnWithIdentifier: FSATableColumnIdentifier_appNameAndIcon]
98 setDataCell: [NJRLabeledImageCell cell]];
[16]99 if (FSACheckMarkImage != nil)
100 [[tableView tableColumnWithIdentifier: FSATableColumnIdentifier_checkMark]
101 setDataCell: [[[NSImageCell alloc] init] autorelease]];
[19]102 [window setResizeIncrements: NSMakeSize(1, [tableView cellHeight])];
[221]103 [tableView setTarget:self];
104 [tableView setAction:@selector(clickInTable:)];
105
[7]106 [self update];
[19]107 [window makeFirstResponder: tableView];
[221]108
109 [[NSNotificationCenter defaultCenter] addObserver:self
110 selector:@selector(update)
111 name:NSUserDefaultsDidChangeNotification
112 object:nil];
[7]113}
114
115- (void)dealloc;
116{
117 // don't release processManager, we don't own it
118 [cocoaApps release];
119 [patchedApps release];
120 [appsByPID release];
121 [super dealloc];
122}
123
124- (pid_t)selectedProcessID;
125{
126 int row = [tableView selectedRow];
127 if (row == -1) return -1;
[221]128
[7]129 return [[cocoaApps objectAtIndex: row] pid];
130}
131
[153]132- (void)_processStatusChanged;
[7]133{
134 [tableView reloadData];
[222]135// [self tableView: tableView shouldSelectRow: [tableView selectedRow]];
[7]136}
137
[153]138- (DeVercruesseProcess *)_applicationForPID:(pid_t)pid;
139{
140 return [appsByPID objectForKey: [NSNumber numberWithInt: pid]];
141}
142
143- (void)didPatchProcessID:(pid_t)pid;
144{
145 DeVercruesseProcess *app = [self _applicationForPID: pid];
[222]146 if(app){
147 [patchingApps removeObject: app];
148 [patchedApps addObject: app];
149 }
[153]150 [self _processStatusChanged];
151}
152
153- (void)isPatchingProcessID:(pid_t)pid;
154{
155 [patchingApps addObject: [self _applicationForPID: pid]];
156 [self _processStatusChanged];
157}
158
[16]159- (BOOL)appIsPEF:(DeVercruesseProcess *)app;
160{
161 NSString *bundleExecutableLoc = [app executableLoc];
162 const char *bundleExecutablePath;
163 int fd;
164 PEFContainerHeader pefHeader;
[221]165
[16]166 if (bundleExecutableLoc == NULL)
167 return NO;
168
169 if ( (bundleExecutablePath = [bundleExecutableLoc fileSystemRepresentation]) == NULL)
170 return NO;
[221]171
[16]172 if ( (fd = open(bundleExecutablePath, O_RDONLY, 0)) == -1)
173 return NO;
[221]174
[222]175 if (read(fd, &pefHeader, sizeof(pefHeader)) != sizeof(pefHeader)){
176 close(fd);
[16]177 return NO;
[222]178 } else
179 close(fd);
[16]180
181 if (pefHeader.tag1 != kPEFTag1 || pefHeader.tag2 != kPEFTag2)
182 return NO;
[221]183
[16]184 return YES;
185}
186
[7]187- (BOOL)appIsCocoa:(DeVercruesseProcess *)app;
188{
189 NSString *bundleExecutableLoc = [app executableLoc];
190 if (bundleExecutableLoc == NULL)
191 return NO;
192 return appContainsLibMatching([bundleExecutableLoc fileSystemRepresentation], FSACocoaFrameworks);
193}
194
[219]195-(BOOL)appIsNative:(DeVercruesseProcess *)app
196{
[221]197 return is_pid_native([app pid]);
[219]198}
199
[7]200- (void)addApp:(DeVercruesseProcess *)app;
201{
[16]202 /* Try to determine if the application is a foreground Cocoa application.
[221]203 In Jaguar, itÕs possible to mix Cocoa in a primarily Carbon application,
204 but we don't support such hybrids because the menu items we add depend
205 on Cocoa dispatch mechanisms.
[16]206
[221]207 The CPS 'flavor' mechanism (isCarbon, isCocoa) is broken in Mac OS X
208 10.1.2 through 10.1.5 and possibly earlier, reporting that all Cocoa apps
209 are Carbon apps. So we use some code extracted from otool to check
210 whether the application links to the Foundation, AppKit or Cocoa
211 frameworks. This problem is fixed in Jaguar, except that certain CFM
212 Carbon apps are reported to be Cocoa apps (Drop Drawers is one example).
213 Conversely, the appIsCocoa: code works on _most_ applications, but
214 Jaguar always correctly identifies Cocoa apps as isCocoa.
[16]215
[221]216 So, our checks go like this:
217 Is the application background-only?
218 Is the application Cocoa or Carbon according to the CPS flavor?
219 If it's Cocoa, is it a CFM app? If so, CPS is lying to us.
220 If it's "Carbon", does it link to AppKit, Foundation or Cocoa?
221 If so, it's really a Cocoa app. If not, it's a Carbon app.
222
223 Be careful not to call appIsCocoa: on a Classic application, you will
224 crash.
[16]225 */
[221]226
[16]227 /*
[221]228 if ([app isCocoa] || [app isCarbon]) {
229 NSLog(@"%@ |%@%@%@%@%@", [app name],
230 [app isBackgroundOnly] ? @" bgOnly" : @"",
231 [app isCocoa] ? @" isCocoa" : @"",
232 [app isCarbon] ? @" isCarbon" : @"",
233 [self appIsPEF: app] ? @" appIsPEF" : @"",
234 [self appIsCocoa: app] ? @" appIsCocoa" : @"");
235 }
236 */
[16]237 if ( ![app isBackgroundOnly] &&
238 ( ( [app isCocoa] && ![self appIsPEF: app]) ||
239 ( [app isCarbon] && [self appIsCocoa: app]))) {
[221]240 if([self appIsNative:app]){
241 [cocoaApps addObject: app];
242 if(finishedLaunch){
243 if([alwaysApps containsObject:[app name]] && ![patchedApps containsObject:app])
[412]244 [(FSAApp *)NSApp installBundleInAppWithPID:[app pid]];
[221]245 }
246 }
[7]247 }
[219]248 [appsByPID setObject: app forKey: [NSNumber numberWithInt: [app pid]]];
[7]249}
250
251// XXX should insert/resort on launch too; this is harder because of synchronization issues
252
253- (void)update;
254{
255 NSEnumerator *e;
256 NSArray *allApps;
257 DeVercruesseProcess *app;
[221]258
[7]259 [cocoaApps removeAllObjects];
260 [appsByPID removeAllObjects];
[153]261 // [processManager update] unneeded: [processManager processes] sends update
[221]262
[7]263 allApps = [processManager processes];
264 e = [allApps objectEnumerator];
[221]265
[7]266 while ( (app = [e nextObject]) != nil) {
267 [self addApp: app];
268 }
269
[221]270 if([[NSUserDefaults standardUserDefaults] objectForKey:@"AlwaysApps"]){
271 [[NSUserDefaults standardUserDefaults] synchronize];
272 [alwaysApps removeAllObjects];
273 [alwaysApps addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlwaysApps"]];
274 [tableView reloadData];
275 }
[7]276 [tableView noteNumberOfRowsChanged];
[153]277 [self _processStatusChanged];
[7]278}
279
[153]280- (NSArray *)cocoaAppProcessIDs;
281{
282 NSEnumerator *e = [cocoaApps objectEnumerator];
283 NSMutableArray *pids = [NSMutableArray arrayWithCapacity: [cocoaApps count]];
284 DeVercruesseProcess *app;
285 while ( (app = [e nextObject]) != nil) {
286 [pids addObject: [NSNumber numberWithInt: [app pid]]];
287 }
288 return pids;
289}
290
[7]291- (void)applicationLaunchedWithProcessID:(pid_t)pid;
292{
[153]293 if ([self _applicationForPID: pid] == nil) {
[7]294 [self update];
295 }
296}
297
298- (void)applicationQuitWithProcessID:(pid_t)pid;
299{
[153]300 DeVercruesseProcess *app = [self _applicationForPID: pid];
[221]301
[7]302 if (app != nil) {
303 [cocoaApps removeObject: app];
[153]304 [appsByPID removeObjectForKey: [NSNumber numberWithLong: pid]];
[7]305 [patchedApps removeObject: app];
306 }
[221]307
[153]308 [tableView noteNumberOfRowsChanged];
309 [self _processStatusChanged];
[7]310}
311
312@end
313
314@implementation FSAAppList (NSTableViewDelegate)
315
316- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row;
317{
318 if ([[tableColumn identifier] isEqualToString: FSATableColumnIdentifier_appNameAndIcon]) {
319 DeVercruesseProcess *app = [cocoaApps objectAtIndex: row];
[221]320
[7]321 NSAssert1([cell isKindOfClass: [NJRLabeledImageCell class]], @"Cell is not what we expected, instead %@", cell);
322 [(NJRLabeledImageCell *)cell setImage: [app img]];
323 }
324}
325
326- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
327{
328 BOOL canInstall = NO;
[221]329
[7]330 if (row != -1) {
[153]331 DeVercruesseProcess *app = [cocoaApps objectAtIndex: row];
332 canInstall = !([patchedApps containsObject: app] ||
333 [patchingApps containsObject: app]);
[7]334 }
[221]335
[7]336 [installButton setEnabled: canInstall];
337
338 return YES;
339}
340
341@end
342
343@implementation FSAAppList (NSTableDataSource)
344
345- (int)numberOfRowsInTableView:(NSTableView *)aTableView
346{
347 return [cocoaApps count];
348}
349
350- (id)tableView:(NSTableView *)aTableView
351 objectValueForTableColumn:(NSTableColumn *)aTableColumn
[221]352 row:(int)rowIndex
[7]353{
354 NSString *columnIdentifier = [aTableColumn identifier];
355
356 if ([columnIdentifier isEqualToString: FSATableColumnIdentifier_appNameAndIcon]) {
357 return [(DeVercruesseProcess *)[cocoaApps objectAtIndex: rowIndex] name];
358 } else if ([columnIdentifier isEqualToString: FSATableColumnIdentifier_checkMark]) {
[153]359 DeVercruesseProcess *app = [cocoaApps objectAtIndex: rowIndex];
360 if ([patchedApps containsObject: app]) {
361 if (FSACheckMarkImage == nil)
362 return FSACheckMarkCharacter;
363 else
364 return FSACheckMarkImage;
365 }
366 if ([patchingApps containsObject: app]) {
367 if (FSAEllipsisImage == nil)
368 return FSAEllipsisCharacter;
369 else
370 return FSAEllipsisImage;
371 }
[219]372 } else if([columnIdentifier isEqualToString:FSATableColumnIdentifier_always]){
[221]373 if([alwaysApps containsObject:[[cocoaApps objectAtIndex: rowIndex] name]])
374 return [NSNumber numberWithBool:YES];
375 else
376 return [NSNumber numberWithBool:NO];
377 }
[7]378 return nil;
379}
380
381@end
382
383@implementation FSAAppList (NSWindowDelegate)
384
385- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
386{
387 NSWindow *window = [tableView window];
388 NSRect frame = [window frame];
389 NSScrollView *scrollView = [tableView enclosingScrollView];
390 float displayedHeight = [[scrollView contentView] bounds].size.height;
391 float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
392 float heightExcess;
[221]393
[7]394 if (heightChange >= 0 && heightChange <= 1) {
395 // either the window is already optimal size, or it's too big
396 float rowHeight = [tableView cellHeight];
397 heightChange = (rowHeight * [tableView numberOfRows]) - displayedHeight;
398 }
[221]399
[7]400 frame.size.height += heightChange;
[221]401
[7]402 if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
403 (heightExcess = [window maxSize].height - frame.size.height) < 1) {
404 heightChange += heightExcess;
405 frame.size.height += heightExcess;
406 }
[221]407
[7]408 frame.origin.y -= heightChange;
[221]409
[7]410 return frame;
411}
412
413@end
414
415@implementation FSAAppList (NSApplicationDelegate)
416
417- (NSMenu *)applicationDockMenu:(NSApplication *)sender;
418{
419 static NSMenu *dockMenu = nil;
[397]420 NSMenuItem *menuItem;
[7]421 DeVercruesseProcess *frontApp = [processManager frontProcess];
422 NSString *appName = [frontApp name];
423 NSString *status = nil;
424 // XXX workaround for broken dock menu sender
425 NSMethodSignature *sig = [NSApp methodSignatureForSelector: @selector(installBundleInFrontmostApp:)];
426 NSInvocation *inv;
[221]427
[7]428 if (dockMenu != nil) {
429 // XXX release invocation
430 [[[dockMenu itemAtIndex: 0] target] release];
431 [dockMenu removeItemAtIndex: 0];
432 } else {
433 dockMenu = [[NSMenu alloc] init];
434 }
[221]435
[153]436 NSAssert(frontApp != nil && appName != nil, @"Can't obtain information on the frontmost application");
[221]437
[7]438 if ([patchedApps containsObject: frontApp]) {
[153]439 status = [NSString stringWithFormat: NSLocalizedString(@"Installed in '%@'", "Dock menu disabled item displayed when FSA already installed, app name parameter"), appName];
[7]440 } else if (![cocoaApps containsObject: frontApp]) {
[153]441 status = [NSString stringWithFormat: NSLocalizedString(@"Can't install because '%@' is not a Cocoa application", "Dock menu disabled item displayed when frontmost app not Cocoa, app name parameter"), appName];
[7]442 }
[221]443
[7]444 if (status == nil) {
[153]445 menuItem = [dockMenu addItemWithTitle: [NSString stringWithFormat: NSLocalizedString(@"Install in '%@'", "Dock menu item to install FSA in frontmost app"), appName]
[7]446 action: @selector(invoke)
447 keyEquivalent: @""];
448 inv = [NSInvocation invocationWithMethodSignature: sig];
449 [inv setSelector: @selector(installBundleInFrontmostApp:)];
450 [inv setTarget: NSApp];
451 [inv setArgument: &menuItem atIndex: 2];
452 [menuItem setTag: [frontApp pid]];
453 [menuItem setTarget: [inv retain]];
454 } else {
455 menuItem = [dockMenu addItemWithTitle: status action: nil keyEquivalent: @""];
456 [menuItem setEnabled: NO];
457 }
[221]458
[7]459 return dockMenu;
460}
461
[219]462- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
463{
[221]464 id anApp;
465 id e = [cocoaApps objectEnumerator];
466 while(anApp = [e nextObject]){
[222]467 if([alwaysApps containsObject:[anApp name]]){
[412]468 [(FSAApp *)NSApp installBundleInAppWithPID:[anApp pid]];
[219]469 }
[221]470 }
471 finishedLaunch = YES;
[219]472}
473
[7]474- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender;
475{
476 return YES;
477}
478
[219]479-(void)clickInTable:(id)sender
480{
[221]481 if([sender clickedColumn] == 2){
482 if([sender clickedRow] >= 0){
483 NSString *appName = [[cocoaApps objectAtIndex:[sender clickedRow]] name];
484 if(![alwaysApps containsObject:appName]){
[412]485 [(FSAApp *)NSApp installBundleInAppWithPID:[[cocoaApps objectAtIndex:[sender clickedRow]] pid]];
[221]486 [alwaysApps addObject:appName];
487 } else
488 [alwaysApps removeObject:appName];
489 [[NSUserDefaults standardUserDefaults] setObject:alwaysApps forKey:@"AlwaysApps"];
[219]490 }
[221]491 }
[219]492}
493
[16]494@end
[219]495
496static int sysctlbyname_with_pid (const char *name, pid_t pid,
[221]497 void *oldp, size_t *oldlenp,
498 void *newp, size_t newlen)
[219]499{
500 if (pid == 0) {
501 if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1) {
502 fprintf(stderr, "sysctlbyname_with_pid(0): sysctlbyname failed:"
[221]503 "%s\n", strerror(errno));
[219]504 return -1;
505 }
506 } else {
507 int mib[CTL_MAXNAME];
508 size_t len = CTL_MAXNAME;
509 if (sysctlnametomib(name, mib, &len) == -1) {
510 fprintf(stderr, "sysctlbyname_with_pid: sysctlnametomib failed:"
[221]511 "%s\n", strerror(errno));
[219]512 return -1;
513 }
514 mib[len] = pid;
515 len++;
516 if (sysctl(mib, len, oldp, oldlenp, newp, newlen) == -1) {
517 fprintf(stderr, "sysctlbyname_with_pid: sysctl failed:"
518 "%s\n", strerror(errno));
519 return -1;
520 }
521 }
522 return 0;
523}
524
525int is_pid_native (pid_t pid)
526{
527 int ret = 0;
528 size_t sz = sizeof(ret);
[221]529
[219]530 if (sysctlbyname_with_pid("sysctl.proc_native", pid,
[221]531 &ret, &sz, NULL, 0) == -1) {
532 if (errno == ENOENT) {
[219]533 // sysctl doesn't exist, which means that this version of Mac OS
534 // pre-dates Rosetta, so the application must be native.
535 return 1;
536 }
537 fprintf(stderr, "is_pid_native: sysctlbyname_with_pid failed:"
538 "%s\n", strerror(errno));
539 return -1;
540 }
541 return ret;
542}
Note: See TracBrowser for help on using the repository browser.