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