1 /* $Id: macterm.c,v 1.26 2002/12/30 18:21:17 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>
67 #define NCOLOURS (lenof(((Config *)0)->colours))
70 #define DEFAULT_FG_BOLD 17
72 #define DEFAULT_BG_BOLD 19
76 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
78 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
81 static void mac_initfont(Session
*);
82 static pascal OSStatus
uni_to_font_fallback(UniChar
*, ByteCount
, ByteCount
*,
83 TextPtr
, ByteCount
, ByteCount
*,
85 ConstUnicodeMappingPtr
);
86 static void mac_initpalette(Session
*);
87 static void mac_adjustwinbg(Session
*);
88 static void mac_adjustsize(Session
*, int, int);
89 static void mac_drawgrowicon(Session
*s
);
90 static pascal void mac_growtermdraghook(void);
91 static pascal void mac_scrolltracker(ControlHandle
, short);
92 static pascal void do_text_for_device(short, short, GDHandle
, long);
93 static int mac_keytrans(Session
*, EventRecord
*, unsigned char *);
94 static void text_click(Session
*, EventRecord
*);
96 void pre_paint(Session
*s
);
97 void post_paint(Session
*s
);
100 static RoutineDescriptor mac_scrolltracker_upp
=
101 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo
,
102 (ProcPtr
)mac_scrolltracker
);
103 static RoutineDescriptor do_text_for_device_upp
=
104 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo
,
105 (ProcPtr
)do_text_for_device
);
106 #else /* not TARGET_RT_MAC_CFM */
107 #define mac_scrolltracker_upp mac_scrolltracker
108 #define do_text_for_device_upp do_text_for_device
109 #endif /* not TARGET_RT_MAC_CFM */
111 static void inbuf_putc(Session
*s
, int c
) {
114 from_backend(s
->term
, 0, &ch
, 1);
117 static void inbuf_putstr(Session
*s
, const char *c
) {
119 from_backend(s
->term
, 0, (char *)c
, strlen(c
));
122 static void display_resource(Session
*s
, unsigned long type
, short id
) {
127 h
= GetResource(type
, id
);
129 fatalbox("Can't get test resource");
130 len
= GetResourceSizeOnDisk(h
);
135 from_backend(s
->term
, 0, t
, len
);
141 void mac_newsession(void) {
144 /* This should obviously be initialised by other means */
145 s
= smalloc(sizeof(*s
));
146 memset(s
, 0, sizeof(*s
));
147 do_defaults(NULL
, &s
->cfg
);
148 s
->back
= &loop_backend
;
152 void mac_opensession(void) {
154 StandardFileReply sfr
;
155 static const OSType sftypes
[] = { 'Sess', 0, 0, 0 };
158 s
= smalloc(sizeof(*s
));
159 memset(s
, 0, sizeof(*s
));
161 StandardGetFile(NULL
, 1, sftypes
, &sfr
);
162 if (!sfr
.sfGood
) goto fail
;
164 sesshandle
= open_settings_r_fsp(&sfr
.sfFile
);
165 if (sesshandle
== NULL
) goto fail
;
166 load_open_settings(sesshandle
, TRUE
, &s
->cfg
);
167 close_settings_r(sesshandle
);
168 s
->back
= &loop_backend
;
177 void mac_startsession(Session
*s
)
182 /* XXX: Own storage management? */
184 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
186 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
187 SetWRefCon(s
->window
, (long)s
);
188 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
189 s
->term
= term_init(&s
->cfg
, s
);
191 s
->logctx
= log_init(s
);
192 term_provide_logctx(s
->term
, s
->logctx
);
194 s
->back
->init(s
->term
, &s
->backhandle
, "localhost", 23, &s
->realhost
, 0);
195 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
197 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
199 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
200 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
202 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
203 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
207 if (HAVE_COLOR_QD()) {
208 /* Set to FALSE to not get palette updates in the background. */
209 SetPalette(s
->window
, s
->palette
, TRUE
);
210 ActivatePalette(s
->window
);
212 ShowWindow(s
->window
);
213 starttime
= TickCount();
214 display_resource(s
, 'pTST', 128);
215 sprintf(msg
, "Elapsed ticks: %d\015\012", TickCount() - starttime
);
216 inbuf_putstr(s
, msg
);
220 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
222 static void mac_initfont(Session
*s
) {
228 macfont
[0] = sprintf((char *)&macfont
[1], "%s", s
->cfg
.font
);
229 GetFNum(macfont
, &s
->fontnum
);
230 TextFont(s
->fontnum
);
231 TextFace(s
->cfg
.fontisbold ? bold
: 0);
232 TextSize(s
->cfg
.fontheight
);
234 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
235 s
->font_ascent
= fi
.ascent
;
236 s
->font_leading
= fi
.leading
;
237 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
238 if (!s
->cfg
.bold_colour
) {
240 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
242 s
->font_boldadjust
= 0;
244 if (s
->uni_to_font
!= NULL
)
245 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
246 if (mac_gestalts
.encvvers
== 0 ||
247 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
248 kTextLanguageDontCare
,
249 kTextRegionDontCare
, macfont
,
251 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) != noErr
) {
252 s
->uni_to_font
= NULL
;
254 if (uni_to_font_fallback_upp
== NULL
)
255 uni_to_font_fallback_upp
=
256 NewUnicodeToTextFallbackProc(&uni_to_font_fallback
);
257 if (SetFallbackUnicodeToText(s
->uni_to_font
,
258 uni_to_font_fallback_upp
,
259 kUnicodeFallbackCustomOnly
| kUnicodeFallbackInterruptSafeMask
,
261 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
262 s
->uni_to_font
= NULL
;
266 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
269 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
270 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
271 ByteCount
*ousedp
, LogicalAddress
*cookie
, ConstUnicodeMappingPtr mapping
)
275 return kTECOutputBufferFullStatus
;
284 * To be called whenever the window size changes.
285 * rows and cols should be desired values.
286 * It's assumed the terminal emulator will be informed, and will set rows
289 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
290 int winwidth
, winheight
;
292 winwidth
= newcols
* s
->font_width
+ 15;
293 winheight
= newrows
* s
->font_height
;
294 SizeWindow(s
->window
, winwidth
, winheight
, true);
295 HideControl(s
->scrollbar
);
296 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
297 SizeControl(s
->scrollbar
, 16, winheight
- 13);
298 ShowControl(s
->scrollbar
);
302 static void mac_initpalette(Session
*s
) {
304 if (!HAVE_COLOR_QD())
307 * Most colours should be inhibited on 2bpp displays.
308 * Palette manager documentation suggests inhibiting all tolerant colours
309 * on greyscale displays.
311 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
312 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
313 #define PM_TOLERANCE 0x2000
314 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
315 if (s
->palette
== NULL
)
316 fatalbox("Unable to create palette");
317 /* In 2bpp, these are the colours we want most. */
318 SetEntryUsage(s
->palette
, DEFAULT_BG
,
319 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
320 SetEntryUsage(s
->palette
, DEFAULT_FG
,
321 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
322 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
323 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
324 SetEntryUsage(s
->palette
, CURSOR_BG
,
325 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
330 * Set the background colour of the window correctly. Should be
331 * called whenever the default background changes.
333 static void mac_adjustwinbg(Session
*s
) {
335 if (!HAVE_COLOR_QD())
337 #if TARGET_RT_CFM /* XXX doesn't link (at least for 68k) */
338 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
339 SetWindowContentColor(s
->window
,
340 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
344 if (s
->wctab
== NULL
)
345 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
346 if (s
->wctab
== NULL
)
347 return; /* do without */
348 (*s
->wctab
)->wCSeed
= 0;
349 (*s
->wctab
)->wCReserved
= 0;
350 (*s
->wctab
)->ctSize
= 0;
351 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
352 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
353 SetWinColor(s
->window
, s
->wctab
);
358 * Set the cursor shape correctly
360 void mac_adjusttermcursor(WindowPtr window
, Point mouse
, RgnHandle cursrgn
) {
362 ControlHandle control
;
367 s
= (Session
*)GetWRefCon(window
);
368 GlobalToLocal(&mouse
);
369 part
= FindControl(mouse
, window
, &control
);
370 if (control
== s
->scrollbar
) {
371 SetCursor(&qd
.arrow
);
372 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
373 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
375 x
= mouse
.h
/ s
->font_width
;
376 y
= mouse
.v
/ s
->font_height
;
378 SetCursor(&qd
.arrow
);
380 SetCursor(*GetCursor(iBeamCursor
));
381 /* Ask for shape changes if we leave this character cell. */
382 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
383 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
384 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
389 * Enable/disable menu items based on the active terminal window.
391 void mac_adjusttermmenus(WindowPtr window
) {
396 s
= (Session
*)GetWRefCon(window
);
397 menu
= GetMenuHandle(mEdit
);
399 DisableItem(menu
, iUndo
);
400 DisableItem(menu
, iCut
);
401 if (1/*s->term->selstate == SELECTED*/)
402 EnableItem(menu
, iCopy
);
404 DisableItem(menu
, iCopy
);
405 if (GetScrap(NULL
, 'TEXT', &offset
) == noTypeErr
)
406 DisableItem(menu
, iPaste
);
408 EnableItem(menu
, iPaste
);
409 DisableItem(menu
, iClear
);
410 EnableItem(menu
, iSelectAll
);
413 void mac_menuterm(WindowPtr window
, short menu
, short item
) {
416 s
= (Session
*)GetWRefCon(window
);
424 term_do_paste(s
->term
);
430 void mac_clickterm(WindowPtr window
, EventRecord
*event
) {
433 ControlHandle control
;
436 s
= (Session
*)GetWRefCon(window
);
438 mouse
= event
->where
;
439 GlobalToLocal(&mouse
);
440 part
= FindControl(mouse
, window
, &control
);
441 if (control
== s
->scrollbar
) {
443 case kControlIndicatorPart
:
444 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
445 term_scroll(s
->term
, +1, GetControlValue(control
));
447 case kControlUpButtonPart
:
448 case kControlDownButtonPart
:
449 case kControlPageUpPart
:
450 case kControlPageDownPart
:
451 TrackControl(control
, mouse
, &mac_scrolltracker_upp
);
455 text_click(s
, event
);
459 static void text_click(Session
*s
, EventRecord
*event
) {
462 static UInt32 lastwhen
= 0;
463 static Session
*lastsess
= NULL
;
464 static int lastrow
= -1, lastcol
= -1;
465 static Mouse_Action lastact
= MA_NOTHING
;
468 localwhere
= event
->where
;
469 GlobalToLocal(&localwhere
);
471 col
= PTOCC(localwhere
.h
);
472 row
= PTOCR(localwhere
.v
);
473 if (event
->when
- lastwhen
< GetDblTime() &&
474 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
475 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
476 lastact
== MA_2CLK ? MA_3CLK
:
477 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
480 /* Fake right button with shift key */
481 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
482 lastact
, col
, row
, event
->modifiers
& shiftKey
,
483 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
487 while (StillDown()) {
488 GetMouse(&localwhere
);
489 col
= PTOCC(localwhere
.h
);
490 row
= PTOCR(localwhere
.v
);
492 event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
493 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
494 event
->modifiers
& controlKey
,
495 event
->modifiers
& optionKey
);
496 if (row
> s
->term
->rows
- 1)
497 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
499 term_scroll(s
->term
, 0, row
);
501 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
502 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
503 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
504 lastwhen
= TickCount();
507 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
520 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
) {
523 * See "Programming with the Text Encoding Conversion Manager"
524 * Appendix E for Unicode scrap conventions.
526 * XXX Need to support TEXT/styl scrap as well.
527 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
528 * XXX Maybe PICT scrap too.
530 if (ZeroScrap() != noErr
)
532 PutScrap(len
* sizeof(*data
), 'utxt', data
);
535 void get_clip(void *frontend
, wchar_t **p
, int *lenp
) {
536 Session
*s
= frontend
;
537 static Handle h
= NULL
;
546 /* XXX Support TEXT-format scrap as well. */
547 if (GetScrap(NULL
, 'utxt', &offset
) > 0) {
549 *lenp
= GetScrap(h
, 'utxt', &offset
) / sizeof(**p
);
552 if (*p
== NULL
|| *lenp
<= 0)
553 fatalbox("Empty scrap");
560 static pascal void mac_scrolltracker(ControlHandle control
, short part
) {
563 s
= (Session
*)GetWRefCon((*control
)->contrlOwner
);
565 case kControlUpButtonPart
:
566 term_scroll(s
->term
, 0, -1);
568 case kControlDownButtonPart
:
569 term_scroll(s
->term
, 0, +1);
571 case kControlPageUpPart
:
572 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
574 case kControlPageDownPart
:
575 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
596 #define K_INSERT 0x7200
597 #define K_HOME 0x7300
598 #define K_PRIOR 0x7400
599 #define K_DELETE 0x7500
601 #define K_NEXT 0x7900
602 #define K_LEFT 0x7b00
603 #define K_RIGHT 0x7c00
604 #define K_DOWN 0x7d00
616 #define KP_CLEAR 0x4700
617 #define KP_EQUAL 0x5100
618 #define KP_SLASH 0x4b00
619 #define KP_STAR 0x4300
620 #define KP_PLUS 0x4500
621 #define KP_MINUS 0x4e00
622 #define KP_DOT 0x4100
623 #define KP_ENTER 0x4c00
625 void mac_keyterm(WindowPtr window
, EventRecord
*event
) {
626 unsigned char buf
[20];
630 s
= (Session
*)GetWRefCon(window
);
631 len
= mac_keytrans(s
, event
, buf
);
632 ldisc_send(s
->ldisc
, (char *)buf
, len
, 1);
634 term_seen_key_event(s
->term
);
636 term_update(s
->term
);
639 static int mac_keytrans(Session
*s
, EventRecord
*event
,
640 unsigned char *output
) {
641 unsigned char *p
= output
;
644 /* No meta key yet -- that'll be rather fun. */
646 /* Keys that we handle locally */
647 if (event
->modifiers
& shiftKey
) {
648 switch (event
->message
& keyCodeMask
) {
649 case K_PRIOR
: /* shift-pageup */
650 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
652 case K_NEXT
: /* shift-pagedown */
653 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
659 * Control-2 should return ^@ (0x00), Control-6 should return
660 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
661 * the DOS keyboard handling did it, and we have nothing better
662 * to do with the key combo in question, we'll also map
663 * Control-Backquote to ^\ (0x1C).
666 if (event
->modifiers
& controlKey
) {
667 switch (event
->message
& charCodeMask
) {
684 * First, all the keys that do tilde codes. (ESC '[' nn '~',
685 * for integer decimal nn.)
687 * We also deal with the weird ones here. Linux VCs replace F1
688 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
689 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
693 switch (event
->message
& keyCodeMask
) {
694 case K_F1
: code
= (event
->modifiers
& shiftKey ?
23 : 11); break;
695 case K_F2
: code
= (event
->modifiers
& shiftKey ?
24 : 12); break;
696 case K_F3
: code
= (event
->modifiers
& shiftKey ?
25 : 13); break;
697 case K_F4
: code
= (event
->modifiers
& shiftKey ?
26 : 14); break;
698 case K_F5
: code
= (event
->modifiers
& shiftKey ?
28 : 15); break;
699 case K_F6
: code
= (event
->modifiers
& shiftKey ?
29 : 17); break;
700 case K_F7
: code
= (event
->modifiers
& shiftKey ?
31 : 18); break;
701 case K_F8
: code
= (event
->modifiers
& shiftKey ?
32 : 19); break;
702 case K_F9
: code
= (event
->modifiers
& shiftKey ?
33 : 20); break;
703 case K_F10
: code
= (event
->modifiers
& shiftKey ?
34 : 21); break;
704 case K_F11
: code
= 23; break;
705 case K_F12
: code
= 24; break;
706 case K_HOME
: code
= 1; break;
707 case K_INSERT
: code
= 2; break;
708 case K_DELETE
: code
= 3; break;
709 case K_END
: code
= 4; break;
710 case K_PRIOR
: code
= 5; break;
711 case K_NEXT
: code
= 6; break;
713 if (s
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
714 p
+= sprintf((char *)p
, "\x1B[[%c", code
+ 'A' - 11);
717 if (s
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
718 p
+= sprintf((char *)p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
722 p
+= sprintf((char *)p
, "\x1B[%d~", code
);
726 if (s
->term
->app_keypad_keys
) {
727 switch (event
->message
& keyCodeMask
) {
728 case KP_ENTER
: p
+= sprintf((char *)p
, "\x1BOM"); return p
- output
;
729 case KP_CLEAR
: p
+= sprintf((char *)p
, "\x1BOP"); return p
- output
;
730 case KP_EQUAL
: p
+= sprintf((char *)p
, "\x1BOQ"); return p
- output
;
731 case KP_SLASH
: p
+= sprintf((char *)p
, "\x1BOR"); return p
- output
;
732 case KP_STAR
: p
+= sprintf((char *)p
, "\x1BOS"); return p
- output
;
733 case KP_PLUS
: p
+= sprintf((char *)p
, "\x1BOl"); return p
- output
;
734 case KP_MINUS
: p
+= sprintf((char *)p
, "\x1BOm"); return p
- output
;
735 case KP_DOT
: p
+= sprintf((char *)p
, "\x1BOn"); return p
- output
;
736 case KP_0
: p
+= sprintf((char *)p
, "\x1BOp"); return p
- output
;
737 case KP_1
: p
+= sprintf((char *)p
, "\x1BOq"); return p
- output
;
738 case KP_2
: p
+= sprintf((char *)p
, "\x1BOr"); return p
- output
;
739 case KP_3
: p
+= sprintf((char *)p
, "\x1BOs"); return p
- output
;
740 case KP_4
: p
+= sprintf((char *)p
, "\x1BOt"); return p
- output
;
741 case KP_5
: p
+= sprintf((char *)p
, "\x1BOu"); return p
- output
;
742 case KP_6
: p
+= sprintf((char *)p
, "\x1BOv"); return p
- output
;
743 case KP_7
: p
+= sprintf((char *)p
, "\x1BOw"); return p
- output
;
744 case KP_8
: p
+= sprintf((char *)p
, "\x1BOx"); return p
- output
;
745 case KP_9
: p
+= sprintf((char *)p
, "\x1BOy"); return p
- output
;
749 switch (event
->message
& keyCodeMask
) {
751 p
+= sprintf((char *)p
,
752 s
->term
->app_cursor_keys ?
"\x1BOA" : "\x1B[A");
755 p
+= sprintf((char *)p
,
756 s
->term
->app_cursor_keys ?
"\x1BOB" : "\x1B[B");
759 p
+= sprintf((char *)p
,
760 s
->term
->app_cursor_keys ?
"\x1BOC" : "\x1B[C");
763 p
+= sprintf((char *)p
,
764 s
->term
->app_cursor_keys ?
"\x1BOD" : "\x1B[D");
770 *p
++ = (s
->cfg
.bksp_is_delete ?
0x7f : 0x08);
773 *p
++ = event
->message
& charCodeMask
;
778 void request_paste(void *frontend
)
780 Session
*s
= frontend
;
783 * In the Mac OS, pasting is synchronous: we can read the
784 * clipboard with no difficulty, so request_paste() can just go
787 term_do_paste(s
->term
);
798 void mac_growterm(WindowPtr window
, EventRecord
*event
) {
801 int newrows
, newcols
;
803 DragGrayRgnUPP draghooksave
;
807 s
= (Session
*)GetWRefCon(window
);
809 draghooksave
= LMGetDragHook();
810 growterm_state
.oldmsg
[0] = '\0';
811 growterm_state
.startmouse
= event
->where
;
812 growterm_state
.s
= s
;
815 BackColor(whiteColor
);
816 ForeColor(blackColor
);
817 TextFont(systemFont
);
821 SetRect(&growterm_state
.msgrect
, 0, 0,
822 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
823 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
824 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
826 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
827 grow_result
= GrowWindow(window
, event
->where
, &limits
);
829 DisposeDragGrayRgnUPP(LMGetDragHook());
830 LMSetDragHook(draghooksave
);
831 InvalRect(&growterm_state
.msgrect
);
835 if (grow_result
!= 0) {
836 newrows
= HiWord(grow_result
) / s
->font_height
;
837 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
838 mac_adjustsize(s
, newrows
, newcols
);
839 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
843 static pascal void mac_growtermdraghook(void)
845 Session
*s
= growterm_state
.s
;
849 int newrows
, newcols
;
852 newrows
= (mouse
.v
- growterm_state
.startmouse
.v
) / s
->font_height
+
854 if (newrows
< 1) newrows
= 1;
855 newcols
= (mouse
.h
- growterm_state
.startmouse
.h
) / s
->font_width
+
857 if (newcols
< 1) newcols
= 1;
858 sprintf(buf
, "%dx%d", newcols
, newrows
);
859 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
861 strcpy(growterm_state
.oldmsg
, buf
);
865 SetPort(growterm_state
.s
->window
);
866 EraseRect(&growterm_state
.msgrect
);
867 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
868 DrawString((StringPtr
)buf
);
872 void mac_activateterm(WindowPtr window
, Boolean active
) {
875 s
= (Session
*)GetWRefCon(window
);
876 s
->term
->has_focus
= active
;
877 term_update(s
->term
);
879 ShowControl(s
->scrollbar
);
882 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
884 BackColor(blackColor
);
885 HideControl(s
->scrollbar
);
890 void mac_updateterm(WindowPtr window
) {
893 s
= (Session
*)GetWRefCon(window
);
897 term_paint(s
->term
, s
,
898 PTOCC((*window
->visRgn
)->rgnBBox
.left
),
899 PTOCR((*window
->visRgn
)->rgnBBox
.top
),
900 PTOCC((*window
->visRgn
)->rgnBBox
.right
),
901 PTOCR((*window
->visRgn
)->rgnBBox
.bottom
), 1);
902 /* Restore default colours in case the Window Manager uses them */
903 if (HAVE_COLOR_QD()) {
904 PmForeColor(DEFAULT_FG
);
905 PmBackColor(DEFAULT_BG
);
907 ForeColor(whiteColor
);
908 BackColor(blackColor
);
910 if (FrontWindow() != window
)
911 EraseRect(&(*s
->scrollbar
)->contrlRect
);
912 UpdateControls(window
, window
->visRgn
);
918 static void mac_drawgrowicon(Session
*s
) {
924 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
925 * See Tech Note TB575 for details.
927 clip
= s
->window
->portRect
;
928 clip
.left
= clip
.right
- 15;
932 DrawGrowIcon(s
->window
);
937 struct do_text_args
{
948 * Call from the terminal emulator to draw a bit of text
950 * x and y are text row and column (zero-based)
952 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
953 unsigned long attr
, int lattr
) {
956 struct do_text_args a
;
958 char mactextbuf
[1024];
959 UniChar unitextbuf
[1024];
966 /* First check this text is relevant */
967 a
.textrect
.top
= y
* s
->font_height
;
968 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
969 a
.textrect
.left
= x
* s
->font_width
;
970 a
.textrect
.right
= (x
+ len
) * s
->font_width
;
971 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
974 if (s
->uni_to_font
!= NULL
) {
975 ByteCount iread
, olen
;
978 for (i
= 0; i
< len
; i
++)
979 unitextbuf
[i
] = (unsigned char)text
[i
] | (attr
& CSET_MASK
);
980 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
981 unitextbuf
, kUnicodeUseFallbacksMask
,
983 1024, &iread
, &olen
, mactextbuf
);
984 if (err
== noErr
|| err
== kTECUsedFallbacksStatus
)
985 text
= mactextbuf
; len
= olen
;
993 a
.numer
.h
= a
.numer
.v
= a
.denom
.h
= a
.denom
.v
= 1;
995 TextFont(s
->fontnum
);
996 if (s
->cfg
.fontisbold
|| (attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
998 if (attr
& ATTR_UNDER
)
1001 TextSize(s
->cfg
.fontheight
);
1003 if (HAVE_COLOR_QD())
1005 SpaceExtra(s
->font_boldadjust
<< 16);
1006 CharExtra(s
->font_boldadjust
<< 16);
1012 RectRgn(textrgn
, &a
.textrect
);
1013 if (HAVE_COLOR_QD())
1014 DeviceLoop(textrgn
, &do_text_for_device_upp
, (long)&a
, 0);
1016 do_text_for_device(1, 0, NULL
, (long)&a
);
1017 DisposeRgn(textrgn
);
1018 /* Tell the window manager about it in case this isn't an update */
1019 ValidRect(&a
.textrect
);
1022 static pascal void do_text_for_device(short depth
, short devflags
,
1023 GDHandle device
, long cookie
) {
1024 struct do_text_args
*a
;
1025 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1027 a
= (struct do_text_args
*)cookie
;
1029 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1030 reverse
= a
->attr
& ATTR_REVERSE
;
1032 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1035 if (HAVE_COLOR_QD()) {
1037 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
) * 2;
1038 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
) * 2;
1041 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1042 * boldens the background, because that's all we can do.
1044 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1045 bgcolour
= DEFAULT_BG
;
1049 fgcolour
= bgcolour
;
1052 if (bright
&& depth
> 2)
1054 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1055 fgcolour
= CURSOR_FG
;
1056 bgcolour
= CURSOR_BG
;
1058 PmForeColor(fgcolour
);
1059 PmBackColor(bgcolour
);
1060 } else { /* No Color Quickdraw */
1061 /* XXX This should be done with a _little_ more configurability */
1063 ForeColor(blackColor
);
1064 BackColor(whiteColor
);
1066 ForeColor(whiteColor
);
1067 BackColor(blackColor
);
1071 EraseRect(&a
->textrect
);
1072 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1073 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1074 if (a
->s
->window
->grafProcs
!= NULL
)
1075 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
,
1076 a
->s
->window
->grafProcs
->textProc
);
1078 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1080 if (a
->attr
& TATTR_PASCURS
) {
1087 PmForeColor(CURSOR_BG
);
1090 FrameRect(&a
->textrect
);
1094 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1095 unsigned long attr
, int lattr
)
1098 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1102 * Call from the terminal emulator to get its graphics context.
1103 * Should probably be called start_redraw or something.
1105 void pre_paint(Session
*s
) {
1107 Rect myrect
, tmprect
;
1109 if (HAVE_COLOR_QD()) {
1110 s
->term
->attr_mask
= 0;
1112 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1113 LocalToGlobal((Point
*)&myrect
.top
);
1114 LocalToGlobal((Point
*)&myrect
.bottom
);
1115 for (gdh
= GetDeviceList();
1117 gdh
= GetNextDevice(gdh
)) {
1118 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1119 TestDeviceAttribute(gdh
, screenActive
) &&
1120 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1121 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1123 if (s
->cfg
.bold_colour
)
1124 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1125 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1128 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1131 s
->term
->attr_mask
= ~0;
1132 return; /* No point checking more screens. */
1137 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1138 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1141 Context
get_ctx(void *frontend
) {
1142 Session
*s
= frontend
;
1148 void free_ctx(Context ctx
) {
1153 * Presumably this does something in Windows
1155 void post_paint(Session
*s
) {
1160 * Set the scroll bar position
1162 * total is the line number of the bottom of the working screen
1163 * start is the line number of the top of the display
1164 * page is the length of the displayed page
1166 void set_sbar(void *frontend
, int total
, int start
, int page
) {
1167 Session
*s
= frontend
;
1169 /* We don't redraw until we've set everything up, to avoid glitches */
1170 (*s
->scrollbar
)->contrlMin
= 0;
1171 (*s
->scrollbar
)->contrlMax
= total
- page
;
1172 SetControlValue(s
->scrollbar
, start
);
1174 /* XXX: This doesn't link for me. */
1175 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1176 SetControlViewSize(s
->scrollbar
, page
);
1180 void sys_cursor(void *frontend
, int x
, int y
)
1183 * I think his is meaningless under Mac OS.
1188 * This is still called when mode==BELL_VISUAL, even though the
1189 * visual bell is handled entirely within terminal.c, because we
1190 * may want to perform additional actions on any kind of bell (for
1191 * example, taskbar flashing in Windows).
1193 void beep(void *frontend
, int mode
)
1195 if (mode
!= BELL_VISUAL
)
1198 * XXX We should indicate the relevant window and/or use the
1199 * Notification Manager
1203 int char_width(Context ctx
, int uc
)
1206 * Until we support exciting character-set stuff, assume all chars are
1213 * Set icon string -- a no-op here (Windowshade?)
1215 void set_icon(void *frontend
, char *icon
) {
1216 Session
*s
= frontend
;
1221 * Set the window title
1223 void set_title(void *frontend
, char *title
) {
1224 Session
*s
= frontend
;
1227 mactitle
[0] = sprintf((char *)&mactitle
[1], "%s", title
);
1228 SetWTitle(s
->window
, mactitle
);
1232 * set or clear the "raw mouse message" mode
1234 void set_raw_mouse_mode(void *frontend
, int activate
)
1236 Session
*s
= frontend
;
1238 s
->raw_mouse
= activate
;
1239 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1243 * Resize the window at the emulator's request
1245 void request_resize(void *frontend
, int w
, int h
) {
1246 Session
*s
= frontend
;
1254 * Iconify (actually collapse) the window at the emulator's request.
1256 void set_iconic(void *frontend
, int iconic
)
1258 Session
*s
= frontend
;
1261 if (mac_gestalts
.apprvers
>= 0x0100 &&
1262 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1263 (features
& kWindowCanCollapse
))
1264 CollapseWindow(s
->window
, iconic
);
1268 * Move the window in response to a server-side request.
1270 void move_window(void *frontend
, int x
, int y
)
1272 Session
*s
= frontend
;
1274 MoveWindow(s
->window
, x
, y
, FALSE
);
1278 * Move the window to the top or bottom of the z-order in response
1279 * to a server-side request.
1281 void set_zorder(void *frontend
, int top
)
1283 Session
*s
= frontend
;
1286 * We also change the input focus to point to the topmost window,
1287 * since that's probably what the Human Interface Guidelines would
1291 SelectWindow(s
->window
);
1293 SendBehind(s
->window
, NULL
);
1297 * Refresh the window in response to a server-side request.
1299 void refresh_window(void *frontend
)
1301 Session
*s
= frontend
;
1303 term_invalidate(s
->term
);
1307 * Maximise or restore the window in response to a server-side
1310 void set_zoomed(void *frontend
, int zoomed
)
1312 Session
*s
= frontend
;
1314 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1318 * Report whether the window is iconic, for terminal reports.
1320 int is_iconic(void *frontend
)
1322 Session
*s
= frontend
;
1325 if (mac_gestalts
.apprvers
>= 0x0100 &&
1326 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1327 (features
& kWindowCanCollapse
))
1328 return IsWindowCollapsed(s
->window
);
1333 * Report the window's position, for terminal reports.
1335 void get_window_pos(void *frontend
, int *x
, int *y
)
1337 Session
*s
= frontend
;
1339 *x
= s
->window
->portRect
.left
;
1340 *y
= s
->window
->portRect
.top
;
1344 * Report the window's pixel size, for terminal reports.
1346 void get_window_pixels(void *frontend
, int *x
, int *y
)
1348 Session
*s
= frontend
;
1350 *x
= s
->window
->portRect
.right
- s
->window
->portRect
.left
;
1351 *y
= s
->window
->portRect
.bottom
- s
->window
->portRect
.top
;
1355 * Return the window or icon title.
1357 char *get_window_title(void *frontend
, int icon
)
1359 Session
*s
= frontend
;
1361 /* Erm, we don't save this at the moment */
1366 * real_palette_set(): This does the actual palette-changing work on behalf
1367 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1368 * is doing a batch of updates.
1370 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1374 if (!HAVE_COLOR_QD())
1376 col
.red
= r
* 0x0101;
1377 col
.green
= g
* 0x0101;
1378 col
.blue
= b
* 0x0101;
1379 SetEntryColor(s
->palette
, n
, &col
);
1383 * Set the logical palette. Called by the terminal emulator.
1385 void palette_set(void *frontend
, int n
, int r
, int g
, int b
) {
1386 Session
*s
= frontend
;
1387 static const int first
[21] = {
1388 0, 2, 4, 6, 8, 10, 12, 14,
1389 1, 3, 5, 7, 9, 11, 13, 15,
1393 if (!HAVE_COLOR_QD())
1395 real_palette_set(s
, first
[n
], r
, g
, b
);
1397 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1398 if (first
[n
] == DEFAULT_BG
)
1400 ActivatePalette(s
->window
);
1404 * Reset to the default palette
1406 void palette_reset(void *frontend
) {
1407 Session
*s
= frontend
;
1408 /* This maps colour indices in cfg to those used in our palette. */
1409 static const int ww
[] = {
1410 6, 7, 8, 9, 10, 11, 12, 13,
1411 14, 15, 16, 17, 18, 19, 20, 21,
1416 if (!HAVE_COLOR_QD())
1419 assert(lenof(ww
) == NCOLOURS
);
1421 for (i
= 0; i
< NCOLOURS
; i
++) {
1422 real_palette_set(s
, i
,
1423 s
->cfg
.colours
[ww
[i
]][0],
1424 s
->cfg
.colours
[ww
[i
]][1],
1425 s
->cfg
.colours
[ww
[i
]][2]);
1428 ActivatePalette(s
->window
);
1429 /* Palette Manager will generate update events as required. */
1433 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1436 void do_scroll(void *frontend
, int topline
, int botline
, int lines
) {
1437 Session
*s
= frontend
;
1439 RgnHandle scrollrgn
= NewRgn();
1440 RgnHandle movedupdate
= NewRgn();
1441 RgnHandle update
= NewRgn();
1442 Point g2l
= { 0, 0 };
1447 * Work out the part of the update region that will scrolled by
1451 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1452 s
->term
->cols
* s
->font_width
,
1453 (botline
+ 1) * s
->font_height
);
1455 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1456 s
->term
->cols
* s
->font_width
,
1457 (botline
- lines
+ 1) * s
->font_height
);
1458 CopyRgn(((WindowPeek
)s
->window
)->updateRgn
, movedupdate
);
1459 GlobalToLocal(&g2l
);
1460 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1461 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1462 ValidRgn(movedupdate
);
1463 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1466 if (HAVE_COLOR_QD())
1467 PmBackColor(DEFAULT_BG
);
1469 BackColor(blackColor
); /* XXX make configurable */
1470 SetRect(&r
, 0, topline
* s
->font_height
,
1471 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1472 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1475 InvalRgn(movedupdate
);
1477 DisposeRgn(scrollrgn
);
1478 DisposeRgn(movedupdate
);
1482 void logevent(void *frontend
, char *str
) {
1484 /* XXX Do something */
1487 /* Dummy routine, only required in plink. */
1488 void ldisc_update(void *frontend
, int echo
, int edit
)
1493 * Mac PuTTY doesn't support printing yet.
1495 printer_job
*printer_start_job(char *printer
)
1501 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1505 void printer_finish_job(printer_job
*pj
)
1509 void frontend_keypress(void *handle
)
1512 * Keypress termination in non-Close-On-Exit mode is not
1513 * currently supported in PuTTY proper, because the window
1514 * always has a perfectly good Close button anyway. So we do
1521 * Ask whether to wipe a session log file before writing to it.
1522 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1524 int askappend(void *frontend
, char *filename
)
1527 /* FIXME: not implemented yet. */
1534 * c-file-style: "simon"