Improve support for non-colour displays by adding a mask of attributes to
[u/mdw/putty] / mac / macterm.c
CommitLineData
2647b103 1/* $Id: macterm.c,v 1.16 2002/11/29 00:32:03 ben Exp $ */
d082ac49 2/*
3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
5 * All rights reserved.
6 *
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
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
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
26 * SOFTWARE.
27 */
28
29/*
30 * macterm.c -- Macintosh terminal front-end
31 */
32
33#include <MacTypes.h>
34#include <Controls.h>
35#include <ControlDefinitions.h>
36#include <Fonts.h>
37#include <Gestalt.h>
38#include <MacMemory.h>
39#include <MacWindows.h>
40#include <MixedMode.h>
41#include <Palettes.h>
42#include <Quickdraw.h>
43#include <QuickdrawText.h>
44#include <Resources.h>
45#include <Scrap.h>
46#include <Script.h>
47#include <Sound.h>
48#include <Threads.h>
49#include <ToolUtils.h>
50
51#include <assert.h>
52#include <limits.h>
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
56
57#include "macresid.h"
58#include "putty.h"
59#include "mac.h"
60#include "terminal.h"
61
62#define NCOLOURS (lenof(((Config *)0)->colours))
63
64#define DEFAULT_FG 16
65#define DEFAULT_FG_BOLD 17
66#define DEFAULT_BG 18
67#define DEFAULT_BG_BOLD 19
68#define CURSOR_FG 20
e0cbe032 69#define CURSOR_BG 21
d082ac49 70
71#define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
72 (x) / s->font_width)
73#define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
74 (y) / s->font_height)
75
76static void mac_initfont(Session *);
77static void mac_initpalette(Session *);
78static void mac_adjustwinbg(Session *);
79static void mac_adjustsize(Session *, int, int);
80static void mac_drawgrowicon(Session *s);
81static pascal void mac_scrolltracker(ControlHandle, short);
82static pascal void do_text_for_device(short, short, GDHandle, long);
83static pascal void mac_set_attr_mask(short, short, GDHandle, long);
84static int mac_keytrans(Session *, EventRecord *, unsigned char *);
85static void text_click(Session *, EventRecord *);
86
87void pre_paint(Session *s);
88void post_paint(Session *s);
89
90#if TARGET_RT_MAC_CFM
91static RoutineDescriptor mac_scrolltracker_upp =
92 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo,
93 (ProcPtr)mac_scrolltracker);
94static RoutineDescriptor do_text_for_device_upp =
95 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo,
96 (ProcPtr)do_text_for_device);
d082ac49 97#else /* not TARGET_RT_MAC_CFM */
98#define mac_scrolltracker_upp mac_scrolltracker
99#define do_text_for_device_upp do_text_for_device
d082ac49 100#endif /* not TARGET_RT_MAC_CFM */
101
102static void inbuf_putc(Session *s, int c) {
103 char ch = c;
104
105 from_backend(s->term, 0, &ch, 1);
106}
107
108static void inbuf_putstr(Session *s, const char *c) {
109
110 from_backend(s->term, 0, (char *)c, strlen(c));
111}
112
113static void display_resource(Session *s, unsigned long type, short id) {
114 Handle h;
115 int len, i;
116 char *t;
117
118 h = GetResource(type, id);
119 if (h == NULL)
120 fatalbox("Can't get test resource");
121 len = GetResourceSizeOnDisk(h);
122 DetachResource(h);
123 HNoPurge(h);
124 HLock(h);
125 t = *h;
126 from_backend(s->term, 0, t, len);
127 term_out(s->term);
128 DisposeHandle(h);
129}
130
131
132void mac_newsession(void) {
133 Session *s;
134 UInt32 starttime;
135 char msg[128];
136 OSErr err;
137
138 /* This should obviously be initialised by other means */
139 s = smalloc(sizeof(*s));
140 memset(s, 0, sizeof(*s));
141 do_defaults(NULL, &s->cfg);
142 s->back = &loop_backend;
143
144 /* XXX: Own storage management? */
145 if (HAVE_COLOR_QD())
146 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
147 else
148 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
149 SetWRefCon(s->window, (long)s);
150 s->scrollbar = GetNewControl(cVScroll, s->window);
f6b14226 151 s->term = term_init(&s->cfg, s);
d082ac49 152
153 s->logctx = log_init(s);
154 term_provide_logctx(s->term, s->logctx);
155
156 s->back->init(s->term, &s->backhandle, "localhost", 23, &s->realhost, 0);
157 s->back->provide_logctx(s->backhandle, s->logctx);
158
159 term_provide_resize_fn(s->term, s->back->size, s->backhandle);
160
161 mac_adjustsize(s, s->cfg.height, s->cfg.width);
162 term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines);
163
fe5634f6 164 s->ldisc = ldisc_create(&s->cfg, s->term, s->back, s->backhandle, s);
d082ac49 165 ldisc_send(s->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
166
167 mac_initfont(s);
168 mac_initpalette(s);
d082ac49 169 if (HAVE_COLOR_QD()) {
170 /* Set to FALSE to not get palette updates in the background. */
171 SetPalette(s->window, s->palette, TRUE);
172 ActivatePalette(s->window);
173 }
174 ShowWindow(s->window);
175 starttime = TickCount();
176 display_resource(s, 'pTST', 128);
177 sprintf(msg, "Elapsed ticks: %d\015\012", TickCount() - starttime);
178 inbuf_putstr(s, msg);
179 term_out(s->term);
180}
181
182static void mac_initfont(Session *s) {
183 Str255 macfont;
184 FontInfo fi;
185
186 SetPort(s->window);
187 macfont[0] = sprintf((char *)&macfont[1], "%s", s->cfg.font);
188 GetFNum(macfont, &s->fontnum);
189 TextFont(s->fontnum);
190 TextFace(s->cfg.fontisbold ? bold : 0);
191 TextSize(s->cfg.fontheight);
192 GetFontInfo(&fi);
193 s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
194 s->font_ascent = fi.ascent;
195 s->font_leading = fi.leading;
196 s->font_height = s->font_ascent + fi.descent + s->font_leading;
197 if (!s->cfg.bold_colour) {
198 TextFace(bold);
199 s->font_boldadjust = s->font_width - CharWidth('W');
200 } else
201 s->font_boldadjust = 0;
202 mac_adjustsize(s, s->term->rows, s->term->cols);
203}
204
205/*
206 * To be called whenever the window size changes.
207 * rows and cols should be desired values.
208 * It's assumed the terminal emulator will be informed, and will set rows
209 * and cols for us.
210 */
211static void mac_adjustsize(Session *s, int newrows, int newcols) {
212 int winwidth, winheight;
213
214 winwidth = newcols * s->font_width + 15;
215 winheight = newrows * s->font_height;
216 SizeWindow(s->window, winwidth, winheight, true);
217 HideControl(s->scrollbar);
218 MoveControl(s->scrollbar, winwidth - 15, -1);
219 SizeControl(s->scrollbar, 16, winheight - 13);
220 ShowControl(s->scrollbar);
221}
222
223static void mac_initpalette(Session *s) {
224 int i;
225
1eb3f8f0 226 if (!HAVE_COLOR_QD())
227 return;
d082ac49 228 /*
229 * Most colours should be inhibited on 2bpp displays.
230 * Palette manager documentation suggests inhibiting all tolerant colours
231 * on greyscale displays.
232 */
8a00b869 233#define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
234 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
d082ac49 235#define PM_TOLERANCE 0x2000
236 s->palette = NewPalette(22, NULL, PM_NORMAL, PM_TOLERANCE);
237 if (s->palette == NULL)
238 fatalbox("Unable to create palette");
239 /* In 2bpp, these are the colours we want most. */
240 SetEntryUsage(s->palette, DEFAULT_BG,
241 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
242 SetEntryUsage(s->palette, DEFAULT_FG,
243 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
244 SetEntryUsage(s->palette, DEFAULT_FG_BOLD,
245 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
8a00b869 246 SetEntryUsage(s->palette, CURSOR_BG,
d082ac49 247 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
248 palette_reset(s);
249}
250
251/*
252 * Set the background colour of the window correctly. Should be
253 * called whenever the default background changes.
254 */
255static void mac_adjustwinbg(Session *s) {
256
257 if (!HAVE_COLOR_QD())
258 return;
259#if TARGET_RT_CFM /* XXX doesn't link (at least for 68k) */
260 if (mac_gestalts.windattr & gestaltWindowMgrPresent)
261 SetWindowContentColor(s->window,
262 &(*s->palette)->pmInfo[DEFAULT_BG].ciRGB);
263 else
264#endif
265 {
266 if (s->wctab == NULL)
267 s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
268 if (s->wctab == NULL)
269 return; /* do without */
270 (*s->wctab)->wCSeed = 0;
271 (*s->wctab)->wCReserved = 0;
272 (*s->wctab)->ctSize = 0;
273 (*s->wctab)->ctTable[0].value = wContentColor;
274 (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
275 SetWinColor(s->window, s->wctab);
276 }
277}
278
279/*
280 * Set the cursor shape correctly
281 */
282void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
283 Session *s;
284 ControlHandle control;
285 short part;
286 int x, y;
287
288 SetPort(window);
289 s = (Session *)GetWRefCon(window);
290 GlobalToLocal(&mouse);
291 part = FindControl(mouse, window, &control);
292 if (control == s->scrollbar) {
293 SetCursor(&qd.arrow);
294 RectRgn(cursrgn, &(*s->scrollbar)->contrlRect);
295 SectRgn(cursrgn, window->visRgn, cursrgn);
296 } else {
297 x = mouse.h / s->font_width;
298 y = mouse.v / s->font_height;
299 if (s->raw_mouse)
300 SetCursor(&qd.arrow);
301 else
302 SetCursor(*GetCursor(iBeamCursor));
303 /* Ask for shape changes if we leave this character cell. */
304 SetRectRgn(cursrgn, x * s->font_width, y * s->font_height,
305 (x + 1) * s->font_width, (y + 1) * s->font_height);
306 SectRgn(cursrgn, window->visRgn, cursrgn);
307 }
308}
309
310/*
311 * Enable/disable menu items based on the active terminal window.
312 */
313void mac_adjusttermmenus(WindowPtr window) {
314 Session *s;
315 MenuHandle menu;
316 long offset;
317
318 s = (Session *)GetWRefCon(window);
319 menu = GetMenuHandle(mEdit);
320 EnableItem(menu, 0);
321 DisableItem(menu, iUndo);
322 DisableItem(menu, iCut);
323 if (1/*s->term->selstate == SELECTED*/)
324 EnableItem(menu, iCopy);
325 else
326 DisableItem(menu, iCopy);
327 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
328 DisableItem(menu, iPaste);
329 else
330 EnableItem(menu, iPaste);
331 DisableItem(menu, iClear);
332 EnableItem(menu, iSelectAll);
333}
334
335void mac_menuterm(WindowPtr window, short menu, short item) {
336 Session *s;
337
338 s = (Session *)GetWRefCon(window);
339 switch (menu) {
340 case mEdit:
341 switch (item) {
342 case iCopy:
343 /* term_copy(s); */
344 break;
345 case iPaste:
346 term_do_paste(s->term);
347 break;
348 }
349 }
350}
351
352void mac_clickterm(WindowPtr window, EventRecord *event) {
353 Session *s;
354 Point mouse;
355 ControlHandle control;
356 int part;
357
358 s = (Session *)GetWRefCon(window);
359 SetPort(window);
360 mouse = event->where;
361 GlobalToLocal(&mouse);
362 part = FindControl(mouse, window, &control);
363 if (control == s->scrollbar) {
364 switch (part) {
365 case kControlIndicatorPart:
366 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
367 term_scroll(s->term, +1, GetControlValue(control));
368 break;
369 case kControlUpButtonPart:
370 case kControlDownButtonPart:
371 case kControlPageUpPart:
372 case kControlPageDownPart:
373 TrackControl(control, mouse, &mac_scrolltracker_upp);
374 break;
375 }
376 } else {
377 text_click(s, event);
378 }
379}
380
381static void text_click(Session *s, EventRecord *event) {
382 Point localwhere;
383 int row, col;
384 static UInt32 lastwhen = 0;
385 static Session *lastsess = NULL;
386 static int lastrow = -1, lastcol = -1;
387 static Mouse_Action lastact = MA_NOTHING;
388
389 SetPort(s->window);
390 localwhere = event->where;
391 GlobalToLocal(&localwhere);
392
393 col = PTOCC(localwhere.h);
394 row = PTOCR(localwhere.v);
395 if (event->when - lastwhen < GetDblTime() &&
396 row == lastrow && col == lastcol && s == lastsess)
397 lastact = (lastact == MA_CLICK ? MA_2CLK :
398 lastact == MA_2CLK ? MA_3CLK :
399 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
400 else
401 lastact = MA_CLICK;
402 /* Fake right button with shift key */
403 term_mouse(s->term, event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
404 lastact, col, row, event->modifiers & shiftKey,
405 event->modifiers & controlKey, event->modifiers & optionKey);
406 lastsess = s;
407 lastrow = row;
408 lastcol = col;
409 while (StillDown()) {
410 GetMouse(&localwhere);
411 col = PTOCC(localwhere.h);
412 row = PTOCR(localwhere.v);
413 term_mouse(s->term,
414 event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
415 MA_DRAG, col, row, event->modifiers & shiftKey,
416 event->modifiers & controlKey,
417 event->modifiers & optionKey);
418 if (row > s->term->rows - 1)
419 term_scroll(s->term, 0, row - (s->term->rows - 1));
420 else if (row < 0)
421 term_scroll(s->term, 0, row);
422 }
423 term_mouse(s->term, event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
424 MA_RELEASE, col, row, event->modifiers & shiftKey,
425 event->modifiers & controlKey, event->modifiers & optionKey);
426 lastwhen = TickCount();
427}
428
429Mouse_Button translate_button(void *frontend, Mouse_Button button)
430{
431
432 switch (button) {
433 case MBT_LEFT:
434 return MBT_SELECT;
435 case MBT_RIGHT:
436 return MBT_EXTEND;
437 default:
438 return 0;
439 }
440}
441
442void write_clip(void *cookie, wchar_t *data, int len, int must_deselect) {
443
444 /*
445 * See "Programming with the Text Encoding Conversion Manager"
446 * Appendix E for Unicode scrap conventions.
447 *
448 * XXX Need to support TEXT/styl scrap as well.
449 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
450 * XXX Maybe PICT scrap too.
451 */
452 if (ZeroScrap() != noErr)
453 return;
454 PutScrap(len * sizeof(*data), 'utxt', data);
455}
456
457void get_clip(void *frontend, wchar_t **p, int *lenp) {
458 Session *s = frontend;
459 static Handle h = NULL;
460 long offset;
461
462 if (p == NULL) {
463 /* release memory */
464 if (h != NULL)
465 DisposeHandle(h);
466 h = NULL;
467 } else
468 /* XXX Support TEXT-format scrap as well. */
469 if (GetScrap(NULL, 'utxt', &offset) > 0) {
470 h = NewHandle(0);
471 *lenp = GetScrap(h, 'utxt', &offset) / sizeof(**p);
472 HLock(h);
473 *p = (wchar_t *)*h;
474 if (*p == NULL || *lenp <= 0)
475 fatalbox("Empty scrap");
476 } else {
477 *p = NULL;
478 *lenp = 0;
479 }
480}
481
482static pascal void mac_scrolltracker(ControlHandle control, short part) {
483 Session *s;
484
485 s = (Session *)GetWRefCon((*control)->contrlOwner);
486 switch (part) {
487 case kControlUpButtonPart:
488 term_scroll(s->term, 0, -1);
489 break;
490 case kControlDownButtonPart:
491 term_scroll(s->term, 0, +1);
492 break;
493 case kControlPageUpPart:
494 term_scroll(s->term, 0, -(s->term->rows - 1));
495 break;
496 case kControlPageDownPart:
497 term_scroll(s->term, 0, +(s->term->rows - 1));
498 break;
499 }
500}
501
502#define K_BS 0x3300
503#define K_F1 0x7a00
504#define K_F2 0x7800
505#define K_F3 0x6300
506#define K_F4 0x7600
507#define K_F5 0x6000
508#define K_F6 0x6100
509#define K_F7 0x6200
510#define K_F8 0x6400
511#define K_F9 0x6500
512#define K_F10 0x6d00
513#define K_F11 0x6700
514#define K_F12 0x6f00
515#define K_F13 0x6900
516#define K_F14 0x6b00
517#define K_F15 0x7100
518#define K_INSERT 0x7200
519#define K_HOME 0x7300
520#define K_PRIOR 0x7400
521#define K_DELETE 0x7500
522#define K_END 0x7700
523#define K_NEXT 0x7900
524#define K_LEFT 0x7b00
525#define K_RIGHT 0x7c00
526#define K_DOWN 0x7d00
527#define K_UP 0x7e00
528#define KP_0 0x5200
529#define KP_1 0x5300
530#define KP_2 0x5400
531#define KP_3 0x5500
532#define KP_4 0x5600
533#define KP_5 0x5700
534#define KP_6 0x5800
535#define KP_7 0x5900
536#define KP_8 0x5b00
537#define KP_9 0x5c00
538#define KP_CLEAR 0x4700
539#define KP_EQUAL 0x5100
540#define KP_SLASH 0x4b00
541#define KP_STAR 0x4300
542#define KP_PLUS 0x4500
543#define KP_MINUS 0x4e00
544#define KP_DOT 0x4100
545#define KP_ENTER 0x4c00
546
547void mac_keyterm(WindowPtr window, EventRecord *event) {
548 unsigned char buf[20];
549 int len;
550 Session *s;
551
552 s = (Session *)GetWRefCon(window);
553 len = mac_keytrans(s, event, buf);
10135a55 554 ldisc_send(s->ldisc, (char *)buf, len, 1);
555 ObscureCursor();
556 term_seen_key_event(s->term);
557 term_out(s->term);
558 term_update(s->term);
d082ac49 559}
560
561static int mac_keytrans(Session *s, EventRecord *event,
562 unsigned char *output) {
563 unsigned char *p = output;
564 int code;
565
566 /* No meta key yet -- that'll be rather fun. */
567
568 /* Keys that we handle locally */
569 if (event->modifiers & shiftKey) {
570 switch (event->message & keyCodeMask) {
571 case K_PRIOR: /* shift-pageup */
572 term_scroll(s->term, 0, -(s->term->rows - 1));
573 return 0;
574 case K_NEXT: /* shift-pagedown */
575 term_scroll(s->term, 0, +(s->term->rows - 1));
576 return 0;
577 }
578 }
579
580 /*
581 * Control-2 should return ^@ (0x00), Control-6 should return
582 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
583 * the DOS keyboard handling did it, and we have nothing better
584 * to do with the key combo in question, we'll also map
585 * Control-Backquote to ^\ (0x1C).
586 */
587
588 if (event->modifiers & controlKey) {
589 switch (event->message & charCodeMask) {
590 case ' ': case '2':
591 *p++ = 0x00;
592 return p - output;
593 case '`':
594 *p++ = 0x1c;
595 return p - output;
596 case '6':
597 *p++ = 0x1e;
598 return p - output;
599 case '/':
600 *p++ = 0x1f;
601 return p - output;
602 }
603 }
604
605 /*
606 * First, all the keys that do tilde codes. (ESC '[' nn '~',
607 * for integer decimal nn.)
608 *
609 * We also deal with the weird ones here. Linux VCs replace F1
610 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
611 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
612 * respectively.
613 */
614 code = 0;
615 switch (event->message & keyCodeMask) {
616 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
617 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
618 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
619 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
620 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
621 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
622 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
623 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
624 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
625 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
626 case K_F11: code = 23; break;
627 case K_F12: code = 24; break;
628 case K_HOME: code = 1; break;
629 case K_INSERT: code = 2; break;
630 case K_DELETE: code = 3; break;
631 case K_END: code = 4; break;
632 case K_PRIOR: code = 5; break;
633 case K_NEXT: code = 6; break;
634 }
635 if (s->cfg.funky_type == 1 && code >= 11 && code <= 15) {
636 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
637 return p - output;
638 }
639 if (s->cfg.rxvt_homeend && (code == 1 || code == 4)) {
640 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
641 return p - output;
642 }
643 if (code) {
644 p += sprintf((char *)p, "\x1B[%d~", code);
645 return p - output;
646 }
647
648 if (s->term->app_keypad_keys) {
649 switch (event->message & keyCodeMask) {
650 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
651 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
652 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
653 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
654 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
655 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
656 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
657 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
658 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
659 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
660 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
661 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
662 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
663 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
664 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
665 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
666 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
667 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
668 }
669 }
670
671 switch (event->message & keyCodeMask) {
672 case K_UP:
673 p += sprintf((char *)p,
674 s->term->app_cursor_keys ? "\x1BOA" : "\x1B[A");
675 return p - output;
676 case K_DOWN:
677 p += sprintf((char *)p,
678 s->term->app_cursor_keys ? "\x1BOB" : "\x1B[B");
679 return p - output;
680 case K_RIGHT:
681 p += sprintf((char *)p,
682 s->term->app_cursor_keys ? "\x1BOC" : "\x1B[C");
683 return p - output;
684 case K_LEFT:
685 p += sprintf((char *)p,
686 s->term->app_cursor_keys ? "\x1BOD" : "\x1B[D");
687 return p - output;
688 case KP_ENTER:
689 *p++ = 0x0d;
690 return p - output;
691 case K_BS:
692 *p++ = (s->cfg.bksp_is_delete ? 0x7f : 0x08);
693 return p - output;
694 default:
695 *p++ = event->message & charCodeMask;
696 return p - output;
697 }
698}
699
700void request_paste(void *frontend)
701{
702 Session *s = frontend;
703
704 /*
705 * In the Mac OS, pasting is synchronous: we can read the
706 * clipboard with no difficulty, so request_paste() can just go
707 * ahead and paste.
708 */
709 term_do_paste(s->term);
710}
711
712void mac_growterm(WindowPtr window, EventRecord *event) {
713 Rect limits;
714 long grow_result;
715 int newrows, newcols;
716 Session *s;
717
718 s = (Session *)GetWRefCon(window);
719 SetRect(&limits, s->font_width + 15, s->font_height, SHRT_MAX, SHRT_MAX);
720 grow_result = GrowWindow(window, event->where, &limits);
721 if (grow_result != 0) {
722 newrows = HiWord(grow_result) / s->font_height;
723 newcols = (LoWord(grow_result) - 15) / s->font_width;
724 mac_adjustsize(s, newrows, newcols);
725 term_size(s->term, newrows, newcols, s->cfg.savelines);
726 }
727}
728
729void mac_activateterm(WindowPtr window, Boolean active) {
730 Session *s;
731
732 s = (Session *)GetWRefCon(window);
7d22c2e8 733 s->term->has_focus = active;
d082ac49 734 term_update(s->term);
735 if (active)
736 ShowControl(s->scrollbar);
737 else {
738 if (HAVE_COLOR_QD())
739 PmBackColor(DEFAULT_BG);/* HideControl clears behind the control */
740 else
741 BackColor(blackColor);
742 HideControl(s->scrollbar);
743 }
744 mac_drawgrowicon(s);
745}
746
747void mac_updateterm(WindowPtr window) {
748 Session *s;
749
750 s = (Session *)GetWRefCon(window);
751 SetPort(window);
752 BeginUpdate(window);
753 pre_paint(s);
754 term_paint(s->term, s,
e84d62e1 755 PTOCC((*window->visRgn)->rgnBBox.left),
756 PTOCR((*window->visRgn)->rgnBBox.top),
757 PTOCC((*window->visRgn)->rgnBBox.right),
758 PTOCR((*window->visRgn)->rgnBBox.bottom), 1);
d082ac49 759 /* Restore default colours in case the Window Manager uses them */
760 if (HAVE_COLOR_QD()) {
761 PmForeColor(DEFAULT_FG);
762 PmBackColor(DEFAULT_BG);
763 } else {
764 ForeColor(whiteColor);
765 BackColor(blackColor);
766 }
767 if (FrontWindow() != window)
768 EraseRect(&(*s->scrollbar)->contrlRect);
769 UpdateControls(window, window->visRgn);
770 mac_drawgrowicon(s);
771 post_paint(s);
772 EndUpdate(window);
773}
774
775static void mac_drawgrowicon(Session *s) {
776 Rect clip;
ea4c3d8a 777 RgnHandle savergn;
d082ac49 778
779 SetPort(s->window);
ea4c3d8a 780 /*
781 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
782 * See Tech Note TB575 for details.
783 */
784 clip = s->window->portRect;
785 clip.left = clip.right - 15;
786 savergn = NewRgn();
787 GetClip(savergn);
d082ac49 788 ClipRect(&clip);
789 DrawGrowIcon(s->window);
ea4c3d8a 790 SetClip(savergn);
791 DisposeRgn(savergn);
d082ac49 792}
793
794struct do_text_args {
795 Session *s;
796 Rect textrect;
d082ac49 797 char *text;
798 int len;
799 unsigned long attr;
800 int lattr;
fd7d8b47 801 Point numer, denom;
d082ac49 802};
803
804/*
805 * Call from the terminal emulator to draw a bit of text
806 *
807 * x and y are text row and column (zero-based)
808 */
809void do_text(Context ctx, int x, int y, char *text, int len,
810 unsigned long attr, int lattr) {
811 Session *s = ctx;
812 int style = 0;
813 struct do_text_args a;
814 RgnHandle textrgn;
d082ac49 815
816 SetPort(s->window);
817
d082ac49 818 /* First check this text is relevant */
819 a.textrect.top = y * s->font_height;
820 a.textrect.bottom = (y + 1) * s->font_height;
821 a.textrect.left = x * s->font_width;
822 a.textrect.right = (x + len) * s->font_width;
823 if (!RectInRgn(&a.textrect, s->window->visRgn))
824 return;
825
826 a.s = s;
827 a.text = text;
828 a.len = len;
829 a.attr = attr;
830 a.lattr = lattr;
fd7d8b47 831 a.numer.h = a.numer.v = a.denom.h = a.denom.v = 1;
d082ac49 832 SetPort(s->window);
833 TextFont(s->fontnum);
834 if (s->cfg.fontisbold || (attr & ATTR_BOLD) && !s->cfg.bold_colour)
835 style |= bold;
836 if (attr & ATTR_UNDER)
837 style |= underline;
838 TextFace(style);
839 TextSize(s->cfg.fontheight);
4d1565a4 840 TextMode(srcOr);
d082ac49 841 if (HAVE_COLOR_QD())
842 if (style & bold) {
843 SpaceExtra(s->font_boldadjust << 16);
844 CharExtra(s->font_boldadjust << 16);
845 } else {
846 SpaceExtra(0);
847 CharExtra(0);
848 }
849 textrgn = NewRgn();
850 RectRgn(textrgn, &a.textrect);
851 if (HAVE_COLOR_QD())
852 DeviceLoop(textrgn, &do_text_for_device_upp, (long)&a, 0);
853 else
854 do_text_for_device(1, 0, NULL, (long)&a);
855 DisposeRgn(textrgn);
856 /* Tell the window manager about it in case this isn't an update */
857 ValidRect(&a.textrect);
858}
859
860static pascal void do_text_for_device(short depth, short devflags,
861 GDHandle device, long cookie) {
862 struct do_text_args *a;
4d1565a4 863 int bgcolour, fgcolour, bright, reverse, tmp;
d082ac49 864
865 a = (struct do_text_args *)cookie;
866
867 bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
4d1565a4 868 reverse = a->attr & ATTR_REVERSE;
d082ac49 869
4d1565a4 870 if (depth == 1 && (a->attr & TATTR_ACTCURS))
871 reverse = !reverse;
d082ac49 872
4d1565a4 873 if (HAVE_COLOR_QD()) {
874 if (depth > 2) {
875 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
876 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
d082ac49 877 } else {
4d1565a4 878 /*
879 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
880 * boldens the background, because that's all we can do.
881 */
882 fgcolour = bright ? DEFAULT_FG_BOLD : DEFAULT_FG;
883 bgcolour = DEFAULT_BG;
d082ac49 884 }
4d1565a4 885 if (reverse) {
886 tmp = fgcolour;
887 fgcolour = bgcolour;
888 bgcolour = tmp;
889 }
890 if (bright && depth > 2)
891 fgcolour++;
892 if ((a->attr & TATTR_ACTCURS) && depth > 1) {
e0cbe032 893 fgcolour = CURSOR_FG;
d082ac49 894 bgcolour = CURSOR_BG;
d082ac49 895 }
896 PmForeColor(fgcolour);
897 PmBackColor(bgcolour);
4d1565a4 898 } else { /* No Color Quickdraw */
899 /* XXX This should be done with a _little_ more configurability */
900 if (reverse) {
901 ForeColor(blackColor);
902 BackColor(whiteColor);
903 } else {
904 ForeColor(whiteColor);
905 BackColor(blackColor);
906 }
d082ac49 907 }
908
4d1565a4 909 EraseRect(&a->textrect);
d082ac49 910 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
911 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
fd7d8b47 912 if (a->s->window->grafProcs != NULL)
913 InvokeQDTextUPP(a->len, a->text, a->numer, a->denom,
914 a->s->window->grafProcs->textProc);
915 else
916 StdText(a->len, a->text, a->numer, a->denom);
d082ac49 917
918 if (a->attr & TATTR_PASCURS) {
919 PenNormal();
920 switch (depth) {
921 case 1:
922 PenMode(patXor);
923 break;
924 default:
925 PmForeColor(CURSOR_BG);
926 break;
927 }
928 FrameRect(&a->textrect);
929 }
930}
931
932void do_cursor(Context ctx, int x, int y, char *text, int len,
933 unsigned long attr, int lattr)
934{
935
e0cbe032 936 do_text(ctx, x, y, text, len, attr, lattr);
d082ac49 937}
938
939/*
940 * Call from the terminal emulator to get its graphics context.
941 * Should probably be called start_redraw or something.
942 */
943void pre_paint(Session *s) {
2647b103 944 GDHandle gdh;
945 Rect myrect, tmprect;
d082ac49 946
2647b103 947 s->term->attr_mask = 0xffffffff;
948 if (HAVE_COLOR_QD()) {
949 SetPort(s->window);
950 myrect = (*s->window->visRgn)->rgnBBox;
951 LocalToGlobal((Point *)&myrect.top);
952 LocalToGlobal((Point *)&myrect.bottom);
953 for (gdh = GetDeviceList();
954 gdh != NULL;
955 gdh = GetNextDevice(gdh)) {
956 if (TestDeviceAttribute(gdh, screenDevice) &&
957 TestDeviceAttribute(gdh, screenActive) &&
958 SectRect(&(*gdh)->gdRect, &myrect, &tmprect)) {
959 switch ((*(*gdh)->gdPMap)->pixelSize) {
960 case 1:
961 if (s->cfg.bold_colour)
962 s->term->attr_mask &= ~ATTR_BOLD;
963 /* FALLTHROUGH */
964 case 2:
965 s->term->attr_mask &= ~ATTR_COLOURS;
966 }
967 }
968 }
969 } else
970 s->term->attr_mask &= ~(ATTR_COLOURS |
971 (s->cfg.bold_colour ? ATTR_BOLD : 0));
d082ac49 972}
973
974Context get_ctx(void *frontend) {
975 Session *s = frontend;
976
977 pre_paint(s);
978 return s;
979}
980
981void free_ctx(Context ctx) {
982
983}
984
d082ac49 985/*
986 * Presumably this does something in Windows
987 */
988void post_paint(Session *s) {
989
990}
991
992/*
993 * Set the scroll bar position
994 *
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
998 */
999void set_sbar(void *frontend, int total, int start, int page) {
1000 Session *s = frontend;
1001
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);
1006#if TARGET_RT_CFM
1007 /* XXX: This doesn't link for me. */
1008 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
1009 SetControlViewSize(s->scrollbar, page);
1010#endif
1011}
1012
1013void sys_cursor(void *frontend, int x, int y)
1014{
1015 /*
1016 * I think his is meaningless under Mac OS.
1017 */
1018}
1019
1020/*
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).
1025 */
1026void beep(void *frontend, int mode)
1027{
1028 if (mode != BELL_VISUAL)
1029 SysBeep(30);
1030 /*
1031 * XXX We should indicate the relevant window and/or use the
1032 * Notification Manager
1033 */
1034}
1035
1036int char_width(Context ctx, int uc)
1037{
1038 /*
1039 * Until we support exciting character-set stuff, assume all chars are
1040 * single-width.
1041 */
1042 return 1;
1043}
1044
1045/*
1046 * Set icon string -- a no-op here (Windowshade?)
1047 */
1048void set_icon(void *frontend, char *icon) {
1049 Session *s = frontend;
1050
1051}
1052
1053/*
1054 * Set the window title
1055 */
1056void set_title(void *frontend, char *title) {
1057 Session *s = frontend;
1058 Str255 mactitle;
1059
1060 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
1061 SetWTitle(s->window, mactitle);
1062}
1063
1064/*
1065 * set or clear the "raw mouse message" mode
1066 */
1067void set_raw_mouse_mode(void *frontend, int activate)
1068{
1069 Session *s = frontend;
1070
1071 s->raw_mouse = activate;
1072 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1073}
1074
1075/*
1076 * Resize the window at the emulator's request
1077 */
1078void request_resize(void *frontend, int w, int h) {
1079 Session *s = frontend;
1080
1081 s->term->cols = w;
1082 s->term->rows = h;
1083 mac_initfont(s);
1084}
1085
1086/*
1087 * Iconify (actually collapse) the window at the emulator's request.
1088 */
1089void set_iconic(void *frontend, int iconic)
1090{
1091 Session *s = frontend;
1092 UInt32 features;
1093
1094 if (mac_gestalts.apprvers >= 0x0100 &&
1095 GetWindowFeatures(s->window, &features) == noErr &&
1096 (features & kWindowCanCollapse))
1097 CollapseWindow(s->window, iconic);
1098}
1099
1100/*
1101 * Move the window in response to a server-side request.
1102 */
1103void move_window(void *frontend, int x, int y)
1104{
1105 Session *s = frontend;
1106
1107 MoveWindow(s->window, x, y, FALSE);
1108}
1109
1110/*
1111 * Move the window to the top or bottom of the z-order in response
1112 * to a server-side request.
1113 */
1114void set_zorder(void *frontend, int top)
1115{
1116 Session *s = frontend;
1117
1118 /*
1119 * We also change the input focus to point to the topmost window,
1120 * since that's probably what the Human Interface Guidelines would
1121 * like us to do.
1122 */
1123 if (top)
1124 SelectWindow(s->window);
1125 else
1126 SendBehind(s->window, NULL);
1127}
1128
1129/*
1130 * Refresh the window in response to a server-side request.
1131 */
1132void refresh_window(void *frontend)
1133{
1134 Session *s = frontend;
1135
1136 term_invalidate(s->term);
1137}
1138
1139/*
1140 * Maximise or restore the window in response to a server-side
1141 * request.
1142 */
1143void set_zoomed(void *frontend, int zoomed)
1144{
1145 Session *s = frontend;
1146
1147 ZoomWindow(s->window, zoomed ? inZoomOut : inZoomIn, FALSE);
1148}
1149
1150/*
1151 * Report whether the window is iconic, for terminal reports.
1152 */
1153int is_iconic(void *frontend)
1154{
1155 Session *s = frontend;
1156 UInt32 features;
1157
1158 if (mac_gestalts.apprvers >= 0x0100 &&
1159 GetWindowFeatures(s->window, &features) == noErr &&
1160 (features & kWindowCanCollapse))
1161 return IsWindowCollapsed(s->window);
1162 return FALSE;
1163}
1164
1165/*
1166 * Report the window's position, for terminal reports.
1167 */
1168void get_window_pos(void *frontend, int *x, int *y)
1169{
1170 Session *s = frontend;
1171
1172 *x = s->window->portRect.left;
1173 *y = s->window->portRect.top;
1174}
1175
1176/*
1177 * Report the window's pixel size, for terminal reports.
1178 */
1179void get_window_pixels(void *frontend, int *x, int *y)
1180{
1181 Session *s = frontend;
1182
1183 *x = s->window->portRect.right - s->window->portRect.left;
1184 *y = s->window->portRect.bottom - s->window->portRect.top;
1185}
1186
1187/*
1188 * Return the window or icon title.
1189 */
1190char *get_window_title(void *frontend, int icon)
1191{
1192 Session *s = frontend;
1193
1194 /* Erm, we don't save this at the moment */
1195 return "";
1196}
1197
1198/*
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.
1202 */
1203static void real_palette_set(Session *s, int n, int r, int g, int b)
1204{
1205 RGBColor col;
1206
1207 if (!HAVE_COLOR_QD())
1208 return;
1209 col.red = r * 0x0101;
1210 col.green = g * 0x0101;
1211 col.blue = b * 0x0101;
d082ac49 1212 SetEntryColor(s->palette, n, &col);
1213}
1214
1215/*
1216 * Set the logical palette. Called by the terminal emulator.
1217 */
1218void 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,
e0cbe032 1223 16, 17, 18, 20, 21
d082ac49 1224 };
1225
1226 if (!HAVE_COLOR_QD())
1227 return;
1228 real_palette_set(s, first[n], r, g, b);
e0cbe032 1229 if (first[n] == 18)
d082ac49 1230 real_palette_set(s, first[n]+1, r, g, b);
1231 if (first[n] == DEFAULT_BG)
1232 mac_adjustwinbg(s);
1233 ActivatePalette(s->window);
1234}
1235
1236/*
1237 * Reset to the default palette
1238 */
1239void 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,
1245 0, 1, 2, 3, 4, 5
1246 };
1247 int i;
1248
1249 if (!HAVE_COLOR_QD())
1250 return;
1251
1252 assert(lenof(ww) == NCOLOURS);
1253
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]);
1259 }
1260 mac_adjustwinbg(s);
1261 ActivatePalette(s->window);
1262 /* Palette Manager will generate update events as required. */
1263}
1264
1265/*
1266 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1267 * for backward.)
1268 */
1269void do_scroll(void *frontend, int topline, int botline, int lines) {
1270 Session *s = frontend;
1271 Rect r;
1272 RgnHandle update;
1273
d082ac49 1274 SetPort(s->window);
1951eebc 1275 PenNormal();
d082ac49 1276 if (HAVE_COLOR_QD())
1277 PmBackColor(DEFAULT_BG);
fd7d8b47 1278 else
1279 BackColor(blackColor); /* XXX make configurable */
d082ac49 1280 update = NewRgn();
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? */
1285 InvalRgn(update);
1286 DisposeRgn(update);
1287}
1288
1289void logevent(void *frontend, char *str) {
1290
1291 /* XXX Do something */
1292}
1293
1294/* Dummy routine, only required in plink. */
1295void ldisc_update(void *frontend, int echo, int edit)
1296{
1297}
1298
1299/*
1300 * Mac PuTTY doesn't support printing yet.
1301 */
1302printer_job *printer_start_job(char *printer)
1303{
1304
1305 return NULL;
1306}
1307
1308void printer_job_data(printer_job *pj, void *data, int len)
1309{
1310}
1311
1312void printer_finish_job(printer_job *pj)
1313{
1314}
1315
1316void frontend_keypress(void *handle)
1317{
1318 /*
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
1322 * nothing here.
1323 */
1324 return;
1325}
1326
1327/*
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).
1330 */
1331int askappend(void *frontend, char *filename)
1332{
1333
1334 /* FIXME: not implemented yet. */
1335 return 2;
1336}
1337
1338/*
1339 * Emacs magic:
1340 * Local Variables:
1341 * c-file-style: "simon"
1342 * End:
1343 */
1344