| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com> | 
					
						
							|  |  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "CharacterMap.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:15:13 +01:00
										 |  |  |  | #ifndef KERNEL
 | 
					
						
							| 
									
										
										
										
											2021-06-30 11:30:28 +02:00
										 |  |  |  | #    include <LibKeyboard/CharacterMapFile.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:15:13 +01:00
										 |  |  |  | #    include <serenity.h>
 | 
					
						
							|  |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | namespace Keyboard { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-30 22:41:29 +01:00
										 |  |  |  | #ifndef KERNEL
 | 
					
						
							|  |  |  |  | // The Kernel explicitly and exclusively links only this file into it.
 | 
					
						
							|  |  |  |  | // Thus, we cannot even include a reference to the symbol `CharacterMapFile::load_from_file`.
 | 
					
						
							|  |  |  |  | Optional<CharacterMap> CharacterMap::load_from_file(const String& map_name) | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-30 22:01:34 +01:00
										 |  |  |  |     auto result = CharacterMapFile::load_from_file(map_name); | 
					
						
							| 
									
										
										
										
											2021-01-30 22:41:29 +01:00
										 |  |  |  |     if (!result.has_value()) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-30 22:41:29 +01:00
										 |  |  |  |     return CharacterMap(map_name, result.value()); | 
					
						
							| 
									
										
										
										
											2021-01-30 22:01:34 +01:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-30 22:41:29 +01:00
										 |  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-30 22:01:34 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | CharacterMap::CharacterMap(const String& map_name, const CharacterMapData& map_data) | 
					
						
							|  |  |  |  |     : m_character_map_data(map_data) | 
					
						
							|  |  |  |  |     , m_character_map_name(map_name) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 16:31:13 -07:00
										 |  |  |  | #ifndef KERNEL
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | int CharacterMap::set_system_map() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:15:13 +01:00
										 |  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-30 22:01:34 +01:00
										 |  |  |  | Result<CharacterMap, OSError> CharacterMap::fetch_system_map() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     CharacterMapData map_data; | 
					
						
							|  |  |  |  |     char keymap_name[50 + 1] = { 0 }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:15:13 +01:00
										 |  |  |  |     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) { | 
					
						
							|  |  |  |  |         return OSError(errno); | 
					
						
							| 
									
										
										
										
											2021-01-30 22:01:34 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return CharacterMap { keymap_name, map_data }; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 16:31:13 -07:00
										 |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 21:12:47 +03:00
										 |  |  |  | u32 CharacterMap::get_char(KeyEvent event) const | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto modifiers = event.modifiers(); | 
					
						
							|  |  |  |  |     auto index = event.scancode & 0xFF; // Index is last byte of scan code.
 | 
					
						
							|  |  |  |  |     auto caps_lock_on = event.caps_lock_on; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |     u32 code_point; | 
					
						
							| 
									
										
										
										
											2021-01-05 09:45:17 +01:00
										 |  |  |  |     if (modifiers & Mod_Alt) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |         code_point = m_character_map_data.alt_map[index]; | 
					
						
							| 
									
										
										
										
											2021-01-05 09:45:17 +01:00
										 |  |  |  |     else if ((modifiers & Mod_Shift) && (modifiers & Mod_AltGr)) | 
					
						
							|  |  |  |  |         code_point = m_character_map_data.shift_altgr_map[index]; | 
					
						
							|  |  |  |  |     else if (modifiers & Mod_Shift) | 
					
						
							|  |  |  |  |         code_point = m_character_map_data.shift_map[index]; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  |     else if (modifiers & Mod_AltGr) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |         code_point = m_character_map_data.altgr_map[index]; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |         code_point = m_character_map_data.map[index]; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |         if (code_point >= 'a' && code_point <= 'z') | 
					
						
							|  |  |  |  |             code_point &= ~0x20; | 
					
						
							|  |  |  |  |         else if (code_point >= 'A' && code_point <= 'Z') | 
					
						
							|  |  |  |  |             code_point |= 0x20; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (event.e0_prefix && event.key == Key_Slash) { | 
					
						
							|  |  |  |  |         // If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |         code_point = '/'; | 
					
						
							| 
									
										
										
										
											2021-01-06 19:27:57 +03:00
										 |  |  |  |     } else if (event.e0_prefix && event.key != Key_Return) { | 
					
						
							|  |  |  |  |         // Except for `keypad-/` and 'keypad-return', all e0 scan codes are not actually characters. i.e., `keypad-0` and
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:26:04 +02:00
										 |  |  |  |         // `Insert` have the same scancode except for the prefix, but insert should not have a code_point.
 | 
					
						
							|  |  |  |  |         code_point = 0; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:51:20 +03:00
										 |  |  |  |     return code_point; | 
					
						
							| 
									
										
										
										
											2020-06-11 22:11:43 +03:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void CharacterMap::set_character_map_data(CharacterMapData character_map_data) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     m_character_map_data = character_map_data; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 02:03:32 +03:00
										 |  |  |  | void CharacterMap::set_character_map_name(const String& character_map_name) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     m_character_map_name = character_map_name; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 21:12:47 +03:00
										 |  |  |  | const String& CharacterMap::character_map_name() const | 
					
						
							| 
									
										
										
										
											2020-08-06 02:03:32 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     return m_character_map_name; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-31 16:23:55 +03:00
										 |  |  |  | } |