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