~aleteoryx/muditaos

ref: 9ef454085e4cca8a1f6c3c270cf460f899c9ee23 muditaos/module-gui/gui/input/Profile.cpp -rw-r--r-- 1.7 KiB
9ef45408 — Lefucjusz [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
// Copyright (c) 2017-2024, 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)
    {
        profileJson = createJson(filepath);
    }

    const std::string &Profile::getName() noexcept
    {
        return profileJson[nameKey].string_value();
    }

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

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

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

        const auto 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;
        const auto 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;
    }

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