diff options
author | Ondrej Čerman | 2019-06-15 16:51:21 +0200 |
---|---|---|
committer | Ondrej Čerman | 2019-06-15 16:51:21 +0200 |
commit | 37fc68ca76571bbb1ccf8c21ea159e5273bcde4a (patch) | |
tree | 352b1cde0b3b331f10cd38538315df1197615019 /src/gui.c | |
parent | afdd058cfb2e06dee04a373ab98b380e4e3e5ad9 (diff) |
New version - code rewritten to c, added GTK3 gui, added Core and Package Power monitoring via MSR
Diffstat (limited to 'src/gui.c')
-rw-r--r-- | src/gui.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/gui.c b/src/gui.c new file mode 100644 index 0000000..c59b19d --- /dev/null +++ b/src/gui.c @@ -0,0 +1,164 @@ +#include <cpuid.h> +#include <gtk/gtk.h> +#include "gui.h" +#include "zenmonitor.h" + +static GtkTreeModel *model = NULL; +static guint timeout = 0; +static SensorSource *sensor_sources; + +enum { + COLUMN_NAME, + COLUMN_VALUE, + NUM_COLUMNS +}; + +static void init_sensors() { + GtkTreeIter iter; + GSList *sensor; + GtkListStore *store; + SensorSource *source; + const SensorInit *data; + gboolean added; + guint i = 0; + + store = GTK_LIST_STORE(model); + for (source = sensor_sources; source->drv; source++) { + if (source->func_init()){ + source->sensors = source->func_get_sensors(); + if (source->sensors != NULL) { + source->enabled = TRUE; + + sensor = source->sensors; + while (sensor) { + data = (SensorInit*)sensor->data; + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, + COLUMN_NAME, data->label, + COLUMN_VALUE, " --- ", + -1); + sensor = sensor->next; + i++; + } + } + } + } +} + +static GtkTreeModel* create_model (void) { + GtkListStore *store; + store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING); + return GTK_TREE_MODEL (store); +} + +static gboolean update_data (gpointer data) { + GtkTreeIter iter; + guint number; + GSList *node; + gchar *value; + SensorSource *source; + const SensorInit *sensorData; + + if (model == NULL) + return G_SOURCE_REMOVE; + + if (!gtk_tree_model_get_iter_first (model, &iter)) + return G_SOURCE_REMOVE; + + for (source = sensor_sources; source->drv; source++) { + if (!source->enabled) + continue; + + source->func_update(); + if (source->sensors){ + node = source->sensors; + + while(node) { + sensorData = (SensorInit*)node->data; + if (*(sensorData->value) != ERROR_VALUE) + value = g_strdup_printf(sensorData->printf_format, *(sensorData->value)); + else + value = g_strdup(" ? ? ?"); + + gtk_list_store_set(GTK_LIST_STORE (model), &iter, COLUMN_VALUE, value, -1); + node = node->next; + if (!gtk_tree_model_iter_next(model, &iter)) + break; + } + } + } + return G_SOURCE_CONTINUE; +} + +static void add_columns (GtkTreeView *treeview) { + GtkCellRenderer *renderer; + GtkTreeViewColumn *column; + GtkTreeModel *model = gtk_tree_view_get_model (treeview); + + // NAME + renderer = gtk_cell_renderer_text_new (); + + column = gtk_tree_view_column_new_with_attributes ("Sensor", renderer, + "text", COLUMN_NAME, + NULL); + g_object_set(renderer, "family", "monotype", NULL); + gtk_tree_view_append_column (treeview, column); + + //VALUE + renderer = gtk_cell_renderer_text_new (); + column = gtk_tree_view_column_new_with_attributes ("Value", renderer, + "text", COLUMN_VALUE, + NULL); + g_object_set(renderer, "family", "monotype", NULL); + gtk_tree_view_append_column (treeview, column); +} + + +int start_gui (SensorSource *ss) { + GtkWidget *window; + GtkWidget *treeview; + GtkWidget *sw; + GtkWidget *vbox; + GtkWidget *dialog; + + window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_title(GTK_WINDOW(window), "Zen monitor"); + gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); + gtk_window_set_default_size(GTK_WINDOW(window), 330, 300); + + g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + + vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); + gtk_container_add(GTK_CONTAINER (window), vbox); + + sw = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); + gtk_box_pack_start(GTK_BOX (vbox), sw, TRUE, TRUE, 0); + + model = create_model(); + treeview = gtk_tree_view_new_with_model(model); + g_object_unref(model); + + gtk_container_add (GTK_CONTAINER(sw), treeview); + add_columns(GTK_TREE_VIEW(treeview)); + gtk_widget_show_all(window); + + if (check_zen()){ + sensor_sources = ss; + init_sensors(); + timeout = g_timeout_add(300, update_data, NULL); + } + else{ + dialog = gtk_message_dialog_new(GTK_WINDOW (window), + GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, + "Zen CPU not detected!"); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + } + + gtk_main(); + return 0; +} + |