Add a configuration option for TCP keepalives (SO_KEEPALIVE), default off.
[u/mdw/putty] / mac / macterm.c
CommitLineData
79bf227b 1/* $Id: macterm.c,v 1.76 2004/06/20 17:07:37 jacob Exp $ */
d082ac49 2/*
3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29/*
30 * macterm.c -- Macintosh terminal front-end
31 */
32
33#include <MacTypes.h>
34#include <Controls.h>
35#include <ControlDefinitions.h>
2b8dca9e 36#include <FixMath.h>
d082ac49 37#include <Fonts.h>
38#include <Gestalt.h>
64b8ebc6 39#include <LowMem.h>
d082ac49 40#include <MacMemory.h>
41#include <MacWindows.h>
42#include <MixedMode.h>
43#include <Palettes.h>
44#include <Quickdraw.h>
45#include <QuickdrawText.h>
46#include <Resources.h>
47#include <Scrap.h>
48#include <Script.h>
49#include <Sound.h>
8768ce31 50#include <TextCommon.h>
d082ac49 51#include <ToolUtils.h>
8768ce31 52#include <UnicodeConverter.h>
d082ac49 53
54#include <assert.h>
55#include <limits.h>
56#include <stdlib.h>
57#include <stdio.h>
58#include <string.h>
59
60#include "macresid.h"
61#include "putty.h"
801e70ea 62#include "charset.h"
d082ac49 63#include "mac.h"
64#include "terminal.h"
65
66#define NCOLOURS (lenof(((Config *)0)->colours))
67
68#define DEFAULT_FG 16
69#define DEFAULT_FG_BOLD 17
70#define DEFAULT_BG 18
71#define DEFAULT_BG_BOLD 19
72#define CURSOR_FG 20
e0cbe032 73#define CURSOR_BG 21
d082ac49 74
75#define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
76 (x) / s->font_width)
77#define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
78 (y) / s->font_height)
79
80static void mac_initfont(Session *);
8768ce31 81static pascal OSStatus uni_to_font_fallback(UniChar *, ByteCount, ByteCount *,
82 TextPtr, ByteCount, ByteCount *,
1d4d52ce 83 LogicalAddress,
8768ce31 84 ConstUnicodeMappingPtr);
d082ac49 85static void mac_initpalette(Session *);
86static void mac_adjustwinbg(Session *);
87static void mac_adjustsize(Session *, int, int);
88static void mac_drawgrowicon(Session *s);
64b8ebc6 89static pascal void mac_growtermdraghook(void);
d082ac49 90static pascal void mac_scrolltracker(ControlHandle, short);
91static pascal void do_text_for_device(short, short, GDHandle, long);
d082ac49 92static void text_click(Session *, EventRecord *);
f854b482 93static void mac_activateterm(WindowPtr, EventRecord *);
94static void mac_adjusttermcursor(WindowPtr, Point, RgnHandle);
95static void mac_adjusttermmenus(WindowPtr);
96static void mac_updateterm(WindowPtr);
97static void mac_clickterm(WindowPtr, EventRecord *);
98static void mac_growterm(WindowPtr, EventRecord *);
99static void mac_keyterm(WindowPtr, EventRecord *);
100static void mac_menuterm(WindowPtr, short, short);
101static void mac_closeterm(WindowPtr);
d082ac49 102
103void pre_paint(Session *s);
104void post_paint(Session *s);
105
ce283213 106void mac_startsession(Session *s)
107{
cbe2d68f 108 const char *errmsg;
5211281f 109 int i;
347bfcd7 110 WinInfo *wi;
ce283213 111
c31f6c61 112 init_ucs(s);
5211281f 113
114 /*
115 * Select protocol. This is farmed out into a table in a
116 * separate file to enable an ssh-free variant.
117 */
118 s->back = NULL;
119 for (i = 0; backends[i].backend != NULL; i++)
120 if (backends[i].protocol == s->cfg.protocol) {
121 s->back = backends[i].backend;
122 break;
123 }
124 if (s->back == NULL)
125 fatalbox("Unsupported protocol number found");
126
d082ac49 127 /* XXX: Own storage management? */
128 if (HAVE_COLOR_QD())
129 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
130 else
131 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
34773273 132 wi = snew(WinInfo);
f854b482 133 memset(wi, 0, sizeof(*wi));
347bfcd7 134 wi->s = s;
135 wi->wtype = wTerminal;
f854b482 136 wi->activate = &mac_activateterm;
137 wi->adjustcursor = &mac_adjusttermcursor;
138 wi->adjustmenus = &mac_adjusttermmenus;
139 wi->update = &mac_updateterm;
140 wi->click = &mac_clickterm;
141 wi->grow = &mac_growterm;
142 wi->key = &mac_keyterm;
143 wi->menu = &mac_menuterm;
144 wi->close = &mac_closeterm;
347bfcd7 145 SetWRefCon(s->window, (long)wi);
d082ac49 146 s->scrollbar = GetNewControl(cVScroll, s->window);
c31f6c61 147 s->term = term_init(&s->cfg, &s->ucsdata, s);
d082ac49 148
2beb0fb0 149 mac_initfont(s);
150 mac_initpalette(s);
151 if (HAVE_COLOR_QD()) {
152 /* Set to FALSE to not get palette updates in the background. */
153 SetPalette(s->window, s->palette, TRUE);
154 ActivatePalette(s->window);
155 }
156
7dcd1f87 157 s->logctx = log_init(s->term, &s->cfg);
d082ac49 158 term_provide_logctx(s->term, s->logctx);
159
1e5bde54 160 errmsg = s->back->init(s, &s->backhandle, &s->cfg, s->cfg.host,
79bf227b 161 s->cfg.port, &s->realhost, s->cfg.tcp_nodelay,
162 s->cfg.tcp_keepalives);
5eec331c 163 if (errmsg != NULL)
164 fatalbox("%s", errmsg);
d082ac49 165 s->back->provide_logctx(s->backhandle, s->logctx);
d9fffe93 166 set_title(s, s->realhost);
d082ac49 167
168 term_provide_resize_fn(s->term, s->back->size, s->backhandle);
169
170 mac_adjustsize(s, s->cfg.height, s->cfg.width);
171 term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines);
172
fe5634f6 173 s->ldisc = ldisc_create(&s->cfg, s->term, s->back, s->backhandle, s);
d082ac49 174 ldisc_send(s->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
175
d082ac49 176 ShowWindow(s->window);
0c4b7799 177 s->next = sesslist;
fabd1805 178 s->prev = &sesslist;
9546c9c6 179 if (s->next != NULL)
b89053c0 180 s->next->prev = &s->next;
0c4b7799 181 sesslist = s;
d082ac49 182}
183
c3aef739 184/*
185 * Try to work out a horizontal scaling factor for the current font
186 * that will give a chracter width of wantwidth. Return it in numer
187 * and denom (suitable for passing to StdText()).
188 */
189static void mac_workoutfontscale(Session *s, int wantwidth,
190 Point *numerp, Point *denomp)
191{
192 Point numer, denom, tmpnumer, tmpdenom;
193 int gotwidth, i;
194 const char text = 'W';
195 FontInfo fi;
1d4d52ce 196#if TARGET_API_MAC_CARBON
bb8dd32f 197 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(s->window));
1d4d52ce 198#else
bb8dd32f 199 QDProcsPtr gp = s->window->grafProcs;
1d4d52ce 200#endif
c3aef739 201
c3aef739 202 numer.v = denom.v = 1; /* always */
203 numer.h = denom.h = 1;
204 for (i = 0; i < 3; i++) {
c3aef739 205 tmpnumer = numer;
206 tmpdenom = denom;
1d4d52ce 207 if (gp != NULL)
c3aef739 208 gotwidth = InvokeQDTxMeasUPP(1, &text, &tmpnumer, &tmpdenom, &fi,
1d4d52ce 209 gp->txMeasProc);
c3aef739 210 else
211 gotwidth = StdTxMeas(1, &text, &tmpnumer, &tmpdenom, &fi);
212 /* The result of StdTxMeas must be scaled by the factors it returns. */
213 gotwidth = FixRound(FixMul(gotwidth << 16,
214 FixRatio(tmpnumer.h, tmpdenom.h)));
c3aef739 215 if (gotwidth == wantwidth)
216 break;
217 numer.h *= wantwidth;
218 denom.h *= gotwidth;
219 }
220 *numerp = numer;
221 *denomp = denom;
222}
223
8768ce31 224static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
225
f854b482 226static void mac_initfont(Session *s)
227{
d082ac49 228 FontInfo fi;
8768ce31 229 TextEncoding enc;
379836ca 230 OptionBits fbflags;
8768ce31 231
5a736db6 232 SetPort((GrafPtr)GetWindowPort(s->window));
e3c5b245 233 GetFNum(s->cfg.font.name, &s->fontnum);
d082ac49 234 TextFont(s->fontnum);
e3c5b245 235 TextFace(s->cfg.font.face);
236 TextSize(s->cfg.font.size);
d082ac49 237 GetFontInfo(&fi);
238 s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
239 s->font_ascent = fi.ascent;
240 s->font_leading = fi.leading;
241 s->font_height = s->font_ascent + fi.descent + s->font_leading;
c3aef739 242 mac_workoutfontscale(s, s->font_width,
243 &s->font_stdnumer, &s->font_stddenom);
244 mac_workoutfontscale(s, s->font_width * 2,
245 &s->font_widenumer, &s->font_widedenom);
e3c5b245 246 TextSize(s->cfg.font.size * 2);
c3aef739 247 mac_workoutfontscale(s, s->font_width * 2,
248 &s->font_bignumer, &s->font_bigdenom);
e3c5b245 249 TextSize(s->cfg.font.size);
d082ac49 250 if (!s->cfg.bold_colour) {
251 TextFace(bold);
252 s->font_boldadjust = s->font_width - CharWidth('W');
253 } else
254 s->font_boldadjust = 0;
8768ce31 255
256 if (s->uni_to_font != NULL)
257 DisposeUnicodeToTextInfo(&s->uni_to_font);
8ef2b196 258 if (mac_gestalts.encvvers != 0 &&
8768ce31 259 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare,
260 kTextLanguageDontCare,
e3c5b245 261 kTextRegionDontCare, s->cfg.font.name,
8ef2b196 262 &enc) == noErr &&
263 CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) {
8768ce31 264 if (uni_to_font_fallback_upp == NULL)
265 uni_to_font_fallback_upp =
1d4d52ce 266 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback);
379836ca 267 fbflags = kUnicodeFallbackCustomOnly;
268 if (mac_gestalts.uncvattr & kTECAddFallbackInterruptMask)
269 fbflags |= kUnicodeFallbackInterruptSafeMask;
8768ce31 270 if (SetFallbackUnicodeToText(s->uni_to_font,
379836ca 271 uni_to_font_fallback_upp, fbflags, NULL) != noErr) {
8768ce31 272 DisposeUnicodeToTextInfo(&s->uni_to_font);
379836ca 273 goto no_encv;
8768ce31 274 }
8ef2b196 275 } else {
e3c5b245 276 char cfontname[256];
277
379836ca 278 no_encv:
8ef2b196 279 s->uni_to_font = NULL;
e3c5b245 280 p2cstrcpy(cfontname, s->cfg.font.name);
8ef2b196 281 s->font_charset =
282 charset_from_macenc(FontToScript(s->fontnum),
283 GetScriptManagerVariable(smRegionCode),
e3c5b245 284 mac_gestalts.sysvers, cfontname);
8768ce31 285 }
286
d082ac49 287 mac_adjustsize(s, s->term->rows, s->term->cols);
288}
289
8768ce31 290static pascal OSStatus uni_to_font_fallback(UniChar *ucp,
291 ByteCount ilen, ByteCount *iusedp, TextPtr obuf, ByteCount olen,
1d4d52ce 292 ByteCount *ousedp, LogicalAddress cookie, ConstUnicodeMappingPtr mapping)
8768ce31 293{
294
295 if (olen < 1)
296 return kTECOutputBufferFullStatus;
d1c57171 297 /*
298 * What I'd _like_ to do here is to somehow generate the
299 * missing-character glyph that every font is required to have.
300 * Unfortunately (and somewhat surprisingly), I can't find any way
301 * to actually ask for it explicitly. Bah.
302 */
303 *obuf = '.';
8768ce31 304 *iusedp = ilen;
305 *ousedp = 1;
306 return noErr;
307}
308
0c4b7799 309/*
310 * Called every time round the event loop.
311 */
312void mac_pollterm(void)
313{
314 Session *s;
315
316 for (s = sesslist; s != NULL; s = s->next) {
317 term_out(s->term);
318 term_update(s->term);
319 }
320}
8768ce31 321
d082ac49 322/*
323 * To be called whenever the window size changes.
324 * rows and cols should be desired values.
325 * It's assumed the terminal emulator will be informed, and will set rows
326 * and cols for us.
327 */
328static void mac_adjustsize(Session *s, int newrows, int newcols) {
329 int winwidth, winheight;
330
331 winwidth = newcols * s->font_width + 15;
332 winheight = newrows * s->font_height;
333 SizeWindow(s->window, winwidth, winheight, true);
334 HideControl(s->scrollbar);
335 MoveControl(s->scrollbar, winwidth - 15, -1);
336 SizeControl(s->scrollbar, 16, winheight - 13);
337 ShowControl(s->scrollbar);
0c367171 338 mac_drawgrowicon(s);
d082ac49 339}
340
f854b482 341static void mac_initpalette(Session *s)
342{
d082ac49 343
1eb3f8f0 344 if (!HAVE_COLOR_QD())
345 return;
d082ac49 346 /*
347 * Most colours should be inhibited on 2bpp displays.
348 * Palette manager documentation suggests inhibiting all tolerant colours
349 * on greyscale displays.
350 */
8a00b869 351#define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
352 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
d082ac49 353#define PM_TOLERANCE 0x2000
354 s->palette = NewPalette(22, NULL, PM_NORMAL, PM_TOLERANCE);
355 if (s->palette == NULL)
356 fatalbox("Unable to create palette");
357 /* In 2bpp, these are the colours we want most. */
358 SetEntryUsage(s->palette, DEFAULT_BG,
359 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
360 SetEntryUsage(s->palette, DEFAULT_FG,
361 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
362 SetEntryUsage(s->palette, DEFAULT_FG_BOLD,
363 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
8a00b869 364 SetEntryUsage(s->palette, CURSOR_BG,
d082ac49 365 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
366 palette_reset(s);
367}
368
369/*
370 * Set the background colour of the window correctly. Should be
371 * called whenever the default background changes.
372 */
f854b482 373static void mac_adjustwinbg(Session *s)
374{
d082ac49 375
376 if (!HAVE_COLOR_QD())
377 return;
1fc898ea 378#if !TARGET_CPU_68K
d082ac49 379 if (mac_gestalts.windattr & gestaltWindowMgrPresent)
380 SetWindowContentColor(s->window,
381 &(*s->palette)->pmInfo[DEFAULT_BG].ciRGB);
382 else
383#endif
384 {
1d4d52ce 385#if !TARGET_API_MAC_CARBON
d082ac49 386 if (s->wctab == NULL)
387 s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
388 if (s->wctab == NULL)
389 return; /* do without */
390 (*s->wctab)->wCSeed = 0;
391 (*s->wctab)->wCReserved = 0;
392 (*s->wctab)->ctSize = 0;
393 (*s->wctab)->ctTable[0].value = wContentColor;
394 (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
395 SetWinColor(s->window, s->wctab);
1d4d52ce 396#endif
d082ac49 397 }
398}
399
400/*
401 * Set the cursor shape correctly
402 */
f854b482 403static void mac_adjusttermcursor(WindowPtr window, Point mouse,
404 RgnHandle cursrgn)
405{
d082ac49 406 Session *s;
407 ControlHandle control;
408 short part;
409 int x, y;
1d4d52ce 410#if TARGET_API_MAC_CARBON
411 Cursor arrow;
412 Rect rect;
413 RgnHandle visrgn;
414#endif
d082ac49 415
5a736db6 416 SetPort((GrafPtr)GetWindowPort(window));
347bfcd7 417 s = mac_windowsession(window);
d082ac49 418 GlobalToLocal(&mouse);
419 part = FindControl(mouse, window, &control);
420 if (control == s->scrollbar) {
1d4d52ce 421#if TARGET_API_MAC_CARBON
422 SetCursor(GetQDGlobalsArrow(&arrow));
423 RectRgn(cursrgn, GetControlBounds(s->scrollbar, &rect));
424#else
d082ac49 425 SetCursor(&qd.arrow);
426 RectRgn(cursrgn, &(*s->scrollbar)->contrlRect);
1d4d52ce 427#endif
d082ac49 428 } else {
429 x = mouse.h / s->font_width;
430 y = mouse.v / s->font_height;
1d4d52ce 431 if (s->raw_mouse) {
432#if TARGET_API_MAC_CARBON
433 SetCursor(GetQDGlobalsArrow(&arrow));
434#else
d082ac49 435 SetCursor(&qd.arrow);
1d4d52ce 436#endif
437 } else
d082ac49 438 SetCursor(*GetCursor(iBeamCursor));
439 /* Ask for shape changes if we leave this character cell. */
440 SetRectRgn(cursrgn, x * s->font_width, y * s->font_height,
441 (x + 1) * s->font_width, (y + 1) * s->font_height);
d082ac49 442 }
1d4d52ce 443#if TARGET_API_MAC_CARBON
444 visrgn = NewRgn();
445 GetPortVisibleRegion(GetWindowPort(window), visrgn);
446 SectRgn(cursrgn, visrgn, cursrgn);
447 DisposeRgn(visrgn);
448#else
449 SectRgn(cursrgn, window->visRgn, cursrgn);
450#endif
d082ac49 451}
452
453/*
454 * Enable/disable menu items based on the active terminal window.
455 */
1d4d52ce 456#if TARGET_API_MAC_CARBON
457#define DisableItem DisableMenuItem
458#define EnableItem EnableMenuItem
459#endif
f854b482 460static void mac_adjusttermmenus(WindowPtr window)
461{
d082ac49 462 Session *s;
463 MenuHandle menu;
1d4d52ce 464#if !TARGET_API_MAC_CARBON
d082ac49 465 long offset;
1d4d52ce 466#endif
d082ac49 467
347bfcd7 468 s = mac_windowsession(window);
b537dd42 469 menu = GetMenuHandle(mFile);
470 DisableItem(menu, iSave); /* XXX enable if modified */
471 EnableItem(menu, iSaveAs);
5211281f 472 EnableItem(menu, iDuplicate);
d082ac49 473 menu = GetMenuHandle(mEdit);
474 EnableItem(menu, 0);
475 DisableItem(menu, iUndo);
476 DisableItem(menu, iCut);
477 if (1/*s->term->selstate == SELECTED*/)
478 EnableItem(menu, iCopy);
479 else
480 DisableItem(menu, iCopy);
1d4d52ce 481#if TARGET_API_MAC_CARBON
482 if (1)
483#else
47de7565 484 if (GetScrap(NULL, kScrapFlavorTypeText, &offset) == noTypeErr)
1d4d52ce 485#endif
d082ac49 486 DisableItem(menu, iPaste);
487 else
488 EnableItem(menu, iPaste);
489 DisableItem(menu, iClear);
490 EnableItem(menu, iSelectAll);
7dcd1f87 491 menu = GetMenuHandle(mWindow);
492 EnableItem(menu, 0);
493 EnableItem(menu, iShowEventLog);
d082ac49 494}
495
f854b482 496static void mac_menuterm(WindowPtr window, short menu, short item)
497{
d082ac49 498 Session *s;
499
347bfcd7 500 s = mac_windowsession(window);
d082ac49 501 switch (menu) {
502 case mEdit:
503 switch (item) {
504 case iCopy:
505 /* term_copy(s); */
506 break;
507 case iPaste:
508 term_do_paste(s->term);
509 break;
510 }
7dcd1f87 511 break;
512 case mWindow:
513 switch(item) {
514 case iShowEventLog:
515 mac_showeventlog(s);
516 break;
517 }
518 break;
d082ac49 519 }
520}
521
f854b482 522static void mac_clickterm(WindowPtr window, EventRecord *event)
523{
d082ac49 524 Session *s;
525 Point mouse;
526 ControlHandle control;
527 int part;
1d4d52ce 528 static ControlActionUPP mac_scrolltracker_upp = NULL;
d082ac49 529
347bfcd7 530 s = mac_windowsession(window);
5a736db6 531 SetPort((GrafPtr)GetWindowPort(window));
d082ac49 532 mouse = event->where;
533 GlobalToLocal(&mouse);
534 part = FindControl(mouse, window, &control);
535 if (control == s->scrollbar) {
536 switch (part) {
537 case kControlIndicatorPart:
538 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
539 term_scroll(s->term, +1, GetControlValue(control));
540 break;
541 case kControlUpButtonPart:
542 case kControlDownButtonPart:
543 case kControlPageUpPart:
544 case kControlPageDownPart:
1d4d52ce 545 if (mac_scrolltracker_upp == NULL)
546 mac_scrolltracker_upp =
547 NewControlActionUPP(&mac_scrolltracker);
548 TrackControl(control, mouse, mac_scrolltracker_upp);
d082ac49 549 break;
550 }
551 } else {
552 text_click(s, event);
553 }
554}
555
f854b482 556static void text_click(Session *s, EventRecord *event)
557{
d082ac49 558 Point localwhere;
559 int row, col;
560 static UInt32 lastwhen = 0;
561 static Session *lastsess = NULL;
562 static int lastrow = -1, lastcol = -1;
563 static Mouse_Action lastact = MA_NOTHING;
564
5a736db6 565 SetPort((GrafPtr)GetWindowPort(s->window));
d082ac49 566 localwhere = event->where;
567 GlobalToLocal(&localwhere);
568
569 col = PTOCC(localwhere.h);
570 row = PTOCR(localwhere.v);
571 if (event->when - lastwhen < GetDblTime() &&
572 row == lastrow && col == lastcol && s == lastsess)
573 lastact = (lastact == MA_CLICK ? MA_2CLK :
574 lastact == MA_2CLK ? MA_3CLK :
575 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
576 else
577 lastact = MA_CLICK;
fc5b0934 578 term_mouse(s->term, MBT_LEFT,
579 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 580 lastact, col, row, event->modifiers & shiftKey,
581 event->modifiers & controlKey, event->modifiers & optionKey);
582 lastsess = s;
583 lastrow = row;
584 lastcol = col;
585 while (StillDown()) {
586 GetMouse(&localwhere);
587 col = PTOCC(localwhere.h);
588 row = PTOCR(localwhere.v);
fc5b0934 589 term_mouse(s->term, MBT_LEFT,
590 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 591 MA_DRAG, col, row, event->modifiers & shiftKey,
592 event->modifiers & controlKey,
593 event->modifiers & optionKey);
594 if (row > s->term->rows - 1)
595 term_scroll(s->term, 0, row - (s->term->rows - 1));
596 else if (row < 0)
597 term_scroll(s->term, 0, row);
598 }
fc5b0934 599 term_mouse(s->term, MBT_LEFT,
600 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 601 MA_RELEASE, col, row, event->modifiers & shiftKey,
602 event->modifiers & controlKey, event->modifiers & optionKey);
603 lastwhen = TickCount();
604}
605
39687cc7 606void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
607{
1d4d52ce 608#if !TARGET_API_MAC_CARBON
39687cc7 609 Session *s = cookie;
610 char *mactextbuf;
611 ByteCount iread, olen;
612 wchar_t *unitextptr;
613 StScrpRec *stsc;
614 size_t stsz;
615 OSErr err;
616 int i;
617
d082ac49 618 /*
619 * See "Programming with the Text Encoding Conversion Manager"
620 * Appendix E for Unicode scrap conventions.
621 *
d082ac49 622 * XXX Maybe PICT scrap too.
623 */
624 if (ZeroScrap() != noErr)
625 return;
47de7565 626 PutScrap(len * sizeof(*data), kScrapFlavorTypeUnicode, data);
39687cc7 627
628 /* Replace LINE SEPARATORs with CR for TEXT output. */
629 for (i = 0; i < len; i++)
630 if (data[i] == 0x2028)
631 data[i] = 0x000d;
632
34773273 633 mactextbuf = snewn(len, char); /* XXX DBCS */
39687cc7 634 if (s->uni_to_font != NULL) {
635 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
636 (UniChar *)data,
637 kUnicodeUseFallbacksMask,
638 0, NULL, NULL, NULL,
639 len, &iread, &olen, mactextbuf);
640 if (err != noErr && err != kTECUsedFallbacksStatus)
641 return;
642 } else if (s->font_charset != CS_NONE) {
643 unitextptr = data;
644 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
645 s->font_charset, NULL, ".", 1);
646 } else
647 return;
47de7565 648 PutScrap(olen, kScrapFlavorTypeText, mactextbuf);
39687cc7 649 sfree(mactextbuf);
650
651 stsz = offsetof(StScrpRec, scrpStyleTab) + sizeof(ScrpSTElement);
652 stsc = smalloc(stsz);
653 stsc->scrpNStyles = 1;
654 stsc->scrpStyleTab[0].scrpStartChar = 0;
655 stsc->scrpStyleTab[0].scrpHeight = s->font_height;
656 stsc->scrpStyleTab[0].scrpAscent = s->font_ascent;
657 stsc->scrpStyleTab[0].scrpFont = s->fontnum;
658 stsc->scrpStyleTab[0].scrpFace = 0;
e3c5b245 659 stsc->scrpStyleTab[0].scrpSize = s->cfg.font.size;
39687cc7 660 stsc->scrpStyleTab[0].scrpColor.red = 0;
661 stsc->scrpStyleTab[0].scrpColor.green = 0;
662 stsc->scrpStyleTab[0].scrpColor.blue = 0;
47de7565 663 PutScrap(stsz, kScrapFlavorTypeTextStyle, stsc);
39687cc7 664 sfree(stsc);
1d4d52ce 665#endif
d082ac49 666}
667
f854b482 668void get_clip(void *frontend, wchar_t **p, int *lenp)
669{
1d4d52ce 670#if TARGET_API_MAC_CARBON
671 *lenp = 0;
672#else
d082ac49 673 Session *s = frontend;
674 static Handle h = NULL;
fd3bd9fb 675 static wchar_t *data = NULL;
676 Handle texth;
d082ac49 677 long offset;
fd3bd9fb 678 int textlen;
679 TextEncoding enc;
680 TextToUnicodeInfo scrap_to_uni;
681 ByteCount iread, olen;
682 int charset;
683 char *tptr;
684 OSErr err;
d082ac49 685
686 if (p == NULL) {
687 /* release memory */
688 if (h != NULL)
689 DisposeHandle(h);
690 h = NULL;
fd3bd9fb 691 if (data != NULL)
692 sfree(data);
693 data = NULL;
694 } else {
47de7565 695 if (GetScrap(NULL, kScrapFlavorTypeUnicode, &offset) > 0) {
fd3bd9fb 696 if (h == NULL)
697 h = NewHandle(0);
47de7565 698 *lenp =
699 GetScrap(h, kScrapFlavorTypeUnicode, &offset) / sizeof(**p);
d082ac49 700 HLock(h);
701 *p = (wchar_t *)*h;
47de7565 702 } else if (GetScrap(NULL, kScrapFlavorTypeText, &offset) > 0) {
fd3bd9fb 703 texth = NewHandle(0);
47de7565 704 textlen = GetScrap(texth, kScrapFlavorTypeText, &offset);
fd3bd9fb 705 HLock(texth);
34773273 706 data = snewn(textlen, wchar_t);
fd3bd9fb 707 /* XXX should use 'styl' scrap if it's there. */
708 if (mac_gestalts.encvvers != 0 &&
709 UpgradeScriptInfoToTextEncoding(smSystemScript,
710 kTextLanguageDontCare,
711 kTextRegionDontCare, NULL,
712 &enc) == noErr &&
713 CreateTextToUnicodeInfoByEncoding(enc, &scrap_to_uni) ==
714 noErr) {
715 err = ConvertFromTextToUnicode(scrap_to_uni, textlen,
716 *texth, 0, 0, NULL, NULL, NULL,
717 textlen * 2,
718 &iread, &olen, data);
719 DisposeTextToUnicodeInfo(&scrap_to_uni);
720 if (err == noErr) {
721 *p = data;
722 *lenp = olen / sizeof(**p);
723 } else {
724 *p = NULL;
725 *lenp = 0;
726 }
727 } else {
728 charset =
729 charset_from_macenc(GetScriptManagerVariable(smSysScript),
730 GetScriptManagerVariable(smRegionCode),
731 mac_gestalts.sysvers, NULL);
732 if (charset != CS_NONE) {
733 tptr = *texth;
734 *lenp = charset_to_unicode(&tptr, &textlen, data,
735 textlen * 2, charset, NULL,
736 NULL, 0);
737 }
738 *p = data;
739 }
740 DisposeHandle(texth);
d082ac49 741 } else {
742 *p = NULL;
743 *lenp = 0;
744 }
fd3bd9fb 745 }
1d4d52ce 746#endif
d082ac49 747}
748
f854b482 749static pascal void mac_scrolltracker(ControlHandle control, short part)
750{
d082ac49 751 Session *s;
752
1d4d52ce 753#if TARGET_API_MAC_CARBON
347bfcd7 754 s = mac_windowsession(GetControlOwner(control));
1d4d52ce 755#else
347bfcd7 756 s = mac_windowsession((*control)->contrlOwner);
1d4d52ce 757#endif
d082ac49 758 switch (part) {
759 case kControlUpButtonPart:
760 term_scroll(s->term, 0, -1);
761 break;
762 case kControlDownButtonPart:
763 term_scroll(s->term, 0, +1);
764 break;
765 case kControlPageUpPart:
766 term_scroll(s->term, 0, -(s->term->rows - 1));
767 break;
768 case kControlPageDownPart:
769 term_scroll(s->term, 0, +(s->term->rows - 1));
770 break;
771 }
772}
773
f854b482 774static void mac_keyterm(WindowPtr window, EventRecord *event)
775{
347bfcd7 776 Session *s = mac_windowsession(window);
6c50d421 777 Key_Sym keysym = PK_NULL;
778 unsigned int mods = 0, flags = PKF_NUMLOCK;
4fa38586 779 UniChar utxt[1];
780 char txt[1];
781 size_t len = 0;
782 ScriptCode key_script;
d082ac49 783
10135a55 784 ObscureCursor();
d082ac49 785
37ea5668 786#if 0
6c50d421 787 fprintf(stderr, "Got key event %08x\n", event->message);
37ea5668 788#endif
d082ac49 789
790 /* No meta key yet -- that'll be rather fun. */
791
792 /* Keys that we handle locally */
793 if (event->modifiers & shiftKey) {
6c50d421 794 switch ((event->message & keyCodeMask) >> 8) {
795 case 0x74: /* shift-pageup */
d082ac49 796 term_scroll(s->term, 0, -(s->term->rows - 1));
6c50d421 797 return;
798 case 0x79: /* shift-pagedown */
d082ac49 799 term_scroll(s->term, 0, +(s->term->rows - 1));
6c50d421 800 return;
d082ac49 801 }
802 }
803
6c50d421 804 if (event->modifiers & shiftKey)
805 mods |= PKM_SHIFT;
806 if (event->modifiers & controlKey)
807 mods |= PKM_CONTROL;
808 if (event->what == autoKey)
809 flags |= PKF_REPEAT;
810
811 /* Mac key events consist of a virtual key code and a character code. */
812
813 switch ((event->message & keyCodeMask) >> 8) {
814 case 0x24: keysym = PK_RETURN; break;
815 case 0x30: keysym = PK_TAB; break;
816 case 0x33: keysym = PK_BACKSPACE; break;
817 case 0x35: keysym = PK_ESCAPE; break;
818
819 case 0x7A: keysym = PK_F1; break;
820 case 0x78: keysym = PK_F2; break;
821 case 0x63: keysym = PK_F3; break;
822 case 0x76: keysym = PK_F4; break;
823 case 0x60: keysym = PK_F5; break;
824 case 0x61: keysym = PK_F6; break;
825 case 0x62: keysym = PK_F7; break;
826 case 0x64: keysym = PK_F8; break;
827 case 0x65: keysym = PK_F9; break;
828 case 0x6D: keysym = PK_F10; break;
829 case 0x67: keysym = PK_F11; break;
830 case 0x6F: keysym = PK_F12; break;
831 case 0x69: keysym = PK_F13; break;
832 case 0x6B: keysym = PK_F14; break;
833 case 0x71: keysym = PK_F15; break;
834
835 case 0x72: keysym = PK_INSERT; break;
836 case 0x73: keysym = PK_HOME; break;
837 case 0x74: keysym = PK_PAGEUP; break;
838 case 0x75: keysym = PK_DELETE; break;
839 case 0x77: keysym = PK_END; break;
840 case 0x79: keysym = PK_PAGEDOWN; break;
841
842 case 0x47: keysym = PK_PF1; break;
843 case 0x51: keysym = PK_PF2; break;
844 case 0x4B: keysym = PK_PF3; break;
845 case 0x43: keysym = PK_PF4; break;
846 case 0x4E: keysym = PK_KPMINUS; break;
847 case 0x45: keysym = PK_KPCOMMA; break;
848 case 0x41: keysym = PK_KPDECIMAL; break;
849 case 0x4C: keysym = PK_KPENTER; break;
850 case 0x52: keysym = PK_KP0; break;
851 case 0x53: keysym = PK_KP1; break;
852 case 0x54: keysym = PK_KP2; break;
853 case 0x55: keysym = PK_KP3; break;
854 case 0x56: keysym = PK_KP4; break;
855 case 0x57: keysym = PK_KP5; break;
856 case 0x58: keysym = PK_KP6; break;
857 case 0x59: keysym = PK_KP7; break;
858 case 0x5B: keysym = PK_KP8; break;
859 case 0x5C: keysym = PK_KP9; break;
860
861 case 0x7B: keysym = PK_LEFT; break;
862 case 0x7C: keysym = PK_RIGHT; break;
863 case 0x7D: keysym = PK_DOWN; break;
864 case 0x7E: keysym = PK_UP; break;
d082ac49 865 }
866
4fa38586 867 /* Map from key script to Unicode. */
868 txt[0] = event->message & charCodeMask;
869 key_script = GetScriptManagerVariable(smKeyScript);
870
871 if (mac_gestalts.encvvers != 0) {
872 static TextToUnicodeInfo key_to_uni = NULL;
873 static ScriptCode key_to_uni_script;
874 TextEncoding enc;
875 ByteCount iread, olen;
876 OSErr err;
877
878 if (key_to_uni != NULL && key_to_uni_script != key_script)
879 DisposeTextToUnicodeInfo(&key_to_uni);
880 if (key_to_uni == NULL || key_to_uni_script != key_script) {
881 if (UpgradeScriptInfoToTextEncoding(key_script,
882 kTextLanguageDontCare,
883 kTextRegionDontCare, NULL,
884 &enc) == noErr &&
885 CreateTextToUnicodeInfoByEncoding(enc, &key_to_uni) == noErr)
886 key_to_uni_script = key_script;
887 else
888 key_to_uni = NULL;
889 }
890 if (key_to_uni != NULL) {
891 err = ConvertFromTextToUnicode(key_to_uni, 1, txt,
892 (kUnicodeKeepInfoMask |
893 kUnicodeStringUnterminatedMask),
894 0, NULL, NULL, NULL,
895 sizeof(utxt), &iread, &olen, utxt);
896 if (err == noErr)
897 len = olen / sizeof(*utxt);
898 }
899 } else {
900 int charset;
901 char *tptr = txt;
902 int tlen = 1;
903
904 charset = charset_from_macenc(key_script,
905 GetScriptManagerVariable(smRegionCode),
906 mac_gestalts.sysvers, NULL);
907 if (charset != CS_NONE) {
908 len = charset_to_unicode(&tptr, &tlen, utxt, sizeof(utxt), charset,
909 NULL, NULL, 0);
910 }
911 }
912 term_key(s->term, keysym, utxt, len, mods, flags);
d082ac49 913}
914
915void request_paste(void *frontend)
916{
917 Session *s = frontend;
918
919 /*
920 * In the Mac OS, pasting is synchronous: we can read the
921 * clipboard with no difficulty, so request_paste() can just go
922 * ahead and paste.
923 */
924 term_do_paste(s->term);
925}
926
64b8ebc6 927static struct {
928 Rect msgrect;
929 Point msgorigin;
8095091b 930 Point zeromouse;
64b8ebc6 931 Session *s;
932 char oldmsg[20];
933} growterm_state;
934
f854b482 935static void mac_growterm(WindowPtr window, EventRecord *event)
936{
d082ac49 937 Rect limits;
938 long grow_result;
939 int newrows, newcols;
940 Session *s;
1d4d52ce 941#if !TARGET_API_MAC_CARBON
64b8ebc6 942 DragGrayRgnUPP draghooksave;
943 GrafPtr portsave;
944 FontInfo fi;
1d4d52ce 945#endif
d082ac49 946
347bfcd7 947 s = mac_windowsession(window);
64b8ebc6 948
1d4d52ce 949#if !TARGET_API_MAC_CARBON
64b8ebc6 950 draghooksave = LMGetDragHook();
951 growterm_state.oldmsg[0] = '\0';
8095091b 952 growterm_state.zeromouse = event->where;
953 growterm_state.zeromouse.h -= s->term->cols * s->font_width;
954 growterm_state.zeromouse.v -= s->term->rows * s->font_height;
64b8ebc6 955 growterm_state.s = s;
956 GetPort(&portsave);
957 SetPort(s->window);
958 BackColor(whiteColor);
959 ForeColor(blackColor);
960 TextFont(systemFont);
961 TextFace(0);
962 TextSize(12);
963 GetFontInfo(&fi);
964 SetRect(&growterm_state.msgrect, 0, 0,
965 StringWidth("\p99999x99999") + 4, fi.ascent + fi.descent + 4);
966 SetPt(&growterm_state.msgorigin, 2, fi.ascent + 2);
967 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook));
1d4d52ce 968#endif
64b8ebc6 969
d082ac49 970 SetRect(&limits, s->font_width + 15, s->font_height, SHRT_MAX, SHRT_MAX);
971 grow_result = GrowWindow(window, event->where, &limits);
64b8ebc6 972
1d4d52ce 973#if !TARGET_API_MAC_CARBON
64b8ebc6 974 DisposeDragGrayRgnUPP(LMGetDragHook());
975 LMSetDragHook(draghooksave);
976 InvalRect(&growterm_state.msgrect);
977
978 SetPort(portsave);
1d4d52ce 979#endif
64b8ebc6 980
d082ac49 981 if (grow_result != 0) {
982 newrows = HiWord(grow_result) / s->font_height;
983 newcols = (LoWord(grow_result) - 15) / s->font_width;
984 mac_adjustsize(s, newrows, newcols);
985 term_size(s->term, newrows, newcols, s->cfg.savelines);
986 }
987}
988
1d4d52ce 989#if !TARGET_API_MAC_CARBON
64b8ebc6 990static pascal void mac_growtermdraghook(void)
991{
992 Session *s = growterm_state.s;
993 GrafPtr portsave;
994 Point mouse;
995 char buf[20];
786ccf05 996 unsigned char pbuf[20];
64b8ebc6 997 int newrows, newcols;
998
999 GetMouse(&mouse);
8095091b 1000 newrows = (mouse.v - growterm_state.zeromouse.v) / s->font_height;
64b8ebc6 1001 if (newrows < 1) newrows = 1;
8095091b 1002 newcols = (mouse.h - growterm_state.zeromouse.h) / s->font_width;
64b8ebc6 1003 if (newcols < 1) newcols = 1;
1004 sprintf(buf, "%dx%d", newcols, newrows);
1005 if (strcmp(buf, growterm_state.oldmsg) == 0)
1006 return;
1007 strcpy(growterm_state.oldmsg, buf);
786ccf05 1008 c2pstrcpy(pbuf, buf);
64b8ebc6 1009
1010 GetPort(&portsave);
1011 SetPort(growterm_state.s->window);
1012 EraseRect(&growterm_state.msgrect);
1013 MoveTo(growterm_state.msgorigin.h, growterm_state.msgorigin.v);
786ccf05 1014 DrawString(pbuf);
64b8ebc6 1015 SetPort(portsave);
1016}
1d4d52ce 1017#endif
64b8ebc6 1018
fabd1805 1019void mac_closeterm(WindowPtr window)
1020{
347bfcd7 1021 Session *s = mac_windowsession(window);
fabd1805 1022
1023 /* XXX warn on close */
1024 HideWindow(s->window);
1025 *s->prev = s->next;
1026 s->next->prev = s->prev;
1027 ldisc_free(s->ldisc);
1028 s->back->free(s->backhandle);
1029 log_free(s->logctx);
1030 if (s->uni_to_font != NULL)
1031 DisposeUnicodeToTextInfo(&s->uni_to_font);
1032 term_free(s->term);
7dcd1f87 1033 mac_freeeventlog(s);
347bfcd7 1034 sfree((WinInfo *)GetWRefCon(s->window));
fabd1805 1035 DisposeWindow(s->window);
1036 DisposePalette(s->palette);
1037 sfree(s);
1038}
1039
f854b482 1040static void mac_activateterm(WindowPtr window, EventRecord *event)
1041{
d082ac49 1042 Session *s;
f854b482 1043 Boolean active = (event->modifiers & activeFlag) != 0;
d082ac49 1044
347bfcd7 1045 s = mac_windowsession(window);
7d22c2e8 1046 s->term->has_focus = active;
d082ac49 1047 term_update(s->term);
1048 if (active)
1049 ShowControl(s->scrollbar);
1050 else {
1051 if (HAVE_COLOR_QD())
1052 PmBackColor(DEFAULT_BG);/* HideControl clears behind the control */
1053 else
1054 BackColor(blackColor);
1055 HideControl(s->scrollbar);
1056 }
1057 mac_drawgrowicon(s);
1058}
1059
f854b482 1060static void mac_updateterm(WindowPtr window)
1061{
d082ac49 1062 Session *s;
1d4d52ce 1063 Rect bbox;
1064#if TARGET_API_MAC_CARBON
1065 RgnHandle visrgn;
1066#endif
d082ac49 1067
347bfcd7 1068 s = mac_windowsession(window);
5a736db6 1069 SetPort((GrafPtr)GetWindowPort(window));
d082ac49 1070 BeginUpdate(window);
1071 pre_paint(s);
1d4d52ce 1072#if TARGET_API_MAC_CARBON
1073 visrgn = NewRgn();
1074 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1075 GetRegionBounds(visrgn, &bbox);
1076#else
1077 bbox = (*window->visRgn)->rgnBBox;
1078#endif
1079 term_paint(s->term, s, PTOCC(bbox.left), PTOCR(bbox.top),
1080 PTOCC(bbox.right), PTOCR(bbox.bottom), 1);
d082ac49 1081 /* Restore default colours in case the Window Manager uses them */
1082 if (HAVE_COLOR_QD()) {
1083 PmForeColor(DEFAULT_FG);
1084 PmBackColor(DEFAULT_BG);
1085 } else {
1086 ForeColor(whiteColor);
1087 BackColor(blackColor);
1088 }
1089 if (FrontWindow() != window)
1d4d52ce 1090#if TARGET_API_MAC_CARBON
1091 EraseRect(GetControlBounds(s->scrollbar, &bbox));
1092 UpdateControls(window, visrgn);
1093 DisposeRgn(visrgn);
1094#else
d082ac49 1095 EraseRect(&(*s->scrollbar)->contrlRect);
1096 UpdateControls(window, window->visRgn);
1d4d52ce 1097#endif
d082ac49 1098 mac_drawgrowicon(s);
1099 post_paint(s);
1100 EndUpdate(window);
1101}
1102
f854b482 1103static void mac_drawgrowicon(Session *s)
1104{
d082ac49 1105 Rect clip;
ea4c3d8a 1106 RgnHandle savergn;
d082ac49 1107
5a736db6 1108 SetPort((GrafPtr)GetWindowPort(s->window));
ea4c3d8a 1109 /*
1110 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1111 * See Tech Note TB575 for details.
1112 */
1d4d52ce 1113#if TARGET_API_MAC_CARBON
1114 GetPortBounds(GetWindowPort(s->window), &clip);
1115#else
ea4c3d8a 1116 clip = s->window->portRect;
1d4d52ce 1117#endif
ea4c3d8a 1118 clip.left = clip.right - 15;
1119 savergn = NewRgn();
1120 GetClip(savergn);
d082ac49 1121 ClipRect(&clip);
1122 DrawGrowIcon(s->window);
ea4c3d8a 1123 SetClip(savergn);
1124 DisposeRgn(savergn);
d082ac49 1125}
1126
1127struct do_text_args {
1128 Session *s;
1129 Rect textrect;
d082ac49 1130 char *text;
1131 int len;
1132 unsigned long attr;
1133 int lattr;
fd7d8b47 1134 Point numer, denom;
d082ac49 1135};
1136
1137/*
1138 * Call from the terminal emulator to draw a bit of text
1139 *
1140 * x and y are text row and column (zero-based)
1141 */
1142void do_text(Context ctx, int x, int y, char *text, int len,
f854b482 1143 unsigned long attr, int lattr)
1144{
d082ac49 1145 Session *s = ctx;
e3c5b245 1146 int style;
d082ac49 1147 struct do_text_args a;
c3aef739 1148 RgnHandle textrgn, saveclip;
1d4d52ce 1149#if TARGET_API_MAC_CARBON
1150 RgnHandle visrgn;
1151#endif
8768ce31 1152 char mactextbuf[1024];
1153 UniChar unitextbuf[1024];
801e70ea 1154 wchar_t *unitextptr;
c3aef739 1155 int i, fontwidth;
801e70ea 1156 ByteCount iread, olen;
1157 OSStatus err;
1d4d52ce 1158 static DeviceLoopDrawingUPP do_text_for_device_upp = NULL;
8768ce31 1159
1160 assert(len <= 1024);
d082ac49 1161
5a736db6 1162 SetPort((GrafPtr)GetWindowPort(s->window));
c3aef739 1163
1164 fontwidth = s->font_width;
1165 if ((lattr & LATTR_MODE) != LATTR_NORM)
1166 fontwidth *= 2;
1167
d082ac49 1168 /* First check this text is relevant */
1169 a.textrect.top = y * s->font_height;
1170 a.textrect.bottom = (y + 1) * s->font_height;
c3aef739 1171 a.textrect.left = x * fontwidth;
1172 a.textrect.right = (x + len) * fontwidth;
1173 if (a.textrect.right > s->term->cols * s->font_width)
1174 a.textrect.right = s->term->cols * s->font_width;
1d4d52ce 1175#if TARGET_API_MAC_CARBON
1176 visrgn = NewRgn();
1177 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1178 if (!RectInRgn(&a.textrect, visrgn)) {
1179 DisposeRgn(visrgn);
1180 return;
1181 }
1182 DisposeRgn(visrgn);
1183#else
d082ac49 1184 if (!RectInRgn(&a.textrect, s->window->visRgn))
1185 return;
1d4d52ce 1186#endif
d082ac49 1187
801e70ea 1188 /* Unpack Unicode from the mad format we get passed */
1189 for (i = 0; i < len; i++)
1190 unitextbuf[i] = (unsigned char)text[i] | (attr & CSET_MASK);
8768ce31 1191
801e70ea 1192 if (s->uni_to_font != NULL) {
8768ce31 1193 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
1194 unitextbuf, kUnicodeUseFallbacksMask,
1195 0, NULL, NULL, NULL,
1196 1024, &iread, &olen, mactextbuf);
801e70ea 1197 if (err != noErr && err != kTECUsedFallbacksStatus)
379836ca 1198 olen = 0;
8ef2b196 1199 } else if (s->font_charset != CS_NONE) {
801e70ea 1200 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1201 unitextptr = (wchar_t *)unitextbuf;
801e70ea 1202 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
8ef2b196 1203 s->font_charset, NULL, ".", 1);
1204 } else
379836ca 1205 olen = 0;
8768ce31 1206
d082ac49 1207 a.s = s;
801e70ea 1208 a.text = mactextbuf;
1209 a.len = olen;
d082ac49 1210 a.attr = attr;
1211 a.lattr = lattr;
c3aef739 1212 switch (lattr & LATTR_MODE) {
1213 case LATTR_NORM:
e3c5b245 1214 TextSize(s->cfg.font.size);
c3aef739 1215 a.numer = s->font_stdnumer;
1216 a.denom = s->font_stddenom;
1217 break;
1218 case LATTR_WIDE:
e3c5b245 1219 TextSize(s->cfg.font.size);
c3aef739 1220 a.numer = s->font_widenumer;
1221 a.denom = s->font_widedenom;
1222 break;
1223 case LATTR_TOP:
1224 case LATTR_BOT:
e3c5b245 1225 TextSize(s->cfg.font.size * 2);
c3aef739 1226 a.numer = s->font_bignumer;
1227 a.denom = s->font_bigdenom;
1228 break;
1229 }
5a736db6 1230 SetPort((GrafPtr)GetWindowPort(s->window));
d082ac49 1231 TextFont(s->fontnum);
e3c5b245 1232 style = s->cfg.font.face;
1233 if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)
d082ac49 1234 style |= bold;
1235 if (attr & ATTR_UNDER)
1236 style |= underline;
1237 TextFace(style);
4d1565a4 1238 TextMode(srcOr);
d082ac49 1239 if (HAVE_COLOR_QD())
1240 if (style & bold) {
1241 SpaceExtra(s->font_boldadjust << 16);
1242 CharExtra(s->font_boldadjust << 16);
1243 } else {
1244 SpaceExtra(0);
1245 CharExtra(0);
1246 }
c3aef739 1247 saveclip = NewRgn();
1248 GetClip(saveclip);
1249 ClipRect(&a.textrect);
d082ac49 1250 textrgn = NewRgn();
1251 RectRgn(textrgn, &a.textrect);
1d4d52ce 1252 if (HAVE_COLOR_QD()) {
1253 if (do_text_for_device_upp == NULL)
1254 do_text_for_device_upp =
1255 NewDeviceLoopDrawingUPP(&do_text_for_device);
1256 DeviceLoop(textrgn, do_text_for_device_upp, (long)&a, 0);
1257 } else
d082ac49 1258 do_text_for_device(1, 0, NULL, (long)&a);
c3aef739 1259 SetClip(saveclip);
1260 DisposeRgn(saveclip);
d082ac49 1261 DisposeRgn(textrgn);
1262 /* Tell the window manager about it in case this isn't an update */
1d4d52ce 1263#if TARGET_API_MAC_CARBON
1264 ValidWindowRect(s->window, &a.textrect);
1265#else
d082ac49 1266 ValidRect(&a.textrect);
1d4d52ce 1267#endif
d082ac49 1268}
1269
1270static pascal void do_text_for_device(short depth, short devflags,
f854b482 1271 GDHandle device, long cookie)
1272{
5a736db6 1273 struct do_text_args *a = (struct do_text_args *)cookie;
4d1565a4 1274 int bgcolour, fgcolour, bright, reverse, tmp;
1d4d52ce 1275#if TARGET_API_MAC_CARBON
1276 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(a->s->window));
1277#else
1278 QDProcsPtr gp = a->s->window->grafProcs;
1279#endif
d082ac49 1280
d082ac49 1281 bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
4d1565a4 1282 reverse = a->attr & ATTR_REVERSE;
d082ac49 1283
4d1565a4 1284 if (depth == 1 && (a->attr & TATTR_ACTCURS))
1285 reverse = !reverse;
d082ac49 1286
4d1565a4 1287 if (HAVE_COLOR_QD()) {
1288 if (depth > 2) {
37ca32ed 1289 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1290 fgcolour = (fgcolour & 0xF) * 2 + (fgcolour & 0x10 ? 1 : 0);
22afb0a5 1291 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
37ca32ed 1292 bgcolour = (bgcolour & 0xF) * 2 + (bgcolour & 0x10 ? 1 : 0);
d082ac49 1293 } else {
4d1565a4 1294 /*
1295 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1296 * boldens the background, because that's all we can do.
1297 */
1298 fgcolour = bright ? DEFAULT_FG_BOLD : DEFAULT_FG;
1299 bgcolour = DEFAULT_BG;
d082ac49 1300 }
4d1565a4 1301 if (reverse) {
1302 tmp = fgcolour;
1303 fgcolour = bgcolour;
1304 bgcolour = tmp;
1305 }
1306 if (bright && depth > 2)
37ca32ed 1307 fgcolour |= 1;
4d1565a4 1308 if ((a->attr & TATTR_ACTCURS) && depth > 1) {
e0cbe032 1309 fgcolour = CURSOR_FG;
d082ac49 1310 bgcolour = CURSOR_BG;
d082ac49 1311 }
1312 PmForeColor(fgcolour);
1313 PmBackColor(bgcolour);
4d1565a4 1314 } else { /* No Color Quickdraw */
1315 /* XXX This should be done with a _little_ more configurability */
1316 if (reverse) {
1317 ForeColor(blackColor);
1318 BackColor(whiteColor);
1319 } else {
1320 ForeColor(whiteColor);
1321 BackColor(blackColor);
1322 }
d082ac49 1323 }
1324
4d1565a4 1325 EraseRect(&a->textrect);
c3aef739 1326 switch (a->lattr & LATTR_MODE) {
1327 case LATTR_NORM:
1328 case LATTR_WIDE:
1329 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
1330 break;
1331 case LATTR_TOP:
1332 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent * 2);
1333 break;
1334 case LATTR_BOT:
1335 MoveTo(a->textrect.left,
1336 a->textrect.top - a->s->font_height + a->s->font_ascent * 2);
1337 break;
1338 }
d082ac49 1339 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1d4d52ce 1340 if (gp != NULL)
1341 InvokeQDTextUPP(a->len, a->text, a->numer, a->denom, gp->textProc);
fd7d8b47 1342 else
1343 StdText(a->len, a->text, a->numer, a->denom);
d082ac49 1344
1345 if (a->attr & TATTR_PASCURS) {
1346 PenNormal();
1347 switch (depth) {
1348 case 1:
1349 PenMode(patXor);
1350 break;
1351 default:
1352 PmForeColor(CURSOR_BG);
1353 break;
1354 }
1355 FrameRect(&a->textrect);
1356 }
1357}
1358
1359void do_cursor(Context ctx, int x, int y, char *text, int len,
1360 unsigned long attr, int lattr)
1361{
1362
e0cbe032 1363 do_text(ctx, x, y, text, len, attr, lattr);
d082ac49 1364}
1365
1366/*
1367 * Call from the terminal emulator to get its graphics context.
1368 * Should probably be called start_redraw or something.
1369 */
f854b482 1370void pre_paint(Session *s)
1371{
2647b103 1372 GDHandle gdh;
1373 Rect myrect, tmprect;
1d4d52ce 1374#if TARGET_API_MAC_CARBON
1375 RgnHandle visrgn;
1376#endif
d082ac49 1377
2647b103 1378 if (HAVE_COLOR_QD()) {
0a0fa8cd 1379 s->term->attr_mask = 0;
5a736db6 1380 SetPort((GrafPtr)GetWindowPort(s->window));
1d4d52ce 1381#if TARGET_API_MAC_CARBON
1382 visrgn = NewRgn();
1383 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1384 GetRegionBounds(visrgn, &myrect);
1385 DisposeRgn(visrgn);
1386#else
2647b103 1387 myrect = (*s->window->visRgn)->rgnBBox;
1d4d52ce 1388#endif
2647b103 1389 LocalToGlobal((Point *)&myrect.top);
1390 LocalToGlobal((Point *)&myrect.bottom);
1391 for (gdh = GetDeviceList();
1392 gdh != NULL;
1393 gdh = GetNextDevice(gdh)) {
1394 if (TestDeviceAttribute(gdh, screenDevice) &&
1395 TestDeviceAttribute(gdh, screenActive) &&
1396 SectRect(&(*gdh)->gdRect, &myrect, &tmprect)) {
1397 switch ((*(*gdh)->gdPMap)->pixelSize) {
1398 case 1:
1399 if (s->cfg.bold_colour)
0a0fa8cd 1400 s->term->attr_mask |= ~(ATTR_COLOURS |
1401 (s->cfg.bold_colour ? ATTR_BOLD : 0));
e373bb53 1402 break;
2647b103 1403 case 2:
e373bb53 1404 s->term->attr_mask |= ~ATTR_COLOURS;
1405 break;
1406 default:
1407 s->term->attr_mask = ~0;
1408 return; /* No point checking more screens. */
2647b103 1409 }
1410 }
1411 }
1412 } else
0a0fa8cd 1413 s->term->attr_mask = ~(ATTR_COLOURS |
2647b103 1414 (s->cfg.bold_colour ? ATTR_BOLD : 0));
d082ac49 1415}
1416
f854b482 1417Context get_ctx(void *frontend)
1418{
d082ac49 1419 Session *s = frontend;
1420
1421 pre_paint(s);
1422 return s;
1423}
1424
f854b482 1425void free_ctx(Context ctx)
1426{
d082ac49 1427
1428}
1429
d082ac49 1430/*
1431 * Presumably this does something in Windows
1432 */
f854b482 1433void post_paint(Session *s)
1434{
d082ac49 1435
1436}
1437
1438/*
1439 * Set the scroll bar position
1440 *
1441 * total is the line number of the bottom of the working screen
1442 * start is the line number of the top of the display
1443 * page is the length of the displayed page
1444 */
f854b482 1445void set_sbar(void *frontend, int total, int start, int page)
1446{
d082ac49 1447 Session *s = frontend;
1448
1449 /* We don't redraw until we've set everything up, to avoid glitches */
1d4d52ce 1450 SetControlMinimum(s->scrollbar, 0);
1451 SetControlMaximum(s->scrollbar, total - page);
d082ac49 1452 SetControlValue(s->scrollbar, start);
1fc898ea 1453#if !TARGET_CPU_68K
d082ac49 1454 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
1455 SetControlViewSize(s->scrollbar, page);
1456#endif
1457}
1458
1459void sys_cursor(void *frontend, int x, int y)
1460{
1461 /*
1462 * I think his is meaningless under Mac OS.
1463 */
1464}
1465
1466/*
1467 * This is still called when mode==BELL_VISUAL, even though the
1468 * visual bell is handled entirely within terminal.c, because we
1469 * may want to perform additional actions on any kind of bell (for
1470 * example, taskbar flashing in Windows).
1471 */
1472void beep(void *frontend, int mode)
1473{
1474 if (mode != BELL_VISUAL)
1475 SysBeep(30);
1476 /*
1477 * XXX We should indicate the relevant window and/or use the
1478 * Notification Manager
1479 */
1480}
1481
1482int char_width(Context ctx, int uc)
1483{
1484 /*
1485 * Until we support exciting character-set stuff, assume all chars are
1486 * single-width.
1487 */
1488 return 1;
1489}
1490
1491/*
1492 * Set icon string -- a no-op here (Windowshade?)
1493 */
1494void set_icon(void *frontend, char *icon) {
1495 Session *s = frontend;
1496
1497}
1498
1499/*
1500 * Set the window title
1501 */
f854b482 1502void set_title(void *frontend, char *title)
1503{
d082ac49 1504 Session *s = frontend;
1505 Str255 mactitle;
1506
7770ad29 1507 c2pstrcpy(mactitle, title);
d082ac49 1508 SetWTitle(s->window, mactitle);
1509}
1510
1511/*
1512 * set or clear the "raw mouse message" mode
1513 */
1514void set_raw_mouse_mode(void *frontend, int activate)
1515{
1516 Session *s = frontend;
1517
1518 s->raw_mouse = activate;
1519 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1520}
1521
1522/*
1523 * Resize the window at the emulator's request
1524 */
8ff3f021 1525void request_resize(void *frontend, int w, int h)
1526{
d082ac49 1527 Session *s = frontend;
8ff3f021 1528 RgnHandle grayrgn;
1529 Rect graybox;
1530 int wlim, hlim;
d082ac49 1531
8ff3f021 1532 /* Arbitrarily clip to the size of the desktop. */
1533 grayrgn = GetGrayRgn();
1534#if TARGET_API_MAC_CARBON
1535 GetRegionBounds(grayrgn, &graybox);
1536#else
1537 graybox = (*grayrgn)->rgnBBox;
1538#endif
1539 wlim = (graybox.right - graybox.left) / s->font_width;
1540 hlim = (graybox.bottom - graybox.top) / s->font_height;
1541 if (w > wlim) w = wlim;
1542 if (h > hlim) h = hlim;
97c511f6 1543 term_size(s->term, h, w, s->cfg.savelines);
d082ac49 1544 mac_initfont(s);
1545}
1546
1547/*
1548 * Iconify (actually collapse) the window at the emulator's request.
1549 */
1550void set_iconic(void *frontend, int iconic)
1551{
1552 Session *s = frontend;
1553 UInt32 features;
1554
1555 if (mac_gestalts.apprvers >= 0x0100 &&
1556 GetWindowFeatures(s->window, &features) == noErr &&
1557 (features & kWindowCanCollapse))
1558 CollapseWindow(s->window, iconic);
1559}
1560
1561/*
1562 * Move the window in response to a server-side request.
1563 */
1564void move_window(void *frontend, int x, int y)
1565{
1566 Session *s = frontend;
1567
1568 MoveWindow(s->window, x, y, FALSE);
1569}
1570
1571/*
1572 * Move the window to the top or bottom of the z-order in response
1573 * to a server-side request.
1574 */
1575void set_zorder(void *frontend, int top)
1576{
1577 Session *s = frontend;
1578
1579 /*
1580 * We also change the input focus to point to the topmost window,
1581 * since that's probably what the Human Interface Guidelines would
1582 * like us to do.
1583 */
1584 if (top)
1585 SelectWindow(s->window);
1586 else
1587 SendBehind(s->window, NULL);
1588}
1589
1590/*
1591 * Refresh the window in response to a server-side request.
1592 */
1593void refresh_window(void *frontend)
1594{
1595 Session *s = frontend;
1596
1597 term_invalidate(s->term);
1598}
1599
1600/*
1601 * Maximise or restore the window in response to a server-side
1602 * request.
1603 */
1604void set_zoomed(void *frontend, int zoomed)
1605{
1606 Session *s = frontend;
1607
1608 ZoomWindow(s->window, zoomed ? inZoomOut : inZoomIn, FALSE);
1609}
1610
1611/*
1612 * Report whether the window is iconic, for terminal reports.
1613 */
1614int is_iconic(void *frontend)
1615{
1616 Session *s = frontend;
1617 UInt32 features;
1618
1619 if (mac_gestalts.apprvers >= 0x0100 &&
1620 GetWindowFeatures(s->window, &features) == noErr &&
1621 (features & kWindowCanCollapse))
1622 return IsWindowCollapsed(s->window);
1623 return FALSE;
1624}
1625
1626/*
1627 * Report the window's position, for terminal reports.
1628 */
1629void get_window_pos(void *frontend, int *x, int *y)
1630{
1631 Session *s = frontend;
1d4d52ce 1632 Rect rect;
d082ac49 1633
1d4d52ce 1634#if TARGET_API_MAC_CARBON
1635 GetPortBounds(GetWindowPort(s->window), &rect);
1636#else
1637 rect = s->window->portRect;
1638#endif
1639 *x = rect.left;
1640 *y = rect.top;
d082ac49 1641}
1642
1643/*
1644 * Report the window's pixel size, for terminal reports.
1645 */
1646void get_window_pixels(void *frontend, int *x, int *y)
1647{
1648 Session *s = frontend;
1d4d52ce 1649 Rect rect;
d082ac49 1650
1d4d52ce 1651#if TARGET_API_MAC_CARBON
1652 GetPortBounds(GetWindowPort(s->window), &rect);
1653#else
1654 rect = s->window->portRect;
1655#endif
1656 *x = rect.right - rect.left;
1657 *y = rect.bottom - rect.top;
d082ac49 1658}
1659
1660/*
1661 * Return the window or icon title.
1662 */
1663char *get_window_title(void *frontend, int icon)
1664{
1665 Session *s = frontend;
b6dfe962 1666 Str255 ptitle;
1667 static char title[256];
d082ac49 1668
b6dfe962 1669 GetWTitle(s->window, ptitle);
1670 p2cstrcpy(title, ptitle);
1671 return title;
d082ac49 1672}
1673
1674/*
1675 * real_palette_set(): This does the actual palette-changing work on behalf
1676 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1677 * is doing a batch of updates.
1678 */
1679static void real_palette_set(Session *s, int n, int r, int g, int b)
1680{
1681 RGBColor col;
1682
1683 if (!HAVE_COLOR_QD())
1684 return;
1685 col.red = r * 0x0101;
1686 col.green = g * 0x0101;
1687 col.blue = b * 0x0101;
d082ac49 1688 SetEntryColor(s->palette, n, &col);
1689}
1690
1691/*
1692 * Set the logical palette. Called by the terminal emulator.
1693 */
f854b482 1694void palette_set(void *frontend, int n, int r, int g, int b)
1695{
d082ac49 1696 Session *s = frontend;
1697 static const int first[21] = {
1698 0, 2, 4, 6, 8, 10, 12, 14,
1699 1, 3, 5, 7, 9, 11, 13, 15,
e0cbe032 1700 16, 17, 18, 20, 21
d082ac49 1701 };
1702
1703 if (!HAVE_COLOR_QD())
1704 return;
1705 real_palette_set(s, first[n], r, g, b);
e0cbe032 1706 if (first[n] == 18)
d082ac49 1707 real_palette_set(s, first[n]+1, r, g, b);
1708 if (first[n] == DEFAULT_BG)
1709 mac_adjustwinbg(s);
1710 ActivatePalette(s->window);
1711}
1712
1713/*
1714 * Reset to the default palette
1715 */
f854b482 1716void palette_reset(void *frontend)
1717{
d082ac49 1718 Session *s = frontend;
1719 /* This maps colour indices in cfg to those used in our palette. */
1720 static const int ww[] = {
1721 6, 7, 8, 9, 10, 11, 12, 13,
1722 14, 15, 16, 17, 18, 19, 20, 21,
1723 0, 1, 2, 3, 4, 5
1724 };
1725 int i;
1726
1727 if (!HAVE_COLOR_QD())
1728 return;
1729
1730 assert(lenof(ww) == NCOLOURS);
1731
1732 for (i = 0; i < NCOLOURS; i++) {
1733 real_palette_set(s, i,
1734 s->cfg.colours[ww[i]][0],
1735 s->cfg.colours[ww[i]][1],
1736 s->cfg.colours[ww[i]][2]);
1737 }
1738 mac_adjustwinbg(s);
1739 ActivatePalette(s->window);
1740 /* Palette Manager will generate update events as required. */
1741}
1742
1743/*
1744 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1745 * for backward.)
1746 */
f854b482 1747void do_scroll(Context ctx, int topline, int botline, int lines)
1748{
341eb978 1749 Session *s = ctx;
d082ac49 1750 Rect r;
11ddab61 1751 RgnHandle scrollrgn = NewRgn();
1752 RgnHandle movedupdate = NewRgn();
1753 RgnHandle update = NewRgn();
1754 Point g2l = { 0, 0 };
d082ac49 1755
5a736db6 1756 SetPort((GrafPtr)GetWindowPort(s->window));
11ddab61 1757
1758 /*
1759 * Work out the part of the update region that will scrolled by
1760 * this operation.
1761 */
1762 if (lines > 0)
1763 SetRectRgn(scrollrgn, 0, (topline + lines) * s->font_height,
1764 s->term->cols * s->font_width,
1765 (botline + 1) * s->font_height);
1766 else
1767 SetRectRgn(scrollrgn, 0, topline * s->font_height,
1768 s->term->cols * s->font_width,
1769 (botline - lines + 1) * s->font_height);
1d4d52ce 1770#if TARGET_API_MAC_CARBON
1771 GetWindowRegion(s->window, kWindowUpdateRgn, movedupdate);
1772#else
1773 GetWindowUpdateRgn(s->window, movedupdate);
1774#endif
11ddab61 1775 GlobalToLocal(&g2l);
1776 OffsetRgn(movedupdate, g2l.h, g2l.v); /* Convert to local co-ords. */
1777 SectRgn(scrollrgn, movedupdate, movedupdate); /* Clip scrolled section. */
1d4d52ce 1778#if TARGET_API_MAC_CARBON
1779 ValidWindowRgn(s->window, movedupdate);
1780#else
11ddab61 1781 ValidRgn(movedupdate);
1d4d52ce 1782#endif
11ddab61 1783 OffsetRgn(movedupdate, 0, -lines * s->font_height); /* Scroll it. */
1784
1951eebc 1785 PenNormal();
d082ac49 1786 if (HAVE_COLOR_QD())
1787 PmBackColor(DEFAULT_BG);
fd7d8b47 1788 else
1789 BackColor(blackColor); /* XXX make configurable */
d082ac49 1790 SetRect(&r, 0, topline * s->font_height,
1791 s->term->cols * s->font_width, (botline + 1) * s->font_height);
1792 ScrollRect(&r, 0, - lines * s->font_height, update);
11ddab61 1793
1d4d52ce 1794#if TARGET_API_MAC_CARBON
1795 InvalWindowRgn(s->window, update);
1796 InvalWindowRgn(s->window, movedupdate);
1797#else
d082ac49 1798 InvalRgn(update);
11ddab61 1799 InvalRgn(movedupdate);
1d4d52ce 1800#endif
11ddab61 1801
1802 DisposeRgn(scrollrgn);
1803 DisposeRgn(movedupdate);
d082ac49 1804 DisposeRgn(update);
1805}
1806
d082ac49 1807/* Dummy routine, only required in plink. */
1808void ldisc_update(void *frontend, int echo, int edit)
1809{
1810}
1811
1812/*
1813 * Mac PuTTY doesn't support printing yet.
1814 */
1815printer_job *printer_start_job(char *printer)
1816{
1817
1818 return NULL;
1819}
1820
1821void printer_job_data(printer_job *pj, void *data, int len)
1822{
1823}
1824
1825void printer_finish_job(printer_job *pj)
1826{
1827}
1828
1829void frontend_keypress(void *handle)
1830{
1831 /*
1832 * Keypress termination in non-Close-On-Exit mode is not
1833 * currently supported in PuTTY proper, because the window
1834 * always has a perfectly good Close button anyway. So we do
1835 * nothing here.
1836 */
1837 return;
1838}
1839
1840/*
1841 * Ask whether to wipe a session log file before writing to it.
1842 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1843 */
9a30e26b 1844int askappend(void *frontend, Filename filename)
d082ac49 1845{
1846
1847 /* FIXME: not implemented yet. */
1848 return 2;
1849}
1850
1e5bde54 1851int from_backend(void *frontend, int is_stderr, const char *data, int len)
1852{
1853 Session *s = frontend;
1854
1855 return term_data(s->term, is_stderr, data, len);
1856}
1857
d082ac49 1858/*
1859 * Emacs magic:
1860 * Local Variables:
1861 * c-file-style: "simon"
1862 * End:
1863 */
1864