-/* $Id: mac.c,v 1.44 2003/02/04 00:33:11 ben Exp $ */
+/* $Id$ */
/*
- * Copyright (c) 1999 Ben Harris
+ * Copyright (c) 1999, 2003 Ben Harris
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
#include <MacTypes.h>
#include <AEDataModel.h>
#include <AppleEvents.h>
+#include <Controls.h>
#include <Quickdraw.h>
#include <Fonts.h>
#include <MacWindows.h>
#include "macresid.h"
#include "putty.h"
#include "ssh.h"
+#include "terminal.h"
#include "mac.h"
-#if !TARGET_API_MAC_CARBON
-QDGlobals qd;
-#endif
-
Session *sesslist;
static int cold = 1;
static int borednow = FALSE;
struct mac_gestalts mac_gestalts;
+UInt32 sleeptime;
static void mac_startup(void);
static void mac_eventloop(void);
static void mac_contentclick(WindowPtr, EventRecord *);
static void mac_growwindow(WindowPtr, EventRecord *);
static void mac_activatewindow(WindowPtr, EventRecord *);
+static void mac_suspendresume(EventRecord *);
static void mac_activateabout(WindowPtr, EventRecord *);
static void mac_updatewindow(WindowPtr);
static void mac_updatelicence(WindowPtr);
static void mac_keypress(EventRecord *);
static int mac_windowtype(WindowPtr);
static void mac_menucommand(long);
-static void mac_openabout(void);
static void mac_openlicence(void);
static void mac_adjustcursor(RgnHandle);
static void mac_adjustmenus(void);
if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
&SetWindowContentColor == kUnresolvedCFragSymbolAddress)
mac_gestalts.windattr = 0;
+ /* Mac OS 8.5 Menu Manager? */
+ if (Gestalt(gestaltMenuMgrAttr, &mac_gestalts.menuattr) != noErr)
+ mac_gestalts.menuattr = 0;
#endif
/* Text Encoding Conversion Manager? */
if (
fatalbox("Unable to create menu bar.");
SetMenuBar(menuBar);
AppendResMenu(GetMenuHandle(mApple), 'DRVR');
+ if (mac_gestalts.menuattr & gestaltMenuMgrAquaLayoutMask) {
+ DeleteMenuItem(GetMenuHandle(mFile), iQuit);
+ /* Also delete the separator above the Quit item. */
+ DeleteMenuItem(GetMenuHandle(mFile), iQuit - 1);
+ }
mac_adjustmenus();
DrawMenuBar();
InitCursor();
RgnHandle cursrgn;
cursrgn = NewRgn();
+ sleeptime = 0;
for (;;) {
mac_adjustcursor(cursrgn);
- gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
+ gotevent = WaitNextEvent(everyEvent, &event, sleeptime, cursrgn);
+ /*
+ * XXX For now, limit sleep time to 1/10 s to work around
+ * wake-before-sleep race in MacTCP code.
+ */
+ sleeptime = 6;
mac_adjustcursor(cursrgn);
- if (gotevent)
+ if (gotevent) {
+ /* Ensure we get a null event when the real ones run out. */
+ sleeptime = 0;
mac_event(&event);
- if (borednow)
- cleanup_exit(0);
+ if (borednow)
+ cleanup_exit(0);
+ }
sk_poll();
- mac_pollterm();
+ if (!gotevent)
+ mac_pollterm();
+ if (mac_gestalts.apprvers >= 0x100 && mac_frontwindow() != NULL)
+ IdleControls(mac_frontwindow());
}
DisposeRgn(cursrgn);
}
}
break;
#endif
+ case osEvt:
+ switch ((event->message & osEvtMessageMask) >> 24) {
+ case suspendResumeMessage:
+ mac_suspendresume(event);
+ break;
+ }
+ break;
case kHighLevelEvent:
AEProcessAppleEvent(event); /* errors? */
break;
}
}
-static void mac_contentclick(WindowPtr window, EventRecord *event) {
- short item;
- DialogRef dialog;
+static void mac_contentclick(WindowPtr window, EventRecord *event)
+{
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_clickterm(window, event);
- break;
- case wAbout:
- dialog = GetDialogFromWindow(window);
- if (DialogSelect(event, &dialog, &item))
- switch (item) {
- case wiAboutLicence:
- mac_openlicence();
- break;
- }
- break;
- case wSettings:
- mac_clickdlg(window, event);
- break;
- }
+ if (mac_wininfo(window)->click != NULL)
+ (*mac_wininfo(window)->click)(window, event);
}
-static void mac_growwindow(WindowPtr window, EventRecord *event) {
+static void mac_growwindow(WindowPtr window, EventRecord *event)
+{
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_growterm(window, event);
- }
+ if (mac_wininfo(window)->grow != NULL)
+ (*mac_wininfo(window)->grow)(window, event);
}
-static void mac_activatewindow(WindowPtr window, EventRecord *event) {
- int active;
+static void mac_activatewindow(WindowPtr window, EventRecord *event)
+{
- active = (event->modifiers & activeFlag) != 0;
mac_adjustmenus();
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_activateterm(window, active);
- break;
- case wSettings:
- mac_activatedlg(window, event);
- break;
- case wAbout:
- mac_activateabout(window, event);
- break;
- }
-}
-
-static void mac_activateabout(WindowPtr window, EventRecord *event) {
- DialogRef dialog;
- DialogItemType itemtype;
- Handle itemhandle;
- short item;
- Rect itemrect;
- int active;
-
- dialog = GetDialogFromWindow(window);
- active = (event->modifiers & activeFlag) != 0;
- GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
- HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
- DialogSelect(event, &dialog, &item);
+ if (mac_wininfo(window)->activate != NULL)
+ (*mac_wininfo(window)->activate)(window, event);
}
static void mac_updatewindow(WindowPtr window)
{
-#if TARGET_API_MAC_CARBON
- RgnHandle rgn;
-#endif
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_updateterm(window);
- break;
- case wAbout:
- case wSettings:
- BeginUpdate(window);
-#if TARGET_API_MAC_CARBON
- rgn = NewRgn();
- GetPortVisibleRegion(GetWindowPort(window), rgn);
- UpdateDialog(GetDialogFromWindow(window), rgn);
- DisposeRgn(rgn);
-#else
- UpdateDialog(window, window->visRgn);
-#endif
- EndUpdate(window);
- break;
- case wLicence:
- mac_updatelicence(window);
- break;
- }
-}
-
-static void mac_updatelicence(WindowPtr window)
-{
- Handle h;
- int len;
- long fondsize;
- Rect textrect;
-
- SetPort((GrafPtr)GetWindowPort(window));
- BeginUpdate(window);
- fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
- TextFont(HiWord(fondsize));
- TextSize(LoWord(fondsize));
- h = Get1Resource('TEXT', wLicence);
- len = GetResourceSizeOnDisk(h);
-#if TARGET_API_MAC_CARBON
- GetPortBounds(GetWindowPort(window), &textrect);
-#else
- textrect = window->portRect;
-#endif
- if (h != NULL) {
- HLock(h);
- TETextBox(*h, len, &textrect, teFlushDefault);
- HUnlock(h);
- }
- EndUpdate(window);
+ if (mac_wininfo(window)->update != NULL)
+ (*mac_wininfo(window)->update)(window);
}
/*
* Work out what kind of window we're dealing with.
- * Concept shamelessly nicked from SurfWriter.
*/
-static int mac_windowtype(WindowPtr window) {
- int kind;
- long refcon;
-
- if (window == NULL)
- return wNone;
- kind = GetWindowKind(window);
- if (kind < 0)
+static int mac_windowtype(WindowPtr window)
+{
+
+#if !TARGET_API_MAC_CARBON
+ if (GetWindowKind(window) < 0)
return wDA;
- if (GetWVariant(window) == zoomDocProc)
- return wTerminal;
- refcon = GetWRefCon(window);
- if (refcon < 1024)
- return refcon;
- else
- return wSettings;
+#endif
+ return ((WinInfo *)GetWRefCon(window))->wtype;
}
/*
static void mac_keypress(EventRecord *event) {
WindowPtr window;
- window = FrontWindow();
+ window = mac_frontwindow();
/*
* Check for a command-key combination, but ignore it if it counts
* as a meta-key combination and we're in a terminal window.
mac_adjustmenus();
mac_menucommand(MenuKey(event->message & charCodeMask));
} else {
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_keyterm(window, event);
- break;
- }
+ if (window != NULL && mac_wininfo(window)->key != NULL)
+ (*mac_wininfo(window)->key)(window, event);
}
}
menu = HiWord(result);
item = LoWord(result);
- window = FrontWindow();
+ window = mac_frontwindow();
/* Things which do the same whatever window we're in. */
switch (menu) {
case mApple:
break;
}
/* If we get here, handling is up to window-specific code. */
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_menuterm(window, menu, item);
- break;
- }
+ if (window != NULL && mac_wininfo(window)->menu != NULL)
+ (*mac_wininfo(window)->menu)(window, menu, item);
+
done:
HiliteMenu(0);
}
-static void mac_openabout(void) {
- DialogItemType itemtype;
- Handle item;
- VersRecHndl vers;
- Rect box;
- StringPtr longvers;
-
- if (windows.about)
- SelectWindow(windows.about);
- else {
- windows.about =
- GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
- vers = (VersRecHndl)Get1Resource('vers', 1);
- if (vers != NULL && *vers != NULL) {
- longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
- GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
- &itemtype, &item, &box);
- assert(itemtype & kStaticTextDialogItem);
- SetDialogItemText(item, longvers);
- }
- ShowWindow(windows.about);
- }
-}
-
-static void mac_openlicence(void) {
-
- if (windows.licence)
- SelectWindow(windows.licence);
- else {
- windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
- ShowWindow(windows.licence);
- }
-}
-
static void mac_closewindow(WindowPtr window) {
switch (mac_windowtype(window)) {
#if !TARGET_API_MAC_CARBON
case wDA:
- CloseDeskAcc(((WindowPeek)window)->windowKind);
+ CloseDeskAcc(GetWindowKind(window));
break;
#endif
- case wTerminal:
- mac_closeterm(window);
- break;
- case wAbout:
- windows.about = NULL;
- DisposeDialog(GetDialogFromWindow(window));
- break;
- case wLicence:
- windows.licence = NULL;
- DisposeWindow(window);
+ default:
+ if (mac_wininfo(window)->close != NULL)
+ (*mac_wininfo(window)->close)(window);
break;
}
}
+static void mac_suspendresume(EventRecord *event)
+{
+ WindowPtr front;
+ EventRecord fakeevent;
+
+ /*
+ * We're called either before we're suspended or after we're
+ * resumed, so we're the front application at this point.
+ */
+ front = FrontWindow();
+ if (front != NULL) {
+ fakeevent.what = activateEvt;
+ fakeevent.message = (UInt32)front;
+ fakeevent.when = event->when;
+ fakeevent.where = event->where;
+ fakeevent.modifiers =
+ (event->message & resumeFlag) ? activeFlag : 0;
+ mac_activatewindow(front, &fakeevent);
+ }
+}
+
static void mac_zoomwindow(WindowPtr window, short part) {
/* FIXME: do something */
WindowPtr window;
MenuHandle menu;
- window = FrontWindow();
+ window = mac_frontwindow();
menu = GetMenuHandle(mApple);
EnableItem(menu, 0);
EnableItem(menu, iAbout);
DisableItem(menu, iClose);
EnableItem(menu, iQuit);
- switch (mac_windowtype(window)) {
- case wSettings:
- DisableItem(menu, iSave); /* XXX enable if modified */
- EnableItem(menu, iSaveAs);
- EnableItem(menu, iDuplicate);
- menu = GetMenuHandle(mEdit);
- DisableItem(menu, 0);
- break;
- case wTerminal:
- mac_adjusttermmenus(window);
- break;
- default:
+ if (window != NULL && mac_wininfo(window)->adjustmenus != NULL)
+ (*mac_wininfo(window)->adjustmenus)(window);
+ else {
DisableItem(menu, iSave);
DisableItem(menu, iSaveAs);
DisableItem(menu, iDuplicate);
menu = GetMenuHandle(mEdit);
DisableItem(menu, 0);
- break;
+ menu = GetMenuHandle(mWindow);
+ DisableItem(menu, 0); /* Until we get more than 1 item on it. */
}
DrawMenuBar();
}
#endif
}
} else {
- switch (mac_windowtype(window)) {
- case wTerminal:
- mac_adjusttermcursor(window, mouse, cursrgn);
- break;
- default:
+ if (mac_wininfo(window)->adjustcursor != NULL)
+ (*mac_wininfo(window)->adjustcursor)(window, mouse, cursrgn);
+ else {
#if TARGET_API_MAC_CARBON
GetQDGlobalsArrow(&arrow);
SetCursor(&arrow);
SetCursor(&qd.arrow);
CopyRgn(window->visRgn, cursrgn);
#endif
- break;
}
}
}
exit(status);
}
-void fatalbox(char *fmt, ...) {
- va_list ap;
- Str255 stuff;
-
- va_start(ap, fmt);
- /* We'd like stuff to be a Pascal string */
- stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
- va_end(ap);
- ParamText(stuff, NULL, NULL, NULL);
- StopAlert(128, NULL);
- cleanup_exit(1);
-}
-
-void modalfatalbox(char *fmt, ...) {
- va_list ap;
- Str255 stuff;
-
- va_start(ap, fmt);
- /* We'd like stuff to be a Pascal string */
- stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
- va_end(ap);
- ParamText(stuff, NULL, NULL, NULL);
- StopAlert(128, NULL);
- cleanup_exit(1);
-}
-
/* This should only kill the current session, not the whole application. */
void connection_fatal(void *fontend, char *fmt, ...) {
va_list ap;
return FALSE;
}
-void agent_query(void *in, int inlen, void **out, int *outlen)
+int agent_query(void *in, int inlen, void **out, int *outlen,
+ void (*callback)(void *, void *, int), void *callback_ctx)
{
*out = NULL;
*outlen = 0;
+ return 1;
}
/* Temporary null routines for testing. */
void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
char *keystr, char *fingerprint)
{
+ Str255 stuff;
+ Session *s = frontend;
+ /*
+ * This function is horribly wrong. For one thing, the alert
+ * shouldn't be modal, it should be movable modal, or a sheet in
+ * Aqua. Also, PuTTY might be in the background, in which case we
+ * should use the Notification Manager to wake up the user. In
+ * any case, we shouldn't hold up processing of other connections'
+ * data just because this one's waiting for the user. It should
+ * also handle a host key cache, of course, and see the note below
+ * about closing the connection. All in all, a bit of a mess
+ * really.
+ */
+
+ stuff[0] = sprintf((char *)(&stuff[1]),
+ "The server's key fingerprint is: %s\n"
+ "Continue connecting?", fingerprint);
+ ParamText(stuff, NULL, NULL, NULL);
+ if (CautionAlert(wQuestion, NULL) == 2) {
+ /*
+ * User chose "Cancel". Unfortunately, if I tear the
+ * connection down here, Bad Things happen when I return. I
+ * think this function should actually return something
+ * telling the SSH code to abandon the connection.
+ */
+ }
}
-void askcipher(void *frontend, char *ciphername, int cs)
+void askalg(void *frontend, const char *algtype, const char *algname)
{
}
/* SGT: I have no idea whether Mac X servers need anything here. */
}
-Filename filename_from_str(const char *str)
-{
- Filename ret;
- Str255 tmp;
-
- /* XXX This fails for filenames over 255 characters long. */
- c2pstrcpy(tmp, str);
- FSMakeFSSpec(0, 0, tmp, &ret.fss);
- return ret;
-}
-
-/*
- * Convert a filename to a string for display purposes.
- * See pp 2-44--2-46 of IM:Files
- *
- * XXX static storage considered harmful
- */
-const char *filename_to_str(const Filename *fn)
-{
- CInfoPBRec pb;
- Str255 dirname;
- OSErr err;
- static char *path = NULL;
- char *newpath;
-
- if (path != NULL) sfree(path);
- path = smalloc(fn->fss.name[0]);
- p2cstrcpy(path, fn->fss.name);
- pb.dirInfo.ioNamePtr = dirname;
- pb.dirInfo.ioVRefNum = fn->fss.vRefNum;
- pb.dirInfo.ioDrParID = fn->fss.parID;
- pb.dirInfo.ioFDirIndex = -1;
- do {
- pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
- err = PBGetCatInfoSync(&pb);
-
- /* XXX Assume not A/UX */
- newpath = smalloc(strlen(path) + dirname[0] + 2);
- p2cstrcpy(newpath, dirname);
- strcat(newpath, ":");
- strcat(newpath, path);
- sfree(path);
- path = newpath;
- } while (pb.dirInfo.ioDrDirID != fsRtDirID);
- return path;
-}
-
-int filename_equal(Filename f1, Filename f2)
+void update_specials_menu(void *frontend)
{
+ Session *s = frontend;
+ WindowPtr front;
- return f1.fss.vRefNum == f2.fss.vRefNum &&
- f1.fss.parID == f2.fss.parID &&
- f1.fss.name[0] == f2.fss.name[0] &&
- memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0;
-}
-
-int filename_is_null(Filename fn)
-{
-
- return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0;
-}
-
-FILE *f_open(Filename fn, char const *mode)
-{
- short savevol;
- long savedir;
- char tmp[256];
- FILE *ret;
-
- HGetVol(NULL, &savevol, &savedir);
- if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) {
- p2cstrcpy(tmp, fn.fss.name);
- ret = fopen(tmp, mode);
- } else
- ret = NULL;
- HSetVol(NULL, savevol, savedir);
- return ret;
+ front = mac_frontwindow();
+ if (front != NULL && mac_windowsession(front) == s)
+ mac_adjustmenus();
}
/*