diff --git a/Gtk4/gtk161_core_voltage/CMakeLists.txt b/Gtk4/gtk161_core_voltage/CMakeLists.txt new file mode 100644 index 0000000..150a67e --- /dev/null +++ b/Gtk4/gtk161_core_voltage/CMakeLists.txt @@ -0,0 +1,75 @@ +set(CMAKE_CXX_STANDARD 17) +cmake_minimum_required(VERSION 3.5.0) +project(gtk161_core_voltage VERSION 1.0.0) + +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../GCR_CMake/macros) +include(GlibCompileResourcesSupport) + +include(CTest) +enable_testing() + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) + +include(CPack) +include_directories(.) +include_directories(..) + +#Find PkgConfig to use gtkmm3 +find_package (PkgConfig REQUIRED) +pkg_check_modules (GTK REQUIRED gtk4) +include_directories (${GTK_INCLUDE_DIRS}) +link_directories (${GTK_LIBRARY_DIRS}) + +#Find Gettext +# find_package (Gettext REQUIRED) +# set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES) + +#Source files +set(SOURCE_FILE src/main.cpp src/MainWin.cpp src/CoreVolt.cpp) + +#Compile Resource + +# set(RESOURCE_LIST +# title_style.css +# icons/scalable/status/win_close.svg +# icons/scalable/status/win_maximize.svg +# icons/scalable/status/win_minimize.svg) + +# compile_gresources(RESOURCE_FILE +# XML_OUT +# TYPE EMBED_C +# RESOURCES ${RESOURCE_LIST} +# PREFIX "/org/gtk/daleclack" +# SOURCE_DIR ${PROJECT_SOURCE_DIR}/res) + +# Add a custom target to the makefile. Now make builds our resource file. +# It depends on the output RESOURCE_FILE. + +# add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE}) + +#For win32 platform,use rc resource and .ico icon +if(WIN32) + SET(CMAKE_RC_COMPILER windres) + set(app_WINRC ../icon.rc) + set_property(SOURCE ../icon.rc APPEND PROPERTY + OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico + ) + add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE}) + add_custom_command( TARGET ${PROJECT_NAME} + COMMAND echo * > ${CMAKE_BINARY_DIR}/.gitignore + COMMAND echo **/* > ${CMAKE_BINARY_DIR}/.hgignore) +else() + add_executable(${PROJECT_NAME} ${SOURCE_FILE}) + add_custom_command( TARGET ${PROJECT_NAME} + COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore + COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore) +endif(WIN32) + +#Add command to generate .gitignore and .mo files +# add_custom_command( TARGET ${PROJECT_NAME} +# COMMAND mkdir -p ${PO_DIR} +# COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} ${CMAKE_SOURCE_DIR}/po/zh_CN.po -o ${PO_DIR}/${PROJECT_NAME}.mo) + +SET (CMAKE_EXTRA_CXX_FLAGS ${GTK_CFLAGS_OTHER}) +target_link_libraries (${PROJECT_NAME} ${GTK_LIBRARIES} -lpthread) diff --git a/Gtk4/gtk161_core_voltage/src/CoreVolt.cpp b/Gtk4/gtk161_core_voltage/src/CoreVolt.cpp new file mode 100644 index 0000000..7d5a2cd --- /dev/null +++ b/Gtk4/gtk161_core_voltage/src/CoreVolt.cpp @@ -0,0 +1,37 @@ +#include "CoreVolt.h" +#include +#include + +static double voltage = 0.0; +double get_core_voltage(char *current_voltage_str, char *max_voltage_str) +{ + double current_voltage = 0.0; + char buffer[10]; + + while (1) + { + // Open Pipe to get output + FILE *pipe = popen("echo \"scale=2; $(rdmsr 0x198 -u --bitfield 47:32)/8192\" | bc", "r"); + + // Check if Pipe is open + if (pipe) + { + // Read from Pipe + while (!feof(pipe)) + { + fgets(buffer, sizeof(buffer) - 1, pipe); + } + + // Update the max voltage + current_voltage = atof(buffer); + snprintf(current_voltage_str, sizeof(current_voltage_str) - 1, "%0.2fV", current_voltage); + if (current_voltage > voltage) + { + voltage = current_voltage; + } + snprintf(max_voltage_str, sizeof(max_voltage_str) - 1, "%0.2fV", voltage); + } + pclose(pipe); + return current_voltage; + } +} diff --git a/Gtk4/gtk161_core_voltage/src/CoreVolt.h b/Gtk4/gtk161_core_voltage/src/CoreVolt.h new file mode 100644 index 0000000..9d1dc8e --- /dev/null +++ b/Gtk4/gtk161_core_voltage/src/CoreVolt.h @@ -0,0 +1,4 @@ +#pragma once + +// Get Core Voltage by a command +double get_core_voltage(char *current_voltage_str, char *max_voltage_str); diff --git a/Gtk4/gtk161_core_voltage/src/MainWin.cpp b/Gtk4/gtk161_core_voltage/src/MainWin.cpp new file mode 100644 index 0000000..6cfa259 --- /dev/null +++ b/Gtk4/gtk161_core_voltage/src/MainWin.cpp @@ -0,0 +1,56 @@ +#include "MainWin.h" +#include "CoreVolt.h" + +struct _MainWin{ + GtkApplicationWindow parent_instance; + GtkWidget *label_maxvolt, *label_currvolt, *label_info1, *label_info2; + GtkWidget *main_frame; + GtkWidget *volt_grid; +}; + +G_DEFINE_TYPE(MainWin, main_win, GTK_TYPE_APPLICATION_WINDOW) + +gboolean core_volt_func(gpointer data){ + MainWin *self = Main_Win(data); + char max_volt[10], curr_volt[10]; + + // Update core voltage values to the labels + get_core_voltage(curr_volt, max_volt); + gtk_label_set_text(GTK_LABEL(self->label_maxvolt), max_volt); + gtk_label_set_text(GTK_LABEL(self->label_currvolt), curr_volt); + + return TRUE; +} + +static void main_win_init(MainWin *self){ + GtkWindow *win = GTK_WINDOW(self); + // Initialize window properties + gtk_window_set_title(win, "Core Voltage Monitor"); + gtk_window_set_default_size(win, 250, 200); + gtk_window_set_icon_name(win, "org.gtk.daleclack"); + + // Add widgets to window + self->volt_grid = gtk_grid_new(); + self->main_frame = gtk_frame_new(NULL); + gtk_frame_set_label_widget(GTK_FRAME(self->main_frame), gtk_label_new("Core Voltage Information")); + gtk_widget_set_valign(GTK_WIDGET(self->main_frame), GTK_ALIGN_CENTER); + self->label_info1 = gtk_label_new("Max Core Voltage: "); + self->label_info2 = gtk_label_new("Current Core Voltage: "); + self->label_maxvolt = gtk_label_new("0.00 V"); + self->label_currvolt = gtk_label_new("0.00 V"); + gtk_grid_attach(GTK_GRID(self->volt_grid), self->label_info1, 0, 0, 1, 1); + gtk_grid_attach(GTK_GRID(self->volt_grid), self->label_maxvolt, 1, 0, 1, 1); + gtk_grid_attach(GTK_GRID(self->volt_grid), self->label_info2, 0, 1, 1, 1); + gtk_grid_attach(GTK_GRID(self->volt_grid), self->label_currvolt, 1, 1, 1, 1); + gtk_frame_set_child(GTK_FRAME(self->main_frame), self->volt_grid); + gtk_window_set_child(win, self->main_frame); + g_timeout_add(1000, core_volt_func, self); +} + +static void main_win_class_init(MainWinClass *klass){ +} + +// Create a new MainWin +MainWin *main_win_new(GtkApplication *app){ + return Main_Win(g_object_new(MAIN_WIN_TYPE, "application", app, NULL)); +} \ No newline at end of file diff --git a/Gtk4/gtk161_core_voltage/src/MainWin.h b/Gtk4/gtk161_core_voltage/src/MainWin.h new file mode 100644 index 0000000..ee7fc4b --- /dev/null +++ b/Gtk4/gtk161_core_voltage/src/MainWin.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +G_DECLARE_FINAL_TYPE(MainWin, main_win, Main, Win, GtkApplicationWindow) + +// Returns the MainWin Type +#define MAIN_WIN_TYPE (main_win_get_type()) + +// Creates a new MainWin Object +MainWin *main_win_new(GtkApplication *app); diff --git a/Gtk4/gtk161_core_voltage/src/main.cpp b/Gtk4/gtk161_core_voltage/src/main.cpp new file mode 100644 index 0000000..f9a0e56 --- /dev/null +++ b/Gtk4/gtk161_core_voltage/src/main.cpp @@ -0,0 +1,16 @@ +#include "MainWin.h" + +static void gtkmain(GtkApplication *app, gpointer user_data){ + MainWin *mainwin = main_win_new(app); + gtk_window_present(GTK_WINDOW(mainwin)); +} + +int main(int argc, char **argv) +{ + // Create a GtkApplication instance + GtkApplication *app = gtk_application_new("org.gtk.daleclack", G_APPLICATION_NON_UNIQUE); + g_signal_connect(app, "activate", G_CALLBACK(gtkmain), NULL); + + // Run the application + return g_application_run(G_APPLICATION(app), argc, argv); +} \ No newline at end of file