Re-engineering of terminal emulator, phase 1.
[u/mdw/putty] / mac / macterm.c
index fbf9412..1ff64ac 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macterm.c,v 1.69 2003/02/07 01:38:12 ben Exp $ */
+/* $Id: macterm.c,v 1.77 2004/10/13 11:50:16 simon Exp $ */
 /*
  * Copyright (c) 1999 Simon Tatham
  * Copyright (c) 1999, 2002 Ben Harris
@@ -90,13 +90,22 @@ static pascal void mac_growtermdraghook(void);
 static pascal void mac_scrolltracker(ControlHandle, short);
 static pascal void do_text_for_device(short, short, GDHandle, long);
 static void text_click(Session *, EventRecord *);
+static void mac_activateterm(WindowPtr, EventRecord *);
+static void mac_adjusttermcursor(WindowPtr, Point, RgnHandle);
+static void mac_adjusttermmenus(WindowPtr);
+static void mac_updateterm(WindowPtr);
+static void mac_clickterm(WindowPtr, EventRecord *);
+static void mac_growterm(WindowPtr, EventRecord *);
+static void mac_keyterm(WindowPtr, EventRecord *);
+static void mac_menuterm(WindowPtr, short, short);
+static void mac_closeterm(WindowPtr);
 
 void pre_paint(Session *s);
 void post_paint(Session *s);
 
 void mac_startsession(Session *s)
 {
-    char *errmsg;
+    const char *errmsg;
     int i;
     WinInfo *wi;
 
@@ -120,9 +129,19 @@ void mac_startsession(Session *s)
        s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
     else
        s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
-    wi = smalloc(sizeof(*wi));
+    wi = snew(WinInfo);
+    memset(wi, 0, sizeof(*wi));
     wi->s = s;
     wi->wtype = wTerminal;
+    wi->activate = &mac_activateterm;
+    wi->adjustcursor = &mac_adjusttermcursor;
+    wi->adjustmenus = &mac_adjusttermmenus;
+    wi->update = &mac_updateterm;
+    wi->click = &mac_clickterm;
+    wi->grow = &mac_growterm;
+    wi->key = &mac_keyterm;
+    wi->menu = &mac_menuterm;
+    wi->close = &mac_closeterm;
     SetWRefCon(s->window, (long)wi);
     s->scrollbar = GetNewControl(cVScroll, s->window);
     s->term = term_init(&s->cfg, &s->ucsdata, s);
@@ -138,8 +157,9 @@ void mac_startsession(Session *s)
     s->logctx = log_init(s->term, &s->cfg);
     term_provide_logctx(s->term, s->logctx);
 
-    errmsg = s->back->init(s->term, &s->backhandle, &s->cfg, s->cfg.host,
-                          s->cfg.port, &s->realhost, s->cfg.tcp_nodelay);
+    errmsg = s->back->init(s, &s->backhandle, &s->cfg, s->cfg.host,
+                          s->cfg.port, &s->realhost, s->cfg.tcp_nodelay,
+                          s->cfg.tcp_keepalives);
     if (errmsg != NULL)
        fatalbox("%s", errmsg);
     s->back->provide_logctx(s->backhandle, s->logctx);
@@ -174,9 +194,9 @@ static void mac_workoutfontscale(Session *s, int wantwidth,
     const char text = 'W';
     FontInfo fi;
 #if TARGET_API_MAC_CARBON
-    CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(s->window));;
+    CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(s->window));
 #else
-    QDProcsPtr gp = s->window->grafProcs;;
+    QDProcsPtr gp = s->window->grafProcs;
 #endif
 
     numer.v = denom.v = 1; /* always */
@@ -203,7 +223,8 @@ static void mac_workoutfontscale(Session *s, int wantwidth,
 
 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
 
-static void mac_initfont(Session *s) {
+static void mac_initfont(Session *s)
+{
     FontInfo fi;
     TextEncoding enc;
     OptionBits fbflags;
@@ -317,7 +338,8 @@ static void mac_adjustsize(Session *s, int newrows, int newcols) {
     mac_drawgrowicon(s);
 }
 
-static void mac_initpalette(Session *s) {
+static void mac_initpalette(Session *s)
+{
 
     if (!HAVE_COLOR_QD())
        return;
@@ -348,7 +370,8 @@ static void mac_initpalette(Session *s) {
  * Set the background colour of the window correctly.  Should be
  * called whenever the default background changes.
  */
-static void mac_adjustwinbg(Session *s) {
+static void mac_adjustwinbg(Session *s)
+{
 
     if (!HAVE_COLOR_QD())
        return;
@@ -377,7 +400,9 @@ static void mac_adjustwinbg(Session *s) {
 /*
  * Set the cursor shape correctly
  */
-void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
+static void mac_adjusttermcursor(WindowPtr window, Point mouse,
+                                RgnHandle cursrgn)
+{
     Session *s;
     ControlHandle control;
     short part;
@@ -432,7 +457,8 @@ void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
 #define DisableItem DisableMenuItem
 #define EnableItem EnableMenuItem
 #endif
-void mac_adjusttermmenus(WindowPtr window) {
+static void mac_adjusttermmenus(WindowPtr window)
+{
     Session *s;
     MenuHandle menu;
 #if !TARGET_API_MAC_CARBON
@@ -467,7 +493,8 @@ void mac_adjusttermmenus(WindowPtr window) {
     EnableItem(menu, iShowEventLog);
 }
 
-void mac_menuterm(WindowPtr window, short menu, short item) {
+static void mac_menuterm(WindowPtr window, short menu, short item)
+{
     Session *s;
 
     s = mac_windowsession(window);
@@ -492,7 +519,8 @@ void mac_menuterm(WindowPtr window, short menu, short item) {
     }
 }
            
-void mac_clickterm(WindowPtr window, EventRecord *event) {
+static void mac_clickterm(WindowPtr window, EventRecord *event)
+{
     Session *s;
     Point mouse;
     ControlHandle control;
@@ -525,7 +553,8 @@ void mac_clickterm(WindowPtr window, EventRecord *event) {
     }
 }
 
-static void text_click(Session *s, EventRecord *event) {
+static void text_click(Session *s, EventRecord *event)
+{
     Point localwhere;
     int row, col;
     static UInt32 lastwhen = 0;
@@ -601,7 +630,7 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
        if (data[i] == 0x2028)
            data[i] = 0x000d;
 
-    mactextbuf = smalloc(len); /* XXX DBCS */
+    mactextbuf = snewn(len, char); /* XXX DBCS */
     if (s->uni_to_font != NULL) {
        err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
                                       (UniChar *)data,
@@ -636,7 +665,8 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
 #endif
 }
 
-void get_clip(void *frontend, wchar_t **p, int *lenp) {
+void get_clip(void *frontend, wchar_t **p, int *lenp)
+{
 #if TARGET_API_MAC_CARBON
     *lenp = 0;
 #else
@@ -673,7 +703,7 @@ void get_clip(void *frontend, wchar_t **p, int *lenp) {
            texth = NewHandle(0);
            textlen = GetScrap(texth, kScrapFlavorTypeText, &offset);
            HLock(texth);
-           data = smalloc(textlen * 2);
+           data = snewn(textlen, wchar_t);
            /* XXX should use 'styl' scrap if it's there. */
            if (mac_gestalts.encvvers != 0 &&
                UpgradeScriptInfoToTextEncoding(smSystemScript,
@@ -716,7 +746,8 @@ void get_clip(void *frontend, wchar_t **p, int *lenp) {
 #endif
 }
 
-static pascal void mac_scrolltracker(ControlHandle control, short part) {
+static pascal void mac_scrolltracker(ControlHandle control, short part)
+{
     Session *s;
 
 #if TARGET_API_MAC_CARBON
@@ -740,7 +771,8 @@ static pascal void mac_scrolltracker(ControlHandle control, short part) {
     }
 }
 
-void mac_keyterm(WindowPtr window, EventRecord *event) {
+static void mac_keyterm(WindowPtr window, EventRecord *event)
+{
     Session *s = mac_windowsession(window);
     Key_Sym keysym = PK_NULL;
     unsigned int mods = 0, flags = PKF_NUMLOCK;
@@ -900,7 +932,8 @@ static struct {
     char oldmsg[20];
 } growterm_state;
 
-void mac_growterm(WindowPtr window, EventRecord *event) {
+static void mac_growterm(WindowPtr window, EventRecord *event)
+{
     Rect limits;
     long grow_result;
     int newrows, newcols;
@@ -1004,8 +1037,10 @@ void mac_closeterm(WindowPtr window)
     sfree(s);
 }
 
-void mac_activateterm(WindowPtr window, Boolean active) {
+static void mac_activateterm(WindowPtr window, EventRecord *event)
+{
     Session *s;
+    Boolean active = (event->modifiers & activeFlag) != 0;
 
     s = mac_windowsession(window);
     s->term->has_focus = active;
@@ -1022,7 +1057,8 @@ void mac_activateterm(WindowPtr window, Boolean active) {
     mac_drawgrowicon(s);
 }
 
-void mac_updateterm(WindowPtr window) {
+static void mac_updateterm(WindowPtr window)
+{
     Session *s;
     Rect bbox;
 #if TARGET_API_MAC_CARBON
@@ -1064,7 +1100,8 @@ void mac_updateterm(WindowPtr window) {
     EndUpdate(window);
 }
 
-static void mac_drawgrowicon(Session *s) {
+static void mac_drawgrowicon(Session *s)
+{
     Rect clip;
     RgnHandle savergn;
 
@@ -1102,8 +1139,9 @@ struct do_text_args {
  *
  * x and y are text row and column (zero-based)
  */
-void do_text(Context ctx, int x, int y, char *text, int len,
-            unsigned long attr, int lattr) {
+void do_text(Context ctx, int x, int y, wchar_t *text, int len,
+            unsigned long attr, int lattr)
+{
     Session *s = ctx;
     int style;
     struct do_text_args a;
@@ -1112,7 +1150,6 @@ void do_text(Context ctx, int x, int y, char *text, int len,
     RgnHandle visrgn;
 #endif
     char mactextbuf[1024];
-    UniChar unitextbuf[1024];
     wchar_t *unitextptr;
     int i, fontwidth;
     ByteCount iread, olen;
@@ -1147,20 +1184,16 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        return;
 #endif
 
-    /* Unpack Unicode from the mad format we get passed */
-    for (i = 0; i < len; i++)
-       unitextbuf[i] = (unsigned char)text[i] | (attr & CSET_MASK);
-
     if (s->uni_to_font != NULL) {
        err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
-                                      unitextbuf, kUnicodeUseFallbacksMask,
+                                      text, kUnicodeUseFallbacksMask,
                                       0, NULL, NULL, NULL,
                                       1024, &iread, &olen, mactextbuf);
        if (err != noErr && err != kTECUsedFallbacksStatus)
            olen = 0;
     } else  if (s->font_charset != CS_NONE) {
        /* XXX this is bogus if wchar_t and UniChar are different sizes. */
-       unitextptr = (wchar_t *)unitextbuf;
+       unitextptr = (wchar_t *)text;
        olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
                                    s->font_charset, NULL, ".", 1);
     } else
@@ -1230,7 +1263,8 @@ void do_text(Context ctx, int x, int y, char *text, int len,
 }
 
 static pascal void do_text_for_device(short depth, short devflags,
-                                     GDHandle device, long cookie) {
+                                     GDHandle device, long cookie)
+{
     struct do_text_args *a = (struct do_text_args *)cookie;
     int bgcolour, fgcolour, bright, reverse, tmp;
 #if TARGET_API_MAC_CARBON
@@ -1328,7 +1362,8 @@ void do_cursor(Context ctx, int x, int y, char *text, int len,
  * Call from the terminal emulator to get its graphics context.
  * Should probably be called start_redraw or something.
  */
-void pre_paint(Session *s) {
+void pre_paint(Session *s)
+{
     GDHandle gdh;
     Rect myrect, tmprect;
 #if TARGET_API_MAC_CARBON
@@ -1374,21 +1409,24 @@ void pre_paint(Session *s) {
                                (s->cfg.bold_colour ? ATTR_BOLD : 0));
 }
 
-Context get_ctx(void *frontend) {
+Context get_ctx(void *frontend)
+{
     Session *s = frontend;
 
     pre_paint(s);
     return s;
 }
 
-void free_ctx(Context ctx) {
+void free_ctx(Context ctx)
+{
 
 }
 
 /*
  * Presumably this does something in Windows
  */
-void post_paint(Session *s) {
+void post_paint(Session *s)
+{
 
 }
 
@@ -1399,7 +1437,8 @@ void post_paint(Session *s) {
  * start is the line number of the top of the display
  * page is the length of the displayed page
  */
-void set_sbar(void *frontend, int total, int start, int page) {
+void set_sbar(void *frontend, int total, int start, int page)
+{
     Session *s = frontend;
 
     /* We don't redraw until we've set everything up, to avoid glitches */
@@ -1455,7 +1494,8 @@ void set_icon(void *frontend, char *icon) {
 /*
  * Set the window title
  */
-void set_title(void *frontend, char *title) {
+void set_title(void *frontend, char *title)
+{
     Session *s = frontend;
     Str255 mactitle;
 
@@ -1477,9 +1517,24 @@ void set_raw_mouse_mode(void *frontend, int activate)
 /*
  * Resize the window at the emulator's request
  */
-void request_resize(void *frontend, int w, int h) {
+void request_resize(void *frontend, int w, int h)
+{
     Session *s = frontend;
+    RgnHandle grayrgn;
+    Rect graybox;
+    int wlim, hlim;
 
+    /* Arbitrarily clip to the size of the desktop. */
+    grayrgn = GetGrayRgn();
+#if TARGET_API_MAC_CARBON
+    GetRegionBounds(grayrgn, &graybox);
+#else
+    graybox = (*grayrgn)->rgnBBox;
+#endif
+    wlim = (graybox.right - graybox.left) / s->font_width;
+    hlim = (graybox.bottom - graybox.top) / s->font_height;
+    if (w > wlim) w = wlim;
+    if (h > hlim) h = hlim;
     term_size(s->term, h, w, s->cfg.savelines);
     mac_initfont(s);
 }
@@ -1631,7 +1686,8 @@ static void real_palette_set(Session *s, int n, int r, int g, int b)
 /*
  * Set the logical palette.  Called by the terminal emulator.
  */
-void palette_set(void *frontend, int n, int r, int g, int b) {
+void palette_set(void *frontend, int n, int r, int g, int b)
+{
     Session *s = frontend;
     static const int first[21] = {
        0, 2, 4, 6, 8, 10, 12, 14,
@@ -1652,7 +1708,8 @@ void palette_set(void *frontend, int n, int r, int g, int b) {
 /*
  * Reset to the default palette
  */
-void palette_reset(void *frontend) {
+void palette_reset(void *frontend)
+{
     Session *s = frontend;
     /* This maps colour indices in cfg to those used in our palette. */
     static const int ww[] = {
@@ -1682,7 +1739,8 @@ void palette_reset(void *frontend) {
  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
  * for backward.)
  */
-void do_scroll(Context ctx, int topline, int botline, int lines) {
+void do_scroll(Context ctx, int topline, int botline, int lines)
+{
     Session *s = ctx;
     Rect r;
     RgnHandle scrollrgn = NewRgn();
@@ -1785,6 +1843,13 @@ int askappend(void *frontend, Filename filename)
     return 2;
 }
 
+int from_backend(void *frontend, int is_stderr, const char *data, int len)
+{
+    Session *s = frontend;
+
+    return term_data(s->term, is_stderr, data, len);
+}
+
 /*
  * Emacs magic:
  * Local Variables: