1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt.X11;
  27 
  28 import java.awt.Component;
  29 import java.awt.Cursor;
  30 import java.awt.Window;
  31 
  32 import java.awt.datatransfer.DataFlavor;
  33 import java.awt.datatransfer.Transferable;
  34 
  35 import java.awt.dnd.DnDConstants;
  36 import java.awt.dnd.DragGestureEvent;
  37 import java.awt.dnd.InvalidDnDOperationException;
  38 
  39 import java.util.*;
  40 
  41 import sun.util.logging.PlatformLogger;
  42 
  43 import sun.awt.dnd.SunDragSourceContextPeer;
  44 import sun.awt.dnd.SunDropTargetContextPeer;
  45 import sun.awt.SunToolkit;
  46 import sun.awt.AWTAccessor;
  47 
  48 /**
  49  * The XDragSourceContextPeer class is the class responsible for handling
  50  * the interaction between the XDnD/Motif DnD subsystem and Java drag sources.
  51  *
  52  * @since 1.5
  53  */
  54 public final class XDragSourceContextPeer
  55     extends SunDragSourceContextPeer implements XDragSourceProtocolListener {
  56     private static final PlatformLogger logger =
  57         PlatformLogger.getLogger("sun.awt.X11.xembed.xdnd.XDragSourceContextPeer");
  58 
  59     /* The events selected on the root window when the drag begins. */
  60     private static final int ROOT_EVENT_MASK = (int)XConstants.ButtonMotionMask |
  61         (int)XConstants.KeyPressMask | (int)XConstants.KeyReleaseMask;
  62     /* The events to be delivered during grab. */
  63     private static final int GRAB_EVENT_MASK = (int)XConstants.ButtonPressMask |
  64         (int)XConstants.ButtonMotionMask | (int)XConstants.ButtonReleaseMask;
  65 
  66     /* The event mask of the root window before the drag operation starts. */
  67     private long rootEventMask = 0;
  68     private boolean dndInProgress = false;
  69     private boolean dragInProgress = false;
  70     private long dragRootWindow = 0;
  71 
  72     /* The protocol chosen for the communication with the current drop target. */
  73     private XDragSourceProtocol dragProtocol = null;
  74     /* The drop action chosen by the current drop target. */
  75     private int targetAction = DnDConstants.ACTION_NONE;
  76     /* The set of drop actions supported by the drag source. */
  77     private int sourceActions = DnDConstants.ACTION_NONE;
  78     /* The drop action selected by the drag source based on the modifiers state
  79        and the action selected by the current drop target. */
  80     private int sourceAction = DnDConstants.ACTION_NONE;
  81     /* The data formats supported by the drag source for the current drag
  82        operation. */
  83     private long[] sourceFormats = null;
  84     /* The XID of the root subwindow that contains the current target. */
  85     private long targetRootSubwindow = 0;
  86     /* The pointer location. */
  87     private int xRoot = 0;
  88     private int yRoot = 0;
  89     /* Keyboard modifiers state. */
  90     private int eventState = 0;
  91 
  92     /* XEmbed DnD support. We act as a proxy between source and target. */
  93     private long proxyModeSourceWindow = 0;
  94 
  95     /* The singleton instance. */
  96     private static final XDragSourceContextPeer theInstance =
  97         new XDragSourceContextPeer(null);
  98 
  99     private XDragSourceContextPeer(DragGestureEvent dge) {
 100         super(dge);
 101     }
 102 
 103     static XDragSourceProtocolListener getXDragSourceProtocolListener() {
 104         return theInstance;
 105     }
 106 
 107     static XDragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge)
 108       throws InvalidDnDOperationException {
 109     theInstance.setTrigger(dge);
 110         return theInstance;
 111     }
 112 
 113     @SuppressWarnings("deprecation")
 114     protected void startDrag(Transferable transferable,
 115                              long[] formats, Map<Long, DataFlavor> formatMap) {
 116         Component component = getTrigger().getComponent();
 117         Component c = null;
 118         XWindowPeer wpeer = null;
 119 
 120         for (c = component; c != null && !(c instanceof Window);
 121              c = AWTAccessor.getComponentAccessor().getParent(c));
 122 
 123         if (c instanceof Window) {
 124             wpeer = (XWindowPeer)c.getPeer();
 125         }
 126 
 127         if (wpeer == null) {
 128             throw new InvalidDnDOperationException(
 129                 "Cannot find top-level for the drag source component");
 130         }
 131 
 132         long xcursor = 0;
 133         long rootWindow = 0;
 134         long dragWindow = 0;
 135         long timeStamp = 0;
 136 
 137         /* Retrieve the X cursor for the drag operation. */
 138         {
 139             Cursor cursor = getCursor();
 140             if (cursor != null) {
 141                 xcursor = XGlobalCursorManager.getCursor(cursor);
 142             }
 143         }
 144 
 145         XToolkit.awtLock();
 146         try {
 147             if (proxyModeSourceWindow != 0) {
 148                 throw new InvalidDnDOperationException("Proxy drag in progress");
 149             }
 150             if (dndInProgress) {
 151                 throw new InvalidDnDOperationException("Drag in progress");
 152             }
 153 
 154             /* Determine the root window for the drag operation. */
 155             {
 156                 long screen = XlibWrapper.XScreenNumberOfScreen(wpeer.getScreen());
 157                 rootWindow = XlibWrapper.RootWindow(XToolkit.getDisplay(), screen);
 158             }
 159 
 160             dragWindow = XWindow.getXAWTRootWindow().getWindow();
 161 
 162             timeStamp = XToolkit.getCurrentServerTime();
 163 
 164             int dropActions = getDragSourceContext().getSourceActions();
 165 
 166             Iterator<XDragSourceProtocol> dragProtocols =
 167                 XDragAndDropProtocols.getDragSourceProtocols();
 168             while (dragProtocols.hasNext()) {
 169                 XDragSourceProtocol dragProtocol = dragProtocols.next();
 170                 try {
 171                     dragProtocol.initializeDrag(dropActions, transferable,
 172                                                 formatMap, formats);
 173                 } catch (XException xe) {
 174                     throw (InvalidDnDOperationException)
 175                         new InvalidDnDOperationException().initCause(xe);
 176                 }
 177             }
 178 
 179             /* Install X grabs. */
 180             {
 181                 int status;
 182                 XWindowAttributes wattr = new XWindowAttributes();
 183                 try {
 184                     status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
 185                                                               rootWindow, wattr.pData);
 186 
 187                     if (status == 0) {
 188                         throw new InvalidDnDOperationException("XGetWindowAttributes failed");
 189                     }
 190 
 191                     rootEventMask = wattr.get_your_event_mask();
 192 
 193                     XlibWrapper.XSelectInput(XToolkit.getDisplay(), rootWindow,
 194                                              rootEventMask | ROOT_EVENT_MASK);
 195                 } finally {
 196                     wattr.dispose();
 197                 }
 198 
 199                 XBaseWindow.ungrabInput();
 200 
 201                 status = XlibWrapper.XGrabPointer(XToolkit.getDisplay(), rootWindow,
 202                                                   0, GRAB_EVENT_MASK,
 203                                                   XConstants.GrabModeAsync,
 204                                                   XConstants.GrabModeAsync,
 205                                                   XConstants.None, xcursor, timeStamp);
 206 
 207                 if (status != XConstants.GrabSuccess) {
 208                     cleanup(timeStamp);
 209                     throwGrabFailureException("Cannot grab pointer", status);
 210                     return;
 211                 }
 212 
 213                 status = XlibWrapper.XGrabKeyboard(XToolkit.getDisplay(), rootWindow,
 214                                                    0,
 215                                                    XConstants.GrabModeAsync,
 216                                                    XConstants.GrabModeAsync,
 217                                                    timeStamp);
 218 
 219                 if (status != XConstants.GrabSuccess) {
 220                     cleanup(timeStamp);
 221                     throwGrabFailureException("Cannot grab keyboard", status);
 222                     return;
 223                 }
 224             }
 225 
 226             /* Update the global state. */
 227             dndInProgress = true;
 228             dragInProgress = true;
 229             dragRootWindow = rootWindow;
 230             sourceActions = dropActions;
 231             sourceFormats = formats;
 232         } finally {
 233             XToolkit.awtUnlock();
 234         }
 235 
 236         /* This implementation doesn't use native context */
 237         setNativeContext(0);
 238 
 239         SunDropTargetContextPeer.setCurrentJVMLocalSourceTransferable(transferable);
 240     }
 241 
 242     public long getProxyModeSourceWindow() {
 243         return proxyModeSourceWindow;
 244     }
 245 
 246     private void setProxyModeSourceWindowImpl(long window) {
 247         proxyModeSourceWindow = window;
 248     }
 249 
 250     public static void setProxyModeSourceWindow(long window) {
 251         theInstance.setProxyModeSourceWindowImpl(window);
 252     }
 253 
 254     /**
 255      * set cursor
 256      */
 257 
 258     public void setCursor(Cursor c) throws InvalidDnDOperationException {
 259         XToolkit.awtLock();
 260         try {
 261             super.setCursor(c);
 262         } finally {
 263             XToolkit.awtUnlock();
 264         }
 265     }
 266 
 267     protected void setNativeCursor(long nativeCtxt, Cursor c, int cType) {
 268         assert XToolkit.isAWTLockHeldByCurrentThread();
 269 
 270         if (c == null) {
 271             return;
 272         }
 273 
 274         long xcursor = XGlobalCursorManager.getCursor(c);
 275 
 276         if (xcursor == 0) {
 277             return;
 278         }
 279 
 280         XlibWrapper.XChangeActivePointerGrab(XToolkit.getDisplay(),
 281                                              GRAB_EVENT_MASK,
 282                                              xcursor,
 283                                              XConstants.CurrentTime);
 284     }
 285 
 286     protected boolean needsBogusExitBeforeDrop() {
 287         return false;
 288     }
 289 
 290     private void throwGrabFailureException(String msg, int grabStatus)
 291       throws InvalidDnDOperationException {
 292         String msgCause = "";
 293         switch (grabStatus) {
 294         case XConstants.GrabNotViewable:  msgCause = "not viewable";    break;
 295         case XConstants.AlreadyGrabbed:   msgCause = "already grabbed"; break;
 296         case XConstants.GrabInvalidTime:  msgCause = "invalid time";    break;
 297         case XConstants.GrabFrozen:       msgCause = "grab frozen";     break;
 298         default:                           msgCause = "unknown failure"; break;
 299         }
 300         throw new InvalidDnDOperationException(msg + ": " + msgCause);
 301     }
 302 
 303     /**
 304      * The caller must own awtLock.
 305      */
 306     public void cleanup(long time) {
 307         if (dndInProgress) {
 308             if (dragProtocol != null) {
 309                 dragProtocol.sendLeaveMessage(time);
 310             }
 311 
 312             if (targetAction != DnDConstants.ACTION_NONE) {
 313                 dragExit(xRoot, yRoot);
 314             }
 315 
 316             dragDropFinished(false, DnDConstants.ACTION_NONE, xRoot, yRoot);
 317         }
 318 
 319         Iterator<XDragSourceProtocol> dragProtocols =
 320             XDragAndDropProtocols.getDragSourceProtocols();
 321         while (dragProtocols.hasNext()) {
 322             XDragSourceProtocol dragProtocol = dragProtocols.next();
 323             try {
 324                 dragProtocol.cleanup();
 325             } catch (XException xe) {
 326                 // Ignore the exception.
 327             }
 328         }
 329 
 330         dndInProgress = false;
 331         dragInProgress = false;
 332         dragRootWindow = 0;
 333         sourceFormats = null;
 334         sourceActions = DnDConstants.ACTION_NONE;
 335         sourceAction = DnDConstants.ACTION_NONE;
 336         eventState = 0;
 337         xRoot = 0;
 338         yRoot = 0;
 339 
 340         cleanupTargetInfo();
 341 
 342         removeDnDGrab(time);
 343     }
 344 
 345     /**
 346      * The caller must own awtLock.
 347      */
 348     private void cleanupTargetInfo() {
 349         targetAction = DnDConstants.ACTION_NONE;
 350         dragProtocol = null;
 351         targetRootSubwindow = 0;
 352     }
 353 
 354     private void removeDnDGrab(long time) {
 355         assert XToolkit.isAWTLockHeldByCurrentThread();
 356 
 357         XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), time);
 358         XlibWrapper.XUngrabKeyboard(XToolkit.getDisplay(), time);
 359 
 360         /* Restore the root event mask if it was changed. */
 361         if ((rootEventMask | ROOT_EVENT_MASK) != rootEventMask &&
 362             dragRootWindow != 0) {
 363 
 364             XlibWrapper.XSelectInput(XToolkit.getDisplay(),
 365                                      dragRootWindow,
 366                                      rootEventMask);
 367         }
 368 
 369         rootEventMask = 0;
 370         dragRootWindow = 0;
 371     }
 372 
 373     private boolean processClientMessage(XClientMessageEvent xclient) {
 374         if (dragProtocol != null) {
 375             return dragProtocol.processClientMessage(xclient);
 376         }
 377         return false;
 378     }
 379 
 380     /**
 381      * Updates the source action according to the specified state.
 382      *
 383      * @returns true if the source
 384      */
 385     private boolean updateSourceAction(int state) {
 386         int action = SunDragSourceContextPeer.convertModifiersToDropAction(XWindow.getModifiers(state, 0, 0),
 387                                                                            sourceActions);
 388         if (sourceAction == action) {
 389             return false;
 390         }
 391         sourceAction = action;
 392         return true;
 393     }
 394 
 395     /**
 396      * Returns the client window under the specified root subwindow.
 397      */
 398     private static long findClientWindow(long window) {
 399         if (XlibUtil.isTrueToplevelWindow(window)) {
 400             return window;
 401         }
 402 
 403         Set<Long> children = XlibUtil.getChildWindows(window);
 404         for (Long child : children) {
 405             long win = findClientWindow(child);
 406             if (win != 0) {
 407                 return win;
 408             }
 409         }
 410 
 411         return 0;
 412     }
 413 
 414     private void doUpdateTargetWindow(long subwindow, long time) {
 415         long clientWindow = 0;
 416         long proxyWindow = 0;
 417         XDragSourceProtocol protocol = null;
 418         boolean isReceiver = false;
 419 
 420         if (subwindow != 0) {
 421             clientWindow = findClientWindow(subwindow);
 422         }
 423 
 424         if (clientWindow != 0) {
 425             Iterator<XDragSourceProtocol> dragProtocols =
 426                 XDragAndDropProtocols.getDragSourceProtocols();
 427             while (dragProtocols.hasNext()) {
 428                 XDragSourceProtocol dragProtocol = dragProtocols.next();
 429                 if (dragProtocol.attachTargetWindow(clientWindow, time)) {
 430                     protocol = dragProtocol;
 431                     break;
 432                 }
 433             }
 434         }
 435 
 436         /* Update the global state. */
 437         dragProtocol = protocol;
 438         targetAction = DnDConstants.ACTION_NONE;
 439         targetRootSubwindow = subwindow;
 440     }
 441 
 442     private void updateTargetWindow(XMotionEvent xmotion) {
 443         assert XToolkit.isAWTLockHeldByCurrentThread();
 444 
 445         int x = xmotion.get_x_root();
 446         int y = xmotion.get_y_root();
 447         long time = xmotion.get_time();
 448         long subwindow = xmotion.get_subwindow();
 449 
 450         /*
 451          * If this event had occurred before the pointer was grabbed,
 452          * query the server for the current root subwindow.
 453          */
 454         if (xmotion.get_window() != xmotion.get_root()) {
 455             XlibWrapper.XQueryPointer(XToolkit.getDisplay(),
 456                                       xmotion.get_root(),
 457                                       XlibWrapper.larg1,  // root
 458                                       XlibWrapper.larg2,  // subwindow
 459                                       XlibWrapper.larg3,  // x_root
 460                                       XlibWrapper.larg4,  // y_root
 461                                       XlibWrapper.larg5,  // x
 462                                       XlibWrapper.larg6,  // y
 463                                       XlibWrapper.larg7); // modifiers
 464             subwindow = Native.getLong(XlibWrapper.larg2);
 465         }
 466 
 467         if (targetRootSubwindow != subwindow) {
 468             if (dragProtocol != null) {
 469                 dragProtocol.sendLeaveMessage(time);
 470 
 471                 /*
 472                  * Neither Motif DnD nor XDnD provide a mean for the target
 473                  * to notify the source that the pointer exits the drop site
 474                  * that occupies the whole top level.
 475                  * We detect this situation and post dragExit.
 476                  */
 477                 if (targetAction != DnDConstants.ACTION_NONE) {
 478                     dragExit(x, y);
 479                 }
 480             }
 481 
 482             /* Update the global state. */
 483             doUpdateTargetWindow(subwindow, time);
 484 
 485             if (dragProtocol != null) {
 486                 dragProtocol.sendEnterMessage(sourceFormats,
 487                                               sourceAction,
 488                                               sourceActions,
 489                                               time);
 490             }
 491         }
 492     }
 493 
 494     /*
 495      * DO NOT USE is_hint field of xmotion since it could not be set when we
 496      * convert XKeyEvent or XButtonRelease to XMotionEvent.
 497      */
 498     private void processMouseMove(XMotionEvent xmotion) {
 499         if (!dragInProgress) {
 500             return;
 501         }
 502         if (xRoot != xmotion.get_x_root() || yRoot != xmotion.get_y_root()) {
 503             xRoot = xmotion.get_x_root();
 504             yRoot = xmotion.get_y_root();
 505 
 506             postDragSourceDragEvent(targetAction,
 507                                     XWindow.getModifiers(xmotion.get_state(),0,0),
 508                                     xRoot, yRoot, DISPATCH_MOUSE_MOVED);
 509         }
 510 
 511         if (eventState != xmotion.get_state()) {
 512             if (updateSourceAction(xmotion.get_state()) && dragProtocol != null) {
 513                 postDragSourceDragEvent(targetAction,
 514                                         XWindow.getModifiers(xmotion.get_state(),0,0),
 515                                         xRoot, yRoot, DISPATCH_CHANGED);
 516             }
 517             eventState = xmotion.get_state();
 518         }
 519 
 520         updateTargetWindow(xmotion);
 521 
 522         if (dragProtocol != null) {
 523             dragProtocol.sendMoveMessage(xmotion.get_x_root(),
 524                                          xmotion.get_y_root(),
 525                                          sourceAction, sourceActions,
 526                                          xmotion.get_time());
 527         }
 528     }
 529 
 530     private void processDrop(XButtonEvent xbutton) {
 531         try {
 532             dragProtocol.initiateDrop(xbutton.get_x_root(),
 533                                       xbutton.get_y_root(),
 534                                       sourceAction, sourceActions,
 535                                       xbutton.get_time());
 536         } catch (XException e) {
 537             cleanup(xbutton.get_time());
 538         }
 539     }
 540 
 541     private boolean processProxyModeEvent(XEvent ev) {
 542         if (getProxyModeSourceWindow() == 0) {
 543             return false;
 544         }
 545 
 546         if (ev.get_type() != XConstants.ClientMessage) {
 547             return false;
 548         }
 549 
 550         if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
 551             logger.finest("        proxyModeSourceWindow=" +
 552                           getProxyModeSourceWindow() +
 553                           " ev=" + ev);
 554         }
 555 
 556         XClientMessageEvent xclient = ev.get_xclient();
 557 
 558         Iterator<XDragSourceProtocol> dragProtocols =
 559             XDragAndDropProtocols.getDragSourceProtocols();
 560         while (dragProtocols.hasNext()) {
 561             XDragSourceProtocol dragProtocol = dragProtocols.next();
 562             if (dragProtocol.processProxyModeEvent(xclient,
 563                                                    getProxyModeSourceWindow())) {
 564                 return true;
 565             }
 566         }
 567 
 568         return false;
 569     }
 570 
 571     /**
 572      * The caller must own awtLock.
 573      *
 574      * @returns true if the even was processed and shouldn't be passed along.
 575      */
 576     private boolean doProcessEvent(XEvent ev) {
 577         assert XToolkit.isAWTLockHeldByCurrentThread();
 578 
 579         if (processProxyModeEvent(ev)) {
 580             return true;
 581         }
 582 
 583         if (!dndInProgress) {
 584             return false;
 585         }
 586 
 587         switch (ev.get_type()) {
 588         case XConstants.ClientMessage: {
 589             XClientMessageEvent xclient = ev.get_xclient();
 590             return processClientMessage(xclient);
 591         }
 592         case XConstants.DestroyNotify: {
 593             XDestroyWindowEvent xde = ev.get_xdestroywindow();
 594 
 595             /* Target crashed during drop processing - cleanup. */
 596             if (!dragInProgress &&
 597                 dragProtocol != null &&
 598                 xde.get_window() == dragProtocol.getTargetWindow()) {
 599                 cleanup(XConstants.CurrentTime);
 600                 return true;
 601             }
 602             /* Pass along */
 603             return false;
 604         }
 605         }
 606 
 607         if (!dragInProgress) {
 608             return false;
 609         }
 610 
 611         /* Process drag-only messages. */
 612         switch (ev.get_type()) {
 613         case XConstants.KeyRelease:
 614         case XConstants.KeyPress: {
 615             XKeyEvent xkey = ev.get_xkey();
 616             long keysym = XlibWrapper.XKeycodeToKeysym(XToolkit.getDisplay(),
 617                                                        xkey.get_keycode(), 0);
 618             switch ((int)keysym) {
 619             case (int)XKeySymConstants.XK_Escape: {
 620                 if (ev.get_type() == XConstants.KeyRelease) {
 621                     cleanup(xkey.get_time());
 622                 }
 623                 break;
 624             }
 625             case (int)XKeySymConstants.XK_Control_R:
 626             case (int)XKeySymConstants.XK_Control_L:
 627             case (int)XKeySymConstants.XK_Shift_R:
 628             case (int)XKeySymConstants.XK_Shift_L: {
 629                 XlibWrapper.XQueryPointer(XToolkit.getDisplay(),
 630                                           xkey.get_root(),
 631                                           XlibWrapper.larg1,  // root
 632                                           XlibWrapper.larg2,  // subwindow
 633                                           XlibWrapper.larg3,  // x_root
 634                                           XlibWrapper.larg4,  // y_root
 635                                           XlibWrapper.larg5,  // x
 636                                           XlibWrapper.larg6,  // y
 637                                           XlibWrapper.larg7); // modifiers
 638                 XMotionEvent xmotion = new XMotionEvent();
 639                 try {
 640                     xmotion.set_type(XConstants.MotionNotify);
 641                     xmotion.set_serial(xkey.get_serial());
 642                     xmotion.set_send_event(xkey.get_send_event());
 643                     xmotion.set_display(xkey.get_display());
 644                     xmotion.set_window(xkey.get_window());
 645                     xmotion.set_root(xkey.get_root());
 646                     xmotion.set_subwindow(xkey.get_subwindow());
 647                     xmotion.set_time(xkey.get_time());
 648                     xmotion.set_x(xkey.get_x());
 649                     xmotion.set_y(xkey.get_y());
 650                     xmotion.set_x_root(xkey.get_x_root());
 651                     xmotion.set_y_root(xkey.get_y_root());
 652                     xmotion.set_state((int)Native.getLong(XlibWrapper.larg7));
 653                     // we do not use this field, so it's unset for now
 654                     // xmotion.set_is_hint(???);
 655                     xmotion.set_same_screen(xkey.get_same_screen());
 656 
 657                     //It's safe to use key event as motion event since we use only their common fields.
 658                     processMouseMove(xmotion);
 659                 } finally {
 660                     xmotion.dispose();
 661                 }
 662                 break;
 663             }
 664             }
 665             return true;
 666         }
 667         case XConstants.ButtonPress:
 668             return true;
 669         case XConstants.MotionNotify:
 670             processMouseMove(ev.get_xmotion());
 671             return true;
 672         case XConstants.ButtonRelease: {
 673             XButtonEvent xbutton = ev.get_xbutton();
 674             /*
 675              * Ignore the buttons above 20 due to the bit limit for
 676              * InputEvent.BUTTON_DOWN_MASK.
 677              * One more bit is reserved for FIRST_HIGH_BIT.
 678              */
 679             if (xbutton.get_button() > SunToolkit.MAX_BUTTONS_SUPPORTED) {
 680                 return true;
 681             }
 682 
 683             /*
 684              * On some X servers it could happen that ButtonRelease coordinates
 685              * differ from the latest MotionNotify coordinates, so we need to
 686              * process it as a mouse motion.
 687              */
 688             XMotionEvent xmotion = new XMotionEvent();
 689             try {
 690                 xmotion.set_type(XConstants.MotionNotify);
 691                 xmotion.set_serial(xbutton.get_serial());
 692                 xmotion.set_send_event(xbutton.get_send_event());
 693                 xmotion.set_display(xbutton.get_display());
 694                 xmotion.set_window(xbutton.get_window());
 695                 xmotion.set_root(xbutton.get_root());
 696                 xmotion.set_subwindow(xbutton.get_subwindow());
 697                 xmotion.set_time(xbutton.get_time());
 698                 xmotion.set_x(xbutton.get_x());
 699                 xmotion.set_y(xbutton.get_y());
 700                 xmotion.set_x_root(xbutton.get_x_root());
 701                 xmotion.set_y_root(xbutton.get_y_root());
 702                 xmotion.set_state(xbutton.get_state());
 703                 // we do not use this field, so it's unset for now
 704                 // xmotion.set_is_hint(???);
 705                 xmotion.set_same_screen(xbutton.get_same_screen());
 706 
 707                 //It's safe to use key event as motion event since we use only their common fields.
 708                 processMouseMove(xmotion);
 709             } finally {
 710                 xmotion.dispose();
 711             }
 712             if (xbutton.get_button() == XConstants.buttons[0]
 713                 || xbutton.get_button() == XConstants.buttons[1]) {
 714                 // drag is initiated with Button1 or Button2 pressed and
 715                 // ended on release of either of these buttons (as the same
 716                 // behavior was with our old Motif DnD-based implementation)
 717                 removeDnDGrab(xbutton.get_time());
 718                 dragInProgress = false;
 719                 if (dragProtocol != null && targetAction != DnDConstants.ACTION_NONE) {
 720                     /*
 721                      * ACTION_NONE indicates that either the drop target rejects the
 722                      * drop or it haven't responded yet. The latter could happen in
 723                      * case of fast drag, slow target-server connection or slow
 724                      * drag notifications processing on the target side.
 725                      */
 726                     processDrop(xbutton);
 727                 } else {
 728                     cleanup(xbutton.get_time());
 729                 }
 730             }
 731             return true;
 732         }
 733         }
 734 
 735         return false;
 736     }
 737 
 738     static boolean processEvent(XEvent ev) {
 739         XToolkit.awtLock();
 740         try {
 741             try {
 742                 return theInstance.doProcessEvent(ev);
 743             } catch (XException e) {
 744                 e.printStackTrace();
 745                 return false;
 746             }
 747         } finally {
 748             XToolkit.awtUnlock();
 749         }
 750     }
 751 
 752     /* XDragSourceProtocolListener implementation */
 753 
 754     public void handleDragReply(int action) {
 755         // NOTE: we have to use the current pointer location, since
 756         // the target didn't specify the coordinates for the reply.
 757         handleDragReply(action, xRoot, yRoot);
 758     }
 759 
 760     public void handleDragReply(int action, int x, int y) {
 761         // NOTE: we have to use the current modifiers state, since
 762         // the target didn't specify the modifiers state for the reply.
 763         handleDragReply(action, xRoot, yRoot, XWindow.getModifiers(eventState,0,0));
 764     }
 765 
 766     public void handleDragReply(int action, int x, int y, int modifiers) {
 767         if (action == DnDConstants.ACTION_NONE &&
 768             targetAction != DnDConstants.ACTION_NONE) {
 769             dragExit(x, y);
 770         } else if (action != DnDConstants.ACTION_NONE) {
 771             int type = 0;
 772 
 773             if (targetAction == DnDConstants.ACTION_NONE) {
 774                 type = SunDragSourceContextPeer.DISPATCH_ENTER;
 775             } else {
 776                 type = SunDragSourceContextPeer.DISPATCH_MOTION;
 777             }
 778 
 779             // Note that we use the modifiers state a
 780             postDragSourceDragEvent(action, modifiers, x, y, type);
 781         }
 782 
 783         targetAction = action;
 784     }
 785 
 786     public void handleDragFinished() {
 787         /* Assume that the drop was successful. */
 788         handleDragFinished(true);
 789     }
 790 
 791     public void handleDragFinished(boolean success) {
 792         /* Assume that the performed drop action is the latest drop action
 793            accepted by the drop target. */
 794         handleDragFinished(true, targetAction);
 795     }
 796 
 797     public void handleDragFinished(boolean success, int action) {
 798         // NOTE: we have to use the current pointer location, since
 799         // the target didn't specify the coordinates for the reply.
 800         handleDragFinished(success, action, xRoot, yRoot);
 801     }
 802 
 803     public void handleDragFinished(boolean success, int action, int x, int y) {
 804         dragDropFinished(success, action, x, y);
 805 
 806         dndInProgress = false;
 807         cleanup(XConstants.CurrentTime);
 808     }
 809 }