Skip to content

Commit

Permalink
Add CLI Options
Browse files Browse the repository at this point in the history
- Only the CoolerBoost for now as a POC
- Modified some lines that static analyzer was complaining about
  • Loading branch information
guillaumeboehm committed Apr 16, 2024
1 parent 6971d52 commit ac3f86e
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(PROJECT_SOURCES
src/mainwindow.ui
src/mainwindow.cpp src/mainwindow.h
src/operate.cpp src/operate.h
src/cli.cpp src/cli.h
src/helper.cpp src/helper.h
src/settings.cpp src/settings.h
src/resources.qrc
Expand Down
50 changes: 50 additions & 0 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (C) 2022 Dmitry Serov
*
* This file is part of MControlCenter.
*
* MControlCenter is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* MControlCenter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MControlCenter. If not, see <https://www.gnu.org/licenses/>.
*/

#include "cli.h"
#include "operate.h"

static Operate operate;

CLI::CLI() {
if (!operate.isEcSysModuleLoaded() && !operate.loadEcSysModule()){
fprintf(stderr, "Failed to load the ec_sys kernel module\n");
exit(1);
}

if(!operate.updateEcData() || operate.getEcVersion().empty()){
fprintf(stderr, "Failed to update EC data\n");
exit(1);
}

if (!operate.doProbe()) {
fprintf(stderr, "Failed probing devices\n");
exit(1);
}
}

CLI::~CLI() {
}

void CLI::setCoolerBoost(bool on){
if(operate.getCoolerBoostState() != on){
fprintf(stdout, "%s Cooler Boost\n", ( on ? "Enabling" : "Disabling" ));
operate.setCoolerBoostState(on);
operate.updateEcData();
}
}
31 changes: 31 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (C) 2022 Dmitry Serov
*
* This file is part of MControlCenter.
*
* MControlCenter is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* MControlCenter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MControlCenter. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef CLI_H
#define CLI_H

class CLI {

public:
CLI();
~CLI();

void setCoolerBoost(bool on);
};

#endif // CLI_H
107 changes: 104 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,111 @@
*/

#include "mainwindow.h"
#include "cli.h"

#include <QApplication>
#include <QTranslator>
#include <QLocalSocket>
#include <QLocalServer>

#include <getopt.h>

struct Options{
bool cli = false;
std::optional<bool> cooler_boost = std::nullopt;
};

void PrintHelp(std::string program_name)
{
fprintf(stdout, R"(Description
Syntax: %s [options]
-B, --coolerboost TOOGLE toggle fan cooler boost
Arguments:
TOGGLE: can be 'ON' or 'OFF'
)", program_name.c_str());
exit(1);
}
const char* const short_opts = "B:h";
const option long_opts[] = {
{"coolerboost", required_argument, nullptr, 'B'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};

void ProcessArgs(int argc, char** argv, Options& options)
{
int option_index;
while (true)
{
const auto opt = getopt_long(argc, argv, short_opts, long_opts, &option_index);

if (-1 == opt){
break;
}

switch (opt)
{
case 0:
options.cli = true;
// long option without short equivalent
break;

case 'B':
options.cli = true;

if(std::strcmp(optarg,"ON") == 0){
options.cooler_boost = std::optional<bool>{true};
}
else if(strcmp(optarg,"OFF") == 0)
{
options.cooler_boost = std::optional<bool>{false};
}
else{
fprintf(stderr, "Wrong TOGGLE value for coolerboost option.\n");
PrintHelp(argv[0]);
}
break;

case 'h': // -h or --help
case '?': // Unrecognized option
default:
PrintHelp(argv[0]);
break;
}
}
}

int main(int argc, char *argv[]) {

Options options;
ProcessArgs(argc, argv, options);

const QString serverName = "MControlCenter";
auto *socket = new QLocalSocket();
QLocalSocket* socket = new QLocalSocket();
socket->connectToServer(serverName);

if(options.cli){
fprintf(stderr, "Executing CLI commands...\n");
CLI cli;
if(options.cooler_boost.has_value()){
cli.setCoolerBoost(options.cooler_boost.value());
}

if (socket->isOpen()) {
socket->write("update");
socket->flush();
socket->close();
}
socket->deleteLater();
return 0;
}

if (socket->isOpen()) {
fprintf(stderr, "Another instance of the application is already running\n");
socket->write("show");
socket->flush();
socket->close();
socket->deleteLater();
return 0;
Expand All @@ -51,8 +143,17 @@ int main(int argc, char *argv[]) {
MainWindow w;

QLocalServer server;
QObject::connect(&server, &QLocalServer::newConnection, [&w]() {
w.show();
QObject::connect(&server, &QLocalServer::newConnection, [&w, &server]() {
QLocalSocket* socket = server.nextPendingConnection();
if(socket->waitForConnected() && socket->waitForReadyRead()){
QByteArray data = socket->readAll();
if(std::strcmp(data.data(), "show") == 0){
w.show();
}
else if(std::strcmp(data.data(), "update") == 0){
w.externalUpdate();
}
}
});
bool serverListening = server.listen(serverName);
if (!serverListening && (server.serverError() == QAbstractSocket::AddressInUseError)) {
Expand Down
18 changes: 12 additions & 6 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <QTimer>
#include <QMessageBox>

Operate operate;
static Operate operate;

bool isActive = false;
bool isUpdateDataError = false;
Expand Down Expand Up @@ -199,6 +199,11 @@ void MainWindow::realtimeUpdate() {
updateData();
}

void MainWindow::externalUpdate() {
if(operate.updateEcData())
updateData();
}

void MainWindow::updateData() {
if (!isUpdateDataError && !operate.getEcVersion().empty()) {
if (!isActive) {
Expand All @@ -216,6 +221,7 @@ void MainWindow::updateData() {
updateFan2Speed();
updateKeyboardBrightness();
updateWebCamState();
updateCoolerBoostState();
} else {
setTabsEnabled(false);
isActive = false;
Expand Down Expand Up @@ -418,10 +424,10 @@ void MainWindow::updateFanMode() {
void MainWindow::updateFanSpeedSettings() {
ui->advancedFanControlCheckBox->setChecked(operate.getFanMode() == fan_mode::advanced_fan_mode);

QVector fan1SpeedSettings = operate.getFan1SpeedSettings();
QVector fan1TempSettings = operate.getFan1TempSettings();
QVector fan2SpeedSettings = operate.getFan2SpeedSettings();
QVector fan2TempSettings = operate.getFan2TempSettings();
QVector<int> fan1SpeedSettings = operate.getFan1SpeedSettings();
QVector<int> fan1TempSettings = operate.getFan1TempSettings();
QVector<int> fan2SpeedSettings = operate.getFan2SpeedSettings();
QVector<int> fan2TempSettings = operate.getFan2TempSettings();

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

void MainWindow::saveStateRequest(QSessionManager &sessionManager) {
sessionManager.setRestartHint(QSessionManager::RestartNever);
}
}
1 change: 1 addition & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void externalUpdate();
void updateData();
static void setUpdateDataError(bool error);

Expand Down

0 comments on commit ac3f86e

Please sign in to comment.