Skip to content

Commit 35a46d5

Browse files
committed
Initial commit
0 parents  commit 35a46d5

File tree

7 files changed

+244
-0
lines changed

7 files changed

+244
-0
lines changed

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app

.vscode/c_cpp_properties.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"/usr/include/gtk-2.0/**",
8+
"/usr/include/gtk-3.0/**"
9+
],
10+
"defines": [],
11+
"compilerPath": "/bin/g++",
12+
"cStandard": "c11",
13+
"cppStandard": "c++17",
14+
"intelliSenseMode": "clang-x64"
15+
}
16+
],
17+
"version": 4
18+
}

.vscode/launch.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(gdb) Launch",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/main.o",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"miDebuggerPath": "/usr/bin/gdb",
19+
"setupCommands": [
20+
{
21+
"description": "Enable pretty-printing for gdb",
22+
"text": "-enable-pretty-printing",
23+
"ignoreFailures": true
24+
}
25+
],
26+
"preLaunchTask": "make build"
27+
}
28+
]
29+
}

.vscode/settings.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"string_view": "cpp",
5+
"*.tcc": "cpp",
6+
"istream": "cpp"
7+
}
8+
}

.vscode/tasks.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "make build",
8+
"type": "shell",
9+
"command": "make build",
10+
"group": {
11+
"kind": "build",
12+
"isDefault": true
13+
}
14+
}
15+
]
16+
}

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.PHONY: clean build debug
2+
3+
build:
4+
# -g command allows debugging using command gdb main
5+
g++ -g main.cpp -o main.o `pkg-config --cflags --libs gtk+-3.0`
6+
# g++ -g main.cpp -o main.o
7+
8+
clean:
9+
rm *.o
10+
11+
debug:
12+
gdb main.o

main.cpp

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include </usr/include/gtk-3.0/gtk/gtk.h>
2+
#include <fstream>
3+
#include <glib.h>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <stdlib.h>
7+
#include <string>
8+
9+
using namespace std;
10+
#define FREQUENCY 5000
11+
/**
12+
* TODO: Fix this global. Need to create a struct that holds
13+
* all the labels and a pointer to the last label address.
14+
* I can pass that struct to update_text and the loop can go until
15+
* we reach that last address.
16+
* https://stackoverflow.com/questions/41687921/looping-through-array-using-pointers
17+
*/
18+
int processorCount = 0;
19+
20+
// TODO: Add thermal data from here /sys/class/thermal
21+
22+
int getProcessorCount() {
23+
ifstream infile("/proc/cpuinfo");
24+
int count = 0;
25+
26+
string line;
27+
while (getline(infile, line)) {
28+
istringstream iss(line);
29+
if (line.find("siblings") != string::npos) {
30+
count = (stof(
31+
line.substr(line.find(":") + 2, (line.length() - line.find(":")))));
32+
break;
33+
}
34+
}
35+
return count;
36+
}
37+
38+
float *getProcessorSpeeds() {
39+
ifstream infile("/proc/cpuinfo");
40+
41+
float *speeds = new float[processorCount];
42+
int count = 0;
43+
string line;
44+
while (getline(infile, line)) {
45+
istringstream iss(line);
46+
if (line.find("cpu MHz") != string::npos) {
47+
speeds[count] =
48+
(stof(line.substr(line.find(":") + 2,
49+
(line.find(".") - 2 - line.find(":")))) /
50+
1000);
51+
count++;
52+
}
53+
}
54+
55+
return speeds;
56+
}
57+
58+
static gboolean updateText(GtkWidget **labels) {
59+
float *processorSpeeds = getProcessorSpeeds();
60+
61+
for (int i = 0; i < processorCount; i++) {
62+
63+
gtk_label_set_text(
64+
GTK_LABEL(labels[i]),
65+
("cpu GHz : " + to_string(processorSpeeds[i])).c_str());
66+
}
67+
68+
return TRUE;
69+
}
70+
71+
static void infoModal(GtkWidget *wid, GtkWidget *win) {
72+
GtkWidget *dialog = NULL;
73+
74+
dialog = gtk_message_dialog_new(GTK_WINDOW(win), GTK_DIALOG_MODAL,
75+
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
76+
"Information Here!");
77+
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
78+
gtk_dialog_run(GTK_DIALOG(dialog));
79+
gtk_widget_destroy(dialog);
80+
}
81+
82+
int main(int argc, char *argv[]) {
83+
processorCount = getProcessorCount();
84+
GtkWidget *button = NULL;
85+
GtkWidget **labels;
86+
GtkWidget *win = NULL;
87+
GtkWidget *vbox = NULL;
88+
labels = g_new(GtkWidget *, processorCount);
89+
90+
/* Initialize GTK+ */
91+
g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc)gtk_false, NULL);
92+
gtk_init(&argc, &argv);
93+
g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
94+
95+
/* Create the main window */
96+
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
97+
gtk_container_set_border_width(GTK_CONTAINER(win), 80);
98+
gtk_window_set_title(GTK_WINDOW(win), "Monitor");
99+
gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
100+
gtk_widget_realize(win);
101+
g_signal_connect(win, "destroy", gtk_main_quit, NULL);
102+
103+
/* Create a vertical box with buttons */
104+
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
105+
gtk_container_add(GTK_CONTAINER(win), vbox);
106+
107+
for (int i = 0; i < processorCount; i++) {
108+
labels[i] = gtk_label_new("cpu GHz : -- ");
109+
gtk_box_pack_start(GTK_BOX(vbox), labels[i], TRUE, TRUE, 0);
110+
}
111+
112+
// TODO: Remove timeout when window is closed
113+
g_timeout_add(FREQUENCY, (GSourceFunc)updateText, labels);
114+
updateText(labels);
115+
116+
button = gtk_button_new_with_label("Info");
117+
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(infoModal),
118+
(gpointer)win);
119+
gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
120+
121+
button = gtk_button_new_with_label("Close");
122+
g_signal_connect(button, "clicked", gtk_main_quit, NULL);
123+
gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
124+
125+
/* Enter the main loop */
126+
gtk_widget_show_all(win);
127+
gtk_main();
128+
return 0;
129+
}

0 commit comments

Comments
 (0)