Add support for wivrn:// intent
Launching with wivrn://address will directly connect to given address, bypassing mDNS service discovery
This commit is contained in:
@@ -81,6 +81,12 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
<category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<data android:scheme="wivrn" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
+22
-2
@@ -521,10 +521,10 @@ application::application(application_info info) :
|
||||
jclass jNativeClass = Env->GetObjectClass(activity->clazz);
|
||||
jtmID = Env->GetMethodID(jNativeClass, "getApplication", "()Landroid/app/Application;");
|
||||
|
||||
jobject jNativeApplication = (jobject)Env->CallObjectMethod(activity->clazz, jtmID);
|
||||
jobject jNativeApplication = Env->CallObjectMethod(activity->clazz, jtmID);
|
||||
jtmID = Env->GetMethodID(Env->GetObjectClass(jNativeApplication), "getApplicationContext", "()Landroid/content/Context;");
|
||||
|
||||
jobject jNativeContext = (jobject)Env->CallObjectMethod(jNativeApplication, jtmID);
|
||||
jobject jNativeContext = Env->CallObjectMethod(jNativeApplication, jtmID);
|
||||
jfieldID jNativeWIFI_SERVICE_fid = Env->GetStaticFieldID(Env->GetObjectClass(jNativeContext), "WIFI_SERVICE", "Ljava/lang/String;");
|
||||
jstring jNativeSFID_jstr = (jstring)Env->GetStaticObjectField(Env->FindClass("android/content/Context"), jNativeWIFI_SERVICE_fid);
|
||||
jstring wifiLockjStr = Env->NewStringUTF("WIVRn");
|
||||
@@ -576,6 +576,26 @@ application::application(application_info info) :
|
||||
|
||||
Env->DeleteLocalRef(wifiLockjStr);
|
||||
|
||||
// Get the intent, to handle wivrn://uri
|
||||
jtmID = Env->GetMethodID(jNativeClass, "getIntent", "()Landroid/content/Intent;");
|
||||
jobject intent = Env->CallObjectMethod(activity->clazz, jtmID);
|
||||
|
||||
jtmID = Env->GetMethodID(Env->GetObjectClass(intent), "getDataString", "()Ljava/lang/String;");
|
||||
jstring dataString_jni = (jstring)Env->CallObjectMethod(intent, jtmID);
|
||||
std::string dataString;
|
||||
if (dataString_jni)
|
||||
{
|
||||
const char * dataString_c = Env->GetStringUTFChars(dataString_jni, NULL);
|
||||
dataString = dataString_c;
|
||||
Env->ReleaseStringUTFChars(dataString_jni, dataString_c);
|
||||
}
|
||||
|
||||
spdlog::info("dataString = {}", dataString);
|
||||
if (dataString.starts_with("wivrn://"))
|
||||
{
|
||||
server_address = dataString.substr(strlen("wivrn://"));
|
||||
}
|
||||
|
||||
app_info.native_app->userData = this;
|
||||
app_info.native_app->onAppCmd = [](android_app * app, int32_t cmd) {
|
||||
switch (cmd)
|
||||
|
||||
@@ -282,6 +282,8 @@ class application
|
||||
bool debug_extensions_found = false;
|
||||
std::atomic<bool> exit_requested = false;
|
||||
|
||||
std::string server_address;
|
||||
|
||||
std::mutex scene_stack_lock;
|
||||
std::vector<std::shared_ptr<scene>> scene_stack;
|
||||
std::shared_ptr<scene> last_scene;
|
||||
@@ -434,4 +436,9 @@ public:
|
||||
{
|
||||
return instance_->vk_queue_family_index;
|
||||
}
|
||||
|
||||
const std::string& get_server_address() const
|
||||
{
|
||||
return server_address;
|
||||
}
|
||||
};
|
||||
|
||||
+60
-1
@@ -19,8 +19,14 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "scenes/lobby.h"
|
||||
#include "scenes/stream.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
#include "spdlog/sinks/android_sink.h"
|
||||
#include <android/native_window.h>
|
||||
@@ -45,7 +51,60 @@ void real_main()
|
||||
info.version = VK_MAKE_VERSION(1, 0, 0);
|
||||
application app(info);
|
||||
|
||||
app.push_scene<scenes::lobby>();
|
||||
std::string server_address = app.get_server_address();
|
||||
if (server_address.empty())
|
||||
app.push_scene<scenes::lobby>();
|
||||
else
|
||||
{
|
||||
std::unique_ptr<wivrn_session> session;
|
||||
|
||||
auto colon = server_address.rfind(":");
|
||||
int port;
|
||||
if (colon == std::string::npos)
|
||||
{
|
||||
port = xrt::drivers::wivrn::default_port;
|
||||
} else {
|
||||
port = std::stoi(server_address.substr(colon + 1));
|
||||
server_address = server_address.substr(0, colon);
|
||||
}
|
||||
struct addrinfo hint{
|
||||
.ai_flags = AI_ADDRCONFIG,
|
||||
.ai_family = AF_UNSPEC,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
};
|
||||
struct addrinfo * addresses;
|
||||
if (int err = getaddrinfo(server_address.c_str(), NULL, &hint, &addresses))
|
||||
{
|
||||
throw std::runtime_error("Unable to resolve address for " + server_address);
|
||||
}
|
||||
for (addrinfo *addr = addresses ; addr and not session; addr = addr->ai_next)
|
||||
{
|
||||
try {
|
||||
char buf[100];
|
||||
switch (addr->ai_family)
|
||||
{
|
||||
case AF_INET:
|
||||
inet_ntop(addr->ai_family, &((sockaddr_in*)addr->ai_addr)->sin_addr, buf, sizeof(buf));
|
||||
spdlog::info("Trying to connect to {} port {}", buf, port);
|
||||
session = std::make_unique<wivrn_session>(((sockaddr_in*)addr->ai_addr)->sin_addr, port);
|
||||
break;
|
||||
case AF_INET6:
|
||||
inet_ntop(addr->ai_family, &((sockaddr_in6*)addr->ai_addr)->sin6_addr, buf, sizeof(buf));
|
||||
spdlog::info("Trying to connect to {} port {}", buf, port);
|
||||
session = std::make_unique<wivrn_session>(((sockaddr_in6*)addr->ai_addr)->sin6_addr, port);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
spdlog::warn("Cannot connect to {}: {}", server_address, e.what());
|
||||
}
|
||||
}
|
||||
freeaddrinfo(addresses);
|
||||
if (not session)
|
||||
throw std::runtime_error("Unable to connect to " + server_address + ":" + std::to_string(port));
|
||||
app.push_scene(scenes::stream::create(std::move(session)));
|
||||
}
|
||||
|
||||
app.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user