|
@@ -0,0 +1,121 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2008 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
|
+
|
|
27
|
+#include <Carbon/Carbon.h>
|
|
28
|
+#include <JavaVM/jni.h>
|
|
29
|
+
|
|
30
|
+/** The method to callback */
|
|
31
|
+static jmethodID callbackMethod;
|
|
32
|
+
|
|
33
|
+/** The JVM our callback is in */
|
|
34
|
+static JavaVM *jvm;
|
|
35
|
+
|
|
36
|
+/** The global reference to the Apple object that wants the callback */
|
|
37
|
+static jobject apple;
|
|
38
|
+
|
|
39
|
+/** Callback from OS X with URL. */
|
|
40
|
+static OSErr openURLCallback(const AppleEvent *theAppleEvent, AppleEvent* reply, long handlerRefcon);
|
|
41
|
+
|
|
42
|
+/**
|
|
43
|
+ * JNI Method to register interest in callback.
|
|
44
|
+ * Obtained from:
|
|
45
|
+ * javah -classpath DMDirc.jar com.dmdirc.ui.swing.Apple
|
|
46
|
+ * Reference:
|
|
47
|
+ * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/func/AEInstallEventHandler
|
|
48
|
+ *
|
|
49
|
+ * @param env The JNIEnvironment for this callback.
|
|
50
|
+ * @param this The object that is registering the callback
|
|
51
|
+ */
|
|
52
|
+JNIEXPORT jint JNICALL Java_com_dmdirc_ui_swing_Apple_registerOpenURLCallback (JNIEnv *env, jobject object) {
|
|
53
|
+ // Find the callback in the object
|
|
54
|
+ callbackMethod = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, object), "handleOpenURL", "(Ljava/lang/String;)V");
|
|
55
|
+
|
|
56
|
+ // Check if the callback exists.
|
|
57
|
+ if (callbackMethod != 0) {
|
|
58
|
+ // Store the JVM for this callback, and a global reference to the Apple object
|
|
59
|
+ (*env)->GetJavaVM(env, &jvm);
|
|
60
|
+ apple = (*env)->NewGlobalRef(env, object);
|
|
61
|
+
|
|
62
|
+ // Now register the callback to ourself.
|
|
63
|
+ return (jint)AEInstallEventHandler(kInternetEventClass, kAEGetURL, NewAEEventHandlerUPP((AEEventHandlerProcPtr)openURLCallback), 0, false);
|
|
64
|
+ } else {
|
|
65
|
+ return 1;
|
|
66
|
+ }
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+/**
|
|
71
|
+ * Callback from OS X with URL.
|
|
72
|
+ * Reference:
|
|
73
|
+ * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/func/NewAEEventHandlerUPP
|
|
74
|
+ * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/tdef/AEEventHandlerUPP
|
|
75
|
+ * http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/c/tdef/AEEventHandlerProcPtr
|
|
76
|
+ *
|
|
77
|
+ * @param theAppleEvent Pointer to apple event handle
|
|
78
|
+ * @param reply Pointer to default reply event
|
|
79
|
+ * @param handlerRefcon Reference constant for this callback. (Ignored)
|
|
80
|
+ */
|
|
81
|
+static OSErr openURLCallback(const AppleEvent *theAppleEvent, AppleEvent* reply, long handlerRefcon) {
|
|
82
|
+ OSErr result = noErr;
|
|
83
|
+ Size givenLength = 0;
|
|
84
|
+ DescType type = typeChar;
|
|
85
|
+
|
|
86
|
+ // Get the size of the string the callback wants to give us, and then
|
|
87
|
+ // check that it is > 0 in length.
|
|
88
|
+ result = AESizeOfParam(theAppleEvent, keyDirectObject, &type, &givenLength);
|
|
89
|
+ if (result == noErr && givenLength != 0) {
|
|
90
|
+ // Allocate a buffer for the result
|
|
91
|
+ // givenLength +1 for the \0
|
|
92
|
+ Size length = givenLength + 1;
|
|
93
|
+ char *dataPtr = (char*)malloc(length);
|
|
94
|
+
|
|
95
|
+ if (dataPtr != 0) {
|
|
96
|
+ // Empty the buffer
|
|
97
|
+ memset(dataPtr, 0, length);
|
|
98
|
+
|
|
99
|
+ // Get the url
|
|
100
|
+ result = AEGetParamPtr(theAppleEvent, keyDirectObject, typeChar, 0, dataPtr, givenLength, &givenLength);
|
|
101
|
+ // Did we get it?
|
|
102
|
+ if (result == noErr) {
|
|
103
|
+ // Get the java environment for the jvm we want to callback to
|
|
104
|
+ JNIEnv *env;
|
|
105
|
+ (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
|
|
106
|
+
|
|
107
|
+ // Convert the url into a java string
|
|
108
|
+ jstring theURL = (*env)->NewStringUTF(env, dataPtr);
|
|
109
|
+
|
|
110
|
+ // Call the method!
|
|
111
|
+ (*env)->CallVoidMethod(env, apple, callbackMethod, theURL);
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ // Free the buffer
|
|
115
|
+ free(dataPtr);
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ // And return!
|
|
120
|
+ return result;
|
|
121
|
+}
|