From 07e23b8401b37c45052293bcc7119ce07542069f Mon Sep 17 00:00:00 2001
From: marco <marco@freepascal.org>
Date: Sat, 6 Apr 2013 18:58:05 +0000
Subject: [PATCH]  * mousebutton 4 and 5 support (gpm only?)  Mantis #13469

git-svn-id: trunk@24164 -
---
 rtl/inc/mouseh.inc   |  2 ++
 rtl/unix/keyboard.pp | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/rtl/inc/mouseh.inc b/rtl/inc/mouseh.inc
index a0455a68d7..1678856dd7 100644
--- a/rtl/inc/mouseh.inc
+++ b/rtl/inc/mouseh.inc
@@ -33,6 +33,8 @@ const
   MouseLeftButton   = $01;                         { Left mouse button }
   MouseRightButton  = $02;                         { Right mouse button }
   MouseMiddleButton = $04;                         { Middle mouse button }
+  MouseButton4      = $08;
+  MouseButton5      = $10;
 
   MouseEventBufSize = 16;                { Size of event queue }
 
diff --git a/rtl/unix/keyboard.pp b/rtl/unix/keyboard.pp
index d34215d071..2bd093ad9d 100644
--- a/rtl/unix/keyboard.pp
+++ b/rtl/unix/keyboard.pp
@@ -491,6 +491,13 @@ const
     Action : 0;
   );
 
+  procedure GenFakeReleaseEvent(MouseEvent : TMouseEvent);
+  begin
+    MouseEvent.action := MouseActionUp;
+    MouseEvent.buttons := 0;
+    PutMouseEvent(MouseEvent);
+  end;
+  
   procedure GenMouseEvent;
   var MouseEvent: TMouseEvent;
       ch : char;
@@ -510,7 +517,7 @@ const
      bit  5   : mouse movement while button down.
      bit  6   : interpret button 1 as button 4
                 interpret button 2 as button 5}
-    case buttonval and 3 of
+    case buttonval and 67 of
       0 : {left button press}
         MouseEvent.buttons:=1;
       1 : {middle button pressed }
@@ -519,6 +526,10 @@ const
         MouseEvent.buttons:=4;
       3 : { no button pressed }
         MouseEvent.buttons:=0;
+      64: { button 4 pressed }
+          MouseEvent.buttons:=8;
+      65: { button 5 pressed }
+          MouseEvent.buttons:=16;
     end;
      if inhead=intail then
        fpSelect(StdInputHandle+1,@fdsin,nil,nil,10);
@@ -552,6 +563,8 @@ const
        end;
 *)
      PutMouseEvent(MouseEvent);
+     if (MouseEvent.buttons and (8+16)) <> 0 then // 'M' escape sequence cannot map button 4&5 release, so fake one.
+       GenFakeReleaseEvent(MouseEvent);
 {$ifdef DebugMouse}
      if MouseEvent.Action=MouseActionDown then
        Write(system.stderr,'Button down : ')