1 /* $Id: macterm.c,v 1.74 2003/04/12 21:07:25 ben Exp $ */
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 NCOLOURS (lenof(((Config *)0)->colours))
69 #define DEFAULT_FG_BOLD 17
71 #define DEFAULT_BG_BOLD 19
75 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
77 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
80 static void mac_initfont(Session
*);
81 static pascal OSStatus
uni_to_font_fallback(UniChar
*, ByteCount
, ByteCount
*,
82 TextPtr
, ByteCount
, ByteCount
*,
84 ConstUnicodeMappingPtr
);
85 static void mac_initpalette(Session
*);
86 static void mac_adjustwinbg(Session
*);
87 static void mac_adjustsize(Session
*, int, int);
88 static void mac_drawgrowicon(Session
*s
);
89 static pascal void mac_growtermdraghook(void);
90 static pascal void mac_scrolltracker(ControlHandle
, short);
91 static pascal void do_text_for_device(short, short, GDHandle
, long);
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
);
163 fatalbox("%s", errmsg
);
164 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
165 set_title(s
, s
->realhost
);
167 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
169 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
170 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
172 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
173 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
175 ShowWindow(s
->window
);
179 s
->next
->prev
= &s
->next
;
184 * Try to work out a horizontal scaling factor for the current font
185 * that will give a chracter width of wantwidth. Return it in numer
186 * and denom (suitable for passing to StdText()).
188 static void mac_workoutfontscale(Session
*s
, int wantwidth
,
189 Point
*numerp
, Point
*denomp
)
191 Point numer
, denom
, tmpnumer
, tmpdenom
;
193 const char text
= 'W';
195 #if TARGET_API_MAC_CARBON
196 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(s
->window
));
198 QDProcsPtr gp
= s
->window
->grafProcs
;
201 numer
.v
= denom
.v
= 1; /* always */
202 numer
.h
= denom
.h
= 1;
203 for (i
= 0; i
< 3; i
++) {
207 gotwidth
= InvokeQDTxMeasUPP(1, &text
, &tmpnumer
, &tmpdenom
, &fi
,
210 gotwidth
= StdTxMeas(1, &text
, &tmpnumer
, &tmpdenom
, &fi
);
211 /* The result of StdTxMeas must be scaled by the factors it returns. */
212 gotwidth
= FixRound(FixMul(gotwidth
<< 16,
213 FixRatio(tmpnumer
.h
, tmpdenom
.h
)));
214 if (gotwidth
== wantwidth
)
216 numer
.h
*= wantwidth
;
223 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
225 static void mac_initfont(Session
*s
)
231 SetPort((GrafPtr
)GetWindowPort(s
->window
));
232 GetFNum(s
->cfg
.font
.name
, &s
->fontnum
);
233 TextFont(s
->fontnum
);
234 TextFace(s
->cfg
.font
.face
);
235 TextSize(s
->cfg
.font
.size
);
237 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
238 s
->font_ascent
= fi
.ascent
;
239 s
->font_leading
= fi
.leading
;
240 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
241 mac_workoutfontscale(s
, s
->font_width
,
242 &s
->font_stdnumer
, &s
->font_stddenom
);
243 mac_workoutfontscale(s
, s
->font_width
* 2,
244 &s
->font_widenumer
, &s
->font_widedenom
);
245 TextSize(s
->cfg
.font
.size
* 2);
246 mac_workoutfontscale(s
, s
->font_width
* 2,
247 &s
->font_bignumer
, &s
->font_bigdenom
);
248 TextSize(s
->cfg
.font
.size
);
249 if (!s
->cfg
.bold_colour
) {
251 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
253 s
->font_boldadjust
= 0;
255 if (s
->uni_to_font
!= NULL
)
256 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
257 if (mac_gestalts
.encvvers
!= 0 &&
258 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
259 kTextLanguageDontCare
,
260 kTextRegionDontCare
, s
->cfg
.font
.name
,
262 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) == noErr
) {
263 if (uni_to_font_fallback_upp
== NULL
)
264 uni_to_font_fallback_upp
=
265 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback
);
266 fbflags
= kUnicodeFallbackCustomOnly
;
267 if (mac_gestalts
.uncvattr
& kTECAddFallbackInterruptMask
)
268 fbflags
|= kUnicodeFallbackInterruptSafeMask
;
269 if (SetFallbackUnicodeToText(s
->uni_to_font
,
270 uni_to_font_fallback_upp
, fbflags
, NULL
) != noErr
) {
271 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
278 s
->uni_to_font
= NULL
;
279 p2cstrcpy(cfontname
, s
->cfg
.font
.name
);
281 charset_from_macenc(FontToScript(s
->fontnum
),
282 GetScriptManagerVariable(smRegionCode
),
283 mac_gestalts
.sysvers
, cfontname
);
286 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
289 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
290 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
291 ByteCount
*ousedp
, LogicalAddress cookie
, ConstUnicodeMappingPtr mapping
)
295 return kTECOutputBufferFullStatus
;
297 * What I'd _like_ to do here is to somehow generate the
298 * missing-character glyph that every font is required to have.
299 * Unfortunately (and somewhat surprisingly), I can't find any way
300 * to actually ask for it explicitly. Bah.
309 * Called every time round the event loop.
311 void mac_pollterm(void)
315 for (s
= sesslist
; s
!= NULL
; s
= s
->next
) {
317 term_update(s
->term
);
322 * To be called whenever the window size changes.
323 * rows and cols should be desired values.
324 * It's assumed the terminal emulator will be informed, and will set rows
327 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
328 int winwidth
, winheight
;
330 winwidth
= newcols
* s
->font_width
+ 15;
331 winheight
= newrows
* s
->font_height
;
332 SizeWindow(s
->window
, winwidth
, winheight
, true);
333 HideControl(s
->scrollbar
);
334 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
335 SizeControl(s
->scrollbar
, 16, winheight
- 13);
336 ShowControl(s
->scrollbar
);
340 static void mac_initpalette(Session
*s
)
343 if (!HAVE_COLOR_QD())
346 * Most colours should be inhibited on 2bpp displays.
347 * Palette manager documentation suggests inhibiting all tolerant colours
348 * on greyscale displays.
350 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
351 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
352 #define PM_TOLERANCE 0x2000
353 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
354 if (s
->palette
== NULL
)
355 fatalbox("Unable to create palette");
356 /* In 2bpp, these are the colours we want most. */
357 SetEntryUsage(s
->palette
, DEFAULT_BG
,
358 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
359 SetEntryUsage(s
->palette
, DEFAULT_FG
,
360 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
361 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
362 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
363 SetEntryUsage(s
->palette
, CURSOR_BG
,
364 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
369 * Set the background colour of the window correctly. Should be
370 * called whenever the default background changes.
372 static void mac_adjustwinbg(Session
*s
)
375 if (!HAVE_COLOR_QD())
378 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
379 SetWindowContentColor(s
->window
,
380 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
384 #if !TARGET_API_MAC_CARBON
385 if (s
->wctab
== NULL
)
386 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
387 if (s
->wctab
== NULL
)
388 return; /* do without */
389 (*s
->wctab
)->wCSeed
= 0;
390 (*s
->wctab
)->wCReserved
= 0;
391 (*s
->wctab
)->ctSize
= 0;
392 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
393 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
394 SetWinColor(s
->window
, s
->wctab
);
400 * Set the cursor shape correctly
402 static void mac_adjusttermcursor(WindowPtr window
, Point mouse
,
406 ControlHandle control
;
409 #if TARGET_API_MAC_CARBON
415 SetPort((GrafPtr
)GetWindowPort(window
));
416 s
= mac_windowsession(window
);
417 GlobalToLocal(&mouse
);
418 part
= FindControl(mouse
, window
, &control
);
419 if (control
== s
->scrollbar
) {
420 #if TARGET_API_MAC_CARBON
421 SetCursor(GetQDGlobalsArrow(&arrow
));
422 RectRgn(cursrgn
, GetControlBounds(s
->scrollbar
, &rect
));
424 SetCursor(&qd
.arrow
);
425 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
428 x
= mouse
.h
/ s
->font_width
;
429 y
= mouse
.v
/ s
->font_height
;
431 #if TARGET_API_MAC_CARBON
432 SetCursor(GetQDGlobalsArrow(&arrow
));
434 SetCursor(&qd
.arrow
);
437 SetCursor(*GetCursor(iBeamCursor
));
438 /* Ask for shape changes if we leave this character cell. */
439 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
440 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
442 #if TARGET_API_MAC_CARBON
444 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
445 SectRgn(cursrgn
, visrgn
, cursrgn
);
448 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
453 * Enable/disable menu items based on the active terminal window.
455 #if TARGET_API_MAC_CARBON
456 #define DisableItem DisableMenuItem
457 #define EnableItem EnableMenuItem
459 static void mac_adjusttermmenus(WindowPtr window
)
463 #if !TARGET_API_MAC_CARBON
467 s
= mac_windowsession(window
);
468 menu
= GetMenuHandle(mFile
);
469 DisableItem(menu
, iSave
); /* XXX enable if modified */
470 EnableItem(menu
, iSaveAs
);
471 EnableItem(menu
, iDuplicate
);
472 menu
= GetMenuHandle(mEdit
);
474 DisableItem(menu
, iUndo
);
475 DisableItem(menu
, iCut
);
476 if (1/*s->term->selstate == SELECTED*/)
477 EnableItem(menu
, iCopy
);
479 DisableItem(menu
, iCopy
);
480 #if TARGET_API_MAC_CARBON
483 if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) == noTypeErr
)
485 DisableItem(menu
, iPaste
);
487 EnableItem(menu
, iPaste
);
488 DisableItem(menu
, iClear
);
489 EnableItem(menu
, iSelectAll
);
490 menu
= GetMenuHandle(mWindow
);
492 EnableItem(menu
, iShowEventLog
);
495 static void mac_menuterm(WindowPtr window
, short menu
, short item
)
499 s
= mac_windowsession(window
);
507 term_do_paste(s
->term
);
521 static void mac_clickterm(WindowPtr window
, EventRecord
*event
)
525 ControlHandle control
;
527 static ControlActionUPP mac_scrolltracker_upp
= NULL
;
529 s
= mac_windowsession(window
);
530 SetPort((GrafPtr
)GetWindowPort(window
));
531 mouse
= event
->where
;
532 GlobalToLocal(&mouse
);
533 part
= FindControl(mouse
, window
, &control
);
534 if (control
== s
->scrollbar
) {
536 case kControlIndicatorPart
:
537 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
538 term_scroll(s
->term
, +1, GetControlValue(control
));
540 case kControlUpButtonPart
:
541 case kControlDownButtonPart
:
542 case kControlPageUpPart
:
543 case kControlPageDownPart
:
544 if (mac_scrolltracker_upp
== NULL
)
545 mac_scrolltracker_upp
=
546 NewControlActionUPP(&mac_scrolltracker
);
547 TrackControl(control
, mouse
, mac_scrolltracker_upp
);
551 text_click(s
, event
);
555 static void text_click(Session
*s
, EventRecord
*event
)
559 static UInt32 lastwhen
= 0;
560 static Session
*lastsess
= NULL
;
561 static int lastrow
= -1, lastcol
= -1;
562 static Mouse_Action lastact
= MA_NOTHING
;
564 SetPort((GrafPtr
)GetWindowPort(s
->window
));
565 localwhere
= event
->where
;
566 GlobalToLocal(&localwhere
);
568 col
= PTOCC(localwhere
.h
);
569 row
= PTOCR(localwhere
.v
);
570 if (event
->when
- lastwhen
< GetDblTime() &&
571 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
572 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
573 lastact
== MA_2CLK ? MA_3CLK
:
574 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
577 term_mouse(s
->term
, MBT_LEFT
,
578 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
579 lastact
, col
, row
, event
->modifiers
& shiftKey
,
580 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
584 while (StillDown()) {
585 GetMouse(&localwhere
);
586 col
= PTOCC(localwhere
.h
);
587 row
= PTOCR(localwhere
.v
);
588 term_mouse(s
->term
, MBT_LEFT
,
589 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
590 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
591 event
->modifiers
& controlKey
,
592 event
->modifiers
& optionKey
);
593 if (row
> s
->term
->rows
- 1)
594 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
596 term_scroll(s
->term
, 0, row
);
598 term_mouse(s
->term
, MBT_LEFT
,
599 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
600 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
601 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
602 lastwhen
= TickCount();
605 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
)
607 #if !TARGET_API_MAC_CARBON
610 ByteCount iread
, olen
;
618 * See "Programming with the Text Encoding Conversion Manager"
619 * Appendix E for Unicode scrap conventions.
621 * XXX Maybe PICT scrap too.
623 if (ZeroScrap() != noErr
)
625 PutScrap(len
* sizeof(*data
), kScrapFlavorTypeUnicode
, data
);
627 /* Replace LINE SEPARATORs with CR for TEXT output. */
628 for (i
= 0; i
< len
; i
++)
629 if (data
[i
] == 0x2028)
632 mactextbuf
= snewn(len
, char); /* XXX DBCS */
633 if (s
->uni_to_font
!= NULL
) {
634 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
636 kUnicodeUseFallbacksMask
,
638 len
, &iread
, &olen
, mactextbuf
);
639 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
641 } else if (s
->font_charset
!= CS_NONE
) {
643 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
644 s
->font_charset
, NULL
, ".", 1);
647 PutScrap(olen
, kScrapFlavorTypeText
, mactextbuf
);
650 stsz
= offsetof(StScrpRec
, scrpStyleTab
) + sizeof(ScrpSTElement
);
651 stsc
= smalloc(stsz
);
652 stsc
->scrpNStyles
= 1;
653 stsc
->scrpStyleTab
[0].scrpStartChar
= 0;
654 stsc
->scrpStyleTab
[0].scrpHeight
= s
->font_height
;
655 stsc
->scrpStyleTab
[0].scrpAscent
= s
->font_ascent
;
656 stsc
->scrpStyleTab
[0].scrpFont
= s
->fontnum
;
657 stsc
->scrpStyleTab
[0].scrpFace
= 0;
658 stsc
->scrpStyleTab
[0].scrpSize
= s
->cfg
.font
.size
;
659 stsc
->scrpStyleTab
[0].scrpColor
.red
= 0;
660 stsc
->scrpStyleTab
[0].scrpColor
.green
= 0;
661 stsc
->scrpStyleTab
[0].scrpColor
.blue
= 0;
662 PutScrap(stsz
, kScrapFlavorTypeTextStyle
, stsc
);
667 void get_clip(void *frontend
, wchar_t **p
, int *lenp
)
669 #if TARGET_API_MAC_CARBON
672 Session
*s
= frontend
;
673 static Handle h
= NULL
;
674 static wchar_t *data
= NULL
;
679 TextToUnicodeInfo scrap_to_uni
;
680 ByteCount iread
, olen
;
694 if (GetScrap(NULL
, kScrapFlavorTypeUnicode
, &offset
) > 0) {
698 GetScrap(h
, kScrapFlavorTypeUnicode
, &offset
) / sizeof(**p
);
701 } else if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) > 0) {
702 texth
= NewHandle(0);
703 textlen
= GetScrap(texth
, kScrapFlavorTypeText
, &offset
);
705 data
= snewn(textlen
, wchar_t);
706 /* XXX should use 'styl' scrap if it's there. */
707 if (mac_gestalts
.encvvers
!= 0 &&
708 UpgradeScriptInfoToTextEncoding(smSystemScript
,
709 kTextLanguageDontCare
,
710 kTextRegionDontCare
, NULL
,
712 CreateTextToUnicodeInfoByEncoding(enc
, &scrap_to_uni
) ==
714 err
= ConvertFromTextToUnicode(scrap_to_uni
, textlen
,
715 *texth
, 0, 0, NULL
, NULL
, NULL
,
717 &iread
, &olen
, data
);
718 DisposeTextToUnicodeInfo(&scrap_to_uni
);
721 *lenp
= olen
/ sizeof(**p
);
728 charset_from_macenc(GetScriptManagerVariable(smSysScript
),
729 GetScriptManagerVariable(smRegionCode
),
730 mac_gestalts
.sysvers
, NULL
);
731 if (charset
!= CS_NONE
) {
733 *lenp
= charset_to_unicode(&tptr
, &textlen
, data
,
734 textlen
* 2, charset
, NULL
,
739 DisposeHandle(texth
);
748 static pascal void mac_scrolltracker(ControlHandle control
, short part
)
752 #if TARGET_API_MAC_CARBON
753 s
= mac_windowsession(GetControlOwner(control
));
755 s
= mac_windowsession((*control
)->contrlOwner
);
758 case kControlUpButtonPart
:
759 term_scroll(s
->term
, 0, -1);
761 case kControlDownButtonPart
:
762 term_scroll(s
->term
, 0, +1);
764 case kControlPageUpPart
:
765 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
767 case kControlPageDownPart
:
768 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
773 static void mac_keyterm(WindowPtr window
, EventRecord
*event
)
775 Session
*s
= mac_windowsession(window
);
776 Key_Sym keysym
= PK_NULL
;
777 unsigned int mods
= 0, flags
= PKF_NUMLOCK
;
781 ScriptCode key_script
;
786 fprintf(stderr
, "Got key event %08x\n", event
->message
);
789 /* No meta key yet -- that'll be rather fun. */
791 /* Keys that we handle locally */
792 if (event
->modifiers
& shiftKey
) {
793 switch ((event
->message
& keyCodeMask
) >> 8) {
794 case 0x74: /* shift-pageup */
795 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
797 case 0x79: /* shift-pagedown */
798 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
803 if (event
->modifiers
& shiftKey
)
805 if (event
->modifiers
& controlKey
)
807 if (event
->what
== autoKey
)
810 /* Mac key events consist of a virtual key code and a character code. */
812 switch ((event
->message
& keyCodeMask
) >> 8) {
813 case 0x24: keysym
= PK_RETURN
; break;
814 case 0x30: keysym
= PK_TAB
; break;
815 case 0x33: keysym
= PK_BACKSPACE
; break;
816 case 0x35: keysym
= PK_ESCAPE
; break;
818 case 0x7A: keysym
= PK_F1
; break;
819 case 0x78: keysym
= PK_F2
; break;
820 case 0x63: keysym
= PK_F3
; break;
821 case 0x76: keysym
= PK_F4
; break;
822 case 0x60: keysym
= PK_F5
; break;
823 case 0x61: keysym
= PK_F6
; break;
824 case 0x62: keysym
= PK_F7
; break;
825 case 0x64: keysym
= PK_F8
; break;
826 case 0x65: keysym
= PK_F9
; break;
827 case 0x6D: keysym
= PK_F10
; break;
828 case 0x67: keysym
= PK_F11
; break;
829 case 0x6F: keysym
= PK_F12
; break;
830 case 0x69: keysym
= PK_F13
; break;
831 case 0x6B: keysym
= PK_F14
; break;
832 case 0x71: keysym
= PK_F15
; break;
834 case 0x72: keysym
= PK_INSERT
; break;
835 case 0x73: keysym
= PK_HOME
; break;
836 case 0x74: keysym
= PK_PAGEUP
; break;
837 case 0x75: keysym
= PK_DELETE
; break;
838 case 0x77: keysym
= PK_END
; break;
839 case 0x79: keysym
= PK_PAGEDOWN
; break;
841 case 0x47: keysym
= PK_PF1
; break;
842 case 0x51: keysym
= PK_PF2
; break;
843 case 0x4B: keysym
= PK_PF3
; break;
844 case 0x43: keysym
= PK_PF4
; break;
845 case 0x4E: keysym
= PK_KPMINUS
; break;
846 case 0x45: keysym
= PK_KPCOMMA
; break;
847 case 0x41: keysym
= PK_KPDECIMAL
; break;
848 case 0x4C: keysym
= PK_KPENTER
; break;
849 case 0x52: keysym
= PK_KP0
; break;
850 case 0x53: keysym
= PK_KP1
; break;
851 case 0x54: keysym
= PK_KP2
; break;
852 case 0x55: keysym
= PK_KP3
; break;
853 case 0x56: keysym
= PK_KP4
; break;
854 case 0x57: keysym
= PK_KP5
; break;
855 case 0x58: keysym
= PK_KP6
; break;
856 case 0x59: keysym
= PK_KP7
; break;
857 case 0x5B: keysym
= PK_KP8
; break;
858 case 0x5C: keysym
= PK_KP9
; break;
860 case 0x7B: keysym
= PK_LEFT
; break;
861 case 0x7C: keysym
= PK_RIGHT
; break;
862 case 0x7D: keysym
= PK_DOWN
; break;
863 case 0x7E: keysym
= PK_UP
; break;
866 /* Map from key script to Unicode. */
867 txt
[0] = event
->message
& charCodeMask
;
868 key_script
= GetScriptManagerVariable(smKeyScript
);
870 if (mac_gestalts
.encvvers
!= 0) {
871 static TextToUnicodeInfo key_to_uni
= NULL
;
872 static ScriptCode key_to_uni_script
;
874 ByteCount iread
, olen
;
877 if (key_to_uni
!= NULL
&& key_to_uni_script
!= key_script
)
878 DisposeTextToUnicodeInfo(&key_to_uni
);
879 if (key_to_uni
== NULL
|| key_to_uni_script
!= key_script
) {
880 if (UpgradeScriptInfoToTextEncoding(key_script
,
881 kTextLanguageDontCare
,
882 kTextRegionDontCare
, NULL
,
884 CreateTextToUnicodeInfoByEncoding(enc
, &key_to_uni
) == noErr
)
885 key_to_uni_script
= key_script
;
889 if (key_to_uni
!= NULL
) {
890 err
= ConvertFromTextToUnicode(key_to_uni
, 1, txt
,
891 (kUnicodeKeepInfoMask
|
892 kUnicodeStringUnterminatedMask
),
894 sizeof(utxt
), &iread
, &olen
, utxt
);
896 len
= olen
/ sizeof(*utxt
);
903 charset
= charset_from_macenc(key_script
,
904 GetScriptManagerVariable(smRegionCode
),
905 mac_gestalts
.sysvers
, NULL
);
906 if (charset
!= CS_NONE
) {
907 len
= charset_to_unicode(&tptr
, &tlen
, utxt
, sizeof(utxt
), charset
,
911 term_key(s
->term
, keysym
, utxt
, len
, mods
, flags
);
914 void request_paste(void *frontend
)
916 Session
*s
= frontend
;
919 * In the Mac OS, pasting is synchronous: we can read the
920 * clipboard with no difficulty, so request_paste() can just go
923 term_do_paste(s
->term
);
934 static void mac_growterm(WindowPtr window
, EventRecord
*event
)
938 int newrows
, newcols
;
940 #if !TARGET_API_MAC_CARBON
941 DragGrayRgnUPP draghooksave
;
946 s
= mac_windowsession(window
);
948 #if !TARGET_API_MAC_CARBON
949 draghooksave
= LMGetDragHook();
950 growterm_state
.oldmsg
[0] = '\0';
951 growterm_state
.zeromouse
= event
->where
;
952 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
953 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
954 growterm_state
.s
= s
;
957 BackColor(whiteColor
);
958 ForeColor(blackColor
);
959 TextFont(systemFont
);
963 SetRect(&growterm_state
.msgrect
, 0, 0,
964 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
965 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
966 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
969 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
970 grow_result
= GrowWindow(window
, event
->where
, &limits
);
972 #if !TARGET_API_MAC_CARBON
973 DisposeDragGrayRgnUPP(LMGetDragHook());
974 LMSetDragHook(draghooksave
);
975 InvalRect(&growterm_state
.msgrect
);
980 if (grow_result
!= 0) {
981 newrows
= HiWord(grow_result
) / s
->font_height
;
982 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
983 mac_adjustsize(s
, newrows
, newcols
);
984 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
988 #if !TARGET_API_MAC_CARBON
989 static pascal void mac_growtermdraghook(void)
991 Session
*s
= growterm_state
.s
;
995 unsigned char pbuf
[20];
996 int newrows
, newcols
;
999 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
1000 if (newrows
< 1) newrows
= 1;
1001 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
1002 if (newcols
< 1) newcols
= 1;
1003 sprintf(buf
, "%dx%d", newcols
, newrows
);
1004 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
1006 strcpy(growterm_state
.oldmsg
, buf
);
1007 c2pstrcpy(pbuf
, buf
);
1010 SetPort(growterm_state
.s
->window
);
1011 EraseRect(&growterm_state
.msgrect
);
1012 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
1018 void mac_closeterm(WindowPtr window
)
1020 Session
*s
= mac_windowsession(window
);
1022 /* XXX warn on close */
1023 HideWindow(s
->window
);
1025 s
->next
->prev
= s
->prev
;
1026 ldisc_free(s
->ldisc
);
1027 s
->back
->free(s
->backhandle
);
1028 log_free(s
->logctx
);
1029 if (s
->uni_to_font
!= NULL
)
1030 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
1032 mac_freeeventlog(s
);
1033 sfree((WinInfo
*)GetWRefCon(s
->window
));
1034 DisposeWindow(s
->window
);
1035 DisposePalette(s
->palette
);
1039 static void mac_activateterm(WindowPtr window
, EventRecord
*event
)
1042 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1044 s
= mac_windowsession(window
);
1045 s
->term
->has_focus
= active
;
1046 term_update(s
->term
);
1048 ShowControl(s
->scrollbar
);
1050 if (HAVE_COLOR_QD())
1051 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
1053 BackColor(blackColor
);
1054 HideControl(s
->scrollbar
);
1056 mac_drawgrowicon(s
);
1059 static void mac_updateterm(WindowPtr window
)
1063 #if TARGET_API_MAC_CARBON
1067 s
= mac_windowsession(window
);
1068 SetPort((GrafPtr
)GetWindowPort(window
));
1069 BeginUpdate(window
);
1071 #if TARGET_API_MAC_CARBON
1073 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1074 GetRegionBounds(visrgn
, &bbox
);
1076 bbox
= (*window
->visRgn
)->rgnBBox
;
1078 term_paint(s
->term
, s
, PTOCC(bbox
.left
), PTOCR(bbox
.top
),
1079 PTOCC(bbox
.right
), PTOCR(bbox
.bottom
), 1);
1080 /* Restore default colours in case the Window Manager uses them */
1081 if (HAVE_COLOR_QD()) {
1082 PmForeColor(DEFAULT_FG
);
1083 PmBackColor(DEFAULT_BG
);
1085 ForeColor(whiteColor
);
1086 BackColor(blackColor
);
1088 if (FrontWindow() != window
)
1089 #if TARGET_API_MAC_CARBON
1090 EraseRect(GetControlBounds(s
->scrollbar
, &bbox
));
1091 UpdateControls(window
, visrgn
);
1094 EraseRect(&(*s
->scrollbar
)->contrlRect
);
1095 UpdateControls(window
, window
->visRgn
);
1097 mac_drawgrowicon(s
);
1102 static void mac_drawgrowicon(Session
*s
)
1107 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1109 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1110 * See Tech Note TB575 for details.
1112 #if TARGET_API_MAC_CARBON
1113 GetPortBounds(GetWindowPort(s
->window
), &clip
);
1115 clip
= s
->window
->portRect
;
1117 clip
.left
= clip
.right
- 15;
1121 DrawGrowIcon(s
->window
);
1123 DisposeRgn(savergn
);
1126 struct do_text_args
{
1137 * Call from the terminal emulator to draw a bit of text
1139 * x and y are text row and column (zero-based)
1141 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1142 unsigned long attr
, int lattr
)
1146 struct do_text_args a
;
1147 RgnHandle textrgn
, saveclip
;
1148 #if TARGET_API_MAC_CARBON
1151 char mactextbuf
[1024];
1152 UniChar unitextbuf
[1024];
1153 wchar_t *unitextptr
;
1155 ByteCount iread
, olen
;
1157 static DeviceLoopDrawingUPP do_text_for_device_upp
= NULL
;
1159 assert(len
<= 1024);
1161 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1163 fontwidth
= s
->font_width
;
1164 if ((lattr
& LATTR_MODE
) != LATTR_NORM
)
1167 /* First check this text is relevant */
1168 a
.textrect
.top
= y
* s
->font_height
;
1169 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
1170 a
.textrect
.left
= x
* fontwidth
;
1171 a
.textrect
.right
= (x
+ len
) * fontwidth
;
1172 if (a
.textrect
.right
> s
->term
->cols
* s
->font_width
)
1173 a
.textrect
.right
= s
->term
->cols
* s
->font_width
;
1174 #if TARGET_API_MAC_CARBON
1176 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1177 if (!RectInRgn(&a
.textrect
, visrgn
)) {
1183 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
1187 /* Unpack Unicode from the mad format we get passed */
1188 for (i
= 0; i
< len
; i
++)
1189 unitextbuf
[i
] = (unsigned char)text
[i
] | (attr
& CSET_MASK
);
1191 if (s
->uni_to_font
!= NULL
) {
1192 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
1193 unitextbuf
, kUnicodeUseFallbacksMask
,
1194 0, NULL
, NULL
, NULL
,
1195 1024, &iread
, &olen
, mactextbuf
);
1196 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
1198 } else if (s
->font_charset
!= CS_NONE
) {
1199 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1200 unitextptr
= (wchar_t *)unitextbuf
;
1201 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
1202 s
->font_charset
, NULL
, ".", 1);
1207 a
.text
= mactextbuf
;
1211 switch (lattr
& LATTR_MODE
) {
1213 TextSize(s
->cfg
.font
.size
);
1214 a
.numer
= s
->font_stdnumer
;
1215 a
.denom
= s
->font_stddenom
;
1218 TextSize(s
->cfg
.font
.size
);
1219 a
.numer
= s
->font_widenumer
;
1220 a
.denom
= s
->font_widedenom
;
1224 TextSize(s
->cfg
.font
.size
* 2);
1225 a
.numer
= s
->font_bignumer
;
1226 a
.denom
= s
->font_bigdenom
;
1229 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1230 TextFont(s
->fontnum
);
1231 style
= s
->cfg
.font
.face
;
1232 if ((attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1234 if (attr
& ATTR_UNDER
)
1238 if (HAVE_COLOR_QD())
1240 SpaceExtra(s
->font_boldadjust
<< 16);
1241 CharExtra(s
->font_boldadjust
<< 16);
1246 saveclip
= NewRgn();
1248 ClipRect(&a
.textrect
);
1250 RectRgn(textrgn
, &a
.textrect
);
1251 if (HAVE_COLOR_QD()) {
1252 if (do_text_for_device_upp
== NULL
)
1253 do_text_for_device_upp
=
1254 NewDeviceLoopDrawingUPP(&do_text_for_device
);
1255 DeviceLoop(textrgn
, do_text_for_device_upp
, (long)&a
, 0);
1257 do_text_for_device(1, 0, NULL
, (long)&a
);
1259 DisposeRgn(saveclip
);
1260 DisposeRgn(textrgn
);
1261 /* Tell the window manager about it in case this isn't an update */
1262 #if TARGET_API_MAC_CARBON
1263 ValidWindowRect(s
->window
, &a
.textrect
);
1265 ValidRect(&a
.textrect
);
1269 static pascal void do_text_for_device(short depth
, short devflags
,
1270 GDHandle device
, long cookie
)
1272 struct do_text_args
*a
= (struct do_text_args
*)cookie
;
1273 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1274 #if TARGET_API_MAC_CARBON
1275 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(a
->s
->window
));
1277 QDProcsPtr gp
= a
->s
->window
->grafProcs
;
1280 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1281 reverse
= a
->attr
& ATTR_REVERSE
;
1283 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1286 if (HAVE_COLOR_QD()) {
1288 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1289 fgcolour
= (fgcolour
& 0xF) * 2 + (fgcolour
& 0x10 ?
1 : 0);
1290 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1291 bgcolour
= (bgcolour
& 0xF) * 2 + (bgcolour
& 0x10 ?
1 : 0);
1294 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1295 * boldens the background, because that's all we can do.
1297 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1298 bgcolour
= DEFAULT_BG
;
1302 fgcolour
= bgcolour
;
1305 if (bright
&& depth
> 2)
1307 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1308 fgcolour
= CURSOR_FG
;
1309 bgcolour
= CURSOR_BG
;
1311 PmForeColor(fgcolour
);
1312 PmBackColor(bgcolour
);
1313 } else { /* No Color Quickdraw */
1314 /* XXX This should be done with a _little_ more configurability */
1316 ForeColor(blackColor
);
1317 BackColor(whiteColor
);
1319 ForeColor(whiteColor
);
1320 BackColor(blackColor
);
1324 EraseRect(&a
->textrect
);
1325 switch (a
->lattr
& LATTR_MODE
) {
1328 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1331 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
* 2);
1334 MoveTo(a
->textrect
.left
,
1335 a
->textrect
.top
- a
->s
->font_height
+ a
->s
->font_ascent
* 2);
1338 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1340 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
, gp
->textProc
);
1342 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1344 if (a
->attr
& TATTR_PASCURS
) {
1351 PmForeColor(CURSOR_BG
);
1354 FrameRect(&a
->textrect
);
1358 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1359 unsigned long attr
, int lattr
)
1362 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1366 * Call from the terminal emulator to get its graphics context.
1367 * Should probably be called start_redraw or something.
1369 void pre_paint(Session
*s
)
1372 Rect myrect
, tmprect
;
1373 #if TARGET_API_MAC_CARBON
1377 if (HAVE_COLOR_QD()) {
1378 s
->term
->attr_mask
= 0;
1379 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1380 #if TARGET_API_MAC_CARBON
1382 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1383 GetRegionBounds(visrgn
, &myrect
);
1386 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1388 LocalToGlobal((Point
*)&myrect
.top
);
1389 LocalToGlobal((Point
*)&myrect
.bottom
);
1390 for (gdh
= GetDeviceList();
1392 gdh
= GetNextDevice(gdh
)) {
1393 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1394 TestDeviceAttribute(gdh
, screenActive
) &&
1395 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1396 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1398 if (s
->cfg
.bold_colour
)
1399 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1400 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1403 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1406 s
->term
->attr_mask
= ~0;
1407 return; /* No point checking more screens. */
1412 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1413 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1416 Context
get_ctx(void *frontend
)
1418 Session
*s
= frontend
;
1424 void free_ctx(Context ctx
)
1430 * Presumably this does something in Windows
1432 void post_paint(Session
*s
)
1438 * Set the scroll bar position
1440 * total is the line number of the bottom of the working screen
1441 * start is the line number of the top of the display
1442 * page is the length of the displayed page
1444 void set_sbar(void *frontend
, int total
, int start
, int page
)
1446 Session
*s
= frontend
;
1448 /* We don't redraw until we've set everything up, to avoid glitches */
1449 SetControlMinimum(s
->scrollbar
, 0);
1450 SetControlMaximum(s
->scrollbar
, total
- page
);
1451 SetControlValue(s
->scrollbar
, start
);
1453 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1454 SetControlViewSize(s
->scrollbar
, page
);
1458 void sys_cursor(void *frontend
, int x
, int y
)
1461 * I think his is meaningless under Mac OS.
1466 * This is still called when mode==BELL_VISUAL, even though the
1467 * visual bell is handled entirely within terminal.c, because we
1468 * may want to perform additional actions on any kind of bell (for
1469 * example, taskbar flashing in Windows).
1471 void beep(void *frontend
, int mode
)
1473 if (mode
!= BELL_VISUAL
)
1476 * XXX We should indicate the relevant window and/or use the
1477 * Notification Manager
1481 int char_width(Context ctx
, int uc
)
1484 * Until we support exciting character-set stuff, assume all chars are
1491 * Set icon string -- a no-op here (Windowshade?)
1493 void set_icon(void *frontend
, char *icon
) {
1494 Session
*s
= frontend
;
1499 * Set the window title
1501 void set_title(void *frontend
, char *title
)
1503 Session
*s
= frontend
;
1506 c2pstrcpy(mactitle
, title
);
1507 SetWTitle(s
->window
, mactitle
);
1511 * set or clear the "raw mouse message" mode
1513 void set_raw_mouse_mode(void *frontend
, int activate
)
1515 Session
*s
= frontend
;
1517 s
->raw_mouse
= activate
;
1518 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1522 * Resize the window at the emulator's request
1524 void request_resize(void *frontend
, int w
, int h
)
1526 Session
*s
= frontend
;
1531 /* Arbitrarily clip to the size of the desktop. */
1532 grayrgn
= GetGrayRgn();
1533 #if TARGET_API_MAC_CARBON
1534 GetRegionBounds(grayrgn
, &graybox
);
1536 graybox
= (*grayrgn
)->rgnBBox
;
1538 wlim
= (graybox
.right
- graybox
.left
) / s
->font_width
;
1539 hlim
= (graybox
.bottom
- graybox
.top
) / s
->font_height
;
1540 if (w
> wlim
) w
= wlim
;
1541 if (h
> hlim
) h
= hlim
;
1542 term_size(s
->term
, h
, w
, s
->cfg
.savelines
);
1547 * Iconify (actually collapse) the window at the emulator's request.
1549 void set_iconic(void *frontend
, int iconic
)
1551 Session
*s
= frontend
;
1554 if (mac_gestalts
.apprvers
>= 0x0100 &&
1555 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1556 (features
& kWindowCanCollapse
))
1557 CollapseWindow(s
->window
, iconic
);
1561 * Move the window in response to a server-side request.
1563 void move_window(void *frontend
, int x
, int y
)
1565 Session
*s
= frontend
;
1567 MoveWindow(s
->window
, x
, y
, FALSE
);
1571 * Move the window to the top or bottom of the z-order in response
1572 * to a server-side request.
1574 void set_zorder(void *frontend
, int top
)
1576 Session
*s
= frontend
;
1579 * We also change the input focus to point to the topmost window,
1580 * since that's probably what the Human Interface Guidelines would
1584 SelectWindow(s
->window
);
1586 SendBehind(s
->window
, NULL
);
1590 * Refresh the window in response to a server-side request.
1592 void refresh_window(void *frontend
)
1594 Session
*s
= frontend
;
1596 term_invalidate(s
->term
);
1600 * Maximise or restore the window in response to a server-side
1603 void set_zoomed(void *frontend
, int zoomed
)
1605 Session
*s
= frontend
;
1607 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1611 * Report whether the window is iconic, for terminal reports.
1613 int is_iconic(void *frontend
)
1615 Session
*s
= frontend
;
1618 if (mac_gestalts
.apprvers
>= 0x0100 &&
1619 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1620 (features
& kWindowCanCollapse
))
1621 return IsWindowCollapsed(s
->window
);
1626 * Report the window's position, for terminal reports.
1628 void get_window_pos(void *frontend
, int *x
, int *y
)
1630 Session
*s
= frontend
;
1633 #if TARGET_API_MAC_CARBON
1634 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1636 rect
= s
->window
->portRect
;
1643 * Report the window's pixel size, for terminal reports.
1645 void get_window_pixels(void *frontend
, int *x
, int *y
)
1647 Session
*s
= frontend
;
1650 #if TARGET_API_MAC_CARBON
1651 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1653 rect
= s
->window
->portRect
;
1655 *x
= rect
.right
- rect
.left
;
1656 *y
= rect
.bottom
- rect
.top
;
1660 * Return the window or icon title.
1662 char *get_window_title(void *frontend
, int icon
)
1664 Session
*s
= frontend
;
1666 static char title
[256];
1668 GetWTitle(s
->window
, ptitle
);
1669 p2cstrcpy(title
, ptitle
);
1674 * real_palette_set(): This does the actual palette-changing work on behalf
1675 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1676 * is doing a batch of updates.
1678 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1682 if (!HAVE_COLOR_QD())
1684 col
.red
= r
* 0x0101;
1685 col
.green
= g
* 0x0101;
1686 col
.blue
= b
* 0x0101;
1687 SetEntryColor(s
->palette
, n
, &col
);
1691 * Set the logical palette. Called by the terminal emulator.
1693 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1695 Session
*s
= frontend
;
1696 static const int first
[21] = {
1697 0, 2, 4, 6, 8, 10, 12, 14,
1698 1, 3, 5, 7, 9, 11, 13, 15,
1702 if (!HAVE_COLOR_QD())
1704 real_palette_set(s
, first
[n
], r
, g
, b
);
1706 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1707 if (first
[n
] == DEFAULT_BG
)
1709 ActivatePalette(s
->window
);
1713 * Reset to the default palette
1715 void palette_reset(void *frontend
)
1717 Session
*s
= frontend
;
1718 /* This maps colour indices in cfg to those used in our palette. */
1719 static const int ww
[] = {
1720 6, 7, 8, 9, 10, 11, 12, 13,
1721 14, 15, 16, 17, 18, 19, 20, 21,
1726 if (!HAVE_COLOR_QD())
1729 assert(lenof(ww
) == NCOLOURS
);
1731 for (i
= 0; i
< NCOLOURS
; i
++) {
1732 real_palette_set(s
, i
,
1733 s
->cfg
.colours
[ww
[i
]][0],
1734 s
->cfg
.colours
[ww
[i
]][1],
1735 s
->cfg
.colours
[ww
[i
]][2]);
1738 ActivatePalette(s
->window
);
1739 /* Palette Manager will generate update events as required. */
1743 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1746 void do_scroll(Context ctx
, int topline
, int botline
, int lines
)
1750 RgnHandle scrollrgn
= NewRgn();
1751 RgnHandle movedupdate
= NewRgn();
1752 RgnHandle update
= NewRgn();
1753 Point g2l
= { 0, 0 };
1755 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1758 * Work out the part of the update region that will scrolled by
1762 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1763 s
->term
->cols
* s
->font_width
,
1764 (botline
+ 1) * s
->font_height
);
1766 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1767 s
->term
->cols
* s
->font_width
,
1768 (botline
- lines
+ 1) * s
->font_height
);
1769 #if TARGET_API_MAC_CARBON
1770 GetWindowRegion(s
->window
, kWindowUpdateRgn
, movedupdate
);
1772 GetWindowUpdateRgn(s
->window
, movedupdate
);
1774 GlobalToLocal(&g2l
);
1775 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1776 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1777 #if TARGET_API_MAC_CARBON
1778 ValidWindowRgn(s
->window
, movedupdate
);
1780 ValidRgn(movedupdate
);
1782 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1785 if (HAVE_COLOR_QD())
1786 PmBackColor(DEFAULT_BG
);
1788 BackColor(blackColor
); /* XXX make configurable */
1789 SetRect(&r
, 0, topline
* s
->font_height
,
1790 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1791 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1793 #if TARGET_API_MAC_CARBON
1794 InvalWindowRgn(s
->window
, update
);
1795 InvalWindowRgn(s
->window
, movedupdate
);
1798 InvalRgn(movedupdate
);
1801 DisposeRgn(scrollrgn
);
1802 DisposeRgn(movedupdate
);
1806 /* Dummy routine, only required in plink. */
1807 void ldisc_update(void *frontend
, int echo
, int edit
)
1812 * Mac PuTTY doesn't support printing yet.
1814 printer_job
*printer_start_job(char *printer
)
1820 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1824 void printer_finish_job(printer_job
*pj
)
1828 void frontend_keypress(void *handle
)
1831 * Keypress termination in non-Close-On-Exit mode is not
1832 * currently supported in PuTTY proper, because the window
1833 * always has a perfectly good Close button anyway. So we do
1840 * Ask whether to wipe a session log file before writing to it.
1841 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1843 int askappend(void *frontend
, Filename filename
)
1846 /* FIXME: not implemented yet. */
1850 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
1852 Session
*s
= frontend
;
1854 return term_data(s
->term
, is_stderr
, data
, len
);
1860 * c-file-style: "simon"