Skip to content

Commit ac3f86e

Browse files
Add CLI Options
- Only the CoolerBoost for now as a POC - Modified some lines that static analyzer was complaining about
1 parent 6971d52 commit ac3f86e

6 files changed

Lines changed: 199 additions & 9 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(PROJECT_SOURCES
1919
src/mainwindow.ui
2020
src/mainwindow.cpp src/mainwindow.h
2121
src/operate.cpp src/operate.h
22+
src/cli.cpp src/cli.h
2223
src/helper.cpp src/helper.h
2324
src/settings.cpp src/settings.h
2425
src/resources.qrc

src/cli.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright (C) 2022 Dmitry Serov
2+
*
3+
* This file is part of MControlCenter.
4+
*
5+
* MControlCenter is free software: you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License as
7+
* published by the Free Software Foundation, either version 3 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* MControlCenter is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with MControlCenter. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "cli.h"
20+
#include "operate.h"
21+
22+
static Operate operate;
23+
24+
CLI::CLI() {
25+
if (!operate.isEcSysModuleLoaded() && !operate.loadEcSysModule()){
26+
fprintf(stderr, "Failed to load the ec_sys kernel module\n");
27+
exit(1);
28+
}
29+
30+
if(!operate.updateEcData() || operate.getEcVersion().empty()){
31+
fprintf(stderr, "Failed to update EC data\n");
32+
exit(1);
33+
}
34+
35+
if (!operate.doProbe()) {
36+
fprintf(stderr, "Failed probing devices\n");
37+
exit(1);
38+
}
39+
}
40+
41+
CLI::~CLI() {
42+
}
43+
44+
void CLI::setCoolerBoost(bool on){
45+
if(operate.getCoolerBoostState() != on){
46+
fprintf(stdout, "%s Cooler Boost\n", ( on ? "Enabling" : "Disabling" ));
47+
operate.setCoolerBoostState(on);
48+
operate.updateEcData();
49+
}
50+
}

src/cli.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (C) 2022 Dmitry Serov
2+
*
3+
* This file is part of MControlCenter.
4+
*
5+
* MControlCenter is free software: you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License as
7+
* published by the Free Software Foundation, either version 3 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* MControlCenter is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with MControlCenter. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef CLI_H
20+
#define CLI_H
21+
22+
class CLI {
23+
24+
public:
25+
CLI();
26+
~CLI();
27+
28+
void setCoolerBoost(bool on);
29+
};
30+
31+
#endif // CLI_H

src/main.cpp

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,111 @@
1717
*/
1818

1919
#include "mainwindow.h"
20+
#include "cli.h"
2021

2122
#include <QApplication>
2223
#include <QTranslator>
2324
#include <QLocalSocket>
2425
#include <QLocalServer>
2526

27+
#include <getopt.h>
28+
29+
struct Options{
30+
bool cli = false;
31+
std::optional<bool> cooler_boost = std::nullopt;
32+
};
33+
34+
void PrintHelp(std::string program_name)
35+
{
36+
fprintf(stdout, R"(Description
37+
Syntax: %s [options]
38+
39+
-B, --coolerboost TOOGLE toggle fan cooler boost
40+
41+
Arguments:
42+
TOGGLE: can be 'ON' or 'OFF'
43+
)", program_name.c_str());
44+
exit(1);
45+
}
46+
const char* const short_opts = "B:h";
47+
const option long_opts[] = {
48+
{"coolerboost", required_argument, nullptr, 'B'},
49+
{"help", no_argument, nullptr, 'h'},
50+
{nullptr, no_argument, nullptr, 0}
51+
};
52+
53+
void ProcessArgs(int argc, char** argv, Options& options)
54+
{
55+
int option_index;
56+
while (true)
57+
{
58+
const auto opt = getopt_long(argc, argv, short_opts, long_opts, &option_index);
59+
60+
if (-1 == opt){
61+
break;
62+
}
63+
64+
switch (opt)
65+
{
66+
case 0:
67+
options.cli = true;
68+
// long option without short equivalent
69+
break;
70+
71+
case 'B':
72+
options.cli = true;
73+
74+
if(std::strcmp(optarg,"ON") == 0){
75+
options.cooler_boost = std::optional<bool>{true};
76+
}
77+
else if(strcmp(optarg,"OFF") == 0)
78+
{
79+
options.cooler_boost = std::optional<bool>{false};
80+
}
81+
else{
82+
fprintf(stderr, "Wrong TOGGLE value for coolerboost option.\n");
83+
PrintHelp(argv[0]);
84+
}
85+
break;
86+
87+
case 'h': // -h or --help
88+
case '?': // Unrecognized option
89+
default:
90+
PrintHelp(argv[0]);
91+
break;
92+
}
93+
}
94+
}
2695

2796
int main(int argc, char *argv[]) {
97+
98+
Options options;
99+
ProcessArgs(argc, argv, options);
100+
28101
const QString serverName = "MControlCenter";
29-
auto *socket = new QLocalSocket();
102+
QLocalSocket* socket = new QLocalSocket();
30103
socket->connectToServer(serverName);
104+
105+
if(options.cli){
106+
fprintf(stderr, "Executing CLI commands...\n");
107+
CLI cli;
108+
if(options.cooler_boost.has_value()){
109+
cli.setCoolerBoost(options.cooler_boost.value());
110+
}
111+
112+
if (socket->isOpen()) {
113+
socket->write("update");
114+
socket->flush();
115+
socket->close();
116+
}
117+
socket->deleteLater();
118+
return 0;
119+
}
120+
31121
if (socket->isOpen()) {
32122
fprintf(stderr, "Another instance of the application is already running\n");
123+
socket->write("show");
124+
socket->flush();
33125
socket->close();
34126
socket->deleteLater();
35127
return 0;
@@ -51,8 +143,17 @@ int main(int argc, char *argv[]) {
51143
MainWindow w;
52144

53145
QLocalServer server;
54-
QObject::connect(&server, &QLocalServer::newConnection, [&w]() {
55-
w.show();
146+
QObject::connect(&server, &QLocalServer::newConnection, [&w, &server]() {
147+
QLocalSocket* socket = server.nextPendingConnection();
148+
if(socket->waitForConnected() && socket->waitForReadyRead()){
149+
QByteArray data = socket->readAll();
150+
if(std::strcmp(data.data(), "show") == 0){
151+
w.show();
152+
}
153+
else if(std::strcmp(data.data(), "update") == 0){
154+
w.externalUpdate();
155+
}
156+
}
56157
});
57158
bool serverListening = server.listen(serverName);
58159
if (!serverListening && (server.serverError() == QAbstractSocket::AddressInUseError)) {

src/mainwindow.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <QTimer>
2424
#include <QMessageBox>
2525

26-
Operate operate;
26+
static Operate operate;
2727

2828
bool isActive = false;
2929
bool isUpdateDataError = false;
@@ -199,6 +199,11 @@ void MainWindow::realtimeUpdate() {
199199
updateData();
200200
}
201201

202+
void MainWindow::externalUpdate() {
203+
if(operate.updateEcData())
204+
updateData();
205+
}
206+
202207
void MainWindow::updateData() {
203208
if (!isUpdateDataError && !operate.getEcVersion().empty()) {
204209
if (!isActive) {
@@ -216,6 +221,7 @@ void MainWindow::updateData() {
216221
updateFan2Speed();
217222
updateKeyboardBrightness();
218223
updateWebCamState();
224+
updateCoolerBoostState();
219225
} else {
220226
setTabsEnabled(false);
221227
isActive = false;
@@ -418,10 +424,10 @@ void MainWindow::updateFanMode() {
418424
void MainWindow::updateFanSpeedSettings() {
419425
ui->advancedFanControlCheckBox->setChecked(operate.getFanMode() == fan_mode::advanced_fan_mode);
420426

421-
QVector fan1SpeedSettings = operate.getFan1SpeedSettings();
422-
QVector fan1TempSettings = operate.getFan1TempSettings();
423-
QVector fan2SpeedSettings = operate.getFan2SpeedSettings();
424-
QVector fan2TempSettings = operate.getFan2TempSettings();
427+
QVector<int> fan1SpeedSettings = operate.getFan1SpeedSettings();
428+
QVector<int> fan1TempSettings = operate.getFan1TempSettings();
429+
QVector<int> fan2SpeedSettings = operate.getFan2SpeedSettings();
430+
QVector<int> fan2TempSettings = operate.getFan2TempSettings();
425431

426432
ui->fan1Speed1Slider->setValue(fan1SpeedSettings[0]);
427433
ui->fan1Speed2Slider->setValue(fan1SpeedSettings[1]);
@@ -772,4 +778,4 @@ void MainWindow::createActions() {
772778

773779
void MainWindow::saveStateRequest(QSessionManager &sessionManager) {
774780
sessionManager.setRestartHint(QSessionManager::RestartNever);
775-
}
781+
}

src/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Q_OBJECT
3535
public:
3636
explicit MainWindow(QWidget *parent = nullptr);
3737
~MainWindow();
38+
void externalUpdate();
3839
void updateData();
3940
static void setUpdateDataError(bool error);
4041

0 commit comments

Comments
 (0)