1 /* $Id: macterm.c,v 1.43 2003/01/12 14:44:12 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>
39 #include <MacMemory.h>
40 #include <MacWindows.h>
41 #include <MixedMode.h>
43 #include <Quickdraw.h>
44 #include <QuickdrawText.h>
45 #include <Resources.h>
49 #include <StandardFile.h>
50 #include <TextCommon.h>
52 #include <ToolUtils.h>
53 #include <UnicodeConverter.h>
68 #define NCOLOURS (lenof(((Config *)0)->colours))
71 #define DEFAULT_FG_BOLD 17
73 #define DEFAULT_BG_BOLD 19
77 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
79 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
82 static void mac_initfont(Session
*);
83 static pascal OSStatus
uni_to_font_fallback(UniChar
*, ByteCount
, ByteCount
*,
84 TextPtr
, ByteCount
, ByteCount
*,
86 ConstUnicodeMappingPtr
);
87 static void mac_initpalette(Session
*);
88 static void mac_adjustwinbg(Session
*);
89 static void mac_adjustsize(Session
*, int, int);
90 static void mac_drawgrowicon(Session
*s
);
91 static pascal void mac_growtermdraghook(void);
92 static pascal void mac_scrolltracker(ControlHandle
, short);
93 static pascal void do_text_for_device(short, short, GDHandle
, long);
94 static int mac_keytrans(Session
*, EventRecord
*, unsigned char *);
95 static void text_click(Session
*, EventRecord
*);
97 void pre_paint(Session
*s
);
98 void post_paint(Session
*s
);
100 #if TARGET_RT_MAC_CFM
101 static RoutineDescriptor mac_scrolltracker_upp
=
102 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo
,
103 (ProcPtr
)mac_scrolltracker
);
104 static RoutineDescriptor do_text_for_device_upp
=
105 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo
,
106 (ProcPtr
)do_text_for_device
);
107 #else /* not TARGET_RT_MAC_CFM */
108 #define mac_scrolltracker_upp mac_scrolltracker
109 #define do_text_for_device_upp do_text_for_device
110 #endif /* not TARGET_RT_MAC_CFM */
112 void mac_opensession(void) {
114 StandardFileReply sfr
;
115 static const OSType sftypes
[] = { 'Sess', 0, 0, 0 };
119 s
= smalloc(sizeof(*s
));
120 memset(s
, 0, sizeof(*s
));
122 StandardGetFile(NULL
, 1, sftypes
, &sfr
);
123 if (!sfr
.sfGood
) goto fail
;
125 sesshandle
= open_settings_r_fsp(&sfr
.sfFile
);
126 if (sesshandle
== NULL
) goto fail
;
127 load_open_settings(sesshandle
, TRUE
, &s
->cfg
);
128 close_settings_r(sesshandle
);
131 * Select protocol. This is farmed out into a table in a
132 * separate file to enable an ssh-free variant.
135 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
136 if (backends
[i
].protocol
== s
->cfg
.protocol
) {
137 s
->back
= backends
[i
].backend
;
140 if (s
->back
== NULL
) {
141 fatalbox("Unsupported protocol number found");
151 void mac_startsession(Session
*s
)
155 /* XXX: Own storage management? */
157 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
159 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
160 SetWRefCon(s
->window
, (long)s
);
161 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
162 s
->term
= term_init(&s
->cfg
, s
);
166 if (HAVE_COLOR_QD()) {
167 /* Set to FALSE to not get palette updates in the background. */
168 SetPalette(s
->window
, s
->palette
, TRUE
);
169 ActivatePalette(s
->window
);
172 s
->logctx
= log_init(s
);
173 term_provide_logctx(s
->term
, s
->logctx
);
175 errmsg
= s
->back
->init(s
->term
, &s
->backhandle
, s
->cfg
.host
, s
->cfg
.port
,
176 &s
->realhost
, s
->cfg
.tcp_nodelay
);
178 fatalbox("%s", errmsg
);
179 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
181 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
183 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
184 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
186 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
187 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
189 ShowWindow(s
->window
);
191 s
->prev
= s
->next
->prev
;
193 s
->next
->prev
= &s
->next
;
197 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
199 static void mac_initfont(Session
*s
) {
206 c2pstrcpy(macfont
, s
->cfg
.font
);
207 GetFNum(macfont
, &s
->fontnum
);
208 TextFont(s
->fontnum
);
209 TextFace(s
->cfg
.fontisbold ? bold
: 0);
210 TextSize(s
->cfg
.fontheight
);
212 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
213 s
->font_ascent
= fi
.ascent
;
214 s
->font_leading
= fi
.leading
;
215 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
216 if (!s
->cfg
.bold_colour
) {
218 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
220 s
->font_boldadjust
= 0;
222 if (s
->uni_to_font
!= NULL
)
223 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
224 if (mac_gestalts
.encvvers
!= 0 &&
225 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
226 kTextLanguageDontCare
,
227 kTextRegionDontCare
, macfont
,
229 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) == noErr
) {
230 if (uni_to_font_fallback_upp
== NULL
)
231 uni_to_font_fallback_upp
=
232 NewUnicodeToTextFallbackProc(&uni_to_font_fallback
);
233 fbflags
= kUnicodeFallbackCustomOnly
;
234 if (mac_gestalts
.uncvattr
& kTECAddFallbackInterruptMask
)
235 fbflags
|= kUnicodeFallbackInterruptSafeMask
;
236 if (SetFallbackUnicodeToText(s
->uni_to_font
,
237 uni_to_font_fallback_upp
, fbflags
, NULL
) != noErr
) {
238 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
243 s
->uni_to_font
= NULL
;
245 charset_from_macenc(FontToScript(s
->fontnum
),
246 GetScriptManagerVariable(smRegionCode
),
247 mac_gestalts
.sysvers
, s
->cfg
.font
);
250 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
253 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
254 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
255 ByteCount
*ousedp
, LogicalAddress
*cookie
, ConstUnicodeMappingPtr mapping
)
259 return kTECOutputBufferFullStatus
;
261 * What I'd _like_ to do here is to somehow generate the
262 * missing-character glyph that every font is required to have.
263 * Unfortunately (and somewhat surprisingly), I can't find any way
264 * to actually ask for it explicitly. Bah.
273 * Called every time round the event loop.
275 void mac_pollterm(void)
279 for (s
= sesslist
; s
!= NULL
; s
= s
->next
) {
281 term_update(s
->term
);
286 * To be called whenever the window size changes.
287 * rows and cols should be desired values.
288 * It's assumed the terminal emulator will be informed, and will set rows
291 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
292 int winwidth
, winheight
;
294 winwidth
= newcols
* s
->font_width
+ 15;
295 winheight
= newrows
* s
->font_height
;
296 SizeWindow(s
->window
, winwidth
, winheight
, true);
297 HideControl(s
->scrollbar
);
298 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
299 SizeControl(s
->scrollbar
, 16, winheight
- 13);
300 ShowControl(s
->scrollbar
);
304 static void mac_initpalette(Session
*s
) {
306 if (!HAVE_COLOR_QD())
309 * Most colours should be inhibited on 2bpp displays.
310 * Palette manager documentation suggests inhibiting all tolerant colours
311 * on greyscale displays.
313 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
314 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
315 #define PM_TOLERANCE 0x2000
316 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
317 if (s
->palette
== NULL
)
318 fatalbox("Unable to create palette");
319 /* In 2bpp, these are the colours we want most. */
320 SetEntryUsage(s
->palette
, DEFAULT_BG
,
321 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
322 SetEntryUsage(s
->palette
, DEFAULT_FG
,
323 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
324 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
325 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
326 SetEntryUsage(s
->palette
, CURSOR_BG
,
327 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
332 * Set the background colour of the window correctly. Should be
333 * called whenever the default background changes.
335 static void mac_adjustwinbg(Session
*s
) {
337 if (!HAVE_COLOR_QD())
340 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
341 SetWindowContentColor(s
->window
,
342 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
346 if (s
->wctab
== NULL
)
347 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
348 if (s
->wctab
== NULL
)
349 return; /* do without */
350 (*s
->wctab
)->wCSeed
= 0;
351 (*s
->wctab
)->wCReserved
= 0;
352 (*s
->wctab
)->ctSize
= 0;
353 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
354 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
355 SetWinColor(s
->window
, s
->wctab
);
360 * Set the cursor shape correctly
362 void mac_adjusttermcursor(WindowPtr window
, Point mouse
, RgnHandle cursrgn
) {
364 ControlHandle control
;
369 s
= (Session
*)GetWRefCon(window
);
370 GlobalToLocal(&mouse
);
371 part
= FindControl(mouse
, window
, &control
);
372 if (control
== s
->scrollbar
) {
373 SetCursor(&qd
.arrow
);
374 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
375 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
377 x
= mouse
.h
/ s
->font_width
;
378 y
= mouse
.v
/ s
->font_height
;
380 SetCursor(&qd
.arrow
);
382 SetCursor(*GetCursor(iBeamCursor
));
383 /* Ask for shape changes if we leave this character cell. */
384 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
385 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
386 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
391 * Enable/disable menu items based on the active terminal window.
393 void mac_adjusttermmenus(WindowPtr window
) {
398 s
= (Session
*)GetWRefCon(window
);
399 menu
= GetMenuHandle(mEdit
);
401 DisableItem(menu
, iUndo
);
402 DisableItem(menu
, iCut
);
403 if (1/*s->term->selstate == SELECTED*/)
404 EnableItem(menu
, iCopy
);
406 DisableItem(menu
, iCopy
);
407 if (GetScrap(NULL
, 'TEXT', &offset
) == noTypeErr
)
408 DisableItem(menu
, iPaste
);
410 EnableItem(menu
, iPaste
);
411 DisableItem(menu
, iClear
);
412 EnableItem(menu
, iSelectAll
);
415 void mac_menuterm(WindowPtr window
, short menu
, short item
) {
418 s
= (Session
*)GetWRefCon(window
);
426 term_do_paste(s
->term
);
432 void mac_clickterm(WindowPtr window
, EventRecord
*event
) {
435 ControlHandle control
;
438 s
= (Session
*)GetWRefCon(window
);
440 mouse
= event
->where
;
441 GlobalToLocal(&mouse
);
442 part
= FindControl(mouse
, window
, &control
);
443 if (control
== s
->scrollbar
) {
445 case kControlIndicatorPart
:
446 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
447 term_scroll(s
->term
, +1, GetControlValue(control
));
449 case kControlUpButtonPart
:
450 case kControlDownButtonPart
:
451 case kControlPageUpPart
:
452 case kControlPageDownPart
:
453 TrackControl(control
, mouse
, &mac_scrolltracker_upp
);
457 text_click(s
, event
);
461 static void text_click(Session
*s
, EventRecord
*event
) {
464 static UInt32 lastwhen
= 0;
465 static Session
*lastsess
= NULL
;
466 static int lastrow
= -1, lastcol
= -1;
467 static Mouse_Action lastact
= MA_NOTHING
;
470 localwhere
= event
->where
;
471 GlobalToLocal(&localwhere
);
473 col
= PTOCC(localwhere
.h
);
474 row
= PTOCR(localwhere
.v
);
475 if (event
->when
- lastwhen
< GetDblTime() &&
476 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
477 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
478 lastact
== MA_2CLK ? MA_3CLK
:
479 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
482 /* Fake right button with shift key */
483 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
484 lastact
, col
, row
, event
->modifiers
& shiftKey
,
485 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
489 while (StillDown()) {
490 GetMouse(&localwhere
);
491 col
= PTOCC(localwhere
.h
);
492 row
= PTOCR(localwhere
.v
);
494 event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
495 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
496 event
->modifiers
& controlKey
,
497 event
->modifiers
& optionKey
);
498 if (row
> s
->term
->rows
- 1)
499 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
501 term_scroll(s
->term
, 0, row
);
503 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
504 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
505 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
506 lastwhen
= TickCount();
509 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
522 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
) {
525 * See "Programming with the Text Encoding Conversion Manager"
526 * Appendix E for Unicode scrap conventions.
528 * XXX Need to support TEXT/styl scrap as well.
529 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
530 * XXX Maybe PICT scrap too.
532 if (ZeroScrap() != noErr
)
534 PutScrap(len
* sizeof(*data
), 'utxt', data
);
537 void get_clip(void *frontend
, wchar_t **p
, int *lenp
) {
538 Session
*s
= frontend
;
539 static Handle h
= NULL
;
548 /* XXX Support TEXT-format scrap as well. */
549 if (GetScrap(NULL
, 'utxt', &offset
) > 0) {
551 *lenp
= GetScrap(h
, 'utxt', &offset
) / sizeof(**p
);
554 if (*p
== NULL
|| *lenp
<= 0)
555 fatalbox("Empty scrap");
562 static pascal void mac_scrolltracker(ControlHandle control
, short part
) {
565 s
= (Session
*)GetWRefCon((*control
)->contrlOwner
);
567 case kControlUpButtonPart
:
568 term_scroll(s
->term
, 0, -1);
570 case kControlDownButtonPart
:
571 term_scroll(s
->term
, 0, +1);
573 case kControlPageUpPart
:
574 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
576 case kControlPageDownPart
:
577 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
598 #define K_INSERT 0x7200
599 #define K_HOME 0x7300
600 #define K_PRIOR 0x7400
601 #define K_DELETE 0x7500
603 #define K_NEXT 0x7900
604 #define K_LEFT 0x7b00
605 #define K_RIGHT 0x7c00
606 #define K_DOWN 0x7d00
618 #define KP_CLEAR 0x4700
619 #define KP_EQUAL 0x5100
620 #define KP_SLASH 0x4b00
621 #define KP_STAR 0x4300
622 #define KP_PLUS 0x4500
623 #define KP_MINUS 0x4e00
624 #define KP_DOT 0x4100
625 #define KP_ENTER 0x4c00
627 void mac_keyterm(WindowPtr window
, EventRecord
*event
) {
628 unsigned char buf
[20];
632 s
= (Session
*)GetWRefCon(window
);
633 len
= mac_keytrans(s
, event
, buf
);
634 ldisc_send(s
->ldisc
, (char *)buf
, len
, 1);
636 term_seen_key_event(s
->term
);
638 term_update(s
->term
);
641 static int mac_keytrans(Session
*s
, EventRecord
*event
,
642 unsigned char *output
) {
643 unsigned char *p
= output
;
646 /* No meta key yet -- that'll be rather fun. */
648 /* Keys that we handle locally */
649 if (event
->modifiers
& shiftKey
) {
650 switch (event
->message
& keyCodeMask
) {
651 case K_PRIOR
: /* shift-pageup */
652 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
654 case K_NEXT
: /* shift-pagedown */
655 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
661 * Control-2 should return ^@ (0x00), Control-6 should return
662 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
663 * the DOS keyboard handling did it, and we have nothing better
664 * to do with the key combo in question, we'll also map
665 * Control-Backquote to ^\ (0x1C).
668 if (event
->modifiers
& controlKey
) {
669 switch (event
->message
& charCodeMask
) {
686 * First, all the keys that do tilde codes. (ESC '[' nn '~',
687 * for integer decimal nn.)
689 * We also deal with the weird ones here. Linux VCs replace F1
690 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
691 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
695 switch (event
->message
& keyCodeMask
) {
696 case K_F1
: code
= (event
->modifiers
& shiftKey ?
23 : 11); break;
697 case K_F2
: code
= (event
->modifiers
& shiftKey ?
24 : 12); break;
698 case K_F3
: code
= (event
->modifiers
& shiftKey ?
25 : 13); break;
699 case K_F4
: code
= (event
->modifiers
& shiftKey ?
26 : 14); break;
700 case K_F5
: code
= (event
->modifiers
& shiftKey ?
28 : 15); break;
701 case K_F6
: code
= (event
->modifiers
& shiftKey ?
29 : 17); break;
702 case K_F7
: code
= (event
->modifiers
& shiftKey ?
31 : 18); break;
703 case K_F8
: code
= (event
->modifiers
& shiftKey ?
32 : 19); break;
704 case K_F9
: code
= (event
->modifiers
& shiftKey ?
33 : 20); break;
705 case K_F10
: code
= (event
->modifiers
& shiftKey ?
34 : 21); break;
706 case K_F11
: code
= 23; break;
707 case K_F12
: code
= 24; break;
708 case K_HOME
: code
= 1; break;
709 case K_INSERT
: code
= 2; break;
710 case K_DELETE
: code
= 3; break;
711 case K_END
: code
= 4; break;
712 case K_PRIOR
: code
= 5; break;
713 case K_NEXT
: code
= 6; break;
715 if (s
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
716 p
+= sprintf((char *)p
, "\x1B[[%c", code
+ 'A' - 11);
719 if (s
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
720 p
+= sprintf((char *)p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
724 p
+= sprintf((char *)p
, "\x1B[%d~", code
);
728 if (s
->term
->app_keypad_keys
) {
729 switch (event
->message
& keyCodeMask
) {
730 case KP_ENTER
: p
+= sprintf((char *)p
, "\x1BOM"); return p
- output
;
731 case KP_CLEAR
: p
+= sprintf((char *)p
, "\x1BOP"); return p
- output
;
732 case KP_EQUAL
: p
+= sprintf((char *)p
, "\x1BOQ"); return p
- output
;
733 case KP_SLASH
: p
+= sprintf((char *)p
, "\x1BOR"); return p
- output
;
734 case KP_STAR
: p
+= sprintf((char *)p
, "\x1BOS"); return p
- output
;
735 case KP_PLUS
: p
+= sprintf((char *)p
, "\x1BOl"); return p
- output
;
736 case KP_MINUS
: p
+= sprintf((char *)p
, "\x1BOm"); return p
- output
;
737 case KP_DOT
: p
+= sprintf((char *)p
, "\x1BOn"); return p
- output
;
738 case KP_0
: p
+= sprintf((char *)p
, "\x1BOp"); return p
- output
;
739 case KP_1
: p
+= sprintf((char *)p
, "\x1BOq"); return p
- output
;
740 case KP_2
: p
+= sprintf((char *)p
, "\x1BOr"); return p
- output
;
741 case KP_3
: p
+= sprintf((char *)p
, "\x1BOs"); return p
- output
;
742 case KP_4
: p
+= sprintf((char *)p
, "\x1BOt"); return p
- output
;
743 case KP_5
: p
+= sprintf((char *)p
, "\x1BOu"); return p
- output
;
744 case KP_6
: p
+= sprintf((char *)p
, "\x1BOv"); return p
- output
;
745 case KP_7
: p
+= sprintf((char *)p
, "\x1BOw"); return p
- output
;
746 case KP_8
: p
+= sprintf((char *)p
, "\x1BOx"); return p
- output
;
747 case KP_9
: p
+= sprintf((char *)p
, "\x1BOy"); return p
- output
;
751 switch (event
->message
& keyCodeMask
) {
753 p
+= sprintf((char *)p
,
754 s
->term
->app_cursor_keys ?
"\x1BOA" : "\x1B[A");
757 p
+= sprintf((char *)p
,
758 s
->term
->app_cursor_keys ?
"\x1BOB" : "\x1B[B");
761 p
+= sprintf((char *)p
,
762 s
->term
->app_cursor_keys ?
"\x1BOC" : "\x1B[C");
765 p
+= sprintf((char *)p
,
766 s
->term
->app_cursor_keys ?
"\x1BOD" : "\x1B[D");
772 *p
++ = (s
->cfg
.bksp_is_delete ?
0x7f : 0x08);
775 *p
++ = event
->message
& charCodeMask
;
780 void request_paste(void *frontend
)
782 Session
*s
= frontend
;
785 * In the Mac OS, pasting is synchronous: we can read the
786 * clipboard with no difficulty, so request_paste() can just go
789 term_do_paste(s
->term
);
800 void mac_growterm(WindowPtr window
, EventRecord
*event
) {
803 int newrows
, newcols
;
805 DragGrayRgnUPP draghooksave
;
809 s
= (Session
*)GetWRefCon(window
);
811 draghooksave
= LMGetDragHook();
812 growterm_state
.oldmsg
[0] = '\0';
813 growterm_state
.zeromouse
= event
->where
;
814 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
815 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
816 growterm_state
.s
= s
;
819 BackColor(whiteColor
);
820 ForeColor(blackColor
);
821 TextFont(systemFont
);
825 SetRect(&growterm_state
.msgrect
, 0, 0,
826 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
827 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
828 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
830 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
831 grow_result
= GrowWindow(window
, event
->where
, &limits
);
833 DisposeDragGrayRgnUPP(LMGetDragHook());
834 LMSetDragHook(draghooksave
);
835 InvalRect(&growterm_state
.msgrect
);
839 if (grow_result
!= 0) {
840 newrows
= HiWord(grow_result
) / s
->font_height
;
841 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
842 mac_adjustsize(s
, newrows
, newcols
);
843 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
847 static pascal void mac_growtermdraghook(void)
849 Session
*s
= growterm_state
.s
;
853 unsigned char pbuf
[20];
854 int newrows
, newcols
;
857 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
858 if (newrows
< 1) newrows
= 1;
859 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
860 if (newcols
< 1) newcols
= 1;
861 sprintf(buf
, "%dx%d", newcols
, newrows
);
862 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
864 strcpy(growterm_state
.oldmsg
, buf
);
865 c2pstrcpy(pbuf
, buf
);
868 SetPort(growterm_state
.s
->window
);
869 EraseRect(&growterm_state
.msgrect
);
870 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
875 void mac_activateterm(WindowPtr window
, Boolean active
) {
878 s
= (Session
*)GetWRefCon(window
);
879 s
->term
->has_focus
= active
;
880 term_update(s
->term
);
882 ShowControl(s
->scrollbar
);
885 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
887 BackColor(blackColor
);
888 HideControl(s
->scrollbar
);
893 void mac_updateterm(WindowPtr window
) {
896 s
= (Session
*)GetWRefCon(window
);
900 term_paint(s
->term
, s
,
901 PTOCC((*window
->visRgn
)->rgnBBox
.left
),
902 PTOCR((*window
->visRgn
)->rgnBBox
.top
),
903 PTOCC((*window
->visRgn
)->rgnBBox
.right
),
904 PTOCR((*window
->visRgn
)->rgnBBox
.bottom
), 1);
905 /* Restore default colours in case the Window Manager uses them */
906 if (HAVE_COLOR_QD()) {
907 PmForeColor(DEFAULT_FG
);
908 PmBackColor(DEFAULT_BG
);
910 ForeColor(whiteColor
);
911 BackColor(blackColor
);
913 if (FrontWindow() != window
)
914 EraseRect(&(*s
->scrollbar
)->contrlRect
);
915 UpdateControls(window
, window
->visRgn
);
921 static void mac_drawgrowicon(Session
*s
) {
927 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
928 * See Tech Note TB575 for details.
930 clip
= s
->window
->portRect
;
931 clip
.left
= clip
.right
- 15;
935 DrawGrowIcon(s
->window
);
940 struct do_text_args
{
951 * Call from the terminal emulator to draw a bit of text
953 * x and y are text row and column (zero-based)
955 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
956 unsigned long attr
, int lattr
) {
959 struct do_text_args a
;
961 char mactextbuf
[1024];
962 UniChar unitextbuf
[1024];
965 ByteCount iread
, olen
;
972 /* First check this text is relevant */
973 a
.textrect
.top
= y
* s
->font_height
;
974 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
975 a
.textrect
.left
= x
* s
->font_width
;
976 a
.textrect
.right
= (x
+ len
) * s
->font_width
;
977 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
980 /* Unpack Unicode from the mad format we get passed */
981 for (i
= 0; i
< len
; i
++)
982 unitextbuf
[i
] = (unsigned char)text
[i
] | (attr
& CSET_MASK
);
984 if (s
->uni_to_font
!= NULL
) {
985 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
986 unitextbuf
, kUnicodeUseFallbacksMask
,
988 1024, &iread
, &olen
, mactextbuf
);
989 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
991 } else if (s
->font_charset
!= CS_NONE
) {
992 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
993 unitextptr
= (wchar_t *)unitextbuf
;
994 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
995 s
->font_charset
, NULL
, ".", 1);
1000 a
.text
= mactextbuf
;
1004 a
.numer
.h
= a
.numer
.v
= a
.denom
.h
= a
.denom
.v
= 1;
1006 TextFont(s
->fontnum
);
1007 if (s
->cfg
.fontisbold
|| (attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1009 if (attr
& ATTR_UNDER
)
1012 TextSize(s
->cfg
.fontheight
);
1014 if (HAVE_COLOR_QD())
1016 SpaceExtra(s
->font_boldadjust
<< 16);
1017 CharExtra(s
->font_boldadjust
<< 16);
1023 RectRgn(textrgn
, &a
.textrect
);
1024 if (HAVE_COLOR_QD())
1025 DeviceLoop(textrgn
, &do_text_for_device_upp
, (long)&a
, 0);
1027 do_text_for_device(1, 0, NULL
, (long)&a
);
1028 DisposeRgn(textrgn
);
1029 /* Tell the window manager about it in case this isn't an update */
1030 ValidRect(&a
.textrect
);
1033 static pascal void do_text_for_device(short depth
, short devflags
,
1034 GDHandle device
, long cookie
) {
1035 struct do_text_args
*a
;
1036 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1038 a
= (struct do_text_args
*)cookie
;
1040 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1041 reverse
= a
->attr
& ATTR_REVERSE
;
1043 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1046 if (HAVE_COLOR_QD()) {
1048 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
) * 2;
1049 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
) * 2;
1052 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1053 * boldens the background, because that's all we can do.
1055 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1056 bgcolour
= DEFAULT_BG
;
1060 fgcolour
= bgcolour
;
1063 if (bright
&& depth
> 2)
1065 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1066 fgcolour
= CURSOR_FG
;
1067 bgcolour
= CURSOR_BG
;
1069 PmForeColor(fgcolour
);
1070 PmBackColor(bgcolour
);
1071 } else { /* No Color Quickdraw */
1072 /* XXX This should be done with a _little_ more configurability */
1074 ForeColor(blackColor
);
1075 BackColor(whiteColor
);
1077 ForeColor(whiteColor
);
1078 BackColor(blackColor
);
1082 EraseRect(&a
->textrect
);
1083 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1084 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1085 if (a
->s
->window
->grafProcs
!= NULL
)
1086 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
,
1087 a
->s
->window
->grafProcs
->textProc
);
1089 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1091 if (a
->attr
& TATTR_PASCURS
) {
1098 PmForeColor(CURSOR_BG
);
1101 FrameRect(&a
->textrect
);
1105 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1106 unsigned long attr
, int lattr
)
1109 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1113 * Call from the terminal emulator to get its graphics context.
1114 * Should probably be called start_redraw or something.
1116 void pre_paint(Session
*s
) {
1118 Rect myrect
, tmprect
;
1120 if (HAVE_COLOR_QD()) {
1121 s
->term
->attr_mask
= 0;
1123 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1124 LocalToGlobal((Point
*)&myrect
.top
);
1125 LocalToGlobal((Point
*)&myrect
.bottom
);
1126 for (gdh
= GetDeviceList();
1128 gdh
= GetNextDevice(gdh
)) {
1129 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1130 TestDeviceAttribute(gdh
, screenActive
) &&
1131 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1132 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1134 if (s
->cfg
.bold_colour
)
1135 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1136 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1139 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1142 s
->term
->attr_mask
= ~0;
1143 return; /* No point checking more screens. */
1148 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1149 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1152 Context
get_ctx(void *frontend
) {
1153 Session
*s
= frontend
;
1159 void free_ctx(Context ctx
) {
1164 * Presumably this does something in Windows
1166 void post_paint(Session
*s
) {
1171 * Set the scroll bar position
1173 * total is the line number of the bottom of the working screen
1174 * start is the line number of the top of the display
1175 * page is the length of the displayed page
1177 void set_sbar(void *frontend
, int total
, int start
, int page
) {
1178 Session
*s
= frontend
;
1180 /* We don't redraw until we've set everything up, to avoid glitches */
1181 (*s
->scrollbar
)->contrlMin
= 0;
1182 (*s
->scrollbar
)->contrlMax
= total
- page
;
1183 SetControlValue(s
->scrollbar
, start
);
1185 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1186 SetControlViewSize(s
->scrollbar
, page
);
1190 void sys_cursor(void *frontend
, int x
, int y
)
1193 * I think his is meaningless under Mac OS.
1198 * This is still called when mode==BELL_VISUAL, even though the
1199 * visual bell is handled entirely within terminal.c, because we
1200 * may want to perform additional actions on any kind of bell (for
1201 * example, taskbar flashing in Windows).
1203 void beep(void *frontend
, int mode
)
1205 if (mode
!= BELL_VISUAL
)
1208 * XXX We should indicate the relevant window and/or use the
1209 * Notification Manager
1213 int char_width(Context ctx
, int uc
)
1216 * Until we support exciting character-set stuff, assume all chars are
1223 * Set icon string -- a no-op here (Windowshade?)
1225 void set_icon(void *frontend
, char *icon
) {
1226 Session
*s
= frontend
;
1231 * Set the window title
1233 void set_title(void *frontend
, char *title
) {
1234 Session
*s
= frontend
;
1237 c2pstrcpy(mactitle
, title
);
1238 SetWTitle(s
->window
, mactitle
);
1242 * set or clear the "raw mouse message" mode
1244 void set_raw_mouse_mode(void *frontend
, int activate
)
1246 Session
*s
= frontend
;
1248 s
->raw_mouse
= activate
;
1249 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1253 * Resize the window at the emulator's request
1255 void request_resize(void *frontend
, int w
, int h
) {
1256 Session
*s
= frontend
;
1264 * Iconify (actually collapse) the window at the emulator's request.
1266 void set_iconic(void *frontend
, int iconic
)
1268 Session
*s
= frontend
;
1271 if (mac_gestalts
.apprvers
>= 0x0100 &&
1272 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1273 (features
& kWindowCanCollapse
))
1274 CollapseWindow(s
->window
, iconic
);
1278 * Move the window in response to a server-side request.
1280 void move_window(void *frontend
, int x
, int y
)
1282 Session
*s
= frontend
;
1284 MoveWindow(s
->window
, x
, y
, FALSE
);
1288 * Move the window to the top or bottom of the z-order in response
1289 * to a server-side request.
1291 void set_zorder(void *frontend
, int top
)
1293 Session
*s
= frontend
;
1296 * We also change the input focus to point to the topmost window,
1297 * since that's probably what the Human Interface Guidelines would
1301 SelectWindow(s
->window
);
1303 SendBehind(s
->window
, NULL
);
1307 * Refresh the window in response to a server-side request.
1309 void refresh_window(void *frontend
)
1311 Session
*s
= frontend
;
1313 term_invalidate(s
->term
);
1317 * Maximise or restore the window in response to a server-side
1320 void set_zoomed(void *frontend
, int zoomed
)
1322 Session
*s
= frontend
;
1324 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1328 * Report whether the window is iconic, for terminal reports.
1330 int is_iconic(void *frontend
)
1332 Session
*s
= frontend
;
1335 if (mac_gestalts
.apprvers
>= 0x0100 &&
1336 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1337 (features
& kWindowCanCollapse
))
1338 return IsWindowCollapsed(s
->window
);
1343 * Report the window's position, for terminal reports.
1345 void get_window_pos(void *frontend
, int *x
, int *y
)
1347 Session
*s
= frontend
;
1349 *x
= s
->window
->portRect
.left
;
1350 *y
= s
->window
->portRect
.top
;
1354 * Report the window's pixel size, for terminal reports.
1356 void get_window_pixels(void *frontend
, int *x
, int *y
)
1358 Session
*s
= frontend
;
1360 *x
= s
->window
->portRect
.right
- s
->window
->portRect
.left
;
1361 *y
= s
->window
->portRect
.bottom
- s
->window
->portRect
.top
;
1365 * Return the window or icon title.
1367 char *get_window_title(void *frontend
, int icon
)
1369 Session
*s
= frontend
;
1371 static char title
[256];
1373 GetWTitle(s
->window
, ptitle
);
1374 p2cstrcpy(title
, ptitle
);
1379 * real_palette_set(): This does the actual palette-changing work on behalf
1380 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1381 * is doing a batch of updates.
1383 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1387 if (!HAVE_COLOR_QD())
1389 col
.red
= r
* 0x0101;
1390 col
.green
= g
* 0x0101;
1391 col
.blue
= b
* 0x0101;
1392 SetEntryColor(s
->palette
, n
, &col
);
1396 * Set the logical palette. Called by the terminal emulator.
1398 void palette_set(void *frontend
, int n
, int r
, int g
, int b
) {
1399 Session
*s
= frontend
;
1400 static const int first
[21] = {
1401 0, 2, 4, 6, 8, 10, 12, 14,
1402 1, 3, 5, 7, 9, 11, 13, 15,
1406 if (!HAVE_COLOR_QD())
1408 real_palette_set(s
, first
[n
], r
, g
, b
);
1410 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1411 if (first
[n
] == DEFAULT_BG
)
1413 ActivatePalette(s
->window
);
1417 * Reset to the default palette
1419 void palette_reset(void *frontend
) {
1420 Session
*s
= frontend
;
1421 /* This maps colour indices in cfg to those used in our palette. */
1422 static const int ww
[] = {
1423 6, 7, 8, 9, 10, 11, 12, 13,
1424 14, 15, 16, 17, 18, 19, 20, 21,
1429 if (!HAVE_COLOR_QD())
1432 assert(lenof(ww
) == NCOLOURS
);
1434 for (i
= 0; i
< NCOLOURS
; i
++) {
1435 real_palette_set(s
, i
,
1436 s
->cfg
.colours
[ww
[i
]][0],
1437 s
->cfg
.colours
[ww
[i
]][1],
1438 s
->cfg
.colours
[ww
[i
]][2]);
1441 ActivatePalette(s
->window
);
1442 /* Palette Manager will generate update events as required. */
1446 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1449 void do_scroll(void *frontend
, int topline
, int botline
, int lines
) {
1450 Session
*s
= frontend
;
1452 RgnHandle scrollrgn
= NewRgn();
1453 RgnHandle movedupdate
= NewRgn();
1454 RgnHandle update
= NewRgn();
1455 Point g2l
= { 0, 0 };
1460 * Work out the part of the update region that will scrolled by
1464 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1465 s
->term
->cols
* s
->font_width
,
1466 (botline
+ 1) * s
->font_height
);
1468 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1469 s
->term
->cols
* s
->font_width
,
1470 (botline
- lines
+ 1) * s
->font_height
);
1471 CopyRgn(((WindowPeek
)s
->window
)->updateRgn
, movedupdate
);
1472 GlobalToLocal(&g2l
);
1473 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1474 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1475 ValidRgn(movedupdate
);
1476 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1479 if (HAVE_COLOR_QD())
1480 PmBackColor(DEFAULT_BG
);
1482 BackColor(blackColor
); /* XXX make configurable */
1483 SetRect(&r
, 0, topline
* s
->font_height
,
1484 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1485 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1488 InvalRgn(movedupdate
);
1490 DisposeRgn(scrollrgn
);
1491 DisposeRgn(movedupdate
);
1495 void logevent(void *frontend
, char *str
) {
1497 fprintf(stderr
, "%s\n", str
);
1500 /* Dummy routine, only required in plink. */
1501 void ldisc_update(void *frontend
, int echo
, int edit
)
1506 * Mac PuTTY doesn't support printing yet.
1508 printer_job
*printer_start_job(char *printer
)
1514 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1518 void printer_finish_job(printer_job
*pj
)
1522 void frontend_keypress(void *handle
)
1525 * Keypress termination in non-Close-On-Exit mode is not
1526 * currently supported in PuTTY proper, because the window
1527 * always has a perfectly good Close button anyway. So we do
1534 * Ask whether to wipe a session log file before writing to it.
1535 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1537 int askappend(void *frontend
, char *filename
)
1540 /* FIXME: not implemented yet. */
1547 * c-file-style: "simon"