-/* $Id: macterm.c,v 1.1.2.14 1999/03/03 22:03:54 ben Exp $ */
+/* $Id: macterm.c,v 1.1.2.23 1999/03/14 20:43:51 ben Exp $ */
/*
* Copyright (c) 1999 Ben Harris
* All rights reserved.
#include <Quickdraw.h>
#include <QuickdrawText.h>
#include <Resources.h>
+#include <Scrap.h>
#include <Sound.h>
#include <ToolUtils.h>
#include <limits.h>
#include <stdlib.h>
+#include <stdio.h>
#include "macresid.h"
#include "putty.h"
#define DEFAULT_FG_BOLD 17
#define DEFAULT_BG 18
#define DEFAULT_BG_BOLD 19
+#define CURSOR_FG 20
+#define CURSOR_FG_BOLD 21
+#define CURSOR_BG 22
+#define CURSOR_BG_BOLD 23
+
+#define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
+ (x) / font_width)
+#define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
+ (y) / font_height)
struct mac_session {
short fontnum;
static void mac_adjustsize(struct mac_session *, int, int);
static pascal void mac_scrolltracker(ControlHandle, short);
static pascal void do_text_for_device(short, short, GDHandle, long);
+static pascal void mac_set_attr_mask(short, short, GDHandle, long);
+static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
+static void text_click(struct mac_session *, EventRecord *);
/*
* Temporary hack till I get the terminal emulator supporting multiple
void mac_newsession(void) {
struct mac_session *s;
int i;
+ UInt32 starttime;
+ char msg[128];
/* This should obviously be initialised by other means */
mac_loadconfig(&cfg);
+/* back = &loop_backend; */
s = smalloc(sizeof(*s));
onlysession = s;
term_size(cfg.height, cfg.width, cfg.savelines);
mac_initfont(s);
mac_initpalette(s);
+ attr_mask = ATTR_MASK;
/* Set to FALSE to not get palette updates in the background. */
SetPalette(s->window, s->palette, TRUE);
ActivatePalette(s->window);
ShowWindow(s->window);
+ starttime = TickCount();
display_resource('pTST', 128);
+ sprintf(msg, "Elapsed ticks: %d\015\012", TickCount() - starttime);
+ inbuf_putstr(msg);
+ term_out();
}
static void mac_initfont(struct mac_session *s) {
SetWinColor(s->window, cth);
}
+/*
+ * Enable/disable menu items based on the active terminal window.
+ */
+void mac_adjusttermmenus(WindowPtr window) {
+ struct mac_session *s;
+ MenuHandle menu;
+ long offset;
+
+ s = (struct mac_session *)GetWRefCon(window);
+ menu = GetMenuHandle(mEdit);
+ EnableItem(menu, 0);
+ DisableItem(menu, iUndo);
+ DisableItem(menu, iCut);
+ DisableItem(menu, iCopy);
+ if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
+ DisableItem(menu, iPaste);
+ else
+ EnableItem(menu, iPaste);
+ DisableItem(menu, iClear);
+ EnableItem(menu, iSelectAll);
+}
+
void mac_clickterm(WindowPtr window, EventRecord *event) {
struct mac_session *s;
Point mouse;
TrackControl(control, mouse, mac_scrolltracker);
break;
}
+ } else {
+ text_click(s, event);
}
}
+static void text_click(struct mac_session *s, EventRecord *event) {
+ Point localwhere;
+ int row, col;
+ static UInt32 lastwhen = 0;
+ static struct mac_session *lastsess = NULL;
+ static int lastrow = -1, lastcol = -1;
+ static Mouse_Action lastact = MA_NOTHING;
+
+ SetPort(s->window);
+ localwhere = event->where;
+ GlobalToLocal(&localwhere);
+
+ col = PTOCC(localwhere.h);
+ row = PTOCR(localwhere.v);
+ if (event->when - lastwhen < GetDblTime() &&
+ row == lastrow && col == lastcol && s == lastsess)
+ lastact = (lastact == MA_CLICK ? MA_2CLK :
+ lastact == MA_2CLK ? MA_3CLK :
+ lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
+ else
+ lastact = MA_CLICK;
+ term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
+ col, row);
+ lastsess = s;
+ lastrow = row;
+ lastcol = col;
+ while (StillDown()) {
+ GetMouse(&localwhere);
+ col = PTOCC(localwhere.h);
+ row = PTOCR(localwhere.v);
+ term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
+ MA_DRAG, col, row);
+ if (row > rows - 1)
+ term_scroll(0, row - (rows - 1));
+ else if (row < 0)
+ term_scroll(0, row);
+ }
+ term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
+ col, row);
+ lastwhen = TickCount();
+}
+
+void write_clip(void *data, int len) {
+
+ if (ZeroScrap() != noErr)
+ return;
+ PutScrap(len, 'TEXT', data);
+}
+
+void get_clip(void **p, int *lenp) {
+
+ /* XXX: do something */
+}
+
static pascal void mac_scrolltracker(ControlHandle control, short part) {
struct mac_session *s;
}
}
+#define K_SPACE 0x3100
+#define K_BS 0x3300
+#define K_F1 0x7a00
+#define K_F2 0x7800
+#define K_F3 0x6300
+#define K_F4 0x7600
+#define K_F5 0x6000
+#define K_F6 0x6100
+#define K_F7 0x6200
+#define K_F8 0x6400
+#define K_F9 0x6500
+#define K_F10 0x6d00
+#define K_F11 0x6700
+#define K_F12 0x6f00
+#define K_INSERT 0x7200
+#define K_HOME 0x7300
+#define K_PRIOR 0x7400
+#define K_DELETE 0x7500
+#define K_END 0x7700
+#define K_NEXT 0x7900
+#define K_LEFT 0x7b00
+#define K_RIGHT 0x7c00
+#define K_DOWN 0x7d00
+#define K_UP 0x7e00
+#define KP_0 0x5200
+#define KP_1 0x5300
+#define KP_2 0x5400
+#define KP_3 0x5500
+#define KP_4 0x5600
+#define KP_5 0x5700
+#define KP_6 0x5800
+#define KP_7 0x5900
+#define KP_8 0x5b00
+#define KP_9 0x5c00
+#define KP_CLEAR 0x4700
+#define KP_EQUAL 0x5100
+#define KP_SLASH 0x4b00
+#define KP_STAR 0x4300
+#define KP_PLUS 0x4500
+#define KP_MINUS 0x4e00
+#define KP_DOT 0x4100
+#define KP_ENTER 0x4c00
+
+void mac_keyterm(WindowPtr window, EventRecord *event) {
+ unsigned char buf[20];
+ int len;
+ struct mac_session *s;
+ int i;
+
+ s = (struct mac_session *)GetWRefCon(window);
+ len = mac_keytrans(s, event, buf);
+ /* XXX: I can't get the loopback backend to link, so we'll do this: */
+/* back->send((char *)buf, len); */
+ for (i = 0; i < len; i++)
+ inbuf_putc(buf[i]);
+ term_out();
+ term_update();
+}
+
+static int mac_keytrans(struct mac_session *s, EventRecord *event,
+ unsigned char *output) {
+ unsigned char *p = output;
+ int code;
+
+ /* No meta key yet -- that'll be rather fun. */
+
+ /* Keys that we handle locally */
+ if (event->modifiers & shiftKey) {
+ switch (event->message & keyCodeMask) {
+ case K_PRIOR: /* shift-pageup */
+ term_scroll(0, -(rows - 1));
+ return 0;
+ case K_NEXT: /* shift-pagedown */
+ term_scroll(0, +(rows - 1));
+ return 0;
+ }
+ }
+
+ /*
+ * Control-2 should return ^@ (0x00), Control-6 should return
+ * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
+ * the DOS keyboard handling did it, and we have nothing better
+ * to do with the key combo in question, we'll also map
+ * Control-Backquote to ^\ (0x1C).
+ */
+
+ if (event->modifiers & controlKey) {
+ switch (event->message & charCodeMask) {
+ case ' ': case '2':
+ *p++ = 0x00;
+ return p - output;
+ case '`':
+ *p++ = 0x1c;
+ return p - output;
+ case '6':
+ *p++ = 0x1e;
+ return p - output;
+ case '/':
+ *p++ = 0x1f;
+ return p - output;
+ }
+ }
+
+ /*
+ * First, all the keys that do tilde codes. (ESC '[' nn '~',
+ * for integer decimal nn.)
+ *
+ * We also deal with the weird ones here. Linux VCs replace F1
+ * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
+ * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
+ * respectively.
+ */
+ code = 0;
+ switch (event->message & keyCodeMask) {
+ case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
+ case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
+ case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
+ case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
+ case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
+ case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
+ case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
+ case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
+ case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
+ case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
+ case K_F11: code = 23; break;
+ case K_F12: code = 24; break;
+ case K_HOME: code = 1; break;
+ case K_INSERT: code = 2; break;
+ case K_DELETE: code = 3; break;
+ case K_END: code = 4; break;
+ case K_PRIOR: code = 5; break;
+ case K_NEXT: code = 6; break;
+ }
+ if (cfg.linux_funkeys && code >= 11 && code <= 15) {
+ p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
+ return p - output;
+ }
+ if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
+ p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
+ return p - output;
+ }
+ if (code) {
+ p += sprintf((char *)p, "\x1B[%d~", code);
+ return p - output;
+ }
+
+ if (app_keypad_keys) {
+ switch (event->message & keyCodeMask) {
+ case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
+ case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
+ case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
+ case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
+ case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
+ case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
+ case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
+ case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
+ case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
+ case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
+ case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
+ case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
+ case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
+ case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
+ case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
+ case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
+ case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
+ case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
+ }
+ }
+
+ switch (event->message & keyCodeMask) {
+ case K_UP:
+ p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
+ return p - output;
+ case K_DOWN:
+ p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
+ return p - output;
+ case K_RIGHT:
+ p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
+ return p - output;
+ case K_LEFT:
+ p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
+ return p - output;
+ case K_BS:
+ *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
+ return p - output;
+ default:
+ *p++ = event->message & charCodeMask;
+ return p - output;
+ }
+}
+
void mac_growterm(WindowPtr window, EventRecord *event) {
Rect limits;
long grow_result;
struct mac_session *s;
s = (struct mac_session *)GetWRefCon(window);
+ has_focus = active;
+ term_update();
if (active)
ShowControl(s->scrollbar);
- else
+ else {
+ PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
HideControl(s->scrollbar);
+ }
}
void mac_updateterm(WindowPtr window) {
s = (struct mac_session *)GetWRefCon(window);
BeginUpdate(window);
+ get_ctx();
term_paint(s,
(*window->visRgn)->rgnBBox.left,
(*window->visRgn)->rgnBBox.top,
DrawGrowIcon(window);
clip.left = SHRT_MIN;
ClipRect(&clip);
+ free_ctx(NULL);
EndUpdate(window);
}
style |= underline;
TextFace(style);
TextSize(cfg.fontheight);
- if (attr & ATTR_REVERSE)
- TextMode(notSrcCopy);
- else
- TextMode(srcCopy);
SetFractEnable(FALSE); /* We want characters on pixel boundaries */
textrgn = NewRgn();
RectRgn(textrgn, &a.textrect);
DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
- /* Tell the window manager about it in case this isn't an update */
DisposeRgn(textrgn);
+ /* Tell the window manager about it in case this isn't an update */
ValidRect(&a.textrect);
}
static pascal void do_text_for_device(short depth, short devflags,
GDHandle device, long cookie) {
struct do_text_args *a;
- int bgcolour, fgcolour;
+ int bgcolour, fgcolour, bright;
a = (struct do_text_args *)cookie;
+ bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
+
+ TextMode(a->attr & ATTR_REVERSE ? notSrcCopy : srcCopy);
+
switch (depth) {
case 1:
/* XXX This should be done with a _little_ more configurability */
ForeColor(whiteColor);
BackColor(blackColor);
+ if (a->attr & ATTR_ACTCURS)
+ TextMode(a->attr & ATTR_REVERSE ? srcCopy : notSrcCopy);
break;
case 2:
- if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
- PmForeColor(DEFAULT_FG_BOLD);
- else
- PmForeColor(DEFAULT_FG);
- PmBackColor(DEFAULT_BG);
+ if (a->attr & ATTR_ACTCURS) {
+ PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
+ PmBackColor(CURSOR_BG);
+ TextMode(srcCopy);
+ } else {
+ PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
+ PmBackColor(DEFAULT_BG);
+ }
break;
default:
- fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
- bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
- if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
- if (a->attr & ATTR_REVERSE)
- bgcolour++;
- else
- fgcolour++;
+ if (a->attr & ATTR_ACTCURS) {
+ fgcolour = bright ? CURSOR_FG_BOLD : CURSOR_FG;
+ bgcolour = CURSOR_BG;
+ TextMode(srcCopy);
+ } else {
+ fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
+ bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
+ if (bright)
+ if (a->attr & ATTR_REVERSE)
+ bgcolour++;
+ else
+ fgcolour++;
+ }
PmForeColor(fgcolour);
PmBackColor(bgcolour);
break;
}
+
MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
DrawText(a->text, 0, a->len);
+
+ if (a->attr & ATTR_PASCURS) {
+ PenNormal();
+ switch (depth) {
+ case 1:
+ PenMode(patXor);
+ break;
+ default:
+ PmForeColor(CURSOR_BG);
+ break;
+ }
+ FrameRect(&a->textrect);
+ }
}
/*
* Call from the terminal emulator to get its graphics context.
+ * Should probably be called start_redraw or something.
*/
struct mac_session *get_ctx(void) {
+ struct mac_session *s = onlysession;
+
+ attr_mask = ATTR_INVALID;
+ DeviceLoop(s->window->visRgn, mac_set_attr_mask, (long)s, 0);
+ return s;
+}
- return onlysession;
+static pascal void mac_set_attr_mask(short depth, short devflags,
+ GDHandle device, long cookie) {
+
+ switch (depth) {
+ default:
+ attr_mask |= ATTR_FGMASK | ATTR_BGMASK;
+ /* FALLTHROUGH */
+ case 2:
+ attr_mask |= ATTR_BOLD;
+ /* FALLTHROUGH */
+ case 1:
+ attr_mask |= ATTR_UNDER | ATTR_REVERSE | ATTR_ACTCURS |
+ ATTR_PASCURS | ATTR_ASCII | ATTR_GBCHR | ATTR_LINEDRW |
+ (cfg.bold_colour ? 0 : ATTR_BOLD);
+ break;
+ }
}
/*
RgnHandle update;
SetPort(s->window);
+ PmBackColor(DEFAULT_BG);
update = NewRgn();
SetRect(&r, 0, topline * font_height,
cols * font_width, (botline + 1) * font_height);
InvalRgn(update);
DisposeRgn(update);
}
+
+/*
+ * Emacs magic:
+ * Local Variables:
+ * c-file-style: "simon"
+ * End:
+ */