CharacterMap.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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& file_name)
  34. {
  35. #ifdef KERNEL
  36. m_character_map_data = default_character_map;
  37. #else
  38. auto result = CharacterMapFile::load_from_file(file_name);
  39. ASSERT(result.has_value());
  40. m_character_map_data = result.value();
  41. #endif
  42. m_character_map_name = file_name;
  43. }
  44. #ifndef KERNEL
  45. int CharacterMap::set_system_map()
  46. {
  47. 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() } };
  48. return syscall(SC_setkeymap, &params);
  49. }
  50. #endif
  51. u32 CharacterMap::get_char(KeyEvent event)
  52. {
  53. auto modifiers = event.modifiers();
  54. auto index = event.scancode & 0xFF; // Index is last byte of scan code.
  55. auto caps_lock_on = event.caps_lock_on;
  56. u32 code_point;
  57. if (modifiers & Mod_Alt)
  58. code_point = m_character_map_data.alt_map[index];
  59. else if ((modifiers & Mod_Shift) && (modifiers & Mod_AltGr))
  60. code_point = m_character_map_data.shift_altgr_map[index];
  61. else if (modifiers & Mod_Shift)
  62. code_point = m_character_map_data.shift_map[index];
  63. else if (modifiers & Mod_AltGr)
  64. code_point = m_character_map_data.altgr_map[index];
  65. else
  66. code_point = m_character_map_data.map[index];
  67. if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
  68. if (code_point >= 'a' && code_point <= 'z')
  69. code_point &= ~0x20;
  70. else if (code_point >= 'A' && code_point <= 'Z')
  71. code_point |= 0x20;
  72. }
  73. if (event.e0_prefix && event.key == Key_Slash) {
  74. // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
  75. code_point = '/';
  76. } else if (event.e0_prefix && event.key != Key_Return) {
  77. // Except for `keypad-/` and 'keypad-return', all e0 scan codes are not actually characters. i.e., `keypad-0` and
  78. // `Insert` have the same scancode except for the prefix, but insert should not have a code_point.
  79. code_point = 0;
  80. }
  81. return code_point;
  82. }
  83. void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
  84. {
  85. m_character_map_data = character_map_data;
  86. }
  87. void CharacterMap::set_character_map_name(const String& character_map_name)
  88. {
  89. m_character_map_name = character_map_name;
  90. }
  91. const String CharacterMap::character_map_name()
  92. {
  93. return m_character_map_name;
  94. }
  95. }