CharacterMap.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "CharacterMap.h"
  27. #include <AK/StringBuilder.h>
  28. #include <Kernel/API/Syscall.h>
  29. #ifndef KERNEL
  30. # include <LibKeyboard/CharacterMapFile.h>
  31. #endif
  32. namespace Keyboard {
  33. CharacterMap::CharacterMap(const String& map_name)
  34. {
  35. #ifdef KERNEL
  36. m_character_map_data = default_character_map;
  37. #else
  38. auto result = CharacterMapFile::load_from_file(map_name);
  39. ASSERT(result.has_value());
  40. m_character_map_data = result.value();
  41. #endif
  42. m_character_map_name = map_name;
  43. }
  44. CharacterMap::CharacterMap(const String& map_name, const CharacterMapData& map_data)
  45. : m_character_map_data(map_data)
  46. , m_character_map_name(map_name)
  47. {
  48. }
  49. #ifndef KERNEL
  50. int CharacterMap::set_system_map()
  51. {
  52. Syscall::SC_setkeymap_params params { m_character_map_data.map, m_character_map_data.shift_map, m_character_map_data.alt_map, m_character_map_data.altgr_map, m_character_map_data.shift_altgr_map, { m_character_map_name.characters(), m_character_map_name.length() } };
  53. return syscall(SC_setkeymap, &params);
  54. }
  55. Result<CharacterMap, OSError> CharacterMap::fetch_system_map()
  56. {
  57. CharacterMapData map_data;
  58. char keymap_name[50 + 1] = { 0 };
  59. Syscall::SC_getkeymap_params params {
  60. map_data.map, map_data.shift_map,
  61. map_data.alt_map,
  62. map_data.altgr_map,
  63. map_data.shift_altgr_map,
  64. { keymap_name, sizeof(keymap_name) }
  65. };
  66. int rc = syscall(SC_getkeymap, &params);
  67. if (rc < 0) {
  68. return OSError(-rc);
  69. }
  70. return CharacterMap { keymap_name, map_data };
  71. }
  72. #endif
  73. u32 CharacterMap::get_char(KeyEvent event)
  74. {
  75. auto modifiers = event.modifiers();
  76. auto index = event.scancode & 0xFF; // Index is last byte of scan code.
  77. auto caps_lock_on = event.caps_lock_on;
  78. u32 code_point;
  79. if (modifiers & Mod_Alt)
  80. code_point = m_character_map_data.alt_map[index];
  81. else if ((modifiers & Mod_Shift) && (modifiers & Mod_AltGr))
  82. code_point = m_character_map_data.shift_altgr_map[index];
  83. else if (modifiers & Mod_Shift)
  84. code_point = m_character_map_data.shift_map[index];
  85. else if (modifiers & Mod_AltGr)
  86. code_point = m_character_map_data.altgr_map[index];
  87. else
  88. code_point = m_character_map_data.map[index];
  89. if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
  90. if (code_point >= 'a' && code_point <= 'z')
  91. code_point &= ~0x20;
  92. else if (code_point >= 'A' && code_point <= 'Z')
  93. code_point |= 0x20;
  94. }
  95. if (event.e0_prefix && event.key == Key_Slash) {
  96. // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
  97. code_point = '/';
  98. } else if (event.e0_prefix && event.key != Key_Return) {
  99. // Except for `keypad-/` and 'keypad-return', all e0 scan codes are not actually characters. i.e., `keypad-0` and
  100. // `Insert` have the same scancode except for the prefix, but insert should not have a code_point.
  101. code_point = 0;
  102. }
  103. return code_point;
  104. }
  105. void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
  106. {
  107. m_character_map_data = character_map_data;
  108. }
  109. void CharacterMap::set_character_map_name(const String& character_map_name)
  110. {
  111. m_character_map_name = character_map_name;
  112. }
  113. const String CharacterMap::character_map_name()
  114. {
  115. return m_character_map_name;
  116. }
  117. }