CharacterMap.cpp 3.3 KB

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