CharacterMap.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CharacterMap.h"
  7. #include <AK/StringBuilder.h>
  8. #ifndef KERNEL
  9. # include <LibKeyboard/CharacterMapFile.h>
  10. # include <serenity.h>
  11. #endif
  12. namespace Keyboard {
  13. #ifndef KERNEL
  14. // The Kernel explicitly and exclusively links only this file into it.
  15. // Thus, we cannot even include a reference to the symbol `CharacterMapFile::load_from_file`.
  16. Optional<CharacterMap> CharacterMap::load_from_file(const String& map_name)
  17. {
  18. auto result = CharacterMapFile::load_from_file(map_name);
  19. if (!result.has_value())
  20. return {};
  21. return CharacterMap(map_name, result.value());
  22. }
  23. #endif
  24. CharacterMap::CharacterMap(const String& map_name, const CharacterMapData& map_data)
  25. : m_character_map_data(map_data)
  26. , m_character_map_name(map_name)
  27. {
  28. }
  29. #ifndef KERNEL
  30. int CharacterMap::set_system_map()
  31. {
  32. 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);
  33. }
  34. Result<CharacterMap, OSError> CharacterMap::fetch_system_map()
  35. {
  36. CharacterMapData map_data;
  37. char keymap_name[50 + 1] = { 0 };
  38. 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) {
  39. return OSError(errno);
  40. }
  41. return CharacterMap { keymap_name, map_data };
  42. }
  43. #endif
  44. u32 CharacterMap::get_char(KeyEvent event) const
  45. {
  46. auto modifiers = event.modifiers();
  47. auto index = event.scancode & 0xFF; // Index is last byte of scan code.
  48. auto caps_lock_on = event.caps_lock_on;
  49. u32 code_point;
  50. if (modifiers & Mod_Alt)
  51. code_point = m_character_map_data.alt_map[index];
  52. else if ((modifiers & Mod_Shift) && (modifiers & Mod_AltGr))
  53. code_point = m_character_map_data.shift_altgr_map[index];
  54. else if (modifiers & Mod_Shift)
  55. code_point = m_character_map_data.shift_map[index];
  56. else if (modifiers & Mod_AltGr)
  57. code_point = m_character_map_data.altgr_map[index];
  58. else
  59. code_point = m_character_map_data.map[index];
  60. if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
  61. if (code_point >= 'a' && code_point <= 'z')
  62. code_point &= ~0x20;
  63. else if (code_point >= 'A' && code_point <= 'Z')
  64. code_point |= 0x20;
  65. }
  66. if (event.e0_prefix && event.key == Key_Slash) {
  67. // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
  68. code_point = '/';
  69. } else if (event.e0_prefix && event.key != Key_Return) {
  70. // Except for `keypad-/` and 'keypad-return', all e0 scan codes are not actually characters. i.e., `keypad-0` and
  71. // `Insert` have the same scancode except for the prefix, but insert should not have a code_point.
  72. code_point = 0;
  73. }
  74. return code_point;
  75. }
  76. void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
  77. {
  78. m_character_map_data = character_map_data;
  79. }
  80. void CharacterMap::set_character_map_name(const String& character_map_name)
  81. {
  82. m_character_map_name = character_map_name;
  83. }
  84. const String& CharacterMap::character_map_name() const
  85. {
  86. return m_character_map_name;
  87. }
  88. }