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