summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/KeymapSwitcher.cpp
blob: f329c15bfa2cbfdaf9f7180c1f340d312e135446 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2021, Timur Sultanov <SultanovTS@yandex.ru>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/JsonObject.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <WindowServer/KeymapSwitcher.h>
#include <spawn.h>
#include <unistd.h>

namespace WindowServer {

KeymapSwitcher::KeymapSwitcher()
{
    m_file_watcher = MUST(Core::FileWatcher::create());

    m_file_watcher->on_change = [this](auto&) {
        refresh();
    };

    MUST(m_file_watcher->add_watch(m_keyboard_config, Core::FileWatcherEvent::Type::ContentModified));

    refresh();
}

void KeymapSwitcher::refresh()
{
    m_keymaps.clear();

    auto mapper_config(Core::ConfigFile::open(m_keyboard_config).release_value_but_fixme_should_propagate_errors());
    auto keymaps = mapper_config->read_entry("Mapping", "Keymaps", "");

    auto keymaps_vector = keymaps.split(',');

    for (auto& keymap : keymaps_vector) {
        m_keymaps.append(keymap);
    }

    if (m_keymaps.is_empty()) {
        dbgln("Empty list of keymaps - adding default (en-us)");
        m_keymaps.append("en-us");
    }

    auto current_keymap = get_current_keymap();

    // Refresh might indicate that some external program has changed the keymap,
    // so better notify our clients that we may have a new keymap
    if (on_keymap_change)
        on_keymap_change(current_keymap);

    if (m_keymaps.find(current_keymap).is_end()) {
        setkeymap(m_keymaps.first());
    }
}

void KeymapSwitcher::next_keymap()
{
    if (m_keymaps.is_empty()) {
        dbgln("No keymaps loaded - leaving system keymap unchanged");
        return; // TODO: figure out what to do when there is no keymap configured
    }

    auto current_keymap_name = get_current_keymap();

    dbgln("Current system keymap: {}", current_keymap_name);

    auto it = m_keymaps.find_if([&](auto const& enumerator) {
        return enumerator == current_keymap_name;
    });

    if (it.is_end()) {
        auto first_keymap = m_keymaps.first();
        dbgln("Cannot find current keymap in the keymap list - setting first available ({})", first_keymap);
        setkeymap(first_keymap);
    } else {
        it++;

        if (it.is_end()) {
            it = m_keymaps.begin();
        }

        dbgln("Setting system keymap to: {}", *it);
        setkeymap(*it);
    }
}

String KeymapSwitcher::get_current_keymap() const
{
    auto proc_keymap = Core::File::construct("/proc/keymap");
    if (!proc_keymap->open(Core::OpenMode::ReadOnly))
        VERIFY_NOT_REACHED();

    auto json = JsonValue::from_string(proc_keymap->read_all()).release_value_but_fixme_should_propagate_errors();
    auto const& keymap_object = json.as_object();
    VERIFY(keymap_object.has("keymap"));
    return keymap_object.get("keymap").to_string();
}

void KeymapSwitcher::setkeymap(const AK::String& keymap)
{
    pid_t child_pid;
    char const* argv[] = { "/bin/keymap", "-m", keymap.characters(), nullptr };
    if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
        perror("posix_spawn");
        dbgln("Failed to call /bin/keymap, error: {} ({})", errno, strerror(errno));
    }
    if (on_keymap_change)
        on_keymap_change(keymap);
}

}