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