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