1 /* $Id: macterm.c,v 1.1.2.20 1999/03/14 13:08:43 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) {
303 if (ZeroScrap() != noErr)
305 PutScrap(len, 'TEXT', data);
308 void get_clip(void **p, int *lenp) {
310 /* XXX: do something */
313 static pascal void mac_scrolltracker(ControlHandle control, short part) {
314 struct mac_session *s;
316 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
318 case kControlUpButtonPart:
321 case kControlDownButtonPart:
324 case kControlPageUpPart:
325 term_scroll(0, -(rows - 1));
327 case kControlPageDownPart:
328 term_scroll(0, +(rows - 1));
333 #define K_SPACE 0x3100
347 #define K_INSERT 0x7200
348 #define K_HOME 0x7300
349 #define K_PRIOR 0x7400
350 #define K_DELETE 0x7500
352 #define K_NEXT 0x7900
353 #define K_LEFT 0x7b00
354 #define K_RIGHT 0x7c00
355 #define K_DOWN 0x7d00
367 #define KP_CLEAR 0x4700
368 #define KP_EQUAL 0x5100
369 #define KP_SLASH 0x4b00
370 #define KP_STAR 0x4300
371 #define KP_PLUS 0x4500
372 #define KP_MINUS 0x4e00
373 #define KP_DOT 0x4100
374 #define KP_ENTER 0x4c00
376 void mac_keyterm(WindowPtr window, EventRecord *event) {
377 unsigned char buf[20];
379 struct mac_session *s;
382 s = (struct mac_session *)GetWRefCon(window);
383 len = mac_keytrans(s, event, buf);
384 /* XXX: I can't get the loopback backend to link, so we'll do this: */
385 /* back->send((char *)buf, len); */
386 for (i = 0; i < len; i++)
392 static int mac_keytrans(struct mac_session *s, EventRecord *event,
393 unsigned char *output) {
394 unsigned char *p = output;
397 /* No meta key yet -- that'll be rather fun. */
399 /* Keys that we handle locally */
400 if (event->modifiers & shiftKey) {
401 switch (event->message & keyCodeMask) {
402 case K_PRIOR: /* shift-pageup */
403 term_scroll(0, -(rows - 1));
405 case K_NEXT: /* shift-pagedown */
406 term_scroll(0, +(rows - 1));
412 * Control-2 should return ^@ (0x00), Control-6 should return
413 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
414 * the DOS keyboard handling did it, and we have nothing better
415 * to do with the key combo in question, we'll also map
416 * Control-Backquote to ^\ (0x1C).
419 if (event->modifiers & controlKey) {
420 switch (event->message & charCodeMask) {
437 * First, all the keys that do tilde codes. (ESC '[' nn '~',
438 * for integer decimal nn.)
440 * We also deal with the weird ones here. Linux VCs replace F1
441 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
442 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
446 switch (event->message & keyCodeMask) {
447 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
448 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
449 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
450 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
451 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
452 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
453 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
454 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
455 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
456 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
457 case K_F11: code = 23; break;
458 case K_F12: code = 24; break;
459 case K_HOME: code = 1; break;
460 case K_INSERT: code = 2; break;
461 case K_DELETE: code = 3; break;
462 case K_END: code = 4; break;
463 case K_PRIOR: code = 5; break;
464 case K_NEXT: code = 6; break;
466 if (cfg.linux_funkeys && code >= 11 && code <= 15) {
467 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
470 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
471 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
475 p += sprintf((char *)p, "\x1B[%d~", code);
479 if (app_keypad_keys) {
480 switch (event->message & keyCodeMask) {
481 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
482 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
483 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
484 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
485 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
486 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
487 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
488 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
489 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
490 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
491 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
492 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
493 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
494 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
495 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
496 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
497 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
498 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
502 switch (event->message & keyCodeMask) {
504 p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
507 p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
510 p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
513 p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
516 *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
519 *p++ = event->message & charCodeMask;
524 void mac_growterm(WindowPtr window, EventRecord *event) {
527 int newrows, newcols;
528 struct mac_session *s;
530 s = (struct mac_session *)GetWRefCon(window);
531 SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
532 grow_result = GrowWindow(window, event->where, &limits);
533 if (grow_result != 0) {
534 newrows = HiWord(grow_result) / font_height;
535 newcols = (LoWord(grow_result) - 15) / font_width;
536 mac_adjustsize(s, newrows, newcols);
537 term_size(newrows, newcols, cfg.savelines);
541 void mac_activateterm(WindowPtr window, Boolean active) {
542 struct mac_session *s;
544 s = (struct mac_session *)GetWRefCon(window);
548 ShowControl(s->scrollbar);
550 HideControl(s->scrollbar);
553 void mac_updateterm(WindowPtr window) {
554 struct mac_session *s;
557 s = (struct mac_session *)GetWRefCon(window);
560 (*window->visRgn)->rgnBBox.left,
561 (*window->visRgn)->rgnBBox.top,
562 (*window->visRgn)->rgnBBox.right,
563 (*window->visRgn)->rgnBBox.bottom);
564 /* Restore default colours in case the Window Manager uses them */
567 if (FrontWindow() != window)
568 EraseRect(&(*s->scrollbar)->contrlRect);
569 UpdateControls(window, window->visRgn);
570 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
571 SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
573 DrawGrowIcon(window);
574 clip.left = SHRT_MIN;
579 struct do_text_args {
580 struct mac_session *s;
588 * Call from the terminal emulator to draw a bit of text
590 * x and y are text row and column (zero-based)
592 void do_text(struct mac_session *s, int x, int y, char *text, int len,
593 unsigned long attr) {
595 int bgcolour, fgcolour;
596 RGBColor rgbfore, rgbback;
597 struct do_text_args a;
602 /* First check this text is relevant */
603 a.textrect.top = y * font_height;
604 a.textrect.bottom = (y + 1) * font_height;
605 a.textrect.left = x * font_width;
606 a.textrect.right = (x + len) * font_width;
607 if (!RectInRgn(&a.textrect, s->window->visRgn))
615 TextFont(s->fontnum);
616 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
618 if (attr & ATTR_UNDER)
621 TextSize(cfg.fontheight);
622 if (attr & ATTR_REVERSE)
623 TextMode(notSrcCopy);
626 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
628 RectRgn(textrgn, &a.textrect);
629 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
630 /* 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;
640 a = (struct do_text_args *)cookie;
644 /* XXX This should be done with a _little_ more configurability */
645 ForeColor(whiteColor);
646 BackColor(blackColor);
649 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
650 PmForeColor(DEFAULT_FG_BOLD);
652 PmForeColor(DEFAULT_FG);
653 if (a->attr & ATTR_ACTCURS)
654 PmBackColor(CURSOR_FG);
656 PmBackColor(DEFAULT_BG);
659 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
660 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
661 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
662 if (a->attr & ATTR_REVERSE)
666 if (a->attr & ATTR_ACTCURS)
667 bgcolour = CURSOR_FG;
668 PmForeColor(fgcolour);
669 PmBackColor(bgcolour);
672 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
673 DrawText(a->text, 0, a->len);
677 * Call from the terminal emulator to get its graphics context.
679 struct mac_session *get_ctx(void) {
685 * Presumably this does something in Windows
687 void free_ctx(struct mac_session *ctx) {
692 * Set the scroll bar position
694 * total is the line number of the bottom of the working screen
695 * start is the line number of the top of the display
696 * page is the length of the displayed page
698 void set_sbar(int total, int start, int page) {
699 struct mac_session *s = onlysession;
701 /* We don't redraw until we've set everything up, to avoid glitches */
702 (*s->scrollbar)->contrlMin = 0;
703 (*s->scrollbar)->contrlMax = total - page;
704 SetControlValue(s->scrollbar, start);
706 /* XXX: This doesn't link for me. */
707 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
708 SetControlViewSize(s->scrollbar, page);
719 * XXX We should indicate the relevant window and/or use the
720 * Notification Manager
725 * Set icon string -- a no-op here (Windowshade?)
727 void set_icon(char *icon) {
732 * Set the window title
734 void set_title(char *title) {
736 struct mac_session *s = onlysession;
738 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
739 SetWTitle(s->window, mactitle);
743 * Resize the window at the emulator's request
745 void request_resize(int w, int h) {
749 mac_initfont(onlysession);
753 * Set the logical palette
755 void palette_set(int n, int r, int g, int b) {
757 struct mac_session *s = onlysession;
758 static const int first[21] = {
759 0, 2, 4, 6, 8, 10, 12, 14,
760 1, 3, 5, 7, 9, 11, 13, 15,
764 if (mac_gestalts.qdvers == gestaltOriginalQD)
766 col.red = r * 0x0101;
767 col.green = g * 0x0101;
768 col.blue = b * 0x0101;
769 SetEntryColor(s->palette, first[n], &col);
771 SetEntryColor(s->palette, first[n]+1, &col);
772 ActivatePalette(s->window);
776 * Reset to the default palette
778 void palette_reset(void) {
779 struct mac_session *s = onlysession;
781 if (mac_gestalts.qdvers == gestaltOriginalQD)
783 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
784 ActivatePalette(s->window);
785 /* Palette Manager will generate update events as required. */
789 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
792 void do_scroll(int topline, int botline, int lines) {
793 struct mac_session *s = onlysession;
798 PmBackColor(DEFAULT_BG);
800 SetRect(&r, 0, topline * font_height,
801 cols * font_width, (botline + 1) * font_height);
802 ScrollRect(&r, 0, - lines * font_height, update);
803 /* XXX: move update region? */
811 * c-file-style: "simon"