CharacterMap.cpp 3.3 KB

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