Repository: SerenityOS/serenity
File: Kernel/Devices/Input/Management.cpp (C++)

Code before:
                 ' ',      0,      0,
     //60                            70                                               80
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
    },

    .shift_altgr_map = {
        0,    '\033',    '1',    '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '0',    '-',    '=',    0x08,
                '\t',    'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',    'o',    'p',    '[',    ']',    '\n',
                   0,    'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',    'l',    ';',   '\'',    '`',       0,
                '\\',    'z',    'x',    'c',    'v',    'b',    'n',    'm',    ',',    '.',    '/',      0,    '*',       0,
                 ' ',      0,      0,
     //60                            70                                               80
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
    },
};
// clang-format on

void InputManagement::set_client(KeyboardClient* client)
{
    SpinlockLocker locker(m_client_lock);
    m_client = client;
}

size_t InputManagement::generate_minor_device_number_for_mouse()
{
    // FIXME: Lock this to prevent race conditions with hot-plugging devices!
    return m_mouse_minor_number++;
}
size_t InputManagement::generate_minor_device_number_for_keyboard()
{
    // FIXME: Lock this to prevent race conditions with hot-plugging devices!
    return m_keyboard_minor_number++;
}

UNMAP_AFTER_INIT InputManagement::KeymapData::KeymapData()
    : character_map_name(KString::must_create("en-us"sv))
    , character_map(DEFAULT_CHARACTER_MAP)
{
}

UNMAP_AFTER_INIT InputManagement::InputManagement()
{
}

void InputManagement::set_maps(NonnullOwnPtr<KString> character_map_name, Keyboard::CharacterMapData const& character_map_data)
{
    m_keymap_data.with([&](auto& keymap_data) {
        keymap_data.character_map_name = move(character_map_name);
        keymap_data.character_map = character_map_data;
        dbgln("New Character map '{}' passed in by client.", keymap_data.character_map_name);
    });

Review comment (suggestion):
nit: I think using `memory_order_relaxed` should be good enough (the default is seq_cst). We just need atomicity, but no ordering requirement with other memory accesses.

Code after:
                 ' ',      0,      0,
     //60                            70                                               80
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
    },

    .shift_altgr_map = {
        0,    '\033',    '1',    '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '0',    '-',    '=',    0x08,
                '\t',    'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',    'o',    'p',    '[',    ']',    '\n',
                   0,    'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',    'l',    ';',   '\'',    '`',       0,
                '\\',    'z',    'x',    'c',    'v',    'b',    'n',    'm',    ',',    '.',    '/',      0,    '*',       0,
                 ' ',      0,      0,
     //60                            70                                               80
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
    },
};
// clang-format on

void InputManagement::set_client(KeyboardClient* client)
{
    SpinlockLocker locker(m_client_lock);
    m_client = client;
}

size_t InputManagement::generate_minor_device_number_for_mouse()
{
    return m_mouse_minor_number.fetch_add(1);
}

size_t InputManagement::generate_minor_device_number_for_keyboard()
{
    return m_keyboard_minor_number.fetch_add(1);
}

UNMAP_AFTER_INIT InputManagement::KeymapData::KeymapData()
    : character_map_name(KString::must_create("en-us"sv))
    , character_map(DEFAULT_CHARACTER_MAP)
{
}

UNMAP_AFTER_INIT InputManagement::InputManagement()
{
}

void InputManagement::set_maps(NonnullOwnPtr<KString> character_map_name, Keyboard::CharacterMapData const& character_map_data)
{
    m_keymap_data.with([&](auto& keymap_data) {
        keymap_data.character_map_name = move(character_map_name);
        keymap_data.character_map = character_map_data;
        dbgln("New Character map '{}' passed in by client.", keymap_data.character_map_name);
    });
}