]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Colour icons, allegedly.
[PuTTY.git] / macterm.c
1 /* $Id: macterm.c,v 1.1.2.23 1999/03/14 20:43:51 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 <Scrap.h>
42 #include <Sound.h>
43 #include <ToolUtils.h>
44
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 #include "macresid.h"
50 #include "putty.h"
51 #include "mac.h"
52
53 #define DEFAULT_FG      16
54 #define DEFAULT_FG_BOLD 17
55 #define DEFAULT_BG      18
56 #define DEFAULT_BG_BOLD 19
57 #define CURSOR_FG       20
58 #define CURSOR_FG_BOLD  21
59 #define CURSOR_BG       22
60 #define CURSOR_BG_BOLD  23
61
62 #define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
63                             (x) / font_width)
64 #define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
65                             (y) / font_height)
66
67 struct mac_session {
68     short               fontnum;
69     int                 font_ascent;
70     WindowPtr           window;
71     PaletteHandle       palette;
72     ControlHandle       scrollbar;
73 };
74
75 static void mac_initfont(struct mac_session *);
76 static void mac_initpalette(struct mac_session *);
77 static void mac_adjustsize(struct mac_session *, int, int);
78 static pascal void mac_scrolltracker(ControlHandle, short);
79 static pascal void do_text_for_device(short, short, GDHandle, long);
80 static pascal void mac_set_attr_mask(short, short, GDHandle, long);
81 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
82 static void text_click(struct mac_session *, EventRecord *);
83
84 /*
85  * Temporary hack till I get the terminal emulator supporting multiple
86  * sessions
87  */
88
89 static struct mac_session *onlysession;
90
91 static void inbuf_putc(int c) {
92     inbuf[inbuf_head] = c;
93     inbuf_head = (inbuf_head+1) & INBUF_MASK;
94 }
95
96 static void inbuf_putstr(const char *c) {
97     while (*c)
98         inbuf_putc(*c++);
99 }
100
101 static void display_resource(unsigned long type, short id) {
102     Handle h;
103     int len, i;
104     char *t;
105
106     h = GetResource(type, id);
107     if (h == NULL)
108         fatalbox("Can't get test resource");
109     SetResAttrs(h, GetResAttrs(h) | resLocked);
110     t = *h;
111     len = GetResourceSizeOnDisk(h);
112     for (i = 0; i < len; i++) {
113         inbuf_putc(t[i]);
114         term_out();
115     }
116     SetResAttrs(h, GetResAttrs(h) & ~resLocked);
117     ReleaseResource(h);
118 }
119         
120
121 void mac_newsession(void) {
122     struct mac_session *s;
123     int i;
124     UInt32 starttime;
125     char msg[128];
126
127     /* This should obviously be initialised by other means */
128     mac_loadconfig(&cfg);
129 /*    back = &loop_backend; */
130     s = smalloc(sizeof(*s));
131     onlysession = s;
132         
133     /* XXX: Own storage management? */
134     if (mac_gestalts.qdvers == gestaltOriginalQD)
135         s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
136     else
137         s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
138     SetWRefCon(s->window, (long)s);
139     s->scrollbar = GetNewControl(cVScroll, s->window);
140     term_init();
141     term_size(cfg.height, cfg.width, cfg.savelines);
142     mac_initfont(s);
143     mac_initpalette(s);
144     attr_mask = ATTR_MASK;
145     /* Set to FALSE to not get palette updates in the background. */
146     SetPalette(s->window, s->palette, TRUE); 
147     ActivatePalette(s->window);
148     ShowWindow(s->window);
149     starttime = TickCount();
150     display_resource('pTST', 128);
151     sprintf(msg, "Elapsed ticks: %d\015\012", TickCount() - starttime);
152     inbuf_putstr(msg);
153     term_out();
154 }
155
156 static void mac_initfont(struct mac_session *s) {
157     Str255 macfont;
158     FontInfo fi;
159  
160     SetPort(s->window);
161     macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
162     GetFNum(macfont, &s->fontnum);
163     TextFont(s->fontnum);
164     TextFace(cfg.fontisbold ? bold : 0);
165     TextSize(cfg.fontheight);
166     GetFontInfo(&fi);
167     font_width = fi.widMax;
168     font_height = fi.ascent + fi.descent + fi.leading;
169     s->font_ascent = fi.ascent;
170     mac_adjustsize(s, rows, cols);
171 }
172
173 /*
174  * To be called whenever the window size changes.
175  * rows and cols should be desired values.
176  * It's assumed the terminal emulator will be informed, and will set rows
177  * and cols for us.
178  */
179 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
180     int winwidth, winheight;
181
182     winwidth = newcols * font_width + 15;
183     winheight = newrows * font_height;
184     SizeWindow(s->window, winwidth, winheight, true);
185     HideControl(s->scrollbar);
186     MoveControl(s->scrollbar, winwidth - 15, -1);
187     SizeControl(s->scrollbar, 16, winheight - 13);
188     ShowControl(s->scrollbar);
189 }
190
191 static void mac_initpalette(struct mac_session *s) {
192     WinCTab ct;
193   
194     if (mac_gestalts.qdvers == gestaltOriginalQD)
195         return;
196     s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
197     if (s->palette == NULL)
198         fatalbox("Unable to create palette");
199     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
200 }
201
202 /*
203  * I don't think this is (a) safe or (b) a good way to do this.
204  */
205 static void mac_updatewinbg(struct mac_session *s) {
206     WinCTab ct;
207     WCTabPtr ctp = &ct;
208     WCTabHandle cth = &ctp;
209
210     ct.wCSeed = 0;
211     ct.wCReserved = 0;
212     ct.ctSize = 1;
213     ct.ctTable[0].value = wContentColor;
214     ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
215     SetWinColor(s->window, cth);
216 }
217
218 /*
219  * Enable/disable menu items based on the active terminal window.
220  */
221 void mac_adjusttermmenus(WindowPtr window) {
222     struct mac_session *s;
223     MenuHandle menu;
224     long offset;
225
226     s = (struct mac_session *)GetWRefCon(window);
227     menu = GetMenuHandle(mEdit);
228     EnableItem(menu, 0);
229     DisableItem(menu, iUndo);
230     DisableItem(menu, iCut);
231     DisableItem(menu, iCopy);
232     if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
233         DisableItem(menu, iPaste);
234     else
235         EnableItem(menu, iPaste);
236     DisableItem(menu, iClear);
237     EnableItem(menu, iSelectAll);
238 }
239
240 void mac_clickterm(WindowPtr window, EventRecord *event) {
241     struct mac_session *s;
242     Point mouse;
243     ControlHandle control;
244     int part;
245
246     s = (struct mac_session *)GetWRefCon(window);
247     SetPort(window);
248     mouse = event->where;
249     GlobalToLocal(&mouse);
250     part = FindControl(mouse, window, &control);
251     if (control == s->scrollbar) {
252         switch (part) {
253           case kControlIndicatorPart:
254             if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
255                 term_scroll(+1, GetControlValue(control));
256             break;
257           case kControlUpButtonPart:
258           case kControlDownButtonPart:
259           case kControlPageUpPart:
260           case kControlPageDownPart:
261             TrackControl(control, mouse, mac_scrolltracker);
262             break;
263         }
264     } else {
265         text_click(s, event);
266     }
267 }
268
269 static void text_click(struct mac_session *s, EventRecord *event) {
270     Point localwhere;
271     int row, col;
272     static UInt32 lastwhen = 0;
273     static struct mac_session *lastsess = NULL;
274     static int lastrow = -1, lastcol = -1;
275     static Mouse_Action lastact = MA_NOTHING;
276
277     SetPort(s->window);
278     localwhere = event->where;
279     GlobalToLocal(&localwhere);
280
281     col = PTOCC(localwhere.h);
282     row = PTOCR(localwhere.v);
283     if (event->when - lastwhen < GetDblTime() &&
284         row == lastrow && col == lastcol && s == lastsess)
285         lastact = (lastact == MA_CLICK ? MA_2CLK :
286                    lastact == MA_2CLK ? MA_3CLK :
287                    lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
288     else
289         lastact = MA_CLICK;
290     term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
291                col, row);
292     lastsess = s;
293     lastrow = row;
294     lastcol = col;
295     while (StillDown()) {
296         GetMouse(&localwhere);
297         col = PTOCC(localwhere.h);
298         row = PTOCR(localwhere.v);
299         term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
300                    MA_DRAG, col, row);
301         if (row > rows - 1)
302             term_scroll(0, row - (rows - 1));
303         else if (row < 0)
304             term_scroll(0, row);
305     }
306     term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
307                col, row);
308     lastwhen = TickCount();
309 }
310
311 void write_clip(void *data, int len) {
312     
313     if (ZeroScrap() != noErr)
314         return;
315     PutScrap(len, 'TEXT', data);
316 }
317
318 void get_clip(void **p, int *lenp) {
319
320     /* XXX: do something */
321 }
322
323 static pascal void mac_scrolltracker(ControlHandle control, short part) {
324     struct mac_session *s;
325
326     s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
327     switch (part) {
328       case kControlUpButtonPart:
329         term_scroll(0, -1);
330         break;
331       case kControlDownButtonPart:
332         term_scroll(0, +1);
333         break;
334       case kControlPageUpPart:
335         term_scroll(0, -(rows - 1));
336         break;
337       case kControlPageDownPart:
338         term_scroll(0, +(rows - 1));
339         break;
340     }
341 }
342
343 #define K_SPACE 0x3100
344 #define K_BS    0x3300
345 #define K_F1    0x7a00
346 #define K_F2    0x7800
347 #define K_F3    0x6300
348 #define K_F4    0x7600
349 #define K_F5    0x6000
350 #define K_F6    0x6100
351 #define K_F7    0x6200
352 #define K_F8    0x6400
353 #define K_F9    0x6500
354 #define K_F10   0x6d00
355 #define K_F11   0x6700
356 #define K_F12   0x6f00
357 #define K_INSERT 0x7200
358 #define K_HOME  0x7300
359 #define K_PRIOR 0x7400
360 #define K_DELETE 0x7500
361 #define K_END   0x7700
362 #define K_NEXT  0x7900
363 #define K_LEFT  0x7b00
364 #define K_RIGHT 0x7c00
365 #define K_DOWN  0x7d00
366 #define K_UP    0x7e00
367 #define KP_0    0x5200
368 #define KP_1    0x5300
369 #define KP_2    0x5400
370 #define KP_3    0x5500
371 #define KP_4    0x5600
372 #define KP_5    0x5700
373 #define KP_6    0x5800
374 #define KP_7    0x5900
375 #define KP_8    0x5b00
376 #define KP_9    0x5c00
377 #define KP_CLEAR 0x4700
378 #define KP_EQUAL 0x5100
379 #define KP_SLASH 0x4b00
380 #define KP_STAR 0x4300
381 #define KP_PLUS 0x4500
382 #define KP_MINUS 0x4e00
383 #define KP_DOT  0x4100
384 #define KP_ENTER 0x4c00
385
386 void mac_keyterm(WindowPtr window, EventRecord *event) {
387     unsigned char buf[20];
388     int len;
389     struct mac_session *s;
390     int i;
391
392     s = (struct mac_session *)GetWRefCon(window);
393     len = mac_keytrans(s, event, buf);
394     /* XXX: I can't get the loopback backend to link, so we'll do this: */
395 /*    back->send((char *)buf, len); */
396     for (i = 0; i < len; i++)
397         inbuf_putc(buf[i]);
398     term_out();
399     term_update();
400 }
401
402 static int mac_keytrans(struct mac_session *s, EventRecord *event,
403                         unsigned char *output) {
404     unsigned char *p = output;
405     int code;
406
407     /* No meta key yet -- that'll be rather fun. */
408
409     /* Keys that we handle locally */
410     if (event->modifiers & shiftKey) {
411         switch (event->message & keyCodeMask) {
412           case K_PRIOR: /* shift-pageup */
413             term_scroll(0, -(rows - 1));
414             return 0;
415           case K_NEXT:  /* shift-pagedown */
416             term_scroll(0, +(rows - 1));
417             return 0;
418         }
419     }
420
421     /*
422      * Control-2 should return ^@ (0x00), Control-6 should return
423      * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
424      * the DOS keyboard handling did it, and we have nothing better
425      * to do with the key combo in question, we'll also map
426      * Control-Backquote to ^\ (0x1C).
427      */
428
429     if (event->modifiers & controlKey) {
430         switch (event->message & charCodeMask) {
431           case ' ': case '2':
432             *p++ = 0x00;
433             return p - output;
434           case '`':
435             *p++ = 0x1c;
436             return p - output;
437           case '6':
438             *p++ = 0x1e;
439             return p - output;
440           case '/':
441             *p++ = 0x1f;
442             return p - output;
443         }
444     }
445
446     /*
447      * First, all the keys that do tilde codes. (ESC '[' nn '~',
448      * for integer decimal nn.)
449      *
450      * We also deal with the weird ones here. Linux VCs replace F1
451      * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
452      * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
453      * respectively.
454      */
455     code = 0;
456     switch (event->message & keyCodeMask) {
457       case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
458       case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
459       case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
460       case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
461       case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
462       case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
463       case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
464       case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
465       case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
466       case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
467       case K_F11: code = 23; break;
468       case K_F12: code = 24; break;
469       case K_HOME: code = 1; break;
470       case K_INSERT: code = 2; break;
471       case K_DELETE: code = 3; break;
472       case K_END: code = 4; break;
473       case K_PRIOR: code = 5; break;
474       case K_NEXT: code = 6; break;
475     }
476     if (cfg.linux_funkeys && code >= 11 && code <= 15) {
477         p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
478         return p - output;
479     }
480     if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
481         p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
482         return p - output;
483     }
484     if (code) {
485         p += sprintf((char *)p, "\x1B[%d~", code);
486         return p - output;
487     }
488
489     if (app_keypad_keys) {
490         switch (event->message & keyCodeMask) {
491           case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
492           case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
493           case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
494           case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
495           case KP_STAR:  p += sprintf((char *)p, "\x1BOS"); return p - output;
496           case KP_PLUS:  p += sprintf((char *)p, "\x1BOl"); return p - output;
497           case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
498           case KP_DOT:   p += sprintf((char *)p, "\x1BOn"); return p - output;
499           case KP_0:     p += sprintf((char *)p, "\x1BOp"); return p - output;
500           case KP_1:     p += sprintf((char *)p, "\x1BOq"); return p - output;
501           case KP_2:     p += sprintf((char *)p, "\x1BOr"); return p - output;
502           case KP_3:     p += sprintf((char *)p, "\x1BOs"); return p - output;
503           case KP_4:     p += sprintf((char *)p, "\x1BOt"); return p - output;
504           case KP_5:     p += sprintf((char *)p, "\x1BOu"); return p - output;
505           case KP_6:     p += sprintf((char *)p, "\x1BOv"); return p - output;
506           case KP_7:     p += sprintf((char *)p, "\x1BOw"); return p - output;
507           case KP_8:     p += sprintf((char *)p, "\x1BOx"); return p - output;
508           case KP_9:     p += sprintf((char *)p, "\x1BOy"); return p - output;
509         }
510     }
511
512     switch (event->message & keyCodeMask) {
513       case K_UP:
514         p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
515         return p - output;
516       case K_DOWN:
517         p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
518         return p - output;
519       case K_RIGHT:
520         p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
521         return p - output;
522       case K_LEFT:
523         p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
524         return p - output;
525       case K_BS:
526         *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
527         return p - output;
528       default:
529         *p++ = event->message & charCodeMask;
530         return p - output;
531     }
532 }
533
534 void mac_growterm(WindowPtr window, EventRecord *event) {
535     Rect limits;
536     long grow_result;
537     int newrows, newcols;
538     struct mac_session *s;
539
540     s = (struct mac_session *)GetWRefCon(window);
541     SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
542     grow_result = GrowWindow(window, event->where, &limits);
543     if (grow_result != 0) {
544         newrows = HiWord(grow_result) / font_height;
545         newcols = (LoWord(grow_result) - 15) / font_width;
546         mac_adjustsize(s, newrows, newcols);
547         term_size(newrows, newcols, cfg.savelines);
548     }
549 }
550
551 void mac_activateterm(WindowPtr window, Boolean active) {
552     struct mac_session *s;
553
554     s = (struct mac_session *)GetWRefCon(window);
555     has_focus = active;
556     term_update();
557     if (active)
558         ShowControl(s->scrollbar);
559     else {
560         PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
561         HideControl(s->scrollbar);
562     }
563 }
564
565 void mac_updateterm(WindowPtr window) {
566     struct mac_session *s;
567     Rect clip;
568
569     s = (struct mac_session *)GetWRefCon(window);
570     BeginUpdate(window);
571     get_ctx();
572     term_paint(s,
573                (*window->visRgn)->rgnBBox.left,
574                (*window->visRgn)->rgnBBox.top,
575                (*window->visRgn)->rgnBBox.right,
576                (*window->visRgn)->rgnBBox.bottom);
577     /* Restore default colours in case the Window Manager uses them */
578     PmForeColor(16);
579     PmBackColor(18);
580     if (FrontWindow() != window)
581         EraseRect(&(*s->scrollbar)->contrlRect);
582     UpdateControls(window, window->visRgn);
583     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
584     SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
585     ClipRect(&clip);
586     DrawGrowIcon(window);
587     clip.left = SHRT_MIN;
588     ClipRect(&clip);
589     free_ctx(NULL);
590     EndUpdate(window);
591 }
592
593 struct do_text_args {
594     struct mac_session *s;
595     Rect textrect;
596     char *text;
597     int len;
598     unsigned long attr;
599 };
600
601 /*
602  * Call from the terminal emulator to draw a bit of text
603  *
604  * x and y are text row and column (zero-based)
605  */
606 void do_text(struct mac_session *s, int x, int y, char *text, int len,
607              unsigned long attr) {
608     int style = 0;
609     int bgcolour, fgcolour;
610     RGBColor rgbfore, rgbback;
611     struct do_text_args a;
612     RgnHandle textrgn;
613
614     SetPort(s->window);
615     
616     /* First check this text is relevant */
617     a.textrect.top = y * font_height;
618     a.textrect.bottom = (y + 1) * font_height;
619     a.textrect.left = x * font_width;
620     a.textrect.right = (x + len) * font_width;
621     if (!RectInRgn(&a.textrect, s->window->visRgn))
622         return;
623
624     a.s = s;
625     a.text = text;
626     a.len = len;
627     a.attr = attr;
628     SetPort(s->window);
629     TextFont(s->fontnum);
630     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
631         style |= bold;
632     if (attr & ATTR_UNDER)
633         style |= underline;
634     TextFace(style);
635     TextSize(cfg.fontheight);
636     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
637     textrgn = NewRgn();
638     RectRgn(textrgn, &a.textrect);
639     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
640     DisposeRgn(textrgn);
641     /* Tell the window manager about it in case this isn't an update */
642     ValidRect(&a.textrect);
643 }
644
645 static pascal void do_text_for_device(short depth, short devflags,
646                                       GDHandle device, long cookie) {
647     struct do_text_args *a;
648     int bgcolour, fgcolour, bright;
649
650     a = (struct do_text_args *)cookie;
651
652     bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
653
654     TextMode(a->attr & ATTR_REVERSE ? notSrcCopy : srcCopy);
655
656     switch (depth) {
657       case 1:
658         /* XXX This should be done with a _little_ more configurability */
659         ForeColor(whiteColor);
660         BackColor(blackColor);
661         if (a->attr & ATTR_ACTCURS)
662             TextMode(a->attr & ATTR_REVERSE ? srcCopy : notSrcCopy);
663         break;
664       case 2:
665         if (a->attr & ATTR_ACTCURS) {
666             PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
667             PmBackColor(CURSOR_BG);
668             TextMode(srcCopy);
669         } else {
670             PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
671             PmBackColor(DEFAULT_BG);
672         }
673         break;
674       default:
675         if (a->attr & ATTR_ACTCURS) {
676             fgcolour = bright ? CURSOR_FG_BOLD : CURSOR_FG;
677             bgcolour = CURSOR_BG;
678             TextMode(srcCopy);
679         } else {
680             fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
681             bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
682             if (bright)
683                 if (a->attr & ATTR_REVERSE)
684                     bgcolour++;
685                 else
686                     fgcolour++;
687         }
688         PmForeColor(fgcolour);
689         PmBackColor(bgcolour);
690         break;
691     }
692
693     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
694     DrawText(a->text, 0, a->len);
695
696     if (a->attr & ATTR_PASCURS) {
697         PenNormal();
698         switch (depth) {
699           case 1:
700             PenMode(patXor);
701             break;
702           default:
703             PmForeColor(CURSOR_BG);
704             break;
705         }
706         FrameRect(&a->textrect);
707     }
708 }
709
710 /*
711  * Call from the terminal emulator to get its graphics context.
712  * Should probably be called start_redraw or something.
713  */
714 struct mac_session *get_ctx(void) {
715     struct mac_session *s = onlysession;
716
717     attr_mask = ATTR_INVALID;
718     DeviceLoop(s->window->visRgn, mac_set_attr_mask, (long)s, 0);
719     return s;
720 }
721
722 static pascal void mac_set_attr_mask(short depth, short devflags,
723                                      GDHandle device, long cookie) {
724
725     switch (depth) {
726       default:
727         attr_mask |= ATTR_FGMASK | ATTR_BGMASK;
728         /* FALLTHROUGH */
729       case 2:
730         attr_mask |= ATTR_BOLD;
731         /* FALLTHROUGH */
732       case 1:
733         attr_mask |= ATTR_UNDER | ATTR_REVERSE | ATTR_ACTCURS |
734             ATTR_PASCURS | ATTR_ASCII | ATTR_GBCHR | ATTR_LINEDRW |
735             (cfg.bold_colour ? 0 : ATTR_BOLD); 
736         break;
737     }
738 }
739
740 /*
741  * Presumably this does something in Windows
742  */
743 void free_ctx(struct mac_session *ctx) {
744
745 }
746
747 /*
748  * Set the scroll bar position
749  *
750  * total is the line number of the bottom of the working screen
751  * start is the line number of the top of the display
752  * page is the length of the displayed page
753  */
754 void set_sbar(int total, int start, int page) {
755     struct mac_session *s = onlysession;
756
757     /* We don't redraw until we've set everything up, to avoid glitches */
758     (*s->scrollbar)->contrlMin = 0;
759     (*s->scrollbar)->contrlMax = total - page;
760     SetControlValue(s->scrollbar, start);
761 #if 0
762     /* XXX: This doesn't link for me. */
763     if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
764         SetControlViewSize(s->scrollbar, page);
765 #endif
766 }
767
768 /*
769  * Beep
770  */
771 void beep(void) {
772
773     SysBeep(30);
774     /*
775      * XXX We should indicate the relevant window and/or use the
776      * Notification Manager
777      */
778 }
779
780 /*
781  * Set icon string -- a no-op here (Windowshade?)
782  */
783 void set_icon(char *icon) {
784
785 }
786
787 /*
788  * Set the window title
789  */
790 void set_title(char *title) {
791     Str255 mactitle;
792     struct mac_session *s = onlysession;
793
794     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
795     SetWTitle(s->window, mactitle);
796 }
797
798 /*
799  * Resize the window at the emulator's request
800  */
801 void request_resize(int w, int h) {
802
803     cols = w;
804     rows = h;
805     mac_initfont(onlysession);
806 }
807
808 /*
809  * Set the logical palette
810  */
811 void palette_set(int n, int r, int g, int b) {
812     RGBColor col;
813     struct mac_session *s = onlysession;
814     static const int first[21] = {
815         0, 2, 4, 6, 8, 10, 12, 14,
816         1, 3, 5, 7, 9, 11, 13, 15,
817         16, 17, 18, 20, 22
818     };
819     
820     if (mac_gestalts.qdvers == gestaltOriginalQD)
821       return;
822     col.red   = r * 0x0101;
823     col.green = g * 0x0101;
824     col.blue  = b * 0x0101;
825     SetEntryColor(s->palette, first[n], &col);
826     if (first[n] >= 18)
827         SetEntryColor(s->palette, first[n]+1, &col);
828     ActivatePalette(s->window);
829 }
830
831 /*
832  * Reset to the default palette
833  */
834 void palette_reset(void) {
835     struct mac_session *s = onlysession;
836
837     if (mac_gestalts.qdvers == gestaltOriginalQD)
838         return;
839     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
840     ActivatePalette(s->window);
841     /* Palette Manager will generate update events as required. */
842 }
843
844 /*
845  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
846  * for backward.)
847  */
848 void do_scroll(int topline, int botline, int lines) {
849     struct mac_session *s = onlysession;
850     Rect r;
851     RgnHandle update;
852
853     SetPort(s->window);
854     PmBackColor(DEFAULT_BG);
855     update = NewRgn();
856     SetRect(&r, 0, topline * font_height,
857             cols * font_width, (botline + 1) * font_height);
858     ScrollRect(&r, 0, - lines * font_height, update);
859     /* XXX: move update region? */
860     InvalRgn(update);
861     DisposeRgn(update);
862 }
863
864 /*
865  * Emacs magic:
866  * Local Variables:
867  * c-file-style: "simon"
868  * End:
869  */