#! /bin/sh -e

# DP: Modified

if [ $# -ne 1 ]; then
    echo >&2 "$0: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch) patch -f -p0 < $0;;
    -unpatch) patch -f -R -p0 < $0;;
    *)
        echo >&2 "$0: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

--- zvt/vt.c	Sat Nov 25 18:49:08 2000
+++ zvt/vt.c	Sat Nov 24 02:01:44 2001
@@ -41,7 +41,7 @@
 #include "vt.h"
 #include "subshell.h"
 
-/* define to 'x' to enable copius debug of this module */
+/* define to 'x' to enable copious debug of this module */
 #define d(x)
 
 /* this one will check nodes aren't 'past' the end of list */
@@ -163,8 +163,8 @@
     }
 
     d({
-      ah = &vt->lines.head;
-      lh = &vt->lines_alt.head;
+      ah = (struct vt_line *)&vt->lines.head;
+      lh = (struct vt_line *)&vt->lines_alt.head;
       while (ah) {
 	printf("%p: %p %p   %p: %p %p\n", ah, ah->next, ah->prev, lh, lh->next, lh->prev);
 	ah = ah->next;
@@ -1373,6 +1373,69 @@
   }
 }
 
+/**
+ * vt_dtterm_seq:
+ * @vt: an initialised &vt_em
+ *
+ * Handle a CSI Ps [; Ps [; Ps]] t request, passing on a request for a report
+ * if a handler has been defined for those sequences requiring a pty response
+ **/
+static void
+vt_dtterm_seq(struct vt_em *vt)
+{
+  int cmd = 0;
+
+  /* fixup: the Ps; Ps; Ps parameters aren't quite loaded the way we expect: *
+   * this makes sure they're all loaded into intargs in order:               *
+   * Note that intargs[0] will be the command number, 1  and 2 are potential *
+   * parameters, which may or may not be used                                */
+  vt->arg.num.intargs[vt->argcnt++] = vt->arg.num.intarg & 0x7fffffff;
+  vt->arg.num.intarg = 0;
+
+  cmd = vt->arg.num.intargs[0];
+  
+  d({
+    int x =  0;
+    printf( "CSI <argcnt:%d state:%d intarg:%d> ", vt->argcnt, vt->state, vt->arg.num.intarg );
+    for(x = 0; x < MIN(VTPARAM_INTARGS,vt->argcnt); x++ )
+      printf("[%d]", vt->arg.num.intargs[x] );
+    printf("t\n");
+   });
+  
+  switch (cmd) {
+  /* These manipulate the window */
+  case 1: /* deiconify */
+  case 2: /* iconify */
+  case 3: /* move-to */
+  case 4: /* resize (pixels) */
+  case 5: /* raise */
+  case 6: /* lower */
+  case 7: /* refresh */
+  case 8: /* resize (chars) */
+  case 9: /* (un)maximise based on parameter */
+  /* These require report-responses: */
+  case 11: /* 11: report iconifiedness as CS1 1|2 t */
+  case 13: /* 13: report position as CSI 3; x; y t (pixels) */
+  case 14: /* 14: report size as CSI 4 ; h ; w t pixels */
+  case 18: /* 18: report size as CSI 8 ; h ; w t chars */
+  case 19: /* 19: report screen size as CSI 9 ; h ; w t chars */
+  case 20: /* 20: report icon label as OSC L label ST */
+  case 21: /* 21: report window title as OSC l title ST */
+    d(printf("vt->dtterm_seq<%p>(<%p>, %d)\n",
+             vt->dtterm_seq,
+             vt->user_data, cmd));
+    if(vt->dtterm_seq) { vt->dtterm_seq(vt->user_data); }
+    break;
+  default:
+    if (cmd >= 24) {
+      d(printf("CSI %d: resize to %d lines\n", cmd, cmd));
+      if(vt->dtterm_seq) { vt->dtterm_seq(vt->user_data); }  
+    } else {
+      d(printf("Unknown dtterm (CSI %d [; Ps [; Ps ]]t) sequence\n", cmd));
+    }
+  }
+}
+
 struct vt_jump {
   void (*process)(struct vt_em *vt);	/* process function */
   int modes;			/* modes appropriate */
@@ -1420,7 +1483,7 @@
   {vt_modeh,VT_EBL}, {0,VT_LIT}, {0,VT_LIT}, {vt_modek,VT_EBL},	/* hijk */
   {vt_model,VT_EBL}, {vt_mode,VT_EBL}, {vt_dsr,VT_EBL}, {0,VT_LIT},	/* lmno */
   {vt_reset,VT_EBL}, {0,VT_LIT}, {vt_scroll,VT_EBL}, {0,VT_LIT},	/* pqrs */
-  {0,VT_LIT}, {0,VT_LIT}, {0,VT_LIT}, {0,VT_LIT},	/* tuvw */
+  {vt_dtterm_seq,VT_LIT}, {0,VT_LIT}, {0,VT_LIT}, {0,VT_LIT},	/* tuvw */
   {0,VT_LIT}, {0,VT_LIT}, {0,VT_LIT}, {0,VT_LIT},	/* xyz{ */
   {0,VT_LIT}, {vt_decic,VT_EBL}, {vt_func,VT_EBL}, {0,VT_LIT},	/* |}~? */
 };
@@ -1445,6 +1508,8 @@
   char *ptr_end;
   void (*process)(struct vt_em *vt);	/* process function */
 
+#define IS_ST(C,P,E) ( ((C) == 0x1b) && ((P)<(E)) && (*((P)++) == 0x5c) )
+  
   /* states:
    *   0: normal escape mode
    *   1: escape mode.  switch on next character.
@@ -1591,12 +1656,20 @@
 	state = 8;
 	continue;
       }
+      d(printf("FALLTHROUGH 2: VT_ARG: %d, VT_EXB: %d, VT_CON: %d, c: %c\n",
+               ((mode & VT_ARG) ? 1 : 0  ),
+               ((mode & VT_EXB) ? 1 : 0  ),
+               ((mode & VT_CON) ? 1 : 0  ),
+               ( isprint(c)     ? c : '_')
+              )
+       );
       /* note -> falls through */
     case 6:
     case 7:
     case 8:
     case 9:
     case 10:
+      d(printf("mode '%c' -> #%d", c, c & 0x7f));
       if (mode & VT_ARG) {
 	/* accumulate subtotal */
 	vt->arg.num.intarg = vt->arg.num.intarg*10+(c-'0');
@@ -1616,6 +1689,9 @@
 	state = 7;		/* any other chars should be numbers anyway */
       } else if (c=='\'') {
 	state = 9;
+      } else if (c=='t')  {     /* dtterm window manipulation sequence */
+        process(vt);
+	state = 0;
       } else {
 	d(printf("unknown option '%c'\n", c));
 	state=0;		/* unexpected escape sequence - ignore */
@@ -1633,9 +1709,9 @@
       break;
       
       
-      /* \E]..;...BEL */
+      /* '\E]..;...BEL' OR '\E]..;...ST' */
     case 4:
-      if (c==0x07) {
+      if (c==0x07 || IS_ST(c,ptr,ptr_end)) {
 	/* handle output */
 	*(vt->arg.txt.outptr)=0;
 	vt_set_text(vt);

--- zvt/vt.h	Fri Oct 29 19:35:49 1999
+++ zvt/vt.h	Fri Nov 23 02:44:15 2001
@@ -152,6 +152,7 @@
 				   old lines are discarded */
   void (*ring_my_bell)(void *user_data);	/* ring my bell ... */
   void (*change_my_name)(void *user_data, VTTITLE_TYPE type, char *name);	/* ring my bell ... */
+  void (*dtterm_seq)(void *user_data);
 
   void *user_data;		/* opaque external data handle for callbacks */
 
