| 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (c) 2002 Ben Harris |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person |
| 7 | * obtaining a copy of this software and associated documentation |
| 8 | * files (the "Software"), to deal in the Software without |
| 9 | * restriction, including without limitation the rights to use, |
| 10 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | * sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following |
| 13 | * conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be |
| 16 | * included in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
| 22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 23 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * macdlg.c - settings dialogue box for Mac OS. |
| 30 | */ |
| 31 | |
| 32 | #include <MacTypes.h> |
| 33 | #include <AEDataModel.h> |
| 34 | #include <AppleEvents.h> |
| 35 | #include <Navigation.h> |
| 36 | #include <Resources.h> |
| 37 | #include <StandardFile.h> |
| 38 | #include <TextUtils.h> |
| 39 | #include <Windows.h> |
| 40 | |
| 41 | #include <assert.h> |
| 42 | #include <string.h> |
| 43 | |
| 44 | #include "putty.h" |
| 45 | #include "dialog.h" |
| 46 | #include "mac.h" |
| 47 | #include "macresid.h" |
| 48 | #include "storage.h" |
| 49 | |
| 50 | static void mac_config(int); |
| 51 | static void mac_closedlg(WindowPtr); |
| 52 | static void mac_enddlg_config(WindowPtr, int); |
| 53 | static void mac_enddlg_reconfig(WindowPtr, int); |
| 54 | |
| 55 | void mac_newsession(void) |
| 56 | { |
| 57 | mac_config(FALSE); |
| 58 | } |
| 59 | |
| 60 | void mac_reconfig(void) |
| 61 | { |
| 62 | mac_config(TRUE); |
| 63 | } |
| 64 | |
| 65 | static void mac_config(int midsession) |
| 66 | { |
| 67 | Session *s; |
| 68 | WinInfo *wi; |
| 69 | static struct sesslist sesslist; |
| 70 | Str255 mactitle; |
| 71 | char *str; |
| 72 | |
| 73 | if (midsession) { |
| 74 | s = mac_windowsession(FrontWindow()); |
| 75 | } else { |
| 76 | s = snew(Session); |
| 77 | memset(s, 0, sizeof(*s)); |
| 78 | do_defaults(NULL, &s->cfg); |
| 79 | s->hasfile = FALSE; |
| 80 | } |
| 81 | |
| 82 | /* Copy the configuration somewhere else in case this is a * |
| 83 | * reconfiguration and the user cancels the operation */ |
| 84 | |
| 85 | s->temp_cfg = s->cfg; |
| 86 | |
| 87 | if (HAVE_COLOR_QD()) |
| 88 | s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1); |
| 89 | else |
| 90 | s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1); |
| 91 | |
| 92 | get_sesslist(&sesslist, TRUE); |
| 93 | s->ctrlbox = ctrl_new_box(); |
| 94 | setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0); |
| 95 | |
| 96 | s->settings_ctrls.data = &s->temp_cfg; |
| 97 | if (midsession) |
| 98 | s->settings_ctrls.end = &mac_enddlg_reconfig; |
| 99 | else |
| 100 | s->settings_ctrls.end = &mac_enddlg_config; |
| 101 | |
| 102 | macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls); |
| 103 | |
| 104 | wi = snew(WinInfo); |
| 105 | memset(wi, 0, sizeof(*wi)); |
| 106 | wi->s = s; |
| 107 | wi->mcs = &s->settings_ctrls; |
| 108 | wi->wtype = wSettings; |
| 109 | wi->update = &macctrl_update; |
| 110 | wi->click = &macctrl_click; |
| 111 | wi->key = &macctrl_key; |
| 112 | wi->activate = &macctrl_activate; |
| 113 | wi->adjustmenus = &macctrl_adjustmenus; |
| 114 | wi->close = &mac_closedlg; |
| 115 | SetWRefCon(s->settings_window, (long)wi); |
| 116 | if (midsession) |
| 117 | str = dupprintf("%s Reconfiguration", appname); |
| 118 | else |
| 119 | str = dupprintf("%s Configuration", appname); |
| 120 | c2pstrcpy(mactitle, str); |
| 121 | sfree(str); |
| 122 | SetWTitle(s->settings_window, mactitle); |
| 123 | ShowWindow(s->settings_window); |
| 124 | } |
| 125 | |
| 126 | static void mac_closedlg(WindowPtr window) |
| 127 | { |
| 128 | Session *s = mac_windowsession(window); |
| 129 | |
| 130 | macctrl_close(window); |
| 131 | DisposeWindow(window); |
| 132 | if (s->window == NULL) |
| 133 | sfree(s); |
| 134 | } |
| 135 | |
| 136 | static void mac_enddlg_config(WindowPtr window, int value) |
| 137 | { |
| 138 | Session *s = mac_windowsession(window); |
| 139 | |
| 140 | if (value == 0) |
| 141 | mac_closedlg(window); |
| 142 | else { |
| 143 | s->cfg = s->temp_cfg; |
| 144 | mac_startsession(s); |
| 145 | mac_closedlg(window); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void mac_enddlg_reconfig(WindowPtr window, int value) |
| 150 | { |
| 151 | Session *s = mac_windowsession(window); |
| 152 | |
| 153 | if (value == 0) |
| 154 | mac_closedlg(window); |
| 155 | else { |
| 156 | Config prev_cfg = s->cfg; |
| 157 | s->cfg = s->temp_cfg; |
| 158 | mac_closedlg(window); |
| 159 | |
| 160 | /* Pass new config data to the logging module */ |
| 161 | log_reconfig(s->logctx, &s->cfg); |
| 162 | |
| 163 | /* |
| 164 | * Flush the line discipline's edit buffer in the |
| 165 | * case where local editing has just been disabled. |
| 166 | */ |
| 167 | if (s->ldisc) |
| 168 | ldisc_send(s->ldisc, NULL, 0, 0); |
| 169 | |
| 170 | /* Change the palette */ |
| 171 | palette_reset(s); |
| 172 | |
| 173 | /* Pass new config data to the terminal */ |
| 174 | term_reconfig(s->term, &s->cfg); |
| 175 | |
| 176 | /* Pass new config data to the back end */ |
| 177 | if (s->back) |
| 178 | s->back->reconfig(s->backhandle, &s->cfg); |
| 179 | |
| 180 | /* Screen size changed ? */ |
| 181 | if (s->cfg.height != prev_cfg.height || |
| 182 | s->cfg.width != prev_cfg.width || |
| 183 | s->cfg.savelines != prev_cfg.savelines) { |
| 184 | request_resize(s, s->cfg.width, s->cfg.height); |
| 185 | } |
| 186 | |
| 187 | /* Set the window title */ |
| 188 | if (s->cfg.wintitle[0]) |
| 189 | set_title(s, s->cfg.wintitle); |
| 190 | |
| 191 | /* Scroll bar */ |
| 192 | if (s->cfg.scrollbar != prev_cfg.scrollbar) |
| 193 | request_resize(s, s->cfg.width, s->cfg.height); |
| 194 | |
| 195 | /* TODO: zoom, font */ |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void mac_dupsession(void) |
| 200 | { |
| 201 | Session *s1 = mac_windowsession(FrontWindow()); |
| 202 | Session *s2; |
| 203 | |
| 204 | s2 = snew(Session); |
| 205 | memset(s2, 0, sizeof(*s2)); |
| 206 | s2->cfg = s1->cfg; |
| 207 | s2->hasfile = s1->hasfile; |
| 208 | s2->savefile = s1->savefile; |
| 209 | |
| 210 | mac_startsession(s2); |
| 211 | } |
| 212 | |
| 213 | static OSErr mac_opensessionfrom(FSSpec *fss) |
| 214 | { |
| 215 | FInfo fi; |
| 216 | Session *s; |
| 217 | void *sesshandle; |
| 218 | OSErr err; |
| 219 | |
| 220 | s = snew(Session); |
| 221 | memset(s, 0, sizeof(*s)); |
| 222 | |
| 223 | err = FSpGetFInfo(fss, &fi); |
| 224 | if (err != noErr) return err; |
| 225 | if (fi.fdFlags & kIsStationery) |
| 226 | s->hasfile = FALSE; |
| 227 | else { |
| 228 | s->hasfile = TRUE; |
| 229 | s->savefile = *fss; |
| 230 | } |
| 231 | |
| 232 | sesshandle = open_settings_r_fsp(fss); |
| 233 | if (sesshandle == NULL) { |
| 234 | /* XXX need a way to pass up an error number */ |
| 235 | err = -9999; |
| 236 | goto fail; |
| 237 | } |
| 238 | load_open_settings(sesshandle, TRUE, &s->cfg); |
| 239 | close_settings_r(sesshandle); |
| 240 | |
| 241 | mac_startsession(s); |
| 242 | return noErr; |
| 243 | |
| 244 | fail: |
| 245 | sfree(s); |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | static OSErr mac_openlist(AEDesc docs) |
| 250 | { |
| 251 | OSErr err; |
| 252 | long ndocs, i; |
| 253 | FSSpec fss; |
| 254 | AEKeyword keywd; |
| 255 | DescType type; |
| 256 | Size size; |
| 257 | |
| 258 | err = AECountItems(&docs, &ndocs); |
| 259 | if (err != noErr) return err; |
| 260 | |
| 261 | for (i = 0; i < ndocs; i++) { |
| 262 | err = AEGetNthPtr(&docs, i + 1, typeFSS, |
| 263 | &keywd, &type, &fss, sizeof(fss), &size); |
| 264 | if (err != noErr) return err;; |
| 265 | err = mac_opensessionfrom(&fss); |
| 266 | if (err != noErr) return err; |
| 267 | } |
| 268 | return noErr; |
| 269 | } |
| 270 | |
| 271 | void mac_opensession(void) |
| 272 | { |
| 273 | |
| 274 | if (mac_gestalts.navsvers > 0) { |
| 275 | NavReplyRecord navr; |
| 276 | NavDialogOptions navopts; |
| 277 | NavTypeListHandle navtypes; |
| 278 | AEDesc defaultloc = { 'null', NULL }; |
| 279 | AEDesc *navdefault = NULL; |
| 280 | short vol; |
| 281 | long dirid; |
| 282 | FSSpec fss; |
| 283 | |
| 284 | if (NavGetDefaultDialogOptions(&navopts) != noErr) return; |
| 285 | /* XXX should we create sessions dir? */ |
| 286 | if (get_session_dir(FALSE, &vol, &dirid) == noErr && |
| 287 | FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr && |
| 288 | AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr) |
| 289 | navdefault = &defaultloc; |
| 290 | /* Can't meaningfully preview a saved session yet */ |
| 291 | navopts.dialogOptionFlags &= ~kNavAllowPreviews; |
| 292 | navtypes = (NavTypeListHandle)GetResource('open', open_pTTY); |
| 293 | if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes, |
| 294 | NULL) == noErr && navr.validRecord) |
| 295 | mac_openlist(navr.selection); |
| 296 | NavDisposeReply(&navr); |
| 297 | if (navtypes != NULL) |
| 298 | ReleaseResource((Handle)navtypes); |
| 299 | } |
| 300 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
| 301 | else { |
| 302 | StandardFileReply sfr; |
| 303 | static const OSType sftypes[] = { 'Sess', 0, 0, 0 }; |
| 304 | |
| 305 | StandardGetFile(NULL, 1, sftypes, &sfr); |
| 306 | if (!sfr.sfGood) return; |
| 307 | |
| 308 | mac_opensessionfrom(&sfr.sfFile); |
| 309 | /* XXX handle error */ |
| 310 | } |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | void mac_savesession(void) |
| 315 | { |
| 316 | Session *s = mac_windowsession(FrontWindow()); |
| 317 | void *sesshandle; |
| 318 | |
| 319 | assert(s->hasfile); |
| 320 | sesshandle = open_settings_w_fsp(&s->savefile); |
| 321 | if (sesshandle == NULL) return; /* XXX report error */ |
| 322 | save_open_settings(sesshandle, TRUE, &s->cfg); |
| 323 | close_settings_w(sesshandle); |
| 324 | } |
| 325 | |
| 326 | void mac_savesessionas(void) |
| 327 | { |
| 328 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
| 329 | Session *s = mac_windowsession(FrontWindow()); |
| 330 | StandardFileReply sfr; |
| 331 | void *sesshandle; |
| 332 | |
| 333 | StandardPutFile("\pSave session as:", |
| 334 | s->hasfile ? s->savefile.name : "\puntitled", &sfr); |
| 335 | if (!sfr.sfGood) return; |
| 336 | |
| 337 | if (!sfr.sfReplacing) { |
| 338 | FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript); |
| 339 | if (ResError() != noErr) return; /* XXX report error */ |
| 340 | } |
| 341 | sesshandle = open_settings_w_fsp(&sfr.sfFile); |
| 342 | if (sesshandle == NULL) return; /* XXX report error */ |
| 343 | save_open_settings(sesshandle, TRUE, &s->cfg); |
| 344 | close_settings_w(sesshandle); |
| 345 | s->hasfile = TRUE; |
| 346 | s->savefile = sfr.sfFile; |
| 347 | #endif |
| 348 | } |
| 349 | |
| 350 | pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply, |
| 351 | long refcon) |
| 352 | { |
| 353 | DescType type; |
| 354 | Size size; |
| 355 | |
| 356 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 357 | &type, NULL, 0, &size) == noErr) |
| 358 | return errAEParamMissed; |
| 359 | |
| 360 | /* XXX we should do something here. */ |
| 361 | return noErr; |
| 362 | } |
| 363 | |
| 364 | pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply, |
| 365 | long refcon) |
| 366 | { |
| 367 | DescType type; |
| 368 | Size size; |
| 369 | AEDescList docs = { typeNull, NULL }; |
| 370 | OSErr err; |
| 371 | |
| 372 | err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs); |
| 373 | if (err != noErr) goto out; |
| 374 | |
| 375 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 376 | &type, NULL, 0, &size) == noErr) { |
| 377 | err = errAEParamMissed; |
| 378 | goto out; |
| 379 | } |
| 380 | |
| 381 | err = mac_openlist(docs); |
| 382 | |
| 383 | out: |
| 384 | AEDisposeDesc(&docs); |
| 385 | return err; |
| 386 | } |
| 387 | |
| 388 | pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply, |
| 389 | long refcon) |
| 390 | { |
| 391 | DescType type; |
| 392 | Size size; |
| 393 | |
| 394 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 395 | &type, NULL, 0, &size) == noErr) |
| 396 | return errAEParamMissed; |
| 397 | |
| 398 | /* We can't meaningfully do anything here. */ |
| 399 | return errAEEventNotHandled; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Local Variables: |
| 404 | * c-file-style: "simon" |
| 405 | * End: |
| 406 | */ |