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 do_text_internal(Context
, int, int, wchar_t *, int,
92 static void text_click(Session
*, EventRecord
*);
93 static void mac_activateterm(WindowPtr
, EventRecord
*);
94 static void mac_adjusttermcursor(WindowPtr
, Point
, RgnHandle
);
95 static void mac_adjusttermmenus(WindowPtr
);
96 static void mac_updateterm(WindowPtr
);
97 static void mac_clickterm(WindowPtr
, EventRecord
*);
98 static void mac_growterm(WindowPtr
, EventRecord
*);
99 static void mac_keyterm(WindowPtr
, EventRecord
*);
100 static void mac_menuterm(WindowPtr
, short, short);
101 static void mac_closeterm(WindowPtr
);
103 void pre_paint(Session
*s
);
104 void post_paint(Session
*s
);
106 void mac_startsession(Session
*s
)
115 * Select protocol. This is farmed out into a table in a
116 * separate file to enable an ssh-free variant.
119 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
120 if (backends
[i
].protocol
== s
->cfg
.protocol
) {
121 s
->back
= backends
[i
].backend
;
125 fatalbox("Unsupported protocol number found");
127 /* XXX: Own storage management? */
129 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
131 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
133 memset(wi
, 0, sizeof(*wi
));
135 wi
->wtype
= wTerminal
;
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
;
145 SetWRefCon(s
->window
, (long)wi
);
146 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
147 s
->term
= term_init(&s
->cfg
, &s
->ucsdata
, 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
);
157 s
->logctx
= log_init(s
->term
, &s
->cfg
);
158 term_provide_logctx(s
->term
, s
->logctx
);
160 errmsg
= s
->back
->init(s
, &s
->backhandle
, &s
->cfg
, s
->cfg
.host
,
161 s
->cfg
.port
, &s
->realhost
, s
->cfg
.tcp_nodelay
,
162 s
->cfg
.tcp_keepalives
);
164 fatalbox("%s", errmsg
);
165 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
166 set_title(s
, s
->realhost
);
168 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
170 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
171 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
173 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
174 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
176 ShowWindow(s
->window
);
180 s
->next
->prev
= &s
->next
;
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()).
189 static void mac_workoutfontscale(Session
*s
, int wantwidth
,
190 Point
*numerp
, Point
*denomp
)
192 Point numer
, denom
, tmpnumer
, tmpdenom
;
194 const char text
= 'W';
196 #if TARGET_API_MAC_CARBON
197 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(s
->window
));
199 QDProcsPtr gp
= s
->window
->grafProcs
;
202 numer
.v
= denom
.v
= 1; /* always */
203 numer
.h
= denom
.h
= 1;
204 for (i
= 0; i
< 3; i
++) {
208 gotwidth
= InvokeQDTxMeasUPP(1, &text
, &tmpnumer
, &tmpdenom
, &fi
,
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
)));
215 if (gotwidth
== wantwidth
)
217 numer
.h
*= wantwidth
;
224 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
226 static void mac_initfont(Session
*s
)
232 SetPort((GrafPtr
)GetWindowPort(s
->window
));
233 GetFNum(s
->cfg
.font
.name
, &s
->fontnum
);
234 TextFont(s
->fontnum
);
235 TextFace(s
->cfg
.font
.face
);
236 TextSize(s
->cfg
.font
.size
);
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
;
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
);
246 TextSize(s
->cfg
.font
.size
* 2);
247 mac_workoutfontscale(s
, s
->font_width
* 2,
248 &s
->font_bignumer
, &s
->font_bigdenom
);
249 TextSize(s
->cfg
.font
.size
);
250 if (!s
->cfg
.bold_colour
) {
252 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
254 s
->font_boldadjust
= 0;
256 if (s
->uni_to_font
!= NULL
)
257 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
258 if (mac_gestalts
.encvvers
!= 0 &&
259 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
260 kTextLanguageDontCare
,
261 kTextRegionDontCare
, s
->cfg
.font
.name
,
263 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) == noErr
) {
264 if (uni_to_font_fallback_upp
== NULL
)
265 uni_to_font_fallback_upp
=
266 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback
);
267 fbflags
= kUnicodeFallbackCustomOnly
;
268 if (mac_gestalts
.uncvattr
& kTECAddFallbackInterruptMask
)
269 fbflags
|= kUnicodeFallbackInterruptSafeMask
;
270 if (SetFallbackUnicodeToText(s
->uni_to_font
,
271 uni_to_font_fallback_upp
, fbflags
, NULL
) != noErr
) {
272 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
279 s
->uni_to_font
= NULL
;
280 p2cstrcpy(cfontname
, s
->cfg
.font
.name
);
282 charset_from_macenc(FontToScript(s
->fontnum
),
283 GetScriptManagerVariable(smRegionCode
),
284 mac_gestalts
.sysvers
, cfontname
);
287 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
290 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
291 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
292 ByteCount
*ousedp
, LogicalAddress cookie
, ConstUnicodeMappingPtr mapping
)
296 return kTECOutputBufferFullStatus
;
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.
310 * To be called whenever the window size changes.
311 * rows and cols should be desired values.
312 * It's assumed the terminal emulator will be informed, and will set rows
315 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
316 int winwidth
, winheight
;
319 extraforscroll
=s
->cfg
.scrollbar ?
15 : 0;
320 winwidth
= newcols
* s
->font_width
+ extraforscroll
;
321 winheight
= newrows
* s
->font_height
;
322 SizeWindow(s
->window
, winwidth
, winheight
, true);
323 if (s
->cfg
.scrollbar
) {
324 HideControl(s
->scrollbar
);
325 MoveControl(s
->scrollbar
, winwidth
- extraforscroll
, -1);
326 SizeControl(s
->scrollbar
, extraforscroll
+ 1, winheight
- 13);
327 ShowControl(s
->scrollbar
);
332 static void mac_initpalette(Session
*s
)
335 if (!HAVE_COLOR_QD())
338 * Most colours should be inhibited on 2bpp displays.
339 * Palette manager documentation suggests inhibiting all tolerant colours
340 * on greyscale displays.
342 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
343 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
344 #define PM_TOLERANCE 0x2000
345 s
->palette
= NewPalette(262, NULL
, PM_NORMAL
, PM_TOLERANCE
);
346 if (s
->palette
== NULL
)
347 fatalbox("Unable to create palette");
348 /* In 2bpp, these are the colours we want most. */
349 SetEntryUsage(s
->palette
, DEFAULT_BG
,
350 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
351 SetEntryUsage(s
->palette
, DEFAULT_FG
,
352 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
353 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
354 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
355 SetEntryUsage(s
->palette
, CURSOR_BG
,
356 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
361 * Set the background colour of the window correctly. Should be
362 * called whenever the default background changes.
364 static void mac_adjustwinbg(Session
*s
)
367 if (!HAVE_COLOR_QD())
370 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
371 SetWindowContentColor(s
->window
,
372 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
376 #if !TARGET_API_MAC_CARBON
377 if (s
->wctab
== NULL
)
378 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
379 if (s
->wctab
== NULL
)
380 return; /* do without */
381 (*s
->wctab
)->wCSeed
= 0;
382 (*s
->wctab
)->wCReserved
= 0;
383 (*s
->wctab
)->ctSize
= 0;
384 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
385 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
386 SetWinColor(s
->window
, s
->wctab
);
392 * Set the cursor shape correctly
394 static void mac_adjusttermcursor(WindowPtr window
, Point mouse
,
398 ControlHandle control
;
401 #if TARGET_API_MAC_CARBON
407 SetPort((GrafPtr
)GetWindowPort(window
));
408 s
= mac_windowsession(window
);
409 GlobalToLocal(&mouse
);
410 part
= FindControl(mouse
, window
, &control
);
411 if (control
== s
->scrollbar
) {
412 #if TARGET_API_MAC_CARBON
413 SetCursor(GetQDGlobalsArrow(&arrow
));
414 RectRgn(cursrgn
, GetControlBounds(s
->scrollbar
, &rect
));
416 SetCursor(&qd
.arrow
);
417 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
420 x
= mouse
.h
/ s
->font_width
;
421 y
= mouse
.v
/ s
->font_height
;
423 #if TARGET_API_MAC_CARBON
424 SetCursor(GetQDGlobalsArrow(&arrow
));
426 SetCursor(&qd
.arrow
);
429 SetCursor(*GetCursor(iBeamCursor
));
430 /* Ask for shape changes if we leave this character cell. */
431 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
432 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
434 #if TARGET_API_MAC_CARBON
436 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
437 SectRgn(cursrgn
, visrgn
, cursrgn
);
440 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
445 * Enable/disable menu items based on the active terminal window.
447 #if TARGET_API_MAC_CARBON
448 #define DisableItem DisableMenuItem
449 #define EnableItem EnableMenuItem
451 static void mac_adjusttermmenus(WindowPtr window
)
455 #if !TARGET_API_MAC_CARBON
459 s
= mac_windowsession(window
);
460 menu
= GetMenuHandle(mFile
);
461 DisableItem(menu
, iSave
); /* XXX enable if modified */
462 EnableItem(menu
, iSaveAs
);
463 EnableItem(menu
, iChange
);
464 EnableItem(menu
, iDuplicate
);
465 menu
= GetMenuHandle(mEdit
);
467 DisableItem(menu
, iUndo
);
468 DisableItem(menu
, iCut
);
469 if (1/*s->term->selstate == SELECTED*/)
470 EnableItem(menu
, iCopy
);
472 DisableItem(menu
, iCopy
);
473 #if TARGET_API_MAC_CARBON
476 if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) == noTypeErr
)
478 DisableItem(menu
, iPaste
);
480 EnableItem(menu
, iPaste
);
481 DisableItem(menu
, iClear
);
482 EnableItem(menu
, iSelectAll
);
483 menu
= GetMenuHandle(mWindow
);
485 EnableItem(menu
, iShowEventLog
);
488 static void mac_menuterm(WindowPtr window
, short menu
, short item
)
492 s
= mac_windowsession(window
);
500 term_do_paste(s
->term
);
514 static void mac_clickterm(WindowPtr window
, EventRecord
*event
)
518 ControlHandle control
;
520 static ControlActionUPP mac_scrolltracker_upp
= NULL
;
522 s
= mac_windowsession(window
);
523 SetPort((GrafPtr
)GetWindowPort(window
));
524 mouse
= event
->where
;
525 GlobalToLocal(&mouse
);
526 part
= FindControl(mouse
, window
, &control
);
527 if (control
== s
->scrollbar
) {
529 case kControlIndicatorPart
:
530 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
531 term_scroll(s
->term
, +1, GetControlValue(control
));
533 case kControlUpButtonPart
:
534 case kControlDownButtonPart
:
535 case kControlPageUpPart
:
536 case kControlPageDownPart
:
537 if (mac_scrolltracker_upp
== NULL
)
538 mac_scrolltracker_upp
=
539 NewControlActionUPP(&mac_scrolltracker
);
540 TrackControl(control
, mouse
, mac_scrolltracker_upp
);
544 text_click(s
, event
);
548 static void text_click(Session
*s
, EventRecord
*event
)
552 static UInt32 lastwhen
= 0;
553 static Session
*lastsess
= NULL
;
554 static int lastrow
= -1, lastcol
= -1;
555 static Mouse_Action lastact
= MA_NOTHING
;
557 SetPort((GrafPtr
)GetWindowPort(s
->window
));
558 localwhere
= event
->where
;
559 GlobalToLocal(&localwhere
);
561 col
= PTOCC(localwhere
.h
);
562 row
= PTOCR(localwhere
.v
);
563 if (event
->when
- lastwhen
< GetDblTime() &&
564 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
565 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
566 lastact
== MA_2CLK ? MA_3CLK
:
567 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
570 term_mouse(s
->term
, MBT_LEFT
,
571 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
572 lastact
, col
, row
, event
->modifiers
& shiftKey
,
573 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
577 while (StillDown()) {
578 GetMouse(&localwhere
);
579 col
= PTOCC(localwhere
.h
);
580 row
= PTOCR(localwhere
.v
);
581 term_mouse(s
->term
, MBT_LEFT
,
582 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
583 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
584 event
->modifiers
& controlKey
,
585 event
->modifiers
& optionKey
);
586 if (row
> s
->term
->rows
- 1)
587 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
589 term_scroll(s
->term
, 0, row
);
591 term_mouse(s
->term
, MBT_LEFT
,
592 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
593 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
594 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
595 lastwhen
= TickCount();
598 void write_clip(void *cookie
, wchar_t *data
, int *attr
, int len
, int must_deselect
)
600 #if !TARGET_API_MAC_CARBON
603 ByteCount iread
, olen
;
611 * See "Programming with the Text Encoding Conversion Manager"
612 * Appendix E for Unicode scrap conventions.
614 * XXX Maybe PICT scrap too.
616 if (ZeroScrap() != noErr
)
618 PutScrap(len
* sizeof(*data
), kScrapFlavorTypeUnicode
, data
);
620 /* Replace LINE SEPARATORs with CR for TEXT output. */
621 for (i
= 0; i
< len
; i
++)
622 if (data
[i
] == 0x2028)
625 mactextbuf
= snewn(len
, char); /* XXX DBCS */
626 if (s
->uni_to_font
!= NULL
) {
627 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
629 kUnicodeUseFallbacksMask
,
631 len
, &iread
, &olen
, mactextbuf
);
632 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
634 } else if (s
->font_charset
!= CS_NONE
) {
636 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
637 s
->font_charset
, NULL
, ".", 1);
640 PutScrap(olen
, kScrapFlavorTypeText
, mactextbuf
);
643 stsz
= offsetof(StScrpRec
, scrpStyleTab
) + sizeof(ScrpSTElement
);
644 stsc
= smalloc(stsz
);
645 stsc
->scrpNStyles
= 1;
646 stsc
->scrpStyleTab
[0].scrpStartChar
= 0;
647 stsc
->scrpStyleTab
[0].scrpHeight
= s
->font_height
;
648 stsc
->scrpStyleTab
[0].scrpAscent
= s
->font_ascent
;
649 stsc
->scrpStyleTab
[0].scrpFont
= s
->fontnum
;
650 stsc
->scrpStyleTab
[0].scrpFace
= 0;
651 stsc
->scrpStyleTab
[0].scrpSize
= s
->cfg
.font
.size
;
652 stsc
->scrpStyleTab
[0].scrpColor
.red
= 0;
653 stsc
->scrpStyleTab
[0].scrpColor
.green
= 0;
654 stsc
->scrpStyleTab
[0].scrpColor
.blue
= 0;
655 PutScrap(stsz
, kScrapFlavorTypeTextStyle
, stsc
);
660 void get_clip(void *frontend
, wchar_t **p
, int *lenp
)
662 #if TARGET_API_MAC_CARBON
665 Session
*s
= frontend
;
666 static Handle h
= NULL
;
667 static wchar_t *data
= NULL
;
672 TextToUnicodeInfo scrap_to_uni
;
673 ByteCount iread
, olen
;
687 if (GetScrap(NULL
, kScrapFlavorTypeUnicode
, &offset
) > 0) {
691 GetScrap(h
, kScrapFlavorTypeUnicode
, &offset
) / sizeof(**p
);
694 } else if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) > 0) {
695 texth
= NewHandle(0);
696 textlen
= GetScrap(texth
, kScrapFlavorTypeText
, &offset
);
698 data
= snewn(textlen
, wchar_t);
699 /* XXX should use 'styl' scrap if it's there. */
700 if (mac_gestalts
.encvvers
!= 0 &&
701 UpgradeScriptInfoToTextEncoding(smSystemScript
,
702 kTextLanguageDontCare
,
703 kTextRegionDontCare
, NULL
,
705 CreateTextToUnicodeInfoByEncoding(enc
, &scrap_to_uni
) ==
707 err
= ConvertFromTextToUnicode(scrap_to_uni
, textlen
,
708 *texth
, 0, 0, NULL
, NULL
, NULL
,
710 &iread
, &olen
, data
);
711 DisposeTextToUnicodeInfo(&scrap_to_uni
);
714 *lenp
= olen
/ sizeof(**p
);
721 charset_from_macenc(GetScriptManagerVariable(smSysScript
),
722 GetScriptManagerVariable(smRegionCode
),
723 mac_gestalts
.sysvers
, NULL
);
724 if (charset
!= CS_NONE
) {
726 *lenp
= charset_to_unicode(&tptr
, &textlen
, data
,
727 textlen
* 2, charset
, NULL
,
732 DisposeHandle(texth
);
741 static pascal void mac_scrolltracker(ControlHandle control
, short part
)
745 #if TARGET_API_MAC_CARBON
746 s
= mac_windowsession(GetControlOwner(control
));
748 s
= mac_windowsession((*control
)->contrlOwner
);
751 case kControlUpButtonPart
:
752 term_scroll(s
->term
, 0, -1);
754 case kControlDownButtonPart
:
755 term_scroll(s
->term
, 0, +1);
757 case kControlPageUpPart
:
758 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
760 case kControlPageDownPart
:
761 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
766 static void mac_keyterm(WindowPtr window
, EventRecord
*event
)
768 Session
*s
= mac_windowsession(window
);
769 Key_Sym keysym
= PK_NULL
;
770 unsigned int mods
= 0, flags
= PKF_NUMLOCK
;
774 ScriptCode key_script
;
779 fprintf(stderr
, "Got key event %08x\n", event
->message
);
782 /* No meta key yet -- that'll be rather fun. */
784 /* Keys that we handle locally */
785 if (event
->modifiers
& shiftKey
) {
786 switch ((event
->message
& keyCodeMask
) >> 8) {
787 case 0x74: /* shift-pageup */
788 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
790 case 0x79: /* shift-pagedown */
791 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
796 if (event
->modifiers
& shiftKey
)
798 if (event
->modifiers
& controlKey
)
800 if (event
->what
== autoKey
)
803 /* Mac key events consist of a virtual key code and a character code. */
805 switch ((event
->message
& keyCodeMask
) >> 8) {
806 case 0x24: keysym
= PK_RETURN
; break;
807 case 0x30: keysym
= PK_TAB
; break;
808 case 0x33: keysym
= PK_BACKSPACE
; break;
809 case 0x35: keysym
= PK_ESCAPE
; break;
811 case 0x7A: keysym
= PK_F1
; break;
812 case 0x78: keysym
= PK_F2
; break;
813 case 0x63: keysym
= PK_F3
; break;
814 case 0x76: keysym
= PK_F4
; break;
815 case 0x60: keysym
= PK_F5
; break;
816 case 0x61: keysym
= PK_F6
; break;
817 case 0x62: keysym
= PK_F7
; break;
818 case 0x64: keysym
= PK_F8
; break;
819 case 0x65: keysym
= PK_F9
; break;
820 case 0x6D: keysym
= PK_F10
; break;
821 case 0x67: keysym
= PK_F11
; break;
822 case 0x6F: keysym
= PK_F12
; break;
823 case 0x69: keysym
= PK_F13
; break;
824 case 0x6B: keysym
= PK_F14
; break;
825 case 0x71: keysym
= PK_F15
; break;
827 case 0x72: keysym
= PK_INSERT
; break;
828 case 0x73: keysym
= PK_HOME
; break;
829 case 0x74: keysym
= PK_PAGEUP
; break;
830 case 0x75: keysym
= PK_DELETE
; break;
831 case 0x77: keysym
= PK_END
; break;
832 case 0x79: keysym
= PK_PAGEDOWN
; break;
834 case 0x47: keysym
= PK_PF1
; break;
835 case 0x51: keysym
= PK_PF2
; break;
836 case 0x4B: keysym
= PK_PF3
; break;
837 case 0x43: keysym
= PK_PF4
; break;
838 case 0x4E: keysym
= PK_KPMINUS
; break;
839 case 0x45: keysym
= PK_KPCOMMA
; break;
840 case 0x41: keysym
= PK_KPDECIMAL
; break;
841 case 0x4C: keysym
= PK_KPENTER
; break;
842 case 0x52: keysym
= PK_KP0
; break;
843 case 0x53: keysym
= PK_KP1
; break;
844 case 0x54: keysym
= PK_KP2
; break;
845 case 0x55: keysym
= PK_KP3
; break;
846 case 0x56: keysym
= PK_KP4
; break;
847 case 0x57: keysym
= PK_KP5
; break;
848 case 0x58: keysym
= PK_KP6
; break;
849 case 0x59: keysym
= PK_KP7
; break;
850 case 0x5B: keysym
= PK_KP8
; break;
851 case 0x5C: keysym
= PK_KP9
; break;
853 case 0x7B: keysym
= PK_LEFT
; break;
854 case 0x7C: keysym
= PK_RIGHT
; break;
855 case 0x7D: keysym
= PK_DOWN
; break;
856 case 0x7E: keysym
= PK_UP
; break;
859 /* Map from key script to Unicode. */
860 txt
[0] = event
->message
& charCodeMask
;
861 key_script
= GetScriptManagerVariable(smKeyScript
);
863 if (mac_gestalts
.encvvers
!= 0) {
864 static TextToUnicodeInfo key_to_uni
= NULL
;
865 static ScriptCode key_to_uni_script
;
867 ByteCount iread
, olen
;
870 if (key_to_uni
!= NULL
&& key_to_uni_script
!= key_script
)
871 DisposeTextToUnicodeInfo(&key_to_uni
);
872 if (key_to_uni
== NULL
|| key_to_uni_script
!= key_script
) {
873 if (UpgradeScriptInfoToTextEncoding(key_script
,
874 kTextLanguageDontCare
,
875 kTextRegionDontCare
, NULL
,
877 CreateTextToUnicodeInfoByEncoding(enc
, &key_to_uni
) == noErr
)
878 key_to_uni_script
= key_script
;
882 if (key_to_uni
!= NULL
) {
883 err
= ConvertFromTextToUnicode(key_to_uni
, 1, txt
,
884 (kUnicodeKeepInfoMask
|
885 kUnicodeStringUnterminatedMask
),
887 sizeof(utxt
), &iread
, &olen
, utxt
);
889 len
= olen
/ sizeof(*utxt
);
896 charset
= charset_from_macenc(key_script
,
897 GetScriptManagerVariable(smRegionCode
),
898 mac_gestalts
.sysvers
, NULL
);
899 if (charset
!= CS_NONE
) {
900 len
= charset_to_unicode(&tptr
, &tlen
, utxt
, sizeof(utxt
), charset
,
904 term_key(s
->term
, keysym
, utxt
, len
, mods
, flags
);
907 void request_paste(void *frontend
)
909 Session
*s
= frontend
;
912 * In the Mac OS, pasting is synchronous: we can read the
913 * clipboard with no difficulty, so request_paste() can just go
916 term_do_paste(s
->term
);
927 static void mac_growterm(WindowPtr window
, EventRecord
*event
)
931 int newrows
, newcols
;
933 #if !TARGET_API_MAC_CARBON
934 DragGrayRgnUPP draghooksave
;
939 s
= mac_windowsession(window
);
941 #if !TARGET_API_MAC_CARBON
942 draghooksave
= LMGetDragHook();
943 growterm_state
.oldmsg
[0] = '\0';
944 growterm_state
.zeromouse
= event
->where
;
945 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
946 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
947 growterm_state
.s
= s
;
950 BackColor(whiteColor
);
951 ForeColor(blackColor
);
952 TextFont(systemFont
);
956 SetRect(&growterm_state
.msgrect
, 0, 0,
957 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
958 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
959 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
962 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
963 grow_result
= GrowWindow(window
, event
->where
, &limits
);
965 #if !TARGET_API_MAC_CARBON
966 DisposeDragGrayRgnUPP(LMGetDragHook());
967 LMSetDragHook(draghooksave
);
968 InvalRect(&growterm_state
.msgrect
);
973 if (grow_result
!= 0) {
974 newrows
= HiWord(grow_result
) / s
->font_height
;
975 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
976 mac_adjustsize(s
, newrows
, newcols
);
977 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
978 s
->cfg
.height
=s
->term
->rows
;
979 s
->cfg
.width
=s
->term
->cols
;
983 #if !TARGET_API_MAC_CARBON
984 static pascal void mac_growtermdraghook(void)
986 Session
*s
= growterm_state
.s
;
990 unsigned char pbuf
[20];
991 int newrows
, newcols
;
994 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
995 if (newrows
< 1) newrows
= 1;
996 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
997 if (newcols
< 1) newcols
= 1;
998 sprintf(buf
, "%dx%d", newcols
, newrows
);
999 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
1001 strcpy(growterm_state
.oldmsg
, buf
);
1002 c2pstrcpy(pbuf
, buf
);
1005 SetPort(growterm_state
.s
->window
);
1006 EraseRect(&growterm_state
.msgrect
);
1007 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
1013 void mac_closeterm(WindowPtr window
)
1016 Session
*s
= mac_windowsession(window
);
1018 if (s
->cfg
.warn_on_close
&& !s
->session_closed
) {
1019 ParamText("\pAre you sure you want to close this session?",
1021 alertret
=CautionAlert(wQuestion
, NULL
);
1022 if (alertret
== 2) {
1028 HideWindow(s
->window
);
1030 s
->next
->prev
= s
->prev
;
1032 ldisc_free(s
->ldisc
);
1034 s
->back
->free(s
->backhandle
);
1035 log_free(s
->logctx
);
1036 if (s
->uni_to_font
!= NULL
)
1037 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
1039 mac_freeeventlog(s
);
1040 sfree((WinInfo
*)GetWRefCon(s
->window
));
1041 DisposeWindow(s
->window
);
1042 DisposePalette(s
->palette
);
1046 static void mac_activateterm(WindowPtr window
, EventRecord
*event
)
1049 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1051 s
= mac_windowsession(window
);
1052 term_set_focus(s
->term
, active
);
1053 term_update(s
->term
);
1054 if (active
&& s
->cfg
.scrollbar
)
1055 ShowControl(s
->scrollbar
);
1057 if (HAVE_COLOR_QD())
1058 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
1060 BackColor(blackColor
);
1061 HideControl(s
->scrollbar
);
1063 mac_drawgrowicon(s
);
1066 static void mac_updateterm(WindowPtr window
)
1070 #if TARGET_API_MAC_CARBON
1074 s
= mac_windowsession(window
);
1075 SetPort((GrafPtr
)GetWindowPort(window
));
1076 BeginUpdate(window
);
1078 #if TARGET_API_MAC_CARBON
1080 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1081 GetRegionBounds(visrgn
, &bbox
);
1083 bbox
= (*window
->visRgn
)->rgnBBox
;
1085 term_paint(s
->term
, s
, PTOCC(bbox
.left
), PTOCR(bbox
.top
),
1086 PTOCC(bbox
.right
), PTOCR(bbox
.bottom
), 1);
1087 /* Restore default colours in case the Window Manager uses them */
1088 if (HAVE_COLOR_QD()) {
1089 PmForeColor(DEFAULT_FG
);
1090 PmBackColor(DEFAULT_BG
);
1092 ForeColor(whiteColor
);
1093 BackColor(blackColor
);
1095 if (FrontWindow() != window
)
1096 #if TARGET_API_MAC_CARBON
1097 EraseRect(GetControlBounds(s
->scrollbar
, &bbox
));
1098 UpdateControls(window
, visrgn
);
1101 EraseRect(&(*s
->scrollbar
)->contrlRect
);
1102 UpdateControls(window
, window
->visRgn
);
1104 mac_drawgrowicon(s
);
1109 static void mac_drawgrowicon(Session
*s
)
1114 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1116 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1117 * See Tech Note TB575 for details.
1119 #if TARGET_API_MAC_CARBON
1120 GetPortBounds(GetWindowPort(s
->window
), &clip
);
1122 clip
= s
->window
->portRect
;
1124 clip
.left
= clip
.right
- 15;
1128 DrawGrowIcon(s
->window
);
1130 DisposeRgn(savergn
);
1133 struct do_text_args
{
1144 * Call from the terminal emulator to draw a bit of text
1146 * x and y are text row and column (zero-based)
1148 static void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1149 unsigned long attr
, int lattr
)
1153 struct do_text_args a
;
1154 RgnHandle textrgn
, saveclip
;
1155 #if TARGET_API_MAC_CARBON
1158 char mactextbuf
[1024];
1159 wchar_t *unitextptr
;
1161 ByteCount iread
, olen
;
1163 static DeviceLoopDrawingUPP do_text_for_device_upp
= NULL
;
1165 assert(len
<= 1024);
1167 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1169 fontwidth
= s
->font_width
;
1170 if ((lattr
& LATTR_MODE
) != LATTR_NORM
)
1173 /* First check this text is relevant */
1174 a
.textrect
.top
= y
* s
->font_height
;
1175 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
1176 a
.textrect
.left
= x
* fontwidth
;
1177 a
.textrect
.right
= (x
+ len
) * fontwidth
;
1178 if (a
.textrect
.right
> s
->term
->cols
* s
->font_width
)
1179 a
.textrect
.right
= s
->term
->cols
* s
->font_width
;
1180 #if TARGET_API_MAC_CARBON
1182 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1183 if (!RectInRgn(&a
.textrect
, visrgn
)) {
1189 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
1193 if (s
->uni_to_font
!= NULL
) {
1194 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
1195 text
, kUnicodeUseFallbacksMask
,
1196 0, NULL
, NULL
, NULL
,
1197 1024, &iread
, &olen
, mactextbuf
);
1198 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
1200 } else if (s
->font_charset
!= CS_NONE
) {
1201 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1202 unitextptr
= (wchar_t *)text
;
1203 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
1204 s
->font_charset
, NULL
, ".", 1);
1209 a
.text
= mactextbuf
;
1213 switch (lattr
& LATTR_MODE
) {
1215 TextSize(s
->cfg
.font
.size
);
1216 a
.numer
= s
->font_stdnumer
;
1217 a
.denom
= s
->font_stddenom
;
1220 TextSize(s
->cfg
.font
.size
);
1221 a
.numer
= s
->font_widenumer
;
1222 a
.denom
= s
->font_widedenom
;
1226 TextSize(s
->cfg
.font
.size
* 2);
1227 a
.numer
= s
->font_bignumer
;
1228 a
.denom
= s
->font_bigdenom
;
1231 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1232 TextFont(s
->fontnum
);
1233 style
= s
->cfg
.font
.face
;
1234 if ((attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1236 if (attr
& ATTR_UNDER
)
1240 if (HAVE_COLOR_QD())
1242 SpaceExtra(s
->font_boldadjust
<< 16);
1243 CharExtra(s
->font_boldadjust
<< 16);
1248 saveclip
= NewRgn();
1250 ClipRect(&a
.textrect
);
1252 RectRgn(textrgn
, &a
.textrect
);
1253 if (HAVE_COLOR_QD()) {
1254 if (do_text_for_device_upp
== NULL
)
1255 do_text_for_device_upp
=
1256 NewDeviceLoopDrawingUPP(&do_text_for_device
);
1257 DeviceLoop(textrgn
, do_text_for_device_upp
, (long)&a
, 0);
1259 do_text_for_device(1, 0, NULL
, (long)&a
);
1261 DisposeRgn(saveclip
);
1262 DisposeRgn(textrgn
);
1263 /* Tell the window manager about it in case this isn't an update */
1264 #if TARGET_API_MAC_CARBON
1265 ValidWindowRect(s
->window
, &a
.textrect
);
1267 ValidRect(&a
.textrect
);
1272 * Wrapper that handles combining characters.
1274 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1275 unsigned long attr
, int lattr
)
1277 if (attr
& TATTR_COMBINING
) {
1278 unsigned long a
= 0;
1279 attr
&= ~TATTR_COMBINING
;
1281 do_text_internal(ctx
, x
, y
, text
, 1, attr
| a
, lattr
);
1283 a
= TATTR_COMBINING
;
1286 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1289 static pascal void do_text_for_device(short depth
, short devflags
,
1290 GDHandle device
, long cookie
)
1292 struct do_text_args
*a
= (struct do_text_args
*)cookie
;
1293 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1294 #if TARGET_API_MAC_CARBON
1295 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(a
->s
->window
));
1297 QDProcsPtr gp
= a
->s
->window
->grafProcs
;
1300 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1301 reverse
= a
->attr
& ATTR_REVERSE
;
1303 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1306 if (HAVE_COLOR_QD()) {
1308 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1309 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1312 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1313 * boldens the background, because that's all we can do.
1315 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1316 bgcolour
= DEFAULT_BG
;
1320 fgcolour
= bgcolour
;
1323 if (bright
&& depth
> 2)
1324 if (fgcolour
< 16) fgcolour
|=8;
1325 else if (fgcolour
>= 256) fgcolour
|=1;
1326 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1327 fgcolour
= CURSOR_FG
;
1328 bgcolour
= CURSOR_BG
;
1330 PmForeColor(fgcolour
);
1331 PmBackColor(bgcolour
);
1332 } else { /* No Color Quickdraw */
1333 /* XXX This should be done with a _little_ more configurability */
1335 ForeColor(blackColor
);
1336 BackColor(whiteColor
);
1338 ForeColor(whiteColor
);
1339 BackColor(blackColor
);
1343 if (!(a
->attr
& TATTR_COMBINING
))
1344 EraseRect(&a
->textrect
);
1345 switch (a
->lattr
& LATTR_MODE
) {
1348 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1351 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
* 2);
1354 MoveTo(a
->textrect
.left
,
1355 a
->textrect
.top
- a
->s
->font_height
+ a
->s
->font_ascent
* 2);
1358 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1360 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
, gp
->textProc
);
1362 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1364 if (a
->attr
& TATTR_PASCURS
) {
1371 PmForeColor(CURSOR_BG
);
1374 FrameRect(&a
->textrect
);
1378 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1379 unsigned long attr
, int lattr
)
1382 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1386 * Call from the terminal emulator to get its graphics context.
1387 * Should probably be called start_redraw or something.
1389 void pre_paint(Session
*s
)
1392 Rect myrect
, tmprect
;
1393 #if TARGET_API_MAC_CARBON
1397 if (HAVE_COLOR_QD()) {
1398 s
->term
->attr_mask
= 0;
1399 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1400 #if TARGET_API_MAC_CARBON
1402 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1403 GetRegionBounds(visrgn
, &myrect
);
1406 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1408 LocalToGlobal((Point
*)&myrect
.top
);
1409 LocalToGlobal((Point
*)&myrect
.bottom
);
1410 for (gdh
= GetDeviceList();
1412 gdh
= GetNextDevice(gdh
)) {
1413 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1414 TestDeviceAttribute(gdh
, screenActive
) &&
1415 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1416 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1418 if (s
->cfg
.bold_colour
)
1419 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1420 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1423 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1426 s
->term
->attr_mask
= ~0;
1427 return; /* No point checking more screens. */
1432 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1433 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1436 Context
get_ctx(void *frontend
)
1438 Session
*s
= frontend
;
1444 void free_ctx(Context ctx
)
1450 * Presumably this does something in Windows
1452 void post_paint(Session
*s
)
1458 * Set the scroll bar position
1460 * total is the line number of the bottom of the working screen
1461 * start is the line number of the top of the display
1462 * page is the length of the displayed page
1464 void set_sbar(void *frontend
, int total
, int start
, int page
)
1466 Session
*s
= frontend
;
1468 /* We don't redraw until we've set everything up, to avoid glitches */
1469 SetControlMinimum(s
->scrollbar
, 0);
1470 SetControlMaximum(s
->scrollbar
, total
- page
);
1471 SetControlValue(s
->scrollbar
, start
);
1473 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1474 SetControlViewSize(s
->scrollbar
, page
);
1478 void sys_cursor(void *frontend
, int x
, int y
)
1481 * I think his is meaningless under Mac OS.
1486 * This is still called when mode==BELL_VISUAL, even though the
1487 * visual bell is handled entirely within terminal.c, because we
1488 * may want to perform additional actions on any kind of bell (for
1489 * example, taskbar flashing in Windows).
1491 void do_beep(void *frontend
, int mode
)
1493 if (mode
!= BELL_VISUAL
)
1496 * XXX We should indicate the relevant window and/or use the
1497 * Notification Manager
1501 int char_width(Context ctx
, int uc
)
1504 * Until we support exciting character-set stuff, assume all chars are
1511 * Set icon string -- a no-op here (Windowshade?)
1513 void set_icon(void *frontend
, char *icon
) {
1514 Session
*s
= frontend
;
1519 * Set the window title
1521 void set_title(void *frontend
, char *title
)
1523 Session
*s
= frontend
;
1526 c2pstrcpy(mactitle
, title
);
1527 SetWTitle(s
->window
, mactitle
);
1531 * Used by backend to indicate busy-ness
1533 void set_busy_status(void *frontend
, int status
)
1535 /* FIXME do something */
1539 * set or clear the "raw mouse message" mode
1541 void set_raw_mouse_mode(void *frontend
, int activate
)
1543 Session
*s
= frontend
;
1545 s
->raw_mouse
= activate
;
1546 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1550 * Resize the window at the emulator's request
1552 void request_resize(void *frontend
, int w
, int h
)
1554 Session
*s
= frontend
;
1559 /* Arbitrarily clip to the size of the desktop. */
1560 grayrgn
= GetGrayRgn();
1561 #if TARGET_API_MAC_CARBON
1562 GetRegionBounds(grayrgn
, &graybox
);
1564 graybox
= (*grayrgn
)->rgnBBox
;
1566 wlim
= (graybox
.right
- graybox
.left
) / s
->font_width
;
1567 hlim
= (graybox
.bottom
- graybox
.top
) / s
->font_height
;
1568 if (w
> wlim
) w
= wlim
;
1569 if (h
> hlim
) h
= hlim
;
1570 term_size(s
->term
, h
, w
, s
->cfg
.savelines
);
1575 * Iconify (actually collapse) the window at the emulator's request.
1577 void set_iconic(void *frontend
, int iconic
)
1579 Session
*s
= frontend
;
1582 if (mac_gestalts
.apprvers
>= 0x0100 &&
1583 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1584 (features
& kWindowCanCollapse
))
1585 CollapseWindow(s
->window
, iconic
);
1589 * Move the window in response to a server-side request.
1591 void move_window(void *frontend
, int x
, int y
)
1593 Session
*s
= frontend
;
1595 MoveWindow(s
->window
, x
, y
, FALSE
);
1599 * Move the window to the top or bottom of the z-order in response
1600 * to a server-side request.
1602 void set_zorder(void *frontend
, int top
)
1604 Session
*s
= frontend
;
1607 * We also change the input focus to point to the topmost window,
1608 * since that's probably what the Human Interface Guidelines would
1612 SelectWindow(s
->window
);
1614 SendBehind(s
->window
, NULL
);
1618 * Refresh the window in response to a server-side request.
1620 void refresh_window(void *frontend
)
1622 Session
*s
= frontend
;
1624 term_invalidate(s
->term
);
1628 * Maximise or restore the window in response to a server-side
1631 void set_zoomed(void *frontend
, int zoomed
)
1633 Session
*s
= frontend
;
1635 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1639 * Report whether the window is iconic, for terminal reports.
1641 int is_iconic(void *frontend
)
1643 Session
*s
= frontend
;
1646 if (mac_gestalts
.apprvers
>= 0x0100 &&
1647 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1648 (features
& kWindowCanCollapse
))
1649 return IsWindowCollapsed(s
->window
);
1654 * Report the window's position, for terminal reports.
1656 void get_window_pos(void *frontend
, int *x
, int *y
)
1658 Session
*s
= frontend
;
1661 #if TARGET_API_MAC_CARBON
1662 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1664 rect
= s
->window
->portRect
;
1671 * Report the window's pixel size, for terminal reports.
1673 void get_window_pixels(void *frontend
, int *x
, int *y
)
1675 Session
*s
= frontend
;
1678 #if TARGET_API_MAC_CARBON
1679 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1681 rect
= s
->window
->portRect
;
1683 *x
= rect
.right
- rect
.left
;
1684 *y
= rect
.bottom
- rect
.top
;
1688 * Return the window or icon title.
1690 char *get_window_title(void *frontend
, int icon
)
1692 Session
*s
= frontend
;
1694 static char title
[256];
1696 GetWTitle(s
->window
, ptitle
);
1697 p2cstrcpy(title
, ptitle
);
1702 * real_palette_set(): This does the actual palette-changing work on behalf
1703 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1704 * is doing a batch of updates.
1706 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1710 if (!HAVE_COLOR_QD())
1712 col
.red
= r
* 0x0101;
1713 col
.green
= g
* 0x0101;
1714 col
.blue
= b
* 0x0101;
1715 SetEntryColor(s
->palette
, n
, &col
);
1719 * Set the logical palette. Called by the terminal emulator.
1721 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1723 Session
*s
= frontend
;
1725 if (!HAVE_COLOR_QD())
1727 real_palette_set(s
, n
, r
, g
, b
);
1728 if (n
== DEFAULT_BG
)
1731 ActivatePalette(s
->window
);
1735 * Reset to the default palette
1737 void palette_reset(void *frontend
)
1739 Session
*s
= frontend
;
1740 /* This maps colour indices in cfg to those used in our palette. */
1741 static const int ww
[] = {
1742 256, 257, 258, 259, 260, 261,
1743 0, 8, 1, 9, 2, 10, 3, 11,
1744 4, 12, 5, 13, 6, 14, 7, 15
1749 if (!HAVE_COLOR_QD())
1752 for (i
= 0; i
< 22; i
++) {
1754 real_palette_set(s
,w
,
1755 s
->cfg
.colours
[i
][0],
1756 s
->cfg
.colours
[i
][1],
1757 s
->cfg
.colours
[i
][2]);
1759 for (i
= 0; i
< 240; i
++) {
1761 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1762 real_palette_set(s
,i
+16,
1767 int shade
= i
- 216;
1768 shade
= (shade
+ 1) * 0xFF / (240 - 216 + 1);
1769 real_palette_set(s
,i
+16,shade
,shade
,shade
);
1775 ActivatePalette(s
->window
);
1776 /* Palette Manager will generate update events as required. */
1780 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1783 void do_scroll(Context ctx
, int topline
, int botline
, int lines
)
1787 RgnHandle scrollrgn
= NewRgn();
1788 RgnHandle movedupdate
= NewRgn();
1789 RgnHandle update
= NewRgn();
1790 Point g2l
= { 0, 0 };
1792 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1795 * Work out the part of the update region that will scrolled by
1799 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1800 s
->term
->cols
* s
->font_width
,
1801 (botline
+ 1) * s
->font_height
);
1803 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1804 s
->term
->cols
* s
->font_width
,
1805 (botline
- lines
+ 1) * s
->font_height
);
1806 #if TARGET_API_MAC_CARBON
1807 GetWindowRegion(s
->window
, kWindowUpdateRgn
, movedupdate
);
1809 GetWindowUpdateRgn(s
->window
, movedupdate
);
1811 GlobalToLocal(&g2l
);
1812 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1813 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1814 #if TARGET_API_MAC_CARBON
1815 ValidWindowRgn(s
->window
, movedupdate
);
1817 ValidRgn(movedupdate
);
1819 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1822 if (HAVE_COLOR_QD())
1823 PmBackColor(DEFAULT_BG
);
1825 BackColor(blackColor
); /* XXX make configurable */
1826 SetRect(&r
, 0, topline
* s
->font_height
,
1827 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1828 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1830 #if TARGET_API_MAC_CARBON
1831 InvalWindowRgn(s
->window
, update
);
1832 InvalWindowRgn(s
->window
, movedupdate
);
1835 InvalRgn(movedupdate
);
1838 DisposeRgn(scrollrgn
);
1839 DisposeRgn(movedupdate
);
1843 /* Dummy routine, only required in plink. */
1844 void ldisc_update(void *frontend
, int echo
, int edit
)
1848 char *get_ttymode(void *frontend
, const char *mode
)
1850 Session
*s
= frontend
;
1851 return term_get_ttymode(s
->term
, mode
);
1855 * Mac PuTTY doesn't support printing yet.
1857 printer_job
*printer_start_job(char *printer
)
1863 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1867 void printer_finish_job(printer_job
*pj
)
1871 void frontend_keypress(void *handle
)
1874 * Keypress termination in non-Close-On-Exit mode is not
1875 * currently supported in PuTTY proper, because the window
1876 * always has a perfectly good Close button anyway. So we do
1883 * Ask whether to wipe a session log file before writing to it.
1884 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1886 int askappend(void *frontend
, Filename filename
,
1887 void (*callback
)(void *ctx
, int result
), void *ctx
)
1890 /* FIXME: not implemented yet. */
1894 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
1896 Session
*s
= frontend
;
1898 return term_data(s
->term
, is_stderr
, data
, len
);
1901 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
1903 Session
*s
= p
->frontend
;
1904 return term_get_userpass_input(s
->term
, p
, in
, inlen
);
1910 * c-file-style: "simon"