]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Keyboard handling and a visible cursor.
[PuTTY.git] / macterm.c
1 /* $Id: macterm.c,v 1.1.2.15 1999/03/07 23:22:23 ben Exp $ */
2 /*
3  * Copyright (c) 1999 Ben Harris
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 /*
29  * macterm.c -- Macintosh terminal front-end
30  */
31
32 #include <MacTypes.h>
33 #include <Controls.h>
34 #include <Fonts.h>
35 #include <Gestalt.h>
36 #include <MacWindows.h>
37 #include <Palettes.h>
38 #include <Quickdraw.h>
39 #include <QuickdrawText.h>
40 #include <Resources.h>
41 #include <Sound.h>
42 #include <ToolUtils.h>
43
44 #include <limits.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47
48 #include "macresid.h"
49 #include "putty.h"
50 #include "mac.h"
51
52 #define DEFAULT_FG      16
53 #define DEFAULT_FG_BOLD 17
54 #define DEFAULT_BG      18
55 #define DEFAULT_BG_BOLD 19
56 #define CURSOR_FG       22
57 #define CURSOR_FG_BOLD  23
58
59 struct mac_session {
60     short               fontnum;
61     int                 font_ascent;
62     WindowPtr           window;
63     PaletteHandle       palette;
64     ControlHandle       scrollbar;
65 };
66
67 static void mac_initfont(struct mac_session *);
68 static void mac_initpalette(struct mac_session *);
69 static void mac_adjustsize(struct mac_session *, int, int);
70 static pascal void mac_scrolltracker(ControlHandle, short);
71 static pascal void do_text_for_device(short, short, GDHandle, long);
72 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
73
74 /*
75  * Temporary hack till I get the terminal emulator supporting multiple
76  * sessions
77  */
78
79 static struct mac_session *onlysession;
80
81 static void inbuf_putc(int c) {
82     inbuf[inbuf_head] = c;
83     inbuf_head = (inbuf_head+1) & INBUF_MASK;
84 }
85
86 static void inbuf_putstr(const char *c) {
87     while (*c)
88         inbuf_putc(*c++);
89 }
90
91 static void display_resource(unsigned long type, short id) {
92     Handle h;
93     int len, i;
94     char *t;
95
96     h = GetResource(type, id);
97     if (h == NULL)
98         fatalbox("Can't get test resource");
99     SetResAttrs(h, GetResAttrs(h) | resLocked);
100     t = *h;
101     len = GetResourceSizeOnDisk(h);
102     for (i = 0; i < len; i++) {
103         inbuf_putc(t[i]);
104         term_out();
105     }
106     SetResAttrs(h, GetResAttrs(h) & ~resLocked);
107     ReleaseResource(h);
108 }
109         
110
111 void mac_newsession(void) {
112     struct mac_session *s;
113     int i;
114
115     /* This should obviously be initialised by other means */
116     mac_loadconfig(&cfg);
117 /*    back = &loop_backend; */
118     s = smalloc(sizeof(*s));
119     onlysession = s;
120         
121     /* XXX: Own storage management? */
122     if (mac_gestalts.qdvers == gestaltOriginalQD)
123         s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
124     else
125         s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
126     SetWRefCon(s->window, (long)s);
127     s->scrollbar = GetNewControl(cVScroll, s->window);
128     term_init();
129     term_size(cfg.height, cfg.width, cfg.savelines);
130     mac_initfont(s);
131     mac_initpalette(s);
132     /* Set to FALSE to not get palette updates in the background. */
133     SetPalette(s->window, s->palette, TRUE); 
134     ActivatePalette(s->window);
135     ShowWindow(s->window);
136     display_resource('pTST', 128);
137 }
138
139 static void mac_initfont(struct mac_session *s) {
140     Str255 macfont;
141     FontInfo fi;
142  
143     SetPort(s->window);
144     macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
145     GetFNum(macfont, &s->fontnum);
146     TextFont(s->fontnum);
147     TextFace(cfg.fontisbold ? bold : 0);
148     TextSize(cfg.fontheight);
149     GetFontInfo(&fi);
150     font_width = fi.widMax;
151     font_height = fi.ascent + fi.descent + fi.leading;
152     s->font_ascent = fi.ascent;
153     mac_adjustsize(s, rows, cols);
154 }
155
156 /*
157  * To be called whenever the window size changes.
158  * rows and cols should be desired values.
159  * It's assumed the terminal emulator will be informed, and will set rows
160  * and cols for us.
161  */
162 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
163     int winwidth, winheight;
164
165     winwidth = newcols * font_width + 15;
166     winheight = newrows * font_height;
167     SizeWindow(s->window, winwidth, winheight, true);
168     HideControl(s->scrollbar);
169     MoveControl(s->scrollbar, winwidth - 15, -1);
170     SizeControl(s->scrollbar, 16, winheight - 13);
171     ShowControl(s->scrollbar);
172 }
173
174 static void mac_initpalette(struct mac_session *s) {
175     WinCTab ct;
176   
177     if (mac_gestalts.qdvers == gestaltOriginalQD)
178         return;
179     s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
180     if (s->palette == NULL)
181         fatalbox("Unable to create palette");
182     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
183 }
184
185 /*
186  * I don't think this is (a) safe or (b) a good way to do this.
187  */
188 static void mac_updatewinbg(struct mac_session *s) {
189     WinCTab ct;
190     WCTabPtr ctp = &ct;
191     WCTabHandle cth = &ctp;
192
193     ct.wCSeed = 0;
194     ct.wCReserved = 0;
195     ct.ctSize = 1;
196     ct.ctTable[0].value = wContentColor;
197     ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
198     SetWinColor(s->window, cth);
199 }
200
201 void mac_clickterm(WindowPtr window, EventRecord *event) {
202     struct mac_session *s;
203     Point mouse;
204     ControlHandle control;
205     int part;
206
207     s = (struct mac_session *)GetWRefCon(window);
208     SetPort(window);
209     mouse = event->where;
210     GlobalToLocal(&mouse);
211     part = FindControl(mouse, window, &control);
212     if (control == s->scrollbar) {
213         switch (part) {
214           case kControlIndicatorPart:
215             if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
216                 term_scroll(+1, GetControlValue(control));
217             break;
218           case kControlUpButtonPart:
219           case kControlDownButtonPart:
220           case kControlPageUpPart:
221           case kControlPageDownPart:
222             TrackControl(control, mouse, mac_scrolltracker);
223             break;
224         }
225     }
226 }
227
228 static pascal void mac_scrolltracker(ControlHandle control, short part) {
229     struct mac_session *s;
230
231     s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
232     switch (part) {
233       case kControlUpButtonPart:
234         term_scroll(0, -1);
235         break;
236       case kControlDownButtonPart:
237         term_scroll(0, +1);
238         break;
239       case kControlPageUpPart:
240         term_scroll(0, -(rows - 1));
241         break;
242       case kControlPageDownPart:
243         term_scroll(0, +(rows - 1));
244         break;
245     }
246 }
247
248 #define K_SPACE 0x3100
249 #define K_BS    0x3300
250 #define K_F1    0x7a00
251 #define K_F2    0x7800
252 #define K_F3    0x6300
253 #define K_F4    0x7600
254 #define K_F5    0x6000
255 #define K_F6    0x6100
256 #define K_F7    0x6200
257 #define K_F8    0x6400
258 #define K_F9    0x6500
259 #define K_F10   0x6d00
260 #define K_F11   0x6700
261 #define K_F12   0x6f00
262 #define K_INSERT 0x7200
263 #define K_HOME  0x7300
264 #define K_PRIOR 0x7400
265 #define K_DELETE 0x7500
266 #define K_END   0x7700
267 #define K_NEXT  0x7900
268 #define K_LEFT  0x7b00
269 #define K_RIGHT 0x7c00
270 #define K_DOWN  0x7d00
271 #define K_UP    0x7e00
272 #define KP_0    0x5200
273 #define KP_1    0x5300
274 #define KP_2    0x5400
275 #define KP_3    0x5500
276 #define KP_4    0x5600
277 #define KP_5    0x5700
278 #define KP_6    0x5800
279 #define KP_7    0x5900
280 #define KP_8    0x5b00
281 #define KP_9    0x5c00
282 #define KP_CLEAR 0x4700
283 #define KP_EQUAL 0x5100
284 #define KP_SLASH 0x4b00
285 #define KP_STAR 0x4300
286 #define KP_PLUS 0x4500
287 #define KP_MINUS 0x4e00
288 #define KP_DOT  0x4100
289 #define KP_ENTER 0x4c00
290
291 void mac_keyterm(WindowPtr window, EventRecord *event) {
292     unsigned char buf[20];
293     int len;
294     struct mac_session *s;
295     int i;
296
297     s = (struct mac_session *)GetWRefCon(window);
298     len = mac_keytrans(s, event, buf);
299     /* XXX: I can't get the loopback backend to link, so we'll do this: */
300 /*    back->send((char *)buf, len); */
301     for (i = 0; i < len; i++)
302         inbuf_putc(buf[i]);
303     term_out();
304     term_update();
305 }
306
307 static int mac_keytrans(struct mac_session *s, EventRecord *event,
308                         unsigned char *output) {
309     unsigned char *p = output;
310     int code;
311
312     /* No meta key yet -- that'll be rather fun. */
313
314     /* Keys that we handle locally */
315     if (event->modifiers & shiftKey) {
316         switch (event->message & keyCodeMask) {
317           case K_PRIOR: /* shift-pageup */
318             term_scroll(0, -(rows - 1));
319             return 0;
320           case K_NEXT:  /* shift-pagedown */
321             term_scroll(0, +(rows - 1));
322             return 0;
323         }
324     }
325
326     /*
327      * Control-2 should return ^@ (0x00), Control-6 should return
328      * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
329      * the DOS keyboard handling did it, and we have nothing better
330      * to do with the key combo in question, we'll also map
331      * Control-Backquote to ^\ (0x1C).
332      */
333
334     if (event->modifiers & controlKey) {
335         switch (event->message & charCodeMask) {
336           case ' ': case '2':
337             *p++ = 0x00;
338             return p - output;
339           case '`':
340             *p++ = 0x1c;
341             return p - output;
342           case '6':
343             *p++ = 0x1e;
344             return p - output;
345           case '/':
346             *p++ = 0x1f;
347             return p - output;
348         }
349     }
350
351     /*
352      * First, all the keys that do tilde codes. (ESC '[' nn '~',
353      * for integer decimal nn.)
354      *
355      * We also deal with the weird ones here. Linux VCs replace F1
356      * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
357      * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
358      * respectively.
359      */
360     code = 0;
361     switch (event->message & keyCodeMask) {
362       case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
363       case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
364       case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
365       case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
366       case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
367       case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
368       case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
369       case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
370       case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
371       case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
372       case K_F11: code = 23; break;
373       case K_F12: code = 24; break;
374       case K_HOME: code = 1; break;
375       case K_INSERT: code = 2; break;
376       case K_DELETE: code = 3; break;
377       case K_END: code = 4; break;
378       case K_PRIOR: code = 5; break;
379       case K_NEXT: code = 6; break;
380     }
381     if (cfg.linux_funkeys && code >= 11 && code <= 15) {
382         p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
383         return p - output;
384     }
385     if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
386         p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
387         return p - output;
388     }
389     if (code) {
390         p += sprintf((char *)p, "\x1B[%d~", code);
391         return p - output;
392     }
393
394     if (app_keypad_keys) {
395         switch (event->message & keyCodeMask) {
396           case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
397           case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
398           case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
399           case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
400           case KP_STAR:  p += sprintf((char *)p, "\x1BOS"); return p - output;
401           case KP_PLUS:  p += sprintf((char *)p, "\x1BOl"); return p - output;
402           case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
403           case KP_DOT:   p += sprintf((char *)p, "\x1BOn"); return p - output;
404           case KP_0:     p += sprintf((char *)p, "\x1BOp"); return p - output;
405           case KP_1:     p += sprintf((char *)p, "\x1BOq"); return p - output;
406           case KP_2:     p += sprintf((char *)p, "\x1BOr"); return p - output;
407           case KP_3:     p += sprintf((char *)p, "\x1BOs"); return p - output;
408           case KP_4:     p += sprintf((char *)p, "\x1BOt"); return p - output;
409           case KP_5:     p += sprintf((char *)p, "\x1BOu"); return p - output;
410           case KP_6:     p += sprintf((char *)p, "\x1BOv"); return p - output;
411           case KP_7:     p += sprintf((char *)p, "\x1BOw"); return p - output;
412           case KP_8:     p += sprintf((char *)p, "\x1BOx"); return p - output;
413           case KP_9:     p += sprintf((char *)p, "\x1BOy"); return p - output;
414         }
415     }
416
417     switch (event->message & keyCodeMask) {
418       case K_UP:
419         p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
420         return p - output;
421       case K_DOWN:
422         p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
423         return p - output;
424       case K_RIGHT:
425         p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
426         return p - output;
427       case K_LEFT:
428         p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
429         return p - output;
430       case K_BS:
431         *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
432         return p - output;
433       default:
434         *p++ = event->message & charCodeMask;
435         return p - output;
436     }
437 }
438
439 void mac_growterm(WindowPtr window, EventRecord *event) {
440     Rect limits;
441     long grow_result;
442     int newrows, newcols;
443     struct mac_session *s;
444
445     s = (struct mac_session *)GetWRefCon(window);
446     SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
447     grow_result = GrowWindow(window, event->where, &limits);
448     if (grow_result != 0) {
449         newrows = HiWord(grow_result) / font_height;
450         newcols = (LoWord(grow_result) - 15) / font_width;
451         mac_adjustsize(s, newrows, newcols);
452         term_size(newrows, newcols, cfg.savelines);
453     }
454 }
455
456 void mac_activateterm(WindowPtr window, Boolean active) {
457     struct mac_session *s;
458
459     s = (struct mac_session *)GetWRefCon(window);
460     has_focus = active;
461     term_update();
462     if (active)
463         ShowControl(s->scrollbar);
464     else
465         HideControl(s->scrollbar);
466 }
467
468 void mac_updateterm(WindowPtr window) {
469     struct mac_session *s;
470     Rect clip;
471
472     s = (struct mac_session *)GetWRefCon(window);
473     BeginUpdate(window);
474     term_paint(s,
475                (*window->visRgn)->rgnBBox.left,
476                (*window->visRgn)->rgnBBox.top,
477                (*window->visRgn)->rgnBBox.right,
478                (*window->visRgn)->rgnBBox.bottom);
479     /* Restore default colours in case the Window Manager uses them */
480     PmForeColor(16);
481     PmBackColor(18);
482     if (FrontWindow() != window)
483         EraseRect(&(*s->scrollbar)->contrlRect);
484     UpdateControls(window, window->visRgn);
485     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
486     SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
487     ClipRect(&clip);
488     DrawGrowIcon(window);
489     clip.left = SHRT_MIN;
490     ClipRect(&clip);
491     EndUpdate(window);
492 }
493
494 struct do_text_args {
495     struct mac_session *s;
496     Rect textrect;
497     char *text;
498     int len;
499     unsigned long attr;
500 };
501
502 /*
503  * Call from the terminal emulator to draw a bit of text
504  *
505  * x and y are text row and column (zero-based)
506  */
507 void do_text(struct mac_session *s, int x, int y, char *text, int len,
508              unsigned long attr) {
509     int style = 0;
510     int bgcolour, fgcolour;
511     RGBColor rgbfore, rgbback;
512     struct do_text_args a;
513     RgnHandle textrgn;
514
515     SetPort(s->window);
516     
517     /* First check this text is relevant */
518     a.textrect.top = y * font_height;
519     a.textrect.bottom = (y + 1) * font_height;
520     a.textrect.left = x * font_width;
521     a.textrect.right = (x + len) * font_width;
522     if (!RectInRgn(&a.textrect, s->window->visRgn))
523         return;
524
525     a.s = s;
526     a.text = text;
527     a.len = len;
528     a.attr = attr;
529     SetPort(s->window);
530     TextFont(s->fontnum);
531     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
532         style |= bold;
533     if (attr & ATTR_UNDER)
534         style |= underline;
535     TextFace(style);
536     TextSize(cfg.fontheight);
537     if (attr & ATTR_REVERSE)
538         TextMode(notSrcCopy);
539     else
540         TextMode(srcCopy);
541     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
542     textrgn = NewRgn();
543     RectRgn(textrgn, &a.textrect);
544     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
545     /* Tell the window manager about it in case this isn't an update */
546     DisposeRgn(textrgn);
547     ValidRect(&a.textrect);
548 }
549
550 static pascal void do_text_for_device(short depth, short devflags,
551                                       GDHandle device, long cookie) {
552     struct do_text_args *a;
553     int bgcolour, fgcolour;
554
555     a = (struct do_text_args *)cookie;
556
557     switch (depth) {
558       case 1:
559         /* XXX This should be done with a _little_ more configurability */
560         ForeColor(whiteColor);
561         BackColor(blackColor);
562         break;
563       case 2:
564         if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
565             PmForeColor(DEFAULT_FG_BOLD);
566         else
567             PmForeColor(DEFAULT_FG);
568         if (a->attr & ATTR_ACTCURS)
569             PmBackColor(CURSOR_FG);
570         else
571             PmBackColor(DEFAULT_BG);
572         break;
573       default:
574         fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
575         bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
576         if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
577             if (a->attr & ATTR_REVERSE)
578                 bgcolour++;
579             else
580                 fgcolour++;
581         if (a->attr & ATTR_ACTCURS)
582             bgcolour = CURSOR_FG;
583         PmForeColor(fgcolour);
584         PmBackColor(bgcolour);
585         break;
586     }
587     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
588     DrawText(a->text, 0, a->len);
589 }
590
591 /*
592  * Call from the terminal emulator to get its graphics context.
593  */
594 struct mac_session *get_ctx(void) {
595
596     return onlysession;
597 }
598
599 /*
600  * Presumably this does something in Windows
601  */
602 void free_ctx(struct mac_session *ctx) {
603
604 }
605
606 /*
607  * Set the scroll bar position
608  *
609  * total is the line number of the bottom of the working screen
610  * start is the line number of the top of the display
611  * page is the length of the displayed page
612  */
613 void set_sbar(int total, int start, int page) {
614     struct mac_session *s = onlysession;
615
616     /* We don't redraw until we've set everything up, to avoid glitches */
617     (*s->scrollbar)->contrlMin = 0;
618     (*s->scrollbar)->contrlMax = total - page;
619     SetControlValue(s->scrollbar, start);
620 #if 0
621     /* XXX: This doesn't link for me. */
622     if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
623         SetControlViewSize(s->scrollbar, page);
624 #endif
625 }
626
627 /*
628  * Beep
629  */
630 void beep(void) {
631
632     SysBeep(30);
633     /*
634      * XXX We should indicate the relevant window and/or use the
635      * Notification Manager
636      */
637 }
638
639 /*
640  * Set icon string -- a no-op here (Windowshade?)
641  */
642 void set_icon(char *icon) {
643
644 }
645
646 /*
647  * Set the window title
648  */
649 void set_title(char *title) {
650     Str255 mactitle;
651     struct mac_session *s = onlysession;
652
653     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
654     SetWTitle(s->window, mactitle);
655 }
656
657 /*
658  * Resize the window at the emulator's request
659  */
660 void request_resize(int w, int h) {
661
662     cols = w;
663     rows = h;
664     mac_initfont(onlysession);
665 }
666
667 /*
668  * Set the logical palette
669  */
670 void palette_set(int n, int r, int g, int b) {
671     RGBColor col;
672     struct mac_session *s = onlysession;
673     static const int first[21] = {
674         0, 2, 4, 6, 8, 10, 12, 14,
675         1, 3, 5, 7, 9, 11, 13, 15,
676         16, 17, 18, 20, 22
677     };
678     
679     if (mac_gestalts.qdvers == gestaltOriginalQD)
680       return;
681     col.red   = r * 0x0101;
682     col.green = g * 0x0101;
683     col.blue  = b * 0x0101;
684     SetEntryColor(s->palette, first[n], &col);
685     if (first[n] >= 18)
686         SetEntryColor(s->palette, first[n]+1, &col);
687     ActivatePalette(s->window);
688 }
689
690 /*
691  * Reset to the default palette
692  */
693 void palette_reset(void) {
694     struct mac_session *s = onlysession;
695
696     if (mac_gestalts.qdvers == gestaltOriginalQD)
697         return;
698     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
699     ActivatePalette(s->window);
700     /* Palette Manager will generate update events as required. */
701 }
702
703 /*
704  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
705  * for backward.)
706  */
707 void do_scroll(int topline, int botline, int lines) {
708     struct mac_session *s = onlysession;
709     Rect r;
710     RgnHandle update;
711
712     SetPort(s->window);
713     PmBackColor(DEFAULT_BG);
714     update = NewRgn();
715     SetRect(&r, 0, topline * font_height,
716             cols * font_width, (botline + 1) * font_height);
717     ScrollRect(&r, 0, - lines * font_height, update);
718     /* XXX: move update region? */
719     InvalRgn(update);
720     DisposeRgn(update);
721 }