swkbd.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. framebuffer_memory = Kernel::SharedMemory::Create(nullptr, capture_info.size, MemoryPermission::ReadWrite,
  34. MemoryPermission::ReadWrite, 0, Kernel::MemoryRegion::BASE, "SoftwareKeyboard Memory");
  35. // Send the response message with the newly created SharedMemory
  36. Service::APT::MessageParameter result;
  37. result.signal = static_cast<u32>(Service::APT::SignalType::LibAppFinished);
  38. result.data = nullptr;
  39. result.buffer_size = 0;
  40. result.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  41. result.sender_id = static_cast<u32>(id);
  42. result.object = framebuffer_memory;
  43. Service::APT::SendParameter(result);
  44. return RESULT_SUCCESS;
  45. }
  46. ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
  47. ASSERT_MSG(parameter.buffer_size == sizeof(config), "The size of the parameter (SoftwareKeyboardConfig) is wrong");
  48. memcpy(&config, parameter.data, parameter.buffer_size);
  49. text_memory = boost::static_pointer_cast<Kernel::SharedMemory, Kernel::Object>(parameter.object);
  50. // TODO(Subv): Verify if this is the correct behavior
  51. memset(text_memory->GetPointer(), 0, text_memory->size);
  52. DrawScreenKeyboard();
  53. started = true;
  54. return RESULT_SUCCESS;
  55. }
  56. void SoftwareKeyboard::Update() {
  57. // TODO(Subv): Handle input using the touch events from the HID module
  58. // TODO(Subv): Remove this hardcoded text
  59. std::u16string text = Common::UTF8ToUTF16("Citra");
  60. memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t));
  61. // TODO(Subv): Ask for input and write it to the shared memory
  62. // TODO(Subv): Find out what are the possible values for the return code,
  63. // some games seem to check for a hardcoded 2
  64. config.return_code = 2;
  65. config.text_length = 6;
  66. config.text_offset = 0;
  67. // TODO(Subv): We're finalizing the applet immediately after it's started,
  68. // but we should defer this call until after all the input has been collected.
  69. Finalize();
  70. }
  71. void SoftwareKeyboard::DrawScreenKeyboard() {
  72. auto bottom_screen = GSP_GPU::GetFrameBufferInfo(0, 1);
  73. auto info = bottom_screen->framebuffer_info[bottom_screen->index];
  74. // TODO(Subv): Draw the HLE keyboard, for now just zero-fill the framebuffer
  75. memset(Memory::GetPointer(info.address_left), 0, info.stride * 320);
  76. GSP_GPU::SetBufferSwap(1, info);
  77. }
  78. void SoftwareKeyboard::Finalize() {
  79. // Let the application know that we're closing
  80. Service::APT::MessageParameter message;
  81. message.buffer_size = sizeof(SoftwareKeyboardConfig);
  82. message.data = reinterpret_cast<u8*>(&config);
  83. message.signal = static_cast<u32>(Service::APT::SignalType::LibAppClosed);
  84. message.destination_id = static_cast<u32>(Service::APT::AppletId::Application);
  85. message.sender_id = static_cast<u32>(id);
  86. Service::APT::SendParameter(message);
  87. started = false;
  88. }
  89. }
  90. } // namespace