Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saitoha/2875648 to your computer and use it in GitHub Desktop.
Save saitoha/2875648 to your computer and use it in GitHub Desktop.
Poderosa urxvt/SGR style mouse reporting patch for Poderosa 4.3.8b (https://sourceforge.net/projects/poderosa/)
Index: TerminalEmulator/XTerm.cs
===================================================================
RCS file: /cvsroot/poderosa/src/TerminalEmulator/XTerm.cs,v
retrieving revision 1.19
diff -u -r1.19 XTerm.cs
--- TerminalEmulator/XTerm.cs 27 May 2012 15:22:50 -0000 1.19
+++ TerminalEmulator/XTerm.cs 5 Jun 2012 01:05:55 -0000
@@ -29,6 +29,14 @@
Normal,
Drag,
Any,
+ }
+
+ private enum MouseTrackingProtocol
+ {
+ Normal,
+ Utf8,
+ Urxvt,
+ Sgr,
}
private bool _gotEscape;
@@ -43,7 +51,7 @@
private readonly int[] _xtermSavedCol = new int[2]; // { main, alternate }
private MouseTrackingState _mouseTrackingState = MouseTrackingState.Off;
- private bool _extendedMouseTracking = false;
+ private MouseTrackingProtocol _mouseTrackingProtocol = MouseTrackingProtocol.Normal;
private int _prevMouseRow = -1;
private int _prevMouseCol = -1;
private MouseButtons _mouseButton = MouseButtons.None;
@@ -99,9 +107,9 @@
// Note: from here, return value must be true even if nothing has been processed actually.
- bool isExtend = _extendedMouseTracking; // copy value because _extendMouseTracking may be changed in another thread.
+ MouseTrackingProtocol protocol = _mouseTrackingProtocol; // copy value because _mouseTrackingProtocol may be changed in another thread.
- int posLimit = isExtend ? MOUSE_POS_EXT_LIMIT : MOUSE_POS_LIMIT;
+ int posLimit = protocol == MouseTrackingProtocol.Normal ? MOUSE_POS_LIMIT : MOUSE_POS_EXT_LIMIT;
if (row < 0)
row = 0;
@@ -138,8 +146,22 @@
case TerminalMouseAction.ButtonUp:
if (button != _mouseButton)
- return true; // ignore
- statBits = 0x03;
+ return true; // ignore
+
+ switch (button) {
+ case MouseButtons.Left:
+ statBits = 0x00;
+ break;
+ case MouseButtons.Middle:
+ statBits = 0x01;
+ break;
+ case MouseButtons.Right:
+ statBits = 0x02;
+ break;
+ default:
+ return true; // unsupported button
+ }
+
_mouseButton = MouseButtons.None;
break;
@@ -193,40 +215,90 @@
statBits += 0x20;
_prevMouseRow = row;
- _prevMouseCol = col;
+ _prevMouseCol = col;
+
+ byte[] data;
+ int dataIndex;
+
+ switch (protocol) {
+
+ case MouseTrackingProtocol.Normal:
+ if (action == TerminalMouseAction.ButtonUp)
+ statBits |= 0x03;
+ data = new byte[8] {
+ (byte)27, // ESCAPE
+ (byte)91, // [
+ (byte)77, // M
+ (byte)statBits,
+ 0,0,0,0,
+ };
+
+ dataIndex = 4;
+
+ if (col == posLimit) // emulate xterm's bug
+ data[dataIndex++] = (byte)0;
+ else
+ data[dataIndex++] = (byte)(col + (1 + 0x20)); // column 0 --> send as 1
+
+ if (row == posLimit) // emulate xterm's bug
+ data[dataIndex++] = (byte)0;
+ else
+ data[dataIndex++] = (byte)(row + (1 + 0x20)); // row 0 --> send as 1
+ TransmitDirect(data, 0, dataIndex);
+ break;
+
+ case MouseTrackingProtocol.Utf8:
+ if (action == TerminalMouseAction.ButtonUp)
+ statBits |= 0x03;
+ data = new byte[8] {
+ (byte)27, // ESCAPE
+ (byte)91, // [
+ (byte)77, // M
+ (byte)statBits,
+ 0,0,0,0,
+ };
+
+ dataIndex = 4;
+
+ if (col < MOUSE_POS_EXT_START)
+ data[dataIndex++] = (byte)(col + (1 + 0x20)); // column 0 --> send as 1
+ else { // encode in UTF-8
+ int val = col + 1 + 0x20;
+ data[dataIndex++] = (byte)(0xc0 + (val >> 6));
+ data[dataIndex++] = (byte)(0x80 + (val & 0x3f));
+ }
- byte[] data = new byte[8] {
- (byte)27, // ESCAPE
- (byte)91, // [
- (byte)77, // M
- (byte)statBits,
- 0,0,0,0,
- };
-
- int dataIndex = 4;
-
- if (col == posLimit) // emulate xterm's bug
- data[dataIndex++] = (byte)0;
- else if (!isExtend || col < MOUSE_POS_EXT_START)
- data[dataIndex++] = (byte)(col + (1 + 0x20)); // column 0 --> send as 1
- else { // encode in UTF-8
- int val = col + 1 + 0x20;
- data[dataIndex++] = (byte)(0xc0 + (val >> 6));
- data[dataIndex++] = (byte)(0x80 + (val & 0x3f));
- }
-
- if (row == posLimit) // emulate xterm's bug
- data[dataIndex++] = (byte)0;
- else if (!isExtend || row < MOUSE_POS_EXT_START)
- data[dataIndex++] = (byte)(row + (1 + 0x20)); // row 0 --> send as 1
- else { // encode in UTF-8
- int val = row + (1 + 0x20);
- data[dataIndex++] = (byte)(0xc0 + (val >> 6));
- data[dataIndex++] = (byte)(0x80 + (val & 0x3f));
+ if (row < MOUSE_POS_EXT_START)
+ data[dataIndex++] = (byte)(row + (1 + 0x20)); // row 0 --> send as 1
+ else { // encode in UTF-8
+ int val = row + (1 + 0x20);
+ data[dataIndex++] = (byte)(0xc0 + (val >> 6));
+ data[dataIndex++] = (byte)(0x80 + (val & 0x3f));
+ }
+ TransmitDirect(data, 0, dataIndex);
+ break;
+
+ case MouseTrackingProtocol.Urxvt:
+ if (action == TerminalMouseAction.ButtonUp)
+ statBits |= 0x03;
+ data = System.Text.Encoding.ASCII.GetBytes(
+ string.Format("\x1b[{0};{1};{2}M\x0", statBits, col, row));
+ TransmitDirect(data, 0, data.Length);
+ break;
+
+ case MouseTrackingProtocol.Sgr:
+ if (action == TerminalMouseAction.ButtonUp) {
+ data = System.Text.Encoding.ASCII.GetBytes(
+ string.Format("\x1b[<{0};{1};{2}m", statBits - 0x20, col, row));
+ }
+ else {
+ data = System.Text.Encoding.ASCII.GetBytes(
+ string.Format("\x1b[<{0};{1};{2}M", statBits - 0x20, col, row));
+ }
+ TransmitDirect(data, 0, data.Length);
+ break;
}
- TransmitDirect(data, 0, dataIndex);
-
return true;
}
@@ -563,10 +635,31 @@
case "1004": // Send FocusIn/FocusOut events
// Not supported
ResetMouseTracking(MouseTrackingState.Off);
- return ProcessCharResult.Processed;
- case "1005": // Enable Extended Mouse Mode
- _extendedMouseTracking = set;
- return ProcessCharResult.Processed;
+ return ProcessCharResult.Processed;
+ case "1005": // Enable UTF8 Mouse Mode
+ if (set) {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Utf8;
+ }
+ else {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Normal;
+ }
+ return ProcessCharResult.Processed;
+ case "1006": // Enable SGR Extended Mouse Mode
+ if (set) {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Sgr;
+ }
+ else {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Normal;
+ }
+ return ProcessCharResult.Processed;
+ case "1015": // Enable UTF8 Extended Mouse Mode
+ if (set) {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Urxvt;
+ }
+ else {
+ _mouseTrackingProtocol = MouseTrackingProtocol.Normal;
+ }
+ return ProcessCharResult.Processed;
case "1034": // Input 8 bits
return ProcessCharResult.Processed;
case "3": //132 Column Mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment