summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibKeyboard/CharacterMap.cpp
blob: 220304bebad51477ed908c0bf726ea681541b0a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "CharacterMap.h"
#include <AK/StringBuilder.h>
#include <LibKeyboard/CharacterMapFile.h>
#include <errno.h>
#include <serenity.h>

namespace Keyboard {

ErrorOr<CharacterMap> CharacterMap::load_from_file(DeprecatedString const& map_name)
{
    auto result = TRY(CharacterMapFile::load_from_file(map_name));

    return CharacterMap(map_name, result);
}

CharacterMap::CharacterMap(DeprecatedString const& map_name, CharacterMapData const& map_data)
    : m_character_map_data(map_data)
    , m_character_map_name(map_name)
{
}

int CharacterMap::set_system_map()
{
    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);
}

ErrorOr<CharacterMap> CharacterMap::fetch_system_map()
{
    CharacterMapData map_data;
    char keymap_name[50 + 1] = { 0 };

    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 Error::from_errno(errno);

    return CharacterMap { keymap_name, map_data };
}

DeprecatedString const& CharacterMap::character_map_name() const
{
    return m_character_map_name;
}
}