Skip to content

Commit

Permalink
Big cleanup of Android-specific code:
Browse files Browse the repository at this point in the history
- moved all logging and debugging functions to android_utils
- splitted a large function in user_callback_android.cpp into three
  functions (that handle fingers, stylus and mouse respectively)
  • Loading branch information
BrunoLevy committed Jul 26, 2023
1 parent 645fd56 commit 3cc52d6
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 233 deletions.
76 changes: 75 additions & 1 deletion src/lib/geogram/basic/android_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <geogram/basic/android_utils.h>
#include <geogram/basic/argused.h>
#include <geogram/basic/string.h>
#include <android_native_app_glue.h>

namespace {
Expand Down Expand Up @@ -411,7 +412,80 @@ namespace GEO {
leave_JNI_function(app, env, thread_attached);
return result;
}

}
}

/*******************************************************************/

namespace {
using namespace GEO;

static const char* event_type_to_str(int32_t event_type) {
switch(event_type) {
case AINPUT_EVENT_TYPE_KEY:
return "key";
case AINPUT_EVENT_TYPE_MOTION:
return "motion";
default:
return "unknown";
}
}

static const char* event_action_to_str(int32_t event_action) {
switch(event_action) {
case AKEY_EVENT_ACTION_DOWN:
return "key/motion_down";
case AKEY_EVENT_ACTION_UP:
return "key/motion_up";
case AMOTION_EVENT_ACTION_BUTTON_PRESS:
return "motion_button_press";
case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
return "motion_button_release";
case AMOTION_EVENT_ACTION_HOVER_MOVE:
return "motion_hover_move";
case AMOTION_EVENT_ACTION_MOVE:
return "motion_move";
case AMOTION_EVENT_ACTION_SCROLL:
return "motion_scroll";
default:
return "unknown";
}
}

static const char* event_tool_type_to_str(int32_t event_tool_type) {
switch(event_tool_type) {
case AMOTION_EVENT_TOOL_TYPE_MOUSE:
return "mouse";
case AMOTION_EVENT_TOOL_TYPE_STYLUS:
return "stylus";
case AMOTION_EVENT_TOOL_TYPE_ERASER:
return "eraser";
case AMOTION_EVENT_TOOL_TYPE_FINGER:
return "finger";
default:
return "unknown";
}
}
}

namespace GEO {
namespace AndroidUtils {

void debug_show_event(AInputEvent* event) {
std::string msg = std::string("Event=") + " type:" +
std::string(event_type_to_str(AInputEvent_getType(event)));

if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
msg += " action:" + std::string(
event_action_to_str(AMotionEvent_getAction(event))
) + " tool:" + std::string(
event_tool_type_to_str(AMotionEvent_getToolType(event,0))
) + " nb_pointers:" + ::GEO::String::to_string(
int(AMotionEvent_getPointerCount(event))
) ;
}
::GEO::AndroidUtils::debug_log(msg);
}
}
}

Expand Down
41 changes: 39 additions & 2 deletions src/lib/geogram/basic/android_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include <geogram/basic/common.h>
#include <geogram/basic/numeric.h>
#include <geogram/basic/argused.h>
#include <android/log.h>
#include <string>

/**
Expand All @@ -50,6 +52,7 @@
*/

struct android_app;
struct AInputEvent;

namespace GEO {
namespace AndroidUtils {
Expand Down Expand Up @@ -111,9 +114,43 @@ namespace GEO {
* temporary files.
*/
std::string GEOGRAM_API temp_folder(android_app* app);
}
}

/**
* \brief Displays a message in the android log in
* Debug mode, ignored in Release mode.
* \details The message can be displayed using
* 'adb logcat | grep GEOGRAM'
* \param[in] str the message to be displayed
*/
inline void debug_log(const char* str) {
geo_argused(str);
#ifdef GEO_DEBUG
__android_log_print(
ANDROID_LOG_VERBOSE, "GEOGRAM", "DBG: %s", str
);
#endif
}

/**
* \brief Displays a message in the android log in
* Debug mode, ignored in Release mode.
* \details The message can be displayed using
* 'adb logcat | grep GEOGRAM'
* \param[in] str the message to be displayed
*/
inline void debug_log(const std::string& str) {
debug_log(str.c_str());
}

/**
* \brief Displays an android event in the android log in
* Debug mode, ignored in release mode.
* \details The message can be displayed using
* 'adb logcat | grep GEOGRAM'
* \param[in] event the event to be displayed
*/
void GEOGRAM_API debug_show_event(AInputEvent* event);

}
}
#endif
9 changes: 5 additions & 4 deletions src/lib/geogram_gfx/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
# include <EGL/eglext.h>
# include <GLES/gl.h>
# include <android_native_app_glue.h>
# include <android/log.h>

#endif

Expand Down Expand Up @@ -1453,7 +1452,9 @@ namespace GEO {

// Check if we are exiting.
if (data_->app->destroyRequested != 0) {
android_debug_log("Destroy requested, freeing resources");
::GEO::AndroidUtils::debug_log(
"Destroy requested, freeing resources"
);
ImGui_terminate();
GL_terminate();
return;
Expand All @@ -1464,7 +1465,7 @@ namespace GEO {
one_frame();
}
}
android_debug_log("End of main loop");
::GEO::AndroidUtils::debug_log("End of main loop");
}

namespace {
Expand Down Expand Up @@ -1576,7 +1577,7 @@ namespace GEO {
"keyboard", "mouse", "finger", "stylus", "unknown"
};

android_debug_log(
::GEO::AndroidUtils::debug_log(
std::string("mouse CB ") +
" (" + String::to_string(x) + "," + String::to_string(y) + ")" +
" btn=" + String::to_string(button) +
Expand Down
Loading

0 comments on commit 3cc52d6

Please sign in to comment.