2 * Copyright (C) 2010-2016 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <sys/types.h>
30 #define _(str) gettext(str)
37 /* Properties of each sensor */
38 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm_enabled";
39 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm_high_threshold";
40 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm_low_threshold";
41 static const char *ATT_SENSOR_COLOR = "color";
42 static const char *ATT_SENSOR_GRAPH_ENABLED = "graph_enabled";
43 static const char *ATT_SENSOR_NAME = "name";
44 static const char *ATT_SENSOR_APPINDICATOR_MENU_DISABLED
45 = "appindicator_menu_disabled";
46 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
47 = "appindicator_label_enabled";
48 static const char *ATT_SENSOR_POSITION = "position";
49 static const char *ATT_SENSOR_HIDE = "hide";
51 /* Update interval of the measures of the sensors */
52 static const char *KEY_SENSOR_UPDATE_INTERVAL
53 = "sensor-update-interval";
56 static const char *KEY_GRAPH_UPDATE_INTERVAL = "graph-update-interval";
57 static const char *KEY_GRAPH_MONITORING_DURATION = "graph-monitoring-duration";
58 static const char *KEY_GRAPH_BACKGROUND_COLOR = "graph-background-color";
59 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
60 static const char *KEY_GRAPH_BACKGROUND_ALPHA = "graph-background-alpha";
61 static const char *KEY_GRAPH_FOREGROUND_COLOR
62 = "graph-foreground-color";
63 static const char *KEY_GRAPH_SMOOTH_CURVES_ENABLED
64 = "graph-smooth-curves-enabled";
66 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
68 static const char *KEY_ALPHA_CHANNEL_ENABLED = "graph-alpha-channel-enabled";
70 /* Inteface settings */
71 static const char *KEY_INTERFACE_SENSORLIST_POSITION
72 = "interface-sensorlist-position";
74 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
75 = "interface-window-decoration-disabled";
77 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
78 = "interface-window-keep-below-enabled";
80 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
81 = "interface-menu-bar-disabled";
83 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
84 = "interface-unity-launcher-count-disabled";
86 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
87 = "interface-hide-on-startup";
89 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
90 = "interface-window-restore-enabled";
92 static const char *KEY_INTERFACE_WINDOW_X = "interface-window-x";
93 static const char *KEY_INTERFACE_WINDOW_Y = "interface-window-y";
94 static const char *KEY_INTERFACE_WINDOW_W = "interface-window-w";
95 static const char *KEY_INTERFACE_WINDOW_H = "interface-window-h";
97 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
98 = "interface-window-divider-pos";
100 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
101 = "interface-temperature-unit";
103 /* Sensor logging settings */
104 static const char *KEY_SLOG_ENABLED = "slog-enabled";
105 static const char *KEY_SLOG_INTERVAL = "slog-interval";
107 /* Path to the script called when a notification is raised */
108 static const char *KEY_NOTIFICATION_SCRIPT = "notif-script";
110 /* Provider settings */
111 static const char *KEY_PROVIDER_LMSENSORS_ENABLED
112 = "provider-lmsensors-enabled";
113 static const char *KEY_PROVIDER_ATIADLSDK_ENABLED
114 = "provider-atiadlsdk-enabled";
115 static const char *KEY_PROVIDER_GTOP2_ENABLED = "provider-gtop2-enabled";
116 static const char *KEY_PROVIDER_HDDTEMP_ENABLED = "provider-hddtemp-enabled";
117 static const char *KEY_PROVIDER_LIBATASMART_ENABLED
118 = "provider-libatasmart-enabled";
119 static const char *KEY_PROVIDER_NVCTRL_ENABLED = "provider-nvctrl-enabled";
120 static const char *KEY_PROVIDER_UDISKS2_ENABLED = "provider-udisks2-enabled";
122 static const char *KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE
123 = "default-high-threshold-temperature";
124 static const char *KEY_DEFAULT_SENSOR_ALARM_ENABLED
125 = "default-sensor-alarm-enabled";
127 static GSettings *settings;
129 static char *user_dir;
131 static GKeyFile *key_file;
133 static char *sensor_config_path;
135 static void (*slog_enabled_cbk)(void *);
137 static char *get_string(const char *key)
139 return g_settings_get_string(settings, key);
142 static void set_string(const char *key, const char *str)
144 g_settings_set_string(settings, key, str);
147 static void set_bool(const char *k, bool b)
149 g_settings_set_boolean(settings, k, b);
152 static bool get_bool(const char *k)
154 return g_settings_get_boolean(settings, k);
157 static void set_int(const char *k, int i)
159 g_settings_set_int(settings, k, i);
162 static double get_double(const char *k)
164 return g_settings_get_double(settings, k);
167 static void set_double(const char *k, double d)
169 g_settings_set_double(settings, k, d);
172 static int get_int(const char *k)
174 return g_settings_get_int(settings, k);
177 char *config_get_notif_script(void)
181 str = get_string(KEY_NOTIFICATION_SCRIPT);
182 if (str && !strlen(str)) {
190 void config_set_notif_script(const char *str)
192 if (str && strlen(str) > 0)
193 set_string(KEY_NOTIFICATION_SCRIPT, str);
195 set_string(KEY_NOTIFICATION_SCRIPT, "");
198 static struct color *get_background_color(void)
203 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR);
205 c = str_to_color(scolor);
209 return color_new(1, 1, 1);
214 static struct color *get_foreground_color(void)
219 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR);
221 c = str_to_color(scolor);
225 return color_new(0, 0, 0);
230 static bool is_alpha_channel_enabled(void)
232 return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
235 static void set_alpha_channeld_enabled(bool b)
237 set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
240 enum sensorlist_position config_get_sensorlist_position(void)
242 return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
245 void config_set_sensorlist_position(enum sensorlist_position pos)
247 set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
250 static double get_graph_background_alpha(void)
252 return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
255 static void set_graph_background_alpha(double alpha)
257 set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
260 static void set_background_color(const struct color *color)
264 scolor = color_to_str(color);
266 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
268 set_string(KEY_GRAPH_BACKGROUND_COLOR, scolor);
273 static void set_foreground_color(const struct color *color)
277 str = color_to_str(color);
279 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
281 set_string(KEY_GRAPH_FOREGROUND_COLOR, str);
286 bool is_slog_enabled(void)
288 return get_bool(KEY_SLOG_ENABLED);
291 static void set_slog_enabled(bool enabled)
293 set_bool(KEY_SLOG_ENABLED, enabled);
296 static void slog_enabled_changed_cbk(GSettings *settings,
300 if (slog_enabled_cbk)
301 slog_enabled_cbk(data);
304 void config_set_slog_enabled_changed_cbk(void (*cbk)(void *), void *data)
308 slog_enabled_cbk = cbk;
310 g_signal_connect_after(settings,
311 "changed::slog-enabled",
312 G_CALLBACK(slog_enabled_changed_cbk),
318 int config_get_slog_interval(void)
320 return get_int(KEY_SLOG_INTERVAL);
323 static void set_slog_interval(int interval)
328 set_int(KEY_SLOG_INTERVAL, interval);
331 bool config_is_window_decoration_enabled(void)
333 return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
336 bool config_is_window_keep_below_enabled(void)
338 return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
341 void config_set_window_decoration_enabled(bool enabled)
343 set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
346 void config_set_window_keep_below_enabled(bool enabled)
348 set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
351 bool config_is_smooth_curves_enabled(void)
353 return get_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED);
356 void config_set_smooth_curves_enabled(bool b)
358 set_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED, b);
361 double config_get_default_high_threshold_temperature(void)
363 return get_double(KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE);
366 static bool config_get_default_sensor_alarm_enabled(void)
368 return get_bool(KEY_DEFAULT_SENSOR_ALARM_ENABLED);
371 static void init(void)
376 settings = g_settings_new("psensor");
381 void config_cleanup(void)
387 g_object_unref(settings);
397 g_key_file_free(key_file);
401 if (sensor_config_path) {
402 free(sensor_config_path);
403 sensor_config_path = NULL;
406 slog_enabled_cbk = NULL;
409 struct config *config_load(void)
415 c = malloc(sizeof(struct config));
417 c->graph_bgcolor = get_background_color();
418 c->graph_fgcolor = get_foreground_color();
419 c->graph_bg_alpha = get_graph_background_alpha();
420 c->alpha_channel_enabled = is_alpha_channel_enabled();
421 c->slog_enabled = is_slog_enabled();
422 c->slog_interval = config_get_slog_interval();
424 c->sensor_update_interval
425 = get_int(KEY_SENSOR_UPDATE_INTERVAL);
426 if (c->sensor_update_interval < 1)
427 c->sensor_update_interval = 1;
429 c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
430 if (c->graph_update_interval < 1)
431 c->graph_update_interval = 1;
433 c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
435 if (c->graph_monitoring_duration < 1)
436 c->graph_monitoring_duration = 10;
438 c->window_restore_enabled
439 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
441 c->window_x = get_int(KEY_INTERFACE_WINDOW_X);
442 c->window_y = get_int(KEY_INTERFACE_WINDOW_Y);
443 c->window_w = get_int(KEY_INTERFACE_WINDOW_W);
444 c->window_h = get_int(KEY_INTERFACE_WINDOW_H);
446 c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
448 c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
450 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
455 c->sensor_values_max_length = compute_values_max_length(c);
460 void config_save(const struct config *c)
462 set_alpha_channeld_enabled(c->alpha_channel_enabled);
463 set_background_color(c->graph_bgcolor);
464 set_foreground_color(c->graph_fgcolor);
465 set_graph_background_alpha(c->graph_bg_alpha);
466 set_slog_enabled(c->slog_enabled);
467 set_slog_interval(c->slog_interval);
469 set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
471 set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
473 set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval);
475 set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
477 set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
478 c->window_restore_enabled);
480 set_int(KEY_INTERFACE_WINDOW_X, c->window_x);
481 set_int(KEY_INTERFACE_WINDOW_Y, c->window_y);
482 set_int(KEY_INTERFACE_WINDOW_W, c->window_w);
483 set_int(KEY_INTERFACE_WINDOW_H, c->window_h);
485 set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
488 const char *get_psensor_user_dir(void)
495 home = getenv("HOME");
500 user_dir = path_append(home, ".psensor");
502 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
503 log_err(_("Failed to create the directory %s: %s"),
517 static const char *get_sensor_config_path(void)
521 if (!sensor_config_path) {
522 dir = get_psensor_user_dir();
525 sensor_config_path = path_append(dir, "psensor.cfg");
528 return sensor_config_path;
531 static GKeyFile *get_sensor_key_file(void)
538 path = get_sensor_config_path();
540 key_file = g_key_file_new();
543 ret = g_key_file_load_from_file(key_file,
545 G_KEY_FILE_KEEP_COMMENTS
546 | G_KEY_FILE_KEEP_TRANSLATIONS,
550 log_warn(_("Failed to load configuration file %s: %s"),
560 static void save_sensor_key_file(void)
568 kfile = get_sensor_key_file();
570 data = g_key_file_to_data(kfile, NULL, NULL);
572 path = get_sensor_config_path();
574 if (!g_file_set_contents(path, data, -1, NULL))
575 log_err(_("Failed to save configuration file %s."), path);
582 void config_sync(void)
587 save_sensor_key_file();
591 static void sensor_set_str(const char *sid, const char *att, const char *str)
595 kfile = get_sensor_key_file();
596 g_key_file_set_string(kfile, sid, att, str);
599 static char *sensor_get_str(const char *sid, const char *att)
603 kfile = get_sensor_key_file();
604 return g_key_file_get_string(kfile, sid, att, NULL);
607 static bool sensor_get_double(const char *sid,
616 kfile = get_sensor_key_file();
619 if (!g_key_file_has_key(kfile, sid, att, &err)) {
621 log_err(err->message);
631 v = g_key_file_get_double(kfile, sid, att, &err);
634 log_err(err->message);
645 static bool sensor_get_bool(const char *sid, const char *att, bool dft)
651 kfile = get_sensor_key_file();
654 ret = g_key_file_get_boolean(kfile, sid, att, &err);
657 if (err->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND)
660 log_err(err->message);
668 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
672 kfile = get_sensor_key_file();
674 g_key_file_set_boolean(kfile, sid, att, enabled);
677 static int sensor_get_int(const char *sid, const char *att)
681 kfile = get_sensor_key_file();
682 return g_key_file_get_integer(kfile, sid, att, NULL);
685 static void sensor_set_int(const char *sid, const char *att, int i)
689 kfile = get_sensor_key_file();
691 g_key_file_set_integer(kfile, sid, att, i);
694 char *config_get_sensor_name(const char *sid)
696 return sensor_get_str(sid, ATT_SENSOR_NAME);
699 void config_set_sensor_name(const char *sid, const char *name)
701 sensor_set_str(sid, ATT_SENSOR_NAME, name);
704 void config_set_sensor_color(const char *sid, const GdkRGBA *color)
708 str = gdk_rgba_to_string(color);
710 sensor_set_str(sid, ATT_SENSOR_COLOR, str);
715 static const char *next_default_color(void)
717 /* copied from the default colors of the gtk color color
720 const char *default_colors[27] = {
721 "#ef2929", /* Scarlet Red */
722 "#fcaf3e", /* Orange */
723 "#fce94f", /* Butter */
724 "#8ae234", /* Chameleon */
725 "#729fcf", /* Sky Blue */
726 "#ad7fa8", /* Plum */
727 "#e9b96e", /* Chocolate */
728 "#888a85", /* Aluminum 1 */
729 "#eeeeec", /* Aluminum 2 */
752 c = default_colors[next_idx % 27];
759 GdkRGBA *config_get_sensor_color(const char *sid)
765 str = sensor_get_str(sid, ATT_SENSOR_COLOR);
768 ret = gdk_rgba_parse(&rgba, str);
773 gdk_rgba_parse(&rgba, next_default_color());
774 config_set_sensor_color(sid, &rgba);
777 return gdk_rgba_copy(&rgba);
780 bool config_is_sensor_graph_enabled(const char *sid)
782 return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED, false);
785 void config_set_sensor_graph_enabled(const char *sid, bool enabled)
787 sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
790 bool config_get_sensor_alarm_high_threshold(const char *sid,
794 return sensor_get_double(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, v, d);
797 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
799 sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
802 bool config_get_sensor_alarm_low_threshold(const char *sid, double *v, double d)
804 return sensor_get_double(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, v, d);
807 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
809 sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
812 bool config_is_appindicator_enabled(const char *sid)
814 return !sensor_get_bool(sid,
815 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
819 void config_set_appindicator_enabled(const char *sid, bool enabled)
822 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
826 int config_get_sensor_position(const char *sid)
828 return sensor_get_int(sid, ATT_SENSOR_POSITION);
831 void config_set_sensor_position(const char *sid, int pos)
833 sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
836 bool config_get_sensor_alarm_enabled(const char *sid)
838 return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED, false);
841 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
843 sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
846 bool config_is_sensor_enabled(const char *sid)
848 return !sensor_get_bool(sid,
850 config_get_default_sensor_alarm_enabled());
853 void config_set_sensor_enabled(const char *sid, bool enabled)
855 sensor_set_bool(sid, ATT_SENSOR_HIDE, !enabled);
858 bool config_is_appindicator_label_enabled(const char *sid)
860 return sensor_get_bool(sid,
861 ATT_SENSOR_APPINDICATOR_LABEL_ENABLED,
865 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
867 sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);
870 GSettings *config_get_GSettings(void)
875 bool config_is_lmsensor_enabled(void)
877 return get_bool(KEY_PROVIDER_LMSENSORS_ENABLED);
880 bool config_is_gtop2_enabled(void)
882 return get_bool(KEY_PROVIDER_GTOP2_ENABLED);
885 bool config_is_udisks2_enabled(void)
887 return get_bool(KEY_PROVIDER_UDISKS2_ENABLED);
890 bool config_is_hddtemp_enabled(void)
892 return get_bool(KEY_PROVIDER_HDDTEMP_ENABLED);
895 bool config_is_libatasmart_enabled(void)
897 return get_bool(KEY_PROVIDER_LIBATASMART_ENABLED);
900 bool config_is_nvctrl_enabled(void)
902 return get_bool(KEY_PROVIDER_NVCTRL_ENABLED);
905 bool config_is_atiadlsdk_enabled(void)
907 return get_bool(KEY_PROVIDER_ATIADLSDK_ENABLED);
910 void config_set_lmsensor_enable(bool b)
912 set_bool(KEY_PROVIDER_LMSENSORS_ENABLED, b);
915 void config_set_nvctrl_enable(bool b)
917 set_bool(KEY_PROVIDER_NVCTRL_ENABLED, b);
920 void config_set_atiadlsdk_enable(bool b)
922 set_bool(KEY_PROVIDER_ATIADLSDK_ENABLED, b);
925 void config_set_gtop2_enable(bool b)
927 set_bool(KEY_PROVIDER_GTOP2_ENABLED, b);
930 void config_set_hddtemp_enable(bool b)
932 set_bool(KEY_PROVIDER_HDDTEMP_ENABLED, b);
935 void config_set_libatasmart_enable(bool b)
937 set_bool(KEY_PROVIDER_LIBATASMART_ENABLED, b);
940 void config_set_udisks2_enable(bool b)
942 set_bool(KEY_PROVIDER_UDISKS2_ENABLED, b);
945 enum temperature_unit config_get_temperature_unit(void)
947 return get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
950 void config_set_temperature_unit(enum temperature_unit u)
952 set_int(KEY_INTERFACE_TEMPERATURE_UNIT, u);
955 bool config_is_menu_bar_enabled(void)
957 return !get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
960 void config_set_menu_bar_enabled(bool enabled)
962 set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, !enabled);
965 bool config_is_count_visible(void)
967 return !get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
970 void config_set_count_visible(bool visible)
972 set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED, !visible);