Skip to content

Commit bf9f704

Browse files
committed
Make GUI + GPU detection
1 parent 9f7caca commit bf9f704

File tree

16 files changed

+543
-41
lines changed

16 files changed

+543
-41
lines changed

CM_Config/LibraryHandler.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
find_package(PkgConfig REQUIRED)
2+
find_package(Qt6 REQUIRED COMPONENTS Widgets Gui Core)
23

34
pkg_check_modules(LIBPCI REQUIRED libpci)
45
include_directories(${LIBPCI_INCLUDE_DIRS})

Include/RunAsGPU/Shared/GraphicalUnit.hpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,12 @@
33

44
#include <string>
55

6-
enum GraphicalUnitConnector {
7-
// The specific GPU is powered by the internal graphical processing unit (iGPU)
8-
// through the CPU
9-
INTERNAL = 0,
10-
11-
// The specific GPU is powered by its own dedicated graphical processing unit (dGPU)
12-
DEDICATED = 1,
13-
14-
// The specific GPU is connected via USB-C (most likely Thunderbolt)
15-
EXTERNAL = 2
16-
};
17-
186
struct GraphicalUnit {
197
int vendor;
208
int product;
219
std::string vendorName;
2210
std::string productName;
2311
std::string fullName;
24-
GraphicalUnitConnector connector;
2512
};
2613

2714
#endif //RUNASGPU_GRAPHICALUNIT_HPP

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RunAsGPU
2+
RunAsGPU is a tool designed to give users a nice way of controlling applications
3+
on what GPU an application runs on. I decided to write this tool to overcome the
4+
issue with the use of `DRI_PRIME`, which for some users may or may not include
5+
certain issues. This tool also helps users that aren't too technical with CLI,
6+
having a proper simple-to-use GUI. GUI should be simple to use with an App
7+
Selection screen (yes, you add apps manually, sorry not sorry), you select the
8+
app, and you run it. Simple GUI in mind, really.
9+
10+
## Use cases
11+
Documentation coming up soon. This project will actually appear sooner than my
12+
other projects, like for example, take
13+
[OsintgramCXX](https://github.com/BC100Dev/OsintgramCXX) that actually takes
14+
forever to finish.

Sources/AppCLI/MainCLI.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ void list() {
1414
std::cout << "GPU - " << unit.vendorName << " (" << std::hex << unit.vendor << ")\n";
1515
std::cout << "\tName = " << unit.productName << " (" << std::hex << unit.product << ")\n";
1616
std::cout << "\tID = " << i << "\n";
17-
std::cout << "\tConnector = ";
18-
19-
switch (unit.connector) {
20-
case EXTERNAL:
21-
std::cout << "External Graphics Card" << std::endl;
22-
break;
23-
case INTERNAL:
24-
std::cout << "Internal GPU" << std::endl;
25-
break;
26-
case DEDICATED:
27-
std::cout << "Dedicated GPU" << std::endl;
28-
break;
29-
}
3017

3118
if (i != gpu_list.size() - 1)
3219
std::cout << std::endl;

Sources/AppGUI/MainGUI.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <QApplication>
2+
#include <QMainWindow>
3+
#include "UI/MainWindow.hpp"
4+
5+
int main(int argc, char** argv, char** envp) {
6+
QApplication app(argc, argv);
7+
QMainWindow mainWin;
8+
Ui_MainWindow ui{};
9+
10+
ui.setupUi(&mainWin);
11+
mainWin.show();
12+
13+
return app.exec();
14+
}

Sources/AppGUI/RunAsGPU_Icon.png

1010 KB
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef RUNASGPU_UNITSELECTORDATA_HPP
2+
#define RUNASGPU_UNITSELECTORDATA_HPP
3+
4+
#include <RunAsGPU/Shared/GraphicalUnit.hpp>
5+
6+
namespace UnitSelectorData {
7+
8+
extern GraphicalUnit unitSelected;
9+
extern GraphicalUnit defaultUnit;
10+
11+
}
12+
13+
#endif //RUNASGPU_UNITSELECTORDATA_HPP

Sources/AppGUI/SourceInfo.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CollectSources(${PROJECT_MODULE_ROOT} ModSources)
2+
3+
include_directories("${PROJECT_MODULE_ROOT}/SharedData")
4+
5+
add_executable(RunAsGPU ${ModSources})
6+
7+
target_link_libraries(RunAsGPU PRIVATE RunAsGPU-shared Qt6::Widgets Qt6::Gui Qt6::Core)
8+
9+
qt_standard_project_setup()

Sources/AppGUI/UI/MainWindow.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "MainWindow.hpp"
2+
#include "UnitSelector.hpp"
3+
4+
#include <QMessageBox>
5+
#include <QStringListModel>
6+
7+
#include <RunAsGPU/Shared/GraphicalUnit.hpp>
8+
#include <RunAsGPU/Shared/Runner.hpp>
9+
10+
#include <filesystem>
11+
#include <fstream>
12+
#include <iostream>
13+
14+
namespace fs = std::filesystem;
15+
16+
static QStringListModel appListModel;
17+
static QStringList appEntries;
18+
19+
int get_default_gpu(const std::vector<GraphicalUnit> &gpu_list) {
20+
const char *home_dir = getenv("HOME");
21+
if (!home_dir)
22+
return -1;
23+
24+
std::string file_path = std::string(home_dir) + "/.config/RunAsGPU/gpu_identifier";
25+
if (!fs::exists(file_path))
26+
return 0;
27+
28+
std::string VID, DID;
29+
std::ifstream file(file_path);
30+
if (file.is_open()) {
31+
std::string line;
32+
33+
while (std::getline(file, line)) {
34+
if (line.find("GPU_VENDOR_ID=") != std::string::npos)
35+
VID = line.substr(line.find('=') + 1);
36+
if (line.find("GPU_DEVICE_ID=") != std::string::npos)
37+
DID = line.substr(line.find('=') + 1);
38+
}
39+
40+
file.close();
41+
42+
if (VID.empty() || DID.empty())
43+
return -1;
44+
}
45+
46+
for (int i = 0; i < gpu_list.size(); i++) {
47+
if (gpu_list[i].vendor == std::stoi(VID, nullptr, 16) &&
48+
gpu_list[i].product == std::stoi(DID, nullptr, 16))
49+
return i;
50+
}
51+
52+
return 0;
53+
}
54+
55+
void Ui_MainWindow::performLogic() const {
56+
std::vector<GraphicalUnit> gpu_list = Runner::ListGraphicalUnits();
57+
if (gpu_list.empty()) {
58+
std::cerr << "GPU list empty, exiting..." << std::endl;
59+
std::abort();
60+
return;
61+
}
62+
63+
int gpuUnit = get_default_gpu(gpu_list);
64+
65+
appListModel.setStringList(appEntries);
66+
appList->setModel(&appListModel);
67+
68+
// "Add Application" button logic
69+
QObject::connect(btnApplicationAdd, &QPushButton::clicked, [&]() {
70+
QString appEntry = "New Application Entry"; // Placeholder for actual selection logic
71+
appEntries.append(appEntry); // Add entry to QStringList
72+
appListModel.setStringList(appEntries); // Update QListView
73+
});
74+
75+
// "Run Application" button logic
76+
QObject::connect(btnRun, &QPushButton::clicked, [&]() {
77+
if (appEntries.isEmpty()) {
78+
QMessageBox::warning(nullptr, "Run Application", "No application selected.");
79+
} else {
80+
QMessageBox::information(nullptr, "Run Application", "Running: " + appEntries.first());
81+
// TODO: Implement actual GPU switching logic
82+
}
83+
});
84+
85+
// "Select GPU" button logic
86+
QObject::connect(btnUnitSelector, &QPushButton::clicked, [&]() {
87+
QDialog dialog;
88+
Ui_UnitSelector ui;
89+
ui.setupUi(&dialog);
90+
91+
std::vector<GraphicalUnit> gList = Runner::ListGraphicalUnits();
92+
ui.gpuList = gList;
93+
94+
int rc = dialog.exec();
95+
});
96+
}

Sources/AppGUI/UI/MainWindow.hpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifndef MAINWINDOW_HPP
2+
#define MAINWINDOW_HPP
3+
4+
#include <QtCore/QVariant>
5+
#include <QtGui/QIcon>
6+
#include <QtWidgets/QApplication>
7+
#include <QtWidgets/QLabel>
8+
#include <QtWidgets/QListView>
9+
#include <QtWidgets/QMainWindow>
10+
#include <QtWidgets/QPushButton>
11+
#include <QtWidgets/QStatusBar>
12+
#include <QtWidgets/QWidget>
13+
14+
QT_BEGIN_NAMESPACE
15+
16+
class Ui_MainWindow {
17+
public:
18+
QWidget *centralwidget;
19+
QPushButton *btnRun;
20+
QListView *appList;
21+
QPushButton *btnApplicationAdd;
22+
QPushButton *btnUnitSelector;
23+
QLabel *labelUnitSelected;
24+
QStatusBar *statusbar;
25+
26+
void setupUi(QMainWindow *MainWindow) {
27+
if (MainWindow->objectName().isEmpty())
28+
MainWindow->setObjectName("MainWindow");
29+
MainWindow->resize(586, 421);
30+
QSizePolicy sizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
31+
sizePolicy.setHorizontalStretch(0);
32+
sizePolicy.setVerticalStretch(0);
33+
sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
34+
MainWindow->setSizePolicy(sizePolicy);
35+
MainWindow->setMinimumSize(QSize(586, 421));
36+
MainWindow->setMaximumSize(QSize(586, 421));
37+
QIcon icon;
38+
icon.addFile(QString::fromUtf8("RunAsGPU_Icon.png"), QSize(), QIcon::Mode::Normal, QIcon::State::Off);
39+
MainWindow->setWindowIcon(icon);
40+
centralwidget = new QWidget(MainWindow);
41+
centralwidget->setObjectName("centralwidget");
42+
btnRun = new QPushButton(centralwidget);
43+
btnRun->setObjectName("btnRun");
44+
btnRun->setGeometry(QRect(467, 310, 111, 34));
45+
appList = new QListView(centralwidget);
46+
appList->setObjectName("appList");
47+
appList->setGeometry(QRect(0, 20, 581, 281));
48+
btnApplicationAdd = new QPushButton(centralwidget);
49+
btnApplicationAdd->setObjectName("btnApplicationAdd");
50+
btnApplicationAdd->setGeometry(QRect(0, 310, 121, 34));
51+
btnUnitSelector = new QPushButton(centralwidget);
52+
btnUnitSelector->setObjectName("btnUnitSelector");
53+
btnUnitSelector->setGeometry(QRect(130, 310, 88, 34));
54+
labelUnitSelected = new QLabel(centralwidget);
55+
labelUnitSelected->setObjectName("labelUnitSelected");
56+
labelUnitSelected->setGeometry(QRect(0, 350, 571, 31));
57+
MainWindow->setCentralWidget(centralwidget);
58+
statusbar = new QStatusBar(MainWindow);
59+
statusbar->setObjectName("statusbar");
60+
MainWindow->setStatusBar(statusbar);
61+
62+
retranslateUi(MainWindow);
63+
64+
QMetaObject::connectSlotsByName(MainWindow);
65+
66+
performLogic();
67+
} // setupUi
68+
69+
void retranslateUi(QMainWindow *MainWindow) const {
70+
MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "RunAsGPU", nullptr));
71+
btnRun->setText(QCoreApplication::translate("MainWindow", "Run Application", nullptr));
72+
btnApplicationAdd->setText(QCoreApplication::translate("MainWindow", "Add Application", nullptr));
73+
btnUnitSelector->setText(QCoreApplication::translate("MainWindow", "Select GPU", nullptr));
74+
labelUnitSelected->setText(QCoreApplication::translate("MainWindow", "Selected GPU: {{UNIT}}", nullptr));
75+
} // retranslateUi
76+
77+
void performLogic() const;
78+
79+
};
80+
81+
namespace Ui {
82+
class MainWindow : public Ui_MainWindow {
83+
};
84+
} // namespace Ui
85+
86+
QT_END_NAMESPACE
87+
88+
#endif // MAINWINDOW_HPP

0 commit comments

Comments
 (0)