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
;
328 winwidth
= newcols
* s
->font_width
+ 15;
329 winheight
= newrows
* s
->font_height
;
330 SizeWindow(s
->window
, winwidth
, winheight
, true);
331 HideControl(s
->scrollbar
);
332 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
333 SizeControl(s
->scrollbar
, 16, winheight
- 13);
334 ShowControl(s
->scrollbar
);
338 static void mac_initpalette(Session
*s
)
341 if (!HAVE_COLOR_QD())
344 * Most colours should be inhibited on 2bpp displays.
345 * Palette manager documentation suggests inhibiting all tolerant colours
346 * on greyscale displays.
348 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
349 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
350 #define PM_TOLERANCE 0x2000
351 s
->palette
= NewPalette(262, NULL
, PM_NORMAL
, PM_TOLERANCE
);
352 if (s
->palette
== NULL
)
353 fatalbox("Unable to create palette");
354 /* In 2bpp, these are the colours we want most. */
355 SetEntryUsage(s
->palette
, DEFAULT_BG
,
356 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
357 SetEntryUsage(s
->palette
, DEFAULT_FG
,
358 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
359 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
360 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
361 SetEntryUsage(s
->palette
, CURSOR_BG
,
362 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
367 * Set the background colour of the window correctly. Should be
368 * called whenever the default background changes.
370 static void mac_adjustwinbg(Session
*s
)
373 if (!HAVE_COLOR_QD())
376 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
377 SetWindowContentColor(s
->window
,
378 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
382 #if !TARGET_API_MAC_CARBON
383 if (s
->wctab
== NULL
)
384 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
385 if (s
->wctab
== NULL
)
386 return; /* do without */
387 (*s
->wctab
)->wCSeed
= 0;
388 (*s
->wctab
)->wCReserved
= 0;
389 (*s
->wctab
)->ctSize
= 0;
390 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
391 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
392 SetWinColor(s
->window
, s
->wctab
);
398 * Set the cursor shape correctly
400 static void mac_adjusttermcursor(WindowPtr window
, Point mouse
,
404 ControlHandle control
;
407 #if TARGET_API_MAC_CARBON
413 SetPort((GrafPtr
)GetWindowPort(window
));
414 s
= mac_windowsession(window
);
415 GlobalToLocal(&mouse
);
416 part
= FindControl(mouse
, window
, &control
);
417 if (control
== s
->scrollbar
) {
418 #if TARGET_API_MAC_CARBON
419 SetCursor(GetQDGlobalsArrow(&arrow
));
420 RectRgn(cursrgn
, GetControlBounds(s
->scrollbar
, &rect
));
422 SetCursor(&qd
.arrow
);
423 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
426 x
= mouse
.h
/ s
->font_width
;
427 y
= mouse
.v
/ s
->font_height
;
429 #if TARGET_API_MAC_CARBON
430 SetCursor(GetQDGlobalsArrow(&arrow
));
432 SetCursor(&qd
.arrow
);
435 SetCursor(*GetCursor(iBeamCursor
));
436 /* Ask for shape changes if we leave this character cell. */
437 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
438 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
440 #if TARGET_API_MAC_CARBON
442 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
443 SectRgn(cursrgn
, visrgn
, cursrgn
);
446 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
451 * Enable/disable menu items based on the active terminal window.
453 #if TARGET_API_MAC_CARBON
454 #define DisableItem DisableMenuItem
455 #define EnableItem EnableMenuItem
457 static void mac_adjusttermmenus(WindowPtr window
)
461 #if !TARGET_API_MAC_CARBON
465 s
= mac_windowsession(window
);
466 menu
= GetMenuHandle(mFile
);
467 DisableItem(menu
, iSave
); /* XXX enable if modified */
468 EnableItem(menu
, iSaveAs
);
469 EnableItem(menu
, iChange
);
470 EnableItem(menu
, iDuplicate
);
471 menu
= GetMenuHandle(mEdit
);
473 DisableItem(menu
, iUndo
);
474 DisableItem(menu
, iCut
);
475 if (1/*s->term->selstate == SELECTED*/)
476 EnableItem(menu
, iCopy
);
478 DisableItem(menu
, iCopy
);
479 #if TARGET_API_MAC_CARBON
482 if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) == noTypeErr
)
484 DisableItem(menu
, iPaste
);
486 EnableItem(menu
, iPaste
);
487 DisableItem(menu
, iClear
);
488 EnableItem(menu
, iSelectAll
);
489 menu
= GetMenuHandle(mWindow
);
491 EnableItem(menu
, iShowEventLog
);
494 static void mac_menuterm(WindowPtr window
, short menu
, short item
)
498 s
= mac_windowsession(window
);
506 term_do_paste(s
->term
);
520 static void mac_clickterm(WindowPtr window
, EventRecord
*event
)
524 ControlHandle control
;
526 static ControlActionUPP mac_scrolltracker_upp
= NULL
;
528 s
= mac_windowsession(window
);
529 SetPort((GrafPtr
)GetWindowPort(window
));
530 mouse
= event
->where
;
531 GlobalToLocal(&mouse
);
532 part
= FindControl(mouse
, window
, &control
);
533 if (control
== s
->scrollbar
) {
535 case kControlIndicatorPart
:
536 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
537 term_scroll(s
->term
, +1, GetControlValue(control
));
539 case kControlUpButtonPart
:
540 case kControlDownButtonPart
:
541 case kControlPageUpPart
:
542 case kControlPageDownPart
:
543 if (mac_scrolltracker_upp
== NULL
)
544 mac_scrolltracker_upp
=
545 NewControlActionUPP(&mac_scrolltracker
);
546 TrackControl(control
, mouse
, mac_scrolltracker_upp
);
550 text_click(s
, event
);
554 static void text_click(Session
*s
, EventRecord
*event
)
558 static UInt32 lastwhen
= 0;
559 static Session
*lastsess
= NULL
;
560 static int lastrow
= -1, lastcol
= -1;
561 static Mouse_Action lastact
= MA_NOTHING
;
563 SetPort((GrafPtr
)GetWindowPort(s
->window
));
564 localwhere
= event
->where
;
565 GlobalToLocal(&localwhere
);
567 col
= PTOCC(localwhere
.h
);
568 row
= PTOCR(localwhere
.v
);
569 if (event
->when
- lastwhen
< GetDblTime() &&
570 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
571 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
572 lastact
== MA_2CLK ? MA_3CLK
:
573 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
576 term_mouse(s
->term
, MBT_LEFT
,
577 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
578 lastact
, col
, row
, event
->modifiers
& shiftKey
,
579 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
583 while (StillDown()) {
584 GetMouse(&localwhere
);
585 col
= PTOCC(localwhere
.h
);
586 row
= PTOCR(localwhere
.v
);
587 term_mouse(s
->term
, MBT_LEFT
,
588 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
589 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
590 event
->modifiers
& controlKey
,
591 event
->modifiers
& optionKey
);
592 if (row
> s
->term
->rows
- 1)
593 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
595 term_scroll(s
->term
, 0, row
);
597 term_mouse(s
->term
, MBT_LEFT
,
598 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
599 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
600 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
601 lastwhen
= TickCount();
604 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
)
606 #if !TARGET_API_MAC_CARBON
609 ByteCount iread
, olen
;
617 * See "Programming with the Text Encoding Conversion Manager"
618 * Appendix E for Unicode scrap conventions.
620 * XXX Maybe PICT scrap too.
622 if (ZeroScrap() != noErr
)
624 PutScrap(len
* sizeof(*data
), kScrapFlavorTypeUnicode
, data
);
626 /* Replace LINE SEPARATORs with CR for TEXT output. */
627 for (i
= 0; i
< len
; i
++)
628 if (data
[i
] == 0x2028)
631 mactextbuf
= snewn(len
, char); /* XXX DBCS */
632 if (s
->uni_to_font
!= NULL
) {
633 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
635 kUnicodeUseFallbacksMask
,
637 len
, &iread
, &olen
, mactextbuf
);
638 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
640 } else if (s
->font_charset
!= CS_NONE
) {
642 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
643 s
->font_charset
, NULL
, ".", 1);
646 PutScrap(olen
, kScrapFlavorTypeText
, mactextbuf
);
649 stsz
= offsetof(StScrpRec
, scrpStyleTab
) + sizeof(ScrpSTElement
);
650 stsc
= smalloc(stsz
);
651 stsc
->scrpNStyles
= 1;
652 stsc
->scrpStyleTab
[0].scrpStartChar
= 0;
653 stsc
->scrpStyleTab
[0].scrpHeight
= s
->font_height
;
654 stsc
->scrpStyleTab
[0].scrpAscent
= s
->font_ascent
;
655 stsc
->scrpStyleTab
[0].scrpFont
= s
->fontnum
;
656 stsc
->scrpStyleTab
[0].scrpFace
= 0;
657 stsc
->scrpStyleTab
[0].scrpSize
= s
->cfg
.font
.size
;
658 stsc
->scrpStyleTab
[0].scrpColor
.red
= 0;
659 stsc
->scrpStyleTab
[0].scrpColor
.green
= 0;
660 stsc
->scrpStyleTab
[0].scrpColor
.blue
= 0;
661 PutScrap(stsz
, kScrapFlavorTypeTextStyle
, stsc
);
666 void get_clip(void *frontend
, wchar_t **p
, int *lenp
)
668 #if TARGET_API_MAC_CARBON
671 Session
*s
= frontend
;
672 static Handle h
= NULL
;
673 static wchar_t *data
= NULL
;
678 TextToUnicodeInfo scrap_to_uni
;
679 ByteCount iread
, olen
;
693 if (GetScrap(NULL
, kScrapFlavorTypeUnicode
, &offset
) > 0) {
697 GetScrap(h
, kScrapFlavorTypeUnicode
, &offset
) / sizeof(**p
);
700 } else if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) > 0) {
701 texth
= NewHandle(0);
702 textlen
= GetScrap(texth
, kScrapFlavorTypeText
, &offset
);
704 data
= snewn(textlen
, wchar_t);
705 /* XXX should use 'styl' scrap if it's there. */
706 if (mac_gestalts
.encvvers
!= 0 &&
707 UpgradeScriptInfoToTextEncoding(smSystemScript
,
708 kTextLanguageDontCare
,
709 kTextRegionDontCare
, NULL
,
711 CreateTextToUnicodeInfoByEncoding(enc
, &scrap_to_uni
) ==
713 err
= ConvertFromTextToUnicode(scrap_to_uni
, textlen
,
714 *texth
, 0, 0, NULL
, NULL
, NULL
,
716 &iread
, &olen
, data
);
717 DisposeTextToUnicodeInfo(&scrap_to_uni
);
720 *lenp
= olen
/ sizeof(**p
);
727 charset_from_macenc(GetScriptManagerVariable(smSysScript
),
728 GetScriptManagerVariable(smRegionCode
),
729 mac_gestalts
.sysvers
, NULL
);
730 if (charset
!= CS_NONE
) {
732 *lenp
= charset_to_unicode(&tptr
, &textlen
, data
,
733 textlen
* 2, charset
, NULL
,
738 DisposeHandle(texth
);
747 static pascal void mac_scrolltracker(ControlHandle control
, short part
)
751 #if TARGET_API_MAC_CARBON
752 s
= mac_windowsession(GetControlOwner(control
));
754 s
= mac_windowsession((*control
)->contrlOwner
);
757 case kControlUpButtonPart
:
758 term_scroll(s
->term
, 0, -1);
760 case kControlDownButtonPart
:
761 term_scroll(s
->term
, 0, +1);
763 case kControlPageUpPart
:
764 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
766 case kControlPageDownPart
:
767 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
772 static void mac_keyterm(WindowPtr window
, EventRecord
*event
)
774 Session
*s
= mac_windowsession(window
);
775 Key_Sym keysym
= PK_NULL
;
776 unsigned int mods
= 0, flags
= PKF_NUMLOCK
;
780 ScriptCode key_script
;
785 fprintf(stderr
, "Got key event %08x\n", event
->message
);
788 /* No meta key yet -- that'll be rather fun. */
790 /* Keys that we handle locally */
791 if (event
->modifiers
& shiftKey
) {
792 switch ((event
->message
& keyCodeMask
) >> 8) {
793 case 0x74: /* shift-pageup */
794 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
796 case 0x79: /* shift-pagedown */
797 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
802 if (event
->modifiers
& shiftKey
)
804 if (event
->modifiers
& controlKey
)
806 if (event
->what
== autoKey
)
809 /* Mac key events consist of a virtual key code and a character code. */
811 switch ((event
->message
& keyCodeMask
) >> 8) {
812 case 0x24: keysym
= PK_RETURN
; break;
813 case 0x30: keysym
= PK_TAB
; break;
814 case 0x33: keysym
= PK_BACKSPACE
; break;
815 case 0x35: keysym
= PK_ESCAPE
; break;
817 case 0x7A: keysym
= PK_F1
; break;
818 case 0x78: keysym
= PK_F2
; break;
819 case 0x63: keysym
= PK_F3
; break;
820 case 0x76: keysym
= PK_F4
; break;
821 case 0x60: keysym
= PK_F5
; break;
822 case 0x61: keysym
= PK_F6
; break;
823 case 0x62: keysym
= PK_F7
; break;
824 case 0x64: keysym
= PK_F8
; break;
825 case 0x65: keysym
= PK_F9
; break;
826 case 0x6D: keysym
= PK_F10
; break;
827 case 0x67: keysym
= PK_F11
; break;
828 case 0x6F: keysym
= PK_F12
; break;
829 case 0x69: keysym
= PK_F13
; break;
830 case 0x6B: keysym
= PK_F14
; break;
831 case 0x71: keysym
= PK_F15
; break;
833 case 0x72: keysym
= PK_INSERT
; break;
834 case 0x73: keysym
= PK_HOME
; break;
835 case 0x74: keysym
= PK_PAGEUP
; break;
836 case 0x75: keysym
= PK_DELETE
; break;
837 case 0x77: keysym
= PK_END
; break;
838 case 0x79: keysym
= PK_PAGEDOWN
; break;
840 case 0x47: keysym
= PK_PF1
; break;
841 case 0x51: keysym
= PK_PF2
; break;
842 case 0x4B: keysym
= PK_PF3
; break;
843 case 0x43: keysym
= PK_PF4
; break;
844 case 0x4E: keysym
= PK_KPMINUS
; break;
845 case 0x45: keysym
= PK_KPCOMMA
; break;
846 case 0x41: keysym
= PK_KPDECIMAL
; break;
847 case 0x4C: keysym
= PK_KPENTER
; break;
848 case 0x52: keysym
= PK_KP0
; break;
849 case 0x53: keysym
= PK_KP1
; break;
850 case 0x54: keysym
= PK_KP2
; break;
851 case 0x55: keysym
= PK_KP3
; break;
852 case 0x56: keysym
= PK_KP4
; break;
853 case 0x57: keysym
= PK_KP5
; break;
854 case 0x58: keysym
= PK_KP6
; break;
855 case 0x59: keysym
= PK_KP7
; break;
856 case 0x5B: keysym
= PK_KP8
; break;
857 case 0x5C: keysym
= PK_KP9
; break;
859 case 0x7B: keysym
= PK_LEFT
; break;
860 case 0x7C: keysym
= PK_RIGHT
; break;
861 case 0x7D: keysym
= PK_DOWN
; break;
862 case 0x7E: keysym
= PK_UP
; break;
865 /* Map from key script to Unicode. */
866 txt
[0] = event
->message
& charCodeMask
;
867 key_script
= GetScriptManagerVariable(smKeyScript
);
869 if (mac_gestalts
.encvvers
!= 0) {
870 static TextToUnicodeInfo key_to_uni
= NULL
;
871 static ScriptCode key_to_uni_script
;
873 ByteCount iread
, olen
;
876 if (key_to_uni
!= NULL
&& key_to_uni_script
!= key_script
)
877 DisposeTextToUnicodeInfo(&key_to_uni
);
878 if (key_to_uni
== NULL
|| key_to_uni_script
!= key_script
) {
879 if (UpgradeScriptInfoToTextEncoding(key_script
,
880 kTextLanguageDontCare
,
881 kTextRegionDontCare
, NULL
,
883 CreateTextToUnicodeInfoByEncoding(enc
, &key_to_uni
) == noErr
)
884 key_to_uni_script
= key_script
;
888 if (key_to_uni
!= NULL
) {
889 err
= ConvertFromTextToUnicode(key_to_uni
, 1, txt
,
890 (kUnicodeKeepInfoMask
|
891 kUnicodeStringUnterminatedMask
),
893 sizeof(utxt
), &iread
, &olen
, utxt
);
895 len
= olen
/ sizeof(*utxt
);
902 charset
= charset_from_macenc(key_script
,
903 GetScriptManagerVariable(smRegionCode
),
904 mac_gestalts
.sysvers
, NULL
);
905 if (charset
!= CS_NONE
) {
906 len
= charset_to_unicode(&tptr
, &tlen
, utxt
, sizeof(utxt
), charset
,
910 term_key(s
->term
, keysym
, utxt
, len
, mods
, flags
);
913 void request_paste(void *frontend
)
915 Session
*s
= frontend
;
918 * In the Mac OS, pasting is synchronous: we can read the
919 * clipboard with no difficulty, so request_paste() can just go
922 term_do_paste(s
->term
);
933 static void mac_growterm(WindowPtr window
, EventRecord
*event
)
937 int newrows
, newcols
;
939 #if !TARGET_API_MAC_CARBON
940 DragGrayRgnUPP draghooksave
;
945 s
= mac_windowsession(window
);
947 #if !TARGET_API_MAC_CARBON
948 draghooksave
= LMGetDragHook();
949 growterm_state
.oldmsg
[0] = '\0';
950 growterm_state
.zeromouse
= event
->where
;
951 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
952 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
953 growterm_state
.s
= s
;
956 BackColor(whiteColor
);
957 ForeColor(blackColor
);
958 TextFont(systemFont
);
962 SetRect(&growterm_state
.msgrect
, 0, 0,
963 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
964 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
965 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
968 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
969 grow_result
= GrowWindow(window
, event
->where
, &limits
);
971 #if !TARGET_API_MAC_CARBON
972 DisposeDragGrayRgnUPP(LMGetDragHook());
973 LMSetDragHook(draghooksave
);
974 InvalRect(&growterm_state
.msgrect
);
979 if (grow_result
!= 0) {
980 newrows
= HiWord(grow_result
) / s
->font_height
;
981 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
982 mac_adjustsize(s
, newrows
, newcols
);
983 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
987 #if !TARGET_API_MAC_CARBON
988 static pascal void mac_growtermdraghook(void)
990 Session
*s
= growterm_state
.s
;
994 unsigned char pbuf
[20];
995 int newrows
, newcols
;
998 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
999 if (newrows
< 1) newrows
= 1;
1000 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
1001 if (newcols
< 1) newcols
= 1;
1002 sprintf(buf
, "%dx%d", newcols
, newrows
);
1003 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
1005 strcpy(growterm_state
.oldmsg
, buf
);
1006 c2pstrcpy(pbuf
, buf
);
1009 SetPort(growterm_state
.s
->window
);
1010 EraseRect(&growterm_state
.msgrect
);
1011 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
1017 void mac_closeterm(WindowPtr window
)
1019 Session
*s
= mac_windowsession(window
);
1021 /* XXX warn on close */
1022 HideWindow(s
->window
);
1024 s
->next
->prev
= s
->prev
;
1025 ldisc_free(s
->ldisc
);
1026 s
->back
->free(s
->backhandle
);
1027 log_free(s
->logctx
);
1028 if (s
->uni_to_font
!= NULL
)
1029 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
1031 mac_freeeventlog(s
);
1032 sfree((WinInfo
*)GetWRefCon(s
->window
));
1033 DisposeWindow(s
->window
);
1034 DisposePalette(s
->palette
);
1038 static void mac_activateterm(WindowPtr window
, EventRecord
*event
)
1041 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1043 s
= mac_windowsession(window
);
1044 term_set_focus(s
->term
, active
);
1045 term_update(s
->term
);
1047 ShowControl(s
->scrollbar
);
1049 if (HAVE_COLOR_QD())
1050 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
1052 BackColor(blackColor
);
1053 HideControl(s
->scrollbar
);
1055 mac_drawgrowicon(s
);
1058 static void mac_updateterm(WindowPtr window
)
1062 #if TARGET_API_MAC_CARBON
1066 s
= mac_windowsession(window
);
1067 SetPort((GrafPtr
)GetWindowPort(window
));
1068 BeginUpdate(window
);
1070 #if TARGET_API_MAC_CARBON
1072 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1073 GetRegionBounds(visrgn
, &bbox
);
1075 bbox
= (*window
->visRgn
)->rgnBBox
;
1077 term_paint(s
->term
, s
, PTOCC(bbox
.left
), PTOCR(bbox
.top
),
1078 PTOCC(bbox
.right
), PTOCR(bbox
.bottom
), 1);
1079 /* Restore default colours in case the Window Manager uses them */
1080 if (HAVE_COLOR_QD()) {
1081 PmForeColor(DEFAULT_FG
);
1082 PmBackColor(DEFAULT_BG
);
1084 ForeColor(whiteColor
);
1085 BackColor(blackColor
);
1087 if (FrontWindow() != window
)
1088 #if TARGET_API_MAC_CARBON
1089 EraseRect(GetControlBounds(s
->scrollbar
, &bbox
));
1090 UpdateControls(window
, visrgn
);
1093 EraseRect(&(*s
->scrollbar
)->contrlRect
);
1094 UpdateControls(window
, window
->visRgn
);
1096 mac_drawgrowicon(s
);
1101 static void mac_drawgrowicon(Session
*s
)
1106 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1108 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1109 * See Tech Note TB575 for details.
1111 #if TARGET_API_MAC_CARBON
1112 GetPortBounds(GetWindowPort(s
->window
), &clip
);
1114 clip
= s
->window
->portRect
;
1116 clip
.left
= clip
.right
- 15;
1120 DrawGrowIcon(s
->window
);
1122 DisposeRgn(savergn
);
1125 struct do_text_args
{
1136 * Call from the terminal emulator to draw a bit of text
1138 * x and y are text row and column (zero-based)
1140 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1141 unsigned long attr
, int lattr
)
1145 struct do_text_args a
;
1146 RgnHandle textrgn
, saveclip
;
1147 #if TARGET_API_MAC_CARBON
1150 char mactextbuf
[1024];
1151 wchar_t *unitextptr
;
1153 ByteCount iread
, olen
;
1155 static DeviceLoopDrawingUPP do_text_for_device_upp
= NULL
;
1157 assert(len
<= 1024);
1159 /* SGT, 2004-10-14: I don't know how to support combining characters
1160 * on the Mac. Hopefully the first person to fail this assertion will
1161 * know how to do it better than me... */
1162 assert(!(attr
& TATTR_COMBINING
));
1164 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1166 fontwidth
= s
->font_width
;
1167 if ((lattr
& LATTR_MODE
) != LATTR_NORM
)
1170 /* First check this text is relevant */
1171 a
.textrect
.top
= y
* s
->font_height
;
1172 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
1173 a
.textrect
.left
= x
* fontwidth
;
1174 a
.textrect
.right
= (x
+ len
) * fontwidth
;
1175 if (a
.textrect
.right
> s
->term
->cols
* s
->font_width
)
1176 a
.textrect
.right
= s
->term
->cols
* s
->font_width
;
1177 #if TARGET_API_MAC_CARBON
1179 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1180 if (!RectInRgn(&a
.textrect
, visrgn
)) {
1186 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
1190 if (s
->uni_to_font
!= NULL
) {
1191 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
1192 text
, kUnicodeUseFallbacksMask
,
1193 0, NULL
, NULL
, NULL
,
1194 1024, &iread
, &olen
, mactextbuf
);
1195 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
1197 } else if (s
->font_charset
!= CS_NONE
) {
1198 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1199 unitextptr
= (wchar_t *)text
;
1200 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
1201 s
->font_charset
, NULL
, ".", 1);
1206 a
.text
= mactextbuf
;
1210 switch (lattr
& LATTR_MODE
) {
1212 TextSize(s
->cfg
.font
.size
);
1213 a
.numer
= s
->font_stdnumer
;
1214 a
.denom
= s
->font_stddenom
;
1217 TextSize(s
->cfg
.font
.size
);
1218 a
.numer
= s
->font_widenumer
;
1219 a
.denom
= s
->font_widedenom
;
1223 TextSize(s
->cfg
.font
.size
* 2);
1224 a
.numer
= s
->font_bignumer
;
1225 a
.denom
= s
->font_bigdenom
;
1228 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1229 TextFont(s
->fontnum
);
1230 style
= s
->cfg
.font
.face
;
1231 if ((attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1233 if (attr
& ATTR_UNDER
)
1237 if (HAVE_COLOR_QD())
1239 SpaceExtra(s
->font_boldadjust
<< 16);
1240 CharExtra(s
->font_boldadjust
<< 16);
1245 saveclip
= NewRgn();
1247 ClipRect(&a
.textrect
);
1249 RectRgn(textrgn
, &a
.textrect
);
1250 if (HAVE_COLOR_QD()) {
1251 if (do_text_for_device_upp
== NULL
)
1252 do_text_for_device_upp
=
1253 NewDeviceLoopDrawingUPP(&do_text_for_device
);
1254 DeviceLoop(textrgn
, do_text_for_device_upp
, (long)&a
, 0);
1256 do_text_for_device(1, 0, NULL
, (long)&a
);
1258 DisposeRgn(saveclip
);
1259 DisposeRgn(textrgn
);
1260 /* Tell the window manager about it in case this isn't an update */
1261 #if TARGET_API_MAC_CARBON
1262 ValidWindowRect(s
->window
, &a
.textrect
);
1264 ValidRect(&a
.textrect
);
1268 static pascal void do_text_for_device(short depth
, short devflags
,
1269 GDHandle device
, long cookie
)
1271 struct do_text_args
*a
= (struct do_text_args
*)cookie
;
1272 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1273 #if TARGET_API_MAC_CARBON
1274 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(a
->s
->window
));
1276 QDProcsPtr gp
= a
->s
->window
->grafProcs
;
1279 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1280 reverse
= a
->attr
& ATTR_REVERSE
;
1282 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1285 if (HAVE_COLOR_QD()) {
1287 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1288 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1291 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1292 * boldens the background, because that's all we can do.
1294 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1295 bgcolour
= DEFAULT_BG
;
1299 fgcolour
= bgcolour
;
1302 if (bright
&& depth
> 2)
1303 if (fgcolour
< 16) fgcolour
|=8;
1304 else if (fgcolour
>= 256) fgcolour
|=1;
1305 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1306 fgcolour
= CURSOR_FG
;
1307 bgcolour
= CURSOR_BG
;
1309 PmForeColor(fgcolour
);
1310 PmBackColor(bgcolour
);
1311 } else { /* No Color Quickdraw */
1312 /* XXX This should be done with a _little_ more configurability */
1314 ForeColor(blackColor
);
1315 BackColor(whiteColor
);
1317 ForeColor(whiteColor
);
1318 BackColor(blackColor
);
1322 EraseRect(&a
->textrect
);
1323 switch (a
->lattr
& LATTR_MODE
) {
1326 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1329 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
* 2);
1332 MoveTo(a
->textrect
.left
,
1333 a
->textrect
.top
- a
->s
->font_height
+ a
->s
->font_ascent
* 2);
1336 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1338 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
, gp
->textProc
);
1340 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1342 if (a
->attr
& TATTR_PASCURS
) {
1349 PmForeColor(CURSOR_BG
);
1352 FrameRect(&a
->textrect
);
1356 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1357 unsigned long attr
, int lattr
)
1360 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1364 * Call from the terminal emulator to get its graphics context.
1365 * Should probably be called start_redraw or something.
1367 void pre_paint(Session
*s
)
1370 Rect myrect
, tmprect
;
1371 #if TARGET_API_MAC_CARBON
1375 if (HAVE_COLOR_QD()) {
1376 s
->term
->attr_mask
= 0;
1377 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1378 #if TARGET_API_MAC_CARBON
1380 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1381 GetRegionBounds(visrgn
, &myrect
);
1384 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1386 LocalToGlobal((Point
*)&myrect
.top
);
1387 LocalToGlobal((Point
*)&myrect
.bottom
);
1388 for (gdh
= GetDeviceList();
1390 gdh
= GetNextDevice(gdh
)) {
1391 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1392 TestDeviceAttribute(gdh
, screenActive
) &&
1393 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1394 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1396 if (s
->cfg
.bold_colour
)
1397 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1398 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1401 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1404 s
->term
->attr_mask
= ~0;
1405 return; /* No point checking more screens. */
1410 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1411 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1414 Context
get_ctx(void *frontend
)
1416 Session
*s
= frontend
;
1422 void free_ctx(Context ctx
)
1428 * Presumably this does something in Windows
1430 void post_paint(Session
*s
)
1436 * Set the scroll bar position
1438 * total is the line number of the bottom of the working screen
1439 * start is the line number of the top of the display
1440 * page is the length of the displayed page
1442 void set_sbar(void *frontend
, int total
, int start
, int page
)
1444 Session
*s
= frontend
;
1446 /* We don't redraw until we've set everything up, to avoid glitches */
1447 SetControlMinimum(s
->scrollbar
, 0);
1448 SetControlMaximum(s
->scrollbar
, total
- page
);
1449 SetControlValue(s
->scrollbar
, start
);
1451 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1452 SetControlViewSize(s
->scrollbar
, page
);
1456 void sys_cursor(void *frontend
, int x
, int y
)
1459 * I think his is meaningless under Mac OS.
1464 * This is still called when mode==BELL_VISUAL, even though the
1465 * visual bell is handled entirely within terminal.c, because we
1466 * may want to perform additional actions on any kind of bell (for
1467 * example, taskbar flashing in Windows).
1469 void beep(void *frontend
, int mode
)
1471 if (mode
!= BELL_VISUAL
)
1474 * XXX We should indicate the relevant window and/or use the
1475 * Notification Manager
1479 int char_width(Context ctx
, int uc
)
1482 * Until we support exciting character-set stuff, assume all chars are
1489 * Set icon string -- a no-op here (Windowshade?)
1491 void set_icon(void *frontend
, char *icon
) {
1492 Session
*s
= frontend
;
1497 * Set the window title
1499 void set_title(void *frontend
, char *title
)
1501 Session
*s
= frontend
;
1504 c2pstrcpy(mactitle
, title
);
1505 SetWTitle(s
->window
, mactitle
);
1509 * set or clear the "raw mouse message" mode
1511 void set_raw_mouse_mode(void *frontend
, int activate
)
1513 Session
*s
= frontend
;
1515 s
->raw_mouse
= activate
;
1516 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1520 * Resize the window at the emulator's request
1522 void request_resize(void *frontend
, int w
, int h
)
1524 Session
*s
= frontend
;
1529 /* Arbitrarily clip to the size of the desktop. */
1530 grayrgn
= GetGrayRgn();
1531 #if TARGET_API_MAC_CARBON
1532 GetRegionBounds(grayrgn
, &graybox
);
1534 graybox
= (*grayrgn
)->rgnBBox
;
1536 wlim
= (graybox
.right
- graybox
.left
) / s
->font_width
;
1537 hlim
= (graybox
.bottom
- graybox
.top
) / s
->font_height
;
1538 if (w
> wlim
) w
= wlim
;
1539 if (h
> hlim
) h
= hlim
;
1540 term_size(s
->term
, h
, w
, s
->cfg
.savelines
);
1545 * Iconify (actually collapse) the window at the emulator's request.
1547 void set_iconic(void *frontend
, int iconic
)
1549 Session
*s
= frontend
;
1552 if (mac_gestalts
.apprvers
>= 0x0100 &&
1553 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1554 (features
& kWindowCanCollapse
))
1555 CollapseWindow(s
->window
, iconic
);
1559 * Move the window in response to a server-side request.
1561 void move_window(void *frontend
, int x
, int y
)
1563 Session
*s
= frontend
;
1565 MoveWindow(s
->window
, x
, y
, FALSE
);
1569 * Move the window to the top or bottom of the z-order in response
1570 * to a server-side request.
1572 void set_zorder(void *frontend
, int top
)
1574 Session
*s
= frontend
;
1577 * We also change the input focus to point to the topmost window,
1578 * since that's probably what the Human Interface Guidelines would
1582 SelectWindow(s
->window
);
1584 SendBehind(s
->window
, NULL
);
1588 * Refresh the window in response to a server-side request.
1590 void refresh_window(void *frontend
)
1592 Session
*s
= frontend
;
1594 term_invalidate(s
->term
);
1598 * Maximise or restore the window in response to a server-side
1601 void set_zoomed(void *frontend
, int zoomed
)
1603 Session
*s
= frontend
;
1605 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1609 * Report whether the window is iconic, for terminal reports.
1611 int is_iconic(void *frontend
)
1613 Session
*s
= frontend
;
1616 if (mac_gestalts
.apprvers
>= 0x0100 &&
1617 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1618 (features
& kWindowCanCollapse
))
1619 return IsWindowCollapsed(s
->window
);
1624 * Report the window's position, for terminal reports.
1626 void get_window_pos(void *frontend
, int *x
, int *y
)
1628 Session
*s
= frontend
;
1631 #if TARGET_API_MAC_CARBON
1632 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1634 rect
= s
->window
->portRect
;
1641 * Report the window's pixel size, for terminal reports.
1643 void get_window_pixels(void *frontend
, int *x
, int *y
)
1645 Session
*s
= frontend
;
1648 #if TARGET_API_MAC_CARBON
1649 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1651 rect
= s
->window
->portRect
;
1653 *x
= rect
.right
- rect
.left
;
1654 *y
= rect
.bottom
- rect
.top
;
1658 * Return the window or icon title.
1660 char *get_window_title(void *frontend
, int icon
)
1662 Session
*s
= frontend
;
1664 static char title
[256];
1666 GetWTitle(s
->window
, ptitle
);
1667 p2cstrcpy(title
, ptitle
);
1672 * real_palette_set(): This does the actual palette-changing work on behalf
1673 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1674 * is doing a batch of updates.
1676 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1680 if (!HAVE_COLOR_QD())
1682 col
.red
= r
* 0x0101;
1683 col
.green
= g
* 0x0101;
1684 col
.blue
= b
* 0x0101;
1685 SetEntryColor(s
->palette
, n
, &col
);
1689 * Set the logical palette. Called by the terminal emulator.
1691 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1693 Session
*s
= frontend
;
1695 if (!HAVE_COLOR_QD())
1697 real_palette_set(s
, n
, r
, g
, b
);
1698 if (n
== DEFAULT_BG
)
1701 ActivatePalette(s
->window
);
1705 * Reset to the default palette
1707 void palette_reset(void *frontend
)
1709 Session
*s
= frontend
;
1710 /* This maps colour indices in cfg to those used in our palette. */
1711 static const int ww
[] = {
1712 256, 257, 258, 259, 260, 261,
1713 0, 8, 1, 9, 2, 10, 3, 11,
1714 4, 12, 5, 13, 6, 14, 7, 15
1719 if (!HAVE_COLOR_QD())
1722 for (i
= 0; i
< 22; i
++) {
1724 real_palette_set(s
,w
,
1725 s
->cfg
.colours
[i
][0],
1726 s
->cfg
.colours
[i
][1],
1727 s
->cfg
.colours
[i
][2]);
1729 for (i
= 0; i
< 240; i
++) {
1731 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1732 real_palette_set(s
,i
+16,
1737 int shade
= i
- 216;
1738 shade
= (shade
+ 1) * 0xFF / (240 - 216 + 1);
1739 real_palette_set(s
,i
+16,shade
,shade
,shade
);
1745 ActivatePalette(s
->window
);
1746 /* Palette Manager will generate update events as required. */
1750 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1753 void do_scroll(Context ctx
, int topline
, int botline
, int lines
)
1757 RgnHandle scrollrgn
= NewRgn();
1758 RgnHandle movedupdate
= NewRgn();
1759 RgnHandle update
= NewRgn();
1760 Point g2l
= { 0, 0 };
1762 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1765 * Work out the part of the update region that will scrolled by
1769 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1770 s
->term
->cols
* s
->font_width
,
1771 (botline
+ 1) * s
->font_height
);
1773 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1774 s
->term
->cols
* s
->font_width
,
1775 (botline
- lines
+ 1) * s
->font_height
);
1776 #if TARGET_API_MAC_CARBON
1777 GetWindowRegion(s
->window
, kWindowUpdateRgn
, movedupdate
);
1779 GetWindowUpdateRgn(s
->window
, movedupdate
);
1781 GlobalToLocal(&g2l
);
1782 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1783 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1784 #if TARGET_API_MAC_CARBON
1785 ValidWindowRgn(s
->window
, movedupdate
);
1787 ValidRgn(movedupdate
);
1789 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1792 if (HAVE_COLOR_QD())
1793 PmBackColor(DEFAULT_BG
);
1795 BackColor(blackColor
); /* XXX make configurable */
1796 SetRect(&r
, 0, topline
* s
->font_height
,
1797 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1798 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1800 #if TARGET_API_MAC_CARBON
1801 InvalWindowRgn(s
->window
, update
);
1802 InvalWindowRgn(s
->window
, movedupdate
);
1805 InvalRgn(movedupdate
);
1808 DisposeRgn(scrollrgn
);
1809 DisposeRgn(movedupdate
);
1813 /* Dummy routine, only required in plink. */
1814 void ldisc_update(void *frontend
, int echo
, int edit
)
1819 * Mac PuTTY doesn't support printing yet.
1821 printer_job
*printer_start_job(char *printer
)
1827 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1831 void printer_finish_job(printer_job
*pj
)
1835 void frontend_keypress(void *handle
)
1838 * Keypress termination in non-Close-On-Exit mode is not
1839 * currently supported in PuTTY proper, because the window
1840 * always has a perfectly good Close button anyway. So we do
1847 * Ask whether to wipe a session log file before writing to it.
1848 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1850 int askappend(void *frontend
, Filename filename
)
1853 /* FIXME: not implemented yet. */
1857 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
1859 Session
*s
= frontend
;
1861 return term_data(s
->term
, is_stderr
, data
, len
);
1867 * c-file-style: "simon"