X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Fcfg.c;h=d9034783ccc72db6ba42e1f95657de573a91bcbf;hp=c47bd061d51d5f8e18e612f58cc37646a315bafc;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hpb=188744a0e57f78e8e9662629de46780cf889ee38 diff --git a/src/cfg.c b/src/cfg.c index c47bd06..d903478 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 jeanfi@gmail.com + * Copyright (C) 2010-2016 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -29,9 +29,8 @@ #include #define _(str) gettext(str) -#include - #include +#include #include #include @@ -47,24 +46,22 @@ static const char *ATT_SENSOR_APPINDICATOR_MENU_DISABLED static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED = "appindicator_label_enabled"; static const char *ATT_SENSOR_POSITION = "position"; +static const char *ATT_SENSOR_HIDE = "hide"; /* Update interval of the measures of the sensors */ static const char *KEY_SENSOR_UPDATE_INTERVAL -= "/apps/psensor/sensor/update_interval"; += "sensor-update-interval"; /* Graph settings */ static const char *KEY_GRAPH_UPDATE_INTERVAL = "graph-update-interval"; - static const char *KEY_GRAPH_MONITORING_DURATION = "graph-monitoring-duration"; - static const char *KEY_GRAPH_BACKGROUND_COLOR = "graph-background-color"; - static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5"; - static const char *KEY_GRAPH_BACKGROUND_ALPHA = "graph-background-alpha"; - static const char *KEY_GRAPH_FOREGROUND_COLOR = "graph-foreground-color"; +static const char *KEY_GRAPH_SMOOTH_CURVES_ENABLED += "graph-smooth-curves-enabled"; static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000"; @@ -110,7 +107,23 @@ static const char *KEY_SLOG_INTERVAL = "slog-interval"; /* Path to the script called when a notification is raised */ static const char *KEY_NOTIFICATION_SCRIPT = "notif-script"; -static GConfClient *client; +/* Provider settings */ +static const char *KEY_PROVIDER_LMSENSORS_ENABLED += "provider-lmsensors-enabled"; +static const char *KEY_PROVIDER_ATIADLSDK_ENABLED += "provider-atiadlsdk-enabled"; +static const char *KEY_PROVIDER_GTOP2_ENABLED = "provider-gtop2-enabled"; +static const char *KEY_PROVIDER_HDDTEMP_ENABLED = "provider-hddtemp-enabled"; +static const char *KEY_PROVIDER_LIBATASMART_ENABLED += "provider-libatasmart-enabled"; +static const char *KEY_PROVIDER_NVCTRL_ENABLED = "provider-nvctrl-enabled"; +static const char *KEY_PROVIDER_UDISKS2_ENABLED = "provider-udisks2-enabled"; + +static const char *KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE += "default-high-threshold-temperature"; +static const char *KEY_DEFAULT_SENSOR_ALARM_ENABLED += "default-sensor-alarm-enabled"; + static GSettings *settings; static char *user_dir; @@ -119,6 +132,8 @@ static GKeyFile *key_file; static char *sensor_config_path; +static void (*slog_enabled_cbk)(void *); + static char *get_string(const char *key) { return g_settings_get_string(settings, key); @@ -159,7 +174,7 @@ static int get_int(const char *k) return g_settings_get_int(settings, k); } -char *config_get_notif_script() +char *config_get_notif_script(void) { char *str; @@ -180,7 +195,7 @@ void config_set_notif_script(const char *str) set_string(KEY_NOTIFICATION_SCRIPT, ""); } -static struct color *get_background_color() +static struct color *get_background_color(void) { char *scolor; struct color *c; @@ -191,12 +206,12 @@ static struct color *get_background_color() free(scolor); if (!c) - return color_new(0xffff, 0xffff, 0xffff); + return color_new(1, 1, 1); return c; } -static struct color *get_foreground_color() +static struct color *get_foreground_color(void) { char *scolor; struct color *c; @@ -207,12 +222,12 @@ static struct color *get_foreground_color() free(scolor); if (!c) - return color_new(0x0000, 0x0000, 0x0000); + return color_new(0, 0, 0); return c; } -static bool is_alpha_channel_enabled() +static bool is_alpha_channel_enabled(void) { return get_bool(KEY_ALPHA_CHANNEL_ENABLED); } @@ -222,17 +237,17 @@ static void set_alpha_channeld_enabled(bool b) set_bool(KEY_ALPHA_CHANNEL_ENABLED, b); } -static enum sensorlist_position get_sensorlist_position() +enum sensorlist_position config_get_sensorlist_position(void) { return get_int(KEY_INTERFACE_SENSORLIST_POSITION); } -static void set_sensorlist_position(enum sensorlist_position pos) +void config_set_sensorlist_position(enum sensorlist_position pos) { set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos); } -static double get_graph_background_alpha() +static double get_graph_background_alpha(void) { return get_double(KEY_GRAPH_BACKGROUND_ALPHA); } @@ -268,7 +283,7 @@ static void set_foreground_color(const struct color *color) free(str); } -bool is_slog_enabled() +bool is_slog_enabled(void) { return get_bool(KEY_SLOG_ENABLED); } @@ -278,22 +293,29 @@ static void set_slog_enabled(bool enabled) set_bool(KEY_SLOG_ENABLED, enabled); } -void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data) +static void slog_enabled_changed_cbk(GSettings *settings, + gchar *key, + gpointer data) +{ + if (slog_enabled_cbk) + slog_enabled_cbk(data); +} + +void config_set_slog_enabled_changed_cbk(void (*cbk)(void *), void *data) { - log_debug("config_slog_enabled_notify_add"); - gconf_client_add_dir(client, - KEY_SLOG_ENABLED, - GCONF_CLIENT_PRELOAD_NONE, - NULL); - gconf_client_notify_add(client, - KEY_SLOG_ENABLED, - cbk, - data, - NULL, - NULL); + log_fct_enter(); + + slog_enabled_cbk = cbk; + + g_signal_connect_after(settings, + "changed::slog-enabled", + G_CALLBACK(slog_enabled_changed_cbk), + data); + + log_fct_exit(); } -int config_get_slog_interval() +int config_get_slog_interval(void) { return get_int(KEY_SLOG_INTERVAL); } @@ -306,47 +328,60 @@ static void set_slog_interval(int interval) set_int(KEY_SLOG_INTERVAL, interval); } -static bool is_window_decoration_enabled() +bool config_is_window_decoration_enabled(void) { return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED); } -static bool is_window_keep_below_enabled() +bool config_is_window_keep_below_enabled(void) { return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED); } -static void set_window_decoration_enabled(bool enabled) +void config_set_window_decoration_enabled(bool enabled) { set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled); } -static void set_window_keep_below_enabled(bool enabled) +void config_set_window_keep_below_enabled(bool enabled) { set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled); } -/* - * Initializes the GConf client. - */ -static void init() +bool config_is_smooth_curves_enabled(void) +{ + return get_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED); +} + +void config_set_smooth_curves_enabled(bool b) +{ + set_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED, b); +} + +double config_get_default_high_threshold_temperature(void) +{ + return get_double(KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE); +} + +static bool config_get_default_sensor_alarm_enabled(void) +{ + return get_bool(KEY_DEFAULT_SENSOR_ALARM_ENABLED); +} + +static void init(void) { - if (!client) - client = gconf_client_get_default(); + log_fct_enter(); if (!settings) settings = g_settings_new("psensor"); + + log_fct_exit(); } -void config_cleanup() +void config_cleanup(void) { config_sync(); - if (client) { - g_object_unref(client); - client = NULL; - } - if (settings) { g_settings_sync(); g_object_unref(settings); @@ -367,9 +402,11 @@ void config_cleanup() free(sensor_config_path); sensor_config_path = NULL; } + + slog_enabled_cbk = NULL; } -struct config *config_load() +struct config *config_load(void) { struct config *c; @@ -381,14 +418,11 @@ struct config *config_load() c->graph_fgcolor = get_foreground_color(); c->graph_bg_alpha = get_graph_background_alpha(); c->alpha_channel_enabled = is_alpha_channel_enabled(); - c->sensorlist_position = get_sensorlist_position(); - c->window_decoration_enabled = is_window_decoration_enabled(); - c->window_keep_below_enabled = is_window_keep_below_enabled(); c->slog_enabled = is_slog_enabled(); c->slog_interval = config_get_slog_interval(); c->sensor_update_interval - = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL); + = get_int(KEY_SENSOR_UPDATE_INTERVAL); if (c->sensor_update_interval < 1) c->sensor_update_interval = 1; @@ -401,19 +435,6 @@ struct config *config_load() if (c->graph_monitoring_duration < 1) c->graph_monitoring_duration = 10; - c->sensor_values_max_length - = (c->graph_monitoring_duration * 60) / c->sensor_update_interval; - - if (c->sensor_values_max_length < 3) - c->sensor_values_max_length = 3; - - c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED); - - c->unity_launcher_count_disabled - = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED); - - c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP); - c->window_restore_enabled = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED); @@ -424,12 +445,14 @@ struct config *config_load() c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS); + c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP); + if (!c->window_restore_enabled || !c->window_w || !c->window_h) { c->window_w = 800; c->window_h = 200; } - c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT); + c->sensor_values_max_length = compute_values_max_length(c); return c; } @@ -440,9 +463,6 @@ void config_save(const struct config *c) set_background_color(c->graph_bgcolor); set_foreground_color(c->graph_fgcolor); set_graph_background_alpha(c->graph_bg_alpha); - set_sensorlist_position(c->sensorlist_position); - set_window_decoration_enabled(c->window_decoration_enabled); - set_window_keep_below_enabled(c->window_keep_below_enabled); set_slog_enabled(c->slog_enabled); set_slog_interval(c->slog_interval); @@ -450,14 +470,7 @@ void config_save(const struct config *c) set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration); - gconf_client_set_int(client, - KEY_SENSOR_UPDATE_INTERVAL, - c->sensor_update_interval, NULL); - - set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled); - - set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED, - c->unity_launcher_count_disabled); + set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval); set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup); @@ -470,11 +483,9 @@ void config_save(const struct config *c) set_int(KEY_INTERFACE_WINDOW_H, c->window_h); set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos); - - set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit); } -const char *get_psensor_user_dir() +const char *get_psensor_user_dir(void) { const char *home; @@ -503,7 +514,7 @@ const char *get_psensor_user_dir() return user_dir; } -static const char *get_sensor_config_path() +static const char *get_sensor_config_path(void) { const char *dir; @@ -517,7 +528,7 @@ static const char *get_sensor_config_path() return sensor_config_path; } -static GKeyFile *get_sensor_key_file() +static GKeyFile *get_sensor_key_file(void) { int ret; GError *err; @@ -536,20 +547,17 @@ static GKeyFile *get_sensor_key_file() &err); if (!ret) { - if (err->code == G_KEY_FILE_ERROR_NOT_FOUND) - log_fct(_("The configuration file " - "does not exist.")); - else - log_err(_("Failed to parse configuration " - "file: %s"), - path); + log_warn(_("Failed to load configuration file %s: %s"), + path, + err->message); + g_error_free(err); } } return key_file; } -static void save_sensor_key_file() +static void save_sensor_key_file(void) { GKeyFile *kfile; const char *path; @@ -571,7 +579,7 @@ static void save_sensor_key_file() log_fct_exit(); } -void config_sync() +void config_sync(void) { log_fct_enter(); if (settings) @@ -596,12 +604,65 @@ static char *sensor_get_str(const char *sid, const char *att) return g_key_file_get_string(kfile, sid, att, NULL); } -static bool sensor_get_bool(const char *sid, const char *att) +static bool sensor_get_double(const char *sid, + const char *att, + double *d, + double d_default) { GKeyFile *kfile; + GError *err; + double v; kfile = get_sensor_key_file(); - return g_key_file_get_boolean(kfile, sid, att, NULL); + + err = NULL; + if (!g_key_file_has_key(kfile, sid, att, &err)) { + if (err) { + log_err(err->message); + g_error_free(err); + return false; + } + + *d = d_default; + return true; + } + + err = NULL; + v = g_key_file_get_double(kfile, sid, att, &err); + + if (err) { + log_err(err->message); + + g_error_free(err); + + return false; + } + + *d = v; + return true; +} + +static bool sensor_get_bool(const char *sid, const char *att, bool dft) +{ + GKeyFile *kfile; + GError *err; + bool ret; + + kfile = get_sensor_key_file(); + + err = NULL; + ret = g_key_file_get_boolean(kfile, sid, att, &err); + + if (err) { + if (err->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND) + ret = dft; + else + log_err(err->message); + + g_error_free(err); + } + + return ret; } static void sensor_set_bool(const char *sid, const char *att, bool enabled) @@ -640,53 +701,97 @@ void config_set_sensor_name(const char *sid, const char *name) sensor_set_str(sid, ATT_SENSOR_NAME, name); } -void config_set_sensor_color(const char *sid, const struct color *color) -{ - char *scolor; - - scolor = color_to_str(color); - - sensor_set_str(sid, ATT_SENSOR_COLOR, scolor); +void config_set_sensor_color(const char *sid, const GdkRGBA *color) +{ + gchar *str; + + str = gdk_rgba_to_string(color); + + sensor_set_str(sid, ATT_SENSOR_COLOR, str); + + g_free(str); +} + +static const char *next_default_color(void) +{ + /* copied from the default colors of the gtk color color + * chooser. + */ + const char *default_colors[27] = { + "#ef2929", /* Scarlet Red */ + "#fcaf3e", /* Orange */ + "#fce94f", /* Butter */ + "#8ae234", /* Chameleon */ + "#729fcf", /* Sky Blue */ + "#ad7fa8", /* Plum */ + "#e9b96e", /* Chocolate */ + "#888a85", /* Aluminum 1 */ + "#eeeeec", /* Aluminum 2 */ + "#cc0000", + "#f57900", + "#edd400", + "#73d216", + "#3465a4", + "#75507b", + "#c17d11", + "#555753", + "#d3d7cf", + "#a40000", + "#ce5c00", + "#c4a000", + "#4e9a06", + "#204a87", + "#5c3566", + "#8f5902", + "#2e3436", + "#babdb6" + }; + static int next_idx; + const char *c; + + c = default_colors[next_idx % 27]; + + next_idx++; - free(scolor); + return c; } -struct color * -config_get_sensor_color(const char *sid, const struct color *dft) +GdkRGBA *config_get_sensor_color(const char *sid) { - char *scolor; - struct color *color; - - scolor = sensor_get_str(sid, ATT_SENSOR_COLOR); + GdkRGBA rgba; + char *str; + gboolean ret; - if (scolor) - color = str_to_color(scolor); - else - color = NULL; + str = sensor_get_str(sid, ATT_SENSOR_COLOR); - if (!color) { - color = color_new(dft->red, dft->green, dft->blue); - config_set_sensor_color(sid, color); + if (str) { + ret = gdk_rgba_parse(&rgba, str); + free(str); } - free(scolor); + if (!str || !ret) { + gdk_rgba_parse(&rgba, next_default_color()); + config_set_sensor_color(sid, &rgba); + } - return color; + return gdk_rgba_copy(&rgba); } -bool config_is_sensor_enabled(const char *sid) +bool config_is_sensor_graph_enabled(const char *sid) { - return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED); + return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED, false); } -void config_set_sensor_enabled(const char *sid, bool enabled) +void config_set_sensor_graph_enabled(const char *sid, bool enabled) { sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled); } -int config_get_sensor_alarm_high_threshold(const char *sid) +bool config_get_sensor_alarm_high_threshold(const char *sid, + double *v, + double d) { - return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD); + return sensor_get_double(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, v, d); } void config_set_sensor_alarm_high_threshold(const char *sid, int threshold) @@ -694,9 +799,9 @@ void config_set_sensor_alarm_high_threshold(const char *sid, int threshold) sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold); } -int config_get_sensor_alarm_low_threshold(const char *sid) +bool config_get_sensor_alarm_low_threshold(const char *sid, double *v, double d) { - return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD); + return sensor_get_double(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, v, d); } void config_set_sensor_alarm_low_threshold(const char *sid, int threshold) @@ -706,14 +811,16 @@ void config_set_sensor_alarm_low_threshold(const char *sid, int threshold) bool config_is_appindicator_enabled(const char *sid) { - return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED); + return !sensor_get_bool(sid, + ATT_SENSOR_APPINDICATOR_MENU_DISABLED, + false); } void config_set_appindicator_enabled(const char *sid, bool enabled) { - return sensor_set_bool(sid, - ATT_SENSOR_APPINDICATOR_MENU_DISABLED, - !enabled); + sensor_set_bool(sid, + ATT_SENSOR_APPINDICATOR_MENU_DISABLED, + !enabled); } int config_get_sensor_position(const char *sid) @@ -723,12 +830,12 @@ int config_get_sensor_position(const char *sid) void config_set_sensor_position(const char *sid, int pos) { - return sensor_set_int(sid, ATT_SENSOR_POSITION, pos); + sensor_set_int(sid, ATT_SENSOR_POSITION, pos); } bool config_get_sensor_alarm_enabled(const char *sid) { - return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED); + return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED, false); } void config_set_sensor_alarm_enabled(const char *sid, bool enabled) @@ -736,12 +843,131 @@ void config_set_sensor_alarm_enabled(const char *sid, bool enabled) sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled); } +bool config_is_sensor_enabled(const char *sid) +{ + return !sensor_get_bool(sid, + ATT_SENSOR_HIDE, + config_get_default_sensor_alarm_enabled()); +} + +void config_set_sensor_enabled(const char *sid, bool enabled) +{ + sensor_set_bool(sid, ATT_SENSOR_HIDE, !enabled); +} + bool config_is_appindicator_label_enabled(const char *sid) { - return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED); + return sensor_get_bool(sid, + ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, + false); } void config_set_appindicator_label_enabled(const char *sid, bool enabled) { sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled); } + +GSettings *config_get_GSettings(void) +{ + return settings; +} + +bool config_is_lmsensor_enabled(void) +{ + return get_bool(KEY_PROVIDER_LMSENSORS_ENABLED); +} + +bool config_is_gtop2_enabled(void) +{ + return get_bool(KEY_PROVIDER_GTOP2_ENABLED); +} + +bool config_is_udisks2_enabled(void) +{ + return get_bool(KEY_PROVIDER_UDISKS2_ENABLED); +} + +bool config_is_hddtemp_enabled(void) +{ + return get_bool(KEY_PROVIDER_HDDTEMP_ENABLED); +} + +bool config_is_libatasmart_enabled(void) +{ + return get_bool(KEY_PROVIDER_LIBATASMART_ENABLED); +} + +bool config_is_nvctrl_enabled(void) +{ + return get_bool(KEY_PROVIDER_NVCTRL_ENABLED); +} + +bool config_is_atiadlsdk_enabled(void) +{ + return get_bool(KEY_PROVIDER_ATIADLSDK_ENABLED); +} + +void config_set_lmsensor_enable(bool b) +{ + set_bool(KEY_PROVIDER_LMSENSORS_ENABLED, b); +} + +void config_set_nvctrl_enable(bool b) +{ + set_bool(KEY_PROVIDER_NVCTRL_ENABLED, b); +} + +void config_set_atiadlsdk_enable(bool b) +{ + set_bool(KEY_PROVIDER_ATIADLSDK_ENABLED, b); +} + +void config_set_gtop2_enable(bool b) +{ + set_bool(KEY_PROVIDER_GTOP2_ENABLED, b); +} + +void config_set_hddtemp_enable(bool b) +{ + set_bool(KEY_PROVIDER_HDDTEMP_ENABLED, b); +} + +void config_set_libatasmart_enable(bool b) +{ + set_bool(KEY_PROVIDER_LIBATASMART_ENABLED, b); +} + +void config_set_udisks2_enable(bool b) +{ + set_bool(KEY_PROVIDER_UDISKS2_ENABLED, b); +} + +enum temperature_unit config_get_temperature_unit(void) +{ + return get_int(KEY_INTERFACE_TEMPERATURE_UNIT); +} + +void config_set_temperature_unit(enum temperature_unit u) +{ + set_int(KEY_INTERFACE_TEMPERATURE_UNIT, u); +} + +bool config_is_menu_bar_enabled(void) +{ + return !get_bool(KEY_INTERFACE_MENU_BAR_DISABLED); +} + +void config_set_menu_bar_enabled(bool enabled) +{ + set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, !enabled); +} + +bool config_is_count_visible(void) +{ + return !get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED); +} + +void config_set_count_visible(bool visible) +{ + set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED, !visible); +}