~aleteoryx/muditaos

ref: 321f56e7744bc9f2cb487f1612cf77eb1ca6e3c7 muditaos/module-apps/application-settings/windows/advanced/CPUModeTestWindow.cpp -rw-r--r-- 9.3 KiB
321f56e7 — Lefucjusz [MOS-1032] Fix misleading 'Save' button behavior when setting time 2 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CPUModeTestWindow.hpp"

#include <system/messages/SentinelRegistrationMessage.hpp>
#include <system/messages/RequestCpuFrequencyMessage.hpp>
#include <application-settings/windows/WindowNames.hpp>
#include <Application.hpp>
#include <TextFixedSize.hpp>
#include <BoxLayout.hpp>

#include <magic_enum.hpp>

namespace magic_enum
{
    template <>
    struct customize::enum_range<bsp::CpuFrequencyMHz>
    {
        static constexpr int min = 0;
        static constexpr int max = 528;
    };
} // namespace magic_enum

namespace gui
{
    CPUModeTestWindow::CPUModeTestWindow(app::ApplicationCommon *app) : AppWindow(app, window::name::cpu_test_window)
    {
        cpuModeTester = std::make_shared<sys::CpuSentinel>(name, app, [&](bsp::CpuFrequencyMHz freq) {
            auto freqToPrint = std::to_string(magic_enum::enum_integer(freq));
            currentFreqValue->setMinimumWidthToFitText(freqToPrint);
            currentFreqValue->setMaximumWidth(currentFreqValue->widgetMinimumArea.w);
            currentFreqValue->informContentChanged();
            currentFreqValue->setText(freqToPrint);

            application->refreshWindow(RefreshModes::GUI_REFRESH_FAST);
        });

        auto sentinelRegistrationMsg = std::make_shared<sys::SentinelRegistrationMessage>(cpuModeTester);
        application->bus.sendUnicastSync(sentinelRegistrationMsg, service::name::system_manager, 30);

        AppWindow::buildInterface();

        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

        setTitle(name);

        body = new VBox(this,
                        style::window::default_left_margin,
                        style::window::default_vertical_pos,
                        style::window::default_body_width,
                        style::window::default_body_height);

        body->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Top));
        body->setEdges(RectangleEdge::None);

        createCurrentFreqBox();
        createPermanentFreqSettingBox();
        createNewFreqBox();

        body->resizeItems();
        setFocusItem(body);
    }

    void CPUModeTestWindow::onClose(Window::CloseReason reason)
    {
        if (reason != Window::CloseReason::Popup && reason != Window::CloseReason::PhoneLock) {
            auto sentinelRemovalMessage = std::make_shared<sys::SentinelRemovalMessage>(name);
            application->bus.sendUnicastSync(sentinelRemovalMessage, service::name::system_manager, 30);
        }
    }

    void CPUModeTestWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        auto freqToPrint = std::to_string(magic_enum::enum_integer(cpuModeTester->GetFrequency()));

        currentFreqValue->setMinimumWidthToFitText(freqToPrint);
        currentFreqValue->setMinimumHeightToFitText();
        currentFreqValue->setMaximumWidth(currentFreqValue->widgetMinimumArea.w);
        currentFreqValue->informContentChanged();
        currentFreqValue->setText(freqToPrint);

        currentFreqSpinner->setCurrentValue(freqToPrint);
    }

    void CPUModeTestWindow::createCurrentFreqBox()
    {
        auto currentFreqBody = new VBox(body);
        currentFreqBody->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        currentFreqBody->setEdges(RectangleEdge::None);
        currentFreqBody->setMinimumSize(style::window::default_body_width, 80);
        currentFreqBody->setMargins({0, 60, 0, 30});
        currentFreqBody->activeItem = false;

        auto currentFreqLabel = new TextFixedSize(currentFreqBody);
        currentFreqLabel->setAlignment({Alignment::Horizontal::Center});
        currentFreqLabel->setFont(style::window::font::small);
        currentFreqLabel->setMinimumWidth(style::window::default_body_width);
        currentFreqLabel->setMinimumHeightToFitText();
        currentFreqLabel->setMargins({0, 0, 0, 15});
        currentFreqLabel->setText("Current CPU Frequency [MHz]:");
        currentFreqLabel->setEdges(RectangleEdge::None);
        currentFreqLabel->drawUnderline(false);

        currentFreqValue = new TextFixedSize(currentFreqBody);
        currentFreqValue->setAlignment({Alignment::Horizontal::Center});
        currentFreqValue->setFont(style::window::font::mediumbold);
        currentFreqValue->setPenWidth(style::window::default_border_focus_w);
        currentFreqValue->setEdges(RectangleEdge::Bottom);
        currentFreqValue->drawUnderline(false);
        currentFreqValue->setEditMode(EditMode::Browse);
        currentFreqValue->activeItem = false;
    }

    void CPUModeTestWindow::createPermanentFreqSettingBox()
    {
        auto permanentFreqBody = new VBox(body);
        permanentFreqBody->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        permanentFreqBody->setEdges(RectangleEdge::None);
        permanentFreqBody->setMinimumSize(style::window::default_body_width, 80);
        permanentFreqBody->setMargins({0, 0, 0, 30});

        auto permanentFreqLabel = new TextFixedSize(permanentFreqBody);
        permanentFreqLabel->setAlignment({Alignment::Horizontal::Center});
        permanentFreqLabel->setFont(style::window::font::small);
        permanentFreqLabel->setMinimumWidth(style::window::default_body_width);
        permanentFreqLabel->setMinimumHeightToFitText();
        permanentFreqLabel->setMargins({0, 0, 0, 15});
        permanentFreqLabel->setText("Set Permanent Frequency:");
        permanentFreqLabel->setEdges(RectangleEdge::None);
        permanentFreqLabel->drawUnderline(false);
        permanentFreqLabel->activeItem = false;

        permanentFreqSpinner = new gui::TextSpinnerBox(permanentFreqBody, {"OFF", "ON"}, Boundaries::Continuous);
        permanentFreqSpinner->setMinimumSize(100, 30);
        auto ret =
            application->async_call<sys::IsCpuPernament, sys::IsCpuPernamentResponse>(service::name::system_manager);
        application->sync(ret);
        permanentFreqSpinner->setCurrentValue(ret.getResult().pernament ? "ON" : "OFF");

        permanentFreqBody->inputCallback = [&](Item &item, const InputEvent &event) {
            auto ret = permanentFreqSpinner->onInput(event);
            if (ret) {
                if (permanentFreqSpinner->getCurrentValue() == "ON") {
                    application->bus.sendUnicastSync(std::make_shared<sys::HoldCpuFrequencyPermanentlyMessage>(
                                                         magic_enum::enum_cast<bsp::CpuFrequencyMHz>(
                                                             std::stoi(currentFreqSpinner->getCurrentValue()))
                                                             .value()),
                                                     service::name::system_manager,
                                                     5000);
                }
                else {
                    application->bus.sendUnicastSync(std::make_shared<sys::ReleaseCpuPermanentFrequencyMessage>(),
                                                     service::name::system_manager,
                                                     5000);
                }
            }

            return ret;
        };
    }

    void CPUModeTestWindow::createNewFreqBox()
    {
        auto newFreqBody = new VBox(body);
        newFreqBody->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        newFreqBody->setEdges(RectangleEdge::None);
        newFreqBody->setMinimumSize(style::window::default_body_width, 80);

        auto newFreqLabel = new TextFixedSize(newFreqBody);
        newFreqLabel->setAlignment({Alignment::Horizontal::Center});
        newFreqLabel->setFont(style::window::font::small);
        newFreqLabel->setMinimumWidth(style::window::default_body_width);
        newFreqLabel->setMinimumHeightToFitText();
        newFreqLabel->setMargins({0, 0, 0, 15});
        newFreqLabel->setText("New Frequency to set [MHz]:");
        newFreqLabel->setEdges(RectangleEdge::None);
        newFreqLabel->drawUnderline(false);
        newFreqLabel->activeItem = false;

        std::vector<UTF8> freqOptions;
        for (auto val : magic_enum::enum_values<bsp::CpuFrequencyMHz>()) {
            if (val <= bsp::getPowerProfile().minimalFrequency) {
                continue;
            }

            freqOptions.push_back(std::to_string(static_cast<int>(val)));
        }

        currentFreqSpinner = new gui::TextSpinnerBox(newFreqBody, freqOptions, Boundaries::Continuous);
        currentFreqSpinner->setMinimumSize(100, 30);

        newFreqBody->inputCallback = [&](Item &item, const InputEvent &event) {
            auto ret = currentFreqSpinner->onInput(event);

            auto async = application->async_call<sys::IsCpuPernament, sys::IsCpuPernamentResponse>(
                service::name::system_manager);
            application->sync(async);
            if (async.getResult().pernament) {
                application->bus.sendUnicastSync(
                    std::make_shared<sys::HoldCpuFrequencyPermanentlyMessage>(
                        magic_enum::enum_cast<bsp::CpuFrequencyMHz>(std::stoi(currentFreqSpinner->getCurrentValue()))
                            .value()),
                    service::name::system_manager,
                    5000);
            }

            return ret;
        };
    }
} /* namespace gui */