aboutsummaryrefslogtreecommitdiffstats
path: root/Java/phidget_jni.c
blob: 1ec7ca67e70fc50657f816480681b892b1da726f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "../stdafx.h"
#include <stdlib.h>
#include "../cphidget.h"
#include "phidget_jni.h"
#include <stdio.h>

JavaVM *ph_vm = 0;

jclass phidget_class;
jmethodID phidget_cons;

jclass ph_exception_class;
jmethodID ph_exception_cons;

jclass attachEvent_class;
jmethodID attachEvent_cons;

jclass detachEvent_class;
jmethodID detachEvent_cons;

jclass errorEvent_class;
jmethodID errorEvent_cons;

jclass serverConnectEvent_class;
jmethodID serverConnectEvent_cons;

jclass serverDisconnectEvent_class;
jmethodID serverDisconnectEvent_cons;

jint JNICALL
JNI_OnLoad(JavaVM *vm, void *reserved)
{
	JNIEnv *env;
	jint version = 0;
	jint result;

	ph_vm = vm;

	result = (*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4);
	
	if(result == JNI_EDETACHED)
		if ((*vm)->AttachCurrentThread(vm, (JNIEnvPtr)&env, NULL))
			JNI_ABORT_STDERR("Couldn't Attach Thread");
	
	if(!(version = (*env)->GetVersion(env)))
		JNI_ABORT_STDERR("Couldn't get version");
	LOG(PHIDGET_LOG_DEBUG,"JNI Version: %08x",version);

	//Load all Phidget classes, and needed methods and fields...
	//Phidget
	if (!(phidget_class = (*env)->FindClass(env, "com/phidgets/Phidget")))
		JNI_ABORT_STDERR("");
	if (!(phidget_class = (jclass)(*env)->NewGlobalRef(env, phidget_class)))
		JNI_ABORT_STDERR("");
	if (!(phidget_cons = (*env)->GetMethodID(env, phidget_class, "<init>", "(J)V")))
		JNI_ABORT_STDERR("");

	//PhidgetException
	if (!(ph_exception_class = (*env)->FindClass(env, "com/phidgets/PhidgetException")))
		JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/PhidgetException");
	if (!(ph_exception_class = (jclass)(*env)->NewGlobalRef(env, ph_exception_class)))
		JNI_ABORT_STDERR("Couldn't create global ref ph_exception_class");
	if (!(ph_exception_cons = (*env)->GetMethodID(env, ph_exception_class, "<init>", "(ILjava/lang/String;)V")))
		JNI_ABORT_STDERR("Couldn't get Method ID <init> from ph_exception_class");

	//AttachEvent
	if (!(attachEvent_class = (*env)->FindClass(env, "com/phidgets/event/AttachEvent")))
		JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/AttachEvent");
	if (!(attachEvent_class = (jclass)(*env)->NewGlobalRef(env, attachEvent_class)))
		JNI_ABORT_STDERR("Couldn't create global ref attachEvent_class");
	if (!(attachEvent_cons = (*env)->GetMethodID(env, attachEvent_class, "<init>", "(Lcom/phidgets/Phidget;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from attachEvent_class");

	//DetachEvent
	if (!(detachEvent_class = (*env)->FindClass(env, "com/phidgets/event/DetachEvent")))
		JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/DetachEvent");
	if (!(detachEvent_class = (jclass)(*env)->NewGlobalRef(env, detachEvent_class)))
		JNI_ABORT_STDERR("Couldn't create global ref detachEvent_class");
	if (!(detachEvent_cons = (*env)->GetMethodID(env, detachEvent_class, "<init>", "(Lcom/phidgets/Phidget;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from detachEvent_class");

	//ErrorEvent
	if (!(errorEvent_class = (*env)->FindClass(env, "com/phidgets/event/ErrorEvent")))
		JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/ErrorEvent");
	if (!(errorEvent_class = (jclass)(*env)->NewGlobalRef(env, errorEvent_class)))
		JNI_ABORT_STDERR("Couldn't create global ref errorEvent_class");
	if (!(errorEvent_cons = (*env)->GetMethodID(env, errorEvent_class, "<init>", "(Lcom/phidgets/Phidget;Lcom/phidgets/PhidgetException;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from errorEvent_class");

	//ServerConnectEvent
	if (!(serverConnectEvent_class = (*env)->FindClass(env, "com/phidgets/event/ServerConnectEvent")))
		JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/ServerConnectEvent");
	if (!(serverConnectEvent_class = (jclass)(*env)->NewGlobalRef(env, serverConnectEvent_class)))
		JNI_ABORT_STDERR("Couldn't create global ref serverConnectEvent_class");
	if (!(serverConnectEvent_cons = (*env)->GetMethodID(env, serverConnectEvent_class, "<init>", "(Ljava/lang/Object;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from serverConnectEvent_class");

	//ServerDisconnectEvent
	if (!(serverDisconnectEvent_class = (*env)->FindClass(env, "com/phidgets/event/ServerDisconnectEvent")))
		JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/ServerDisconnectEvent");
	if (!(serverDisconnectEvent_class = (jclass)(*env)->NewGlobalRef(env, serverDisconnectEvent_class)))
		JNI_ABORT_STDERR("Couldn't create global ref serverDisconnectEvent_class");
	if (!(serverDisconnectEvent_cons = (*env)->GetMethodID(env, serverDisconnectEvent_class, "<init>", "(Ljava/lang/Object;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from serverDisconnectEvent_class");

	com_phidgets_Phidget_OnLoad(env);
	com_phidgets_Manager_OnLoad(env);
	com_phidgets_Dictionary_OnLoad(env);
	com_phidgets_DictionaryKeyListener_OnLoad(env);
	com_phidgets_AccelerometerPhidget_OnLoad(env);
	com_phidgets_AdvancedServoPhidget_OnLoad(env);
	com_phidgets_AnalogPhidget_OnLoad(env);
	com_phidgets_BridgePhidget_OnLoad(env);
	com_phidgets_EncoderPhidget_OnLoad(env);
    com_phidgets_FrequencyCounterPhidget_OnLoad(env);
    com_phidgets_GPSPhidget_OnLoad(env);
	com_phidgets_InterfaceKitPhidget_OnLoad(env);
	com_phidgets_IRPhidget_OnLoad(env);
	com_phidgets_LEDPhidget_OnLoad(env);
	com_phidgets_MotorControlPhidget_OnLoad(env);
	com_phidgets_PHSensorPhidget_OnLoad(env);
	com_phidgets_RFIDPhidget_OnLoad(env);
	com_phidgets_ServoPhidget_OnLoad(env);
	com_phidgets_SpatialPhidget_OnLoad(env);
	com_phidgets_StepperPhidget_OnLoad(env);
	com_phidgets_TemperatureSensorPhidget_OnLoad(env);
	com_phidgets_TextLCDPhidget_OnLoad(env);
	com_phidgets_TextLEDPhidget_OnLoad(env);
	com_phidgets_WeightSensorPhidget_OnLoad(env);
	
#ifdef _ANDROID
	if(com_phidgets_usb_Phidget_OnLoad(env) && com_phidgets_usb_Manager_OnLoad(env))
		AndroidUsbJarAvailable = PTRUE;
	else
		AndroidUsbJarAvailable = PFALSE;
#endif

	return JNI_VERSION_1_4;
}

jlong
updateGlobalRef(JNIEnv *env, jobject obj, jfieldID fid, jboolean b)
{
	/*
	 * Manages the global reference held by phidget21 to the handler
	 * target.
	 */
	jlong gr;

	if ((gr = (*env)->GetLongField(env, obj, fid)) != 0)
		(*env)->DeleteGlobalRef(env, (jobject)(uintptr_t)gr);
	gr = b ? (jlong)(uintptr_t)(*env)->NewGlobalRef(env, obj) : 0;
	(*env)->SetLongField(env, obj, fid, gr);

	return gr;
}