swkbd.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/service/hid/hid.h"
  13. #include "core/hle/service/gsp_gpu.h"
  14. #include "core/hle/result.h"
  15. #include "core/memory.h"
  16. #include "video_core/video_core.h"
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. namespace HLE {
  19. namespace Applets {
  20. ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) {
  21. if (parameter.signal != static_cast<u32>(Service::APT::SignalType::LibAppJustStarted)) {
  22. LOG_ERROR(Service_APT, "unsupported signal %u", parameter.signal);
  23. UNIMPLEMENTED();
  24. // TODO(Subv): Find the right error code
  25. return ResultCode(-1);
  26. }
  27. // The LibAppJustStarted message contains a buffer with the size of the framebuffer shared 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.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(heap_memory, 0, heap_memory->size(),
  37. 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::LibAppFinished);
  42. result.data = nullptr;
  43. result.buffer_size = 0;
  44. result.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  45. result.sender_id = static_cast<u32>(id);
  46. result.object = framebuffer_memory;
  47. Service::APT::SendParameter(result);
  48. return RESULT_SUCCESS;
  49. }
  50. ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
  51. ASSERT_MSG(parameter.buffer_size == sizeof(config), "The size of the parameter (SoftwareKeyboardConfig) is wrong");
  52. memcpy(&config, parameter.data, parameter.buffer_size);
  53. text_memory = boost::static_pointer_cast<Kernel::SharedMemory, Kernel::Object>(parameter.object);
  54. // TODO(Subv): Verify if this is the correct behavior
  55. memset(text_memory->GetPointer(), 0, text_memory->size);
  56. DrawScreenKeyboard();
  57. started = true;
  58. return RESULT_SUCCESS;
  59. }
  60. void SoftwareKeyboard::Update() {
  61. // TODO(Subv): Handle input using the touch events from the HID module
  62. // TODO(Subv): Remove this hardcoded text
  63. std::u16string text = Common::UTF8ToUTF16("Citra");
  64. memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t));
  65. // TODO(Subv): Ask for input and write it to the shared memory
  66. // TODO(Subv): Find out what are the possible values for the return code,
  67. // some games seem to check for a hardcoded 2
  68. config.return_code = 2;
  69. config.text_length = 6;
  70. config.text_offset = 0;
  71. // TODO(Subv): We're finalizing the applet immediately after it's started,
  72. // but we should defer this call until after all the input has been collected.
  73. Finalize();
  74. }
  75. void SoftwareKeyboard::DrawScreenKeyboard() {
  76. auto bottom_screen = GSP_GPU::GetFrameBufferInfo(0, 1);
  77. auto info = bottom_screen->framebuffer_info[bottom_screen->index];
  78. // TODO(Subv): Draw the HLE keyboard, for now just zero-fill the framebuffer
  79. memset(Memory::GetPointer(info.address_left), 0, info.stride * 320);
  80. GSP_GPU::SetBufferSwap(1, info);
  81. }
  82. void SoftwareKeyboard::Finalize() {
  83. // Let the application know that we're closing
  84. Service::APT::MessageParameter message;
  85. message.buffer_size = sizeof(SoftwareKeyboardConfig);
  86. message.data = reinterpret_cast<u8*>(&config);
  87. message.signal = static_cast<u32>(Service::APT::SignalType::LibAppClosed);
  88. message.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  89. message.sender_id = static_cast<u32>(id);
  90. Service::APT::SendParameter(message);
  91. started = false;
  92. }
  93. }
  94. } // namespace