1 /* $Id: macterm.c,v 1.1.2.21 1999/03/14 15:51:34 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 int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
81 static void text_click(struct mac_session *, EventRecord *);
84 * Temporary hack till I get the terminal emulator supporting multiple
88 static struct mac_session *onlysession;
90 static void inbuf_putc(int c) {
91 inbuf[inbuf_head] = c;
92 inbuf_head = (inbuf_head+1) & INBUF_MASK;
95 static void inbuf_putstr(const char *c) {
100 static void display_resource(unsigned long type, short id) {
105 h = GetResource(type, id);
107 fatalbox("Can't get test resource");
108 SetResAttrs(h, GetResAttrs(h) | resLocked);
110 len = GetResourceSizeOnDisk(h);
111 for (i = 0; i < len; i++) {
115 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
120 void mac_newsession(void) {
121 struct mac_session *s;
124 /* This should obviously be initialised by other means */
125 mac_loadconfig(&cfg);
126 /* back = &loop_backend; */
127 s = smalloc(sizeof(*s));
130 /* XXX: Own storage management? */
131 if (mac_gestalts.qdvers == gestaltOriginalQD)
132 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
134 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
135 SetWRefCon(s->window, (long)s);
136 s->scrollbar = GetNewControl(cVScroll, s->window);
138 term_size(cfg.height, cfg.width, cfg.savelines);
141 /* Set to FALSE to not get palette updates in the background. */
142 SetPalette(s->window, s->palette, TRUE);
143 ActivatePalette(s->window);
144 ShowWindow(s->window);
145 display_resource('pTST', 128);
148 static void mac_initfont(struct mac_session *s) {
153 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
154 GetFNum(macfont, &s->fontnum);
155 TextFont(s->fontnum);
156 TextFace(cfg.fontisbold ? bold : 0);
157 TextSize(cfg.fontheight);
159 font_width = fi.widMax;
160 font_height = fi.ascent + fi.descent + fi.leading;
161 s->font_ascent = fi.ascent;
162 mac_adjustsize(s, rows, cols);
166 * To be called whenever the window size changes.
167 * rows and cols should be desired values.
168 * It's assumed the terminal emulator will be informed, and will set rows
171 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
172 int winwidth, winheight;
174 winwidth = newcols * font_width + 15;
175 winheight = newrows * font_height;
176 SizeWindow(s->window, winwidth, winheight, true);
177 HideControl(s->scrollbar);
178 MoveControl(s->scrollbar, winwidth - 15, -1);
179 SizeControl(s->scrollbar, 16, winheight - 13);
180 ShowControl(s->scrollbar);
183 static void mac_initpalette(struct mac_session *s) {
186 if (mac_gestalts.qdvers == gestaltOriginalQD)
188 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
189 if (s->palette == NULL)
190 fatalbox("Unable to create palette");
191 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
195 * I don't think this is (a) safe or (b) a good way to do this.
197 static void mac_updatewinbg(struct mac_session *s) {
200 WCTabHandle cth = &ctp;
205 ct.ctTable[0].value = wContentColor;
206 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
207 SetWinColor(s->window, cth);
211 * Enable/disable menu items based on the active terminal window.
213 void mac_adjusttermmenus(WindowPtr window) {
214 struct mac_session *s;
218 s = (struct mac_session *)GetWRefCon(window);
219 menu = GetMenuHandle(mEdit);
221 DisableItem(menu, iUndo);
222 DisableItem(menu, iCut);
223 DisableItem(menu, iCopy);
224 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
225 DisableItem(menu, iPaste);
227 EnableItem(menu, iPaste);
228 DisableItem(menu, iClear);
229 EnableItem(menu, iSelectAll);
232 void mac_clickterm(WindowPtr window, EventRecord *event) {
233 struct mac_session *s;
235 ControlHandle control;
238 s = (struct mac_session *)GetWRefCon(window);
240 mouse = event->where;
241 GlobalToLocal(&mouse);
242 part = FindControl(mouse, window, &control);
243 if (control == s->scrollbar) {
245 case kControlIndicatorPart:
246 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
247 term_scroll(+1, GetControlValue(control));
249 case kControlUpButtonPart:
250 case kControlDownButtonPart:
251 case kControlPageUpPart:
252 case kControlPageDownPart:
253 TrackControl(control, mouse, mac_scrolltracker);
257 text_click(s, event);
261 static void text_click(struct mac_session *s, EventRecord *event) {
264 static UInt32 lastwhen = 0;
265 static struct mac_session *lastsess = NULL;
266 static int lastrow = -1, lastcol = -1;
267 static Mouse_Action lastact = MA_NOTHING;
270 localwhere = event->where;
271 GlobalToLocal(&localwhere);
273 col = PTOCC(localwhere.h);
274 row = PTOCR(localwhere.v);
275 if (event->when - lastwhen < GetDblTime() &&
276 row == lastrow && col == lastcol && s == lastsess)
277 lastact = (lastact == MA_CLICK ? MA_2CLK :
278 lastact == MA_2CLK ? MA_3CLK :
279 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
282 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
287 while (StillDown()) {
288 GetMouse(&localwhere);
289 col = PTOCC(localwhere.h);
290 row = PTOCR(localwhere.v);
291 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
294 term_scroll(0, row - (rows - 1));
298 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
300 lastwhen = TickCount();
303 void write_clip(void *data, int len) {
305 if (ZeroScrap() != noErr)
307 PutScrap(len, 'TEXT', data);
310 void get_clip(void **p, int *lenp) {
312 /* XXX: do something */
315 static pascal void mac_scrolltracker(ControlHandle control, short part) {
316 struct mac_session *s;
318 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
320 case kControlUpButtonPart:
323 case kControlDownButtonPart:
326 case kControlPageUpPart:
327 term_scroll(0, -(rows - 1));
329 case kControlPageDownPart:
330 term_scroll(0, +(rows - 1));
335 #define K_SPACE 0x3100
349 #define K_INSERT 0x7200
350 #define K_HOME 0x7300
351 #define K_PRIOR 0x7400
352 #define K_DELETE 0x7500
354 #define K_NEXT 0x7900
355 #define K_LEFT 0x7b00
356 #define K_RIGHT 0x7c00
357 #define K_DOWN 0x7d00
369 #define KP_CLEAR 0x4700
370 #define KP_EQUAL 0x5100
371 #define KP_SLASH 0x4b00
372 #define KP_STAR 0x4300
373 #define KP_PLUS 0x4500
374 #define KP_MINUS 0x4e00
375 #define KP_DOT 0x4100
376 #define KP_ENTER 0x4c00
378 void mac_keyterm(WindowPtr window, EventRecord *event) {
379 unsigned char buf[20];
381 struct mac_session *s;
384 s = (struct mac_session *)GetWRefCon(window);
385 len = mac_keytrans(s, event, buf);
386 /* XXX: I can't get the loopback backend to link, so we'll do this: */
387 /* back->send((char *)buf, len); */
388 for (i = 0; i < len; i++)
394 static int mac_keytrans(struct mac_session *s, EventRecord *event,
395 unsigned char *output) {
396 unsigned char *p = output;
399 /* No meta key yet -- that'll be rather fun. */
401 /* Keys that we handle locally */
402 if (event->modifiers & shiftKey) {
403 switch (event->message & keyCodeMask) {
404 case K_PRIOR: /* shift-pageup */
405 term_scroll(0, -(rows - 1));
407 case K_NEXT: /* shift-pagedown */
408 term_scroll(0, +(rows - 1));
414 * Control-2 should return ^@ (0x00), Control-6 should return
415 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
416 * the DOS keyboard handling did it, and we have nothing better
417 * to do with the key combo in question, we'll also map
418 * Control-Backquote to ^\ (0x1C).
421 if (event->modifiers & controlKey) {
422 switch (event->message & charCodeMask) {
439 * First, all the keys that do tilde codes. (ESC '[' nn '~',
440 * for integer decimal nn.)
442 * We also deal with the weird ones here. Linux VCs replace F1
443 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
444 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
448 switch (event->message & keyCodeMask) {
449 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
450 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
451 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
452 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
453 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
454 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
455 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
456 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
457 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
458 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
459 case K_F11: code = 23; break;
460 case K_F12: code = 24; break;
461 case K_HOME: code = 1; break;
462 case K_INSERT: code = 2; break;
463 case K_DELETE: code = 3; break;
464 case K_END: code = 4; break;
465 case K_PRIOR: code = 5; break;
466 case K_NEXT: code = 6; break;
468 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
469 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
472 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
473 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
477 p += sprintf((char *)p, "\x1B[%d~", code);
481 if (app_keypad_keys) {
482 switch (event->message & keyCodeMask) {
483 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
484 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
485 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
486 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
487 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
488 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
489 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
490 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
491 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
492 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
493 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
494 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
495 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
496 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
497 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
498 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
499 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
500 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
504 switch (event->message & keyCodeMask) {
506 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
509 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
512 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
515 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
518 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
521 *p++ = event->message & charCodeMask;
526 void mac_growterm(WindowPtr window, EventRecord *event) {
529 int newrows, newcols;
530 struct mac_session *s;
532 s = (struct mac_session *)GetWRefCon(window);
533 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
534 grow_result = GrowWindow(window, event->where, &limits);
535 if (grow_result != 0) {
536 newrows = HiWord(grow_result) / font_height;
537 newcols = (LoWord(grow_result) - 15) / font_width;
538 mac_adjustsize(s, newrows, newcols);
539 term_size(newrows, newcols, cfg.savelines);
543 void mac_activateterm(WindowPtr window, Boolean active) {
544 struct mac_session *s;
546 s = (struct mac_session *)GetWRefCon(window);
550 ShowControl(s->scrollbar);
552 PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
553 HideControl(s->scrollbar);
557 void mac_updateterm(WindowPtr window) {
558 struct mac_session *s;
561 s = (struct mac_session *)GetWRefCon(window);
564 (*window->visRgn)->rgnBBox.left,
565 (*window->visRgn)->rgnBBox.top,
566 (*window->visRgn)->rgnBBox.right,
567 (*window->visRgn)->rgnBBox.bottom);
568 /* Restore default colours in case the Window Manager uses them */
571 if (FrontWindow() != window)
572 EraseRect(&(*s->scrollbar)->contrlRect);
573 UpdateControls(window, window->visRgn);
574 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
575 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
577 DrawGrowIcon(window);
578 clip.left = SHRT_MIN;
583 struct do_text_args {
584 struct mac_session *s;
592 * Call from the terminal emulator to draw a bit of text
594 * x and y are text row and column (zero-based)
596 void do_text(struct mac_session *s, int x, int y, char *text, int len,
597 unsigned long attr) {
599 int bgcolour, fgcolour;
600 RGBColor rgbfore, rgbback;
601 struct do_text_args a;
606 /* First check this text is relevant */
607 a.textrect.top = y * font_height;
608 a.textrect.bottom = (y + 1) * font_height;
609 a.textrect.left = x * font_width;
610 a.textrect.right = (x + len) * font_width;
611 if (!RectInRgn(&a.textrect, s->window->visRgn))
619 TextFont(s->fontnum);
620 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
622 if (attr & ATTR_UNDER)
625 TextSize(cfg.fontheight);
626 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
628 RectRgn(textrgn, &a.textrect);
629 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
631 /* Tell the window manager about it in case this isn't an update */
632 ValidRect(&a.textrect);
635 static pascal void do_text_for_device(short depth, short devflags,
636 GDHandle device, long cookie) {
637 struct do_text_args *a;
638 int bgcolour, fgcolour, bright;
640 a = (struct do_text_args *)cookie;
642 bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
644 if (a->attr & ATTR_REVERSE)
645 TextMode(notSrcCopy);
651 /* XXX This should be done with a _little_ more configurability */
652 ForeColor(whiteColor);
653 BackColor(blackColor);
654 if (a->attr & ATTR_ACTCURS)
655 TextMode((a->attr & ATTR_REVERSE) ? srcCopy : notSrcCopy);
658 if (a->attr & ATTR_ACTCURS) {
659 PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
660 PmBackColor(CURSOR_BG);
663 PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
664 PmBackColor(DEFAULT_BG);
668 if (a->attr & ATTR_ACTCURS) {
669 fgcolour = (bright ? CURSOR_FG_BOLD : CURSOR_FG);
670 bgcolour = CURSOR_BG;
673 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
674 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
676 if (a->attr & ATTR_REVERSE)
681 PmForeColor(fgcolour);
682 PmBackColor(bgcolour);
686 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
687 DrawText(a->text, 0, a->len);
689 if (a->attr & ATTR_PASCURS) {
696 PmForeColor(CURSOR_BG);
699 FrameRect(&a->textrect);
704 * Call from the terminal emulator to get its graphics context.
706 struct mac_session *get_ctx(void) {
712 * Presumably this does something in Windows
714 void free_ctx(struct mac_session *ctx) {
719 * Set the scroll bar position
721 * total is the line number of the bottom of the working screen
722 * start is the line number of the top of the display
723 * page is the length of the displayed page
725 void set_sbar(int total, int start, int page) {
726 struct mac_session *s = onlysession;
728 /* We don't redraw until we've set everything up, to avoid glitches */
729 (*s->scrollbar)->contrlMin = 0;
730 (*s->scrollbar)->contrlMax = total - page;
731 SetControlValue(s->scrollbar, start);
733 /* XXX: This doesn't link for me. */
734 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
735 SetControlViewSize(s->scrollbar, page);
746 * XXX We should indicate the relevant window and/or use the
747 * Notification Manager
752 * Set icon string -- a no-op here (Windowshade?)
754 void set_icon(char *icon) {
759 * Set the window title
761 void set_title(char *title) {
763 struct mac_session *s = onlysession;
765 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
766 SetWTitle(s->window, mactitle);
770 * Resize the window at the emulator's request
772 void request_resize(int w, int h) {
776 mac_initfont(onlysession);
780 * Set the logical palette
782 void palette_set(int n, int r, int g, int b) {
784 struct mac_session *s = onlysession;
785 static const int first[21] = {
786 0, 2, 4, 6, 8, 10, 12, 14,
787 1, 3, 5, 7, 9, 11, 13, 15,
791 if (mac_gestalts.qdvers == gestaltOriginalQD)
793 col.red = r * 0x0101;
794 col.green = g * 0x0101;
795 col.blue = b * 0x0101;
796 SetEntryColor(s->palette, first[n], &col);
798 SetEntryColor(s->palette, first[n]+1, &col);
799 ActivatePalette(s->window);
803 * Reset to the default palette
805 void palette_reset(void) {
806 struct mac_session *s = onlysession;
808 if (mac_gestalts.qdvers == gestaltOriginalQD)
810 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
811 ActivatePalette(s->window);
812 /* Palette Manager will generate update events as required. */
816 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
819 void do_scroll(int topline, int botline, int lines) {
820 struct mac_session *s = onlysession;
825 PmBackColor(DEFAULT_BG);
827 SetRect(&r, 0, topline * font_height,
828 cols * font_width, (botline + 1) * font_height);
829 ScrollRect(&r, 0, - lines * font_height, update);
830 /* XXX: move update region? */
838 * c-file-style: "simon"