phidget_jni.c
Go to the documentation of this file.
00001 #include "../stdafx.h"
00002 #include <stdlib.h>
00003 #include "../cphidget.h"
00004 #include "phidget_jni.h"
00005 #include <stdio.h>
00006 
00007 JavaVM *ph_vm = 0;
00008 
00009 jclass phidget_class;
00010 jmethodID phidget_cons;
00011 
00012 jclass ph_exception_class;
00013 jmethodID ph_exception_cons;
00014 
00015 jclass attachEvent_class;
00016 jmethodID attachEvent_cons;
00017 
00018 jclass detachEvent_class;
00019 jmethodID detachEvent_cons;
00020 
00021 jclass errorEvent_class;
00022 jmethodID errorEvent_cons;
00023 
00024 jclass serverConnectEvent_class;
00025 jmethodID serverConnectEvent_cons;
00026 
00027 jclass serverDisconnectEvent_class;
00028 jmethodID serverDisconnectEvent_cons;
00029 
00030 #ifdef _WINDOWS
00031 
00032 extern int useThreadSecurity;
00033 extern void destroyThreadSecurityAttributes();
00034 
00035 #endif
00036 
00037 static void detachCurrentThreadFromJavaVM(void)
00038 {
00039         JNIEnv *env;
00040         if(ph_vm != NULL && *ph_vm != NULL)
00041         {
00042                 if((*ph_vm)->GetEnv(ph_vm, (void **)&env, JNI_VERSION_1_4) != JNI_EDETACHED)
00043                 {
00044                         (*ph_vm)->DetachCurrentThread(ph_vm);
00045                 }
00046         }
00047 }
00048 
00049 jint JNICALL
00050 JNI_OnLoad(JavaVM *vm, void *reserved)
00051 {
00052         JNIEnv *env;
00053         jint version = 0;
00054 
00055         ph_vm = vm;
00056 
00057         // Don't set thread security when we're using Java -  this break events.
00058         #ifdef _WINDOWS
00059                 useThreadSecurity = PFALSE;
00060                 destroyThreadSecurityAttributes();
00061         #endif
00062         
00063         if((*ph_vm)->GetEnv(ph_vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK)
00064         {
00065                 return -1;
00066         }
00067         
00068         if(!(version = (*env)->GetVersion(env)))
00069                 JNI_ABORT_STDERR("Couldn't get version");
00070         LOG(PHIDGET_LOG_DEBUG,"JNI Version: %08x",version);
00071 
00072         //Load all Phidget classes, and needed methods and fields...
00073         //Phidget
00074         if (!(phidget_class = (*env)->FindClass(env, "com/phidgets/Phidget")))
00075                 JNI_ABORT_STDERR("");
00076         if (!(phidget_class = (jclass)(*env)->NewGlobalRef(env, phidget_class)))
00077                 JNI_ABORT_STDERR("");
00078         if (!(phidget_cons = (*env)->GetMethodID(env, phidget_class, "<init>", "(J)V")))
00079                 JNI_ABORT_STDERR("");
00080 
00081         //PhidgetException
00082         if (!(ph_exception_class = (*env)->FindClass(env, "com/phidgets/PhidgetException")))
00083                 JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/PhidgetException");
00084         if (!(ph_exception_class = (jclass)(*env)->NewGlobalRef(env, ph_exception_class)))
00085                 JNI_ABORT_STDERR("Couldn't create global ref ph_exception_class");
00086         if (!(ph_exception_cons = (*env)->GetMethodID(env, ph_exception_class, "<init>", "(ILjava/lang/String;)V")))
00087                 JNI_ABORT_STDERR("Couldn't get Method ID <init> from ph_exception_class");
00088 
00089         //AttachEvent
00090         if (!(attachEvent_class = (*env)->FindClass(env, "com/phidgets/event/AttachEvent")))
00091                 JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/AttachEvent");
00092         if (!(attachEvent_class = (jclass)(*env)->NewGlobalRef(env, attachEvent_class)))
00093                 JNI_ABORT_STDERR("Couldn't create global ref attachEvent_class");
00094         if (!(attachEvent_cons = (*env)->GetMethodID(env, attachEvent_class, "<init>", "(Lcom/phidgets/Phidget;)V")))
00095                 JNI_ABORT_STDERR("Couldn't get method ID <init> from attachEvent_class");
00096 
00097         //DetachEvent
00098         if (!(detachEvent_class = (*env)->FindClass(env, "com/phidgets/event/DetachEvent")))
00099                 JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/DetachEvent");
00100         if (!(detachEvent_class = (jclass)(*env)->NewGlobalRef(env, detachEvent_class)))
00101                 JNI_ABORT_STDERR("Couldn't create global ref detachEvent_class");
00102         if (!(detachEvent_cons = (*env)->GetMethodID(env, detachEvent_class, "<init>", "(Lcom/phidgets/Phidget;)V")))
00103                 JNI_ABORT_STDERR("Couldn't get method ID <init> from detachEvent_class");
00104 
00105         //ErrorEvent
00106         if (!(errorEvent_class = (*env)->FindClass(env, "com/phidgets/event/ErrorEvent")))
00107                 JNI_ABORT_STDERR("Coulnd't FindClass com/phidgets/ErrorEvent");
00108         if (!(errorEvent_class = (jclass)(*env)->NewGlobalRef(env, errorEvent_class)))
00109                 JNI_ABORT_STDERR("Couldn't create global ref errorEvent_class");
00110         if (!(errorEvent_cons = (*env)->GetMethodID(env, errorEvent_class, "<init>", "(Lcom/phidgets/Phidget;Lcom/phidgets/PhidgetException;)V")))
00111                 JNI_ABORT_STDERR("Couldn't get method ID <init> from errorEvent_class");
00112 
00113         //ServerConnectEvent
00114         if (!(serverConnectEvent_class = (*env)->FindClass(env, "com/phidgets/event/ServerConnectEvent")))
00115                 JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/ServerConnectEvent");
00116         if (!(serverConnectEvent_class = (jclass)(*env)->NewGlobalRef(env, serverConnectEvent_class)))
00117                 JNI_ABORT_STDERR("Couldn't create global ref serverConnectEvent_class");
00118         if (!(serverConnectEvent_cons = (*env)->GetMethodID(env, serverConnectEvent_class, "<init>", "(Ljava/lang/Object;)V")))
00119                 JNI_ABORT_STDERR("Couldn't get method ID <init> from serverConnectEvent_class");
00120 
00121         //ServerDisconnectEvent
00122         if (!(serverDisconnectEvent_class = (*env)->FindClass(env, "com/phidgets/event/ServerDisconnectEvent")))
00123                 JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/ServerDisconnectEvent");
00124         if (!(serverDisconnectEvent_class = (jclass)(*env)->NewGlobalRef(env, serverDisconnectEvent_class)))
00125                 JNI_ABORT_STDERR("Couldn't create global ref serverDisconnectEvent_class");
00126         if (!(serverDisconnectEvent_cons = (*env)->GetMethodID(env, serverDisconnectEvent_class, "<init>", "(Ljava/lang/Object;)V")))
00127                 JNI_ABORT_STDERR("Couldn't get method ID <init> from serverDisconnectEvent_class");
00128 
00129         com_phidgets_Phidget_OnLoad(env);
00130         com_phidgets_Manager_OnLoad(env);
00131         com_phidgets_Dictionary_OnLoad(env);
00132         com_phidgets_DictionaryKeyListener_OnLoad(env);
00133         com_phidgets_AccelerometerPhidget_OnLoad(env);
00134         com_phidgets_AdvancedServoPhidget_OnLoad(env);
00135         com_phidgets_AnalogPhidget_OnLoad(env);
00136         com_phidgets_BridgePhidget_OnLoad(env);
00137         com_phidgets_EncoderPhidget_OnLoad(env);
00138     com_phidgets_FrequencyCounterPhidget_OnLoad(env);
00139     com_phidgets_GPSPhidget_OnLoad(env);
00140         com_phidgets_InterfaceKitPhidget_OnLoad(env);
00141         com_phidgets_IRPhidget_OnLoad(env);
00142         com_phidgets_LEDPhidget_OnLoad(env);
00143         com_phidgets_MotorControlPhidget_OnLoad(env);
00144         com_phidgets_PHSensorPhidget_OnLoad(env);
00145         com_phidgets_RFIDPhidget_OnLoad(env);
00146         com_phidgets_ServoPhidget_OnLoad(env);
00147         com_phidgets_SpatialPhidget_OnLoad(env);
00148         com_phidgets_StepperPhidget_OnLoad(env);
00149         com_phidgets_TemperatureSensorPhidget_OnLoad(env);
00150         com_phidgets_TextLCDPhidget_OnLoad(env);
00151         com_phidgets_TextLEDPhidget_OnLoad(env);
00152         com_phidgets_WeightSensorPhidget_OnLoad(env);
00153 
00154         //So the main library can detach threads from java before they exit.
00155         fptrJavaDetachCurrentThread = detachCurrentThreadFromJavaVM;
00156         
00157 #ifdef _ANDROID
00158         if(com_phidgets_usb_Phidget_OnLoad(env) && com_phidgets_usb_Manager_OnLoad(env))
00159                 AndroidUsbJarAvailable = PTRUE;
00160         else
00161                 AndroidUsbJarAvailable = PFALSE;
00162 #endif
00163 
00164         return JNI_VERSION_1_4;
00165 }
00166 
00167 jlong
00168 updateGlobalRef(JNIEnv *env, jobject obj, jfieldID fid, jboolean b)
00169 {
00170         /*
00171          * Manages the global reference held by phidget21 to the handler
00172          * target.
00173          */
00174         jlong gr;
00175 
00176         if ((gr = (*env)->GetLongField(env, obj, fid)) != 0)
00177                 (*env)->DeleteGlobalRef(env, (jobject)(uintptr_t)gr);
00178         gr = b ? (jlong)(uintptr_t)(*env)->NewGlobalRef(env, obj) : 0;
00179         (*env)->SetLongField(env, obj, fid, gr);
00180 
00181         return gr;
00182 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Fri Jun 6 2014 18:37:21