2 * osxmain.m: main-program file of Mac OS X PuTTY.
5 #import <Cocoa/Cocoa.h>
7 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
12 /* ----------------------------------------------------------------------
16 AppController *controller;
18 /* ----------------------------------------------------------------------
19 * Miscellaneous elements of the interface to the cross-platform
20 * and Unix PuTTY code.
23 char *platform_get_x_display(void) {
27 FontSpec platform_default_fontspec(const char *name)
34 Filename platform_default_filename(const char *name)
37 if (!strcmp(name, "LogFileName"))
38 strcpy(ret.path, "putty.log");
44 char *platform_default_s(const char *name)
49 int platform_default_i(const char *name, int def)
51 if (!strcmp(name, "CloseOnExit"))
52 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
56 char *x_get_default(const char *key)
58 return NULL; /* this is a stub */
61 static void commonfatalbox(const char *p, va_list ap)
67 * We may have come here because we ran out of memory, in which
68 * case it's entirely likely that that further memory
69 * allocations will fail. So (a) we use vsnprintf to format the
70 * error message rather than the usual dupvprintf; and (b) we
71 * have a fallback way to get the message out via stderr if
72 * even creating an NSAlert fails.
74 vsnprintf(errorbuf, lenof(errorbuf), p, ap);
76 alert = [NSAlert alloc];
78 fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf);
80 alert = [[alert init] autorelease];
81 [alert addButtonWithTitle:@"Terminate"];
82 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
88 void nonfatal(void *frontend, const char *p, ...)
95 errorbuf = dupvprintf(p, ap);
98 alert = [[[NSAlert alloc] init] autorelease];
99 [alert addButtonWithTitle:@"Error"];
100 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
106 void fatalbox(const char *p, ...)
110 commonfatalbox(p, ap);
114 void modalfatalbox(const char *p, ...)
118 commonfatalbox(p, ap);
122 void cmdline_error(const char *p, ...)
125 fprintf(stderr, "%s: ", appname);
127 vfprintf(stderr, p, ap);
136 void cleanup_exit(int code)
146 /* ----------------------------------------------------------------------
147 * Tiny extension to NSMenuItem which carries a payload of a `void
148 * *', allowing several menu items to invoke the same message but
149 * pass different data through it.
151 @interface DataMenuItem : NSMenuItem
155 - (void)setPayload:(void *)d;
156 - (void *)getPayload;
158 @implementation DataMenuItem
159 - (void)setPayload:(void *)d
169 /* ----------------------------------------------------------------------
170 * Utility routines for constructing OS X menus.
173 NSMenu *newmenu(const char *title)
175 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
176 initWithTitle:[NSString stringWithCString:title]]
180 NSMenu *newsubmenu(NSMenu *parent, const char *title)
185 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
186 initWithTitle:[NSString stringWithCString:title]
190 child = newmenu(title);
191 [item setEnabled:YES];
192 [item setSubmenu:child];
193 [parent addItem:item];
197 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
198 const char *key, id target, SEL action)
200 unsigned mask = NSCommandKeyMask;
202 if (key[strcspn(key, "-")]) {
203 while (*key && *key != '-') {
204 int c = tolower((unsigned char)*key);
206 mask |= NSShiftKeyMask;
207 } else if (c == 'o' || c == 'a') {
208 mask |= NSAlternateKeyMask;
216 item = [[item initWithTitle:[NSString stringWithCString:title]
218 keyEquivalent:[NSString stringWithCString:key]]
222 [item setKeyEquivalentModifierMask: mask];
224 [item setEnabled:YES];
225 [item setTarget:target];
226 [item setAction:action];
228 [parent addItem:item];
233 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
234 id target, SEL action)
236 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
237 parent, title, key, target, action);
240 /* ----------------------------------------------------------------------
241 * AppController: the object which receives the messages from all
242 * menu selections that aren't standard OS X functions.
244 @implementation AppController
253 - (void)newTerminal:(id)sender
258 do_defaults(NULL, &cfg);
260 cfg.protocol = -1; /* PROT_TERMINAL */
262 win = [[SessionWindow alloc] initWithConfig:cfg];
263 [win makeKeyAndOrderFront:self];
266 - (void)newSessionConfig:(id)sender
271 do_defaults(NULL, &cfg);
273 win = [[ConfigWindow alloc] initWithConfig:cfg];
274 [win makeKeyAndOrderFront:self];
277 - (void)newSessionWithConfig:(id)vdata
281 NSData *data = (NSData *)vdata;
283 assert([data length] == sizeof(cfg));
284 [data getBytes:&cfg];
286 win = [[SessionWindow alloc] initWithConfig:cfg];
287 [win makeKeyAndOrderFront:self];
290 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
292 NSMenu *menu = newmenu("Dock Menu");
294 * FIXME: Add some useful things to this, probably including
295 * the saved session list.
300 - (void)timerFired:(id)sender
304 assert(sender == timer);
306 /* `sender' is the timer itself, so its userInfo is an NSNumber. */
307 now = [(NSNumber *)[sender userInfo] longValue];
313 if (run_timers(now, &next))
314 [self setTimer:next];
317 - (void)setTimer:(long)next
319 long interval = next - GETTICKCOUNT();
323 interval = 1; /* just in case */
325 finterval = interval / (float)TICKSPERSEC;
331 timer = [NSTimer scheduledTimerWithTimeInterval:finterval
332 target:self selector:@selector(timerFired:)
333 userInfo:[NSNumber numberWithLong:next] repeats:NO];
338 void timer_change_notify(long next)
340 [controller setTimer:next];
343 /* ----------------------------------------------------------------------
344 * Annoyingly, it looks as if I have to actually subclass
345 * NSApplication if I want to catch NSApplicationDefined events. So
348 @interface MyApplication : NSApplication
352 @implementation MyApplication
353 - (void)sendEvent:(NSEvent *)ev
355 if ([ev type] == NSApplicationDefined)
356 osxsel_process_results();
358 [super sendEvent:ev];
362 /* ----------------------------------------------------------------------
363 * Main program. Constructs the menus and runs the application.
365 int main(int argc, char **argv)
367 NSAutoreleasePool *pool;
372 pool = [[NSAutoreleasePool alloc] init];
374 icon = [NSImage imageNamed:@"NSApplicationIcon"];
375 [MyApplication sharedApplication];
376 [NSApp setApplicationIconImage:icon];
378 controller = [[[AppController alloc] init] autorelease];
379 [NSApp setDelegate:controller];
381 [NSApp setMainMenu: newmenu("Main Menu")];
383 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
384 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
385 [menu addItem:[NSMenuItem separatorItem]];
386 item = newitem(menu, "Hide PuTTY", "h", NSApp, @selector(hide:));
387 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
388 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
389 [menu addItem:[NSMenuItem separatorItem]];
390 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
391 [NSApp setAppleMenu: menu];
393 menu = newsubmenu([NSApp mainMenu], "File");
394 item = newitem(menu, "New", "n", NULL, @selector(newSessionConfig:));
395 item = newitem(menu, "New Terminal", "t", NULL, @selector(newTerminal:));
396 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
398 menu = newsubmenu([NSApp mainMenu], "Window");
399 [NSApp setWindowsMenu: menu];
400 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));
402 // menu = newsubmenu([NSApp mainMenu], "Help");
403 // item = newitem(menu, "PuTTY Help", "?", NSApp, @selector(showHelp:));
406 * Start up the sub-thread doing select().
411 * Start up networking.
416 * FIXME: To make initial debugging more convenient I'm going
417 * to start by opening a session window unconditionally. This
418 * will probably change later on.
420 [controller newSessionConfig:nil];