| 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 | Str255 mactitle; |
| 70 | char *str; |
| 71 | |
| 72 | if (midsession) { |
| 73 | s = mac_windowsession(FrontWindow()); |
| 74 | } else { |
| 75 | s = snew(Session); |
| 76 | memset(s, 0, sizeof(*s)); |
| 77 | do_defaults(NULL, &s->cfg); |
| 78 | s->hasfile = FALSE; |
| 79 | s->session_closed = 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 | s->ctrlbox = ctrl_new_box(); |
| 93 | setup_config_box(s->ctrlbox, midsession, 0, 0); |
| 94 | |
| 95 | s->settings_ctrls.data = &s->temp_cfg; |
| 96 | if (midsession) |
| 97 | s->settings_ctrls.end = &mac_enddlg_reconfig; |
| 98 | else |
| 99 | s->settings_ctrls.end = &mac_enddlg_config; |
| 100 | |
| 101 | macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls); |
| 102 | |
| 103 | wi = snew(WinInfo); |
| 104 | memset(wi, 0, sizeof(*wi)); |
| 105 | wi->s = s; |
| 106 | wi->mcs = &s->settings_ctrls; |
| 107 | wi->wtype = wSettings; |
| 108 | wi->update = &macctrl_update; |
| 109 | wi->click = &macctrl_click; |
| 110 | wi->key = &macctrl_key; |
| 111 | wi->activate = &macctrl_activate; |
| 112 | wi->adjustmenus = &macctrl_adjustmenus; |
| 113 | wi->close = &mac_closedlg; |
| 114 | SetWRefCon(s->settings_window, (long)wi); |
| 115 | if (midsession) |
| 116 | str = dupprintf("%s Reconfiguration", appname); |
| 117 | else |
| 118 | str = dupprintf("%s Configuration", appname); |
| 119 | c2pstrcpy(mactitle, str); |
| 120 | sfree(str); |
| 121 | SetWTitle(s->settings_window, mactitle); |
| 122 | ShowWindow(s->settings_window); |
| 123 | } |
| 124 | |
| 125 | static void mac_closedlg(WindowPtr window) |
| 126 | { |
| 127 | Session *s = mac_windowsession(window); |
| 128 | |
| 129 | macctrl_close(window); |
| 130 | DisposeWindow(window); |
| 131 | if (s->window == NULL) |
| 132 | sfree(s); |
| 133 | } |
| 134 | |
| 135 | static void mac_enddlg_config(WindowPtr window, int value) |
| 136 | { |
| 137 | Session *s = mac_windowsession(window); |
| 138 | |
| 139 | if (value == 0) |
| 140 | mac_closedlg(window); |
| 141 | else { |
| 142 | s->cfg = s->temp_cfg; |
| 143 | mac_startsession(s); |
| 144 | mac_closedlg(window); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void mac_enddlg_reconfig(WindowPtr window, int value) |
| 149 | { |
| 150 | Session *s = mac_windowsession(window); |
| 151 | |
| 152 | if (value == 0) |
| 153 | mac_closedlg(window); |
| 154 | else { |
| 155 | Config prev_cfg = s->cfg; |
| 156 | s->cfg = s->temp_cfg; |
| 157 | mac_closedlg(window); |
| 158 | |
| 159 | /* Pass new config data to the logging module */ |
| 160 | log_reconfig(s->logctx, &s->cfg); |
| 161 | |
| 162 | /* |
| 163 | * Flush the line discipline's edit buffer in the |
| 164 | * case where local editing has just been disabled. |
| 165 | */ |
| 166 | if (s->ldisc) |
| 167 | ldisc_send(s->ldisc, NULL, 0, 0); |
| 168 | |
| 169 | /* Change the palette */ |
| 170 | palette_reset(s); |
| 171 | |
| 172 | /* Reinitialise line codepage */ |
| 173 | init_ucs(s); |
| 174 | |
| 175 | /* Pass new config data to the terminal */ |
| 176 | term_reconfig(s->term, &s->cfg); |
| 177 | |
| 178 | /* Pass new config data to the back end */ |
| 179 | if (s->back) |
| 180 | s->back->reconfig(s->backhandle, &s->cfg); |
| 181 | |
| 182 | /* Screen size changed ? */ |
| 183 | if (s->cfg.height != prev_cfg.height || |
| 184 | s->cfg.width != prev_cfg.width || |
| 185 | s->cfg.savelines != prev_cfg.savelines) { |
| 186 | request_resize(s, s->cfg.width, s->cfg.height); |
| 187 | } |
| 188 | |
| 189 | /* Set the window title */ |
| 190 | if (s->cfg.wintitle[0]) |
| 191 | set_title(s, s->cfg.wintitle); |
| 192 | |
| 193 | /* Scroll bar */ |
| 194 | if (s->cfg.scrollbar != prev_cfg.scrollbar) |
| 195 | request_resize(s, s->cfg.width, s->cfg.height); |
| 196 | |
| 197 | /* TODO: zoom, font */ |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void mac_dupsession(void) |
| 202 | { |
| 203 | Session *s1 = mac_windowsession(FrontWindow()); |
| 204 | Session *s2; |
| 205 | |
| 206 | s2 = snew(Session); |
| 207 | memset(s2, 0, sizeof(*s2)); |
| 208 | s2->cfg = s1->cfg; |
| 209 | s2->hasfile = s1->hasfile; |
| 210 | s2->savefile = s1->savefile; |
| 211 | |
| 212 | mac_startsession(s2); |
| 213 | } |
| 214 | |
| 215 | static OSErr mac_opensessionfrom(FSSpec *fss) |
| 216 | { |
| 217 | FInfo fi; |
| 218 | Session *s; |
| 219 | void *sesshandle; |
| 220 | OSErr err; |
| 221 | |
| 222 | s = snew(Session); |
| 223 | memset(s, 0, sizeof(*s)); |
| 224 | |
| 225 | err = FSpGetFInfo(fss, &fi); |
| 226 | if (err != noErr) return err; |
| 227 | if (fi.fdFlags & kIsStationery) |
| 228 | s->hasfile = FALSE; |
| 229 | else { |
| 230 | s->hasfile = TRUE; |
| 231 | s->savefile = *fss; |
| 232 | } |
| 233 | |
| 234 | sesshandle = open_settings_r_fsp(fss); |
| 235 | if (sesshandle == NULL) { |
| 236 | /* XXX need a way to pass up an error number */ |
| 237 | err = -9999; |
| 238 | goto fail; |
| 239 | } |
| 240 | load_open_settings(sesshandle, TRUE, &s->cfg); |
| 241 | close_settings_r(sesshandle); |
| 242 | |
| 243 | mac_startsession(s); |
| 244 | return noErr; |
| 245 | |
| 246 | fail: |
| 247 | sfree(s); |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static OSErr mac_openlist(AEDesc docs) |
| 252 | { |
| 253 | OSErr err; |
| 254 | long ndocs, i; |
| 255 | FSSpec fss; |
| 256 | AEKeyword keywd; |
| 257 | DescType type; |
| 258 | Size size; |
| 259 | |
| 260 | err = AECountItems(&docs, &ndocs); |
| 261 | if (err != noErr) return err; |
| 262 | |
| 263 | for (i = 0; i < ndocs; i++) { |
| 264 | err = AEGetNthPtr(&docs, i + 1, typeFSS, |
| 265 | &keywd, &type, &fss, sizeof(fss), &size); |
| 266 | if (err != noErr) return err;; |
| 267 | err = mac_opensessionfrom(&fss); |
| 268 | if (err != noErr) return err; |
| 269 | } |
| 270 | return noErr; |
| 271 | } |
| 272 | |
| 273 | void mac_opensession(void) |
| 274 | { |
| 275 | |
| 276 | if (mac_gestalts.navsvers > 0) { |
| 277 | NavReplyRecord navr; |
| 278 | NavDialogOptions navopts; |
| 279 | NavTypeListHandle navtypes; |
| 280 | AEDesc defaultloc = { 'null', NULL }; |
| 281 | AEDesc *navdefault = NULL; |
| 282 | short vol; |
| 283 | long dirid; |
| 284 | FSSpec fss; |
| 285 | |
| 286 | if (NavGetDefaultDialogOptions(&navopts) != noErr) return; |
| 287 | /* XXX should we create sessions dir? */ |
| 288 | if (get_session_dir(FALSE, &vol, &dirid) == noErr && |
| 289 | FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr && |
| 290 | AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr) |
| 291 | navdefault = &defaultloc; |
| 292 | /* Can't meaningfully preview a saved session yet */ |
| 293 | navopts.dialogOptionFlags &= ~kNavAllowPreviews; |
| 294 | navtypes = (NavTypeListHandle)GetResource('open', open_pTTY); |
| 295 | if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes, |
| 296 | NULL) == noErr && navr.validRecord) |
| 297 | mac_openlist(navr.selection); |
| 298 | NavDisposeReply(&navr); |
| 299 | if (navtypes != NULL) |
| 300 | ReleaseResource((Handle)navtypes); |
| 301 | } |
| 302 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
| 303 | else { |
| 304 | StandardFileReply sfr; |
| 305 | static const OSType sftypes[] = { 'Sess', 0, 0, 0 }; |
| 306 | |
| 307 | StandardGetFile(NULL, 1, sftypes, &sfr); |
| 308 | if (!sfr.sfGood) return; |
| 309 | |
| 310 | mac_opensessionfrom(&sfr.sfFile); |
| 311 | /* XXX handle error */ |
| 312 | } |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | void mac_savesession(void) |
| 317 | { |
| 318 | Session *s = mac_windowsession(FrontWindow()); |
| 319 | void *sesshandle; |
| 320 | |
| 321 | assert(s->hasfile); |
| 322 | sesshandle = open_settings_w_fsp(&s->savefile); |
| 323 | if (sesshandle == NULL) return; /* XXX report error */ |
| 324 | save_open_settings(sesshandle, TRUE, &s->cfg); |
| 325 | close_settings_w(sesshandle); |
| 326 | } |
| 327 | |
| 328 | void mac_savesessionas(void) |
| 329 | { |
| 330 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
| 331 | Session *s = mac_windowsession(FrontWindow()); |
| 332 | StandardFileReply sfr; |
| 333 | void *sesshandle; |
| 334 | |
| 335 | StandardPutFile("\pSave session as:", |
| 336 | s->hasfile ? s->savefile.name : "\puntitled", &sfr); |
| 337 | if (!sfr.sfGood) return; |
| 338 | |
| 339 | if (!sfr.sfReplacing) { |
| 340 | FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript); |
| 341 | if (ResError() != noErr) return; /* XXX report error */ |
| 342 | } |
| 343 | sesshandle = open_settings_w_fsp(&sfr.sfFile); |
| 344 | if (sesshandle == NULL) return; /* XXX report error */ |
| 345 | save_open_settings(sesshandle, TRUE, &s->cfg); |
| 346 | close_settings_w(sesshandle); |
| 347 | s->hasfile = TRUE; |
| 348 | s->savefile = sfr.sfFile; |
| 349 | #endif |
| 350 | } |
| 351 | |
| 352 | pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply, |
| 353 | long refcon) |
| 354 | { |
| 355 | DescType type; |
| 356 | Size size; |
| 357 | |
| 358 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 359 | &type, NULL, 0, &size) == noErr) |
| 360 | return errAEParamMissed; |
| 361 | |
| 362 | /* XXX we should do something here. */ |
| 363 | return noErr; |
| 364 | } |
| 365 | |
| 366 | pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply, |
| 367 | long refcon) |
| 368 | { |
| 369 | DescType type; |
| 370 | Size size; |
| 371 | AEDescList docs = { typeNull, NULL }; |
| 372 | OSErr err; |
| 373 | |
| 374 | err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs); |
| 375 | if (err != noErr) goto out; |
| 376 | |
| 377 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 378 | &type, NULL, 0, &size) == noErr) { |
| 379 | err = errAEParamMissed; |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | err = mac_openlist(docs); |
| 384 | |
| 385 | out: |
| 386 | AEDisposeDesc(&docs); |
| 387 | return err; |
| 388 | } |
| 389 | |
| 390 | pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply, |
| 391 | long refcon) |
| 392 | { |
| 393 | DescType type; |
| 394 | Size size; |
| 395 | |
| 396 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
| 397 | &type, NULL, 0, &size) == noErr) |
| 398 | return errAEParamMissed; |
| 399 | |
| 400 | /* We can't meaningfully do anything here. */ |
| 401 | return errAEEventNotHandled; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Local Variables: |
| 406 | * c-file-style: "simon" |
| 407 | * End: |
| 408 | */ |