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