changeset 4936:d59e99cf611f

7124382: [macosx] Property sun.awt.enableExtraMouseButtons is always 'false' Reviewed-by: art
author kizune
date Thu, 02 Feb 2012 22:27:04 +0300
parents be57a1294c05
children d56728a282c6
files src/macosx/classes/sun/lwawt/LWWindowPeer.java src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java src/macosx/classes/sun/lwawt/macosx/CRobot.java src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java src/macosx/classes/sun/lwawt/macosx/event/NSEvent.java src/macosx/native/sun/awt/AWTEvent.m src/macosx/native/sun/awt/CRobot.m src/macosx/native/sun/awt/LWCToolkit.h src/macosx/native/sun/awt/LWCToolkit.m
diffstat 9 files changed, 177 insertions(+), 203 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu Feb 02 22:27:04 2012 +0300
@@ -96,11 +96,6 @@
     // depending on what mouse button is being dragged according to Cocoa
     private static LWComponentPeer mouseDownTarget[] = new LWComponentPeer[3];
 
-    private static final int ButtonsDownMask = InputEvent.BUTTON1_DOWN_MASK |
-                                               InputEvent.BUTTON2_DOWN_MASK |
-                                               InputEvent.BUTTON3_DOWN_MASK;
-
-
     // A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
     // on MOUSE_RELEASE. Click events are only generated if there were no drag
     // events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
@@ -711,13 +706,12 @@
             }
             // TODO: fill "bdata" member of AWTEvent
 
-            // For pressed/dragged/released events JDK 1.6 treats other
+            int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
+            int otherButtonsPressed = modifiers & ~eventButtonMask;
+
+            // For pressed/dragged/released events OS X treats other
             // mouse buttons as if they were BUTTON2, so we do the same
-            int eventButtonMask = (button == 0) ? MouseEvent.BUTTON2_DOWN_MASK :
-                                                  MouseEvent.getMaskForButton(button);
-            int mouseButtonsPressed = modifiers & ButtonsDownMask;
-            int otherButtonsPressed = mouseButtonsPressed & ~eventButtonMask;
-            int targetIdx = (button == 0) ? MouseEvent.BUTTON2 - 1 : button - 1;
+            int targetIdx = (button > 3) ? MouseEvent.BUTTON2 - 1 : button - 1;
 
             // MOUSE_ENTERED/EXITED are generated for the components strictly under
             // mouse even when dragging. That's why we first update lastMouseEventPeer
@@ -743,7 +737,7 @@
                 // button is being dragged. Use it to determine the peer that
                 // should receive the dragged event.
                 targetPeer = mouseDownTarget[targetIdx];
-                mouseClickButtons &= ~mouseButtonsPressed;
+                mouseClickButtons &= ~modifiers;
             } else if (id == MouseEvent.MOUSE_RELEASED) {
                 // TODO: currently, mouse released event goes to the same component
                 // that received corresponding mouse pressed event. For most cases,
@@ -752,7 +746,7 @@
                 // hidden/removed in between of mouse pressed/released events.
                 targetPeer = mouseDownTarget[targetIdx];
 
-                if ((mouseButtonsPressed & eventButtonMask) == 0) {
+                if ((modifiers & eventButtonMask) == 0) {
                     mouseDownTarget[targetIdx] = null;
                 }
 
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java	Thu Feb 02 22:27:04 2012 +0300
@@ -25,9 +25,10 @@
 
 package sun.lwawt.macosx;
 
+import sun.awt.SunToolkit;
 import sun.lwawt.LWWindowPeer;
 import sun.lwawt.macosx.event.NSEvent;
-
+import java.awt.Toolkit;
 import java.awt.event.MouseEvent;
 import java.awt.event.InputEvent;
 import java.awt.event.MouseWheelEvent;
@@ -52,6 +53,12 @@
     public void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
                                  int clickCount, int x, int y, int absoluteX, int absoluteY)
     {
+        final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
+        if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
+                || buttonNumber > tk.getNumberOfButtons() - 1) {
+            return;
+        }
+
         int jeventType = isNpapiCallback ? NSEvent.npEventTypeToJavaEventType(eventType) :
                                            NSEvent.nsEventTypeToJavaEventType(eventType);
 
--- a/src/macosx/classes/sun/lwawt/macosx/CRobot.java	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/macosx/CRobot.java	Thu Feb 02 22:27:04 2012 +0300
@@ -26,30 +26,21 @@
 package sun.lwawt.macosx;
 
 import java.awt.*;
-import java.awt.event.*;
 import java.awt.peer.*;
 
 import sun.awt.CGraphicsDevice;
 
 class CRobot implements RobotPeer {
-
     private static final int MOUSE_LOCATION_UNKNOWN      = -1;
-    private static final int BUTTON_STATE_UNKNOWN        = 0;
-    private static final int BUTTON_STATE_DOWN           = 1;
-    private static final int BUTTON_STATE_UP             = 2;
-    private static final int MODIFIER_PREVIOUSLY_UNKNOWN = 0;
-    private static final int MODIFIER_PREVIOUSLY_UP      = 1;
-    private static final int MODIFIER_PREVIOUSLY_DOWN    = 2;
 
     private final CGraphicsDevice fDevice;
-    private int fMouseLastX             = MOUSE_LOCATION_UNKNOWN;
-    private int fMouseLastY             = MOUSE_LOCATION_UNKNOWN;
-    private int fMouse1DesiredState     = BUTTON_STATE_UNKNOWN;
-    private int fMouse2DesiredState     = BUTTON_STATE_UNKNOWN;
-    private int fMouse3DesiredState     = BUTTON_STATE_UNKNOWN;
-    private int fMouse2KeyModifierState = MODIFIER_PREVIOUSLY_UNKNOWN;
-    private int fMouse3KeyModifierState = MODIFIER_PREVIOUSLY_UNKNOWN;
-    private boolean fMouseMoveAction;
+    private int mouseLastX = MOUSE_LOCATION_UNKNOWN;
+    private int mouseLastY = MOUSE_LOCATION_UNKNOWN;
+
+    // OS X doesn't generate dragged event as a result of button press and
+    // mouse move events. This means that we have to track buttons state
+    // in order to generate dragged events ourselves.
+    private int mouseButtonsState = 0;
 
     /**
      * Uses the given GraphicsDevice as the coordinate system for subsequent
@@ -71,12 +62,11 @@
      */
     @Override
     public void mouseMove(int x, int y) {
-        fMouseLastX = x;
-        fMouseLastY = y;
-        fMouseMoveAction = true;
-        mouseEvent(fDevice.getCoreGraphicsScreen(), fMouseLastX, fMouseLastY,
-                   fMouse1DesiredState, fMouse2DesiredState,
-                   fMouse3DesiredState, fMouseMoveAction);
+        mouseLastX = x;
+        mouseLastY = y;
+
+        mouseEvent(fDevice.getCoreGraphicsScreen(), mouseLastX, mouseLastY,
+                   mouseButtonsState, true, true);
     }
 
     /**
@@ -87,32 +77,10 @@
      */
     @Override
     public void mousePress(int buttons) {
-        if ((buttons & InputEvent.BUTTON1_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON1_DOWN_MASK) != 0) {
-            fMouse1DesiredState = BUTTON_STATE_DOWN;
-        } else {
-            fMouse1DesiredState = BUTTON_STATE_UNKNOWN;
-        }
+        mouseButtonsState |= buttons;
 
-        if ((buttons & InputEvent.BUTTON2_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON2_DOWN_MASK) != 0) {
-            fMouse2DesiredState = BUTTON_STATE_DOWN;
-        } else {
-            fMouse2DesiredState = BUTTON_STATE_UNKNOWN;
-        }
-
-        if ((buttons & InputEvent.BUTTON3_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON3_DOWN_MASK) != 0) {
-            fMouse3DesiredState = BUTTON_STATE_DOWN;
-        } else {
-            fMouse3DesiredState = BUTTON_STATE_UNKNOWN;
-        }
-
-        fMouseMoveAction = false;
-
-        mouseEvent(fDevice.getCoreGraphicsScreen(), fMouseLastX, fMouseLastY,
-                   fMouse1DesiredState, fMouse2DesiredState,
-                   fMouse3DesiredState, fMouseMoveAction);
+        mouseEvent(fDevice.getCoreGraphicsScreen(), mouseLastX, mouseLastY,
+                   buttons, true, false);
     }
 
     /**
@@ -123,32 +91,10 @@
      */
     @Override
     public void mouseRelease(int buttons) {
-        if ((buttons & InputEvent.BUTTON1_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON1_DOWN_MASK) != 0) {
-            fMouse1DesiredState = BUTTON_STATE_UP;
-        } else {
-            fMouse1DesiredState = BUTTON_STATE_UNKNOWN;
-        }
+        mouseButtonsState &= ~buttons;
 
-        if ((buttons & InputEvent.BUTTON2_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON2_DOWN_MASK) != 0) {
-            fMouse2DesiredState = BUTTON_STATE_UP;
-        } else {
-            fMouse2DesiredState = BUTTON_STATE_UNKNOWN;
-        }
-
-        if ((buttons & InputEvent.BUTTON3_MASK) != 0 ||
-                (buttons & InputEvent.BUTTON3_DOWN_MASK) != 0) {
-            fMouse3DesiredState = BUTTON_STATE_UP;
-        } else {
-            fMouse3DesiredState = BUTTON_STATE_UNKNOWN;
-        }
-
-        fMouseMoveAction = false;
-
-        mouseEvent(fDevice.getCoreGraphicsScreen(), fMouseLastX, fMouseLastY,
-                   fMouse1DesiredState, fMouse2DesiredState,
-                   fMouse3DesiredState, fMouseMoveAction);
+        mouseEvent(fDevice.getCoreGraphicsScreen(), mouseLastX, mouseLastY,
+                   buttons, false, false);
     }
 
     @Override
@@ -218,10 +164,9 @@
 
     private native void initRobot();
     private native void mouseEvent(int screen, int lastX, int lastY,
-                                   int button1DesiredState,
-                                   int button2DesiredState,
-                                   int button3DesiredState,
-                                   boolean moveAction);
+                                   int buttonsState,
+                                   boolean isButtonsDownState,
+                                   boolean isMouseMove);
     private native void keyEvent(int javaKeyCode, boolean keydown);
     private void getScreenPixels(Rectangle r, int[] pixels){
         nativeGetScreenPixels(r.x, r.y, r.width, r.height, pixels);
--- a/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Thu Feb 02 22:27:04 2012 +0300
@@ -54,7 +54,12 @@
  * Mac OS X Cocoa-based AWT Toolkit.
  */
 public class LWCToolkit extends LWToolkit {
-    private static final Integer BUTTONS = 3; // number of available mouse buttons
+    // While it is possible to enumerate all mouse devices
+    // and query them for the number of buttons, the code
+    // that does it is rather complex. Instead, we opt for
+    // the easy way and just support up to 5 mouse buttons,
+    // like Windows.
+    private static final int BUTTONS = 5;
 
     private static native void initIDs();
 
@@ -84,6 +89,10 @@
 
     public LWCToolkit() {
         SunToolkit.setDataTransfererClassName("sun.lwawt.macosx.CDataTransferer");
+
+        areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
+        //set system property if not yet assigned
+        System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
     }
 
     /*
@@ -391,14 +400,19 @@
         }
     }
 
-    /*
-     * TODO: take a look on pointer API for Cocoa. For now, suppress extra-button
-     * mouse events.
-     */
+    //Is it allowed to generate events assigned to extra mouse buttons.
+    //Set to true by default.
+    private static boolean areExtraMouseButtonsEnabled = true;
+
     public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
-        return false;
+        return areExtraMouseButtonsEnabled;
     }
 
+    public int getNumberOfButtons(){
+        return BUTTONS;
+    }
+
+
     @Override
     public boolean isTraySupported() {
         return true;
--- a/src/macosx/classes/sun/lwawt/macosx/event/NSEvent.java	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/macosx/event/NSEvent.java	Thu Feb 02 22:27:04 2012 +0300
@@ -26,7 +26,6 @@
 package sun.lwawt.macosx.event;
 
 import sun.lwawt.macosx.CocoaConstants;
-
 import java.awt.event.*;
 
 /*
@@ -134,7 +133,7 @@
      * Converts an NSEvent button number to a MouseEvent constant.
      */
     public static int nsButtonToJavaButton(int buttonNumber) {
-        int jbuttonNumber = MouseEvent.NOBUTTON;
+        int jbuttonNumber = buttonNumber + 1;
         switch (buttonNumber) {
             case CocoaConstants.kCGMouseButtonLeft:
                 jbuttonNumber = MouseEvent.BUTTON1;
--- a/src/macosx/native/sun/awt/AWTEvent.m	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/native/sun/awt/AWTEvent.m	Thu Feb 02 22:27:04 2012 +0300
@@ -690,32 +690,8 @@
     }
 }
 
-/*
- * Converts the NSEvent type to a Java mouse button mask.
- */
-static jint
-NsMouseModifiersToJavaModifiers(NSEvent *event)
-{
-    switch ([event type]) {
-    case NSLeftMouseDown:
-        return java_awt_event_InputEvent_BUTTON1_DOWN_MASK;
-    case NSRightMouseDown:
-        return java_awt_event_InputEvent_BUTTON3_DOWN_MASK;
-    case NSOtherMouseDown:
-        return java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
-    default:
-        return 0;
-    }
-}
-
 jint GetJavaMouseModifiers(NSInteger button, NSUInteger modifierFlags)
 {
-    static NSInteger sMaxMouseButton = 2;
-
-    if (button > sMaxMouseButton) {
-        sMaxMouseButton = button;
-    }
-
     // Mousing needs the key modifiers
     jint modifiers = NsKeyModifiersToJavaModifiers(modifierFlags);
 
@@ -739,12 +715,11 @@
         modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
     }
 
-    // like JDK 1.6, we pretend that additional buttons are BUTTON2
-    NSInteger otherButton = 3;
-    for (; otherButton <= sMaxMouseButton; otherButton++) {
+    NSInteger extraButton = 3;
+    for (; extraButton < gNumberOfButtons; extraButton++) {
         if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
-                                 otherButton)) {
-            modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
+                                 extraButton)) {
+            modifiers |= gButtonDownMasks[extraButton];
         }
     }
 
@@ -1049,7 +1024,9 @@
     // out = [jkeyCode, jkeyLocation];
     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
-
+    
+    (*env)->ReleaseIntArrayElements(env, inData, data, 0);
+    
 JNF_COCOA_EXIT(env);
 
     return postsTyped;
@@ -1088,5 +1065,7 @@
     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
     (*env)->SetIntArrayRegion(env, outData, 2, 1, &jkeyType);
 
+    (*env)->ReleaseIntArrayElements(env, inData, data, 0);
+    
 JNF_COCOA_EXIT(env);
 }
--- a/src/macosx/native/sun/awt/CRobot.m	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/native/sun/awt/CRobot.m	Thu Feb 02 22:27:04 2012 +0300
@@ -26,9 +26,10 @@
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
 #import <ApplicationServices/ApplicationServices.h>
 
-
 #import "LWCToolkit.h"
 #import "sun_lwawt_macosx_CRobot.h"
+#import "java_awt_event_InputEvent.h"
+
 
 // Starting number for event numbers generated by Robot.
 // Apple docs don't mention at all what are the requirements
@@ -40,8 +41,6 @@
 // work for others as well.
 #define ROBOT_EVENT_NUMBER_START 32000
 
-// Theoretically, Quarts works with up to 32 buttons.
-#define k_JAVA_ROBOT_NUMBER_KNOWN_BUTTONS    3
 #define k_JAVA_ROBOT_WHEEL_COUNT 1
 
 #if !defined(kCGBitmapByteOrder32Host)
@@ -62,6 +61,7 @@
 // that is incremented on each button press. Otherwise, strange things
 // happen with z-order.
 static int gsEventNumber;
+static int* gsButtonEventNumber;
 
 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);
 
@@ -82,7 +82,7 @@
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    initRobot
- * Signature: ()V
+ * Signature: (V)V
  */
 JNIEXPORT void JNICALL
 Java_sun_lwawt_macosx_CRobot_initRobot
@@ -92,6 +92,10 @@
     // Always set all states, in case Apple ever changes default behaviors.
     static int setupDone = 0;
     if (!setupDone) {
+        int i;
+        jint* tmp;
+        jboolean copy = JNI_FALSE;
+
         setupDone = 1;
         // Don't block local events after posting ours
         CGSetLocalEventsSuppressionInterval(0.0);
@@ -109,22 +113,30 @@
 
         gsClickCount = 0;
         gsLastClickTime = 0;
+        gsEventNumber = ROBOT_EVENT_NUMBER_START;
 
-        gsEventNumber = ROBOT_EVENT_NUMBER_START;
+        gsButtonEventNumber = (int*)malloc(sizeof(int) * gNumberOfButtons);
+        if (gsButtonEventNumber == NULL) {
+            JNU_ThrowOutOfMemoryError(env, NULL);
+            return;
+        }
+
+        for (i = 0; i < gNumberOfButtons; ++i) {
+            gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
+        }
     }
 }
 
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    mouseEvent
- * Signature: (IIIIIIZ)V
+ * Signature: (IIIIZZ)V
  */
 JNIEXPORT void JNICALL
 Java_sun_lwawt_macosx_CRobot_mouseEvent
 (JNIEnv *env, jobject peer,
- jint screenIndex, jint mouseLastX, jint mouseLastY,
- jint mouse1State, jint mouse2State,
- jint mouse3State, jboolean mouseMoveAction)
+ jint screenIndex, jint mouseLastX, jint mouseLastY, jint buttonsState,
+ jboolean isButtonsDownState, jboolean isMouseMove)
 {
     JNF_COCOA_ENTER(env);
 
@@ -180,79 +192,73 @@
     point.x = mouseLastX + globalDeviceBounds.origin.x;
     point.y = mouseLastY + globalDeviceBounds.origin.y;
 
-    BOOL isDown = NO;
-    CGMouseButton button = kCGMouseButtonLeft;
-    CGEventType type = kCGEventMouseMoved;
+    __block CGMouseButton button = kCGMouseButtonLeft;
+    __block CGEventType type = kCGEventMouseMoved;
+
+    void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
+        ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
+          CGEventType cgButtonDragged) {
+
+            button = cgButton;
+            type = cgButtonUp;
+
+            if (isButtonsDownState) {
+                if (isMouseMove) {
+                    type = cgButtonDragged;
+                } else {
+                    type = cgButtonDown;
+                }
+            }
+        };
+
+    // Left
+    if (buttonsState & java_awt_event_InputEvent_BUTTON1_MASK ||
+        buttonsState & java_awt_event_InputEvent_BUTTON1_DOWN_MASK ) {
+
+        HandleRobotButton(kCGMouseButtonLeft, kCGEventLeftMouseUp,
+                          kCGEventLeftMouseDown, kCGEventLeftMouseDragged);
+    }
+
+    // Other
+    if (buttonsState & java_awt_event_InputEvent_BUTTON2_MASK ||
+        buttonsState & java_awt_event_InputEvent_BUTTON2_DOWN_MASK ) {
+
+        HandleRobotButton(kCGMouseButtonCenter, kCGEventOtherMouseUp,
+                          kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
+    }
+
+    // Right
+    if (buttonsState & java_awt_event_InputEvent_BUTTON3_MASK ||
+        buttonsState & java_awt_event_InputEvent_BUTTON3_DOWN_MASK ) {
+
+        HandleRobotButton(kCGMouseButtonRight, kCGEventRightMouseUp,
+                          kCGEventRightMouseDown, kCGEventRightMouseDragged);
+    }
+
+    // Extra
+    if (gNumberOfButtons > 3) {
+        int extraButton;
+        for (extraButton = 3; extraButton < gNumberOfButtons; ++extraButton) {
+            if ((buttonsState & gButtonDownMasks[extraButton])) {
+                HandleRobotButton(extraButton, kCGEventOtherMouseUp,
+                            kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
+            }
+        }
+    }
+
     int clickCount = 0;
     int eventNumber = gsEventNumber;
 
-    // When moving, the buttons aren't changed from their current state.
-    if (mouseMoveAction == JNI_FALSE) {
-        // Some of the buttons are changing state.
-
-        // Left
-        if (mouse1State != sun_lwawt_macosx_CRobot_BUTTON_STATE_UNKNOWN) {
-            static int sEventNumber = ROBOT_EVENT_NUMBER_START;
-
-            button = kCGMouseButtonLeft;
-            if (mouse1State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-                sEventNumber = gsEventNumber++;
-                type = kCGEventLeftMouseDown;
-                isDown = YES;
-            } else {
-                type = kCGEventLeftMouseUp;
-            }
-
-            eventNumber = sEventNumber;
-        }
-
-        // Other
-        if (mouse2State != sun_lwawt_macosx_CRobot_BUTTON_STATE_UNKNOWN) {
-            static int sEventNumber = ROBOT_EVENT_NUMBER_START;
-
-            button = kCGMouseButtonCenter;
-            if (mouse2State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-                sEventNumber = gsEventNumber++;
-                type = kCGEventOtherMouseDown;
-                isDown = YES;
-            } else {
-                type = kCGEventOtherMouseUp;
-            }
-
-            eventNumber = sEventNumber;
-        }
-
-        // Right
-        if (mouse3State != sun_lwawt_macosx_CRobot_BUTTON_STATE_UNKNOWN) {
-            static int sEventNumber = ROBOT_EVENT_NUMBER_START;
-
-            button = kCGMouseButtonRight;
-            if (mouse3State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-                sEventNumber = gsEventNumber++;
-                type = kCGEventRightMouseDown;
-                isDown = YES;
-            } else {
-                type = kCGEventRightMouseUp;
-            }
-
-            eventNumber = sEventNumber;
-        }
-
-        clickCount = GetClickCount(isDown);
-    } else {
+    if (isMouseMove) {
         // any mouse movement resets click count
         gsLastClickTime = 0;
+    } else {
+        clickCount = GetClickCount(isButtonsDownState);
 
-        // could be mouse moved or mouse dragged
-        if (mouse1State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-            type = kCGEventLeftMouseDragged;
-        } else if (mouse2State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-            type = kCGEventOtherMouseDragged;
-        } else if (mouse3State == sun_lwawt_macosx_CRobot_BUTTON_STATE_DOWN) {
-            type = kCGEventRightMouseDragged;
-        } else {
-            type = kCGEventMouseMoved;
+        if (isButtonsDownState) {
+            gsButtonEventNumber[button] = gsEventNumber++;
         }
+        eventNumber = gsButtonEventNumber[button];
     }
 
     PostMouseEvent(point, button, type, clickCount, eventNumber);
--- a/src/macosx/native/sun/awt/LWCToolkit.h	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/native/sun/awt/LWCToolkit.h	Thu Feb 02 22:27:04 2012 +0300
@@ -33,6 +33,11 @@
 
 #define DEBUG 1
 
+// number of mouse buttons supported
+extern int gNumberOfButtons;
+
+// InputEvent mask array
+extern jint* gButtonDownMasks;
 
 @interface AWTToolkit : NSObject { }
 + (long) getEventCount;
--- a/src/macosx/native/sun/awt/LWCToolkit.m	Thu Feb 02 09:03:49 2012 -0800
+++ b/src/macosx/native/sun/awt/LWCToolkit.m	Thu Feb 02 22:27:04 2012 +0300
@@ -26,6 +26,7 @@
 #import <dlfcn.h>
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
 
+#include "jni_util.h"
 #import "CMenuBar.h"
 #import "InitIDs.h"
 #import "LWCToolkit.h"
@@ -35,6 +36,8 @@
 
 #import "sun_lwawt_macosx_LWCToolkit.h"
 
+int gNumberOfButtons;
+jint* gButtonDownMasks;
 
 @implementation AWTToolkit
 
@@ -214,6 +217,28 @@
         static JNF_STATIC_MEMBER_CACHE(jsm_installToolkitThreadNameInJava, jc_LWCToolkit, "installToolkitThreadNameInJava", "()V");
         JNFCallStaticVoidMethod(env, jsm_installToolkitThreadNameInJava);
     });
+    
+    gNumberOfButtons = sun_lwawt_macosx_LWCToolkit_BUTTONS;
+    
+    jclass inputEventClazz = (*env)->FindClass(env, "java/awt/event/InputEvent");
+    jmethodID getButtonDownMasksID = (*env)->GetStaticMethodID(env, inputEventClazz, "getButtonDownMasks", "()[I");
+    jintArray obj = (jintArray)(*env)->CallStaticObjectMethod(env, inputEventClazz, getButtonDownMasksID);
+    jint * tmp = (*env)->GetIntArrayElements(env, obj, JNI_FALSE);
+    
+    gButtonDownMasks = (jint*)malloc(sizeof(jint) * gNumberOfButtons);
+    if (gButtonDownMasks == NULL) {
+        gNumberOfButtons = 0;
+        JNU_ThrowOutOfMemoryError(env, NULL);
+        return;
+    }
+    
+    int i;
+    for (i = 0; i < gNumberOfButtons; i++) {
+        gButtonDownMasks[i] = tmp[i];
+    }
+    
+    (*env)->ReleaseIntArrayElements(env, obj, tmp, 0);
+    (*env)->DeleteLocalRef(env, obj);
 }
 
 static UInt32 RGB(NSColor *c) {