2015-05-26 06:30:20 +02:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-06-20 14:01:50 +02:00
|
|
|
#include <algorithm>
|
2015-07-12 23:10:37 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
2015-05-26 06:30:20 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2015-05-27 22:21:06 +02:00
|
|
|
#include "common/string_util.h"
|
2018-10-13 23:08:37 +02:00
|
|
|
#include "core/core.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "core/hle/applets/swkbd.h"
|
2015-07-12 23:10:37 +02:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
|
|
|
#include "core/hle/result.h"
|
2017-12-16 20:35:37 +01:00
|
|
|
#include "core/hle/service/gsp/gsp.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/hle/service/hid/hid.h"
|
2015-07-12 23:10:37 +02:00
|
|
|
#include "core/memory.h"
|
2015-05-26 06:30:20 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-02-19 02:34:18 +01:00
|
|
|
namespace HLE::Applets {
|
2015-05-26 06:30:20 +02:00
|
|
|
|
|
|
|
ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) {
|
2019-03-17 02:35:20 +01:00
|
|
|
switch (parameter.signal) {
|
|
|
|
case Service::APT::SignalType::Request: {
|
|
|
|
// The LibAppJustStarted message contains a buffer with the size of the framebuffer shared
|
|
|
|
// memory.
|
|
|
|
// Create the SharedMemory that will hold the framebuffer data
|
|
|
|
Service::APT::CaptureBufferInfo capture_info;
|
|
|
|
ASSERT(sizeof(capture_info) == parameter.buffer.size());
|
|
|
|
|
2019-03-18 15:02:31 +01:00
|
|
|
std::memcpy(&capture_info, parameter.buffer.data(), sizeof(capture_info));
|
2019-03-17 02:35:20 +01:00
|
|
|
|
|
|
|
using Kernel::MemoryPermission;
|
|
|
|
// Create a SharedMemory that directly points to this heap block.
|
|
|
|
framebuffer_memory = Core::System::GetInstance().Kernel().CreateSharedMemoryForApplet(
|
|
|
|
0, capture_info.size, MemoryPermission::ReadWrite, MemoryPermission::ReadWrite,
|
|
|
|
"SoftwareKeyboard Memory");
|
|
|
|
|
|
|
|
// Send the response message with the newly created SharedMemory
|
|
|
|
Service::APT::MessageParameter result;
|
|
|
|
result.signal = Service::APT::SignalType::Response;
|
|
|
|
result.buffer.clear();
|
|
|
|
result.destination_id = Service::APT::AppletId::Application;
|
|
|
|
result.sender_id = id;
|
|
|
|
result.object = framebuffer_memory;
|
|
|
|
|
|
|
|
SendParameter(result);
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Service::APT::SignalType::Message: {
|
|
|
|
// Callback result
|
|
|
|
ASSERT_MSG(parameter.buffer.size() == sizeof(config),
|
|
|
|
"The size of the parameter (SoftwareKeyboardConfig) is wrong");
|
|
|
|
|
2019-03-18 15:02:31 +01:00
|
|
|
std::memcpy(&config, parameter.buffer.data(), parameter.buffer.size());
|
2019-03-17 02:35:20 +01:00
|
|
|
|
|
|
|
switch (config.callback_result) {
|
|
|
|
case SoftwareKeyboardCallbackResult::OK:
|
|
|
|
// Finish execution
|
|
|
|
Finalize();
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
|
|
|
|
case SoftwareKeyboardCallbackResult::Close:
|
|
|
|
// Let the frontend display error and quit
|
2019-04-08 16:34:21 +02:00
|
|
|
frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg));
|
2019-03-17 02:35:20 +01:00
|
|
|
config.return_code = SoftwareKeyboardResult::BannedInput;
|
|
|
|
config.text_offset = config.text_length = 0;
|
|
|
|
Finalize();
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
|
|
|
|
case SoftwareKeyboardCallbackResult::Continue:
|
|
|
|
// Let the frontend display error and get input again
|
|
|
|
// The input will be sent for validation again on next Update().
|
2019-04-08 16:34:21 +02:00
|
|
|
frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg));
|
2019-03-17 02:35:20 +01:00
|
|
|
frontend_applet->Execute(ToFrontendConfig(config));
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Service_APT, "unsupported signal {}", static_cast<u32>(parameter.signal));
|
2015-05-26 06:30:20 +02:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
// TODO(Subv): Find the right error code
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
2019-03-17 02:35:20 +01:00
|
|
|
}
|
2015-05-26 06:30:20 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 22:21:06 +02:00
|
|
|
ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
|
2016-09-18 02:38:01 +02:00
|
|
|
ASSERT_MSG(parameter.buffer.size() == sizeof(config),
|
|
|
|
"The size of the parameter (SoftwareKeyboardConfig) is wrong");
|
2015-05-27 22:21:06 +02:00
|
|
|
|
2016-04-16 12:18:49 +02:00
|
|
|
memcpy(&config, parameter.buffer.data(), parameter.buffer.size());
|
2019-03-23 21:04:19 +01:00
|
|
|
text_memory = std::static_pointer_cast<Kernel::SharedMemory, Kernel::Object>(parameter.object);
|
2015-05-26 06:30:20 +02:00
|
|
|
|
2015-05-26 18:00:26 +02:00
|
|
|
DrawScreenKeyboard();
|
|
|
|
|
2018-06-20 06:01:54 +02:00
|
|
|
using namespace Frontend;
|
2019-02-15 19:20:06 +01:00
|
|
|
frontend_applet = Core::System::GetInstance().GetSoftwareKeyboard();
|
|
|
|
ASSERT(frontend_applet);
|
|
|
|
|
2019-03-17 02:35:20 +01:00
|
|
|
frontend_applet->Execute(ToFrontendConfig(config));
|
2018-06-20 06:01:54 +02:00
|
|
|
|
2016-12-07 23:11:39 +01:00
|
|
|
is_running = true;
|
2015-05-26 18:00:26 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareKeyboard::Update() {
|
2019-03-17 02:35:20 +01:00
|
|
|
if (!frontend_applet->DataReady())
|
|
|
|
return;
|
|
|
|
|
2018-06-20 06:01:54 +02:00
|
|
|
using namespace Frontend;
|
2019-03-17 02:35:20 +01:00
|
|
|
const KeyboardData& data = frontend_applet->ReceiveData();
|
2018-06-20 06:01:54 +02:00
|
|
|
std::u16string text = Common::UTF8ToUTF16(data.text);
|
2019-12-01 16:00:09 +01:00
|
|
|
// Include a null terminator
|
|
|
|
memcpy(text_memory->GetPointer(), text.c_str(), (text.length() + 1) * sizeof(char16_t));
|
2018-06-20 06:01:54 +02:00
|
|
|
switch (config.num_buttons_m1) {
|
2018-06-20 14:01:50 +02:00
|
|
|
case SoftwareKeyboardButtonConfig::SingleButton:
|
|
|
|
config.return_code = SoftwareKeyboardResult::D0Click;
|
2018-06-20 06:01:54 +02:00
|
|
|
break;
|
2018-06-20 14:01:50 +02:00
|
|
|
case SoftwareKeyboardButtonConfig::DualButton:
|
2018-06-20 06:01:54 +02:00
|
|
|
if (data.button == 0)
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::D1Click0;
|
2018-06-20 06:01:54 +02:00
|
|
|
else
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::D1Click1;
|
2018-06-20 06:01:54 +02:00
|
|
|
break;
|
2018-06-20 14:01:50 +02:00
|
|
|
case SoftwareKeyboardButtonConfig::TripleButton:
|
2018-06-20 06:01:54 +02:00
|
|
|
if (data.button == 0)
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::D2Click0;
|
2018-06-20 06:01:54 +02:00
|
|
|
else if (data.button == 1)
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::D2Click1;
|
2018-06-20 06:01:54 +02:00
|
|
|
else
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::D2Click2;
|
2018-06-20 06:01:54 +02:00
|
|
|
break;
|
2018-06-20 14:01:50 +02:00
|
|
|
case SoftwareKeyboardButtonConfig::NoButton:
|
2018-06-20 06:01:54 +02:00
|
|
|
// TODO: find out what is actually returned
|
2018-06-20 14:01:50 +02:00
|
|
|
config.return_code = SoftwareKeyboardResult::None;
|
2018-06-20 06:01:54 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-06-20 14:01:50 +02:00
|
|
|
LOG_CRITICAL(Applet_SWKBD, "Unknown button config {}",
|
2018-09-22 02:15:55 +02:00
|
|
|
static_cast<u32>(config.num_buttons_m1));
|
2018-06-20 06:01:54 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2015-05-26 18:00:26 +02:00
|
|
|
|
2018-06-20 14:01:50 +02:00
|
|
|
config.text_length = static_cast<u16>(text.size());
|
2015-05-26 06:30:20 +02:00
|
|
|
config.text_offset = 0;
|
|
|
|
|
2019-03-17 02:35:20 +01:00
|
|
|
if (config.filter_flags & HLE::Applets::SoftwareKeyboardFilter::Callback) {
|
|
|
|
// Send the message to invoke callback
|
|
|
|
Service::APT::MessageParameter message;
|
|
|
|
message.buffer.resize(sizeof(SoftwareKeyboardConfig));
|
|
|
|
std::memcpy(message.buffer.data(), &config, message.buffer.size());
|
|
|
|
message.signal = Service::APT::SignalType::Message;
|
|
|
|
message.destination_id = Service::APT::AppletId::Application;
|
|
|
|
message.sender_id = id;
|
|
|
|
SendParameter(message);
|
|
|
|
} else {
|
|
|
|
Finalize();
|
|
|
|
}
|
2015-05-26 18:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareKeyboard::DrawScreenKeyboard() {
|
2018-06-20 14:01:50 +02:00
|
|
|
// TODO(Subv): Draw the HLE keyboard, for now just do nothing
|
2015-05-26 18:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareKeyboard::Finalize() {
|
|
|
|
// Let the application know that we're closing
|
2015-05-26 06:30:20 +02:00
|
|
|
Service::APT::MessageParameter message;
|
2016-04-16 12:18:49 +02:00
|
|
|
message.buffer.resize(sizeof(SoftwareKeyboardConfig));
|
|
|
|
std::memcpy(message.buffer.data(), &config, message.buffer.size());
|
2018-01-25 14:39:54 +01:00
|
|
|
message.signal = Service::APT::SignalType::WakeupByExit;
|
|
|
|
message.destination_id = Service::APT::AppletId::Application;
|
|
|
|
message.sender_id = id;
|
|
|
|
SendParameter(message);
|
2015-05-26 06:30:20 +02:00
|
|
|
|
2016-12-07 23:11:39 +01:00
|
|
|
is_running = false;
|
2018-11-10 17:27:15 +01:00
|
|
|
text_memory = nullptr;
|
2015-05-26 06:30:20 +02:00
|
|
|
}
|
2018-06-20 06:01:54 +02:00
|
|
|
|
2018-06-20 14:01:50 +02:00
|
|
|
Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(
|
|
|
|
const SoftwareKeyboardConfig& config) const {
|
2018-06-20 06:01:54 +02:00
|
|
|
using namespace Frontend;
|
|
|
|
KeyboardConfig frontend_config;
|
2018-09-22 02:15:55 +02:00
|
|
|
frontend_config.button_config =
|
|
|
|
static_cast<ButtonConfig>(static_cast<u32>(config.num_buttons_m1));
|
|
|
|
frontend_config.accept_mode = static_cast<AcceptedInput>(static_cast<u32>(config.valid_input));
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.multiline_mode = config.multiline;
|
|
|
|
frontend_config.max_text_length = config.max_text_length;
|
|
|
|
frontend_config.max_digits = config.max_digits;
|
2019-04-08 16:34:21 +02:00
|
|
|
frontend_config.hint_text = Common::UTF16BufferToUTF8(config.hint_text);
|
2020-05-31 03:40:12 +02:00
|
|
|
for (const auto& text : config.button_text) {
|
|
|
|
frontend_config.button_text.push_back(Common::UTF16BufferToUTF8(text));
|
2018-06-20 06:01:54 +02:00
|
|
|
}
|
|
|
|
frontend_config.filters.prevent_digit =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::Digits);
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.filters.prevent_at =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::At);
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.filters.prevent_percent =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::Percent);
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.filters.prevent_backslash =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::Backslash);
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.filters.prevent_profanity =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::Profanity);
|
2018-06-20 06:01:54 +02:00
|
|
|
frontend_config.filters.enable_callback =
|
2018-06-20 14:01:50 +02:00
|
|
|
static_cast<bool>(config.filter_flags & SoftwareKeyboardFilter::Callback);
|
2018-06-20 06:01:54 +02:00
|
|
|
return frontend_config;
|
|
|
|
}
|
2019-02-19 02:34:18 +01:00
|
|
|
} // namespace HLE::Applets
|