Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DMDirc-Apple.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * JNI library for OS X url handling.
  23. * Compile with:
  24. * gcc -dynamiclib -framework JavaVM -framework Carbon -o libDMDirc-Apple.jnilib DMDirc-Apple.c -arch x86_64
  25. */
  26. #include <Carbon/Carbon.h>
  27. #include <JavaVM/jni.h>
  28. /** The method to callback */
  29. static jmethodID callbackMethod;
  30. /** The JVM our callback is in */
  31. static JavaVM *jvm;
  32. /** The global reference to the Apple object that wants the callback */
  33. static jobject apple;
  34. /** Callback from OS X with URL. */
  35. static OSErr openURLCallback(const AppleEvent *theAppleEvent, AppleEvent* reply, long handlerRefcon);
  36. /**
  37. * JNI Method to register interest in callback.
  38. * Obtained from:
  39. * javah -classpath plugins/ui_swing.jar com.dmdirc.addons.ui_swing.Apple
  40. * Reference:
  41. * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/func/AEInstallEventHandler
  42. *
  43. * @param env The JNIEnvironment for this callback.
  44. * @param this The object that is registering the callback
  45. */
  46. JNIEXPORT jint JNICALL Java_com_dmdirc_addons_ui_1swing_Apple_registerOpenURLCallback (JNIEnv *env, jobject object) {
  47. // Find the callback in the object
  48. callbackMethod = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, object), "handleOpenURL", "(Ljava/lang/String;)V");
  49. // Check if the callback exists.
  50. if (callbackMethod != 0) {
  51. // Store the JVM for this callback, and a global reference to the Apple object
  52. (*env)->GetJavaVM(env, &jvm);
  53. apple = (*env)->NewGlobalRef(env, object);
  54. // Now register the callback to ourself.
  55. return (jint)AEInstallEventHandler(kInternetEventClass, kAEGetURL, NewAEEventHandlerUPP((AEEventHandlerProcPtr)openURLCallback), 0, false);
  56. } else {
  57. return 1;
  58. }
  59. }
  60. /**
  61. * Callback from OS X with URL.
  62. * Reference:
  63. * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/func/NewAEEventHandlerUPP
  64. * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/tdef/AEEventHandlerUPP
  65. * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/tdef/AEEventHandlerProcPtr
  66. *
  67. * @param theAppleEvent Pointer to apple event handle
  68. * @param reply Pointer to default reply event
  69. * @param handlerRefcon Reference constant for this callback. (Ignored)
  70. */
  71. static OSErr openURLCallback(const AppleEvent *theAppleEvent, AppleEvent* reply, long handlerRefcon) {
  72. OSErr result = noErr;
  73. Size givenLength = 0;
  74. DescType type = typeChar;
  75. // Get the size of the string the callback wants to give us, and then
  76. // check that it is > 0 in length.
  77. result = AESizeOfParam(theAppleEvent, keyDirectObject, &type, &givenLength);
  78. if (result == noErr && givenLength != 0) {
  79. // Allocate a buffer for the result
  80. // givenLength +1 for the \0
  81. Size length = givenLength + 1;
  82. char *dataPtr = (char*)malloc(length);
  83. if (dataPtr != 0) {
  84. // Empty the buffer
  85. memset(dataPtr, 0, length);
  86. // Get the url
  87. result = AEGetParamPtr(theAppleEvent, keyDirectObject, typeChar, 0, dataPtr, givenLength, &givenLength);
  88. // Did we get it?
  89. if (result == noErr) {
  90. // Get the java environment for the jvm we want to callback to
  91. JNIEnv *env;
  92. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  93. // Convert the url into a java string
  94. jstring theURL = (*env)->NewStringUTF(env, dataPtr);
  95. // Call the method!
  96. (*env)->CallVoidMethod(env, apple, callbackMethod, theURL);
  97. }
  98. // Free the buffer
  99. free(dataPtr);
  100. }
  101. }
  102. // And return!
  103. return result;
  104. }