1 /* $Id: macterm.c,v 1.1.2.18 1999/03/11 23:23:45 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 23
64 PaletteHandle palette;
65 ControlHandle scrollbar;
68 static void mac_initfont(struct mac_session *);
69 static void mac_initpalette(struct mac_session *);
70 static void mac_adjustsize(struct mac_session *, int, int);
71 static pascal void mac_scrolltracker(ControlHandle, short);
72 static pascal void do_text_for_device(short, short, GDHandle, long);
73 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
74 static void text_click(struct mac_session *, EventRecord *);
77 * Temporary hack till I get the terminal emulator supporting multiple
81 static struct mac_session *onlysession;
83 static void inbuf_putc(int c) {
84 inbuf[inbuf_head] = c;
85 inbuf_head = (inbuf_head+1) & INBUF_MASK;
88 static void inbuf_putstr(const char *c) {
93 static void display_resource(unsigned long type, short id) {
98 h = GetResource(type, id);
100 fatalbox("Can't get test resource");
101 SetResAttrs(h, GetResAttrs(h) | resLocked);
103 len = GetResourceSizeOnDisk(h);
104 for (i = 0; i < len; i++) {
108 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
113 void mac_newsession(void) {
114 struct mac_session *s;
117 /* This should obviously be initialised by other means */
118 mac_loadconfig(&cfg);
119 /* back = &loop_backend; */
120 s = smalloc(sizeof(*s));
123 /* XXX: Own storage management? */
124 if (mac_gestalts.qdvers == gestaltOriginalQD)
125 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
127 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
128 SetWRefCon(s->window, (long)s);
129 s->scrollbar = GetNewControl(cVScroll, s->window);
131 term_size(cfg.height, cfg.width, cfg.savelines);
134 /* Set to FALSE to not get palette updates in the background. */
135 SetPalette(s->window, s->palette, TRUE);
136 ActivatePalette(s->window);
137 ShowWindow(s->window);
138 display_resource('pTST', 128);
141 static void mac_initfont(struct mac_session *s) {
146 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
147 GetFNum(macfont, &s->fontnum);
148 TextFont(s->fontnum);
149 TextFace(cfg.fontisbold ? bold : 0);
150 TextSize(cfg.fontheight);
152 font_width = fi.widMax;
153 font_height = fi.ascent + fi.descent + fi.leading;
154 s->font_ascent = fi.ascent;
155 mac_adjustsize(s, rows, cols);
159 * To be called whenever the window size changes.
160 * rows and cols should be desired values.
161 * It's assumed the terminal emulator will be informed, and will set rows
164 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
165 int winwidth, winheight;
167 winwidth = newcols * font_width + 15;
168 winheight = newrows * font_height;
169 SizeWindow(s->window, winwidth, winheight, true);
170 HideControl(s->scrollbar);
171 MoveControl(s->scrollbar, winwidth - 15, -1);
172 SizeControl(s->scrollbar, 16, winheight - 13);
173 ShowControl(s->scrollbar);
176 static void mac_initpalette(struct mac_session *s) {
179 if (mac_gestalts.qdvers == gestaltOriginalQD)
181 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
182 if (s->palette == NULL)
183 fatalbox("Unable to create palette");
184 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
188 * I don't think this is (a) safe or (b) a good way to do this.
190 static void mac_updatewinbg(struct mac_session *s) {
193 WCTabHandle cth = &ctp;
198 ct.ctTable[0].value = wContentColor;
199 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
200 SetWinColor(s->window, cth);
204 * Enable/disable menu items based on the active terminal window.
206 void mac_adjusttermmenus(WindowPtr window) {
207 struct mac_session *s;
211 s = (struct mac_session *)GetWRefCon(window);
212 menu = GetMenuHandle(mEdit);
214 DisableItem(menu, iUndo);
215 DisableItem(menu, iCut);
216 DisableItem(menu, iCopy);
217 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
218 DisableItem(menu, iPaste);
220 EnableItem(menu, iPaste);
221 DisableItem(menu, iClear);
222 EnableItem(menu, iSelectAll);
225 void mac_clickterm(WindowPtr window, EventRecord *event) {
226 struct mac_session *s;
228 ControlHandle control;
231 s = (struct mac_session *)GetWRefCon(window);
233 mouse = event->where;
234 GlobalToLocal(&mouse);
235 part = FindControl(mouse, window, &control);
236 if (control == s->scrollbar) {
238 case kControlIndicatorPart:
239 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
240 term_scroll(+1, GetControlValue(control));
242 case kControlUpButtonPart:
243 case kControlDownButtonPart:
244 case kControlPageUpPart:
245 case kControlPageDownPart:
246 TrackControl(control, mouse, mac_scrolltracker);
250 text_click(s, event);
254 static void text_click(struct mac_session *s, EventRecord *event) {
257 static UInt32 lastwhen = 0;
258 static struct mac_session *lastsess = NULL;
259 static int lastrow = -1, lastcol = -1;
260 static Mouse_Action lastact = MA_NOTHING;
263 localwhere = event->where;
264 GlobalToLocal(&localwhere);
266 col = localwhere.h / font_width;
267 row = localwhere.v / font_height;
268 if (event->when - lastwhen < GetDblTime() &&
269 row == lastrow && col == lastcol && s == lastsess)
270 lastact = (lastact == MA_CLICK ? MA_2CLK :
271 lastact == MA_2CLK ? MA_3CLK :
272 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
275 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
280 while (StillDown()) {
281 GetMouse(&localwhere);
282 col = localwhere.h / font_width;
283 row = localwhere.v / font_height;
284 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
287 term_scroll(0, row - (rows - 1));
289 term_scroll(0, row - 1);
291 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
293 lastwhen = TickCount();
296 void write_clip(void *data, int len) {
298 if (ZeroScrap() != noErr)
300 PutScrap(len, 'TEXT', data);
303 void get_clip(void **p, int *lenp) {
305 /* XXX: do something */
308 static pascal void mac_scrolltracker(ControlHandle control, short part) {
309 struct mac_session *s;
311 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
313 case kControlUpButtonPart:
316 case kControlDownButtonPart:
319 case kControlPageUpPart:
320 term_scroll(0, -(rows - 1));
322 case kControlPageDownPart:
323 term_scroll(0, +(rows - 1));
328 #define K_SPACE 0x3100
342 #define K_INSERT 0x7200
343 #define K_HOME 0x7300
344 #define K_PRIOR 0x7400
345 #define K_DELETE 0x7500
347 #define K_NEXT 0x7900
348 #define K_LEFT 0x7b00
349 #define K_RIGHT 0x7c00
350 #define K_DOWN 0x7d00
362 #define KP_CLEAR 0x4700
363 #define KP_EQUAL 0x5100
364 #define KP_SLASH 0x4b00
365 #define KP_STAR 0x4300
366 #define KP_PLUS 0x4500
367 #define KP_MINUS 0x4e00
368 #define KP_DOT 0x4100
369 #define KP_ENTER 0x4c00
371 void mac_keyterm(WindowPtr window, EventRecord *event) {
372 unsigned char buf[20];
374 struct mac_session *s;
377 s = (struct mac_session *)GetWRefCon(window);
378 len = mac_keytrans(s, event, buf);
379 /* XXX: I can't get the loopback backend to link, so we'll do this: */
380 /* back->send((char *)buf, len); */
381 for (i = 0; i < len; i++)
387 static int mac_keytrans(struct mac_session *s, EventRecord *event,
388 unsigned char *output) {
389 unsigned char *p = output;
392 /* No meta key yet -- that'll be rather fun. */
394 /* Keys that we handle locally */
395 if (event->modifiers & shiftKey) {
396 switch (event->message & keyCodeMask) {
397 case K_PRIOR: /* shift-pageup */
398 term_scroll(0, -(rows - 1));
400 case K_NEXT: /* shift-pagedown */
401 term_scroll(0, +(rows - 1));
407 * Control-2 should return ^@ (0x00), Control-6 should return
408 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
409 * the DOS keyboard handling did it, and we have nothing better
410 * to do with the key combo in question, we'll also map
411 * Control-Backquote to ^\ (0x1C).
414 if (event->modifiers & controlKey) {
415 switch (event->message & charCodeMask) {
432 * First, all the keys that do tilde codes. (ESC '[' nn '~',
433 * for integer decimal nn.)
435 * We also deal with the weird ones here. Linux VCs replace F1
436 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
437 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
441 switch (event->message & keyCodeMask) {
442 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
443 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
444 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
445 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
446 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
447 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
448 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
449 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
450 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
451 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
452 case K_F11: code = 23; break;
453 case K_F12: code = 24; break;
454 case K_HOME: code = 1; break;
455 case K_INSERT: code = 2; break;
456 case K_DELETE: code = 3; break;
457 case K_END: code = 4; break;
458 case K_PRIOR: code = 5; break;
459 case K_NEXT: code = 6; break;
461 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
462 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
465 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
466 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
470 p += sprintf((char *)p, "\x1B[%d~", code);
474 if (app_keypad_keys) {
475 switch (event->message & keyCodeMask) {
476 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
477 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
478 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
479 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
480 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
481 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
482 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
483 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
484 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
485 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
486 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
487 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
488 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
489 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
490 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
491 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
492 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
493 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
497 switch (event->message & keyCodeMask) {
499 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
502 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
505 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
508 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
511 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
514 *p++ = event->message & charCodeMask;
519 void mac_growterm(WindowPtr window, EventRecord *event) {
522 int newrows, newcols;
523 struct mac_session *s;
525 s = (struct mac_session *)GetWRefCon(window);
526 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
527 grow_result = GrowWindow(window, event->where, &limits);
528 if (grow_result != 0) {
529 newrows = HiWord(grow_result) / font_height;
530 newcols = (LoWord(grow_result) - 15) / font_width;
531 mac_adjustsize(s, newrows, newcols);
532 term_size(newrows, newcols, cfg.savelines);
536 void mac_activateterm(WindowPtr window, Boolean active) {
537 struct mac_session *s;
539 s = (struct mac_session *)GetWRefCon(window);
543 ShowControl(s->scrollbar);
545 HideControl(s->scrollbar);
548 void mac_updateterm(WindowPtr window) {
549 struct mac_session *s;
552 s = (struct mac_session *)GetWRefCon(window);
555 (*window->visRgn)->rgnBBox.left,
556 (*window->visRgn)->rgnBBox.top,
557 (*window->visRgn)->rgnBBox.right,
558 (*window->visRgn)->rgnBBox.bottom);
559 /* Restore default colours in case the Window Manager uses them */
562 if (FrontWindow() != window)
563 EraseRect(&(*s->scrollbar)->contrlRect);
564 UpdateControls(window, window->visRgn);
565 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
566 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
568 DrawGrowIcon(window);
569 clip.left = SHRT_MIN;
574 struct do_text_args {
575 struct mac_session *s;
583 * Call from the terminal emulator to draw a bit of text
585 * x and y are text row and column (zero-based)
587 void do_text(struct mac_session *s, int x, int y, char *text, int len,
588 unsigned long attr) {
590 int bgcolour, fgcolour;
591 RGBColor rgbfore, rgbback;
592 struct do_text_args a;
597 /* First check this text is relevant */
598 a.textrect.top = y * font_height;
599 a.textrect.bottom = (y + 1) * font_height;
600 a.textrect.left = x * font_width;
601 a.textrect.right = (x + len) * font_width;
602 if (!RectInRgn(&a.textrect, s->window->visRgn))
610 TextFont(s->fontnum);
611 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
613 if (attr & ATTR_UNDER)
616 TextSize(cfg.fontheight);
617 if (attr & ATTR_REVERSE)
618 TextMode(notSrcCopy);
621 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
623 RectRgn(textrgn, &a.textrect);
624 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
625 /* Tell the window manager about it in case this isn't an update */
627 ValidRect(&a.textrect);
630 static pascal void do_text_for_device(short depth, short devflags,
631 GDHandle device, long cookie) {
632 struct do_text_args *a;
633 int bgcolour, fgcolour;
635 a = (struct do_text_args *)cookie;
639 /* XXX This should be done with a _little_ more configurability */
640 ForeColor(whiteColor);
641 BackColor(blackColor);
644 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
645 PmForeColor(DEFAULT_FG_BOLD);
647 PmForeColor(DEFAULT_FG);
648 if (a->attr & ATTR_ACTCURS)
649 PmBackColor(CURSOR_FG);
651 PmBackColor(DEFAULT_BG);
654 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
655 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
656 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
657 if (a->attr & ATTR_REVERSE)
661 if (a->attr & ATTR_ACTCURS)
662 bgcolour = CURSOR_FG;
663 PmForeColor(fgcolour);
664 PmBackColor(bgcolour);
667 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
668 DrawText(a->text, 0, a->len);
672 * Call from the terminal emulator to get its graphics context.
674 struct mac_session *get_ctx(void) {
680 * Presumably this does something in Windows
682 void free_ctx(struct mac_session *ctx) {
687 * Set the scroll bar position
689 * total is the line number of the bottom of the working screen
690 * start is the line number of the top of the display
691 * page is the length of the displayed page
693 void set_sbar(int total, int start, int page) {
694 struct mac_session *s = onlysession;
696 /* We don't redraw until we've set everything up, to avoid glitches */
697 (*s->scrollbar)->contrlMin = 0;
698 (*s->scrollbar)->contrlMax = total - page;
699 SetControlValue(s->scrollbar, start);
701 /* XXX: This doesn't link for me. */
702 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
703 SetControlViewSize(s->scrollbar, page);
714 * XXX We should indicate the relevant window and/or use the
715 * Notification Manager
720 * Set icon string -- a no-op here (Windowshade?)
722 void set_icon(char *icon) {
727 * Set the window title
729 void set_title(char *title) {
731 struct mac_session *s = onlysession;
733 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
734 SetWTitle(s->window, mactitle);
738 * Resize the window at the emulator's request
740 void request_resize(int w, int h) {
744 mac_initfont(onlysession);
748 * Set the logical palette
750 void palette_set(int n, int r, int g, int b) {
752 struct mac_session *s = onlysession;
753 static const int first[21] = {
754 0, 2, 4, 6, 8, 10, 12, 14,
755 1, 3, 5, 7, 9, 11, 13, 15,
759 if (mac_gestalts.qdvers == gestaltOriginalQD)
761 col.red = r * 0x0101;
762 col.green = g * 0x0101;
763 col.blue = b * 0x0101;
764 SetEntryColor(s->palette, first[n], &col);
766 SetEntryColor(s->palette, first[n]+1, &col);
767 ActivatePalette(s->window);
771 * Reset to the default palette
773 void palette_reset(void) {
774 struct mac_session *s = onlysession;
776 if (mac_gestalts.qdvers == gestaltOriginalQD)
778 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
779 ActivatePalette(s->window);
780 /* Palette Manager will generate update events as required. */
784 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
787 void do_scroll(int topline, int botline, int lines) {
788 struct mac_session *s = onlysession;
793 PmBackColor(DEFAULT_BG);
795 SetRect(&r, 0, topline * font_height,
796 cols * font_width, (botline + 1) * font_height);
797 ScrollRect(&r, 0, - lines * font_height, update);
798 /* XXX: move update region? */
806 * c-file-style: "simon"