From cda5d8cd4045c09aee07902c0bc5b78f7bab4c7c Mon Sep 17 00:00:00 2001 From: Edouard Dupin Date: Wed, 18 Apr 2012 18:14:13 +0200 Subject: [PATCH] Start of calling the jvm from the C code --- Java/PROJECT_NAME.java | 46 +++++-- Java/ewolAndroidAbstraction.cpp | 158 +++++++++++++++++++++++ Sources/libetk/etk/Debug.cpp | 9 +- Sources/libetk/etk/Stream.h | 6 +- Sources/libewol/ewol/base/gui.cpp | 4 +- Sources/libewol/ewol/base/guiAndroid.cpp | 13 ++ Sources/libewol/ewol/base/guiX11.cpp | 10 ++ Sources/libewol/ewol/widget/Menu.cpp | 25 +++- Sources/libewol/ewol/widget/Menu.h | 7 + 9 files changed, 259 insertions(+), 19 deletions(-) diff --git a/Java/PROJECT_NAME.java b/Java/PROJECT_NAME.java index ed79f4a5..cabae81f 100644 --- a/Java/PROJECT_NAME.java +++ b/Java/PROJECT_NAME.java @@ -35,6 +35,9 @@ import android.content.res.AssetManager; * */ public class __PROJECT_NAME__ extends Activity { + private static native void TouchEvent(); + private static native void ActivitySetJavaVortualMachineStart(); + private static native void ActivitySetJavaVortualMachineStop(); private static native void ActivityOnCreate(); private static native void ActivityOnStart(); private static native void ActivityOnReStart(); @@ -50,9 +53,13 @@ public class __PROJECT_NAME__ extends Activity { System.loadLibrary("__PROJECT_PACKAGE__"); } - @Override protected void onCreate(Bundle savedInstanceState) { + @Override protected void onCreate(Bundle savedInstanceState) + { super.onCreate(savedInstanceState); + // set the java evironement in the C sources : + ActivitySetJavaVortualMachineStart(); + // Load the application directory ActivityParamSetArchiveDir(1, getFilesDir().toString()); ActivityParamSetArchiveDir(2, getCacheDir().toString()); @@ -87,45 +94,68 @@ public class __PROJECT_NAME__ extends Activity { setContentView(mGLView); } - @Override protected void onStart() { + @Override protected void onStart() + { super.onStart(); // call C ActivityOnStart(); } - @Override protected void onRestart() { + @Override protected void onRestart() + { super.onRestart(); // call C ActivityOnReStart(); } - @Override protected void onResume() { + @Override protected void onResume() + { super.onResume(); mGLView.onResume(); // call C ActivityOnResume(); } - @Override protected void onPause() { + @Override protected void onPause() + { super.onPause(); mGLView.onPause(); // call C ActivityOnPause(); } - @Override protected void onStop() { + @Override protected void onStop() + { super.onStop(); // call C ActivityOnStop(); } - @Override protected void onDestroy() { + @Override protected void onDestroy() + { super.onDestroy(); // call C ActivityOnDestroy(); + // Remove the java Virtual machine pointer form the C code + ActivitySetJavaVortualMachineStop(); } - public void onConfigurationChanged(Configuration newConfig) { + public void onConfigurationChanged(Configuration newConfig) + { super.onConfigurationChanged(newConfig); } + + public static void eventFromCPP(String[] args) + { + if (args[0] == "Keyboard_Show") { + TouchEvent(); + TouchEvent(); + TouchEvent(); + } else if (args[0] == "Keyboard_Hide") { + TouchEvent(); + TouchEvent(); + } else { + TouchEvent(); + } + } } diff --git a/Java/ewolAndroidAbstraction.cpp b/Java/ewolAndroidAbstraction.cpp index b399e57e..d9e85a99 100644 --- a/Java/ewolAndroidAbstraction.cpp +++ b/Java/ewolAndroidAbstraction.cpp @@ -42,9 +42,123 @@ void EWOL_ThreadEventInputState(int pointerID, bool isUp, float x, float y); void EWOL_NativeRender(void); void EWOL_NativeGLDestroy(void); +// get a resources from the java environement : +static JNIEnv* JavaVirtualMachinePointer = NULL; // the JVM +static jclass javaClassActivity = 0; // main activity class (android ...) +static jmethodID javaClassActivityEntryPoint = 0; // basic methode to call ... +// generic classes +static jclass javaDefaultClassString = 0; // default string class + + +static JavaVM* g_JavaVM = NULL; + + +void SendSystemMessage(const char * dataString) +{ + APPL_DEBUG("C->java : send message to the java : \"" << dataString << "\""); + if (NULL == g_JavaVM) { + APPL_DEBUG("C->java : JVM not initialised"); + return; + } + JNIEnv *JavaVirtualMachinePointer; + int status = g_JavaVM->GetEnv((void **) &JavaVirtualMachinePointer, JNI_VERSION_1_6); + if (status == JNI_EDETACHED) { + status = g_JavaVM->AttachCurrentThread(&JavaVirtualMachinePointer, NULL); + if (status != JNI_OK) { + APPL_DEBUG("C->java : AttachCurrentThread failed : " << status); + return; + } + jclass javaClassActivity = JavaVirtualMachinePointer->FindClass("com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__" ); + if (javaClassActivity == 0) { + APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__ class"); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + jmethodID javaClassActivityEntryPoint = JavaVirtualMachinePointer->GetStaticMethodID(javaClassActivity, "eventFromCPP", "([Ljava/lang/String;)V" ); + if (javaClassActivityEntryPoint == 0) { + APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__.eventFromCPP" ); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + jclass javaDefaultClassString = JavaVirtualMachinePointer->FindClass("java/lang/String" ); + if (javaDefaultClassString == 0) { + APPL_DEBUG("C->java : Can't find java/lang/String" ); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + // create the string to the java + jstring jstr = JavaVirtualMachinePointer->NewStringUTF(dataString); + if (jstr == 0) { + APPL_DEBUG("C->java : Out of memory" ); + return; + } + // create argument list + jobjectArray args = JavaVirtualMachinePointer->NewObjectArray(1, javaDefaultClassString, jstr); + if (args == 0) { + APPL_DEBUG("C->java : Out of memory" ); + return; + } + //Call java ... + JavaVirtualMachinePointer->CallStaticVoidMethod(javaClassActivity, javaClassActivityEntryPoint, args); + + // manage execption : + if (JavaVirtualMachinePointer->ExceptionOccurred()) { + JavaVirtualMachinePointer->ExceptionDescribe(); + } + } else { + APPL_DEBUG("C->java : do nothing ... "); + } + + +/* + if (NULL == JavaVirtualMachinePointer) { + APPL_DEBUG("C->java : JVM not initialised"); + return; + } + // create the string to the java + jstring jstr = JavaVirtualMachinePointer->NewStringUTF(dataString); + if (jstr == 0) { + APPL_DEBUG("C->java : Out of memory" ); + return; + } + // create argument list + jobjectArray args = JavaVirtualMachinePointer->NewObjectArray(1, javaDefaultClassString, jstr); + if (args == 0) { + APPL_DEBUG("C->java : Out of memory" ); + return; + } + //Call java ... + JavaVirtualMachinePointer->CallStaticVoidMethod(javaClassActivity, javaClassActivityEntryPoint, args); + + // manage execption : + if (JavaVirtualMachinePointer->ExceptionOccurred()) { + JavaVirtualMachinePointer->ExceptionDescribe(); + } +*/ +} + extern "C" { + // JNI OnLoad + JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* jvm, void* reserved) + { + // get the java virtual machine handle ... + g_JavaVM = jvm; + APPL_DEBUG("JNI-> load the jvm ..." ); + return JNI_VERSION_1_6; + } + // JNI OnUnLoad + JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) + { + g_JavaVM = NULL; + APPL_DEBUG("JNI-> Un-load the jvm ..." ); + } + + /* Call to initialize the graphics state */ void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityParamSetArchiveDir( JNIEnv* env, jobject thiz, jint mode, jstring myString) @@ -55,6 +169,49 @@ extern "C" //env->ReleaseStringUTFChars(str,myString,0); } + void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivitySetJavaVortualMachineStart( JNIEnv* env ) + { + APPL_DEBUG("*******************************************"); + APPL_DEBUG("** Set JVM Pointer **"); + APPL_DEBUG("*******************************************"); + JavaVirtualMachinePointer = env; + // get default needed all time elements : + if (NULL != JavaVirtualMachinePointer) { + javaClassActivity = JavaVirtualMachinePointer->FindClass("com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__" ); + if (javaClassActivity == 0) { + APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__ class"); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + javaClassActivityEntryPoint = JavaVirtualMachinePointer->GetStaticMethodID(javaClassActivity, "eventFromCPP", "([Ljava/lang/String;)V" ); + if (javaClassActivityEntryPoint == 0) { + APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__.eventFromCPP" ); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + javaDefaultClassString = JavaVirtualMachinePointer->FindClass("java/lang/String" ); + if (javaDefaultClassString == 0) { + APPL_DEBUG("C->java : Can't find java/lang/String" ); + // remove access on the virtual machine : + JavaVirtualMachinePointer = NULL; + return; + } + } + } + void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivitySetJavaVortualMachineStop( JNIEnv* env ) + { + APPL_DEBUG("*******************************************"); + APPL_DEBUG("** Remove JVM Pointer **"); + APPL_DEBUG("*******************************************"); + JavaVirtualMachinePointer = NULL; + } + void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___TouchEvent( JNIEnv* env ) + { + APPL_DEBUG(" ==> Touch Event"); + } + void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityOnCreate( JNIEnv* env ) { APPL_DEBUG("*******************************************"); @@ -67,6 +224,7 @@ extern "C" APPL_DEBUG("*******************************************"); APPL_DEBUG("** Activity On Start **"); APPL_DEBUG("*******************************************"); + //SendSystemMessage(" testmessages ... "); } void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityOnReStart( JNIEnv* env ) { diff --git a/Sources/libetk/etk/Debug.cpp b/Sources/libetk/etk/Debug.cpp index e360d9f1..00401512 100644 --- a/Sources/libetk/etk/Debug.cpp +++ b/Sources/libetk/etk/Debug.cpp @@ -37,17 +37,16 @@ void TOOLS_DisplayFuncName(int32_t ligne, const char* className, const char* fun if (NULL == className) { if (NULL == libName) { - snprintf(tmpName, FUNCTION_NAME_SIZE, "???????? | (l=%5d) %s ",ligne, funcName); + snprintf(tmpName, FUNCTION_NAME_SIZE-1, "???????? | (l=%5d) %s ",ligne, funcName); } else { - snprintf(tmpName, FUNCTION_NAME_SIZE, "%s | (l=%5d) %s ",libName, ligne, funcName); + snprintf(tmpName, FUNCTION_NAME_SIZE-1, "%s | (l=%5d) %s ",libName, ligne, funcName); } } else { if (NULL == libName) { - snprintf(tmpName, FUNCTION_NAME_SIZE, "???????? | (l=%5d) %s::%s ",ligne, className, funcName); + snprintf(tmpName, FUNCTION_NAME_SIZE-1, "???????? | (l=%5d) %s::%s ",ligne, className, funcName); } else { - snprintf(tmpName, FUNCTION_NAME_SIZE, "%s | (l=%5d) %s::%s ", libName, ligne, className, funcName); + snprintf(tmpName, FUNCTION_NAME_SIZE-1, "%s | (l=%5d) %s::%s ", libName, ligne, className, funcName); } - } tmpName[FUNCTION_NAME_SIZE-4] = ' '; tmpName[FUNCTION_NAME_SIZE-3] = '|'; diff --git a/Sources/libetk/etk/Stream.h b/Sources/libetk/etk/Stream.h index 52dd3232..cef7553c 100644 --- a/Sources/libetk/etk/Stream.h +++ b/Sources/libetk/etk/Stream.h @@ -34,9 +34,9 @@ #if defined(__PLATFORM__Android) # include -# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "====> EWOL", __VA_ARGS__)) -# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "====> EWOL", __VA_ARGS__)) -# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "====> EWOL", __VA_ARGS__)) +# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "EWOL", __VA_ARGS__)) +# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "EWOL", __VA_ARGS__)) +# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "EWOL", __VA_ARGS__)) #endif #define MAX_LOG_SIZE (16000) diff --git a/Sources/libewol/ewol/base/gui.cpp b/Sources/libewol/ewol/base/gui.cpp index 07925290..0b42d5d7 100644 --- a/Sources/libewol/ewol/base/gui.cpp +++ b/Sources/libewol/ewol/base/gui.cpp @@ -133,7 +133,7 @@ void guiAbstraction::SendKeyboardEventMove(bool isDown, ewol::eventKbMoveType_te } - +/* void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode) { if (NULL != gui_uniqueWindows) { @@ -148,7 +148,7 @@ void guiAbstraction::KeyboardHide(void) } ForceRedrawAll(); } - +*/ diff --git a/Sources/libewol/ewol/base/guiAndroid.cpp b/Sources/libewol/ewol/base/guiAndroid.cpp index 830c5051..a5741a28 100644 --- a/Sources/libewol/ewol/base/guiAndroid.cpp +++ b/Sources/libewol/ewol/base/guiAndroid.cpp @@ -247,7 +247,20 @@ void guiAbstraction::Stop(void) { // TODo : send a message to the android system to stop ... } +// java system to send message : +void SendSystemMessage(const char * dataString); +void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode) +{ + // send a message at the java : + SendSystemMessage("Keyboard_Show"); +} + +void guiAbstraction::KeyboardHide(void) +{ + // send a message at the java : + SendSystemMessage("Keyboard_Hide"); +} void guiAbstraction::ChangeSize(int32_t w, int32_t h) { diff --git a/Sources/libewol/ewol/base/guiX11.cpp b/Sources/libewol/ewol/base/guiX11.cpp index d4965bcb..373b0e21 100644 --- a/Sources/libewol/ewol/base/guiX11.cpp +++ b/Sources/libewol/ewol/base/guiX11.cpp @@ -1006,6 +1006,16 @@ void guiAbstraction::Stop(void) } +void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode) +{ + // nothing to do : No keyboard on computer ... +} + +void guiAbstraction::KeyboardHide(void) +{ + // nothing to do : No keyboard on computer ... +} + void guiAbstraction::ChangeSize(int32_t w, int32_t h) diff --git a/Sources/libewol/ewol/widget/Menu.cpp b/Sources/libewol/ewol/widget/Menu.cpp index 05d23708..07488468 100644 --- a/Sources/libewol/ewol/widget/Menu.cpp +++ b/Sources/libewol/ewol/widget/Menu.cpp @@ -87,7 +87,8 @@ const char * const ewol::Menu::GetObjectType(void) void ewol::Menu::SubWidgetRemoveAll(void) { - EWOL_ERROR("Not availlable"); + Clear(); + ewol::SizerHori::SubWidgetRemoveAll(); } void ewol::Menu::SubWidgetAdd(ewol::Widget* newWidget) @@ -253,3 +254,25 @@ void ewol::Menu::OnReceiveMessage(ewol::EObject * CallerObject, const char * eve } } } + +/** + * @brief Inform object that an other object is removed ... + * @param[in] removeObject Pointer on the EObject remeved ==> the user must remove all reference on this EObject + * @note : Sub classes must call this class + * @return --- + */ +void ewol::Menu::OnObjectRemove(ewol::EObject * removeObject) +{ + ewol::SizerHori::OnObjectRemove(removeObject); + if (m_widgetContextMenu == removeObject) { + m_widgetContextMenu = NULL; + } + for(int32_t jjj=0; jjjm_widgetPointer == removeObject) { + m_listElement[jjj]->m_widgetPointer = NULL; + } + } + } +} + diff --git a/Sources/libewol/ewol/widget/Menu.h b/Sources/libewol/ewol/widget/Menu.h index 35441712..41b99786 100644 --- a/Sources/libewol/ewol/widget/Menu.h +++ b/Sources/libewol/ewol/widget/Menu.h @@ -88,6 +88,13 @@ namespace ewol { * @return --- */ virtual void OnReceiveMessage(ewol::EObject * CallerObject, const char * eventId, etk::UString data); + /** + * @brief Inform object that an other object is removed ... + * @param[in] removeObject Pointer on the EObject remeved ==> the user must remove all reference on this EObject + * @note : Sub classes must call this class + * @return --- + */ + virtual void OnObjectRemove(ewol::EObject * removeObject); }; extern const char * const TYPE_EOBJECT_WIDGET_MENU;