1 /* $Id: macterm.c,v 1.10 2002/11/23 22:36:56 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>
38 #include <MacMemory.h>
39 #include <MacWindows.h>
40 #include <MixedMode.h>
42 #include <Quickdraw.h>
43 #include <QuickdrawText.h>
44 #include <Resources.h>
49 #include <ToolUtils.h>
62 #define NCOLOURS (lenof(((Config *)0)->colours))
65 #define DEFAULT_FG_BOLD 17
67 #define DEFAULT_BG_BOLD 19
71 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
73 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
76 static void mac_initfont(Session
*);
77 static void mac_initpalette(Session
*);
78 static void mac_adjustwinbg(Session
*);
79 static void mac_adjustsize(Session
*, int, int);
80 static void mac_drawgrowicon(Session
*s
);
81 static pascal void mac_scrolltracker(ControlHandle
, short);
82 static pascal void do_text_for_device(short, short, GDHandle
, long);
83 static pascal void mac_set_attr_mask(short, short, GDHandle
, long);
84 static int mac_keytrans(Session
*, EventRecord
*, unsigned char *);
85 static void text_click(Session
*, EventRecord
*);
87 void pre_paint(Session
*s
);
88 void post_paint(Session
*s
);
91 static RoutineDescriptor mac_scrolltracker_upp
=
92 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo
,
93 (ProcPtr
)mac_scrolltracker
);
94 static RoutineDescriptor do_text_for_device_upp
=
95 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo
,
96 (ProcPtr
)do_text_for_device
);
97 static RoutineDescriptor mac_set_attr_mask_upp
=
98 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo
,
99 (ProcPtr
)mac_set_attr_mask
);
100 #else /* not TARGET_RT_MAC_CFM */
101 #define mac_scrolltracker_upp mac_scrolltracker
102 #define do_text_for_device_upp do_text_for_device
103 #define mac_set_attr_mask_upp mac_set_attr_mask
104 #endif /* not TARGET_RT_MAC_CFM */
106 static void inbuf_putc(Session
*s
, int c
) {
109 from_backend(s
->term
, 0, &ch
, 1);
112 static void inbuf_putstr(Session
*s
, const char *c
) {
114 from_backend(s
->term
, 0, (char *)c
, strlen(c
));
117 static void display_resource(Session
*s
, unsigned long type
, short id
) {
122 h
= GetResource(type
, id
);
124 fatalbox("Can't get test resource");
125 len
= GetResourceSizeOnDisk(h
);
130 from_backend(s
->term
, 0, t
, len
);
136 void mac_newsession(void) {
142 /* This should obviously be initialised by other means */
143 s
= smalloc(sizeof(*s
));
144 memset(s
, 0, sizeof(*s
));
145 do_defaults(NULL
, &s
->cfg
);
146 s
->back
= &loop_backend
;
148 /* XXX: Own storage management? */
150 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
152 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
153 SetWRefCon(s
->window
, (long)s
);
154 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
155 s
->term
= term_init(&s
->cfg
, s
);
157 s
->logctx
= log_init(s
);
158 term_provide_logctx(s
->term
, s
->logctx
);
160 s
->back
->init(s
->term
, &s
->backhandle
, "localhost", 23, &s
->realhost
, 0);
161 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
163 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
165 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
166 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
168 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
169 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
173 s
->attr_mask
= ATTR_MASK
;
174 if (HAVE_COLOR_QD()) {
175 /* Set to FALSE to not get palette updates in the background. */
176 SetPalette(s
->window
, s
->palette
, TRUE
);
177 ActivatePalette(s
->window
);
179 ShowWindow(s
->window
);
180 starttime
= TickCount();
181 display_resource(s
, 'pTST', 128);
182 sprintf(msg
, "Elapsed ticks: %d\015\012", TickCount() - starttime
);
183 inbuf_putstr(s
, msg
);
187 static void mac_initfont(Session
*s
) {
192 macfont
[0] = sprintf((char *)&macfont
[1], "%s", s
->cfg
.font
);
193 GetFNum(macfont
, &s
->fontnum
);
194 TextFont(s
->fontnum
);
195 TextFace(s
->cfg
.fontisbold ? bold
: 0);
196 TextSize(s
->cfg
.fontheight
);
198 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
199 s
->font_ascent
= fi
.ascent
;
200 s
->font_leading
= fi
.leading
;
201 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
202 if (!s
->cfg
.bold_colour
) {
204 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
206 s
->font_boldadjust
= 0;
207 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
211 * To be called whenever the window size changes.
212 * rows and cols should be desired values.
213 * It's assumed the terminal emulator will be informed, and will set rows
216 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
217 int winwidth
, winheight
;
219 winwidth
= newcols
* s
->font_width
+ 15;
220 winheight
= newrows
* s
->font_height
;
221 SizeWindow(s
->window
, winwidth
, winheight
, true);
222 HideControl(s
->scrollbar
);
223 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
224 SizeControl(s
->scrollbar
, 16, winheight
- 13);
225 ShowControl(s
->scrollbar
);
228 static void mac_initpalette(Session
*s
) {
231 if (!HAVE_COLOR_QD())
234 * Most colours should be inhibited on 2bpp displays.
235 * Palette manager documentation suggests inhibiting all tolerant colours
236 * on greyscale displays.
238 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
239 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
240 #define PM_TOLERANCE 0x2000
241 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
242 if (s
->palette
== NULL
)
243 fatalbox("Unable to create palette");
244 /* In 2bpp, these are the colours we want most. */
245 SetEntryUsage(s
->palette
, DEFAULT_BG
,
246 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
247 SetEntryUsage(s
->palette
, DEFAULT_FG
,
248 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
249 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
250 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
251 SetEntryUsage(s
->palette
, CURSOR_BG
,
252 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
257 * Set the background colour of the window correctly. Should be
258 * called whenever the default background changes.
260 static void mac_adjustwinbg(Session
*s
) {
262 if (!HAVE_COLOR_QD())
264 #if TARGET_RT_CFM /* XXX doesn't link (at least for 68k) */
265 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
266 SetWindowContentColor(s
->window
,
267 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
271 if (s
->wctab
== NULL
)
272 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
273 if (s
->wctab
== NULL
)
274 return; /* do without */
275 (*s
->wctab
)->wCSeed
= 0;
276 (*s
->wctab
)->wCReserved
= 0;
277 (*s
->wctab
)->ctSize
= 0;
278 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
279 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
280 SetWinColor(s
->window
, s
->wctab
);
285 * Set the cursor shape correctly
287 void mac_adjusttermcursor(WindowPtr window
, Point mouse
, RgnHandle cursrgn
) {
289 ControlHandle control
;
294 s
= (Session
*)GetWRefCon(window
);
295 GlobalToLocal(&mouse
);
296 part
= FindControl(mouse
, window
, &control
);
297 if (control
== s
->scrollbar
) {
298 SetCursor(&qd
.arrow
);
299 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
300 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
302 x
= mouse
.h
/ s
->font_width
;
303 y
= mouse
.v
/ s
->font_height
;
305 SetCursor(&qd
.arrow
);
307 SetCursor(*GetCursor(iBeamCursor
));
308 /* Ask for shape changes if we leave this character cell. */
309 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
310 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
311 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
316 * Enable/disable menu items based on the active terminal window.
318 void mac_adjusttermmenus(WindowPtr window
) {
323 s
= (Session
*)GetWRefCon(window
);
324 menu
= GetMenuHandle(mEdit
);
326 DisableItem(menu
, iUndo
);
327 DisableItem(menu
, iCut
);
328 if (1/*s->term->selstate == SELECTED*/)
329 EnableItem(menu
, iCopy
);
331 DisableItem(menu
, iCopy
);
332 if (GetScrap(NULL
, 'TEXT', &offset
) == noTypeErr
)
333 DisableItem(menu
, iPaste
);
335 EnableItem(menu
, iPaste
);
336 DisableItem(menu
, iClear
);
337 EnableItem(menu
, iSelectAll
);
340 void mac_menuterm(WindowPtr window
, short menu
, short item
) {
343 s
= (Session
*)GetWRefCon(window
);
351 term_do_paste(s
->term
);
357 void mac_clickterm(WindowPtr window
, EventRecord
*event
) {
360 ControlHandle control
;
363 s
= (Session
*)GetWRefCon(window
);
365 mouse
= event
->where
;
366 GlobalToLocal(&mouse
);
367 part
= FindControl(mouse
, window
, &control
);
368 if (control
== s
->scrollbar
) {
370 case kControlIndicatorPart
:
371 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
372 term_scroll(s
->term
, +1, GetControlValue(control
));
374 case kControlUpButtonPart
:
375 case kControlDownButtonPart
:
376 case kControlPageUpPart
:
377 case kControlPageDownPart
:
378 TrackControl(control
, mouse
, &mac_scrolltracker_upp
);
382 text_click(s
, event
);
386 static void text_click(Session
*s
, EventRecord
*event
) {
389 static UInt32 lastwhen
= 0;
390 static Session
*lastsess
= NULL
;
391 static int lastrow
= -1, lastcol
= -1;
392 static Mouse_Action lastact
= MA_NOTHING
;
395 localwhere
= event
->where
;
396 GlobalToLocal(&localwhere
);
398 col
= PTOCC(localwhere
.h
);
399 row
= PTOCR(localwhere
.v
);
400 if (event
->when
- lastwhen
< GetDblTime() &&
401 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
402 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
403 lastact
== MA_2CLK ? MA_3CLK
:
404 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
407 /* Fake right button with shift key */
408 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
409 lastact
, col
, row
, event
->modifiers
& shiftKey
,
410 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
414 while (StillDown()) {
415 GetMouse(&localwhere
);
416 col
= PTOCC(localwhere
.h
);
417 row
= PTOCR(localwhere
.v
);
419 event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
420 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
421 event
->modifiers
& controlKey
,
422 event
->modifiers
& optionKey
);
423 if (row
> s
->term
->rows
- 1)
424 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
426 term_scroll(s
->term
, 0, row
);
428 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
429 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
430 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
431 lastwhen
= TickCount();
434 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
447 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
) {
450 * See "Programming with the Text Encoding Conversion Manager"
451 * Appendix E for Unicode scrap conventions.
453 * XXX Need to support TEXT/styl scrap as well.
454 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
455 * XXX Maybe PICT scrap too.
457 if (ZeroScrap() != noErr
)
459 PutScrap(len
* sizeof(*data
), 'utxt', data
);
462 void get_clip(void *frontend
, wchar_t **p
, int *lenp
) {
463 Session
*s
= frontend
;
464 static Handle h
= NULL
;
473 /* XXX Support TEXT-format scrap as well. */
474 if (GetScrap(NULL
, 'utxt', &offset
) > 0) {
476 *lenp
= GetScrap(h
, 'utxt', &offset
) / sizeof(**p
);
479 if (*p
== NULL
|| *lenp
<= 0)
480 fatalbox("Empty scrap");
487 static pascal void mac_scrolltracker(ControlHandle control
, short part
) {
490 s
= (Session
*)GetWRefCon((*control
)->contrlOwner
);
492 case kControlUpButtonPart
:
493 term_scroll(s
->term
, 0, -1);
495 case kControlDownButtonPart
:
496 term_scroll(s
->term
, 0, +1);
498 case kControlPageUpPart
:
499 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
501 case kControlPageDownPart
:
502 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
523 #define K_INSERT 0x7200
524 #define K_HOME 0x7300
525 #define K_PRIOR 0x7400
526 #define K_DELETE 0x7500
528 #define K_NEXT 0x7900
529 #define K_LEFT 0x7b00
530 #define K_RIGHT 0x7c00
531 #define K_DOWN 0x7d00
543 #define KP_CLEAR 0x4700
544 #define KP_EQUAL 0x5100
545 #define KP_SLASH 0x4b00
546 #define KP_STAR 0x4300
547 #define KP_PLUS 0x4500
548 #define KP_MINUS 0x4e00
549 #define KP_DOT 0x4100
550 #define KP_ENTER 0x4c00
552 void mac_keyterm(WindowPtr window
, EventRecord
*event
) {
553 unsigned char buf
[20];
557 s
= (Session
*)GetWRefCon(window
);
558 len
= mac_keytrans(s
, event
, buf
);
559 ldisc_send(s
->ldisc
, (char *)buf
, len
, 1);
561 term_seen_key_event(s
->term
);
563 term_update(s
->term
);
566 static int mac_keytrans(Session
*s
, EventRecord
*event
,
567 unsigned char *output
) {
568 unsigned char *p
= output
;
571 /* No meta key yet -- that'll be rather fun. */
573 /* Keys that we handle locally */
574 if (event
->modifiers
& shiftKey
) {
575 switch (event
->message
& keyCodeMask
) {
576 case K_PRIOR
: /* shift-pageup */
577 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
579 case K_NEXT
: /* shift-pagedown */
580 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
586 * Control-2 should return ^@ (0x00), Control-6 should return
587 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
588 * the DOS keyboard handling did it, and we have nothing better
589 * to do with the key combo in question, we'll also map
590 * Control-Backquote to ^\ (0x1C).
593 if (event
->modifiers
& controlKey
) {
594 switch (event
->message
& charCodeMask
) {
611 * First, all the keys that do tilde codes. (ESC '[' nn '~',
612 * for integer decimal nn.)
614 * We also deal with the weird ones here. Linux VCs replace F1
615 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
616 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
620 switch (event
->message
& keyCodeMask
) {
621 case K_F1
: code
= (event
->modifiers
& shiftKey ?
23 : 11); break;
622 case K_F2
: code
= (event
->modifiers
& shiftKey ?
24 : 12); break;
623 case K_F3
: code
= (event
->modifiers
& shiftKey ?
25 : 13); break;
624 case K_F4
: code
= (event
->modifiers
& shiftKey ?
26 : 14); break;
625 case K_F5
: code
= (event
->modifiers
& shiftKey ?
28 : 15); break;
626 case K_F6
: code
= (event
->modifiers
& shiftKey ?
29 : 17); break;
627 case K_F7
: code
= (event
->modifiers
& shiftKey ?
31 : 18); break;
628 case K_F8
: code
= (event
->modifiers
& shiftKey ?
32 : 19); break;
629 case K_F9
: code
= (event
->modifiers
& shiftKey ?
33 : 20); break;
630 case K_F10
: code
= (event
->modifiers
& shiftKey ?
34 : 21); break;
631 case K_F11
: code
= 23; break;
632 case K_F12
: code
= 24; break;
633 case K_HOME
: code
= 1; break;
634 case K_INSERT
: code
= 2; break;
635 case K_DELETE
: code
= 3; break;
636 case K_END
: code
= 4; break;
637 case K_PRIOR
: code
= 5; break;
638 case K_NEXT
: code
= 6; break;
640 if (s
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
641 p
+= sprintf((char *)p
, "\x1B[[%c", code
+ 'A' - 11);
644 if (s
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
645 p
+= sprintf((char *)p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
649 p
+= sprintf((char *)p
, "\x1B[%d~", code
);
653 if (s
->term
->app_keypad_keys
) {
654 switch (event
->message
& keyCodeMask
) {
655 case KP_ENTER
: p
+= sprintf((char *)p
, "\x1BOM"); return p
- output
;
656 case KP_CLEAR
: p
+= sprintf((char *)p
, "\x1BOP"); return p
- output
;
657 case KP_EQUAL
: p
+= sprintf((char *)p
, "\x1BOQ"); return p
- output
;
658 case KP_SLASH
: p
+= sprintf((char *)p
, "\x1BOR"); return p
- output
;
659 case KP_STAR
: p
+= sprintf((char *)p
, "\x1BOS"); return p
- output
;
660 case KP_PLUS
: p
+= sprintf((char *)p
, "\x1BOl"); return p
- output
;
661 case KP_MINUS
: p
+= sprintf((char *)p
, "\x1BOm"); return p
- output
;
662 case KP_DOT
: p
+= sprintf((char *)p
, "\x1BOn"); return p
- output
;
663 case KP_0
: p
+= sprintf((char *)p
, "\x1BOp"); return p
- output
;
664 case KP_1
: p
+= sprintf((char *)p
, "\x1BOq"); return p
- output
;
665 case KP_2
: p
+= sprintf((char *)p
, "\x1BOr"); return p
- output
;
666 case KP_3
: p
+= sprintf((char *)p
, "\x1BOs"); return p
- output
;
667 case KP_4
: p
+= sprintf((char *)p
, "\x1BOt"); return p
- output
;
668 case KP_5
: p
+= sprintf((char *)p
, "\x1BOu"); return p
- output
;
669 case KP_6
: p
+= sprintf((char *)p
, "\x1BOv"); return p
- output
;
670 case KP_7
: p
+= sprintf((char *)p
, "\x1BOw"); return p
- output
;
671 case KP_8
: p
+= sprintf((char *)p
, "\x1BOx"); return p
- output
;
672 case KP_9
: p
+= sprintf((char *)p
, "\x1BOy"); return p
- output
;
676 switch (event
->message
& keyCodeMask
) {
678 p
+= sprintf((char *)p
,
679 s
->term
->app_cursor_keys ?
"\x1BOA" : "\x1B[A");
682 p
+= sprintf((char *)p
,
683 s
->term
->app_cursor_keys ?
"\x1BOB" : "\x1B[B");
686 p
+= sprintf((char *)p
,
687 s
->term
->app_cursor_keys ?
"\x1BOC" : "\x1B[C");
690 p
+= sprintf((char *)p
,
691 s
->term
->app_cursor_keys ?
"\x1BOD" : "\x1B[D");
697 *p
++ = (s
->cfg
.bksp_is_delete ?
0x7f : 0x08);
700 *p
++ = event
->message
& charCodeMask
;
705 void request_paste(void *frontend
)
707 Session
*s
= frontend
;
710 * In the Mac OS, pasting is synchronous: we can read the
711 * clipboard with no difficulty, so request_paste() can just go
714 term_do_paste(s
->term
);
717 void mac_growterm(WindowPtr window
, EventRecord
*event
) {
720 int newrows
, newcols
;
723 s
= (Session
*)GetWRefCon(window
);
724 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
725 grow_result
= GrowWindow(window
, event
->where
, &limits
);
726 if (grow_result
!= 0) {
727 newrows
= HiWord(grow_result
) / s
->font_height
;
728 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
729 mac_adjustsize(s
, newrows
, newcols
);
730 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
734 void mac_activateterm(WindowPtr window
, Boolean active
) {
737 s
= (Session
*)GetWRefCon(window
);
738 s
->term
->has_focus
= active
;
739 term_update(s
->term
);
741 ShowControl(s
->scrollbar
);
744 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
746 BackColor(blackColor
);
747 HideControl(s
->scrollbar
);
752 void mac_updateterm(WindowPtr window
) {
755 s
= (Session
*)GetWRefCon(window
);
759 term_paint(s
->term
, s
,
760 PTOCC((*window
->visRgn
)->rgnBBox
.left
),
761 PTOCR((*window
->visRgn
)->rgnBBox
.top
),
762 PTOCC((*window
->visRgn
)->rgnBBox
.right
),
763 PTOCR((*window
->visRgn
)->rgnBBox
.bottom
), 1);
764 /* Restore default colours in case the Window Manager uses them */
765 if (HAVE_COLOR_QD()) {
766 PmForeColor(DEFAULT_FG
);
767 PmBackColor(DEFAULT_BG
);
769 ForeColor(whiteColor
);
770 BackColor(blackColor
);
772 if (FrontWindow() != window
)
773 EraseRect(&(*s
->scrollbar
)->contrlRect
);
774 UpdateControls(window
, window
->visRgn
);
780 static void mac_drawgrowicon(Session
*s
) {
784 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
785 SetRect(&clip
, s
->window
->portRect
.right
- 15, SHRT_MIN
,
788 DrawGrowIcon(s
->window
);
789 clip
.left
= SHRT_MIN
;
793 struct do_text_args
{
804 * Call from the terminal emulator to draw a bit of text
806 * x and y are text row and column (zero-based)
808 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
809 unsigned long attr
, int lattr
) {
812 struct do_text_args a
;
817 /* First check this text is relevant */
818 a
.textrect
.top
= y
* s
->font_height
;
819 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
820 a
.textrect
.left
= x
* s
->font_width
;
821 a
.textrect
.right
= (x
+ len
) * s
->font_width
;
822 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
830 if (s
->font_leading
> 0)
832 a
.textrect
.left
, a
.textrect
.bottom
- s
->font_leading
,
833 a
.textrect
.right
, a
.textrect
.bottom
);
835 SetRect(&a
.leadrect
, 0, 0, 0, 0);
837 TextFont(s
->fontnum
);
838 if (s
->cfg
.fontisbold
|| (attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
840 if (attr
& ATTR_UNDER
)
843 TextSize(s
->cfg
.fontheight
);
844 SetFractEnable(FALSE
); /* We want characters on pixel boundaries */
847 SpaceExtra(s
->font_boldadjust
<< 16);
848 CharExtra(s
->font_boldadjust
<< 16);
854 RectRgn(textrgn
, &a
.textrect
);
856 DeviceLoop(textrgn
, &do_text_for_device_upp
, (long)&a
, 0);
858 do_text_for_device(1, 0, NULL
, (long)&a
);
860 /* Tell the window manager about it in case this isn't an update */
861 ValidRect(&a
.textrect
);
864 static pascal void do_text_for_device(short depth
, short devflags
,
865 GDHandle device
, long cookie
) {
866 struct do_text_args
*a
;
867 int bgcolour
, fgcolour
, bright
;
869 a
= (struct do_text_args
*)cookie
;
871 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
873 TextMode(a
->attr
& ATTR_REVERSE ? notSrcCopy
: srcCopy
);
877 /* XXX This should be done with a _little_ more configurability */
878 ForeColor(whiteColor
);
879 BackColor(blackColor
);
880 if (a
->attr
& TATTR_ACTCURS
)
881 TextMode(a
->attr
& ATTR_REVERSE ? srcCopy
: notSrcCopy
);
884 if (a
->attr
& TATTR_ACTCURS
) {
885 PmForeColor(CURSOR_FG
);
886 PmBackColor(CURSOR_BG
);
889 PmForeColor(bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
);
890 PmBackColor(DEFAULT_BG
);
894 if (a
->attr
& TATTR_ACTCURS
) {
895 fgcolour
= CURSOR_FG
;
896 bgcolour
= CURSOR_BG
;
899 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
) * 2;
900 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
) * 2;
902 if (a
->attr
& ATTR_REVERSE
)
907 PmForeColor(fgcolour
);
908 PmBackColor(bgcolour
);
912 if (a
->attr
& ATTR_REVERSE
)
913 PaintRect(&a
->leadrect
);
915 EraseRect(&a
->leadrect
);
916 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
917 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
918 DrawText(a
->text
, 0, a
->len
);
920 if (a
->attr
& TATTR_PASCURS
) {
927 PmForeColor(CURSOR_BG
);
930 FrameRect(&a
->textrect
);
934 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
935 unsigned long attr
, int lattr
)
938 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
942 * Call from the terminal emulator to get its graphics context.
943 * Should probably be called start_redraw or something.
945 void pre_paint(Session
*s
) {
947 s
->attr_mask
= ATTR_INVALID
;
949 DeviceLoop(s
->window
->visRgn
, &mac_set_attr_mask_upp
, (long)s
, 0);
951 mac_set_attr_mask(1, 0, NULL
, (long)s
);
954 Context
get_ctx(void *frontend
) {
955 Session
*s
= frontend
;
961 void free_ctx(Context ctx
) {
965 static pascal void mac_set_attr_mask(short depth
, short devflags
,
966 GDHandle device
, long cookie
) {
968 Session
*s
= (Session
*)cookie
;
972 s
->attr_mask
|= ATTR_FGMASK
| ATTR_BGMASK
;
975 s
->attr_mask
|= ATTR_BOLD
;
978 s
->attr_mask
|= ATTR_UNDER
| ATTR_REVERSE
| TATTR_ACTCURS
|
979 TATTR_PASCURS
| ATTR_ASCII
| ATTR_GBCHR
| ATTR_LINEDRW
|
980 (s
->cfg
.bold_colour ?
0 : ATTR_BOLD
);
986 * Presumably this does something in Windows
988 void post_paint(Session
*s
) {
993 * Set the scroll bar position
995 * total is the line number of the bottom of the working screen
996 * start is the line number of the top of the display
997 * page is the length of the displayed page
999 void set_sbar(void *frontend
, int total
, int start
, int page
) {
1000 Session
*s
= frontend
;
1002 /* We don't redraw until we've set everything up, to avoid glitches */
1003 (*s
->scrollbar
)->contrlMin
= 0;
1004 (*s
->scrollbar
)->contrlMax
= total
- page
;
1005 SetControlValue(s
->scrollbar
, start
);
1007 /* XXX: This doesn't link for me. */
1008 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1009 SetControlViewSize(s
->scrollbar
, page
);
1013 void sys_cursor(void *frontend
, int x
, int y
)
1016 * I think his is meaningless under Mac OS.
1021 * This is still called when mode==BELL_VISUAL, even though the
1022 * visual bell is handled entirely within terminal.c, because we
1023 * may want to perform additional actions on any kind of bell (for
1024 * example, taskbar flashing in Windows).
1026 void beep(void *frontend
, int mode
)
1028 if (mode
!= BELL_VISUAL
)
1031 * XXX We should indicate the relevant window and/or use the
1032 * Notification Manager
1036 int char_width(Context ctx
, int uc
)
1039 * Until we support exciting character-set stuff, assume all chars are
1046 * Set icon string -- a no-op here (Windowshade?)
1048 void set_icon(void *frontend
, char *icon
) {
1049 Session
*s
= frontend
;
1054 * Set the window title
1056 void set_title(void *frontend
, char *title
) {
1057 Session
*s
= frontend
;
1060 mactitle
[0] = sprintf((char *)&mactitle
[1], "%s", title
);
1061 SetWTitle(s
->window
, mactitle
);
1065 * set or clear the "raw mouse message" mode
1067 void set_raw_mouse_mode(void *frontend
, int activate
)
1069 Session
*s
= frontend
;
1071 s
->raw_mouse
= activate
;
1072 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1076 * Resize the window at the emulator's request
1078 void request_resize(void *frontend
, int w
, int h
) {
1079 Session
*s
= frontend
;
1087 * Iconify (actually collapse) the window at the emulator's request.
1089 void set_iconic(void *frontend
, int iconic
)
1091 Session
*s
= frontend
;
1094 if (mac_gestalts
.apprvers
>= 0x0100 &&
1095 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1096 (features
& kWindowCanCollapse
))
1097 CollapseWindow(s
->window
, iconic
);
1101 * Move the window in response to a server-side request.
1103 void move_window(void *frontend
, int x
, int y
)
1105 Session
*s
= frontend
;
1107 MoveWindow(s
->window
, x
, y
, FALSE
);
1111 * Move the window to the top or bottom of the z-order in response
1112 * to a server-side request.
1114 void set_zorder(void *frontend
, int top
)
1116 Session
*s
= frontend
;
1119 * We also change the input focus to point to the topmost window,
1120 * since that's probably what the Human Interface Guidelines would
1124 SelectWindow(s
->window
);
1126 SendBehind(s
->window
, NULL
);
1130 * Refresh the window in response to a server-side request.
1132 void refresh_window(void *frontend
)
1134 Session
*s
= frontend
;
1136 term_invalidate(s
->term
);
1140 * Maximise or restore the window in response to a server-side
1143 void set_zoomed(void *frontend
, int zoomed
)
1145 Session
*s
= frontend
;
1147 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1151 * Report whether the window is iconic, for terminal reports.
1153 int is_iconic(void *frontend
)
1155 Session
*s
= frontend
;
1158 if (mac_gestalts
.apprvers
>= 0x0100 &&
1159 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1160 (features
& kWindowCanCollapse
))
1161 return IsWindowCollapsed(s
->window
);
1166 * Report the window's position, for terminal reports.
1168 void get_window_pos(void *frontend
, int *x
, int *y
)
1170 Session
*s
= frontend
;
1172 *x
= s
->window
->portRect
.left
;
1173 *y
= s
->window
->portRect
.top
;
1177 * Report the window's pixel size, for terminal reports.
1179 void get_window_pixels(void *frontend
, int *x
, int *y
)
1181 Session
*s
= frontend
;
1183 *x
= s
->window
->portRect
.right
- s
->window
->portRect
.left
;
1184 *y
= s
->window
->portRect
.bottom
- s
->window
->portRect
.top
;
1188 * Return the window or icon title.
1190 char *get_window_title(void *frontend
, int icon
)
1192 Session
*s
= frontend
;
1194 /* Erm, we don't save this at the moment */
1199 * real_palette_set(): This does the actual palette-changing work on behalf
1200 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1201 * is doing a batch of updates.
1203 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1207 if (!HAVE_COLOR_QD())
1209 col
.red
= r
* 0x0101;
1210 col
.green
= g
* 0x0101;
1211 col
.blue
= b
* 0x0101;
1212 SetEntryColor(s
->palette
, n
, &col
);
1216 * Set the logical palette. Called by the terminal emulator.
1218 void palette_set(void *frontend
, int n
, int r
, int g
, int b
) {
1219 Session
*s
= frontend
;
1220 static const int first
[21] = {
1221 0, 2, 4, 6, 8, 10, 12, 14,
1222 1, 3, 5, 7, 9, 11, 13, 15,
1226 if (!HAVE_COLOR_QD())
1228 real_palette_set(s
, first
[n
], r
, g
, b
);
1230 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1231 if (first
[n
] == DEFAULT_BG
)
1233 ActivatePalette(s
->window
);
1237 * Reset to the default palette
1239 void palette_reset(void *frontend
) {
1240 Session
*s
= frontend
;
1241 /* This maps colour indices in cfg to those used in our palette. */
1242 static const int ww
[] = {
1243 6, 7, 8, 9, 10, 11, 12, 13,
1244 14, 15, 16, 17, 18, 19, 20, 21,
1249 if (!HAVE_COLOR_QD())
1252 assert(lenof(ww
) == NCOLOURS
);
1254 for (i
= 0; i
< NCOLOURS
; i
++) {
1255 real_palette_set(s
, i
,
1256 s
->cfg
.colours
[ww
[i
]][0],
1257 s
->cfg
.colours
[ww
[i
]][1],
1258 s
->cfg
.colours
[ww
[i
]][2]);
1261 ActivatePalette(s
->window
);
1262 /* Palette Manager will generate update events as required. */
1266 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1269 void do_scroll(void *frontend
, int topline
, int botline
, int lines
) {
1270 Session
*s
= frontend
;
1274 /* FIXME: This is seriously broken on Original QuickDraw. No idea why. */
1276 if (HAVE_COLOR_QD())
1277 PmBackColor(DEFAULT_BG
);
1279 BackColor(blackColor
);
1281 SetRect(&r
, 0, topline
* s
->font_height
,
1282 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1283 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1284 /* XXX: move update region? */
1289 void logevent(void *frontend
, char *str
) {
1291 /* XXX Do something */
1294 /* Dummy routine, only required in plink. */
1295 void ldisc_update(void *frontend
, int echo
, int edit
)
1300 * Mac PuTTY doesn't support printing yet.
1302 printer_job
*printer_start_job(char *printer
)
1308 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1312 void printer_finish_job(printer_job
*pj
)
1316 void frontend_keypress(void *handle
)
1319 * Keypress termination in non-Close-On-Exit mode is not
1320 * currently supported in PuTTY proper, because the window
1321 * always has a perfectly good Close button anyway. So we do
1328 * Ask whether to wipe a session log file before writing to it.
1329 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1331 int askappend(void *frontend
, char *filename
)
1334 /* FIXME: not implemented yet. */
1341 * c-file-style: "simon"