Skip to content

Commit

Permalink
Core: add option to change temperature unit
Browse files Browse the repository at this point in the history
Fix #297
  • Loading branch information
TheTumultuousUnicornOfDarkness committed Oct 8, 2023
1 parent 18d7adf commit 008e03c
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 12 deletions.
13 changes: 13 additions & 0 deletions data/com.github.thetumultuousunicornofdarkness.cpu-x.gschema.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<schemalist>
<enum id="com.github.thetumultuousunicornofdarkness.cpu-x.temperature-unit">
<value value="0" nick="celsius"/>
<value value="1" nick="fahrenheit"/>
<value value="2" nick="kelvin"/>
<value value="3" nick="rankine"/>
</enum>
<enum id="com.github.thetumultuousunicornofdarkness.cpu-x.theme">
<value value="0" nick="auto"/>
<value value="1" nick="light"/>
Expand All @@ -17,6 +23,13 @@
</enum>

<schema id="com.github.thetumultuousunicornofdarkness.cpu-x" path="/com/github/thetumultuousunicornofdarkness/cpu-x/">
<key name="temperature-unit" enum="com.github.thetumultuousunicornofdarkness.cpu-x.temperature-unit">
<default>'celsius'</default>
<summary>Temperature unit</summary>
<description>
Set temperature unit
</description>
</key>
<key name="refresh-time" type="u">
<default>1</default>
<range min="1" max="65535"/>
Expand Down
41 changes: 36 additions & 5 deletions data/cpu-x-gtk-3.12.ui
Original file line number Diff line number Diff line change
Expand Up @@ -5704,7 +5704,7 @@
<property name="scrollable">True</property>
<property name="enable-popup">True</property>
<child>
<!-- n-columns=2 n-rows=2 -->
<!-- n-columns=2 n-rows=3 -->
<object class="GtkGrid" id="interface_box">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand All @@ -5723,7 +5723,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
Expand All @@ -5734,7 +5734,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
Expand All @@ -5747,7 +5747,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
Expand All @@ -5764,7 +5764,38 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="temperatureunit_lab">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes" comments="Setting: temperature unit">Temperature unit</property>
<property name="mnemonic-widget">refreshtime_val</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="temperatureunit_val">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="active">0</property>
<items>
<item id="celsius">Celsius (°C)</item>
<item id="fahrenheit">Fahrenheit (°F)</item>
<item id="kelvin">Kelvin (K)</item>
<item id="rankine">Rankine (Ra)</item>
</items>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
Expand Down
8 changes: 4 additions & 4 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ static int call_libcpuid_msr_dynamic(Data &data)

/* CPU Temperature */
if(msg.temp != CPU_INVALID_VALUE)
cpu_type.processor.temperature.value = string_format("%i°C", msg.temp);
cpu_type.processor.temperature.value = string_format_temperature_unit("%i", msg.temp);

return 0;
}
Expand Down Expand Up @@ -1637,7 +1637,7 @@ static int gpu_monitoring([[maybe_unused]] Data &data)

/* Set labels value */
SET_LABEL_VALUE(vbios_version, "%s", vbios_version.value.c_str());
SET_LABEL_VALUE(temperature, "%.2Lf°C", std::stoull(temperature.value) / temperature.divisor);
SET_LABEL_VALUE(temperature, "%s", string_format_temperature_unit("%.2f", std::stoull(temperature.value) / temperature.divisor).c_str());
SET_LABEL_VALUE(usage, "%s%%", usage.value.c_str());
SET_LABEL_VALUE(core_voltage, "%.2Lf V", std::stoull(core_voltage.value) / core_voltage.divisor);
SET_LABEL_VALUE(power_avg, "%.2Lf W", std::stoull(power_avg.value) / power_avg.divisor);
Expand Down Expand Up @@ -2133,7 +2133,7 @@ static int cputab_temp_fallback(Data &data)
{
const double tmp = std::stod(temperature);
if(tmp > 0.0)
cpu_type.processor.temperature.value = string_format("%.1f°C", tmp / 1e3);
cpu_type.processor.temperature.value = string_format_temperature_unit("%.1f", tmp / 1e3);
}
}

Expand All @@ -2145,7 +2145,7 @@ static int cputab_temp_fallback(Data &data)

snprintf(name, MAXSTR, "dev.cpu.%i.temperature", current_core_id);
if(!(err = sysctlbyname(name, &temperature, &len, NULL, 0)))
cpu_type.processor.temperature.value = string_format("%.1f°C", double(temperature - 2731) / 10.0); // decikelvins
cpu_type.processor.temperature.value = string_format_temperature_unit("%.1f", double(temperature - 2731) / 10.0); // decikelvins
#endif /* __linux__ */

if(err)
Expand Down
9 changes: 8 additions & 1 deletion src/gui_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void GtkData::get_widgets(Glib::RefPtr<Gtk::Builder> builder)
builder->get_widget("settingsbutton", this->settingsbutton);
builder->get_widget("validatebutton", this->validatebutton);
builder->get_widget("cancelbutton", this->cancelbutton);
builder->get_widget("temperatureunit_val", this->temperatureunit);
builder->get_widget("refreshtime_val", this->refreshtime);
builder->get_widget("theme_val", this->theme);
builder->get_widget("defaulttab_val", this->defaulttab);
Expand Down Expand Up @@ -529,9 +530,13 @@ void GtkData::set_signals()
/* Hide settings window and apply changes */
this->validatebutton->signal_clicked().connect([this]
{
/* Apply new temperature unit */
Options::set_temp_unit(static_cast<OptTempUnit>(settings->get_enum("temperature-unit")));

/* Apply new refresh time */
Options::set_refr_time(settings->get_uint("refresh-time"));
this->refresh_handle.disconnect();
this->refresh_handle = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &GtkData::grefresh), settings->get_uint("refresh-time"));
this->refresh_handle = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &GtkData::grefresh), Options::get_refr_time());

/* Apply new color theme */
this->check_theme_color();
Expand All @@ -553,6 +558,7 @@ void GtkData::set_signals()

void GtkData::bind_settings()
{
settings->bind("temperature-unit", this->temperatureunit, "active-id");
settings->bind("refresh-time", this->refreshtime, "value" );
settings->bind("gui-theme", this->theme, "active-id");
settings->bind("default-tab", this->defaulttab, "active-id");
Expand Down Expand Up @@ -1234,6 +1240,7 @@ void load_settings()
Glib::init();
settings = Gio::Settings::create(APPLICATION_ID);
settings->delay();
Options::set_temp_unit (static_cast<OptTempUnit>(settings->get_enum("temperature-unit")));
Options::set_refr_time (settings->get_uint("refresh-time"));
Options::set_selected_page (static_cast<TabNumber>(settings->get_enum("default-tab")));
Options::set_selected_type (settings->get_uint("default-core-type"), -1);
Expand Down
1 change: 1 addition & 0 deletions src/gui_gtk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct GtkData
Gtk::Button *settingsbutton = nullptr;
Gtk::Button *validatebutton = nullptr;
Gtk::Button *cancelbutton = nullptr;
Gtk::ComboBoxText *temperatureunit = nullptr;
Gtk::SpinButton *refreshtime = nullptr;
Gtk::ComboBoxText *theme = nullptr;
Gtk::ComboBoxText *defaulttab = nullptr;
Expand Down
14 changes: 13 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cstring>
#include <csignal>
#include <clocale>
#include <cctype>
#include <string>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -64,6 +65,7 @@ static const std::list<struct Getopt> cpux_options =
{ true, 'D', "dump", no_argument, N_("Dump all data on standard output and exit") },
{ HAS_DMIDECODE, 'M', "dmidecode", no_argument, N_("Run embedded command dmidecode and exit") },
{ HAS_BANDWIDTH, 'B', "bandwidth", no_argument, N_("Run embedded command bandwidth and exit") },
{ true, 'u', "temp-unit", required_argument, N_("Set temperature unit (c[elsius]|f[ahrenheit]|k[elvin]|r[ankine])") },
{ true, 'r', "refresh", required_argument, N_("Set custom time between two refreshes (in seconds)") },
{ true, 't', "tab", required_argument, N_("Set default tab (integer)") },
{ HAS_LIBCPUID, 'p', "type", required_argument, N_("Select core type to monitor (integer)") },
Expand Down Expand Up @@ -262,6 +264,16 @@ static void parse_arguments(std::forward_list<std::string> &cmd_args)
case 'B':
Options::set_output_type(OUT_BANDWIDTH);
break;
case 'u':
switch(std::tolower(optarg[0]))
{
case 'c': Options::set_temp_unit(CELSIUS); break;
case 'f': Options::set_temp_unit(FAHRENHEIT); break;
case 'k': Options::set_temp_unit(KELVIN); break;
case 'r': Options::set_temp_unit(RANKINE); break;
default: help(binary_name); exit(EXIT_FAILURE);
}
break;
case 'r':
Options::set_refr_time(std::stoul(optarg));
break;
Expand Down Expand Up @@ -328,7 +340,7 @@ static void parse_arguments(std::forward_list<std::string> &cmd_args)
}
else if(!strcmp(longopts[longindex].name, "keymap"))
{
switch(optarg[0])
switch(std::tolower(optarg[0]))
{
case 'a': Options::set_keymap(ARROWS); break;
case 'e': Options::set_keymap(EMACS); break;
Expand Down
22 changes: 22 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,25 @@ OptKeymap Options::get_keymap()
{
return Options::keymap;
}

bool Options::set_temp_unit(OptTempUnit temp_unit)
{
switch(temp_unit)
{
case CELSIUS:
case FAHRENHEIT:
case KELVIN:
case RANKINE:
Options::temp_unit = temp_unit;
return true;
default:
Options::temp_unit = CELSIUS;
MSG_ERROR("Options::set_temp_unit() called with %i", temp_unit);
return false;
}
}

OptTempUnit Options::get_temp_unit()
{
return Options::temp_unit;
}
14 changes: 13 additions & 1 deletion src/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ enum OptKeymap
LASTKEYMAP
};

enum OptTempUnit
{
CELSIUS,
FAHRENHEIT,
KELVIN,
RANKINE,
LASTTEMPUNIT
};

enum TabNumber
{
TAB_CPU, TAB_CACHES, TAB_MOTHERBOARD, TAB_MEMORY, TAB_SYSTEM, TAB_GRAPHICS, TAB_BENCH, TAB_ABOUT
Expand Down Expand Up @@ -91,6 +100,8 @@ class Options
static uint16_t get_refr_time();
static bool set_keymap(OptKeymap keymap);
static OptKeymap get_keymap();
static bool set_temp_unit(OptTempUnit temp_unit);
static OptTempUnit get_temp_unit();

private:
static inline bool cpuid_decimal = false;
Expand All @@ -112,7 +123,8 @@ class Options
static inline uint16_t refr_time = 1;

static inline TabNumber selected_page = TAB_CPU;
static inline OptKeymap keymap = ARROWS;
static inline OptKeymap keymap = ARROWS;
static inline OptTempUnit temp_unit = CELSIUS;
Options() = delete;
~Options() = delete;
};
Expand Down
32 changes: 32 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,38 @@ std::string string_set_size_unit(char *str_src)
}
#undef TOKEN_LEN

/* Format temperature with proper unit */
std::string string_format_temperature_unit(const char *fmt, const double temp_celsius)
{
double temp;
std::string unit;

switch(Options::get_temp_unit())
{
case FAHRENHEIT:
unit = "°F";
temp = temp_celsius * 9/5 + 32;
break;
case KELVIN:
unit = "K";
temp = temp_celsius + 273.15;
break;
case RANKINE:
unit = "Ra";
temp = temp_celsius * 9/5 + 491.67;
break;
default:
unit = "°C";
temp = temp_celsius;
break;
}

std::string ret = string_format(fmt, temp);
ret += unit;
MSG_DEBUG("string_format_temperature_unit: %.2f°C ==> %s", temp_celsius, ret.c_str());
return ret;
}

/* Open a file and put its content in a variable ('str' accept printf-like format) */
int fopen_to_str(std::string &out, const char *str, ...)
{
Expand Down
3 changes: 3 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ void string_trim(std::string &str);
/* Duplicate a string and set data size unit */
std::string string_set_size_unit(char *str_src);

/* Format temperature with proper unit */
std::string string_format_temperature_unit(const char *fmt, const double temp_celsius);

/* Open a file and put its content in a variable ('str' accept printf-like format) */
int fopen_to_str(std::string &out, const char *str, ...);

Expand Down

0 comments on commit 008e03c

Please sign in to comment.