1 /* $Id: macterm.c,v 1.1.2.17 1999/03/11 21:40:32 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));
291 lastwhen = TickCount();
294 static pascal void mac_scrolltracker(ControlHandle control, short part) {
295 struct mac_session *s;
297 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
299 case kControlUpButtonPart:
302 case kControlDownButtonPart:
305 case kControlPageUpPart:
306 term_scroll(0, -(rows - 1));
308 case kControlPageDownPart:
309 term_scroll(0, +(rows - 1));
314 #define K_SPACE 0x3100
328 #define K_INSERT 0x7200
329 #define K_HOME 0x7300
330 #define K_PRIOR 0x7400
331 #define K_DELETE 0x7500
333 #define K_NEXT 0x7900
334 #define K_LEFT 0x7b00
335 #define K_RIGHT 0x7c00
336 #define K_DOWN 0x7d00
348 #define KP_CLEAR 0x4700
349 #define KP_EQUAL 0x5100
350 #define KP_SLASH 0x4b00
351 #define KP_STAR 0x4300
352 #define KP_PLUS 0x4500
353 #define KP_MINUS 0x4e00
354 #define KP_DOT 0x4100
355 #define KP_ENTER 0x4c00
357 void mac_keyterm(WindowPtr window, EventRecord *event) {
358 unsigned char buf[20];
360 struct mac_session *s;
363 s = (struct mac_session *)GetWRefCon(window);
364 len = mac_keytrans(s, event, buf);
365 /* XXX: I can't get the loopback backend to link, so we'll do this: */
366 /* back->send((char *)buf, len); */
367 for (i = 0; i < len; i++)
373 static int mac_keytrans(struct mac_session *s, EventRecord *event,
374 unsigned char *output) {
375 unsigned char *p = output;
378 /* No meta key yet -- that'll be rather fun. */
380 /* Keys that we handle locally */
381 if (event->modifiers & shiftKey) {
382 switch (event->message & keyCodeMask) {
383 case K_PRIOR: /* shift-pageup */
384 term_scroll(0, -(rows - 1));
386 case K_NEXT: /* shift-pagedown */
387 term_scroll(0, +(rows - 1));
393 * Control-2 should return ^@ (0x00), Control-6 should return
394 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
395 * the DOS keyboard handling did it, and we have nothing better
396 * to do with the key combo in question, we'll also map
397 * Control-Backquote to ^\ (0x1C).
400 if (event->modifiers & controlKey) {
401 switch (event->message & charCodeMask) {
418 * First, all the keys that do tilde codes. (ESC '[' nn '~',
419 * for integer decimal nn.)
421 * We also deal with the weird ones here. Linux VCs replace F1
422 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
423 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
427 switch (event->message & keyCodeMask) {
428 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
429 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
430 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
431 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
432 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
433 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
434 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
435 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
436 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
437 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
438 case K_F11: code = 23; break;
439 case K_F12: code = 24; break;
440 case K_HOME: code = 1; break;
441 case K_INSERT: code = 2; break;
442 case K_DELETE: code = 3; break;
443 case K_END: code = 4; break;
444 case K_PRIOR: code = 5; break;
445 case K_NEXT: code = 6; break;
447 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
448 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
451 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
452 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
456 p += sprintf((char *)p, "\x1B[%d~", code);
460 if (app_keypad_keys) {
461 switch (event->message & keyCodeMask) {
462 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
463 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
464 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
465 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
466 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
467 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
468 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
469 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
470 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
471 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
472 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
473 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
474 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
475 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
476 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
477 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
478 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
479 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
483 switch (event->message & keyCodeMask) {
485 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
488 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
491 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
494 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
497 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
500 *p++ = event->message & charCodeMask;
505 void mac_growterm(WindowPtr window, EventRecord *event) {
508 int newrows, newcols;
509 struct mac_session *s;
511 s = (struct mac_session *)GetWRefCon(window);
512 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
513 grow_result = GrowWindow(window, event->where, &limits);
514 if (grow_result != 0) {
515 newrows = HiWord(grow_result) / font_height;
516 newcols = (LoWord(grow_result) - 15) / font_width;
517 mac_adjustsize(s, newrows, newcols);
518 term_size(newrows, newcols, cfg.savelines);
522 void mac_activateterm(WindowPtr window, Boolean active) {
523 struct mac_session *s;
525 s = (struct mac_session *)GetWRefCon(window);
529 ShowControl(s->scrollbar);
531 HideControl(s->scrollbar);
534 void mac_updateterm(WindowPtr window) {
535 struct mac_session *s;
538 s = (struct mac_session *)GetWRefCon(window);
541 (*window->visRgn)->rgnBBox.left,
542 (*window->visRgn)->rgnBBox.top,
543 (*window->visRgn)->rgnBBox.right,
544 (*window->visRgn)->rgnBBox.bottom);
545 /* Restore default colours in case the Window Manager uses them */
548 if (FrontWindow() != window)
549 EraseRect(&(*s->scrollbar)->contrlRect);
550 UpdateControls(window, window->visRgn);
551 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
552 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
554 DrawGrowIcon(window);
555 clip.left = SHRT_MIN;
560 struct do_text_args {
561 struct mac_session *s;
569 * Call from the terminal emulator to draw a bit of text
571 * x and y are text row and column (zero-based)
573 void do_text(struct mac_session *s, int x, int y, char *text, int len,
574 unsigned long attr) {
576 int bgcolour, fgcolour;
577 RGBColor rgbfore, rgbback;
578 struct do_text_args a;
583 /* First check this text is relevant */
584 a.textrect.top = y * font_height;
585 a.textrect.bottom = (y + 1) * font_height;
586 a.textrect.left = x * font_width;
587 a.textrect.right = (x + len) * font_width;
588 if (!RectInRgn(&a.textrect, s->window->visRgn))
596 TextFont(s->fontnum);
597 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
599 if (attr & ATTR_UNDER)
602 TextSize(cfg.fontheight);
603 if (attr & ATTR_REVERSE)
604 TextMode(notSrcCopy);
607 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
609 RectRgn(textrgn, &a.textrect);
610 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
611 /* Tell the window manager about it in case this isn't an update */
613 ValidRect(&a.textrect);
616 static pascal void do_text_for_device(short depth, short devflags,
617 GDHandle device, long cookie) {
618 struct do_text_args *a;
619 int bgcolour, fgcolour;
621 a = (struct do_text_args *)cookie;
625 /* XXX This should be done with a _little_ more configurability */
626 ForeColor(whiteColor);
627 BackColor(blackColor);
630 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
631 PmForeColor(DEFAULT_FG_BOLD);
633 PmForeColor(DEFAULT_FG);
634 if (a->attr & ATTR_ACTCURS)
635 PmBackColor(CURSOR_FG);
637 PmBackColor(DEFAULT_BG);
640 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
641 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
642 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
643 if (a->attr & ATTR_REVERSE)
647 if (a->attr & ATTR_ACTCURS)
648 bgcolour = CURSOR_FG;
649 PmForeColor(fgcolour);
650 PmBackColor(bgcolour);
653 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
654 DrawText(a->text, 0, a->len);
658 * Call from the terminal emulator to get its graphics context.
660 struct mac_session *get_ctx(void) {
666 * Presumably this does something in Windows
668 void free_ctx(struct mac_session *ctx) {
673 * Set the scroll bar position
675 * total is the line number of the bottom of the working screen
676 * start is the line number of the top of the display
677 * page is the length of the displayed page
679 void set_sbar(int total, int start, int page) {
680 struct mac_session *s = onlysession;
682 /* We don't redraw until we've set everything up, to avoid glitches */
683 (*s->scrollbar)->contrlMin = 0;
684 (*s->scrollbar)->contrlMax = total - page;
685 SetControlValue(s->scrollbar, start);
687 /* XXX: This doesn't link for me. */
688 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
689 SetControlViewSize(s->scrollbar, page);
700 * XXX We should indicate the relevant window and/or use the
701 * Notification Manager
706 * Set icon string -- a no-op here (Windowshade?)
708 void set_icon(char *icon) {
713 * Set the window title
715 void set_title(char *title) {
717 struct mac_session *s = onlysession;
719 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
720 SetWTitle(s->window, mactitle);
724 * Resize the window at the emulator's request
726 void request_resize(int w, int h) {
730 mac_initfont(onlysession);
734 * Set the logical palette
736 void palette_set(int n, int r, int g, int b) {
738 struct mac_session *s = onlysession;
739 static const int first[21] = {
740 0, 2, 4, 6, 8, 10, 12, 14,
741 1, 3, 5, 7, 9, 11, 13, 15,
745 if (mac_gestalts.qdvers == gestaltOriginalQD)
747 col.red = r * 0x0101;
748 col.green = g * 0x0101;
749 col.blue = b * 0x0101;
750 SetEntryColor(s->palette, first[n], &col);
752 SetEntryColor(s->palette, first[n]+1, &col);
753 ActivatePalette(s->window);
757 * Reset to the default palette
759 void palette_reset(void) {
760 struct mac_session *s = onlysession;
762 if (mac_gestalts.qdvers == gestaltOriginalQD)
764 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
765 ActivatePalette(s->window);
766 /* Palette Manager will generate update events as required. */
770 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
773 void do_scroll(int topline, int botline, int lines) {
774 struct mac_session *s = onlysession;
779 PmBackColor(DEFAULT_BG);
781 SetRect(&r, 0, topline * font_height,
782 cols * font_width, (botline + 1) * font_height);
783 ScrollRect(&r, 0, - lines * font_height, update);
784 /* XXX: move update region? */
792 * c-file-style: "simon"