CharacterMap.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <LibKeyboard/CharacterMapFile.h>
  29. #ifndef KERNEL
  30. # include <serenity.h>
  31. #endif
  32. namespace Keyboard {
  33. #ifndef KERNEL
  34. // The Kernel explicitly and exclusively links only this file into it.
  35. // Thus, we cannot even include a reference to the symbol `CharacterMapFile::load_from_file`.
  36. Optional<CharacterMap> CharacterMap::load_from_file(const String& map_name)
  37. {
  38. auto result = CharacterMapFile::load_from_file(map_name);
  39. if (!result.has_value())
  40. return {};
  41. return CharacterMap(map_name, result.value());
  42. }
  43. #endif
  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. return setkeymap(m_character_map_name.characters(), 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);
  53. }
  54. Result<CharacterMap, OSError> CharacterMap::fetch_system_map()
  55. {
  56. CharacterMapData map_data;
  57. char keymap_name[50 + 1] = { 0 };
  58. if (getkeymap(keymap_name, sizeof(keymap_name), map_data.map, map_data.shift_map, map_data.alt_map, map_data.altgr_map, map_data.shift_altgr_map) < 0) {
  59. return OSError(errno);
  60. }
  61. return CharacterMap { keymap_name, map_data };
  62. }
  63. #endif
  64. u32 CharacterMap::get_char(KeyEvent event) const
  65. {
  66. auto modifiers = event.modifiers();
  67. auto index = event.scancode & 0xFF; // Index is last byte of scan code.
  68. auto caps_lock_on = event.caps_lock_on;
  69. u32 code_point;
  70. if (modifiers & Mod_Alt)
  71. code_point = m_character_map_data.alt_map[index];
  72. else if ((modifiers & Mod_Shift) && (modifiers & Mod_AltGr))
  73. code_point = m_character_map_data.shift_altgr_map[index];
  74. else if (modifiers & Mod_Shift)
  75. code_point = m_character_map_data.shift_map[index];
  76. else if (modifiers & Mod_AltGr)
  77. code_point = m_character_map_data.altgr_map[index];
  78. else
  79. code_point = m_character_map_data.map[index];
  80. if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
  81. if (code_point >= 'a' && code_point <= 'z')
  82. code_point &= ~0x20;
  83. else if (code_point >= 'A' && code_point <= 'Z')
  84. code_point |= 0x20;
  85. }
  86. if (event.e0_prefix && event.key == Key_Slash) {
  87. // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
  88. code_point = '/';
  89. } else if (event.e0_prefix && event.key != Key_Return) {
  90. // Except for `keypad-/` and 'keypad-return', all e0 scan codes are not actually characters. i.e., `keypad-0` and
  91. // `Insert` have the same scancode except for the prefix, but insert should not have a code_point.
  92. code_point = 0;
  93. }
  94. return code_point;
  95. }
  96. void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
  97. {
  98. m_character_map_data = character_map_data;
  99. }
  100. void CharacterMap::set_character_map_name(const String& character_map_name)
  101. {
  102. m_character_map_name = character_map_name;
  103. }
  104. const String& CharacterMap::character_map_name() const
  105. {
  106. return m_character_map_name;
  107. }
  108. }