Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Feature #162

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@
*.exe
*.out
*.app

# Generated files
src/i18n/*

# Generated build files
compile_commands.json
build/
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ 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/msi-ec_helper.cpp src/msi-ec_helper.h
src/settings.cpp src/settings.h
src/options.cpp src/options.h
src/resources.qrc
)

Expand Down
58 changes: 58 additions & 0 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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(Options::State state){
bool on = false;
if(state == Options::State::TOGGLE){ // TOGGLE
on = !operate.getCoolerBoostState();
}
else{
on = state;
}

if(operate.getCoolerBoostState() != on){
fprintf(stdout, "%s Cooler Boost\n", ( on ? "Enabling" : "Disabling" ));
operate.setCoolerBoostState(on);
operate.updateEcData();
}
}
33 changes: 33 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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

#include "options.h"

class CLI {

public:
CLI();
~CLI();

void setCoolerBoost(Options::State on);
};

#endif // CLI_H
40 changes: 37 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

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

#include <QApplication>
#include <QTranslator>
Expand All @@ -25,11 +27,34 @@


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

Options options;
options.process_args(argc, argv);

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 +76,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 @@ -204,6 +204,11 @@ void MainWindow::realtimeUpdate() {
updateData();
}

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

void MainWindow::updateData() {
if (!isUpdateDataError && !operate.getEcVersion().empty()) {
if (!isActive) {
Expand All @@ -221,6 +226,7 @@ void MainWindow::updateData() {
updateFan2Speed();
updateKeyboardBrightness();
updateWebCamState();
updateCoolerBoostState();
} else {
setTabsEnabled(false);
isActive = false;
Expand Down Expand Up @@ -427,10 +433,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 @@ -801,4 +807,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
85 changes: 85 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* 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 "options.h"

#include <cstring>

Options::Options()
:cli(false), cooler_boost(std::nullopt)
{}

void Options::print_help(std::string program_name)
{
fprintf(stdout, R"(Description
Syntax: %s [options]

-B, --coolerboost STATE toggle fan cooler boost
-h show help

Arguments:
STATE: can be 'ON', 'OFF' or 'TOGGLE'
)", program_name.c_str());
exit(1);
}

void Options::process_args(int argc, char** argv)
{
int option_index;
while (true)
{
const auto opt = getopt_long(argc, argv, short_opts.data(), long_opts, &option_index);

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

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

case 'B':
cli = true;

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

case 'h': // -h or --help
case '?': // Unrecognized option
default:
print_help(argv[0]);
break;
}
}
}
53 changes: 53 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* 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 OPTIONS_H
#define OPTIONS_H

#include <getopt.h>
#include <optional>
#include <string>

class Options{
public:
Options();
~Options() = default;

enum State {
OFF = 0,
ON = 1,
TOGGLE = 2
};

bool cli;
std::optional<State> cooler_boost;

void process_args(int argc, char** argv);

private:
static constexpr std::string_view const short_opts = "B:h";
static constexpr option long_opts[] = {
{"coolerboost", required_argument, nullptr, 'B'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};

void print_help(std::string program_name);
};

#endif // OPTIONS_H