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