1 /* $Id: macterm.c,v 1.41 2003/01/12 13:50:04 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
);
177 fatalbox("%s", errmsg
);
178 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
180 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
182 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
183 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
185 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
186 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
188 ShowWindow(s
->window
);
190 s
->prev
= s
->next
->prev
;
191 if (ret
->next
!= NULL
)
192 s
->next
->prev
= &s
->next
;
196 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
198 static void mac_initfont(Session
*s
) {
205 c2pstrcpy(macfont
, s
->cfg
.font
);
206 GetFNum(macfont
, &s
->fontnum
);
207 TextFont(s
->fontnum
);
208 TextFace(s
->cfg
.fontisbold ? bold
: 0);
209 TextSize(s
->cfg
.fontheight
);
211 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
212 s
->font_ascent
= fi
.ascent
;
213 s
->font_leading
= fi
.leading
;
214 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
215 if (!s
->cfg
.bold_colour
) {
217 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
219 s
->font_boldadjust
= 0;
221 if (s
->uni_to_font
!= NULL
)
222 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
223 if (mac_gestalts
.encvvers
!= 0 &&
224 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
225 kTextLanguageDontCare
,
226 kTextRegionDontCare
, macfont
,
228 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) == noErr
) {
229 if (uni_to_font_fallback_upp
== NULL
)
230 uni_to_font_fallback_upp
=
231 NewUnicodeToTextFallbackProc(&uni_to_font_fallback
);
232 fbflags
= kUnicodeFallbackCustomOnly
;
233 if (mac_gestalts
.uncvattr
& kTECAddFallbackInterruptMask
)
234 fbflags
|= kUnicodeFallbackInterruptSafeMask
;
235 if (SetFallbackUnicodeToText(s
->uni_to_font
,
236 uni_to_font_fallback_upp
, fbflags
, NULL
) != noErr
) {
237 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
242 s
->uni_to_font
= NULL
;
244 charset_from_macenc(FontToScript(s
->fontnum
),
245 GetScriptManagerVariable(smRegionCode
),
246 mac_gestalts
.sysvers
, s
->cfg
.font
);
249 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
252 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
253 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
254 ByteCount
*ousedp
, LogicalAddress
*cookie
, ConstUnicodeMappingPtr mapping
)
258 return kTECOutputBufferFullStatus
;
260 * What I'd _like_ to do here is to somehow generate the
261 * missing-character glyph that every font is required to have.
262 * Unfortunately (and somewhat surprisingly), I can't find any way
263 * to actually ask for it explicitly. Bah.
272 * Called every time round the event loop.
274 void mac_pollterm(void)
278 for (s
= sesslist
; s
!= NULL
; s
= s
->next
) {
280 term_update(s
->term
);
285 * To be called whenever the window size changes.
286 * rows and cols should be desired values.
287 * It's assumed the terminal emulator will be informed, and will set rows
290 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
291 int winwidth
, winheight
;
293 winwidth
= newcols
* s
->font_width
+ 15;
294 winheight
= newrows
* s
->font_height
;
295 SizeWindow(s
->window
, winwidth
, winheight
, true);
296 HideControl(s
->scrollbar
);
297 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
298 SizeControl(s
->scrollbar
, 16, winheight
- 13);
299 ShowControl(s
->scrollbar
);
303 static void mac_initpalette(Session
*s
) {
305 if (!HAVE_COLOR_QD())
308 * Most colours should be inhibited on 2bpp displays.
309 * Palette manager documentation suggests inhibiting all tolerant colours
310 * on greyscale displays.
312 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
313 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
314 #define PM_TOLERANCE 0x2000
315 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
316 if (s
->palette
== NULL
)
317 fatalbox("Unable to create palette");
318 /* In 2bpp, these are the colours we want most. */
319 SetEntryUsage(s
->palette
, DEFAULT_BG
,
320 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
321 SetEntryUsage(s
->palette
, DEFAULT_FG
,
322 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
323 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
324 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
325 SetEntryUsage(s
->palette
, CURSOR_BG
,
326 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
331 * Set the background colour of the window correctly. Should be
332 * called whenever the default background changes.
334 static void mac_adjustwinbg(Session
*s
) {
336 if (!HAVE_COLOR_QD())
339 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
340 SetWindowContentColor(s
->window
,
341 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
345 if (s
->wctab
== NULL
)
346 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
347 if (s
->wctab
== NULL
)
348 return; /* do without */
349 (*s
->wctab
)->wCSeed
= 0;
350 (*s
->wctab
)->wCReserved
= 0;
351 (*s
->wctab
)->ctSize
= 0;
352 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
353 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
354 SetWinColor(s
->window
, s
->wctab
);
359 * Set the cursor shape correctly
361 void mac_adjusttermcursor(WindowPtr window
, Point mouse
, RgnHandle cursrgn
) {
363 ControlHandle control
;
368 s
= (Session
*)GetWRefCon(window
);
369 GlobalToLocal(&mouse
);
370 part
= FindControl(mouse
, window
, &control
);
371 if (control
== s
->scrollbar
) {
372 SetCursor(&qd
.arrow
);
373 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
374 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
376 x
= mouse
.h
/ s
->font_width
;
377 y
= mouse
.v
/ s
->font_height
;
379 SetCursor(&qd
.arrow
);
381 SetCursor(*GetCursor(iBeamCursor
));
382 /* Ask for shape changes if we leave this character cell. */
383 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
384 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
385 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
390 * Enable/disable menu items based on the active terminal window.
392 void mac_adjusttermmenus(WindowPtr window
) {
397 s
= (Session
*)GetWRefCon(window
);
398 menu
= GetMenuHandle(mEdit
);
400 DisableItem(menu
, iUndo
);
401 DisableItem(menu
, iCut
);
402 if (1/*s->term->selstate == SELECTED*/)
403 EnableItem(menu
, iCopy
);
405 DisableItem(menu
, iCopy
);
406 if (GetScrap(NULL
, 'TEXT', &offset
) == noTypeErr
)
407 DisableItem(menu
, iPaste
);
409 EnableItem(menu
, iPaste
);
410 DisableItem(menu
, iClear
);
411 EnableItem(menu
, iSelectAll
);
414 void mac_menuterm(WindowPtr window
, short menu
, short item
) {
417 s
= (Session
*)GetWRefCon(window
);
425 term_do_paste(s
->term
);
431 void mac_clickterm(WindowPtr window
, EventRecord
*event
) {
434 ControlHandle control
;
437 s
= (Session
*)GetWRefCon(window
);
439 mouse
= event
->where
;
440 GlobalToLocal(&mouse
);
441 part
= FindControl(mouse
, window
, &control
);
442 if (control
== s
->scrollbar
) {
444 case kControlIndicatorPart
:
445 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
446 term_scroll(s
->term
, +1, GetControlValue(control
));
448 case kControlUpButtonPart
:
449 case kControlDownButtonPart
:
450 case kControlPageUpPart
:
451 case kControlPageDownPart
:
452 TrackControl(control
, mouse
, &mac_scrolltracker_upp
);
456 text_click(s
, event
);
460 static void text_click(Session
*s
, EventRecord
*event
) {
463 static UInt32 lastwhen
= 0;
464 static Session
*lastsess
= NULL
;
465 static int lastrow
= -1, lastcol
= -1;
466 static Mouse_Action lastact
= MA_NOTHING
;
469 localwhere
= event
->where
;
470 GlobalToLocal(&localwhere
);
472 col
= PTOCC(localwhere
.h
);
473 row
= PTOCR(localwhere
.v
);
474 if (event
->when
- lastwhen
< GetDblTime() &&
475 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
476 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
477 lastact
== MA_2CLK ? MA_3CLK
:
478 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
481 /* Fake right button with shift key */
482 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
483 lastact
, col
, row
, event
->modifiers
& shiftKey
,
484 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
488 while (StillDown()) {
489 GetMouse(&localwhere
);
490 col
= PTOCC(localwhere
.h
);
491 row
= PTOCR(localwhere
.v
);
493 event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
494 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
495 event
->modifiers
& controlKey
,
496 event
->modifiers
& optionKey
);
497 if (row
> s
->term
->rows
- 1)
498 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
500 term_scroll(s
->term
, 0, row
);
502 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
503 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
504 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
505 lastwhen
= TickCount();
508 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
521 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
) {
524 * See "Programming with the Text Encoding Conversion Manager"
525 * Appendix E for Unicode scrap conventions.
527 * XXX Need to support TEXT/styl scrap as well.
528 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
529 * XXX Maybe PICT scrap too.
531 if (ZeroScrap() != noErr
)
533 PutScrap(len
* sizeof(*data
), 'utxt', data
);
536 void get_clip(void *frontend
, wchar_t **p
, int *lenp
) {
537 Session
*s
= frontend
;
538 static Handle h
= NULL
;
547 /* XXX Support TEXT-format scrap as well. */
548 if (GetScrap(NULL
, 'utxt', &offset
) > 0) {
550 *lenp
= GetScrap(h
, 'utxt', &offset
) / sizeof(**p
);
553 if (*p
== NULL
|| *lenp
<= 0)
554 fatalbox("Empty scrap");
561 static pascal void mac_scrolltracker(ControlHandle control
, short part
) {
564 s
= (Session
*)GetWRefCon((*control
)->contrlOwner
);
566 case kControlUpButtonPart
:
567 term_scroll(s
->term
, 0, -1);
569 case kControlDownButtonPart
:
570 term_scroll(s
->term
, 0, +1);
572 case kControlPageUpPart
:
573 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
575 case kControlPageDownPart
:
576 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
597 #define K_INSERT 0x7200
598 #define K_HOME 0x7300
599 #define K_PRIOR 0x7400
600 #define K_DELETE 0x7500
602 #define K_NEXT 0x7900
603 #define K_LEFT 0x7b00
604 #define K_RIGHT 0x7c00
605 #define K_DOWN 0x7d00
617 #define KP_CLEAR 0x4700
618 #define KP_EQUAL 0x5100
619 #define KP_SLASH 0x4b00
620 #define KP_STAR 0x4300
621 #define KP_PLUS 0x4500
622 #define KP_MINUS 0x4e00
623 #define KP_DOT 0x4100
624 #define KP_ENTER 0x4c00
626 void mac_keyterm(WindowPtr window
, EventRecord
*event
) {
627 unsigned char buf
[20];
631 s
= (Session
*)GetWRefCon(window
);
632 len
= mac_keytrans(s
, event
, buf
);
633 ldisc_send(s
->ldisc
, (char *)buf
, len
, 1);
635 term_seen_key_event(s
->term
);
637 term_update(s
->term
);
640 static int mac_keytrans(Session
*s
, EventRecord
*event
,
641 unsigned char *output
) {
642 unsigned char *p
= output
;
645 /* No meta key yet -- that'll be rather fun. */
647 /* Keys that we handle locally */
648 if (event
->modifiers
& shiftKey
) {
649 switch (event
->message
& keyCodeMask
) {
650 case K_PRIOR
: /* shift-pageup */
651 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
653 case K_NEXT
: /* shift-pagedown */
654 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
660 * Control-2 should return ^@ (0x00), Control-6 should return
661 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
662 * the DOS keyboard handling did it, and we have nothing better
663 * to do with the key combo in question, we'll also map
664 * Control-Backquote to ^\ (0x1C).
667 if (event
->modifiers
& controlKey
) {
668 switch (event
->message
& charCodeMask
) {
685 * First, all the keys that do tilde codes. (ESC '[' nn '~',
686 * for integer decimal nn.)
688 * We also deal with the weird ones here. Linux VCs replace F1
689 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
690 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
694 switch (event
->message
& keyCodeMask
) {
695 case K_F1
: code
= (event
->modifiers
& shiftKey ?
23 : 11); break;
696 case K_F2
: code
= (event
->modifiers
& shiftKey ?
24 : 12); break;
697 case K_F3
: code
= (event
->modifiers
& shiftKey ?
25 : 13); break;
698 case K_F4
: code
= (event
->modifiers
& shiftKey ?
26 : 14); break;
699 case K_F5
: code
= (event
->modifiers
& shiftKey ?
28 : 15); break;
700 case K_F6
: code
= (event
->modifiers
& shiftKey ?
29 : 17); break;
701 case K_F7
: code
= (event
->modifiers
& shiftKey ?
31 : 18); break;
702 case K_F8
: code
= (event
->modifiers
& shiftKey ?
32 : 19); break;
703 case K_F9
: code
= (event
->modifiers
& shiftKey ?
33 : 20); break;
704 case K_F10
: code
= (event
->modifiers
& shiftKey ?
34 : 21); break;
705 case K_F11
: code
= 23; break;
706 case K_F12
: code
= 24; break;
707 case K_HOME
: code
= 1; break;
708 case K_INSERT
: code
= 2; break;
709 case K_DELETE
: code
= 3; break;
710 case K_END
: code
= 4; break;
711 case K_PRIOR
: code
= 5; break;
712 case K_NEXT
: code
= 6; break;
714 if (s
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
715 p
+= sprintf((char *)p
, "\x1B[[%c", code
+ 'A' - 11);
718 if (s
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
719 p
+= sprintf((char *)p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
723 p
+= sprintf((char *)p
, "\x1B[%d~", code
);
727 if (s
->term
->app_keypad_keys
) {
728 switch (event
->message
& keyCodeMask
) {
729 case KP_ENTER
: p
+= sprintf((char *)p
, "\x1BOM"); return p
- output
;
730 case KP_CLEAR
: p
+= sprintf((char *)p
, "\x1BOP"); return p
- output
;
731 case KP_EQUAL
: p
+= sprintf((char *)p
, "\x1BOQ"); return p
- output
;
732 case KP_SLASH
: p
+= sprintf((char *)p
, "\x1BOR"); return p
- output
;
733 case KP_STAR
: p
+= sprintf((char *)p
, "\x1BOS"); return p
- output
;
734 case KP_PLUS
: p
+= sprintf((char *)p
, "\x1BOl"); return p
- output
;
735 case KP_MINUS
: p
+= sprintf((char *)p
, "\x1BOm"); return p
- output
;
736 case KP_DOT
: p
+= sprintf((char *)p
, "\x1BOn"); return p
- output
;
737 case KP_0
: p
+= sprintf((char *)p
, "\x1BOp"); return p
- output
;
738 case KP_1
: p
+= sprintf((char *)p
, "\x1BOq"); return p
- output
;
739 case KP_2
: p
+= sprintf((char *)p
, "\x1BOr"); return p
- output
;
740 case KP_3
: p
+= sprintf((char *)p
, "\x1BOs"); return p
- output
;
741 case KP_4
: p
+= sprintf((char *)p
, "\x1BOt"); return p
- output
;
742 case KP_5
: p
+= sprintf((char *)p
, "\x1BOu"); return p
- output
;
743 case KP_6
: p
+= sprintf((char *)p
, "\x1BOv"); return p
- output
;
744 case KP_7
: p
+= sprintf((char *)p
, "\x1BOw"); return p
- output
;
745 case KP_8
: p
+= sprintf((char *)p
, "\x1BOx"); return p
- output
;
746 case KP_9
: p
+= sprintf((char *)p
, "\x1BOy"); return p
- output
;
750 switch (event
->message
& keyCodeMask
) {
752 p
+= sprintf((char *)p
,
753 s
->term
->app_cursor_keys ?
"\x1BOA" : "\x1B[A");
756 p
+= sprintf((char *)p
,
757 s
->term
->app_cursor_keys ?
"\x1BOB" : "\x1B[B");
760 p
+= sprintf((char *)p
,
761 s
->term
->app_cursor_keys ?
"\x1BOC" : "\x1B[C");
764 p
+= sprintf((char *)p
,
765 s
->term
->app_cursor_keys ?
"\x1BOD" : "\x1B[D");
771 *p
++ = (s
->cfg
.bksp_is_delete ?
0x7f : 0x08);
774 *p
++ = event
->message
& charCodeMask
;
779 void request_paste(void *frontend
)
781 Session
*s
= frontend
;
784 * In the Mac OS, pasting is synchronous: we can read the
785 * clipboard with no difficulty, so request_paste() can just go
788 term_do_paste(s
->term
);
799 void mac_growterm(WindowPtr window
, EventRecord
*event
) {
802 int newrows
, newcols
;
804 DragGrayRgnUPP draghooksave
;
808 s
= (Session
*)GetWRefCon(window
);
810 draghooksave
= LMGetDragHook();
811 growterm_state
.oldmsg
[0] = '\0';
812 growterm_state
.zeromouse
= event
->where
;
813 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
814 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
815 growterm_state
.s
= s
;
818 BackColor(whiteColor
);
819 ForeColor(blackColor
);
820 TextFont(systemFont
);
824 SetRect(&growterm_state
.msgrect
, 0, 0,
825 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
826 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
827 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
829 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
830 grow_result
= GrowWindow(window
, event
->where
, &limits
);
832 DisposeDragGrayRgnUPP(LMGetDragHook());
833 LMSetDragHook(draghooksave
);
834 InvalRect(&growterm_state
.msgrect
);
838 if (grow_result
!= 0) {
839 newrows
= HiWord(grow_result
) / s
->font_height
;
840 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
841 mac_adjustsize(s
, newrows
, newcols
);
842 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
846 static pascal void mac_growtermdraghook(void)
848 Session
*s
= growterm_state
.s
;
852 unsigned char pbuf
[20];
853 int newrows
, newcols
;
856 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
857 if (newrows
< 1) newrows
= 1;
858 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
859 if (newcols
< 1) newcols
= 1;
860 sprintf(buf
, "%dx%d", newcols
, newrows
);
861 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
863 strcpy(growterm_state
.oldmsg
, buf
);
864 c2pstrcpy(pbuf
, buf
);
867 SetPort(growterm_state
.s
->window
);
868 EraseRect(&growterm_state
.msgrect
);
869 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
874 void mac_activateterm(WindowPtr window
, Boolean active
) {
877 s
= (Session
*)GetWRefCon(window
);
878 s
->term
->has_focus
= active
;
879 term_update(s
->term
);
881 ShowControl(s
->scrollbar
);
884 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
886 BackColor(blackColor
);
887 HideControl(s
->scrollbar
);
892 void mac_updateterm(WindowPtr window
) {
895 s
= (Session
*)GetWRefCon(window
);
899 term_paint(s
->term
, s
,
900 PTOCC((*window
->visRgn
)->rgnBBox
.left
),
901 PTOCR((*window
->visRgn
)->rgnBBox
.top
),
902 PTOCC((*window
->visRgn
)->rgnBBox
.right
),
903 PTOCR((*window
->visRgn
)->rgnBBox
.bottom
), 1);
904 /* Restore default colours in case the Window Manager uses them */
905 if (HAVE_COLOR_QD()) {
906 PmForeColor(DEFAULT_FG
);
907 PmBackColor(DEFAULT_BG
);
909 ForeColor(whiteColor
);
910 BackColor(blackColor
);
912 if (FrontWindow() != window
)
913 EraseRect(&(*s
->scrollbar
)->contrlRect
);
914 UpdateControls(window
, window
->visRgn
);
920 static void mac_drawgrowicon(Session
*s
) {
926 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
927 * See Tech Note TB575 for details.
929 clip
= s
->window
->portRect
;
930 clip
.left
= clip
.right
- 15;
934 DrawGrowIcon(s
->window
);
939 struct do_text_args
{
950 * Call from the terminal emulator to draw a bit of text
952 * x and y are text row and column (zero-based)
954 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
955 unsigned long attr
, int lattr
) {
958 struct do_text_args a
;
960 char mactextbuf
[1024];
961 UniChar unitextbuf
[1024];
964 ByteCount iread
, olen
;
971 /* First check this text is relevant */
972 a
.textrect
.top
= y
* s
->font_height
;
973 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
974 a
.textrect
.left
= x
* s
->font_width
;
975 a
.textrect
.right
= (x
+ len
) * s
->font_width
;
976 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
979 /* Unpack Unicode from the mad format we get passed */
980 for (i
= 0; i
< len
; i
++)
981 unitextbuf
[i
] = (unsigned char)text
[i
] | (attr
& CSET_MASK
);
983 if (s
->uni_to_font
!= NULL
) {
984 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
985 unitextbuf
, kUnicodeUseFallbacksMask
,
987 1024, &iread
, &olen
, mactextbuf
);
988 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
990 } else if (s
->font_charset
!= CS_NONE
) {
991 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
992 unitextptr
= (wchar_t *)unitextbuf
;
993 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
994 s
->font_charset
, NULL
, ".", 1);
1003 a
.numer
.h
= a
.numer
.v
= a
.denom
.h
= a
.denom
.v
= 1;
1005 TextFont(s
->fontnum
);
1006 if (s
->cfg
.fontisbold
|| (attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1008 if (attr
& ATTR_UNDER
)
1011 TextSize(s
->cfg
.fontheight
);
1013 if (HAVE_COLOR_QD())
1015 SpaceExtra(s
->font_boldadjust
<< 16);
1016 CharExtra(s
->font_boldadjust
<< 16);
1022 RectRgn(textrgn
, &a
.textrect
);
1023 if (HAVE_COLOR_QD())
1024 DeviceLoop(textrgn
, &do_text_for_device_upp
, (long)&a
, 0);
1026 do_text_for_device(1, 0, NULL
, (long)&a
);
1027 DisposeRgn(textrgn
);
1028 /* Tell the window manager about it in case this isn't an update */
1029 ValidRect(&a
.textrect
);
1032 static pascal void do_text_for_device(short depth
, short devflags
,
1033 GDHandle device
, long cookie
) {
1034 struct do_text_args
*a
;
1035 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1037 a
= (struct do_text_args
*)cookie
;
1039 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1040 reverse
= a
->attr
& ATTR_REVERSE
;
1042 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1045 if (HAVE_COLOR_QD()) {
1047 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
) * 2;
1048 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
) * 2;
1051 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1052 * boldens the background, because that's all we can do.
1054 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1055 bgcolour
= DEFAULT_BG
;
1059 fgcolour
= bgcolour
;
1062 if (bright
&& depth
> 2)
1064 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1065 fgcolour
= CURSOR_FG
;
1066 bgcolour
= CURSOR_BG
;
1068 PmForeColor(fgcolour
);
1069 PmBackColor(bgcolour
);
1070 } else { /* No Color Quickdraw */
1071 /* XXX This should be done with a _little_ more configurability */
1073 ForeColor(blackColor
);
1074 BackColor(whiteColor
);
1076 ForeColor(whiteColor
);
1077 BackColor(blackColor
);
1081 EraseRect(&a
->textrect
);
1082 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1083 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1084 if (a
->s
->window
->grafProcs
!= NULL
)
1085 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
,
1086 a
->s
->window
->grafProcs
->textProc
);
1088 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1090 if (a
->attr
& TATTR_PASCURS
) {
1097 PmForeColor(CURSOR_BG
);
1100 FrameRect(&a
->textrect
);
1104 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1105 unsigned long attr
, int lattr
)
1108 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1112 * Call from the terminal emulator to get its graphics context.
1113 * Should probably be called start_redraw or something.
1115 void pre_paint(Session
*s
) {
1117 Rect myrect
, tmprect
;
1119 if (HAVE_COLOR_QD()) {
1120 s
->term
->attr_mask
= 0;
1122 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1123 LocalToGlobal((Point
*)&myrect
.top
);
1124 LocalToGlobal((Point
*)&myrect
.bottom
);
1125 for (gdh
= GetDeviceList();
1127 gdh
= GetNextDevice(gdh
)) {
1128 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1129 TestDeviceAttribute(gdh
, screenActive
) &&
1130 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1131 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1133 if (s
->cfg
.bold_colour
)
1134 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1135 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1138 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1141 s
->term
->attr_mask
= ~0;
1142 return; /* No point checking more screens. */
1147 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1148 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1151 Context
get_ctx(void *frontend
) {
1152 Session
*s
= frontend
;
1158 void free_ctx(Context ctx
) {
1163 * Presumably this does something in Windows
1165 void post_paint(Session
*s
) {
1170 * Set the scroll bar position
1172 * total is the line number of the bottom of the working screen
1173 * start is the line number of the top of the display
1174 * page is the length of the displayed page
1176 void set_sbar(void *frontend
, int total
, int start
, int page
) {
1177 Session
*s
= frontend
;
1179 /* We don't redraw until we've set everything up, to avoid glitches */
1180 (*s
->scrollbar
)->contrlMin
= 0;
1181 (*s
->scrollbar
)->contrlMax
= total
- page
;
1182 SetControlValue(s
->scrollbar
, start
);
1184 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1185 SetControlViewSize(s
->scrollbar
, page
);
1189 void sys_cursor(void *frontend
, int x
, int y
)
1192 * I think his is meaningless under Mac OS.
1197 * This is still called when mode==BELL_VISUAL, even though the
1198 * visual bell is handled entirely within terminal.c, because we
1199 * may want to perform additional actions on any kind of bell (for
1200 * example, taskbar flashing in Windows).
1202 void beep(void *frontend
, int mode
)
1204 if (mode
!= BELL_VISUAL
)
1207 * XXX We should indicate the relevant window and/or use the
1208 * Notification Manager
1212 int char_width(Context ctx
, int uc
)
1215 * Until we support exciting character-set stuff, assume all chars are
1222 * Set icon string -- a no-op here (Windowshade?)
1224 void set_icon(void *frontend
, char *icon
) {
1225 Session
*s
= frontend
;
1230 * Set the window title
1232 void set_title(void *frontend
, char *title
) {
1233 Session
*s
= frontend
;
1236 c2pstrcpy(mactitle
, title
);
1237 SetWTitle(s
->window
, mactitle
);
1241 * set or clear the "raw mouse message" mode
1243 void set_raw_mouse_mode(void *frontend
, int activate
)
1245 Session
*s
= frontend
;
1247 s
->raw_mouse
= activate
;
1248 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1252 * Resize the window at the emulator's request
1254 void request_resize(void *frontend
, int w
, int h
) {
1255 Session
*s
= frontend
;
1263 * Iconify (actually collapse) the window at the emulator's request.
1265 void set_iconic(void *frontend
, int iconic
)
1267 Session
*s
= frontend
;
1270 if (mac_gestalts
.apprvers
>= 0x0100 &&
1271 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1272 (features
& kWindowCanCollapse
))
1273 CollapseWindow(s
->window
, iconic
);
1277 * Move the window in response to a server-side request.
1279 void move_window(void *frontend
, int x
, int y
)
1281 Session
*s
= frontend
;
1283 MoveWindow(s
->window
, x
, y
, FALSE
);
1287 * Move the window to the top or bottom of the z-order in response
1288 * to a server-side request.
1290 void set_zorder(void *frontend
, int top
)
1292 Session
*s
= frontend
;
1295 * We also change the input focus to point to the topmost window,
1296 * since that's probably what the Human Interface Guidelines would
1300 SelectWindow(s
->window
);
1302 SendBehind(s
->window
, NULL
);
1306 * Refresh the window in response to a server-side request.
1308 void refresh_window(void *frontend
)
1310 Session
*s
= frontend
;
1312 term_invalidate(s
->term
);
1316 * Maximise or restore the window in response to a server-side
1319 void set_zoomed(void *frontend
, int zoomed
)
1321 Session
*s
= frontend
;
1323 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1327 * Report whether the window is iconic, for terminal reports.
1329 int is_iconic(void *frontend
)
1331 Session
*s
= frontend
;
1334 if (mac_gestalts
.apprvers
>= 0x0100 &&
1335 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1336 (features
& kWindowCanCollapse
))
1337 return IsWindowCollapsed(s
->window
);
1342 * Report the window's position, for terminal reports.
1344 void get_window_pos(void *frontend
, int *x
, int *y
)
1346 Session
*s
= frontend
;
1348 *x
= s
->window
->portRect
.left
;
1349 *y
= s
->window
->portRect
.top
;
1353 * Report the window's pixel size, for terminal reports.
1355 void get_window_pixels(void *frontend
, int *x
, int *y
)
1357 Session
*s
= frontend
;
1359 *x
= s
->window
->portRect
.right
- s
->window
->portRect
.left
;
1360 *y
= s
->window
->portRect
.bottom
- s
->window
->portRect
.top
;
1364 * Return the window or icon title.
1366 char *get_window_title(void *frontend
, int icon
)
1368 Session
*s
= frontend
;
1370 static char title
[256];
1372 GetWTitle(s
->window
, ptitle
);
1373 p2cstrcpy(title
, ptitle
);
1378 * real_palette_set(): This does the actual palette-changing work on behalf
1379 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1380 * is doing a batch of updates.
1382 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1386 if (!HAVE_COLOR_QD())
1388 col
.red
= r
* 0x0101;
1389 col
.green
= g
* 0x0101;
1390 col
.blue
= b
* 0x0101;
1391 SetEntryColor(s
->palette
, n
, &col
);
1395 * Set the logical palette. Called by the terminal emulator.
1397 void palette_set(void *frontend
, int n
, int r
, int g
, int b
) {
1398 Session
*s
= frontend
;
1399 static const int first
[21] = {
1400 0, 2, 4, 6, 8, 10, 12, 14,
1401 1, 3, 5, 7, 9, 11, 13, 15,
1405 if (!HAVE_COLOR_QD())
1407 real_palette_set(s
, first
[n
], r
, g
, b
);
1409 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1410 if (first
[n
] == DEFAULT_BG
)
1412 ActivatePalette(s
->window
);
1416 * Reset to the default palette
1418 void palette_reset(void *frontend
) {
1419 Session
*s
= frontend
;
1420 /* This maps colour indices in cfg to those used in our palette. */
1421 static const int ww
[] = {
1422 6, 7, 8, 9, 10, 11, 12, 13,
1423 14, 15, 16, 17, 18, 19, 20, 21,
1428 if (!HAVE_COLOR_QD())
1431 assert(lenof(ww
) == NCOLOURS
);
1433 for (i
= 0; i
< NCOLOURS
; i
++) {
1434 real_palette_set(s
, i
,
1435 s
->cfg
.colours
[ww
[i
]][0],
1436 s
->cfg
.colours
[ww
[i
]][1],
1437 s
->cfg
.colours
[ww
[i
]][2]);
1440 ActivatePalette(s
->window
);
1441 /* Palette Manager will generate update events as required. */
1445 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1448 void do_scroll(void *frontend
, int topline
, int botline
, int lines
) {
1449 Session
*s
= frontend
;
1451 RgnHandle scrollrgn
= NewRgn();
1452 RgnHandle movedupdate
= NewRgn();
1453 RgnHandle update
= NewRgn();
1454 Point g2l
= { 0, 0 };
1459 * Work out the part of the update region that will scrolled by
1463 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1464 s
->term
->cols
* s
->font_width
,
1465 (botline
+ 1) * s
->font_height
);
1467 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1468 s
->term
->cols
* s
->font_width
,
1469 (botline
- lines
+ 1) * s
->font_height
);
1470 CopyRgn(((WindowPeek
)s
->window
)->updateRgn
, movedupdate
);
1471 GlobalToLocal(&g2l
);
1472 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1473 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1474 ValidRgn(movedupdate
);
1475 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1478 if (HAVE_COLOR_QD())
1479 PmBackColor(DEFAULT_BG
);
1481 BackColor(blackColor
); /* XXX make configurable */
1482 SetRect(&r
, 0, topline
* s
->font_height
,
1483 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1484 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1487 InvalRgn(movedupdate
);
1489 DisposeRgn(scrollrgn
);
1490 DisposeRgn(movedupdate
);
1494 void logevent(void *frontend
, char *str
) {
1496 fprintf(stderr
, "%s\n", str
);
1499 /* Dummy routine, only required in plink. */
1500 void ldisc_update(void *frontend
, int echo
, int edit
)
1505 * Mac PuTTY doesn't support printing yet.
1507 printer_job
*printer_start_job(char *printer
)
1513 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1517 void printer_finish_job(printer_job
*pj
)
1521 void frontend_keypress(void *handle
)
1524 * Keypress termination in non-Close-On-Exit mode is not
1525 * currently supported in PuTTY proper, because the window
1526 * always has a perfectly good Close button anyway. So we do
1533 * Ask whether to wipe a session log file before writing to it.
1534 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1536 int askappend(void *frontend
, char *filename
)
1539 /* FIXME: not implemented yet. */
1546 * c-file-style: "simon"