~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-gui/gui/input/Profile.cpp -rw-r--r-- 7.4 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * @file Profile.cpp
 * @author Robert Borzecki (robert.borzecki@mudita.com)
 * @date 24 cze 2019
 * @brief
 * @copyright Copyright (C) 2019 mudita.com
 * @details
 */
#include <string>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include "log/log.hpp"
#include "utf8/UTF8.hpp"
#include "vfs.hpp"
#include "Profile.hpp"

namespace gui
{

    const uint32_t KeyProfile::none_key = 0;

    KeyProfile::KeyProfile()
    {}
    KeyProfile::~KeyProfile()
    {}

    Profile::Profile(const std::string &name)
    {
        LOG_INFO("Create!");
        load(name);
    }

    Profile::Profile(Profile &&p)
    {
        this->name = p.name;
        this->keys = p.keys;
        // this is important, we need to assure that moved Profile doesn't clean up our memory again
        p.clear();
    }

    Profile::~Profile()
    {
        for (auto it = keys.begin(); it != keys.end(); it++) {
            delete it->second;
        }
        keys.clear();
    }

    static inline std::string trim(const std::string &s)
    {
        auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c); });
        return std::string(wsfront,
                           std::find_if_not(s.rbegin(), std::string::const_reverse_iterator(wsfront), [](int c) {
                               return std::isspace(c);
                           }).base());
    }

    template <typename Out> void split(const std::string &s, char delim, Out result)
    {
        std::stringstream ss(s);
        std::string item;
        while (std::getline(ss, item, delim)) {
            *(result++) = item;
        }
    }

    static std::vector<std::string> split(const std::string &s, char delim)
    {
        std::vector<std::string> elems;
        split(s, delim, std::back_inserter(elems));
        return elems;
    }

    bool Profile::load(std::string filename)
    {
        auto file = vfs.fopen(filename.c_str(), "rb");

        if (file == nullptr) {
            LOG_FATAL("no KeyProfile file: %s", filename.c_str());
            return false;
        }

        enum class LineType
        {
            KEY_CODE = 0,
            CYCLIC,
            CHARACTERS,
            TIMEOUTS
        };

        LineType lineType = LineType::KEY_CODE;
        bool name         = false;

        KeyProfile *pk = nullptr;
        KeyProfile tmp;
        while (vfs.eof(file) != true) {

            std::string line = trim(vfs.getline(file));
            if ((line[0] == '#') || (line.empty())) {
                continue;
            }
            else {

                // first not commented line is a name of profile
                if (name == false) {
                    LOG_INFO("profile name: %s", line.c_str());
                    setName(line);
                    name = true;
                }
                else {
                    // new structure, create profile key and read the keyboard's key code
                    if (lineType == LineType::KEY_CODE) {
                        uint32_t keyCode;
                        std::stringstream(line) >> keyCode;
                        pk          = new KeyProfile();
                        pk->keyCode = keyCode;
                        lineType    = LineType::CYCLIC;
                    }
                    else if (lineType == LineType::CYCLIC) {
                        pk->cyclic = (line.compare("true") == 0);
                        lineType   = LineType::CHARACTERS;
                    }
                    else if (lineType == LineType::CHARACTERS) {
                        addCharacters(pk, line);
                        lineType = LineType::TIMEOUTS;
                    }
                    else if (lineType == LineType::TIMEOUTS) {
                        addTimeouts(pk, line);
                        if ((pk->chars.size() == pk->timeouts.size()) && (keys.find(pk->keyCode) == keys.end())) {
                            addKeyProfile(pk);
                        }
                        else {
                            LOG_FATAL("Incorrect number of chars or key code duplicate key code: [%" PRIu32 "]",
                                      pk->keyCode);
                            delete pk;
                            pk = nullptr;
                        }

                        lineType = LineType::KEY_CODE;
                    }
                    else {
                        LOG_FATAL("invalid line: [%s]", line.c_str());
                    }
                }
            }
        }

        vfs.fclose(file);

        return true;
    }

    int is_utf8_character(unsigned char c)
    {
        if ((c >> 7) == 0b1) {
            if ((c >> 6) == 0b10) {
                return 2; // 2nd, 3rd or 4th byte of a utf-8 character
            }
            else {
                return 1; // 1st byte of a utf-8 character
            }
        }
        else {
            return 0; // a single byte character
        }
    }

    static std::vector<std::string> split_str(const std::string &s, int chars_number, int start_pos)
    {
        int pos = start_pos, char_pos, bytes_to_write;
        std::vector<std::string> elems;
        while (pos < (int)s.length()) {
            bytes_to_write = chars_number;
            char_pos       = pos + 1;
            if (is_utf8_character(s[char_pos]) > 0) {
                bytes_to_write = bytes_to_write + 1;
            }
            elems.push_back(s.substr(pos, bytes_to_write));
            pos = pos + bytes_to_write + 1;
        }
        return elems;
    }

    void Profile::addCharacters(KeyProfile *pk, const std::string &s)
    {

        uint32_t charKey;
        std::vector<std::string> vec = split_str(s, 3, 0);
        for (std::string s : vec) {
            std::string ts = trim(s);
            if (ts[0] == '\'') {
                // empty character - no character
                if (s.size() == 2) {
                    pk->chars.push_back(0);
                    break;
                }
                ts       = s.substr(1, s.size() - 1);
                UTF8 utf = UTF8(ts);
                charKey  = utf[0];
                pk->chars.push_back(charKey);
            }
            else if (ts.substr(0, 2) == "0x") {
                std::stringstream ss;
                ss << std::hex << ts;
                ss >> charKey;
                pk->chars.push_back(charKey);
            }
        }
    }
    void Profile::addTimeouts(KeyProfile *pk, const std::string &s)
    {
        uint32_t timeout;
        std::vector<std::string> vec = split(s, ',');
        for (std::string s : vec) {
            std::stringstream(trim(s)) >> timeout;
            pk->timeouts.push_back(timeout);
        }
    }

    void Profile::addKeyProfile(KeyProfile *pk)
    {
        if (pk != nullptr)
            keys.insert(std::pair<uint32_t, KeyProfile *>(pk->keyCode, pk));
    }

    const KeyProfile *Profile::getKeyProfile(uint32_t keyCode)
    {
        auto key = keys.find(keyCode);
        if (key != keys.end())
            return key->second;
        return nullptr;
    }

    uint32_t Profile::get(bsp::KeyCodes code, uint32_t times)
    {
        const KeyProfile *p = getKeyProfile(static_cast<uint32_t>(code));
        if (p == nullptr) {
            LOG_DEBUG("KeyProfile for key: %" PRIu32 " not found", static_cast<uint32_t>(code));
            return 0;
        }
        return p->chars[times % p->chars.size()];
    }

} /* namespace gui */