Index: src/apps/terminal/TermParse.cpp
===================================================================
--- src/apps/terminal/TermParse.cpp	(revision 30246)
+++ src/apps/terminal/TermParse.cpp	(working copy)
@@ -1109,10 +1109,28 @@
 			// Use Alternate Screen Buffer.
 			fBuffer->UseAlternateScreenBuffer(false);
 			break;
+		case 1000:
+			// TODO
+			// Send Mouse X & Y on button press and release.
+			break;
+		case 1003:
+			// Use All Motion Mouse Tracking
+			fBuffer->ReportAnyMouseEvent(true);
+			break;		
 		case 1034:
 			// Interpret "meta" key, sets eighth bit.
 			// Not supported yet.
 			break;
+		case 1036:
+			// TODO
+			// Send ESC when Meta modifies a key
+			// Not supported yet.
+			break;
+		case 1039:
+			// TODO
+			// Send ESC when Alt modifies a key
+			// Not supported yet.
+			break;			
 		case 1049:
 			// Save cursor as in DECSC and use Alternate Screen Buffer, clearing
 			// it first.
@@ -1155,10 +1173,29 @@
 			// Use Normal Screen Buffer.
 			fBuffer->UseNormalScreenBuffer();
 			break;
+		case 1000:
+			// TODO
+			// Don’t send Mouse X & Y on button press and release.
+			break;
+		case 1003:
+			// TODO
+			fBuffer->ReportAnyMouseEvent(false);
+			// Don’t use All Motion Mouse Tracking.
+			break;
 		case 1034:
 			// Don’t interpret "meta" key.
 			// Not supported yet.
 			break;
+		case 1036:
+			// TODO
+			// Don’t send ESC when Meta modifies a key
+			// Not supported yet.
+			break;
+		case 1039:
+			// TODO
+			// Don’t send ESC when Alt modifies a key
+			// Not supported yet.
+			break;
 		case 1049:
 			// Use Normal Screen Buffer and restore cursor as in DECRC.
 			fBuffer->UseNormalScreenBuffer();
Index: src/apps/terminal/TermView.cpp
===================================================================
--- src/apps/terminal/TermView.cpp	(revision 30246)
+++ src/apps/terminal/TermView.cpp	(working copy)
@@ -170,7 +170,8 @@
 	fColumns(COLUMNS_DEFAULT),
 	fRows(ROWS_DEFAULT),
 	fEncoding(M_UTF8),
-	fScrBufSize(historySize)
+	fScrBufSize(historySize),
+	fReportAnyMouseEvent(false)
 {
 	_InitObject(argc, argv);
 }
@@ -183,7 +184,8 @@
 	fColumns(columns),
 	fRows(rows),
 	fEncoding(M_UTF8),
-	fScrBufSize(historySize)
+	fScrBufSize(historySize),
+	fReportAnyMouseEvent(false)
 {
 	_InitObject(argc, argv);
 	SetTermSize(fRows, fColumns, true);
@@ -207,7 +209,8 @@
 	fColumns(COLUMNS_DEFAULT),
 	fRows(ROWS_DEFAULT),
 	fEncoding(M_UTF8),
-	fScrBufSize(1000)
+	fScrBufSize(1000),
+	fReportAnyMouseEvent(false)
 {
 	// We need this
 	SetFlags(Flags() | B_WILL_DRAW | B_PULSE_NEEDED);
@@ -277,6 +280,7 @@
 	fMouseTracking = false;
 	fIMflag = false;
 	fCheckMouseTracking = false;
+	fReportAnyMouseEvent = false;
 
 	fTextBuffer = new(std::nothrow) TerminalBuffer;
 	if (fTextBuffer == NULL)
@@ -1517,7 +1521,13 @@
 				SetTitle(title);
 			break;
 		}
-
+		case MSG_REPORT_ANY_MOUSE_EVENT:
+		{
+			bool reportAnyMouseEvent;
+			if (msg->FindBool("reportAnyMouseEvent", &reportAnyMouseEvent) == B_OK)
+				fReportAnyMouseEvent = reportAnyMouseEvent;
+			break;
+		}
 		case MSG_REMOVE_RESIZE_VIEW_IF_NEEDED:
 		{
 			BPoint point;
@@ -2024,96 +2034,135 @@
 
 
 void
+TermView::_SendMouseEvent(int32 buttons, int32 mode, int32 x, int32 y, 
+	bool motion)
+{
+	char destBuffer[5];
+	char xterm_buttons;
+	char xterm_x, xterm_y;
+
+	if (buttons == B_PRIMARY_MOUSE_BUTTON) {
+		xterm_buttons = 32 + 0;
+ 	} else if (buttons == B_SECONDARY_MOUSE_BUTTON) {
+		xterm_buttons = 32 + 1;
+	} else if (buttons == B_TERTIARY_MOUSE_BUTTON) {
+		xterm_buttons = 32 + 2;
+	} else {
+		xterm_buttons = 32 + 3;
+	}
+
+	if (motion) {
+		xterm_buttons += 32;
+	}
+	
+	xterm_x = x + 1 + 32;
+	xterm_y = y + 1 + 32;
+	 
+	destBuffer[0] = '\033';
+	destBuffer[1] = '[';
+	destBuffer[2] = 'M';
+	destBuffer[3] = xterm_buttons;
+	destBuffer[4] = xterm_x;
+	destBuffer[5] = xterm_y;
+	fShell->Write(destBuffer, 6);
+}
+
+
+void
 TermView::MouseDown(BPoint where)
 {
 	if (!IsFocus())
 		MakeFocus();
 
-	int32 buttons;
+	int32 buttons, mod;
 	Window()->CurrentMessage()->FindInt32("buttons", &buttons);
+	Window()->CurrentMessage()->FindInt32("modifiers", &mod);
 
 	fMouseButtons = buttons;
 
-	// paste button
-	if ((buttons & (B_SECONDARY_MOUSE_BUTTON | B_TERTIARY_MOUSE_BUTTON)) != 0) {
-		Paste(gMouseClipboard);
-		fLastClickPoint = where;
-		return;
-	}
+	if (fReportAnyMouseEvent) {
+  		TermPos clickPos = _ConvertToTerminal(where);
+  		_SendMouseEvent(buttons, mod, clickPos.x, clickPos.y, false);
+	} else {
+		// paste button
+		if ((buttons & (B_SECONDARY_MOUSE_BUTTON | B_TERTIARY_MOUSE_BUTTON)) != 0) {
+			Paste(gMouseClipboard);
+			fLastClickPoint = where;
+			return;
+		}
 
-	// Select Region
-	if (buttons == B_PRIMARY_MOUSE_BUTTON) {
-		int32 mod, clicks;
-		Window()->CurrentMessage()->FindInt32("modifiers", &mod);
-		Window()->CurrentMessage()->FindInt32("clicks", &clicks);
+		// Select Region
+		if (buttons == B_PRIMARY_MOUSE_BUTTON) {
+			int32 clicks;
+			Window()->CurrentMessage()->FindInt32("clicks", &clicks);
 
-		if (_HasSelection()) {
-			TermPos inPos = _ConvertToTerminal(where);
-			if (_CheckSelectedRegion(inPos)) {
-				if (mod & B_CONTROL_KEY) {
-					BPoint p;
-					uint32 bt;
-					do {
-						GetMouse(&p, &bt);
+			if (_HasSelection()) {
+				TermPos inPos = _ConvertToTerminal(where);
+				if (_CheckSelectedRegion(inPos)) {
+					if (mod & B_CONTROL_KEY) {
+						BPoint p;
+						uint32 bt;
+						do {
+							GetMouse(&p, &bt);
 
-						if (bt == 0) {
-							_Deselect();
-							return;
-						}
+							if (bt == 0) {
+								_Deselect();
+								return;
+							}
 
-						snooze(40000);
+							snooze(40000);
 
-					} while (abs((int)(where.x - p.x)) < 4
-						&& abs((int)(where.y - p.y)) < 4);
+						} while (abs((int)(where.x - p.x)) < 4
+							&& abs((int)(where.y - p.y)) < 4);
 
-					InitiateDrag();
-					return;
+						InitiateDrag();
+						return;
+					}
 				}
 			}
-		}
 
-		// If mouse has moved too much, disable double/triple click.
-		if (_MouseDistanceSinceLastClick(where) > 8)
-			clicks = 1;
+			// If mouse has moved too much, disable double/triple click.
+			if (_MouseDistanceSinceLastClick(where) > 8)
+				clicks = 1;
 
-		SetMouseEventMask(B_POINTER_EVENTS | B_KEYBOARD_EVENTS,
-			B_NO_POINTER_HISTORY | B_LOCK_WINDOW_FOCUS);
+			SetMouseEventMask(B_POINTER_EVENTS | B_KEYBOARD_EVENTS,
+				B_NO_POINTER_HISTORY | B_LOCK_WINDOW_FOCUS);
 
-		TermPos clickPos = _ConvertToTerminal(where);
+			TermPos clickPos = _ConvertToTerminal(where);
 
-		if (mod & B_SHIFT_KEY) {
-			fInitialSelectionStart = clickPos;
-			fInitialSelectionEnd = clickPos;
-			_ExtendSelection(fInitialSelectionStart, true, false);
-		} else {
-			_Deselect();
-			fInitialSelectionStart = clickPos;
-			fInitialSelectionEnd = clickPos;
-		}
+			if (mod & B_SHIFT_KEY) {
+				fInitialSelectionStart = clickPos;
+				fInitialSelectionEnd = clickPos;
+				_ExtendSelection(fInitialSelectionStart, true, false);
+			} else {
+				_Deselect();
+				fInitialSelectionStart = clickPos;
+				fInitialSelectionEnd = clickPos;
+			}
 
-		// If clicks larger than 3, reset mouse click counter.
-		clicks = (clicks - 1) % 3 + 1;
+			// If clicks larger than 3, reset mouse click counter.
+			clicks = (clicks - 1) % 3 + 1;
 
-		switch (clicks) {
-			case 1:
-				fCheckMouseTracking = true;
-				fSelectGranularity = SELECT_CHARS;
-	      		break;
+			switch (clicks) {
+				case 1:
+					fCheckMouseTracking = true;
+					fSelectGranularity = SELECT_CHARS;
+		      		break;
 
-			case 2:
-				_SelectWord(where, (mod & B_SHIFT_KEY) != 0, false);
-				fMouseTracking = true;
-				fSelectGranularity = SELECT_WORDS;
-				break;
+				case 2:
+					_SelectWord(where, (mod & B_SHIFT_KEY) != 0, false);
+					fMouseTracking = true;
+					fSelectGranularity = SELECT_WORDS;
+					break;
 
-			case 3:
-	 			_SelectLine(where, (mod & B_SHIFT_KEY) != 0, false);
-				fMouseTracking = true;
-				fSelectGranularity = SELECT_LINES;
-				break;
-		}
-  	}
-
+				case 3:
+		 			_SelectLine(where, (mod & B_SHIFT_KEY) != 0, false);
+					fMouseTracking = true;
+					fSelectGranularity = SELECT_LINES;
+					break;
+			}
+	  	}
+	}
 	fLastClickPoint = where;
 }
 
@@ -2121,63 +2170,72 @@
 void
 TermView::MouseMoved(BPoint where, uint32 transit, const BMessage *message)
 {
-	if (fCheckMouseTracking) {
-		if (_MouseDistanceSinceLastClick(where) > 9)
-			fMouseTracking = true;
-	}
-	if (!fMouseTracking)
-		return;
+	if (fReportAnyMouseEvent) {
+		int32 mod;
+		Window()->CurrentMessage()->FindInt32("modifiers", &mod);
+		
+  		TermPos clickPos = _ConvertToTerminal(where);
+  		_SendMouseEvent(fMouseButtons, mod, clickPos.x, clickPos.y, true);
+	} else {
+	
+		if (fCheckMouseTracking) {
+			if (_MouseDistanceSinceLastClick(where) > 9)
+				fMouseTracking = true;
+		}
+		if (!fMouseTracking)
+			return;
 
-	bool doAutoScroll = false;
+		bool doAutoScroll = false;
 
-	if (where.y < 0) {
-		doAutoScroll = true;
-		fAutoScrollSpeed = where.y;
-		where.x = 0;
-		where.y = 0;
-	}
+		if (where.y < 0) {
+			doAutoScroll = true;
+			fAutoScrollSpeed = where.y;
+			where.x = 0;
+			where.y = 0;
+		}
 
-	BRect bounds(Bounds());
-	if (where.y > bounds.bottom) {
-		doAutoScroll = true;
-		fAutoScrollSpeed = where.y - bounds.bottom;
-		where.x = bounds.right;
-		where.y = bounds.bottom;
-	}
+		BRect bounds(Bounds());
+		if (where.y > bounds.bottom) {
+			doAutoScroll = true;
+			fAutoScrollSpeed = where.y - bounds.bottom;
+			where.x = bounds.right;
+			where.y = bounds.bottom;
+		}
 
-	if (doAutoScroll) {
-		if (fAutoScrollRunner == NULL) {
-			BMessage message(kAutoScroll);
-			fAutoScrollRunner = new (std::nothrow) BMessageRunner(
-				BMessenger(this), &message, 10000);
+		if (doAutoScroll) {
+			if (fAutoScrollRunner == NULL) {
+				BMessage message(kAutoScroll);
+				fAutoScrollRunner = new (std::nothrow) BMessageRunner(
+					BMessenger(this), &message, 10000);
+			}
+		} else {
+			delete fAutoScrollRunner;
+			fAutoScrollRunner = NULL;
 		}
-	} else {
-		delete fAutoScrollRunner;
-		fAutoScrollRunner = NULL;
-	}
 
-	switch (fSelectGranularity) {
-		case SELECT_CHARS:
-		{
-			// If we just start selecting, we first select the initially
-			// hit char, so that we get a proper initial selection -- the char
-			// in question, which will thus always be selected, regardless of
-			// whether selecting forward or backward.
-			if (fInitialSelectionStart == fInitialSelectionEnd) {
-				_Select(fInitialSelectionStart, fInitialSelectionEnd, true,
-					true);
+		switch (fSelectGranularity) {
+			case SELECT_CHARS:
+			{
+				// If we just start selecting, we first select the initially
+				// hit char, so that we get a proper initial selection -- the char
+				// in question, which will thus always be selected, regardless of
+				// whether selecting forward or backward.
+				if (fInitialSelectionStart == fInitialSelectionEnd) {
+					_Select(fInitialSelectionStart, fInitialSelectionEnd, true,
+						true);
+				}
+
+				_ExtendSelection(_ConvertToTerminal(where), true, true);
+	      		break;
 			}
-
-			_ExtendSelection(_ConvertToTerminal(where), true, true);
-      		break;
+			case SELECT_WORDS:
+				_SelectWord(where, true, true);
+	      		break;
+			case SELECT_LINES:
+				_SelectLine(where, true, true);
+	      		break;
 		}
-		case SELECT_WORDS:
-			_SelectWord(where, true, true);
-      		break;
-		case SELECT_LINES:
-			_SelectLine(where, true, true);
-      		break;
-	  }
+	}
 }
 
 
@@ -2196,11 +2254,17 @@
 	// clipboard.
 	int32 buttons;
 	Window()->CurrentMessage()->FindInt32("buttons", &buttons);
-	if ((buttons & B_PRIMARY_MOUSE_BUTTON) == 0
-		&& (fMouseButtons & B_PRIMARY_MOUSE_BUTTON) != 0) {
-		Copy(gMouseClipboard);
+
+	if (fReportAnyMouseEvent) {
+	  	TermPos clickPos = _ConvertToTerminal(where);
+	  	_SendMouseEvent(0, 0, clickPos.x, clickPos.y, false);
+	} else {
+		if ((buttons & B_PRIMARY_MOUSE_BUTTON) == 0
+			&& (fMouseButtons & B_PRIMARY_MOUSE_BUTTON) != 0) {
+			Copy(gMouseClipboard);
+		}
+		
 	}
-
 	fMouseButtons = buttons;
 }
 
Index: src/apps/terminal/TerminalBuffer.h
===================================================================
--- src/apps/terminal/TerminalBuffer.h	(revision 30246)
+++ src/apps/terminal/TerminalBuffer.h	(working copy)
@@ -34,6 +34,8 @@
 
 			void				UseAlternateScreenBuffer(bool clear);
 			void				UseNormalScreenBuffer();
+			
+			void				ReportAnyMouseEvent(bool reportAnyMouseEvent);
 
 protected:
 	virtual	void				NotifyListener();
Index: src/apps/terminal/TerminalBuffer.cpp
===================================================================
--- src/apps/terminal/TerminalBuffer.cpp	(revision 30246)
+++ src/apps/terminal/TerminalBuffer.cpp	(working copy)
@@ -75,6 +75,17 @@
 
 
 void
+TerminalBuffer::ReportAnyMouseEvent(bool reportAnyMouseEvent)
+{
+	if (fListenerValid) {
+		BMessage message(MSG_REPORT_ANY_MOUSE_EVENT);
+		message.AddBool("reportAnyMouseEvent", reportAnyMouseEvent);
+		fListener.SendMessage(&message);
+	}	
+}
+
+
+void
 TerminalBuffer::SetEncoding(int encoding)
 {
 	fEncoding = encoding;
Index: src/apps/terminal/TermView.h
===================================================================
--- src/apps/terminal/TermView.h	(revision 30246)
+++ src/apps/terminal/TermView.h	(working copy)
@@ -177,6 +177,8 @@
 			void			_ScrollTo(float y, bool scrollGfx);
 			void			_ScrollToRange(TermPos start, TermPos end);
 
+			void			_SendMouseEvent(int32 button, int32 mode, int32 x, 
+								int32 y, bool motion);
 private:
 	class CharClassifier;
 
@@ -251,6 +253,8 @@
 			bool			fCheckMouseTracking;
 			int				fSelectGranularity;
 			BPoint			fLastClickPoint;
+			
+			bool			fReportAnyMouseEvent;
 
 			// Input Method parameter.
 			int				fIMViewPtr;
Index: src/apps/terminal/TermConst.h
===================================================================
--- src/apps/terminal/TermConst.h	(revision 30246)
+++ src/apps/terminal/TermConst.h	(working copy)
@@ -91,6 +91,7 @@
 const uint32 MSG_TERMINAL_BUFFER_CHANGED		= 'bufc';
 const uint32 MSG_SET_TERMNAL_TITLE				= 'sett';
 const uint32 MSG_QUIT_TERMNAL					= 'qutt';
+const uint32 MSG_REPORT_ANY_MOUSE_EVENT			= 'mous';
 
 // Preference Read/Write Keys
 const char* const PREF_HALF_FONT_FAMILY = "Half Font Family";
