jdk/src/solaris/native/sun/awt/awt_Selection.c
changeset 1192 715cf9378c53
parent 1051 90cf935adb35
parent 1191 f142c1da78c2
child 1193 41afb8ee8f45
equal deleted inserted replaced
1051:90cf935adb35 1192:715cf9378c53
     1 /*
       
     2  * Copyright 1996-2003 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 #ifdef HEADLESS
       
    27     #error This file should not be included in headless library
       
    28 #endif
       
    29 
       
    30 #include "awt_p.h"
       
    31 #include "awt_DataTransferer.h"
       
    32 #include "java_awt_datatransfer_Transferable.h"
       
    33 #include "java_awt_datatransfer_DataFlavor.h"
       
    34 #include "sun_awt_motif_X11Selection.h"
       
    35 #include "sun_awt_motif_X11Clipboard.h"
       
    36 #include <X11/Intrinsic.h>
       
    37 #include <X11/Xatom.h>
       
    38 #include <inttypes.h>
       
    39 
       
    40 #include <jni.h>
       
    41 #include <jni_util.h>
       
    42 
       
    43 /* fieldIDs for X11Selection fields that may be accessed from C */
       
    44 static struct X11SelectionIDs {
       
    45     jfieldID holder;
       
    46     jfieldID atom;
       
    47     jfieldID contents;
       
    48     jfieldID selections;
       
    49 } x11SelectionIDs;
       
    50 
       
    51 DECLARE_JAVA_CLASS(selectionClazz, "sun/awt/motif/X11Selection")
       
    52 
       
    53 static jobject
       
    54 call_getSelectionsArray(JNIEnv* env) {
       
    55     DECLARE_STATIC_OBJECT_JAVA_METHOD(getSelectionsArray, selectionClazz,
       
    56                                     "getSelectionsArray", "()[Ljava/lang/Object;")
       
    57     DASSERT(!JNU_IsNull(env, getSelectionsArray));
       
    58     return (*env)->CallStaticObjectMethod(env, clazz, getSelectionsArray);
       
    59 }
       
    60 
       
    61 static void
       
    62 call_checkChange(JNIEnv* env, jobject jselection, jlongArray targetArray)
       
    63 {
       
    64     DECLARE_VOID_JAVA_METHOD(checkChangeMID, selectionClazz,
       
    65                              "checkChange", "([J)V")
       
    66     DASSERT(!JNU_IsNull(env, jselection));
       
    67 
       
    68     (*env)->CallVoidMethod(env, jselection, checkChangeMID, targetArray);
       
    69 }
       
    70 
       
    71 static jlongArray
       
    72 call_getSelectionAtomsToCheckChange(JNIEnv* env)
       
    73 {
       
    74     DECLARE_STATIC_OBJECT_JAVA_METHOD(getSelectionAtomsToCheckChangeMID,
       
    75             selectionClazz, "getSelectionAtomsToCheckChange", "()[J")
       
    76 
       
    77     return (jlongArray)(*env)->CallStaticObjectMethod(env,
       
    78             get_selectionClazz(env), getSelectionAtomsToCheckChangeMID);
       
    79 
       
    80 }
       
    81 
       
    82 
       
    83 /*
       
    84  * Class:     sun_awt_motif_X11Selection
       
    85  * Method:    initIDs
       
    86  * Signature: ()V
       
    87  */
       
    88 /* This function gets called from the static initializer for
       
    89    X11Selection.java to initialize the fieldIDs for fields that may
       
    90    be accessed from C */
       
    91 JNIEXPORT void JNICALL Java_sun_awt_motif_X11Selection_initIDs
       
    92     (JNIEnv *env, jclass cls)
       
    93 {
       
    94     x11SelectionIDs.holder = (*env)->
       
    95         GetFieldID(env, cls, "holder","Lsun/awt/motif/X11SelectionHolder;");
       
    96     x11SelectionIDs.atom = (*env)->GetFieldID(env, cls, "atom", "J");
       
    97     x11SelectionIDs.contents = (*env)->
       
    98         GetFieldID(env, cls, "contents",
       
    99                    "Ljava/awt/datatransfer/Transferable;");
       
   100     x11SelectionIDs.selections = (*env)->
       
   101         GetStaticFieldID(env, cls, "selections", "Ljava/util/Vector;");
       
   102 }
       
   103 
       
   104 /*
       
   105  * Class:     sun_awt_motif_X11Selection
       
   106  * Method:    init
       
   107  * Signature: ()V
       
   108  */
       
   109 JNIEXPORT void JNICALL Java_sun_awt_motif_X11Selection_init
       
   110     (JNIEnv *env, jclass this)
       
   111 {
       
   112     AWT_LOCK();
       
   113 
       
   114     AWT_UNLOCK();
       
   115 }
       
   116 
       
   117 static jobject
       
   118 getX11Selection(JNIEnv * env, Atom atom)
       
   119 {
       
   120     jobjectArray selections;
       
   121     jsize selectionCount, i;
       
   122     jobject selection;
       
   123     jobject returnSelection = NULL;
       
   124 
       
   125     selections = (jobjectArray)call_getSelectionsArray(env);
       
   126 
       
   127     if (JNU_IsNull(env, selections)) {
       
   128         return NULL;
       
   129     }
       
   130 
       
   131     selectionCount = (*env)->GetArrayLength(env, selections);
       
   132 
       
   133     for (i = 0; i < selectionCount; i++) {
       
   134         selection = (*env)->GetObjectArrayElement(env, selections, i);
       
   135         if ((*env)->ExceptionCheck(env)) {
       
   136             (*env)->ExceptionDescribe(env);
       
   137             (*env)->ExceptionClear(env);
       
   138             break;
       
   139         }
       
   140         if (JNU_IsNull(env, selection)) {
       
   141             break;
       
   142         }
       
   143         if ((*env)->GetLongField(env, selection, x11SelectionIDs.atom) == atom) {
       
   144             returnSelection = selection;
       
   145         } else {
       
   146             (*env)->DeleteLocalRef(env, selection);
       
   147         }
       
   148     }
       
   149 
       
   150     (*env)->DeleteLocalRef(env, selections);
       
   151 
       
   152     return returnSelection;
       
   153 }
       
   154 
       
   155 Boolean
       
   156 awtJNI_isSelectionOwner(JNIEnv * env, char *sel_str)
       
   157 {
       
   158     Atom selection;
       
   159     jobject x11sel;
       
   160 
       
   161     selection = XInternAtom(awt_display, sel_str, False);
       
   162 
       
   163     x11sel = getX11Selection(env, selection);
       
   164     if (!JNU_IsNull(env, x11sel)) {
       
   165         jobject holder;
       
   166 
       
   167         holder = (*env)->GetObjectField(env, x11sel, x11SelectionIDs.holder);
       
   168         if (!JNU_IsNull(env, holder)) {
       
   169             return TRUE;
       
   170         }
       
   171     }
       
   172     return FALSE;
       
   173 }
       
   174 
       
   175 static void losingSelectionOwnership(Widget w, Atom * selection);
       
   176 
       
   177 void
       
   178 awtJNI_notifySelectionLost(JNIEnv * env, char *sel_str)
       
   179 {
       
   180     Atom selection;
       
   181 
       
   182     selection = XInternAtom(awt_display, sel_str, False);
       
   183     losingSelectionOwnership(NULL, &selection);
       
   184 }
       
   185 
       
   186 static void
       
   187 losingSelectionOwnership(Widget w, Atom * selection)
       
   188 {
       
   189     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
       
   190     jobject this = getX11Selection(env, *selection);
       
   191 
       
   192     /*
       
   193      * SECURITY: OK to call this on privileged thread - peer does
       
   194      *         not call into client code
       
   195      */
       
   196     JNU_CallMethodByName(env, NULL, this, "lostSelectionOwnership", "()V");
       
   197     if ((*env)->ExceptionOccurred(env)) {
       
   198         (*env)->ExceptionDescribe(env);
       
   199         (*env)->ExceptionClear(env);
       
   200     }
       
   201     /*
       
   202      * Fix for 4692059.
       
   203      * The native context is cleaned up on the event dispatch thread after the
       
   204      * references to the current contents and owner are cleared.
       
   205      */
       
   206 }
       
   207 
       
   208 /*
       
   209  * Class:     sun_awt_motif_X11Selection
       
   210  * Method:    pGetSelectionOwnership
       
   211  * Signature: (Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;[JLjava/util/Map;Lsun/awt/motif/X11SelectionHolder;)Z
       
   212  */
       
   213 JNIEXPORT jboolean JNICALL
       
   214 Java_sun_awt_motif_X11Selection_pGetSelectionOwnership(JNIEnv *env,
       
   215                                                        jobject this,
       
   216                                                        jobject source,
       
   217                                                        jobject transferable,
       
   218                                                        jlongArray formats,
       
   219                                                        jobject formatMap,
       
   220                                                        jobject holder)
       
   221 {
       
   222     Boolean gotit = False;
       
   223     Atom selection = (Atom)(*env)->GetLongField(env, this,
       
   224                                                 x11SelectionIDs.atom);
       
   225     awt_convertDataCallbackStruct* structPtr = NULL;
       
   226     Time time = CurrentTime;
       
   227 
       
   228     AWT_LOCK();
       
   229 
       
   230     time = awt_util_getCurrentServerTime();
       
   231 
       
   232     (*env)->SetObjectField(env, this, x11SelectionIDs.holder, NULL);
       
   233     (*env)->SetObjectField(env, this, x11SelectionIDs.contents, NULL);
       
   234 
       
   235     gotit = XtOwnSelection(awt_root_shell, selection, time, awt_convertData,
       
   236                            losingSelectionOwnership, NULL);
       
   237 
       
   238     if (gotit) {
       
   239         if (XFindContext(awt_display, selection, awt_convertDataContext,
       
   240                          (XPointer*)&structPtr) == 0 && structPtr != NULL) {
       
   241             (*env)->DeleteGlobalRef(env, structPtr->source);
       
   242             (*env)->DeleteGlobalRef(env, structPtr->transferable);
       
   243             (*env)->DeleteGlobalRef(env, structPtr->formatMap);
       
   244             (*env)->DeleteGlobalRef(env, structPtr->formats);
       
   245             memset(structPtr, 0, sizeof(awt_convertDataCallbackStruct));
       
   246         } else {
       
   247             XDeleteContext(awt_display, selection, awt_convertDataContext);
       
   248 
       
   249             structPtr = calloc(1, sizeof(awt_convertDataCallbackStruct));
       
   250 
       
   251             if (structPtr == NULL) {
       
   252                 XtDisownSelection(awt_root_shell, selection, time);
       
   253                 AWT_UNLOCK();
       
   254                 JNU_ThrowOutOfMemoryError(env, "");
       
   255                 return JNI_FALSE;
       
   256             }
       
   257 
       
   258             if (XSaveContext(awt_display, selection, awt_convertDataContext,
       
   259                              (XPointer)structPtr) == XCNOMEM) {
       
   260                 XtDisownSelection(awt_root_shell, selection, time);
       
   261                 free(structPtr);
       
   262                 AWT_UNLOCK();
       
   263                 JNU_ThrowInternalError(env, "Failed to save context data for selection.");
       
   264                 return JNI_FALSE;
       
   265             }
       
   266         }
       
   267 
       
   268         structPtr->source = (*env)->NewGlobalRef(env, source);
       
   269         structPtr->transferable = (*env)->NewGlobalRef(env, transferable);
       
   270         structPtr->formatMap = (*env)->NewGlobalRef(env, formatMap);
       
   271         structPtr->formats = (*env)->NewGlobalRef(env, formats);
       
   272 
       
   273         if (JNU_IsNull(env, structPtr->source) ||
       
   274             JNU_IsNull(env, structPtr->transferable) ||
       
   275             JNU_IsNull(env, structPtr->formatMap) ||
       
   276             JNU_IsNull(env, structPtr->formats)) {
       
   277 
       
   278             if (!JNU_IsNull(env, structPtr->source)) {
       
   279                 (*env)->DeleteGlobalRef(env, structPtr->source);
       
   280             }
       
   281             if (!JNU_IsNull(env, structPtr->transferable)) {
       
   282                 (*env)->DeleteGlobalRef(env, structPtr->transferable);
       
   283             }
       
   284             if (!JNU_IsNull(env, structPtr->formatMap)) {
       
   285                 (*env)->DeleteGlobalRef(env, structPtr->formatMap);
       
   286             }
       
   287             if (!JNU_IsNull(env, structPtr->formats)) {
       
   288                 (*env)->DeleteGlobalRef(env, structPtr->formats);
       
   289             }
       
   290             XtDisownSelection(awt_root_shell, selection, time);
       
   291             XDeleteContext(awt_display, selection, awt_convertDataContext);
       
   292             free(structPtr);
       
   293             AWT_UNLOCK();
       
   294             JNU_ThrowOutOfMemoryError(env, "");
       
   295             return JNI_FALSE;
       
   296         }
       
   297 
       
   298         (*env)->SetObjectField(env, this, x11SelectionIDs.holder, holder);
       
   299         (*env)->SetObjectField(env, this, x11SelectionIDs.contents, transferable);
       
   300     }
       
   301     AWT_UNLOCK();
       
   302 
       
   303     return (gotit ? JNI_TRUE : JNI_FALSE);
       
   304 }
       
   305 
       
   306 /*
       
   307  * Class:     sun_awt_motif_X11Selection
       
   308  * Method:    clearNativeContext
       
   309  * Signature: ()V
       
   310  */
       
   311 JNIEXPORT void JNICALL
       
   312 Java_sun_awt_motif_X11Selection_clearNativeContext(JNIEnv *env, jobject this) {
       
   313     Atom selection = (Atom)(*env)->GetLongField(env, this,
       
   314                                                 x11SelectionIDs.atom);
       
   315 
       
   316     AWT_LOCK();
       
   317 
       
   318     XtDisownSelection(awt_root_shell, selection, CurrentTime);
       
   319     awt_cleanupConvertDataContext(env, selection);
       
   320 
       
   321     AWT_UNLOCK();
       
   322 }
       
   323 
       
   324 
       
   325 static void
       
   326 getSelectionTargetsToCheckChange(Widget w, XtPointer client_data,
       
   327         Atom * selection, Atom * type, XtPointer value, unsigned long *length,
       
   328         int32_t *format)
       
   329 {
       
   330     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
       
   331     size_t count = 0, i = 0, j = 0;
       
   332     jlongArray targetArray = NULL;
       
   333 
       
   334     // Should keep this in sync with getSelectionTargets() so that
       
   335     // this function yields non-null targetArray iff
       
   336     // getSelectionTargets() yields SelectionSuccess.
       
   337     if (*type == XA_TARGETS || *type == XA_ATOM) {
       
   338         targetArray = getSelectionTargetsHelper(env, value, *length);
       
   339     } else if (*type != XT_CONVERT_FAIL) {
       
   340         targetArray = (*env)->NewLongArray(env, 0);
       
   341     }
       
   342 
       
   343     if (value != NULL) {
       
   344         XtFree(value);
       
   345         value = NULL;
       
   346     }
       
   347 
       
   348     {
       
   349         jobject jselection = getX11Selection(env, *selection);
       
   350         call_checkChange(env, jselection, targetArray);
       
   351         if ((*env)->ExceptionCheck(env)) {
       
   352             (*env)->ExceptionDescribe(env);
       
   353             (*env)->ExceptionClear(env);
       
   354         }
       
   355         (*env)->DeleteLocalRef(env, targetArray);
       
   356         (*env)->DeleteLocalRef(env, jselection);
       
   357     }
       
   358 }
       
   359 
       
   360 
       
   361 static Atom _XA_JAVA_TIME_PROPERTY_ATOM_CHECK_SELECTION_CHANGE_ON_TIMEOUT = 0;
       
   362 
       
   363 static void
       
   364 checkSelectionChangeOnTimeout(XtPointer client_data, XtIntervalId* id)
       
   365 {
       
   366     // We don't call XtGetSelectionValue(..., TARGETS, ..., awt_util_getCurrentServerTime())
       
   367     // here because awt_util_getCurrentServerTime() may block toolkit therad for a while
       
   368     // whereas the current function is called very often at regular intervals.
       
   369     // Instead we call XtGetSelectionValue(..., XtLastTimestampProcessed(awt_display))
       
   370     // in the property change event handler wherein we have got an up-to-date timestamp.
       
   371 
       
   372     XChangeProperty(awt_display, XtWindow(awt_root_shell),
       
   373                     _XA_JAVA_TIME_PROPERTY_ATOM_CHECK_SELECTION_CHANGE_ON_TIMEOUT,
       
   374                     XA_ATOM, 32, PropModeAppend, (unsigned char *)"", 0);
       
   375     XFlush(awt_display);
       
   376 }
       
   377 
       
   378 
       
   379 static unsigned long selectionPollInterval;
       
   380 
       
   381 static void
       
   382 propertyChangeEventHandlerToSelectionCheck
       
   383 (Widget w, XtPointer client_data, XEvent* event, Boolean* continue_to_dispatch)
       
   384 {
       
   385     JNIEnv *env;
       
   386     jlongArray jselectionAtoms;
       
   387 
       
   388     if (event->type != PropertyNotify || event->xproperty.atom !=
       
   389             _XA_JAVA_TIME_PROPERTY_ATOM_CHECK_SELECTION_CHANGE_ON_TIMEOUT) {
       
   390         return;
       
   391     }
       
   392 
       
   393     env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
       
   394     jselectionAtoms = call_getSelectionAtomsToCheckChange(env);
       
   395 
       
   396     DASSERT(!JNU_IsNull(env, jselectionAtoms));
       
   397     if ((*env)->ExceptionCheck(env)) {
       
   398         (*env)->ExceptionDescribe(env);
       
   399         (*env)->ExceptionClear(env);
       
   400     } else {
       
   401         jsize len = (*env)->GetArrayLength(env, jselectionAtoms);
       
   402         jlong* selectionAtomsNative =
       
   403                 (*env)->GetLongArrayElements(env, jselectionAtoms, NULL);
       
   404         if (!JNU_IsNull(env, selectionAtomsNative)) {
       
   405             jsize i = 0;
       
   406             for (i = 0; i < len; i++) {
       
   407                 XtGetSelectionValue(awt_root_shell, (Atom)selectionAtomsNative[i], XA_TARGETS,
       
   408                                     getSelectionTargetsToCheckChange, (XtPointer)NULL,
       
   409                                     XtLastTimestampProcessed(awt_display));
       
   410             }
       
   411             (*env)->ReleaseLongArrayElements(env, jselectionAtoms,
       
   412                                              selectionAtomsNative, JNI_ABORT);
       
   413         }
       
   414     }
       
   415 
       
   416     // Reschedule the timer callback.
       
   417     XtAppAddTimeOut(awt_appContext, selectionPollInterval,
       
   418                     checkSelectionChangeOnTimeout, client_data);
       
   419 }
       
   420 
       
   421 
       
   422 static BOOL isClipboardViewerRegistered = FALSE;
       
   423 
       
   424 /*
       
   425  * Class:     sun_awt_motif_X11Clipboard
       
   426  * Method:    registerClipboardViewer
       
   427  * Signature: (I)V
       
   428  */
       
   429 JNIEXPORT void JNICALL
       
   430 Java_sun_awt_motif_X11Clipboard_registerClipboardViewer(JNIEnv *env, jobject self,
       
   431                                                         jint pollInterval)
       
   432 {
       
   433     AWT_LOCK();
       
   434 
       
   435     if (isClipboardViewerRegistered) {
       
   436         AWT_UNLOCK();
       
   437         return;
       
   438     }
       
   439 
       
   440     if (_XA_JAVA_TIME_PROPERTY_ATOM_CHECK_SELECTION_CHANGE_ON_TIMEOUT == 0) {
       
   441         _XA_JAVA_TIME_PROPERTY_ATOM_CHECK_SELECTION_CHANGE_ON_TIMEOUT =
       
   442                 XInternAtom(awt_display,
       
   443                             "_SUNW_JAVA_AWT_TIME_CHECK_SELECTION_CHANGE_ON_TIMEOUT",
       
   444                             False);
       
   445     }
       
   446 
       
   447     XtAddEventHandler(awt_root_shell, PropertyChangeMask, False,
       
   448                       propertyChangeEventHandlerToSelectionCheck, NULL);
       
   449 
       
   450     selectionPollInterval = pollInterval;
       
   451 
       
   452     XtAppAddTimeOut(awt_appContext, selectionPollInterval,
       
   453                     checkSelectionChangeOnTimeout, (XtPointer)NULL);
       
   454 
       
   455     isClipboardViewerRegistered = TRUE;
       
   456 
       
   457     AWT_UNLOCK();
       
   458 }
       
   459 
       
   460 /*
       
   461  * Class:     sun_awt_motif_X11Clipboard
       
   462  * Method:    unregisterClipboardViewer
       
   463  * Signature: ()V
       
   464  */
       
   465 JNIEXPORT void JNICALL
       
   466 Java_sun_awt_motif_X11Clipboard_unregisterClipboardViewer(JNIEnv *env, jobject self)
       
   467 {
       
   468     AWT_LOCK();
       
   469 
       
   470     if (!isClipboardViewerRegistered) {
       
   471         AWT_UNLOCK();
       
   472         return;
       
   473     }
       
   474 
       
   475     XtRemoveEventHandler(awt_root_shell, PropertyChangeMask, False,
       
   476                          propertyChangeEventHandlerToSelectionCheck, NULL);
       
   477 
       
   478     isClipboardViewerRegistered = FALSE;
       
   479 
       
   480     AWT_UNLOCK();
       
   481 }
       
   482 
       
   483 
       
   484 /*
       
   485  * Class:     sun_awt_motif_X11Clipboard
       
   486  * Method:    getClipboardFormats
       
   487  * Signature: (J)[J
       
   488  */
       
   489 JNIEXPORT jlongArray JNICALL
       
   490 Java_sun_awt_motif_X11Clipboard_getClipboardFormats
       
   491     (JNIEnv *env, jclass cls, jlong selectionAtom)
       
   492 {
       
   493     Time time_stamp = awt_util_getCurrentServerTime();
       
   494     return get_selection_targets(env, selectionAtom, time_stamp);
       
   495 }
       
   496 
       
   497 /*
       
   498  * Class:     sun_awt_motif_X11Clipboard
       
   499  * Method:    getClipboardData
       
   500  * Signature: (JJ)[B
       
   501  */
       
   502 JNIEXPORT jbyteArray JNICALL
       
   503 Java_sun_awt_motif_X11Clipboard_getClipboardData
       
   504     (JNIEnv *env, jclass cls, jlong selectionAtom, jlong format)
       
   505 {
       
   506     Time time_stamp = awt_util_getCurrentServerTime();
       
   507     return get_selection_data(env, selectionAtom, format, time_stamp);
       
   508 }