swkbd.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <string>
  6. #include "common/assert.h"
  7. #include "common/logging/log.h"
  8. #include "common/string_util.h"
  9. #include "core/hle/applets/swkbd.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/kernel/shared_memory.h"
  12. #include "core/hle/result.h"
  13. #include "core/hle/service/gsp_gpu.h"
  14. #include "core/hle/service/hid/hid.h"
  15. #include "core/memory.h"
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. namespace HLE {
  18. namespace Applets {
  19. ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) {
  20. if (parameter.signal != static_cast<u32>(Service::APT::SignalType::Request)) {
  21. LOG_ERROR(Service_APT, "unsupported signal %u", parameter.signal);
  22. UNIMPLEMENTED();
  23. // TODO(Subv): Find the right error code
  24. return ResultCode(-1);
  25. }
  26. // The LibAppJustStarted message contains a buffer with the size of the framebuffer shared
  27. // memory.
  28. // Create the SharedMemory that will hold the framebuffer data
  29. Service::APT::CaptureBufferInfo capture_info;
  30. ASSERT(sizeof(capture_info) == parameter.buffer.size());
  31. memcpy(&capture_info, parameter.buffer.data(), sizeof(capture_info));
  32. using Kernel::MemoryPermission;
  33. // Allocate a heap block of the required size for this applet.
  34. heap_memory = std::make_shared<std::vector<u8>>(capture_info.size);
  35. // Create a SharedMemory that directly points to this heap block.
  36. framebuffer_memory = Kernel::SharedMemory::CreateForApplet(
  37. heap_memory, 0, capture_info.size, MemoryPermission::ReadWrite, MemoryPermission::ReadWrite,
  38. "SoftwareKeyboard Memory");
  39. // Send the response message with the newly created SharedMemory
  40. Service::APT::MessageParameter result;
  41. result.signal = static_cast<u32>(Service::APT::SignalType::Response);
  42. result.buffer.clear();
  43. result.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  44. result.sender_id = static_cast<u32>(id);
  45. result.object = framebuffer_memory;
  46. Service::APT::SendParameter(result);
  47. return RESULT_SUCCESS;
  48. }
  49. ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
  50. ASSERT_MSG(parameter.buffer.size() == sizeof(config),
  51. "The size of the parameter (SoftwareKeyboardConfig) is wrong");
  52. memcpy(&config, parameter.buffer.data(), parameter.buffer.size());
  53. text_memory =
  54. boost::static_pointer_cast<Kernel::SharedMemory, Kernel::Object>(parameter.object);
  55. // TODO(Subv): Verify if this is the correct behavior
  56. memset(text_memory->GetPointer(), 0, text_memory->size);
  57. DrawScreenKeyboard();
  58. is_running = true;
  59. return RESULT_SUCCESS;
  60. }
  61. void SoftwareKeyboard::Update() {
  62. // TODO(Subv): Handle input using the touch events from the HID module
  63. // TODO(Subv): Remove this hardcoded text
  64. std::u16string text = Common::UTF8ToUTF16("Citra");
  65. memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t));
  66. // TODO(Subv): Ask for input and write it to the shared memory
  67. // TODO(Subv): Find out what are the possible values for the return code,
  68. // some games seem to check for a hardcoded 2
  69. config.return_code = 2;
  70. config.text_length = 6;
  71. config.text_offset = 0;
  72. // TODO(Subv): We're finalizing the applet immediately after it's started,
  73. // but we should defer this call until after all the input has been collected.
  74. Finalize();
  75. }
  76. void SoftwareKeyboard::DrawScreenKeyboard() {
  77. auto bottom_screen = Service::GSP::GetFrameBufferInfo(0, 1);
  78. auto info = bottom_screen->framebuffer_info[bottom_screen->index];
  79. // TODO(Subv): Draw the HLE keyboard, for now just zero-fill the framebuffer
  80. Memory::ZeroBlock(info.address_left, info.stride * 320);
  81. Service::GSP::SetBufferSwap(1, info);
  82. }
  83. void SoftwareKeyboard::Finalize() {
  84. // Let the application know that we're closing
  85. Service::APT::MessageParameter message;
  86. message.buffer.resize(sizeof(SoftwareKeyboardConfig));
  87. std::memcpy(message.buffer.data(), &config, message.buffer.size());
  88. message.signal = static_cast<u32>(Service::APT::SignalType::WakeupByExit);
  89. message.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  90. message.sender_id = static_cast<u32>(id);
  91. Service::APT::SendParameter(message);
  92. is_running = false;
  93. }
  94. }
  95. } // namespace