Skip to content

Commit

Permalink
Add manifest entries for PICO
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jun 21, 2024
1 parent 94c2d3e commit affeafe
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Add OpenXRMetaPassthroughColorLut
- Add feature flags to Khronos loader for HTC
- Add XR_HTC_passthrough extension wrapper
- Add manifest entries to Pico loader

## 2.0.3
- Migrate the export scripts from gdscript to C++ via gdextension
Expand Down
137 changes: 136 additions & 1 deletion common/src/main/cpp/export/pico_export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@

#include "export/pico_export_plugin.h"

#include <godot_cpp/classes/project_settings.hpp>

using namespace godot;

void PicoEditorPlugin::_bind_methods() {}

void PicoEditorPlugin::_enter_tree() {
// Initialize the editor export plugin
pico_export_plugin.instantiate();
pico_export_plugin->set_vendor_name(PICO_VENDOR_NAME);
add_export_plugin(pico_export_plugin);
}

Expand All @@ -46,3 +47,137 @@ void PicoEditorPlugin::_exit_tree() {

pico_export_plugin.unref();
}

PicoEditorExportPlugin::PicoEditorExportPlugin() {
set_vendor_name(PICO_VENDOR_NAME);

_eye_tracking_option = _generate_export_option(
"pico_xr_features/eye_tracking",
"",
Variant::Type::INT,
PROPERTY_HINT_ENUM,
"No,Yes",
PROPERTY_USAGE_DEFAULT,
pico::MANIFEST_FALSE_VALUE,
false);

_face_tracking_option = _generate_export_option(
"pico_xr_features/face_tracking",
"",
Variant::Type::INT,
PROPERTY_HINT_ENUM,
"No,Face only,Lipsync only,Hybrid",
PROPERTY_USAGE_DEFAULT,
pico::FACE_TRACKING_NONE_VALUE,
false);
}

void PicoEditorExportPlugin::_bind_methods() {}

TypedArray<Dictionary> PicoEditorExportPlugin::_get_export_options(const Ref<EditorExportPlatform> &platform) const {
TypedArray<Dictionary> export_options;
if (!_supports_platform(platform)) {
return export_options;
}

export_options.append(_get_vendor_toggle_option());
export_options.append(_eye_tracking_option);
export_options.append(_face_tracking_option);

return export_options;
}

bool PicoEditorExportPlugin::_is_eye_tracking_enabled() const {
bool eye_tracking_project_setting_enabled = ProjectSettings::get_singleton()->get_setting_with_override("xr/openxr/extensions/eye_gaze_interaction");
if (!eye_tracking_project_setting_enabled) {
return false;
}

int eye_tracking_option_value = _get_int_option("pico_xr_features/eye_tracking", pico::MANIFEST_FALSE_VALUE);
return eye_tracking_option_value == pico::MANIFEST_TRUE_VALUE;
}

PackedStringArray PicoEditorExportPlugin::_get_export_features(const Ref<EditorExportPlatform> &platform, bool debug) const {
PackedStringArray features;
if (!_supports_platform(platform)) {
return features;
}

// Add the eye tracking feature if necessary
if (_is_eye_tracking_enabled()) {
features.append(EYE_GAZE_INTERACTION_FEATURE);
}

return features;
}

String PicoEditorExportPlugin::_get_export_option_warning(const Ref<EditorExportPlatform> &platform, const String &option) const {
if (!_supports_platform(platform)) {
return "";
}

bool openxr_enabled = _is_openxr_enabled();
if (option == "pico_xr_features/eye_tracking") {
if (!openxr_enabled && _get_int_option(option, pico::MANIFEST_FALSE_VALUE) == pico::MANIFEST_TRUE_VALUE) {
return "\"Eye tracking\" requires \"XR Mode\" to be \"OpenXR\".\n";
}
} else if (option == "pico_xr_features/face_tracking") {
int face_tracking = _get_int_option(option, pico::FACE_TRACKING_NONE_VALUE);
if (!openxr_enabled && face_tracking > pico::FACE_TRACKING_NONE_VALUE) {
return "\"Face tracking\" requires \"XR Mode\" to be \"OpenXR\".\n";
}
bool record_audio = _get_bool_option("permissions/record_audio");
if (!record_audio && face_tracking == pico::FACE_TRACKING_LIPSYNCONLY_VALUE) {
return "\"Lipsync face tracking\" requires \"Record Audio\" to be checked.\n";
}
if (!record_audio && face_tracking == pico::FACE_TRACKING_HYBRID_VALUE) {
return "\"Hybrid face tracking\" requires \"Record Audio\" to be checked.\n";
}
}

return OpenXREditorExportPlugin::_get_export_option_warning(platform, option);
}

String PicoEditorExportPlugin::_get_android_manifest_element_contents(const Ref<EditorExportPlatform> &platform, bool debug) const {
String contents;
if (!_supports_platform(platform) || !_is_vendor_plugin_enabled()) {
return contents;
}

// Check for eye tracking
if (_is_eye_tracking_enabled()) {
contents += " <uses-permission android:name=\"com.picovr.permission.EYE_TRACKING\" />\n";
}

// Face tracking
int face_tracking = _get_int_option("pico_xr_features/face_tracking", pico::FACE_TRACKING_NONE_VALUE);
if (face_tracking == pico::FACE_TRACKING_FACEONLY_VALUE || face_tracking == pico::FACE_TRACKING_HYBRID_VALUE) {
contents += " <uses-permission android:name=\"com.picovr.permission.FACE_TRACKING\" />\n";
}

return contents;
}

String PicoEditorExportPlugin::_get_android_manifest_application_element_contents(const Ref<EditorExportPlatform> &platform, bool debug) const {
String contents;
if (!_supports_platform(platform) || !_is_vendor_plugin_enabled()) {
return contents;
}

// Standard entries
contents += " <meta-data tools:node=\"replace\" android:name=\"pvr.app.type\" android:value=\"vr\" />\n";
contents += " <meta-data tools:node=\"replace\" android:name=\"pvr.display.orientation\" android:value=\"180\" />\n";

// Check for eye tracking
if (_is_eye_tracking_enabled()) {
contents += " <meta-data tools:node=\"replace\" android:name=\"picovr.software.eye_tracking\" android:value=\"1\" />\n";
}

// Face tracking
int face_tracking = _get_int_option("pico_xr_features/face_tracking", pico::FACE_TRACKING_NONE_VALUE);
if (face_tracking > pico::FACE_TRACKING_NONE_VALUE) {
contents += " <meta-data tools:node=\"replace\" android:name=\"picovr.software.face_tracking\" android:value=\"1\" />\n";
}

return contents;
}
39 changes: 38 additions & 1 deletion common/src/main/cpp/include/export/pico_export_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@

using namespace godot;

namespace pico {
static const int MANIFEST_FALSE_VALUE = 0;
static const int MANIFEST_TRUE_VALUE = 1;

static const int FACE_TRACKING_NONE_VALUE = 0;
static const int FACE_TRACKING_FACEONLY_VALUE = 1;
static const int FACE_TRACKING_LIPSYNCONLY_VALUE = 2;
static const int FACE_TRACKING_HYBRID_VALUE = 3;

} // namespace

class PicoEditorExportPlugin : public OpenXREditorExportPlugin {
GDCLASS(PicoEditorExportPlugin, OpenXREditorExportPlugin)

public:
PicoEditorExportPlugin();

TypedArray<Dictionary> _get_export_options(const Ref<EditorExportPlatform> &platform) const override;

PackedStringArray _get_export_features(const Ref<EditorExportPlatform> &platform, bool debug) const override;

String _get_export_option_warning(const Ref<EditorExportPlatform> &platform, const String &option) const override;

String _get_android_manifest_element_contents(const Ref<EditorExportPlatform> &platform, bool debug) const override;
String _get_android_manifest_application_element_contents(const Ref<EditorExportPlatform> &platform, bool debug) const override;

protected:
static void _bind_methods();

Dictionary _eye_tracking_option;
Dictionary _face_tracking_option;

private:
bool _is_eye_tracking_enabled() const;

};

class PicoEditorPlugin : public EditorPlugin {
GDCLASS(PicoEditorPlugin, EditorPlugin)

Expand All @@ -46,5 +83,5 @@ class PicoEditorPlugin : public EditorPlugin {
static void _bind_methods();

private:
Ref<OpenXREditorExportPlugin> pico_export_plugin;
Ref<PicoEditorExportPlugin> pico_export_plugin;
};
1 change: 1 addition & 0 deletions common/src/main/cpp/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<MetaEditorPlugin>();
EditorPlugins::add_by_type<MetaEditorPlugin>();

ClassDB::register_class<PicoEditorExportPlugin>();
ClassDB::register_class<PicoEditorPlugin>();
EditorPlugins::add_by_type<PicoEditorPlugin>();
} break;
Expand Down
1 change: 1 addition & 0 deletions docs/make_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"MetaEditorExportPlugin",
"MetaEditorPlugin",
"OpenXREditorExportPlugin",
"PicoEditorExportPlugin",
"PicoEditorPlugin",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@

package org.godotengine.openxr.vendors.pico

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.PermissionInfo
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.view.View
import org.godotengine.godot.Godot
import org.godotengine.godot.plugin.GodotPlugin
import org.godotengine.godot.utils.PermissionsUtil

/**
* Godot plugin for the Pico OpenXR loader.
Expand All @@ -40,6 +52,9 @@ class GodotOpenXRPico(godot: Godot?) : GodotPlugin(godot) {
companion object {
private val TAG = GodotOpenXRPico::class.java.simpleName

const val EYE_TRACKING_PERMISSION = "com.picovr.permission.EYE_TRACKING"
const val FACE_TRACKING_PERMISSION = "com.picovr.permission.FACE_TRACKING"

init {
try {
Log.v(TAG, "Loading godotopenxrvendors library")
Expand All @@ -48,11 +63,116 @@ class GodotOpenXRPico(godot: Godot?) : GodotPlugin(godot) {
Log.e(TAG, "Unable to load godotopenxrvendors shared library")
}
}

/**
* Returns the information of the desired permission.
* @param context the caller context for this method.
* @param permission the name of the permission.
* @return permission info object
* @throws PackageManager.NameNotFoundException the exception is thrown when a given package, application, or component name cannot be found.
*
* TODO: remove this when Godot 4.3 is out with the new `PermissionsUtil.requestPermissions(...)` api
*/
@Throws(PackageManager.NameNotFoundException::class)
private fun getPermissionInfo(context: Context, permission: String): PermissionInfo {
val packageManager = context.packageManager
return packageManager.getPermissionInfo(permission, 0)
}

/**
* Request a list of dangerous permissions. The requested permissions must be included in the app's AndroidManifest
* @param permissions list of the permissions to request.
* @param activity the caller activity for this method.
* @return true/false. "true" if permissions are already granted, "false" if a permissions request was dispatched.
*
* TODO: remove this when Godot 4.3 is out with the new `PermissionsUtil.requestPermissions(...)` api
*/
private fun requestPermissions(activity: Activity?, permissions: List<String>): Boolean {
if (activity == null) {
return false
}
if (permissions.isEmpty()) {
return true
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Not necessary, asked on install already
return true
}

val requestedPermissions: MutableSet<String> = HashSet()
for (permission in permissions) {
try {
if (permission == Manifest.permission.MANAGE_EXTERNAL_STORAGE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
Log.d(TAG, "Requesting permission $permission")
try {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.setData(Uri.parse(String.format("package:%s", activity.packageName)))
activity.startActivityForResult(intent, PermissionsUtil.REQUEST_MANAGE_EXTERNAL_STORAGE_REQ_CODE)
} catch (ignored: Exception) {
val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
activity.startActivityForResult(intent, PermissionsUtil.REQUEST_MANAGE_EXTERNAL_STORAGE_REQ_CODE)
}
}
} else {
val permissionInfo = getPermissionInfo(activity, permission)
val protectionLevel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) permissionInfo.protection else permissionInfo.protectionLevel
if (protectionLevel == PermissionInfo.PROTECTION_DANGEROUS && activity.checkSelfPermission(permission) !== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Requesting permission $permission")
requestedPermissions.add(permission)
}
}
} catch (e: PackageManager.NameNotFoundException) {
// Skip this permission and continue.
Log.w(TAG, "Unable to identify permission $permission", e)
}
}
if (requestedPermissions.isEmpty()) {
// If list is empty, all of dangerous permissions were granted.
return true
}
activity.requestPermissions(requestedPermissions.toTypedArray<String>(), PermissionsUtil.REQUEST_ALL_PERMISSION_REQ_CODE)
return true
}

/**
* Dispatch the necessary requests for all plugin's permissions in the app's manifest.
*/
@JvmStatic
fun requestAllPluginPermissions(activity: Activity) {
val permissionsToRequest = ArrayList<String>()
// Request the eye tracking permission if it's included in the manifest
if (PermissionsUtil.hasManifestPermission(activity, EYE_TRACKING_PERMISSION)) {
permissionsToRequest.add(EYE_TRACKING_PERMISSION)
}
// Request the face tracking permission if it's included in the manifest
if (PermissionsUtil.hasManifestPermission(activity, FACE_TRACKING_PERMISSION)) {
permissionsToRequest.add(FACE_TRACKING_PERMISSION)
}

if (permissionsToRequest.isNotEmpty()) {
requestPermissions(activity, permissionsToRequest)
}
}
}

override fun getPluginGDExtensionLibrariesPaths() = setOf("res://addons/godotopenxrvendors/plugin.gdextension")

override fun getPluginName(): String {
return "GodotOpenXRPico"
}

override fun supportsFeature(featureTag: String): Boolean {
if ("PERMISSION_XR_EXT_eye_gaze_interaction" == featureTag) {
val grantedPermissions = godot?.getGrantedPermissions()
if (grantedPermissions != null) {
for (permission in grantedPermissions) {
if (EYE_TRACKING_PERMISSION == permission) {
return true
}
}
}
}
return false
}
}

0 comments on commit affeafe

Please sign in to comment.