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