1 /* $Id: macterm.c,v 1.1.2.19 1999/03/13 18:23:24 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
60 #define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
62 #define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
69 PaletteHandle palette;
70 ControlHandle scrollbar;
73 static void mac_initfont(struct mac_session *);
74 static void mac_initpalette(struct mac_session *);
75 static void mac_adjustsize(struct mac_session *, int, int);
76 static pascal void mac_scrolltracker(ControlHandle, short);
77 static pascal void do_text_for_device(short, short, GDHandle, long);
78 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
79 static void text_click(struct mac_session *, EventRecord *);
82 * Temporary hack till I get the terminal emulator supporting multiple
86 static struct mac_session *onlysession;
88 static void inbuf_putc(int c) {
89 inbuf[inbuf_head] = c;
90 inbuf_head = (inbuf_head+1) & INBUF_MASK;
93 static void inbuf_putstr(const char *c) {
98 static void display_resource(unsigned long type, short id) {
103 h = GetResource(type, id);
105 fatalbox("Can't get test resource");
106 SetResAttrs(h, GetResAttrs(h) | resLocked);
108 len = GetResourceSizeOnDisk(h);
109 for (i = 0; i < len; i++) {
113 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
118 void mac_newsession(void) {
119 struct mac_session *s;
122 /* This should obviously be initialised by other means */
123 mac_loadconfig(&cfg);
124 /* back = &loop_backend; */
125 s = smalloc(sizeof(*s));
128 /* XXX: Own storage management? */
129 if (mac_gestalts.qdvers == gestaltOriginalQD)
130 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
132 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
133 SetWRefCon(s->window, (long)s);
134 s->scrollbar = GetNewControl(cVScroll, s->window);
136 term_size(cfg.height, cfg.width, cfg.savelines);
139 /* Set to FALSE to not get palette updates in the background. */
140 SetPalette(s->window, s->palette, TRUE);
141 ActivatePalette(s->window);
142 ShowWindow(s->window);
143 display_resource('pTST', 128);
146 static void mac_initfont(struct mac_session *s) {
151 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
152 GetFNum(macfont, &s->fontnum);
153 TextFont(s->fontnum);
154 TextFace(cfg.fontisbold ? bold : 0);
155 TextSize(cfg.fontheight);
157 font_width = fi.widMax;
158 font_height = fi.ascent + fi.descent + fi.leading;
159 s->font_ascent = fi.ascent;
160 mac_adjustsize(s, rows, cols);
164 * To be called whenever the window size changes.
165 * rows and cols should be desired values.
166 * It's assumed the terminal emulator will be informed, and will set rows
169 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
170 int winwidth, winheight;
172 winwidth = newcols * font_width + 15;
173 winheight = newrows * font_height;
174 SizeWindow(s->window, winwidth, winheight, true);
175 HideControl(s->scrollbar);
176 MoveControl(s->scrollbar, winwidth - 15, -1);
177 SizeControl(s->scrollbar, 16, winheight - 13);
178 ShowControl(s->scrollbar);
181 static void mac_initpalette(struct mac_session *s) {
184 if (mac_gestalts.qdvers == gestaltOriginalQD)
186 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
187 if (s->palette == NULL)
188 fatalbox("Unable to create palette");
189 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
193 * I don't think this is (a) safe or (b) a good way to do this.
195 static void mac_updatewinbg(struct mac_session *s) {
198 WCTabHandle cth = &ctp;
203 ct.ctTable[0].value = wContentColor;
204 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
205 SetWinColor(s->window, cth);
209 * Enable/disable menu items based on the active terminal window.
211 void mac_adjusttermmenus(WindowPtr window) {
212 struct mac_session *s;
216 s = (struct mac_session *)GetWRefCon(window);
217 menu = GetMenuHandle(mEdit);
219 DisableItem(menu, iUndo);
220 DisableItem(menu, iCut);
221 DisableItem(menu, iCopy);
222 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
223 DisableItem(menu, iPaste);
225 EnableItem(menu, iPaste);
226 DisableItem(menu, iClear);
227 EnableItem(menu, iSelectAll);
230 void mac_clickterm(WindowPtr window, EventRecord *event) {
231 struct mac_session *s;
233 ControlHandle control;
236 s = (struct mac_session *)GetWRefCon(window);
238 mouse = event->where;
239 GlobalToLocal(&mouse);
240 part = FindControl(mouse, window, &control);
241 if (control == s->scrollbar) {
243 case kControlIndicatorPart:
244 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
245 term_scroll(+1, GetControlValue(control));
247 case kControlUpButtonPart:
248 case kControlDownButtonPart:
249 case kControlPageUpPart:
250 case kControlPageDownPart:
251 TrackControl(control, mouse, mac_scrolltracker);
255 text_click(s, event);
259 static void text_click(struct mac_session *s, EventRecord *event) {
262 static UInt32 lastwhen = 0;
263 static struct mac_session *lastsess = NULL;
264 static int lastrow = -1, lastcol = -1;
265 static Mouse_Action lastact = MA_NOTHING;
268 localwhere = event->where;
269 GlobalToLocal(&localwhere);
271 col = PTOCC(localwhere.h);
272 row = PTOCR(localwhere.v);
273 if (event->when - lastwhen < GetDblTime() &&
274 row == lastrow && col == lastcol && s == lastsess)
275 lastact = (lastact == MA_CLICK ? MA_2CLK :
276 lastact == MA_2CLK ? MA_3CLK :
277 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
280 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
285 while (StillDown()) {
286 GetMouse(&localwhere);
287 col = PTOCC(localwhere.h);
288 row = PTOCR(localwhere.v);
289 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
292 term_scroll(0, row - (rows - 1));
296 term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
298 lastwhen = TickCount();
301 void write_clip(void *data, int len) {
304 if (ZeroScrap() != noErr)
306 PutScrap(len, 'TEXT', data);
309 void get_clip(void **p, int *lenp) {
311 /* XXX: do something */
314 static pascal void mac_scrolltracker(ControlHandle control, short part) {
315 struct mac_session *s;
317 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
319 case kControlUpButtonPart:
322 case kControlDownButtonPart:
325 case kControlPageUpPart:
326 term_scroll(0, -(rows - 1));
328 case kControlPageDownPart:
329 term_scroll(0, +(rows - 1));
334 #define K_SPACE 0x3100
348 #define K_INSERT 0x7200
349 #define K_HOME 0x7300
350 #define K_PRIOR 0x7400
351 #define K_DELETE 0x7500
353 #define K_NEXT 0x7900
354 #define K_LEFT 0x7b00
355 #define K_RIGHT 0x7c00
356 #define K_DOWN 0x7d00
368 #define KP_CLEAR 0x4700
369 #define KP_EQUAL 0x5100
370 #define KP_SLASH 0x4b00
371 #define KP_STAR 0x4300
372 #define KP_PLUS 0x4500
373 #define KP_MINUS 0x4e00
374 #define KP_DOT 0x4100
375 #define KP_ENTER 0x4c00
377 void mac_keyterm(WindowPtr window, EventRecord *event) {
378 unsigned char buf[20];
380 struct mac_session *s;
383 s = (struct mac_session *)GetWRefCon(window);
384 len = mac_keytrans(s, event, buf);
385 /* XXX: I can't get the loopback backend to link, so we'll do this: */
386 /* back->send((char *)buf, len); */
387 for (i = 0; i < len; i++)
393 static int mac_keytrans(struct mac_session *s, EventRecord *event,
394 unsigned char *output) {
395 unsigned char *p = output;
398 /* No meta key yet -- that'll be rather fun. */
400 /* Keys that we handle locally */
401 if (event->modifiers & shiftKey) {
402 switch (event->message & keyCodeMask) {
403 case K_PRIOR: /* shift-pageup */
404 term_scroll(0, -(rows - 1));
406 case K_NEXT: /* shift-pagedown */
407 term_scroll(0, +(rows - 1));
413 * Control-2 should return ^@ (0x00), Control-6 should return
414 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
415 * the DOS keyboard handling did it, and we have nothing better
416 * to do with the key combo in question, we'll also map
417 * Control-Backquote to ^\ (0x1C).
420 if (event->modifiers & controlKey) {
421 switch (event->message & charCodeMask) {
438 * First, all the keys that do tilde codes. (ESC '[' nn '~',
439 * for integer decimal nn.)
441 * We also deal with the weird ones here. Linux VCs replace F1
442 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
443 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
447 switch (event->message & keyCodeMask) {
448 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
449 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
450 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
451 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
452 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
453 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
454 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
455 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
456 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
457 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
458 case K_F11: code = 23; break;
459 case K_F12: code = 24; break;
460 case K_HOME: code = 1; break;
461 case K_INSERT: code = 2; break;
462 case K_DELETE: code = 3; break;
463 case K_END: code = 4; break;
464 case K_PRIOR: code = 5; break;
465 case K_NEXT: code = 6; break;
467 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
468 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
471 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
472 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
476 p += sprintf((char *)p, "\x1B[%d~", code);
480 if (app_keypad_keys) {
481 switch (event->message & keyCodeMask) {
482 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
483 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
484 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
485 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
486 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
487 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
488 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
489 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
490 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
491 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
492 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
493 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
494 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
495 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
496 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
497 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
498 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
499 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
503 switch (event->message & keyCodeMask) {
505 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
508 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
511 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
514 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
517 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
520 *p++ = event->message & charCodeMask;
525 void mac_growterm(WindowPtr window, EventRecord *event) {
528 int newrows, newcols;
529 struct mac_session *s;
531 s = (struct mac_session *)GetWRefCon(window);
532 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
533 grow_result = GrowWindow(window, event->where, &limits);
534 if (grow_result != 0) {
535 newrows = HiWord(grow_result) / font_height;
536 newcols = (LoWord(grow_result) - 15) / font_width;
537 mac_adjustsize(s, newrows, newcols);
538 term_size(newrows, newcols, cfg.savelines);
542 void mac_activateterm(WindowPtr window, Boolean active) {
543 struct mac_session *s;
545 s = (struct mac_session *)GetWRefCon(window);
549 ShowControl(s->scrollbar);
551 HideControl(s->scrollbar);
554 void mac_updateterm(WindowPtr window) {
555 struct mac_session *s;
558 s = (struct mac_session *)GetWRefCon(window);
561 (*window->visRgn)->rgnBBox.left,
562 (*window->visRgn)->rgnBBox.top,
563 (*window->visRgn)->rgnBBox.right,
564 (*window->visRgn)->rgnBBox.bottom);
565 /* Restore default colours in case the Window Manager uses them */
568 if (FrontWindow() != window)
569 EraseRect(&(*s->scrollbar)->contrlRect);
570 UpdateControls(window, window->visRgn);
571 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
572 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
574 DrawGrowIcon(window);
575 clip.left = SHRT_MIN;
580 struct do_text_args {
581 struct mac_session *s;
589 * Call from the terminal emulator to draw a bit of text
591 * x and y are text row and column (zero-based)
593 void do_text(struct mac_session *s, int x, int y, char *text, int len,
594 unsigned long attr) {
596 int bgcolour, fgcolour;
597 RGBColor rgbfore, rgbback;
598 struct do_text_args a;
603 /* First check this text is relevant */
604 a.textrect.top = y * font_height;
605 a.textrect.bottom = (y + 1) * font_height;
606 a.textrect.left = x * font_width;
607 a.textrect.right = (x + len) * font_width;
608 if (!RectInRgn(&a.textrect, s->window->visRgn))
616 TextFont(s->fontnum);
617 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
619 if (attr & ATTR_UNDER)
622 TextSize(cfg.fontheight);
623 if (attr & ATTR_REVERSE)
624 TextMode(notSrcCopy);
627 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
629 RectRgn(textrgn, &a.textrect);
630 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
631 /* Tell the window manager about it in case this isn't an update */
633 ValidRect(&a.textrect);
636 static pascal void do_text_for_device(short depth, short devflags,
637 GDHandle device, long cookie) {
638 struct do_text_args *a;
639 int bgcolour, fgcolour;
641 a = (struct do_text_args *)cookie;
645 /* XXX This should be done with a _little_ more configurability */
646 ForeColor(whiteColor);
647 BackColor(blackColor);
650 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
651 PmForeColor(DEFAULT_FG_BOLD);
653 PmForeColor(DEFAULT_FG);
654 if (a->attr & ATTR_ACTCURS)
655 PmBackColor(CURSOR_FG);
657 PmBackColor(DEFAULT_BG);
660 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
661 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
662 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
663 if (a->attr & ATTR_REVERSE)
667 if (a->attr & ATTR_ACTCURS)
668 bgcolour = CURSOR_FG;
669 PmForeColor(fgcolour);
670 PmBackColor(bgcolour);
673 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
674 DrawText(a->text, 0, a->len);
678 * Call from the terminal emulator to get its graphics context.
680 struct mac_session *get_ctx(void) {
686 * Presumably this does something in Windows
688 void free_ctx(struct mac_session *ctx) {
693 * Set the scroll bar position
695 * total is the line number of the bottom of the working screen
696 * start is the line number of the top of the display
697 * page is the length of the displayed page
699 void set_sbar(int total, int start, int page) {
700 struct mac_session *s = onlysession;
702 /* We don't redraw until we've set everything up, to avoid glitches */
703 (*s->scrollbar)->contrlMin = 0;
704 (*s->scrollbar)->contrlMax = total - page;
705 SetControlValue(s->scrollbar, start);
707 /* XXX: This doesn't link for me. */
708 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
709 SetControlViewSize(s->scrollbar, page);
720 * XXX We should indicate the relevant window and/or use the
721 * Notification Manager
726 * Set icon string -- a no-op here (Windowshade?)
728 void set_icon(char *icon) {
733 * Set the window title
735 void set_title(char *title) {
737 struct mac_session *s = onlysession;
739 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
740 SetWTitle(s->window, mactitle);
744 * Resize the window at the emulator's request
746 void request_resize(int w, int h) {
750 mac_initfont(onlysession);
754 * Set the logical palette
756 void palette_set(int n, int r, int g, int b) {
758 struct mac_session *s = onlysession;
759 static const int first[21] = {
760 0, 2, 4, 6, 8, 10, 12, 14,
761 1, 3, 5, 7, 9, 11, 13, 15,
765 if (mac_gestalts.qdvers == gestaltOriginalQD)
767 col.red = r * 0x0101;
768 col.green = g * 0x0101;
769 col.blue = b * 0x0101;
770 SetEntryColor(s->palette, first[n], &col);
772 SetEntryColor(s->palette, first[n]+1, &col);
773 ActivatePalette(s->window);
777 * Reset to the default palette
779 void palette_reset(void) {
780 struct mac_session *s = onlysession;
782 if (mac_gestalts.qdvers == gestaltOriginalQD)
784 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
785 ActivatePalette(s->window);
786 /* Palette Manager will generate update events as required. */
790 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
793 void do_scroll(int topline, int botline, int lines) {
794 struct mac_session *s = onlysession;
799 PmBackColor(DEFAULT_BG);
801 SetRect(&r, 0, topline * font_height,
802 cols * font_width, (botline + 1) * font_height);
803 ScrollRect(&r, 0, - lines * font_height, update);
804 /* XXX: move update region? */
812 * c-file-style: "simon"