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