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(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 fatalbox(char *p, ...)
92 commonfatalbox(p, ap);
96 void modalfatalbox(char *p, ...)
100 commonfatalbox(p, ap);
104 void cmdline_error(char *p, ...)
107 fprintf(stderr, "%s: ", appname);
109 vfprintf(stderr, p, ap);
118 void cleanup_exit(int code)
128 /* ----------------------------------------------------------------------
129 * Tiny extension to NSMenuItem which carries a payload of a `void
130 * *', allowing several menu items to invoke the same message but
131 * pass different data through it.
133 @interface DataMenuItem : NSMenuItem
137 - (void)setPayload:(void *)d;
138 - (void *)getPayload;
140 @implementation DataMenuItem
141 - (void)setPayload:(void *)d
151 /* ----------------------------------------------------------------------
152 * Utility routines for constructing OS X menus.
155 NSMenu *newmenu(const char *title)
157 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
158 initWithTitle:[NSString stringWithCString:title]]
162 NSMenu *newsubmenu(NSMenu *parent, const char *title)
167 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
168 initWithTitle:[NSString stringWithCString:title]
172 child = newmenu(title);
173 [item setEnabled:YES];
174 [item setSubmenu:child];
175 [parent addItem:item];
179 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
180 const char *key, id target, SEL action)
182 unsigned mask = NSCommandKeyMask;
184 if (key[strcspn(key, "-")]) {
185 while (*key && *key != '-') {
186 int c = tolower((unsigned char)*key);
188 mask |= NSShiftKeyMask;
189 } else if (c == 'o' || c == 'a') {
190 mask |= NSAlternateKeyMask;
198 item = [[item initWithTitle:[NSString stringWithCString:title]
200 keyEquivalent:[NSString stringWithCString:key]]
204 [item setKeyEquivalentModifierMask: mask];
206 [item setEnabled:YES];
207 [item setTarget:target];
208 [item setAction:action];
210 [parent addItem:item];
215 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
216 id target, SEL action)
218 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
219 parent, title, key, target, action);
222 /* ----------------------------------------------------------------------
223 * AppController: the object which receives the messages from all
224 * menu selections that aren't standard OS X functions.
226 @implementation AppController
235 - (void)newTerminal:(id)sender
240 do_defaults(NULL, &cfg);
242 cfg.protocol = -1; /* PROT_TERMINAL */
244 win = [[SessionWindow alloc] initWithConfig:cfg];
245 [win makeKeyAndOrderFront:self];
248 - (void)newSessionConfig:(id)sender
253 do_defaults(NULL, &cfg);
255 win = [[ConfigWindow alloc] initWithConfig:cfg];
256 [win makeKeyAndOrderFront:self];
259 - (void)newSessionWithConfig:(id)vdata
263 NSData *data = (NSData *)vdata;
265 assert([data length] == sizeof(cfg));
266 [data getBytes:&cfg];
268 win = [[SessionWindow alloc] initWithConfig:cfg];
269 [win makeKeyAndOrderFront:self];
272 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
274 NSMenu *menu = newmenu("Dock Menu");
276 * FIXME: Add some useful things to this, probably including
277 * the saved session list.
282 - (void)timerFired:(id)sender
286 assert(sender == timer);
288 /* `sender' is the timer itself, so its userInfo is an NSNumber. */
289 now = [(NSNumber *)[sender userInfo] longValue];
295 if (run_timers(now, &next))
296 [self setTimer:next];
299 - (void)setTimer:(long)next
301 long interval = next - GETTICKCOUNT();
305 interval = 1; /* just in case */
307 finterval = interval / (float)TICKSPERSEC;
313 timer = [NSTimer scheduledTimerWithTimeInterval:finterval
314 target:self selector:@selector(timerFired:)
315 userInfo:[NSNumber numberWithLong:next] repeats:NO];
320 void timer_change_notify(long next)
322 [controller setTimer:next];
325 /* ----------------------------------------------------------------------
326 * Annoyingly, it looks as if I have to actually subclass
327 * NSApplication if I want to catch NSApplicationDefined events. So
330 @interface MyApplication : NSApplication
334 @implementation MyApplication
335 - (void)sendEvent:(NSEvent *)ev
337 if ([ev type] == NSApplicationDefined)
338 osxsel_process_results();
340 [super sendEvent:ev];
344 /* ----------------------------------------------------------------------
345 * Main program. Constructs the menus and runs the application.
347 int main(int argc, char **argv)
349 NSAutoreleasePool *pool;
354 pool = [[NSAutoreleasePool alloc] init];
356 icon = [NSImage imageNamed:@"NSApplicationIcon"];
357 [MyApplication sharedApplication];
358 [NSApp setApplicationIconImage:icon];
360 controller = [[[AppController alloc] init] autorelease];
361 [NSApp setDelegate:controller];
363 [NSApp setMainMenu: newmenu("Main Menu")];
365 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
366 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
367 [menu addItem:[NSMenuItem separatorItem]];
368 item = newitem(menu, "Hide PuTTY", "h", NSApp, @selector(hide:));
369 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
370 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
371 [menu addItem:[NSMenuItem separatorItem]];
372 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
373 [NSApp setAppleMenu: menu];
375 menu = newsubmenu([NSApp mainMenu], "File");
376 item = newitem(menu, "New", "n", NULL, @selector(newSessionConfig:));
377 item = newitem(menu, "New Terminal", "t", NULL, @selector(newTerminal:));
378 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
380 menu = newsubmenu([NSApp mainMenu], "Window");
381 [NSApp setWindowsMenu: menu];
382 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));
384 // menu = newsubmenu([NSApp mainMenu], "Help");
385 // item = newitem(menu, "PuTTY Help", "?", NSApp, @selector(showHelp:));
388 * Start up the sub-thread doing select().
393 * Start up networking.
398 * FIXME: To make initial debugging more convenient I'm going
399 * to start by opening a session window unconditionally. This
400 * will probably change later on.
402 [controller newSessionConfig:nil];