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