CharacterMap.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. UNUSED_PARAM(file_name);
  37. m_character_map_data = default_character_map;
  38. #else
  39. auto result = CharacterMapFile::load_from_file(file_name);
  40. ASSERT(result.has_value());
  41. m_character_map_data = result.value();
  42. #endif
  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 };
  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_Shift)
  58. code_point = m_character_map_data.shift_map[index];
  59. else if (modifiers & Mod_Alt)
  60. code_point = m_character_map_data.alt_map[index];
  61. else if (modifiers & Mod_AltGr)
  62. code_point = m_character_map_data.altgr_map[index];
  63. else
  64. code_point = m_character_map_data.map[index];
  65. if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
  66. if (code_point >= 'a' && code_point <= 'z')
  67. code_point &= ~0x20;
  68. else if (code_point >= 'A' && code_point <= 'Z')
  69. code_point |= 0x20;
  70. }
  71. if (event.e0_prefix && event.key == Key_Slash) {
  72. // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
  73. code_point = '/';
  74. }
  75. return code_point;
  76. }
  77. void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
  78. {
  79. m_character_map_data = character_map_data;
  80. }
  81. }