~aleteoryx/muditaos

ref: 47f377d54056569c784af3b24b7a46fc37a025c9 muditaos/module-gui/gui/input/Profile.cpp -rw-r--r-- 1.7 KiB
47f377d5 — Lefucjusz Revert "[MOS-1064] Fix no input language selected for French/Spanish" 1 year, 9 months ago
                                                                                
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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>
#include "utf8/UTF8.hpp"
#include "Profile.hpp"
#include <Utils.hpp>
#include <gsl/gsl>

namespace gui
{

    Profile::Profile(const std::filesystem::path &filepath)
    {
        name       = filepath.stem();
        inputChars = createJson(filepath);
    }

    const std::string &Profile::getName() noexcept
    {
        return name;
    }

    const json11::Json &Profile::getJson() const noexcept
    {
        return inputChars;
    }

    const json11::Json Profile::createJson(const std::string &filepath)
    {
        auto fd = std::fopen(filepath.c_str(), "r");

        if (fd == nullptr) {
            LOG_FATAL("Error during opening file %s", filepath.c_str());
            return json11::Json();
        }

        uint32_t fsize = std::filesystem::file_size(filepath);

        auto stream = std::make_unique<char[]>(fsize + 1);

        memset(stream.get(), 0, fsize + 1);

        std::fread(stream.get(), 1, fsize, fd);

        std::string err;
        json11::Json parsedJson = json11::Json::parse(stream.get(), err);

        auto _ = gsl::finally([fd] { std::fclose(fd); });

        if (err.length() != 0) {
            LOG_FATAL("Parsing json error: %s", err.c_str());
            return json11::Json();
        }
        return parsedJson;
    }

    uint32_t Profile::getCharKey(bsp::KeyCodes code, uint32_t times)
    {
        std::string ts = inputChars[utils::to_string(static_cast<int>(code))].string_value();
        UTF8 utf       = UTF8(ts);
        if (ts.size() > 0) {
            return utf[times % utf.length()];
        }
        return utf[0];
    }

} /* namespace gui */