|
| 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