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