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