1 /* $Id: macterm.c,v 1.1.2.15 1999/03/07 23:22:23 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * macterm.c -- Macintosh terminal front-end
36 #include <MacWindows.h>
38 #include <Quickdraw.h>
39 #include <QuickdrawText.h>
40 #include <Resources.h>
42 #include <ToolUtils.h>
53 #define DEFAULT_FG_BOLD 17
55 #define DEFAULT_BG_BOLD 19
57 #define CURSOR_FG_BOLD 23
63 PaletteHandle palette;
64 ControlHandle scrollbar;
67 static void mac_initfont(struct mac_session *);
68 static void mac_initpalette(struct mac_session *);
69 static void mac_adjustsize(struct mac_session *, int, int);
70 static pascal void mac_scrolltracker(ControlHandle, short);
71 static pascal void do_text_for_device(short, short, GDHandle, long);
72 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
75 * Temporary hack till I get the terminal emulator supporting multiple
79 static struct mac_session *onlysession;
81 static void inbuf_putc(int c) {
82 inbuf[inbuf_head] = c;
83 inbuf_head = (inbuf_head+1) & INBUF_MASK;
86 static void inbuf_putstr(const char *c) {
91 static void display_resource(unsigned long type, short id) {
96 h = GetResource(type, id);
98 fatalbox("Can't get test resource");
99 SetResAttrs(h, GetResAttrs(h) | resLocked);
101 len = GetResourceSizeOnDisk(h);
102 for (i = 0; i < len; i++) {
106 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
111 void mac_newsession(void) {
112 struct mac_session *s;
115 /* This should obviously be initialised by other means */
116 mac_loadconfig(&cfg);
117 /* back = &loop_backend; */
118 s = smalloc(sizeof(*s));
121 /* XXX: Own storage management? */
122 if (mac_gestalts.qdvers == gestaltOriginalQD)
123 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
125 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
126 SetWRefCon(s->window, (long)s);
127 s->scrollbar = GetNewControl(cVScroll, s->window);
129 term_size(cfg.height, cfg.width, cfg.savelines);
132 /* Set to FALSE to not get palette updates in the background. */
133 SetPalette(s->window, s->palette, TRUE);
134 ActivatePalette(s->window);
135 ShowWindow(s->window);
136 display_resource('pTST', 128);
139 static void mac_initfont(struct mac_session *s) {
144 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
145 GetFNum(macfont, &s->fontnum);
146 TextFont(s->fontnum);
147 TextFace(cfg.fontisbold ? bold : 0);
148 TextSize(cfg.fontheight);
150 font_width = fi.widMax;
151 font_height = fi.ascent + fi.descent + fi.leading;
152 s->font_ascent = fi.ascent;
153 mac_adjustsize(s, rows, cols);
157 * To be called whenever the window size changes.
158 * rows and cols should be desired values.
159 * It's assumed the terminal emulator will be informed, and will set rows
162 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
163 int winwidth, winheight;
165 winwidth = newcols * font_width + 15;
166 winheight = newrows * font_height;
167 SizeWindow(s->window, winwidth, winheight, true);
168 HideControl(s->scrollbar);
169 MoveControl(s->scrollbar, winwidth - 15, -1);
170 SizeControl(s->scrollbar, 16, winheight - 13);
171 ShowControl(s->scrollbar);
174 static void mac_initpalette(struct mac_session *s) {
177 if (mac_gestalts.qdvers == gestaltOriginalQD)
179 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
180 if (s->palette == NULL)
181 fatalbox("Unable to create palette");
182 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
186 * I don't think this is (a) safe or (b) a good way to do this.
188 static void mac_updatewinbg(struct mac_session *s) {
191 WCTabHandle cth = &ctp;
196 ct.ctTable[0].value = wContentColor;
197 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
198 SetWinColor(s->window, cth);
201 void mac_clickterm(WindowPtr window, EventRecord *event) {
202 struct mac_session *s;
204 ControlHandle control;
207 s = (struct mac_session *)GetWRefCon(window);
209 mouse = event->where;
210 GlobalToLocal(&mouse);
211 part = FindControl(mouse, window, &control);
212 if (control == s->scrollbar) {
214 case kControlIndicatorPart:
215 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
216 term_scroll(+1, GetControlValue(control));
218 case kControlUpButtonPart:
219 case kControlDownButtonPart:
220 case kControlPageUpPart:
221 case kControlPageDownPart:
222 TrackControl(control, mouse, mac_scrolltracker);
228 static pascal void mac_scrolltracker(ControlHandle control, short part) {
229 struct mac_session *s;
231 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
233 case kControlUpButtonPart:
236 case kControlDownButtonPart:
239 case kControlPageUpPart:
240 term_scroll(0, -(rows - 1));
242 case kControlPageDownPart:
243 term_scroll(0, +(rows - 1));
248 #define K_SPACE 0x3100
262 #define K_INSERT 0x7200
263 #define K_HOME 0x7300
264 #define K_PRIOR 0x7400
265 #define K_DELETE 0x7500
267 #define K_NEXT 0x7900
268 #define K_LEFT 0x7b00
269 #define K_RIGHT 0x7c00
270 #define K_DOWN 0x7d00
282 #define KP_CLEAR 0x4700
283 #define KP_EQUAL 0x5100
284 #define KP_SLASH 0x4b00
285 #define KP_STAR 0x4300
286 #define KP_PLUS 0x4500
287 #define KP_MINUS 0x4e00
288 #define KP_DOT 0x4100
289 #define KP_ENTER 0x4c00
291 void mac_keyterm(WindowPtr window, EventRecord *event) {
292 unsigned char buf[20];
294 struct mac_session *s;
297 s = (struct mac_session *)GetWRefCon(window);
298 len = mac_keytrans(s, event, buf);
299 /* XXX: I can't get the loopback backend to link, so we'll do this: */
300 /* back->send((char *)buf, len); */
301 for (i = 0; i < len; i++)
307 static int mac_keytrans(struct mac_session *s, EventRecord *event,
308 unsigned char *output) {
309 unsigned char *p = output;
312 /* No meta key yet -- that'll be rather fun. */
314 /* Keys that we handle locally */
315 if (event->modifiers & shiftKey) {
316 switch (event->message & keyCodeMask) {
317 case K_PRIOR: /* shift-pageup */
318 term_scroll(0, -(rows - 1));
320 case K_NEXT: /* shift-pagedown */
321 term_scroll(0, +(rows - 1));
327 * Control-2 should return ^@ (0x00), Control-6 should return
328 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
329 * the DOS keyboard handling did it, and we have nothing better
330 * to do with the key combo in question, we'll also map
331 * Control-Backquote to ^\ (0x1C).
334 if (event->modifiers & controlKey) {
335 switch (event->message & charCodeMask) {
352 * First, all the keys that do tilde codes. (ESC '[' nn '~',
353 * for integer decimal nn.)
355 * We also deal with the weird ones here. Linux VCs replace F1
356 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
357 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
361 switch (event->message & keyCodeMask) {
362 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
363 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
364 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
365 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
366 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
367 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
368 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
369 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
370 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
371 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
372 case K_F11: code = 23; break;
373 case K_F12: code = 24; break;
374 case K_HOME: code = 1; break;
375 case K_INSERT: code = 2; break;
376 case K_DELETE: code = 3; break;
377 case K_END: code = 4; break;
378 case K_PRIOR: code = 5; break;
379 case K_NEXT: code = 6; break;
381 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
382 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
385 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
386 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
390 p += sprintf((char *)p, "\x1B[%d~", code);
394 if (app_keypad_keys) {
395 switch (event->message & keyCodeMask) {
396 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
397 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
398 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
399 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
400 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
401 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
402 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
403 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
404 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
405 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
406 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
407 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
408 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
409 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
410 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
411 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
412 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
413 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
417 switch (event->message & keyCodeMask) {
419 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
422 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
425 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
428 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
431 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
434 *p++ = event->message & charCodeMask;
439 void mac_growterm(WindowPtr window, EventRecord *event) {
442 int newrows, newcols;
443 struct mac_session *s;
445 s = (struct mac_session *)GetWRefCon(window);
446 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
447 grow_result = GrowWindow(window, event->where, &limits);
448 if (grow_result != 0) {
449 newrows = HiWord(grow_result) / font_height;
450 newcols = (LoWord(grow_result) - 15) / font_width;
451 mac_adjustsize(s, newrows, newcols);
452 term_size(newrows, newcols, cfg.savelines);
456 void mac_activateterm(WindowPtr window, Boolean active) {
457 struct mac_session *s;
459 s = (struct mac_session *)GetWRefCon(window);
463 ShowControl(s->scrollbar);
465 HideControl(s->scrollbar);
468 void mac_updateterm(WindowPtr window) {
469 struct mac_session *s;
472 s = (struct mac_session *)GetWRefCon(window);
475 (*window->visRgn)->rgnBBox.left,
476 (*window->visRgn)->rgnBBox.top,
477 (*window->visRgn)->rgnBBox.right,
478 (*window->visRgn)->rgnBBox.bottom);
479 /* Restore default colours in case the Window Manager uses them */
482 if (FrontWindow() != window)
483 EraseRect(&(*s->scrollbar)->contrlRect);
484 UpdateControls(window, window->visRgn);
485 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
486 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
488 DrawGrowIcon(window);
489 clip.left = SHRT_MIN;
494 struct do_text_args {
495 struct mac_session *s;
503 * Call from the terminal emulator to draw a bit of text
505 * x and y are text row and column (zero-based)
507 void do_text(struct mac_session *s, int x, int y, char *text, int len,
508 unsigned long attr) {
510 int bgcolour, fgcolour;
511 RGBColor rgbfore, rgbback;
512 struct do_text_args a;
517 /* First check this text is relevant */
518 a.textrect.top = y * font_height;
519 a.textrect.bottom = (y + 1) * font_height;
520 a.textrect.left = x * font_width;
521 a.textrect.right = (x + len) * font_width;
522 if (!RectInRgn(&a.textrect, s->window->visRgn))
530 TextFont(s->fontnum);
531 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
533 if (attr & ATTR_UNDER)
536 TextSize(cfg.fontheight);
537 if (attr & ATTR_REVERSE)
538 TextMode(notSrcCopy);
541 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
543 RectRgn(textrgn, &a.textrect);
544 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
545 /* Tell the window manager about it in case this isn't an update */
547 ValidRect(&a.textrect);
550 static pascal void do_text_for_device(short depth, short devflags,
551 GDHandle device, long cookie) {
552 struct do_text_args *a;
553 int bgcolour, fgcolour;
555 a = (struct do_text_args *)cookie;
559 /* XXX This should be done with a _little_ more configurability */
560 ForeColor(whiteColor);
561 BackColor(blackColor);
564 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
565 PmForeColor(DEFAULT_FG_BOLD);
567 PmForeColor(DEFAULT_FG);
568 if (a->attr & ATTR_ACTCURS)
569 PmBackColor(CURSOR_FG);
571 PmBackColor(DEFAULT_BG);
574 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
575 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
576 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
577 if (a->attr & ATTR_REVERSE)
581 if (a->attr & ATTR_ACTCURS)
582 bgcolour = CURSOR_FG;
583 PmForeColor(fgcolour);
584 PmBackColor(bgcolour);
587 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
588 DrawText(a->text, 0, a->len);
592 * Call from the terminal emulator to get its graphics context.
594 struct mac_session *get_ctx(void) {
600 * Presumably this does something in Windows
602 void free_ctx(struct mac_session *ctx) {
607 * Set the scroll bar position
609 * total is the line number of the bottom of the working screen
610 * start is the line number of the top of the display
611 * page is the length of the displayed page
613 void set_sbar(int total, int start, int page) {
614 struct mac_session *s = onlysession;
616 /* We don't redraw until we've set everything up, to avoid glitches */
617 (*s->scrollbar)->contrlMin = 0;
618 (*s->scrollbar)->contrlMax = total - page;
619 SetControlValue(s->scrollbar, start);
621 /* XXX: This doesn't link for me. */
622 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
623 SetControlViewSize(s->scrollbar, page);
634 * XXX We should indicate the relevant window and/or use the
635 * Notification Manager
640 * Set icon string -- a no-op here (Windowshade?)
642 void set_icon(char *icon) {
647 * Set the window title
649 void set_title(char *title) {
651 struct mac_session *s = onlysession;
653 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
654 SetWTitle(s->window, mactitle);
658 * Resize the window at the emulator's request
660 void request_resize(int w, int h) {
664 mac_initfont(onlysession);
668 * Set the logical palette
670 void palette_set(int n, int r, int g, int b) {
672 struct mac_session *s = onlysession;
673 static const int first[21] = {
674 0, 2, 4, 6, 8, 10, 12, 14,
675 1, 3, 5, 7, 9, 11, 13, 15,
679 if (mac_gestalts.qdvers == gestaltOriginalQD)
681 col.red = r * 0x0101;
682 col.green = g * 0x0101;
683 col.blue = b * 0x0101;
684 SetEntryColor(s->palette, first[n], &col);
686 SetEntryColor(s->palette, first[n]+1, &col);
687 ActivatePalette(s->window);
691 * Reset to the default palette
693 void palette_reset(void) {
694 struct mac_session *s = onlysession;
696 if (mac_gestalts.qdvers == gestaltOriginalQD)
698 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
699 ActivatePalette(s->window);
700 /* Palette Manager will generate update events as required. */
704 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
707 void do_scroll(int topline, int botline, int lines) {
708 struct mac_session *s = onlysession;
713 PmBackColor(DEFAULT_BG);
715 SetRect(&r, 0, topline * font_height,
716 cols * font_width, (botline + 1) * font_height);
717 ScrollRect(&r, 0, - lines * font_height, update);
718 /* XXX: move update region? */