3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
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
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
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
30 * macterm.c -- Macintosh terminal front-end
35 #include <ControlDefinitions.h>
40 #include <MacMemory.h>
41 #include <MacWindows.h>
42 #include <MixedMode.h>
44 #include <Quickdraw.h>
45 #include <QuickdrawText.h>
46 #include <Resources.h>
50 #include <TextCommon.h>
51 #include <ToolUtils.h>
52 #include <UnicodeConverter.h>
66 #define DEFAULT_FG 256
67 #define DEFAULT_FG_BOLD 257
68 #define DEFAULT_BG 258
69 #define DEFAULT_BG_BOLD 259
73 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
75 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
78 static void mac_initfont(Session *);
79 static pascal OSStatus uni_to_font_fallback(UniChar *, ByteCount, ByteCount *,
80 TextPtr, ByteCount, ByteCount *,
82 ConstUnicodeMappingPtr);
83 static void mac_initpalette(Session *);
84 static void mac_adjustwinbg(Session *);
85 static void mac_adjustsize(Session *, int, int);
86 static void mac_drawgrowicon(Session *s);
87 static pascal void mac_growtermdraghook(void);
88 static pascal void mac_scrolltracker(ControlHandle, short);
89 static pascal void do_text_for_device(short, short, GDHandle, long);
90 static void text_click(Session *, EventRecord *);
91 static void mac_activateterm(WindowPtr, EventRecord *);
92 static void mac_adjusttermcursor(WindowPtr, Point, RgnHandle);
93 static void mac_adjusttermmenus(WindowPtr);
94 static void mac_updateterm(WindowPtr);
95 static void mac_clickterm(WindowPtr, EventRecord *);
96 static void mac_growterm(WindowPtr, EventRecord *);
97 static void mac_keyterm(WindowPtr, EventRecord *);
98 static void mac_menuterm(WindowPtr, short, short);
99 static void mac_closeterm(WindowPtr);
101 void pre_paint(Session *s);
102 void post_paint(Session *s);
104 void mac_startsession(Session *s)
113 * Select protocol. This is farmed out into a table in a
114 * separate file to enable an ssh-free variant.
117 for (i = 0; backends[i].backend != NULL; i++)
118 if (backends[i].protocol == s->cfg.protocol) {
119 s->back = backends[i].backend;
123 fatalbox("Unsupported protocol number found");
125 /* XXX: Own storage management? */
127 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
129 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
131 memset(wi, 0, sizeof(*wi));
133 wi->wtype = wTerminal;
134 wi->activate = &mac_activateterm;
135 wi->adjustcursor = &mac_adjusttermcursor;
136 wi->adjustmenus = &mac_adjusttermmenus;
137 wi->update = &mac_updateterm;
138 wi->click = &mac_clickterm;
139 wi->grow = &mac_growterm;
140 wi->key = &mac_keyterm;
141 wi->menu = &mac_menuterm;
142 wi->close = &mac_closeterm;
143 SetWRefCon(s->window, (long)wi);
144 s->scrollbar = GetNewControl(cVScroll, s->window);
145 s->term = term_init(&s->cfg, &s->ucsdata, s);
149 if (HAVE_COLOR_QD()) {
150 /* Set to FALSE to not get palette updates in the background. */
151 SetPalette(s->window, s->palette, TRUE);
152 ActivatePalette(s->window);
155 s->logctx = log_init(s->term, &s->cfg);
156 term_provide_logctx(s->term, s->logctx);
158 errmsg = s->back->init(s, &s->backhandle, &s->cfg, s->cfg.host,
159 s->cfg.port, &s->realhost, s->cfg.tcp_nodelay,
160 s->cfg.tcp_keepalives);
162 fatalbox("%s", errmsg);
163 s->back->provide_logctx(s->backhandle, s->logctx);
164 set_title(s, s->realhost);
166 term_provide_resize_fn(s->term, s->back->size, s->backhandle);
168 mac_adjustsize(s, s->cfg.height, s->cfg.width);
169 term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines);
171 s->ldisc = ldisc_create(&s->cfg, s->term, s->back, s->backhandle, s);
172 ldisc_send(s->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
174 ShowWindow(s->window);
178 s->next->prev = &s->next;
183 * Try to work out a horizontal scaling factor for the current font
184 * that will give a chracter width of wantwidth. Return it in numer
185 * and denom (suitable for passing to StdText()).
187 static void mac_workoutfontscale(Session *s, int wantwidth,
188 Point *numerp, Point *denomp)
190 Point numer, denom, tmpnumer, tmpdenom;
192 const char text = 'W';
194 #if TARGET_API_MAC_CARBON
195 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(s->window));
197 QDProcsPtr gp = s->window->grafProcs;
200 numer.v = denom.v = 1; /* always */
201 numer.h = denom.h = 1;
202 for (i = 0; i < 3; i++) {
206 gotwidth = InvokeQDTxMeasUPP(1, &text, &tmpnumer, &tmpdenom, &fi,
209 gotwidth = StdTxMeas(1, &text, &tmpnumer, &tmpdenom, &fi);
210 /* The result of StdTxMeas must be scaled by the factors it returns. */
211 gotwidth = FixRound(FixMul(gotwidth << 16,
212 FixRatio(tmpnumer.h, tmpdenom.h)));
213 if (gotwidth == wantwidth)
215 numer.h *= wantwidth;
222 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
224 static void mac_initfont(Session *s)
230 SetPort((GrafPtr)GetWindowPort(s->window));
231 GetFNum(s->cfg.font.name, &s->fontnum);
232 TextFont(s->fontnum);
233 TextFace(s->cfg.font.face);
234 TextSize(s->cfg.font.size);
236 s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
237 s->font_ascent = fi.ascent;
238 s->font_leading = fi.leading;
239 s->font_height = s->font_ascent + fi.descent + s->font_leading;
240 mac_workoutfontscale(s, s->font_width,
241 &s->font_stdnumer, &s->font_stddenom);
242 mac_workoutfontscale(s, s->font_width * 2,
243 &s->font_widenumer, &s->font_widedenom);
244 TextSize(s->cfg.font.size * 2);
245 mac_workoutfontscale(s, s->font_width * 2,
246 &s->font_bignumer, &s->font_bigdenom);
247 TextSize(s->cfg.font.size);
248 if (!s->cfg.bold_colour) {
250 s->font_boldadjust = s->font_width - CharWidth('W');
252 s->font_boldadjust = 0;
254 if (s->uni_to_font != NULL)
255 DisposeUnicodeToTextInfo(&s->uni_to_font);
256 if (mac_gestalts.encvvers != 0 &&
257 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare,
258 kTextLanguageDontCare,
259 kTextRegionDontCare, s->cfg.font.name,
261 CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) {
262 if (uni_to_font_fallback_upp == NULL)
263 uni_to_font_fallback_upp =
264 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback);
265 fbflags = kUnicodeFallbackCustomOnly;
266 if (mac_gestalts.uncvattr & kTECAddFallbackInterruptMask)
267 fbflags |= kUnicodeFallbackInterruptSafeMask;
268 if (SetFallbackUnicodeToText(s->uni_to_font,
269 uni_to_font_fallback_upp, fbflags, NULL) != noErr) {
270 DisposeUnicodeToTextInfo(&s->uni_to_font);
277 s->uni_to_font = NULL;
278 p2cstrcpy(cfontname, s->cfg.font.name);
280 charset_from_macenc(FontToScript(s->fontnum),
281 GetScriptManagerVariable(smRegionCode),
282 mac_gestalts.sysvers, cfontname);
285 mac_adjustsize(s, s->term->rows, s->term->cols);
288 static pascal OSStatus uni_to_font_fallback(UniChar *ucp,
289 ByteCount ilen, ByteCount *iusedp, TextPtr obuf, ByteCount olen,
290 ByteCount *ousedp, LogicalAddress cookie, ConstUnicodeMappingPtr mapping)
294 return kTECOutputBufferFullStatus;
296 * What I'd _like_ to do here is to somehow generate the
297 * missing-character glyph that every font is required to have.
298 * Unfortunately (and somewhat surprisingly), I can't find any way
299 * to actually ask for it explicitly. Bah.
308 * Called every time round the event loop.
310 void mac_pollterm(void)
314 for (s = sesslist; s != NULL; s = s->next) {
315 term_update(s->term);
320 * To be called whenever the window size changes.
321 * rows and cols should be desired values.
322 * It's assumed the terminal emulator will be informed, and will set rows
325 static void mac_adjustsize(Session *s, int newrows, int newcols) {
326 int winwidth, winheight;
329 extraforscroll=s->cfg.scrollbar ? 15 : 0;
330 winwidth = newcols * s->font_width + extraforscroll;
331 winheight = newrows * s->font_height;
332 SizeWindow(s->window, winwidth, winheight, true);
333 if (s->cfg.scrollbar) {
334 HideControl(s->scrollbar);
335 MoveControl(s->scrollbar, winwidth - extraforscroll, -1);
336 SizeControl(s->scrollbar, extraforscroll + 1, winheight - 13);
337 ShowControl(s->scrollbar);
342 static void mac_initpalette(Session *s)
345 if (!HAVE_COLOR_QD())
348 * Most colours should be inhibited on 2bpp displays.
349 * Palette manager documentation suggests inhibiting all tolerant colours
350 * on greyscale displays.
352 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
353 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
354 #define PM_TOLERANCE 0x2000
355 s->palette = NewPalette(262, NULL, PM_NORMAL, PM_TOLERANCE);
356 if (s->palette == NULL)
357 fatalbox("Unable to create palette");
358 /* In 2bpp, these are the colours we want most. */
359 SetEntryUsage(s->palette, DEFAULT_BG,
360 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
361 SetEntryUsage(s->palette, DEFAULT_FG,
362 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
363 SetEntryUsage(s->palette, DEFAULT_FG_BOLD,
364 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
365 SetEntryUsage(s->palette, CURSOR_BG,
366 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
371 * Set the background colour of the window correctly. Should be
372 * called whenever the default background changes.
374 static void mac_adjustwinbg(Session *s)
377 if (!HAVE_COLOR_QD())
380 if (mac_gestalts.windattr & gestaltWindowMgrPresent)
381 SetWindowContentColor(s->window,
382 &(*s->palette)->pmInfo[DEFAULT_BG].ciRGB);
386 #if !TARGET_API_MAC_CARBON
387 if (s->wctab == NULL)
388 s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
389 if (s->wctab == NULL)
390 return; /* do without */
391 (*s->wctab)->wCSeed = 0;
392 (*s->wctab)->wCReserved = 0;
393 (*s->wctab)->ctSize = 0;
394 (*s->wctab)->ctTable[0].value = wContentColor;
395 (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
396 SetWinColor(s->window, s->wctab);
402 * Set the cursor shape correctly
404 static void mac_adjusttermcursor(WindowPtr window, Point mouse,
408 ControlHandle control;
411 #if TARGET_API_MAC_CARBON
417 SetPort((GrafPtr)GetWindowPort(window));
418 s = mac_windowsession(window);
419 GlobalToLocal(&mouse);
420 part = FindControl(mouse, window, &control);
421 if (control == s->scrollbar) {
422 #if TARGET_API_MAC_CARBON
423 SetCursor(GetQDGlobalsArrow(&arrow));
424 RectRgn(cursrgn, GetControlBounds(s->scrollbar, &rect));
426 SetCursor(&qd.arrow);
427 RectRgn(cursrgn, &(*s->scrollbar)->contrlRect);
430 x = mouse.h / s->font_width;
431 y = mouse.v / s->font_height;
433 #if TARGET_API_MAC_CARBON
434 SetCursor(GetQDGlobalsArrow(&arrow));
436 SetCursor(&qd.arrow);
439 SetCursor(*GetCursor(iBeamCursor));
440 /* Ask for shape changes if we leave this character cell. */
441 SetRectRgn(cursrgn, x * s->font_width, y * s->font_height,
442 (x + 1) * s->font_width, (y + 1) * s->font_height);
444 #if TARGET_API_MAC_CARBON
446 GetPortVisibleRegion(GetWindowPort(window), visrgn);
447 SectRgn(cursrgn, visrgn, cursrgn);
450 SectRgn(cursrgn, window->visRgn, cursrgn);
455 * Enable/disable menu items based on the active terminal window.
457 #if TARGET_API_MAC_CARBON
458 #define DisableItem DisableMenuItem
459 #define EnableItem EnableMenuItem
461 static void mac_adjusttermmenus(WindowPtr window)
465 #if !TARGET_API_MAC_CARBON
469 s = mac_windowsession(window);
470 menu = GetMenuHandle(mFile);
471 DisableItem(menu, iSave); /* XXX enable if modified */
472 EnableItem(menu, iSaveAs);
473 EnableItem(menu, iChange);
474 EnableItem(menu, iDuplicate);
475 menu = GetMenuHandle(mEdit);
477 DisableItem(menu, iUndo);
478 DisableItem(menu, iCut);
479 if (1/*s->term->selstate == SELECTED*/)
480 EnableItem(menu, iCopy);
482 DisableItem(menu, iCopy);
483 #if TARGET_API_MAC_CARBON
486 if (GetScrap(NULL, kScrapFlavorTypeText, &offset) == noTypeErr)
488 DisableItem(menu, iPaste);
490 EnableItem(menu, iPaste);
491 DisableItem(menu, iClear);
492 EnableItem(menu, iSelectAll);
493 menu = GetMenuHandle(mWindow);
495 EnableItem(menu, iShowEventLog);
498 static void mac_menuterm(WindowPtr window, short menu, short item)
502 s = mac_windowsession(window);
510 term_do_paste(s->term);
524 static void mac_clickterm(WindowPtr window, EventRecord *event)
528 ControlHandle control;
530 static ControlActionUPP mac_scrolltracker_upp = NULL;
532 s = mac_windowsession(window);
533 SetPort((GrafPtr)GetWindowPort(window));
534 mouse = event->where;
535 GlobalToLocal(&mouse);
536 part = FindControl(mouse, window, &control);
537 if (control == s->scrollbar) {
539 case kControlIndicatorPart:
540 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
541 term_scroll(s->term, +1, GetControlValue(control));
543 case kControlUpButtonPart:
544 case kControlDownButtonPart:
545 case kControlPageUpPart:
546 case kControlPageDownPart:
547 if (mac_scrolltracker_upp == NULL)
548 mac_scrolltracker_upp =
549 NewControlActionUPP(&mac_scrolltracker);
550 TrackControl(control, mouse, mac_scrolltracker_upp);
554 text_click(s, event);
558 static void text_click(Session *s, EventRecord *event)
562 static UInt32 lastwhen = 0;
563 static Session *lastsess = NULL;
564 static int lastrow = -1, lastcol = -1;
565 static Mouse_Action lastact = MA_NOTHING;
567 SetPort((GrafPtr)GetWindowPort(s->window));
568 localwhere = event->where;
569 GlobalToLocal(&localwhere);
571 col = PTOCC(localwhere.h);
572 row = PTOCR(localwhere.v);
573 if (event->when - lastwhen < GetDblTime() &&
574 row == lastrow && col == lastcol && s == lastsess)
575 lastact = (lastact == MA_CLICK ? MA_2CLK :
576 lastact == MA_2CLK ? MA_3CLK :
577 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
580 term_mouse(s->term, MBT_LEFT,
581 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
582 lastact, col, row, event->modifiers & shiftKey,
583 event->modifiers & controlKey, event->modifiers & optionKey);
587 while (StillDown()) {
588 GetMouse(&localwhere);
589 col = PTOCC(localwhere.h);
590 row = PTOCR(localwhere.v);
591 term_mouse(s->term, MBT_LEFT,
592 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
593 MA_DRAG, col, row, event->modifiers & shiftKey,
594 event->modifiers & controlKey,
595 event->modifiers & optionKey);
596 if (row > s->term->rows - 1)
597 term_scroll(s->term, 0, row - (s->term->rows - 1));
599 term_scroll(s->term, 0, row);
601 term_mouse(s->term, MBT_LEFT,
602 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
603 MA_RELEASE, col, row, event->modifiers & shiftKey,
604 event->modifiers & controlKey, event->modifiers & optionKey);
605 lastwhen = TickCount();
608 void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
610 #if !TARGET_API_MAC_CARBON
613 ByteCount iread, olen;
621 * See "Programming with the Text Encoding Conversion Manager"
622 * Appendix E for Unicode scrap conventions.
624 * XXX Maybe PICT scrap too.
626 if (ZeroScrap() != noErr)
628 PutScrap(len * sizeof(*data), kScrapFlavorTypeUnicode, data);
630 /* Replace LINE SEPARATORs with CR for TEXT output. */
631 for (i = 0; i < len; i++)
632 if (data[i] == 0x2028)
635 mactextbuf = snewn(len, char); /* XXX DBCS */
636 if (s->uni_to_font != NULL) {
637 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
639 kUnicodeUseFallbacksMask,
641 len, &iread, &olen, mactextbuf);
642 if (err != noErr && err != kTECUsedFallbacksStatus)
644 } else if (s->font_charset != CS_NONE) {
646 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
647 s->font_charset, NULL, ".", 1);
650 PutScrap(olen, kScrapFlavorTypeText, mactextbuf);
653 stsz = offsetof(StScrpRec, scrpStyleTab) + sizeof(ScrpSTElement);
654 stsc = smalloc(stsz);
655 stsc->scrpNStyles = 1;
656 stsc->scrpStyleTab[0].scrpStartChar = 0;
657 stsc->scrpStyleTab[0].scrpHeight = s->font_height;
658 stsc->scrpStyleTab[0].scrpAscent = s->font_ascent;
659 stsc->scrpStyleTab[0].scrpFont = s->fontnum;
660 stsc->scrpStyleTab[0].scrpFace = 0;
661 stsc->scrpStyleTab[0].scrpSize = s->cfg.font.size;
662 stsc->scrpStyleTab[0].scrpColor.red = 0;
663 stsc->scrpStyleTab[0].scrpColor.green = 0;
664 stsc->scrpStyleTab[0].scrpColor.blue = 0;
665 PutScrap(stsz, kScrapFlavorTypeTextStyle, stsc);
670 void get_clip(void *frontend, wchar_t **p, int *lenp)
672 #if TARGET_API_MAC_CARBON
675 Session *s = frontend;
676 static Handle h = NULL;
677 static wchar_t *data = NULL;
682 TextToUnicodeInfo scrap_to_uni;
683 ByteCount iread, olen;
697 if (GetScrap(NULL, kScrapFlavorTypeUnicode, &offset) > 0) {
701 GetScrap(h, kScrapFlavorTypeUnicode, &offset) / sizeof(**p);
704 } else if (GetScrap(NULL, kScrapFlavorTypeText, &offset) > 0) {
705 texth = NewHandle(0);
706 textlen = GetScrap(texth, kScrapFlavorTypeText, &offset);
708 data = snewn(textlen, wchar_t);
709 /* XXX should use 'styl' scrap if it's there. */
710 if (mac_gestalts.encvvers != 0 &&
711 UpgradeScriptInfoToTextEncoding(smSystemScript,
712 kTextLanguageDontCare,
713 kTextRegionDontCare, NULL,
715 CreateTextToUnicodeInfoByEncoding(enc, &scrap_to_uni) ==
717 err = ConvertFromTextToUnicode(scrap_to_uni, textlen,
718 *texth, 0, 0, NULL, NULL, NULL,
720 &iread, &olen, data);
721 DisposeTextToUnicodeInfo(&scrap_to_uni);
724 *lenp = olen / sizeof(**p);
731 charset_from_macenc(GetScriptManagerVariable(smSysScript),
732 GetScriptManagerVariable(smRegionCode),
733 mac_gestalts.sysvers, NULL);
734 if (charset != CS_NONE) {
736 *lenp = charset_to_unicode(&tptr, &textlen, data,
737 textlen * 2, charset, NULL,
742 DisposeHandle(texth);
751 static pascal void mac_scrolltracker(ControlHandle control, short part)
755 #if TARGET_API_MAC_CARBON
756 s = mac_windowsession(GetControlOwner(control));
758 s = mac_windowsession((*control)->contrlOwner);
761 case kControlUpButtonPart:
762 term_scroll(s->term, 0, -1);
764 case kControlDownButtonPart:
765 term_scroll(s->term, 0, +1);
767 case kControlPageUpPart:
768 term_scroll(s->term, 0, -(s->term->rows - 1));
770 case kControlPageDownPart:
771 term_scroll(s->term, 0, +(s->term->rows - 1));
776 static void mac_keyterm(WindowPtr window, EventRecord *event)
778 Session *s = mac_windowsession(window);
779 Key_Sym keysym = PK_NULL;
780 unsigned int mods = 0, flags = PKF_NUMLOCK;
784 ScriptCode key_script;
789 fprintf(stderr, "Got key event %08x\n", event->message);
792 /* No meta key yet -- that'll be rather fun. */
794 /* Keys that we handle locally */
795 if (event->modifiers & shiftKey) {
796 switch ((event->message & keyCodeMask) >> 8) {
797 case 0x74: /* shift-pageup */
798 term_scroll(s->term, 0, -(s->term->rows - 1));
800 case 0x79: /* shift-pagedown */
801 term_scroll(s->term, 0, +(s->term->rows - 1));
806 if (event->modifiers & shiftKey)
808 if (event->modifiers & controlKey)
810 if (event->what == autoKey)
813 /* Mac key events consist of a virtual key code and a character code. */
815 switch ((event->message & keyCodeMask) >> 8) {
816 case 0x24: keysym = PK_RETURN; break;
817 case 0x30: keysym = PK_TAB; break;
818 case 0x33: keysym = PK_BACKSPACE; break;
819 case 0x35: keysym = PK_ESCAPE; break;
821 case 0x7A: keysym = PK_F1; break;
822 case 0x78: keysym = PK_F2; break;
823 case 0x63: keysym = PK_F3; break;
824 case 0x76: keysym = PK_F4; break;
825 case 0x60: keysym = PK_F5; break;
826 case 0x61: keysym = PK_F6; break;
827 case 0x62: keysym = PK_F7; break;
828 case 0x64: keysym = PK_F8; break;
829 case 0x65: keysym = PK_F9; break;
830 case 0x6D: keysym = PK_F10; break;
831 case 0x67: keysym = PK_F11; break;
832 case 0x6F: keysym = PK_F12; break;
833 case 0x69: keysym = PK_F13; break;
834 case 0x6B: keysym = PK_F14; break;
835 case 0x71: keysym = PK_F15; break;
837 case 0x72: keysym = PK_INSERT; break;
838 case 0x73: keysym = PK_HOME; break;
839 case 0x74: keysym = PK_PAGEUP; break;
840 case 0x75: keysym = PK_DELETE; break;
841 case 0x77: keysym = PK_END; break;
842 case 0x79: keysym = PK_PAGEDOWN; break;
844 case 0x47: keysym = PK_PF1; break;
845 case 0x51: keysym = PK_PF2; break;
846 case 0x4B: keysym = PK_PF3; break;
847 case 0x43: keysym = PK_PF4; break;
848 case 0x4E: keysym = PK_KPMINUS; break;
849 case 0x45: keysym = PK_KPCOMMA; break;
850 case 0x41: keysym = PK_KPDECIMAL; break;
851 case 0x4C: keysym = PK_KPENTER; break;
852 case 0x52: keysym = PK_KP0; break;
853 case 0x53: keysym = PK_KP1; break;
854 case 0x54: keysym = PK_KP2; break;
855 case 0x55: keysym = PK_KP3; break;
856 case 0x56: keysym = PK_KP4; break;
857 case 0x57: keysym = PK_KP5; break;
858 case 0x58: keysym = PK_KP6; break;
859 case 0x59: keysym = PK_KP7; break;
860 case 0x5B: keysym = PK_KP8; break;
861 case 0x5C: keysym = PK_KP9; break;
863 case 0x7B: keysym = PK_LEFT; break;
864 case 0x7C: keysym = PK_RIGHT; break;
865 case 0x7D: keysym = PK_DOWN; break;
866 case 0x7E: keysym = PK_UP; break;
869 /* Map from key script to Unicode. */
870 txt[0] = event->message & charCodeMask;
871 key_script = GetScriptManagerVariable(smKeyScript);
873 if (mac_gestalts.encvvers != 0) {
874 static TextToUnicodeInfo key_to_uni = NULL;
875 static ScriptCode key_to_uni_script;
877 ByteCount iread, olen;
880 if (key_to_uni != NULL && key_to_uni_script != key_script)
881 DisposeTextToUnicodeInfo(&key_to_uni);
882 if (key_to_uni == NULL || key_to_uni_script != key_script) {
883 if (UpgradeScriptInfoToTextEncoding(key_script,
884 kTextLanguageDontCare,
885 kTextRegionDontCare, NULL,
887 CreateTextToUnicodeInfoByEncoding(enc, &key_to_uni) == noErr)
888 key_to_uni_script = key_script;
892 if (key_to_uni != NULL) {
893 err = ConvertFromTextToUnicode(key_to_uni, 1, txt,
894 (kUnicodeKeepInfoMask |
895 kUnicodeStringUnterminatedMask),
897 sizeof(utxt), &iread, &olen, utxt);
899 len = olen / sizeof(*utxt);
906 charset = charset_from_macenc(key_script,
907 GetScriptManagerVariable(smRegionCode),
908 mac_gestalts.sysvers, NULL);
909 if (charset != CS_NONE) {
910 len = charset_to_unicode(&tptr, &tlen, utxt, sizeof(utxt), charset,
914 term_key(s->term, keysym, utxt, len, mods, flags);
917 void request_paste(void *frontend)
919 Session *s = frontend;
922 * In the Mac OS, pasting is synchronous: we can read the
923 * clipboard with no difficulty, so request_paste() can just go
926 term_do_paste(s->term);
937 static void mac_growterm(WindowPtr window, EventRecord *event)
941 int newrows, newcols;
943 #if !TARGET_API_MAC_CARBON
944 DragGrayRgnUPP draghooksave;
949 s = mac_windowsession(window);
951 #if !TARGET_API_MAC_CARBON
952 draghooksave = LMGetDragHook();
953 growterm_state.oldmsg[0] = '\0';
954 growterm_state.zeromouse = event->where;
955 growterm_state.zeromouse.h -= s->term->cols * s->font_width;
956 growterm_state.zeromouse.v -= s->term->rows * s->font_height;
957 growterm_state.s = s;
960 BackColor(whiteColor);
961 ForeColor(blackColor);
962 TextFont(systemFont);
966 SetRect(&growterm_state.msgrect, 0, 0,
967 StringWidth("\p99999x99999") + 4, fi.ascent + fi.descent + 4);
968 SetPt(&growterm_state.msgorigin, 2, fi.ascent + 2);
969 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook));
972 SetRect(&limits, s->font_width + 15, s->font_height, SHRT_MAX, SHRT_MAX);
973 grow_result = GrowWindow(window, event->where, &limits);
975 #if !TARGET_API_MAC_CARBON
976 DisposeDragGrayRgnUPP(LMGetDragHook());
977 LMSetDragHook(draghooksave);
978 InvalRect(&growterm_state.msgrect);
983 if (grow_result != 0) {
984 newrows = HiWord(grow_result) / s->font_height;
985 newcols = (LoWord(grow_result) - 15) / s->font_width;
986 mac_adjustsize(s, newrows, newcols);
987 term_size(s->term, newrows, newcols, s->cfg.savelines);
988 s->cfg.height=s->term->rows;
989 s->cfg.width=s->term->cols;
993 #if !TARGET_API_MAC_CARBON
994 static pascal void mac_growtermdraghook(void)
996 Session *s = growterm_state.s;
1000 unsigned char pbuf[20];
1001 int newrows, newcols;
1004 newrows = (mouse.v - growterm_state.zeromouse.v) / s->font_height;
1005 if (newrows < 1) newrows = 1;
1006 newcols = (mouse.h - growterm_state.zeromouse.h) / s->font_width;
1007 if (newcols < 1) newcols = 1;
1008 sprintf(buf, "%dx%d", newcols, newrows);
1009 if (strcmp(buf, growterm_state.oldmsg) == 0)
1011 strcpy(growterm_state.oldmsg, buf);
1012 c2pstrcpy(pbuf, buf);
1015 SetPort(growterm_state.s->window);
1016 EraseRect(&growterm_state.msgrect);
1017 MoveTo(growterm_state.msgorigin.h, growterm_state.msgorigin.v);
1023 void mac_closeterm(WindowPtr window)
1025 Session *s = mac_windowsession(window);
1027 /* XXX warn on close */
1028 HideWindow(s->window);
1030 s->next->prev = s->prev;
1031 ldisc_free(s->ldisc);
1032 s->back->free(s->backhandle);
1033 log_free(s->logctx);
1034 if (s->uni_to_font != NULL)
1035 DisposeUnicodeToTextInfo(&s->uni_to_font);
1037 mac_freeeventlog(s);
1038 sfree((WinInfo *)GetWRefCon(s->window));
1039 DisposeWindow(s->window);
1040 DisposePalette(s->palette);
1044 static void mac_activateterm(WindowPtr window, EventRecord *event)
1047 Boolean active = (event->modifiers & activeFlag) != 0;
1049 s = mac_windowsession(window);
1050 term_set_focus(s->term, active);
1051 term_update(s->term);
1052 if (active && s->cfg.scrollbar)
1053 ShowControl(s->scrollbar);
1055 if (HAVE_COLOR_QD())
1056 PmBackColor(DEFAULT_BG);/* HideControl clears behind the control */
1058 BackColor(blackColor);
1059 HideControl(s->scrollbar);
1061 mac_drawgrowicon(s);
1064 static void mac_updateterm(WindowPtr window)
1068 #if TARGET_API_MAC_CARBON
1072 s = mac_windowsession(window);
1073 SetPort((GrafPtr)GetWindowPort(window));
1074 BeginUpdate(window);
1076 #if TARGET_API_MAC_CARBON
1078 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1079 GetRegionBounds(visrgn, &bbox);
1081 bbox = (*window->visRgn)->rgnBBox;
1083 term_paint(s->term, s, PTOCC(bbox.left), PTOCR(bbox.top),
1084 PTOCC(bbox.right), PTOCR(bbox.bottom), 1);
1085 /* Restore default colours in case the Window Manager uses them */
1086 if (HAVE_COLOR_QD()) {
1087 PmForeColor(DEFAULT_FG);
1088 PmBackColor(DEFAULT_BG);
1090 ForeColor(whiteColor);
1091 BackColor(blackColor);
1093 if (FrontWindow() != window)
1094 #if TARGET_API_MAC_CARBON
1095 EraseRect(GetControlBounds(s->scrollbar, &bbox));
1096 UpdateControls(window, visrgn);
1099 EraseRect(&(*s->scrollbar)->contrlRect);
1100 UpdateControls(window, window->visRgn);
1102 mac_drawgrowicon(s);
1107 static void mac_drawgrowicon(Session *s)
1112 SetPort((GrafPtr)GetWindowPort(s->window));
1114 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1115 * See Tech Note TB575 for details.
1117 #if TARGET_API_MAC_CARBON
1118 GetPortBounds(GetWindowPort(s->window), &clip);
1120 clip = s->window->portRect;
1122 clip.left = clip.right - 15;
1126 DrawGrowIcon(s->window);
1128 DisposeRgn(savergn);
1131 struct do_text_args {
1142 * Call from the terminal emulator to draw a bit of text
1144 * x and y are text row and column (zero-based)
1146 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
1147 unsigned long attr, int lattr)
1151 struct do_text_args a;
1152 RgnHandle textrgn, saveclip;
1153 #if TARGET_API_MAC_CARBON
1156 char mactextbuf[1024];
1157 wchar_t *unitextptr;
1159 ByteCount iread, olen;
1161 static DeviceLoopDrawingUPP do_text_for_device_upp = NULL;
1163 assert(len <= 1024);
1165 /* SGT, 2004-10-14: I don't know how to support combining characters
1166 * on the Mac. Hopefully the first person to fail this assertion will
1167 * know how to do it better than me... */
1168 assert(!(attr & TATTR_COMBINING));
1170 SetPort((GrafPtr)GetWindowPort(s->window));
1172 fontwidth = s->font_width;
1173 if ((lattr & LATTR_MODE) != LATTR_NORM)
1176 /* First check this text is relevant */
1177 a.textrect.top = y * s->font_height;
1178 a.textrect.bottom = (y + 1) * s->font_height;
1179 a.textrect.left = x * fontwidth;
1180 a.textrect.right = (x + len) * fontwidth;
1181 if (a.textrect.right > s->term->cols * s->font_width)
1182 a.textrect.right = s->term->cols * s->font_width;
1183 #if TARGET_API_MAC_CARBON
1185 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1186 if (!RectInRgn(&a.textrect, visrgn)) {
1192 if (!RectInRgn(&a.textrect, s->window->visRgn))
1196 if (s->uni_to_font != NULL) {
1197 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
1198 text, kUnicodeUseFallbacksMask,
1199 0, NULL, NULL, NULL,
1200 1024, &iread, &olen, mactextbuf);
1201 if (err != noErr && err != kTECUsedFallbacksStatus)
1203 } else if (s->font_charset != CS_NONE) {
1204 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1205 unitextptr = (wchar_t *)text;
1206 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
1207 s->font_charset, NULL, ".", 1);
1212 a.text = mactextbuf;
1216 switch (lattr & LATTR_MODE) {
1218 TextSize(s->cfg.font.size);
1219 a.numer = s->font_stdnumer;
1220 a.denom = s->font_stddenom;
1223 TextSize(s->cfg.font.size);
1224 a.numer = s->font_widenumer;
1225 a.denom = s->font_widedenom;
1229 TextSize(s->cfg.font.size * 2);
1230 a.numer = s->font_bignumer;
1231 a.denom = s->font_bigdenom;
1234 SetPort((GrafPtr)GetWindowPort(s->window));
1235 TextFont(s->fontnum);
1236 style = s->cfg.font.face;
1237 if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)
1239 if (attr & ATTR_UNDER)
1243 if (HAVE_COLOR_QD())
1245 SpaceExtra(s->font_boldadjust << 16);
1246 CharExtra(s->font_boldadjust << 16);
1251 saveclip = NewRgn();
1253 ClipRect(&a.textrect);
1255 RectRgn(textrgn, &a.textrect);
1256 if (HAVE_COLOR_QD()) {
1257 if (do_text_for_device_upp == NULL)
1258 do_text_for_device_upp =
1259 NewDeviceLoopDrawingUPP(&do_text_for_device);
1260 DeviceLoop(textrgn, do_text_for_device_upp, (long)&a, 0);
1262 do_text_for_device(1, 0, NULL, (long)&a);
1264 DisposeRgn(saveclip);
1265 DisposeRgn(textrgn);
1266 /* Tell the window manager about it in case this isn't an update */
1267 #if TARGET_API_MAC_CARBON
1268 ValidWindowRect(s->window, &a.textrect);
1270 ValidRect(&a.textrect);
1274 static pascal void do_text_for_device(short depth, short devflags,
1275 GDHandle device, long cookie)
1277 struct do_text_args *a = (struct do_text_args *)cookie;
1278 int bgcolour, fgcolour, bright, reverse, tmp;
1279 #if TARGET_API_MAC_CARBON
1280 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(a->s->window));
1282 QDProcsPtr gp = a->s->window->grafProcs;
1285 bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
1286 reverse = a->attr & ATTR_REVERSE;
1288 if (depth == 1 && (a->attr & TATTR_ACTCURS))
1291 if (HAVE_COLOR_QD()) {
1293 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1294 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1297 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1298 * boldens the background, because that's all we can do.
1300 fgcolour = bright ? DEFAULT_FG_BOLD : DEFAULT_FG;
1301 bgcolour = DEFAULT_BG;
1305 fgcolour = bgcolour;
1308 if (bright && depth > 2)
1309 if (fgcolour < 16) fgcolour |=8;
1310 else if (fgcolour >= 256) fgcolour |=1;
1311 if ((a->attr & TATTR_ACTCURS) && depth > 1) {
1312 fgcolour = CURSOR_FG;
1313 bgcolour = CURSOR_BG;
1315 PmForeColor(fgcolour);
1316 PmBackColor(bgcolour);
1317 } else { /* No Color Quickdraw */
1318 /* XXX This should be done with a _little_ more configurability */
1320 ForeColor(blackColor);
1321 BackColor(whiteColor);
1323 ForeColor(whiteColor);
1324 BackColor(blackColor);
1328 EraseRect(&a->textrect);
1329 switch (a->lattr & LATTR_MODE) {
1332 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
1335 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent * 2);
1338 MoveTo(a->textrect.left,
1339 a->textrect.top - a->s->font_height + a->s->font_ascent * 2);
1342 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1344 InvokeQDTextUPP(a->len, a->text, a->numer, a->denom, gp->textProc);
1346 StdText(a->len, a->text, a->numer, a->denom);
1348 if (a->attr & TATTR_PASCURS) {
1355 PmForeColor(CURSOR_BG);
1358 FrameRect(&a->textrect);
1362 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
1363 unsigned long attr, int lattr)
1366 do_text(ctx, x, y, text, len, attr, lattr);
1370 * Call from the terminal emulator to get its graphics context.
1371 * Should probably be called start_redraw or something.
1373 void pre_paint(Session *s)
1376 Rect myrect, tmprect;
1377 #if TARGET_API_MAC_CARBON
1381 if (HAVE_COLOR_QD()) {
1382 s->term->attr_mask = 0;
1383 SetPort((GrafPtr)GetWindowPort(s->window));
1384 #if TARGET_API_MAC_CARBON
1386 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1387 GetRegionBounds(visrgn, &myrect);
1390 myrect = (*s->window->visRgn)->rgnBBox;
1392 LocalToGlobal((Point *)&myrect.top);
1393 LocalToGlobal((Point *)&myrect.bottom);
1394 for (gdh = GetDeviceList();
1396 gdh = GetNextDevice(gdh)) {
1397 if (TestDeviceAttribute(gdh, screenDevice) &&
1398 TestDeviceAttribute(gdh, screenActive) &&
1399 SectRect(&(*gdh)->gdRect, &myrect, &tmprect)) {
1400 switch ((*(*gdh)->gdPMap)->pixelSize) {
1402 if (s->cfg.bold_colour)
1403 s->term->attr_mask |= ~(ATTR_COLOURS |
1404 (s->cfg.bold_colour ? ATTR_BOLD : 0));
1407 s->term->attr_mask |= ~ATTR_COLOURS;
1410 s->term->attr_mask = ~0;
1411 return; /* No point checking more screens. */
1416 s->term->attr_mask = ~(ATTR_COLOURS |
1417 (s->cfg.bold_colour ? ATTR_BOLD : 0));
1420 Context get_ctx(void *frontend)
1422 Session *s = frontend;
1428 void free_ctx(Context ctx)
1434 * Presumably this does something in Windows
1436 void post_paint(Session *s)
1442 * Set the scroll bar position
1444 * total is the line number of the bottom of the working screen
1445 * start is the line number of the top of the display
1446 * page is the length of the displayed page
1448 void set_sbar(void *frontend, int total, int start, int page)
1450 Session *s = frontend;
1452 /* We don't redraw until we've set everything up, to avoid glitches */
1453 SetControlMinimum(s->scrollbar, 0);
1454 SetControlMaximum(s->scrollbar, total - page);
1455 SetControlValue(s->scrollbar, start);
1457 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
1458 SetControlViewSize(s->scrollbar, page);
1462 void sys_cursor(void *frontend, int x, int y)
1465 * I think his is meaningless under Mac OS.
1470 * This is still called when mode==BELL_VISUAL, even though the
1471 * visual bell is handled entirely within terminal.c, because we
1472 * may want to perform additional actions on any kind of bell (for
1473 * example, taskbar flashing in Windows).
1475 void beep(void *frontend, int mode)
1477 if (mode != BELL_VISUAL)
1480 * XXX We should indicate the relevant window and/or use the
1481 * Notification Manager
1485 int char_width(Context ctx, int uc)
1488 * Until we support exciting character-set stuff, assume all chars are
1495 * Set icon string -- a no-op here (Windowshade?)
1497 void set_icon(void *frontend, char *icon) {
1498 Session *s = frontend;
1503 * Set the window title
1505 void set_title(void *frontend, char *title)
1507 Session *s = frontend;
1510 c2pstrcpy(mactitle, title);
1511 SetWTitle(s->window, mactitle);
1515 * set or clear the "raw mouse message" mode
1517 void set_raw_mouse_mode(void *frontend, int activate)
1519 Session *s = frontend;
1521 s->raw_mouse = activate;
1522 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1526 * Resize the window at the emulator's request
1528 void request_resize(void *frontend, int w, int h)
1530 Session *s = frontend;
1535 /* Arbitrarily clip to the size of the desktop. */
1536 grayrgn = GetGrayRgn();
1537 #if TARGET_API_MAC_CARBON
1538 GetRegionBounds(grayrgn, &graybox);
1540 graybox = (*grayrgn)->rgnBBox;
1542 wlim = (graybox.right - graybox.left) / s->font_width;
1543 hlim = (graybox.bottom - graybox.top) / s->font_height;
1544 if (w > wlim) w = wlim;
1545 if (h > hlim) h = hlim;
1546 term_size(s->term, h, w, s->cfg.savelines);
1551 * Iconify (actually collapse) the window at the emulator's request.
1553 void set_iconic(void *frontend, int iconic)
1555 Session *s = frontend;
1558 if (mac_gestalts.apprvers >= 0x0100 &&
1559 GetWindowFeatures(s->window, &features) == noErr &&
1560 (features & kWindowCanCollapse))
1561 CollapseWindow(s->window, iconic);
1565 * Move the window in response to a server-side request.
1567 void move_window(void *frontend, int x, int y)
1569 Session *s = frontend;
1571 MoveWindow(s->window, x, y, FALSE);
1575 * Move the window to the top or bottom of the z-order in response
1576 * to a server-side request.
1578 void set_zorder(void *frontend, int top)
1580 Session *s = frontend;
1583 * We also change the input focus to point to the topmost window,
1584 * since that's probably what the Human Interface Guidelines would
1588 SelectWindow(s->window);
1590 SendBehind(s->window, NULL);
1594 * Refresh the window in response to a server-side request.
1596 void refresh_window(void *frontend)
1598 Session *s = frontend;
1600 term_invalidate(s->term);
1604 * Maximise or restore the window in response to a server-side
1607 void set_zoomed(void *frontend, int zoomed)
1609 Session *s = frontend;
1611 ZoomWindow(s->window, zoomed ? inZoomOut : inZoomIn, FALSE);
1615 * Report whether the window is iconic, for terminal reports.
1617 int is_iconic(void *frontend)
1619 Session *s = frontend;
1622 if (mac_gestalts.apprvers >= 0x0100 &&
1623 GetWindowFeatures(s->window, &features) == noErr &&
1624 (features & kWindowCanCollapse))
1625 return IsWindowCollapsed(s->window);
1630 * Report the window's position, for terminal reports.
1632 void get_window_pos(void *frontend, int *x, int *y)
1634 Session *s = frontend;
1637 #if TARGET_API_MAC_CARBON
1638 GetPortBounds(GetWindowPort(s->window), &rect);
1640 rect = s->window->portRect;
1647 * Report the window's pixel size, for terminal reports.
1649 void get_window_pixels(void *frontend, int *x, int *y)
1651 Session *s = frontend;
1654 #if TARGET_API_MAC_CARBON
1655 GetPortBounds(GetWindowPort(s->window), &rect);
1657 rect = s->window->portRect;
1659 *x = rect.right - rect.left;
1660 *y = rect.bottom - rect.top;
1664 * Return the window or icon title.
1666 char *get_window_title(void *frontend, int icon)
1668 Session *s = frontend;
1670 static char title[256];
1672 GetWTitle(s->window, ptitle);
1673 p2cstrcpy(title, ptitle);
1678 * real_palette_set(): This does the actual palette-changing work on behalf
1679 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1680 * is doing a batch of updates.
1682 static void real_palette_set(Session *s, int n, int r, int g, int b)
1686 if (!HAVE_COLOR_QD())
1688 col.red = r * 0x0101;
1689 col.green = g * 0x0101;
1690 col.blue = b * 0x0101;
1691 SetEntryColor(s->palette, n, &col);
1695 * Set the logical palette. Called by the terminal emulator.
1697 void palette_set(void *frontend, int n, int r, int g, int b)
1699 Session *s = frontend;
1701 if (!HAVE_COLOR_QD())
1703 real_palette_set(s, n, r, g, b);
1704 if (n == DEFAULT_BG)
1707 ActivatePalette(s->window);
1711 * Reset to the default palette
1713 void palette_reset(void *frontend)
1715 Session *s = frontend;
1716 /* This maps colour indices in cfg to those used in our palette. */
1717 static const int ww[] = {
1718 256, 257, 258, 259, 260, 261,
1719 0, 8, 1, 9, 2, 10, 3, 11,
1720 4, 12, 5, 13, 6, 14, 7, 15
1725 if (!HAVE_COLOR_QD())
1728 for (i = 0; i < 22; i++) {
1730 real_palette_set(s,w,
1731 s->cfg.colours[i][0],
1732 s->cfg.colours[i][1],
1733 s->cfg.colours[i][2]);
1735 for (i = 0; i < 240; i++) {
1737 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1738 real_palette_set(s,i+16,
1743 int shade = i - 216;
1744 shade = (shade + 1) * 0xFF / (240 - 216 + 1);
1745 real_palette_set(s,i+16,shade,shade,shade);
1751 ActivatePalette(s->window);
1752 /* Palette Manager will generate update events as required. */
1756 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1759 void do_scroll(Context ctx, int topline, int botline, int lines)
1763 RgnHandle scrollrgn = NewRgn();
1764 RgnHandle movedupdate = NewRgn();
1765 RgnHandle update = NewRgn();
1766 Point g2l = { 0, 0 };
1768 SetPort((GrafPtr)GetWindowPort(s->window));
1771 * Work out the part of the update region that will scrolled by
1775 SetRectRgn(scrollrgn, 0, (topline + lines) * s->font_height,
1776 s->term->cols * s->font_width,
1777 (botline + 1) * s->font_height);
1779 SetRectRgn(scrollrgn, 0, topline * s->font_height,
1780 s->term->cols * s->font_width,
1781 (botline - lines + 1) * s->font_height);
1782 #if TARGET_API_MAC_CARBON
1783 GetWindowRegion(s->window, kWindowUpdateRgn, movedupdate);
1785 GetWindowUpdateRgn(s->window, movedupdate);
1787 GlobalToLocal(&g2l);
1788 OffsetRgn(movedupdate, g2l.h, g2l.v); /* Convert to local co-ords. */
1789 SectRgn(scrollrgn, movedupdate, movedupdate); /* Clip scrolled section. */
1790 #if TARGET_API_MAC_CARBON
1791 ValidWindowRgn(s->window, movedupdate);
1793 ValidRgn(movedupdate);
1795 OffsetRgn(movedupdate, 0, -lines * s->font_height); /* Scroll it. */
1798 if (HAVE_COLOR_QD())
1799 PmBackColor(DEFAULT_BG);
1801 BackColor(blackColor); /* XXX make configurable */
1802 SetRect(&r, 0, topline * s->font_height,
1803 s->term->cols * s->font_width, (botline + 1) * s->font_height);
1804 ScrollRect(&r, 0, - lines * s->font_height, update);
1806 #if TARGET_API_MAC_CARBON
1807 InvalWindowRgn(s->window, update);
1808 InvalWindowRgn(s->window, movedupdate);
1811 InvalRgn(movedupdate);
1814 DisposeRgn(scrollrgn);
1815 DisposeRgn(movedupdate);
1819 /* Dummy routine, only required in plink. */
1820 void ldisc_update(void *frontend, int echo, int edit)
1825 * Mac PuTTY doesn't support printing yet.
1827 printer_job *printer_start_job(char *printer)
1833 void printer_job_data(printer_job *pj, void *data, int len)
1837 void printer_finish_job(printer_job *pj)
1841 void frontend_keypress(void *handle)
1844 * Keypress termination in non-Close-On-Exit mode is not
1845 * currently supported in PuTTY proper, because the window
1846 * always has a perfectly good Close button anyway. So we do
1853 * Ask whether to wipe a session log file before writing to it.
1854 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1856 int askappend(void *frontend, Filename filename)
1859 /* FIXME: not implemented yet. */
1863 int from_backend(void *frontend, int is_stderr, const char *data, int len)
1865 Session *s = frontend;
1867 return term_data(s->term, is_stderr, data, len);
1873 * c-file-style: "simon"