diff --git a/agdk/common/base_game_framework/include/game_controller_manager.h b/agdk/common/base_game_framework/include/game_controller_manager.h index 2bb98211..82654d39 100644 --- a/agdk/common/base_game_framework/include/game_controller_manager.h +++ b/agdk/common/base_game_framework/include/game_controller_manager.h @@ -73,7 +73,7 @@ class GameControllerManager { * (i.e. handset, not controller) motion data * @return true if the device can report motion data, false if not */ - bool GetIntegratedMotionDataSupport() { return integrated_motion_data_; } + bool GetIntegratedMotionDataSupport() const { return integrated_motion_data_; } /** * @brief Retrieves the specified `GameController` object @@ -155,6 +155,9 @@ class GameControllerManager { void *callback_user_data_ = nullptr; void *integrated_user_data_ = nullptr; void *motion_user_data_ = nullptr; + // Integrated (handset) sensors available to Paddleboat. Sensor reporting is only + // enabled while an integrated motion data callback is registered. + uint32_t integrated_sensor_flags_ = 0; bool initialized_ = false; bool integrated_motion_data_ = false; diff --git a/agdk/common/base_game_framework/src/android/game_controller_manager_android.cpp b/agdk/common/base_game_framework/src/android/game_controller_manager_android.cpp index 95cd67be..1e11dbd6 100644 --- a/agdk/common/base_game_framework/src/android/game_controller_manager_android.cpp +++ b/agdk/common/base_game_framework/src/android/game_controller_manager_android.cpp @@ -20,15 +20,26 @@ namespace base_game_framework { +static_assert(GameControllerManager::kMaxControllers <= PADDLEBOAT_MAX_CONTROLLERS, + "GameControllerManager slots must map to valid Paddleboat controller indices"); + static void controller_status_callback_(const int32_t controller_index, const Paddleboat_ControllerStatus controller_status, void *user_data) { GameControllerManager *manager = reinterpret_cast(user_data); if (manager != nullptr) { - GameController::GameControllerStatus status = - controller_status == PADDLEBOAT_CONTROLLER_ACTIVE ? - GameController::kActive : GameController::kInactive; - manager->SetControllerStatus(static_cast(controller_index), status); + // Status callbacks report JUST_CONNECTED / JUST_DISCONNECTED transitions, not just + // ACTIVE / INACTIVE. + switch (controller_status) { + case PADDLEBOAT_CONTROLLER_ACTIVE: + case PADDLEBOAT_CONTROLLER_JUST_CONNECTED: + manager->SetControllerStatus(controller_index, GameController::kActive); + break; + case PADDLEBOAT_CONTROLLER_INACTIVE: + case PADDLEBOAT_CONTROLLER_JUST_DISCONNECTED: + manager->SetControllerStatus(controller_index, GameController::kInactive); + break; + } } } @@ -57,8 +68,11 @@ GameControllerManager::GameControllerManager() { if (error_code == PADDLEBOAT_NO_ERROR) { initialized_ = true; Paddleboat_setControllerStatusCallback(controller_status_callback_, this); - Paddleboat_setMotionDataCallbackWithIntegratedFlags(motion_data_callback_, - Paddleboat_getIntegratedMotionSensorFlags(), this); + // Only controller motion is reported until an integrated motion callback is set, so the + // handset's accelerometer/gyroscope aren't sampled for apps that never use them. + Paddleboat_setMotionDataCallback(motion_data_callback_, this); + integrated_sensor_flags_ = Paddleboat_getIntegratedMotionSensorFlags(); + integrated_motion_data_ = (integrated_sensor_flags_ != PADDLEBOAT_INTEGRATED_SENSOR_NONE); } else { DebugManager::Log(DebugManager::kLog_Channel_Default, DebugManager::kLog_Level_Error, BGM_CLASS_TAG, "Paddleboat_init failed with error code %d", error_code); @@ -103,14 +117,13 @@ void GameControllerManager::Update() { for (int32_t i = 0; i < kMaxControllers; ++i) { const Paddleboat_ControllerStatus status = Paddleboat_getControllerStatus(i); - // Check for status mismatch if we didn't get the status callback yet - if (status == PADDLEBOAT_CONTROLLER_ACTIVE - && game_controllers_[i].controller_status_ == GameController::kInactive) { - game_controllers_[i].controller_status_ = GameController::kActive; - UpdateControllerInfo(i); - } else if (status == PADDLEBOAT_CONTROLLER_INACTIVE - && game_controllers_[i].controller_status_ == GameController::kActive) { - game_controllers_[i].controller_status_ = GameController::kInactive; + // Check for status mismatch if we didn't get the status callback yet, e.g. a + // controller already connected before the callback was registered. + const GameController::GameControllerStatus current = game_controllers_[i].controller_status_; + if (status == PADDLEBOAT_CONTROLLER_ACTIVE && current == GameController::kInactive) { + SetControllerStatus(i, GameController::kActive); + } else if (status == PADDLEBOAT_CONTROLLER_INACTIVE && current == GameController::kActive) { + SetControllerStatus(i, GameController::kInactive); } } } @@ -182,22 +195,63 @@ GameController &GameControllerManager::GetGameController(const int32_t controlle void GameControllerManager::SetControllerStatus(const int32_t controller_index, const GameController::GameControllerStatus status) { - if (controller_index >= 0 && controller_index < GameControllerManager::kMaxControllers) { - game_controllers_[controller_index].controller_status_ = status; - if (status == GameController::kActive) { - UpdateControllerInfo(controller_index); + if (controller_index < 0 || controller_index >= GameControllerManager::kMaxControllers) { + return; + } + GameController &controller = game_controllers_[controller_index]; + if (status == GameController::kActive) { + const bool was_active = (controller.controller_status_ == GameController::kActive); + const int32_t previous_device_id = controller.controller_info_.device_id; + UpdateControllerInfo(controller_index); + if (was_active) { + if (controller.controller_info_.device_id == previous_device_id) { + return; // Same controller, no change to report. + } + // A different controller took this slot without a disconnect being reported + // (e.g. swapped while the app was stopped): report the old one as disconnected. + if (status_callback_ != nullptr) { + status_callback_(controller_index, GameController::kInactive, callback_user_data_); + } } - if (status_callback_ != nullptr) { - status_callback_(controller_index, status, callback_user_data_); + controller.controller_status_ = GameController::kActive; + controller.controller_data_ = GameController::GameControllerData(); + } else { + if (controller.controller_status_ == GameController::kInactive) { + return; } + controller.controller_status_ = GameController::kInactive; + // Don't leave buttons/sticks held at disconnect time stuck in the slot's data. + controller.controller_data_ = GameController::GameControllerData(); + controller.controller_info_ = GameController::GameControllerInfo(); + } + if (status_callback_ != nullptr) { + status_callback_(controller_index, status, callback_user_data_); } } void GameControllerManager::SetIntegratedMotionDataCallback( IntegratedMotionDataCallback callback, void *user_data) { - if (integrated_motion_data_) { + if (!integrated_motion_data_) { + return; + } + if (callback != nullptr) { + // Store the callback before Paddleboat starts reporting integrated sensor events. integrated_motion_data_callback_ = callback; integrated_user_data_ = user_data; + const Paddleboat_ErrorCode error = Paddleboat_setMotionDataCallbackWithIntegratedFlags( + motion_data_callback_, + static_cast(integrated_sensor_flags_), this); + if (error != PADDLEBOAT_NO_ERROR) { + DebugManager::Log(DebugManager::kLog_Channel_Default, DebugManager::kLog_Level_Error, + BGM_CLASS_TAG, "Enabling integrated motion data failed: %d", error); + integrated_motion_data_callback_ = nullptr; + integrated_user_data_ = nullptr; + } + } else { + // Stop sampling the handset sensors before clearing the callback. + Paddleboat_setMotionDataCallback(motion_data_callback_, this); + integrated_motion_data_callback_ = nullptr; + integrated_user_data_ = nullptr; } } @@ -207,6 +261,12 @@ void GameControllerManager::SetGameControllerMotionDataCallback( motion_user_data_ = user_data; } +void GameControllerManager::SetConsumeBackButton(bool consume_back_button) { + if (initialized_) { + Paddleboat_setBackButtonConsumed(consume_back_button); + } +} + void GameControllerManager::SetGameControllerStatusCallback(GameControllerStatusCallback callback, void *user_data) { status_callback_ = callback; diff --git a/agdk/common/base_game_framework/src/android/platform_events_android.cpp b/agdk/common/base_game_framework/src/android/platform_events_android.cpp index 8ac38171..38aba048 100644 --- a/agdk/common/base_game_framework/src/android/platform_events_android.cpp +++ b/agdk/common/base_game_framework/src/android/platform_events_android.cpp @@ -26,32 +26,41 @@ namespace base_game_framework { -// enums from NativeAppGlueAppCmd in android_native_app_glue.h -constexpr const char *kGlueCommandStrings[] = { - "UNUSED_APP_CMD_INPUT_CHANGED", - "APP_CMD_INIT_WINDOW", - "APP_CMD_TERM_WINDOW", - "APP_CMD_WINDOW_RESIZED", - "APP_CMD_WINDOW_REDRAW_NEEDED", - "APP_CMD_CONTENT_RECT_CHANGED", - "APP_CMD_GAINED_FOCUS", - "APP_CMD_LOST_FOCUS", - "APP_CMD_CONFIG_CHANGED", - "APP_CMD_LOW_MEMORY", - "APP_CMD_START", - "APP_CMD_RESUME", - "APP_CMD_SAVE_STATE", - "APP_CMD_PAUSE", - "APP_CMD_STOP", - "APP_CMD_DESTROY", - "APP_CMD_WINDOW_INSETS_CHANGED" -}; +// Debug names for NativeAppGlueAppCmd values from android_native_app_glue.h. +// A switch on the enum constants (rather than an array indexed by value) can't read out +// of bounds or drift out of order. It deliberately has no default case, so -Wswitch +// flags any command added by a future GameActivity release. +static const char *GetGlueCommandName(const int32_t cmd) { +#define BGF_GLUE_COMMAND_CASE(command) case command: return #command + switch (static_cast(cmd)) { + BGF_GLUE_COMMAND_CASE(UNUSED_APP_CMD_INPUT_CHANGED); + BGF_GLUE_COMMAND_CASE(APP_CMD_INIT_WINDOW); + BGF_GLUE_COMMAND_CASE(APP_CMD_TERM_WINDOW); + BGF_GLUE_COMMAND_CASE(APP_CMD_WINDOW_RESIZED); + BGF_GLUE_COMMAND_CASE(APP_CMD_WINDOW_REDRAW_NEEDED); + BGF_GLUE_COMMAND_CASE(APP_CMD_CONTENT_RECT_CHANGED); + BGF_GLUE_COMMAND_CASE(APP_CMD_SOFTWARE_KB_VIS_CHANGED); + BGF_GLUE_COMMAND_CASE(APP_CMD_GAINED_FOCUS); + BGF_GLUE_COMMAND_CASE(APP_CMD_LOST_FOCUS); + BGF_GLUE_COMMAND_CASE(APP_CMD_CONFIG_CHANGED); + BGF_GLUE_COMMAND_CASE(APP_CMD_LOW_MEMORY); + BGF_GLUE_COMMAND_CASE(APP_CMD_START); + BGF_GLUE_COMMAND_CASE(APP_CMD_RESUME); + BGF_GLUE_COMMAND_CASE(APP_CMD_SAVE_STATE); + BGF_GLUE_COMMAND_CASE(APP_CMD_PAUSE); + BGF_GLUE_COMMAND_CASE(APP_CMD_STOP); + BGF_GLUE_COMMAND_CASE(APP_CMD_DESTROY); + BGF_GLUE_COMMAND_CASE(APP_CMD_WINDOW_INSETS_CHANGED); + BGF_GLUE_COMMAND_CASE(APP_CMD_EDITOR_ACTION); + BGF_GLUE_COMMAND_CASE(APP_CMD_KEY_EVENT); + BGF_GLUE_COMMAND_CASE(APP_CMD_TOUCH_EVENT); + } +#undef BGF_GLUE_COMMAND_CASE + return "UNKNOWN_APP_CMD"; +} void PlatformEventAndroid::ProcessApplicationEvent(struct android_app *app, int32_t cmd) { - const char *cmdString = "INVALID COMMAND"; - if (cmd >= UNUSED_APP_CMD_INPUT_CHANGED && cmd <= APP_CMD_WINDOW_INSETS_CHANGED) { - cmdString = kGlueCommandStrings[cmd]; - } + const char *cmdString = GetGlueCommandName(cmd); DebugManager::Log(DebugManager::kLog_Channel_Default, DebugManager::kLog_Level_Debug, diff --git a/agdk/common/base_game_framework/src/system_event_manager.cpp b/agdk/common/base_game_framework/src/system_event_manager.cpp index 99597ab7..42be0bf2 100644 --- a/agdk/common/base_game_framework/src/system_event_manager.cpp +++ b/agdk/common/base_game_framework/src/system_event_manager.cpp @@ -72,7 +72,7 @@ void SystemEventManager::SetLifecycleEventCallback(LifecycleEventCallback callba void SystemEventManager::SetMemoryWarningEventCallback(MemoryWarningEventCallback callback, void *user_data) { memory_warning_event_callback_ = callback; - lifecycle_event_user_data_ = user_data; + memory_warning_event_user_data_ = user_data; } void SystemEventManager::SetReadSaveStateCallback(ReadSaveStateCallback callback, void *user_data) {