1 /* $Id: macterm.c,v 1.1.2.23 1999/03/14 20:43:51 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>
43 #include <ToolUtils.h>
54 #define DEFAULT_FG_BOLD 17
56 #define DEFAULT_BG_BOLD 19
58 #define CURSOR_FG_BOLD 21
60 #define CURSOR_BG_BOLD 23
62 #define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
64 #define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
71 PaletteHandle palette;
72 ControlHandle scrollbar;
75 static void mac_initfont(struct mac_session *);
76 static void mac_initpalette(struct mac_session *);
77 static void mac_adjustsize(struct mac_session *, int, int);
78 static pascal void mac_scrolltracker(ControlHandle, short);
79 static pascal void do_text_for_device(short, short, GDHandle, long);
80 static pascal void mac_set_attr_mask(short, short, GDHandle, long);
81 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
82 static void text_click(struct mac_session *, EventRecord *);
85 * Temporary hack till I get the terminal emulator supporting multiple
89 static struct mac_session *onlysession;
91 static void inbuf_putc(int c) {
92 inbuf[inbuf_head] = c;
93 inbuf_head = (inbuf_head+1) & INBUF_MASK;
96 static void inbuf_putstr(const char *c) {
101 static void display_resource(unsigned long type, short id) {
106 h = GetResource(type, id);
108 fatalbox("Can't get test resource");
109 SetResAttrs(h, GetResAttrs(h) | resLocked);
111 len = GetResourceSizeOnDisk(h);
112 for (i = 0; i < len; i++) {
116 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
121 void mac_newsession(void) {
122 struct mac_session *s;
127 /* This should obviously be initialised by other means */
128 mac_loadconfig(&cfg);
129 /* back = &loop_backend; */
130 s = smalloc(sizeof(*s));
133 /* XXX: Own storage management? */
134 if (mac_gestalts.qdvers == gestaltOriginalQD)
135 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
137 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
138 SetWRefCon(s->window, (long)s);
139 s->scrollbar = GetNewControl(cVScroll, s->window);
141 term_size(cfg.height, cfg.width, cfg.savelines);
144 attr_mask = ATTR_MASK;
145 /* Set to FALSE to not get palette updates in the background. */
146 SetPalette(s->window, s->palette, TRUE);
147 ActivatePalette(s->window);
148 ShowWindow(s->window);
149 starttime = TickCount();
150 display_resource('pTST', 128);
151 sprintf(msg, "Elapsed ticks: %d\015\012", TickCount() - starttime);
156 static void mac_initfont(struct mac_session *s) {
161 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
162 GetFNum(macfont, &s->fontnum);
163 TextFont(s->fontnum);
164 TextFace(cfg.fontisbold ? bold : 0);
165 TextSize(cfg.fontheight);
167 font_width = fi.widMax;
168 font_height = fi.ascent + fi.descent + fi.leading;
169 s->font_ascent = fi.ascent;
170 mac_adjustsize(s, rows, cols);
174 * To be called whenever the window size changes.
175 * rows and cols should be desired values.
176 * It's assumed the terminal emulator will be informed, and will set rows
179 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
180 int winwidth, winheight;
182 winwidth = newcols * font_width + 15;
183 winheight = newrows * font_height;
184 SizeWindow(s->window, winwidth, winheight, true);
185 HideControl(s->scrollbar);
186 MoveControl(s->scrollbar, winwidth - 15, -1);
187 SizeControl(s->scrollbar, 16, winheight - 13);
188 ShowControl(s->scrollbar);
191 static void mac_initpalette(struct mac_session *s) {
194 if (mac_gestalts.qdvers == gestaltOriginalQD)
196 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
197 if (s->palette == NULL)
198 fatalbox("Unable to create palette");
199 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
203 * I don't think this is (a) safe or (b) a good way to do this.
205 static void mac_updatewinbg(struct mac_session *s) {
208 WCTabHandle cth = &ctp;
213 ct.ctTable[0].value = wContentColor;
214 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
215 SetWinColor(s->window, cth);
219 * Enable/disable menu items based on the active terminal window.
221 void mac_adjusttermmenus(WindowPtr window) {
222 struct mac_session *s;
226 s = (struct mac_session *)GetWRefCon(window);
227 menu = GetMenuHandle(mEdit);
229 DisableItem(menu, iUndo);
230 DisableItem(menu, iCut);
231 DisableItem(menu, iCopy);
232 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
233 DisableItem(menu, iPaste);
235 EnableItem(menu, iPaste);
236 DisableItem(menu, iClear);
237 EnableItem(menu, iSelectAll);
240 void mac_clickterm(WindowPtr window, EventRecord *event) {
241 struct mac_session *s;
243 ControlHandle control;
246 s = (struct mac_session *)GetWRefCon(window);
248 mouse = event->where;
249 GlobalToLocal(&mouse);
250 part = FindControl(mouse, window, &control);
251 if (control == s->scrollbar) {
253 case kControlIndicatorPart:
254 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
255 term_scroll(+1, GetControlValue(control));
257 case kControlUpButtonPart:
258 case kControlDownButtonPart:
259 case kControlPageUpPart:
260 case kControlPageDownPart:
261 TrackControl(control, mouse, mac_scrolltracker);
265 text_click(s, event);
269 static void text_click(struct mac_session *s, EventRecord *event) {
272 static UInt32 lastwhen = 0;
273 static struct mac_session *lastsess = NULL;
274 static int lastrow = -1, lastcol = -1;
275 static Mouse_Action lastact = MA_NOTHING;
278 localwhere = event->where;
279 GlobalToLocal(&localwhere);
281 col = PTOCC(localwhere.h);
282 row = PTOCR(localwhere.v);
283 if (event->when - lastwhen < GetDblTime() &&
284 row == lastrow && col == lastcol && s == lastsess)
285 lastact = (lastact == MA_CLICK ? MA_2CLK :
286 lastact == MA_2CLK ? MA_3CLK :
287 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
290 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
295 while (StillDown()) {
296 GetMouse(&localwhere);
297 col = PTOCC(localwhere.h);
298 row = PTOCR(localwhere.v);
299 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
302 term_scroll(0, row - (rows - 1));
306 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
308 lastwhen = TickCount();
311 void write_clip(void *data, int len) {
313 if (ZeroScrap() != noErr)
315 PutScrap(len, 'TEXT', data);
318 void get_clip(void **p, int *lenp) {
320 /* XXX: do something */
323 static pascal void mac_scrolltracker(ControlHandle control, short part) {
324 struct mac_session *s;
326 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
328 case kControlUpButtonPart:
331 case kControlDownButtonPart:
334 case kControlPageUpPart:
335 term_scroll(0, -(rows - 1));
337 case kControlPageDownPart:
338 term_scroll(0, +(rows - 1));
343 #define K_SPACE 0x3100
357 #define K_INSERT 0x7200
358 #define K_HOME 0x7300
359 #define K_PRIOR 0x7400
360 #define K_DELETE 0x7500
362 #define K_NEXT 0x7900
363 #define K_LEFT 0x7b00
364 #define K_RIGHT 0x7c00
365 #define K_DOWN 0x7d00
377 #define KP_CLEAR 0x4700
378 #define KP_EQUAL 0x5100
379 #define KP_SLASH 0x4b00
380 #define KP_STAR 0x4300
381 #define KP_PLUS 0x4500
382 #define KP_MINUS 0x4e00
383 #define KP_DOT 0x4100
384 #define KP_ENTER 0x4c00
386 void mac_keyterm(WindowPtr window, EventRecord *event) {
387 unsigned char buf[20];
389 struct mac_session *s;
392 s = (struct mac_session *)GetWRefCon(window);
393 len = mac_keytrans(s, event, buf);
394 /* XXX: I can't get the loopback backend to link, so we'll do this: */
395 /* back->send((char *)buf, len); */
396 for (i = 0; i < len; i++)
402 static int mac_keytrans(struct mac_session *s, EventRecord *event,
403 unsigned char *output) {
404 unsigned char *p = output;
407 /* No meta key yet -- that'll be rather fun. */
409 /* Keys that we handle locally */
410 if (event->modifiers & shiftKey) {
411 switch (event->message & keyCodeMask) {
412 case K_PRIOR: /* shift-pageup */
413 term_scroll(0, -(rows - 1));
415 case K_NEXT: /* shift-pagedown */
416 term_scroll(0, +(rows - 1));
422 * Control-2 should return ^@ (0x00), Control-6 should return
423 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
424 * the DOS keyboard handling did it, and we have nothing better
425 * to do with the key combo in question, we'll also map
426 * Control-Backquote to ^\ (0x1C).
429 if (event->modifiers & controlKey) {
430 switch (event->message & charCodeMask) {
447 * First, all the keys that do tilde codes. (ESC '[' nn '~',
448 * for integer decimal nn.)
450 * We also deal with the weird ones here. Linux VCs replace F1
451 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
452 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
456 switch (event->message & keyCodeMask) {
457 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
458 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
459 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
460 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
461 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
462 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
463 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
464 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
465 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
466 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
467 case K_F11: code = 23; break;
468 case K_F12: code = 24; break;
469 case K_HOME: code = 1; break;
470 case K_INSERT: code = 2; break;
471 case K_DELETE: code = 3; break;
472 case K_END: code = 4; break;
473 case K_PRIOR: code = 5; break;
474 case K_NEXT: code = 6; break;
476 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
477 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
480 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
481 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
485 p += sprintf((char *)p, "\x1B[%d~", code);
489 if (app_keypad_keys) {
490 switch (event->message & keyCodeMask) {
491 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
492 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
493 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
494 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
495 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
496 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
497 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
498 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
499 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
500 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
501 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
502 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
503 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
504 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
505 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
506 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
507 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
508 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
512 switch (event->message & keyCodeMask) {
514 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
517 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
520 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
523 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
526 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
529 *p++ = event->message & charCodeMask;
534 void mac_growterm(WindowPtr window, EventRecord *event) {
537 int newrows, newcols;
538 struct mac_session *s;
540 s = (struct mac_session *)GetWRefCon(window);
541 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
542 grow_result = GrowWindow(window, event->where, &limits);
543 if (grow_result != 0) {
544 newrows = HiWord(grow_result) / font_height;
545 newcols = (LoWord(grow_result) - 15) / font_width;
546 mac_adjustsize(s, newrows, newcols);
547 term_size(newrows, newcols, cfg.savelines);
551 void mac_activateterm(WindowPtr window, Boolean active) {
552 struct mac_session *s;
554 s = (struct mac_session *)GetWRefCon(window);
558 ShowControl(s->scrollbar);
560 PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
561 HideControl(s->scrollbar);
565 void mac_updateterm(WindowPtr window) {
566 struct mac_session *s;
569 s = (struct mac_session *)GetWRefCon(window);
573 (*window->visRgn)->rgnBBox.left,
574 (*window->visRgn)->rgnBBox.top,
575 (*window->visRgn)->rgnBBox.right,
576 (*window->visRgn)->rgnBBox.bottom);
577 /* Restore default colours in case the Window Manager uses them */
580 if (FrontWindow() != window)
581 EraseRect(&(*s->scrollbar)->contrlRect);
582 UpdateControls(window, window->visRgn);
583 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
584 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
586 DrawGrowIcon(window);
587 clip.left = SHRT_MIN;
593 struct do_text_args {
594 struct mac_session *s;
602 * Call from the terminal emulator to draw a bit of text
604 * x and y are text row and column (zero-based)
606 void do_text(struct mac_session *s, int x, int y, char *text, int len,
607 unsigned long attr) {
609 int bgcolour, fgcolour;
610 RGBColor rgbfore, rgbback;
611 struct do_text_args a;
616 /* First check this text is relevant */
617 a.textrect.top = y * font_height;
618 a.textrect.bottom = (y + 1) * font_height;
619 a.textrect.left = x * font_width;
620 a.textrect.right = (x + len) * font_width;
621 if (!RectInRgn(&a.textrect, s->window->visRgn))
629 TextFont(s->fontnum);
630 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
632 if (attr & ATTR_UNDER)
635 TextSize(cfg.fontheight);
636 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
638 RectRgn(textrgn, &a.textrect);
639 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
641 /* Tell the window manager about it in case this isn't an update */
642 ValidRect(&a.textrect);
645 static pascal void do_text_for_device(short depth, short devflags,
646 GDHandle device, long cookie) {
647 struct do_text_args *a;
648 int bgcolour, fgcolour, bright;
650 a = (struct do_text_args *)cookie;
652 bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
654 TextMode(a->attr & ATTR_REVERSE ? notSrcCopy : srcCopy);
658 /* XXX This should be done with a _little_ more configurability */
659 ForeColor(whiteColor);
660 BackColor(blackColor);
661 if (a->attr & ATTR_ACTCURS)
662 TextMode(a->attr & ATTR_REVERSE ? srcCopy : notSrcCopy);
665 if (a->attr & ATTR_ACTCURS) {
666 PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
667 PmBackColor(CURSOR_BG);
670 PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
671 PmBackColor(DEFAULT_BG);
675 if (a->attr & ATTR_ACTCURS) {
676 fgcolour = bright ? CURSOR_FG_BOLD : CURSOR_FG;
677 bgcolour = CURSOR_BG;
680 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
681 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
683 if (a->attr & ATTR_REVERSE)
688 PmForeColor(fgcolour);
689 PmBackColor(bgcolour);
693 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
694 DrawText(a->text, 0, a->len);
696 if (a->attr & ATTR_PASCURS) {
703 PmForeColor(CURSOR_BG);
706 FrameRect(&a->textrect);
711 * Call from the terminal emulator to get its graphics context.
712 * Should probably be called start_redraw or something.
714 struct mac_session *get_ctx(void) {
715 struct mac_session *s = onlysession;
717 attr_mask = ATTR_INVALID;
718 DeviceLoop(s->window->visRgn, mac_set_attr_mask, (long)s, 0);
722 static pascal void mac_set_attr_mask(short depth, short devflags,
723 GDHandle device, long cookie) {
727 attr_mask |= ATTR_FGMASK | ATTR_BGMASK;
730 attr_mask |= ATTR_BOLD;
733 attr_mask |= ATTR_UNDER | ATTR_REVERSE | ATTR_ACTCURS |
734 ATTR_PASCURS | ATTR_ASCII | ATTR_GBCHR | ATTR_LINEDRW |
735 (cfg.bold_colour ? 0 : ATTR_BOLD);
741 * Presumably this does something in Windows
743 void free_ctx(struct mac_session *ctx) {
748 * Set the scroll bar position
750 * total is the line number of the bottom of the working screen
751 * start is the line number of the top of the display
752 * page is the length of the displayed page
754 void set_sbar(int total, int start, int page) {
755 struct mac_session *s = onlysession;
757 /* We don't redraw until we've set everything up, to avoid glitches */
758 (*s->scrollbar)->contrlMin = 0;
759 (*s->scrollbar)->contrlMax = total - page;
760 SetControlValue(s->scrollbar, start);
762 /* XXX: This doesn't link for me. */
763 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
764 SetControlViewSize(s->scrollbar, page);
775 * XXX We should indicate the relevant window and/or use the
776 * Notification Manager
781 * Set icon string -- a no-op here (Windowshade?)
783 void set_icon(char *icon) {
788 * Set the window title
790 void set_title(char *title) {
792 struct mac_session *s = onlysession;
794 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
795 SetWTitle(s->window, mactitle);
799 * Resize the window at the emulator's request
801 void request_resize(int w, int h) {
805 mac_initfont(onlysession);
809 * Set the logical palette
811 void palette_set(int n, int r, int g, int b) {
813 struct mac_session *s = onlysession;
814 static const int first[21] = {
815 0, 2, 4, 6, 8, 10, 12, 14,
816 1, 3, 5, 7, 9, 11, 13, 15,
820 if (mac_gestalts.qdvers == gestaltOriginalQD)
822 col.red = r * 0x0101;
823 col.green = g * 0x0101;
824 col.blue = b * 0x0101;
825 SetEntryColor(s->palette, first[n], &col);
827 SetEntryColor(s->palette, first[n]+1, &col);
828 ActivatePalette(s->window);
832 * Reset to the default palette
834 void palette_reset(void) {
835 struct mac_session *s = onlysession;
837 if (mac_gestalts.qdvers == gestaltOriginalQD)
839 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
840 ActivatePalette(s->window);
841 /* Palette Manager will generate update events as required. */
845 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
848 void do_scroll(int topline, int botline, int lines) {
849 struct mac_session *s = onlysession;
854 PmBackColor(DEFAULT_BG);
856 SetRect(&r, 0, topline * font_height,
857 cols * font_width, (botline + 1) * font_height);
858 ScrollRect(&r, 0, - lines * font_height, update);
859 /* XXX: move update region? */
867 * c-file-style: "simon"