diff --git a/src/ansi.c b/src/ansi.c
index 04c9012..deea103 100644
--- a/src/ansi.c
+++ b/src/ansi.c
@@ -193,6 +193,7 @@ register struct win *p;
   p->w_insert = 0;
   p->w_revvid = 0;
   p->w_mouse = 0;
+  p->w_mouse_encoding = 0;
   p->w_curinv = 0;
   p->w_curvvis = 0;
   p->w_autolf = 0;
@@ -1466,6 +1467,11 @@ int c, intermediate;
 	      curr->w_mouse = i ? a1 : 0;
 	      LMouseMode(&curr->w_layer, curr->w_mouse);
 	      break;
+	    case 1006:	/* sgr mouse*/
+	      curr->w_mouse_encoding = i ? a1 : 0;
+	      LMouseEncoding(&curr->w_layer, curr->w_mouse_encoding);
+	      break;
+	 /* case 1015:	   urxvt mouse */
 	    }
 	}
       break;
diff --git a/src/display.c b/src/display.c
index 61fff7d..f859c79 100644
--- a/src/display.c
+++ b/src/display.c
@@ -183,6 +183,7 @@ DefRestore()
   LCursorkeysMode(flayer, 0);
   LCursorVisibility(flayer, 0);
   LMouseMode(flayer, 0);
+  LMouseEncoding(flayer, 0);
   LSetRendition(flayer, &mchar_null);
   LSetFlow(flayer, nwin_default.flowflag & FLOW_NOW);
 }
@@ -836,6 +837,35 @@ int mode;
     }
 }
 
+void
+MouseEncoding(mode)
+int mode;
+{
+  if (!display)
+    return;
+
+  if (mode < D_mousetrack)
+    mode = D_mousetrack;
+
+  if (D_mouseencoding != mode)
+    {
+      char mousebuf[20];
+      if (!D_CXT)
+	return;
+      if (D_mouseencoding)
+	{
+	  sprintf(mousebuf, "\033[?%dl", D_mouseencoding);
+	  AddStr(mousebuf);
+	}
+      if (mode)
+	{
+	  sprintf(mousebuf, "\033[?%dh", mode);
+	  AddStr(mousebuf);
+	}
+      D_mouseencoding = mode;
+    }
+}
+
 static int StrCost;
 
 /* ARGSUSED */
@@ -1261,6 +1291,7 @@ int cur_only;
   CursorkeysMode(0);
   CursorVisibility(0);
   MouseMode(0);
+  MouseEncoding(0);
   SetRendition(&mchar_null);
   SetFlow(FLOW_NOW);
 
@@ -3129,6 +3160,7 @@ NukePending()
   int oldkeypad = D_keypad, oldcursorkeys = D_cursorkeys;
   int oldcurvis = D_curvis;
   int oldmouse = D_mouse;
+  int oldmouseencoding = D_mouseencoding;
 
   oldrend = D_rend;
   len = D_obufp - D_obuf;
@@ -3191,6 +3223,7 @@ NukePending()
   CursorkeysMode(oldcursorkeys);
   CursorVisibility(oldcurvis);
   MouseMode(oldmouse);
+  MouseEncoding(oldmouseencoding);
   if (D_CWS)
     {
       debug("ResizeDisplay: using WS\n");
@@ -3415,20 +3448,89 @@ char *data;
   if (D_mouse && D_forecv)
     {
       unsigned char *bp = (unsigned char *)buf;
-      int x, y, i = size;
+      int x, y, i = size, j, k;
 
       /* XXX this assumes that the string is read in as a whole... */
       for (i = size; i > 0; i--, bp++)
 	{
-	  if (i > 5 && bp[0] == 033 && bp[1] == '[' && bp[2] == 'M')
+	  if (bp[0] == 033 && bp[1] == '[' && bp[2] == 'M')
+	    {
+	      /* 7bit Normal encoding */
+	      bp++;
+	      i--;
+	    }
+	  else if (bp[0] == 033 && bp[1] == '[' && bp[2] == '<')
 	    {
+	      /* 7bit SGR 1006 encoding */
 	      bp++;
 	      i--;
 	    }
-	  else if (i < 5 || bp[0] != 0233 || bp[1] != 'M')
+	  else if (bp[0] == 0233 && bp[1] == 'M')
+	    {
+	      /* 8bit Normal encoding */
+	    }
+	  else if (bp[0] == 0233 && bp[1] == '<')
+	    {
+	      /* 8bit SGR 1006 encoding */
+	    }
+	  else
 	    continue;
-	  x = bp[3] - 33;
-	  y = bp[4] - 33;
+
+	  if (bp[1] == 'M') {
+	    /* Normal encoding:
+	     *
+	     *   CSI M Cb Cx Cy
+	     *   (Cb, Cx, Cy as byte.)
+	     *
+	     * This coordinate values (Cx, Cy) are added origin offset +1
+	     * and base offset +32 (to make it printable)
+	     */
+	    x = bp[3] - 33;
+	    y = bp[4] - 33;
+	  } else if (bp[1] == '<') {
+	    /* SGR 1006 encoding:
+	     *
+	     *   CSI M Db ; Dx ; Dy M/m
+	     *   (Db, Dx, Dy as decimal formatted strings.)
+	     *
+	     * The coordinate values (Dx, Dy) are added origin offset +1
+	     */
+	    i--;
+	    if (i <= 0)
+	      break;
+	    /* read first parameter (Db) */
+	    for (j = 2; i > 0 && '0' <= bp[j] && bp[j] <= '9'; j++, i--)
+	        ;
+	    if (i <= 0)
+	      break;
+	    /* recognize separator */
+	    if (bp[j++] != ';')
+	      continue;
+	    i--;
+	    if (i <= 0)
+	      break;
+	    /* read second parameter (Dx) */
+	    for (x = 0; i > 0 && '0' <= bp[j] && bp[j] <= '9'; j++, i--)
+	      x = x * 10 + bp[j] - '0';
+	    /* prevent integer overflow */
+	    if (x < 0)
+	      break;
+	    if (i <= 0)
+	      break;
+	    /* recognize separator */
+	    if (bp[j++] != ';')
+	      continue;
+	    i--;
+	    if (i <= 0)
+	      break;
+	    /* read third parameter (Dy) */
+	    for (y = 0; i > 0 && '0' <= bp[j] && bp[j] <= '9'; j++, i--)
+	      y = y * 10 + bp[j] - '0';
+	    /* prevent integer overflow */
+	    if (y < 0)
+	      break;
+	  }
+
 	  if (x >= D_forecv->c_xs && x <= D_forecv->c_xe && y >= D_forecv->c_ys && y <= D_forecv->c_ye)
 	    {
 	      if ((D_fore && D_fore->w_mouse) || (D_mousetrack && D_forecv->c_layer->l_mode == 1))
@@ -3438,15 +3540,36 @@ char *data;
 		  y -= D_forecv->c_yoff;
 		  if (x >= 0 && x < D_forecv->c_layer->l_width && y >= 0 && y < D_forecv->c_layer->l_height)
 		    {
-		      bp[3] = x + 33;
-		      bp[4] = y + 33;
-		      i -= 4;
-		      bp += 4;
+		      if (bp[1] == 'M')
+			{
+			  bp[3] = x + 33;
+			  bp[4] = y + 33;
+			  i -= 4;
+			  bp += 4;
+			}
+		      else if (bp[1] == '<')
+			{
+			  k = j;
+			  while (bp[--k] != ';')
+			    {
+			      bp[k] = y % 10 + '0';
+			      y /= 10;
+			    }
+			  while (bp[--k] != ';')
+			    {
+			      bp[k] = x % 10 + '0';
+			      x /= 10;
+			    }
+			  i -= j;
+			  bp += j;
+			}
 		      continue;
 		    }
 		}
 	    }
-	  else if (D_mousetrack && bp[2] == '#')
+	  else if (D_mousetrack && (
+		      (bp[1] == 'M' && bp[2] == '#') || /* Normal encoding */
+		      (bp[1] == '<' && bp[j] == 'm')))  /* SGR 1006 encoding */
 	    {
 	      /* 'focus' to the clicked region, only on mouse up */
 	      struct canvas *cv = FindCanvas(x, y);
@@ -3462,11 +3585,23 @@ char *data;
 	      bp--;
 	      size--;
 	    }
-	  if (i > 5)
-	    bcopy((char *)bp + 5, (char *)bp, i - 5);
-	  bp--;
-	  i -= 4;
-	  size -= 5;
+
+	  if (bp[1] == 'M')
+	    {
+	      if (i > 5)
+		bcopy((char *)bp + 5, (char *)bp, i - 5);
+	      bp--;
+	      i -= 4;
+	      size -= 5;
+	    }
+	  else if (bp[1] == '<')
+	    {
+	      if (i > j + 1)
+		bcopy((char *)bp + j + 1, (char *)bp, i - j - 1);
+	      bp--;
+	      i -= j;
+	      size -= j + 1;
+	    }
 	}
     }
 #ifdef ENCODINGS
diff --git a/src/display.h b/src/display.h
index e8b3b80..80877a5 100644
--- a/src/display.h
+++ b/src/display.h
@@ -100,6 +100,7 @@ struct display
   int	d_hstatus;		/* hardstatus used */
   int	d_lp_missing;		/* last character on bot line missing */
   int   d_mouse;		/* mouse mode */
+  int   d_mouseencoding;	/* mouse encoding */
   int	d_mousetrack;		/* set when user wants to use mouse even when the window
 				   does not */
 #ifdef RXVT_OSC
@@ -227,6 +228,7 @@ extern struct display TheDisplay;
 #define D_hstatus	DISPLAY(d_hstatus)
 #define D_lp_missing	DISPLAY(d_lp_missing)
 #define D_mouse		DISPLAY(d_mouse)
+#define D_mouseencoding	DISPLAY(d_mouseencoding)
 #define D_mousetrack	DISPLAY(d_mousetrack)
 #define D_xtermosc	DISPLAY(d_xtermosc)
 #define D_lpchar	DISPLAY(d_lpchar)
diff --git a/src/layer.c b/src/layer.c
index acf91d8..1c3e997 100644
--- a/src/layer.c
+++ b/src/layer.c
@@ -912,6 +912,23 @@ int on;
 }
 
 void
+LMouseEncoding(l, on)
+struct layer *l;
+int on;
+{
+  struct canvas *cv;
+  for (cv = l->l_cvlist; cv; cv = cv->c_lnext)
+    {
+      display = cv->c_display;
+      if (D_blocked)
+	continue;
+      if (cv != D_forecv)
+	continue;
+      MouseEncoding(on);
+    }
+}
+
+void
 LClearAll(l, uself)
 struct layer *l;
 int uself;
diff --git a/src/window.c b/src/window.c
index 0023065..6e506d9 100644
--- a/src/window.c
+++ b/src/window.c
@@ -502,6 +502,7 @@ WinRestore()
       ReverseVideo(fore->w_revvid);
       CursorVisibility(fore->w_curinv ? -1 : fore->w_curvvis);
       MouseMode(fore->w_mouse);
+      MouseEncoding(fore->w_mouse_encoding);
     }
 }
 
diff --git a/src/window.h b/src/window.h
index 7311ecb..11ce0a6 100644
--- a/src/window.h
+++ b/src/window.h
@@ -235,6 +235,7 @@ struct win
   char	 w_xtermosc[4][MAXSTR];	/* special xterm/rxvt escapes */
 #endif
   int    w_mouse;		/* mouse mode 0,9,1000 */
+  int    w_mouse_encoding;	/* mouse encoding 0,1006,1015 */
 #ifdef HAVE_BRAILLE
   int	 w_bd_x, w_bd_y;	/* Braille cursor position */
 #endif
