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