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