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 const char platform_x11_best_transport[] = "unix";
25 char *platform_get_x_display(void) {
29 FontSpec platform_default_fontspec(const char *name)
36 Filename platform_default_filename(const char *name)
39 if (!strcmp(name, "LogFileName"))
40 strcpy(ret.path, "putty.log");
46 char *platform_default_s(const char *name)
51 int platform_default_i(const char *name, int def)
53 if (!strcmp(name, "CloseOnExit"))
54 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
58 char *x_get_default(const char *key)
60 return NULL; /* this is a stub */
63 static void commonfatalbox(char *p, va_list ap)
69 * We may have come here because we ran out of memory, in which
70 * case it's entirely likely that that further memory
71 * allocations will fail. So (a) we use vsnprintf to format the
72 * error message rather than the usual dupvprintf; and (b) we
73 * have a fallback way to get the message out via stderr if
74 * even creating an NSAlert fails.
76 vsnprintf(errorbuf, lenof(errorbuf), p, ap);
78 alert = [NSAlert alloc];
80 fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf);
82 alert = [[alert init] autorelease];
83 [alert addButtonWithTitle:@"Terminate"];
84 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
90 void fatalbox(char *p, ...)
94 commonfatalbox(p, ap);
98 void modalfatalbox(char *p, ...)
102 commonfatalbox(p, ap);
106 void cmdline_error(char *p, ...)
109 fprintf(stderr, "%s: ", appname);
111 vfprintf(stderr, p, ap);
120 void cleanup_exit(int code)
130 /* ----------------------------------------------------------------------
131 * Tiny extension to NSMenuItem which carries a payload of a `void
132 * *', allowing several menu items to invoke the same message but
133 * pass different data through it.
135 @interface DataMenuItem : NSMenuItem
139 - (void)setPayload:(void *)d;
140 - (void *)getPayload;
142 @implementation DataMenuItem
143 - (void)setPayload:(void *)d
153 /* ----------------------------------------------------------------------
154 * Utility routines for constructing OS X menus.
157 NSMenu *newmenu(const char *title)
159 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
160 initWithTitle:[NSString stringWithCString:title]]
164 NSMenu *newsubmenu(NSMenu *parent, const char *title)
169 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
170 initWithTitle:[NSString stringWithCString:title]
174 child = newmenu(title);
175 [item setEnabled:YES];
176 [item setSubmenu:child];
177 [parent addItem:item];
181 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
182 const char *key, id target, SEL action)
184 unsigned mask = NSCommandKeyMask;
186 if (key[strcspn(key, "-")]) {
187 while (*key && *key != '-') {
188 int c = tolower((unsigned char)*key);
190 mask |= NSShiftKeyMask;
191 } else if (c == 'o' || c == 'a') {
192 mask |= NSAlternateKeyMask;
200 item = [[item initWithTitle:[NSString stringWithCString:title]
202 keyEquivalent:[NSString stringWithCString:key]]
206 [item setKeyEquivalentModifierMask: mask];
208 [item setEnabled:YES];
209 [item setTarget:target];
210 [item setAction:action];
212 [parent addItem:item];
217 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
218 id target, SEL action)
220 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
221 parent, title, key, target, action);
224 /* ----------------------------------------------------------------------
225 * AppController: the object which receives the messages from all
226 * menu selections that aren't standard OS X functions.
228 @implementation AppController
237 - (void)newTerminal:(id)sender
242 do_defaults(NULL, &cfg);
244 cfg.protocol = -1; /* PROT_TERMINAL */
246 win = [[SessionWindow alloc] initWithConfig:cfg];
247 [win makeKeyAndOrderFront:self];
250 - (void)newSessionConfig:(id)sender
255 do_defaults(NULL, &cfg);
257 win = [[ConfigWindow alloc] initWithConfig:cfg];
258 [win makeKeyAndOrderFront:self];
261 - (void)newSessionWithConfig:(id)vdata
265 NSData *data = (NSData *)vdata;
267 assert([data length] == sizeof(cfg));
268 [data getBytes:&cfg];
270 win = [[SessionWindow alloc] initWithConfig:cfg];
271 [win makeKeyAndOrderFront:self];
274 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
276 NSMenu *menu = newmenu("Dock Menu");
278 * FIXME: Add some useful things to this, probably including
279 * the saved session list.
284 - (void)timerFired:(id)sender
288 assert(sender == timer);
290 /* `sender' is the timer itself, so its userInfo is an NSNumber. */
291 now = [(NSNumber *)[sender userInfo] longValue];
297 if (run_timers(now, &next))
298 [self setTimer:next];
301 - (void)setTimer:(long)next
303 long interval = next - GETTICKCOUNT();
307 interval = 1; /* just in case */
309 finterval = interval / (float)TICKSPERSEC;
315 timer = [NSTimer scheduledTimerWithTimeInterval:finterval
316 target:self selector:@selector(timerFired:)
317 userInfo:[NSNumber numberWithLong:next] repeats:NO];
322 void timer_change_notify(long next)
324 [controller setTimer:next];
327 /* ----------------------------------------------------------------------
328 * Annoyingly, it looks as if I have to actually subclass
329 * NSApplication if I want to catch NSApplicationDefined events. So
332 @interface MyApplication : NSApplication
336 @implementation MyApplication
337 - (void)sendEvent:(NSEvent *)ev
339 if ([ev type] == NSApplicationDefined)
340 osxsel_process_results();
342 [super sendEvent:ev];
346 /* ----------------------------------------------------------------------
347 * Main program. Constructs the menus and runs the application.
349 int main(int argc, char **argv)
351 NSAutoreleasePool *pool;
356 pool = [[NSAutoreleasePool alloc] init];
358 icon = [NSImage imageNamed:@"NSApplicationIcon"];
359 [MyApplication sharedApplication];
360 [NSApp setApplicationIconImage:icon];
362 controller = [[[AppController alloc] init] autorelease];
363 [NSApp setDelegate:controller];
365 [NSApp setMainMenu: newmenu("Main Menu")];
367 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
368 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
369 [menu addItem:[NSMenuItem separatorItem]];
370 item = newitem(menu, "Hide PuTTY", "h", NSApp, @selector(hide:));
371 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
372 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
373 [menu addItem:[NSMenuItem separatorItem]];
374 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
375 [NSApp setAppleMenu: menu];
377 menu = newsubmenu([NSApp mainMenu], "File");
378 item = newitem(menu, "New", "n", NULL, @selector(newSessionConfig:));
379 item = newitem(menu, "New Terminal", "t", NULL, @selector(newTerminal:));
380 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
382 menu = newsubmenu([NSApp mainMenu], "Window");
383 [NSApp setWindowsMenu: menu];
384 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));
386 // menu = newsubmenu([NSApp mainMenu], "Help");
387 // item = newitem(menu, "PuTTY Help", "?", NSApp, @selector(showHelp:));
390 * Start up the sub-thread doing select().
395 * Start up networking.
400 * FIXME: To make initial debugging more convenient I'm going
401 * to start by opening a session window unconditionally. This
402 * will probably change later on.
404 [controller newSessionConfig:nil];