mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-07-11 09:35:09 +02:00
176 lines
4.9 KiB
C++
176 lines
4.9 KiB
C++
#include "AdhocServerScreen.h"
|
|
|
|
#include "Common/Net/Resolve.h"
|
|
#include "Common/UI/Root.h"
|
|
#include "Common/StringUtils.h"
|
|
#include "Core/HLE/sceNetAdhoc.h"
|
|
|
|
AdhocServerScreen::AdhocServerScreen(std::string *value, std::string_view title)
|
|
: UI::PopupScreen(title, T(I18NCat::DIALOG, "OK"), T(I18NCat::DIALOG, "Cancel")), value_(value) {
|
|
resolver_ = std::thread([](AdhocServerScreen *thiz) {
|
|
thiz->ResolverThread();
|
|
}, this);
|
|
editValue_ = *value;
|
|
AdhocLoadServerList();
|
|
}
|
|
|
|
AdhocServerScreen::~AdhocServerScreen() {
|
|
{
|
|
std::unique_lock<std::mutex> guard(resolverLock_);
|
|
resolverState_ = ResolverState::QUIT;
|
|
resolverCond_.notify_one();
|
|
}
|
|
resolver_.join();
|
|
}
|
|
|
|
void AdhocServerScreen::sendMessage(UIMessage message, const char *value) {
|
|
if (message == UIMessage::ADHOC_SERVER_LIST_CHANGED) {
|
|
RecreateViews();
|
|
}
|
|
}
|
|
|
|
void AdhocServerScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
|
using namespace UI;
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
|
auto di = GetI18NCategory(I18NCat::DIALOG);
|
|
auto n = GetI18NCategory(I18NCat::NETWORKING);
|
|
|
|
auto listItems = AdhocGetServerList();
|
|
|
|
PopupTextInputChoice *textInputChoice = parent->Add(new PopupTextInputChoice(GetRequesterToken(), &editValue_, n->T("Hostname"), "", 256, screenManager()));
|
|
parent->Add(new Spacer(5.0f));
|
|
|
|
// Start with the downloaded list.
|
|
std::vector<AdhocServerListEntry> entries = listItems;
|
|
|
|
// Add localhost and local IPs, since those are common ones to connect to.
|
|
{
|
|
AdhocServerListEntry localhostEntry;
|
|
localhostEntry.name = "localhost";
|
|
localhostEntry.host = "localhost";
|
|
entries.push_back(localhostEntry);
|
|
|
|
std::vector<std::string> listIP;
|
|
net::GetLocalIP4List(listIP);
|
|
|
|
for (const auto &item : listIP) {
|
|
if (startsWith(item, "127.") || startsWith(item, "169.254.") || startsWith(item, "0.")) {
|
|
continue;
|
|
}
|
|
AdhocServerListEntry entry;
|
|
entry.name = item;
|
|
entry.host = item;
|
|
entries.push_back(entry);
|
|
}
|
|
}
|
|
|
|
ScrollView *scrollView = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0f));
|
|
LinearLayout *innerView = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT));
|
|
innerView->SetSpacing(5.0f);
|
|
|
|
// TODO: Fancier UI.
|
|
for (const auto &entry : entries) {
|
|
// Filter out IP prefixed with "127." and "169.254." also "0." since they can be redundant or unusable
|
|
auto button = innerView->Add(new Button(entry.host, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
|
|
button->OnClick.Add([this](UI::EventParams &e) {
|
|
std::string value = e.v->Tag();
|
|
if (!value.empty()) {
|
|
editValue_ = value;
|
|
// TODO: Let's change this to an actual button later.
|
|
System_CopyStringToClipboard(value);
|
|
}
|
|
});
|
|
button->SetTag(entry.host);
|
|
}
|
|
|
|
scrollView->Add(innerView);
|
|
parent->Add(scrollView);
|
|
|
|
progressView_ = parent->Add(new NoticeView(NoticeLevel::INFO, n->T("Validating address..."), "", new LinearLayoutParams(Margins(0, 5, 0, 0))));
|
|
progressView_->SetVisibility(UI::V_GONE);
|
|
}
|
|
|
|
void AdhocServerScreen::ResolverThread() {
|
|
std::unique_lock<std::mutex> guard(resolverLock_);
|
|
|
|
while (resolverState_ != ResolverState::QUIT) {
|
|
resolverCond_.wait(guard);
|
|
|
|
if (resolverState_ == ResolverState::QUEUED) {
|
|
resolverState_ = ResolverState::PROGRESS;
|
|
|
|
addrinfo *resolved = nullptr;
|
|
std::string err;
|
|
toResolveResult_ = net::DNSResolve(toResolve_, "80", &resolved, err);
|
|
if (resolved)
|
|
net::DNSResolveFree(resolved);
|
|
|
|
resolverState_ = ResolverState::READY;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool AdhocServerScreen::CanComplete(DialogResult result) {
|
|
auto n = GetI18NCategory(I18NCat::NETWORKING);
|
|
|
|
if (result != DR_OK)
|
|
return true;
|
|
|
|
std::string value = editValue_;
|
|
if (lastResolved_ == value) {
|
|
return true;
|
|
}
|
|
|
|
// Currently running.
|
|
if (resolverState_ == ResolverState::PROGRESS)
|
|
return false;
|
|
|
|
std::lock_guard<std::mutex> guard(resolverLock_);
|
|
switch (resolverState_) {
|
|
case ResolverState::PROGRESS:
|
|
case ResolverState::QUIT:
|
|
return false;
|
|
|
|
case ResolverState::QUEUED:
|
|
case ResolverState::WAITING:
|
|
break;
|
|
|
|
case ResolverState::READY:
|
|
if (toResolve_ == value) {
|
|
// Reset the state, nothing there now.
|
|
resolverState_ = ResolverState::WAITING;
|
|
toResolve_.clear();
|
|
lastResolved_ = value;
|
|
lastResolvedResult_ = toResolveResult_;
|
|
|
|
if (lastResolvedResult_) {
|
|
progressView_->SetVisibility(UI::V_GONE);
|
|
} else {
|
|
progressView_->SetText(n->T("Invalid IP or hostname"));
|
|
progressView_->SetLevel(NoticeLevel::ERROR);
|
|
progressView_->SetVisibility(UI::V_VISIBLE);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Throw away that last result, it was for a different value.
|
|
break;
|
|
}
|
|
|
|
resolverState_ = ResolverState::QUEUED;
|
|
toResolve_ = value;
|
|
resolverCond_.notify_one();
|
|
|
|
progressView_->SetText(n->T("Validating address..."));
|
|
progressView_->SetLevel(NoticeLevel::INFO);
|
|
progressView_->SetVisibility(UI::V_VISIBLE);
|
|
|
|
return false;
|
|
}
|
|
|
|
void AdhocServerScreen::OnCompleted(DialogResult result) {
|
|
if (result == DR_OK) {
|
|
*value_ = StripSpaces(editValue_);
|
|
}
|
|
}
|