Skip to content

Commit

Permalink
Add dbus method and mcetool option for requesting settings reset
Browse files Browse the repository at this point in the history
Allows resetting mce settings back to defaults without needing to
know what the defaults are and/or stopping mce service.

To reset all settings to defaults, use:
  mcetool --reset-settings

To reset for example only display related settings, use:
  mcetool --reset-settings=/display/

[mce] Add dbus method for requesting settings reset. Fixes JB#24286
  • Loading branch information
spiiroin committed Feb 17, 2015
1 parent 11867bb commit 72f83bd
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .depend
Expand Up @@ -4,6 +4,7 @@ builtin-gconf.o:\
mce-dbus.h\
mce-io.h\
mce-log.h\
modules/display.h\
modules/filter-brightness-als.h\
modules/memnotify.h\
powerkey.h\
Expand All @@ -15,6 +16,7 @@ builtin-gconf.pic.o:\
mce-dbus.h\
mce-io.h\
mce-log.h\
modules/display.h\
modules/filter-brightness-als.h\
modules/memnotify.h\
powerkey.h\
Expand Down
86 changes: 80 additions & 6 deletions builtin-gconf.c
Expand Up @@ -27,6 +27,7 @@

#include "modules/memnotify.h"
#include "modules/filter-brightness-als.h"
#include "modules/display.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -107,6 +108,7 @@ static void gconf_entry_free(GConfEntry *self);
static void gconf_entry_free_cb(gpointer self);
const char *gconf_entry_get_key(const GConfEntry *entry);
GConfValue *gconf_entry_get_value(const GConfEntry *entry);
static const char *gconf_entry_get_default(const GConfEntry *entry);
#if GCONF_ENABLE_DEBUG_LOGGING
static void gconf_client_debug(GConfClient *self);
#endif
Expand Down Expand Up @@ -1038,6 +1040,34 @@ gconf_entry_get_value(const GConfEntry *entry)
return entry ? entry->value : 0;
}

/** Get reset default value for config entry
*/
static const char *gconf_entry_get_default(const GConfEntry *entry)
{
/* Brightness values are a special because the config
* defaults use legacy 1-5 range and are migrated to
* current 1-100 range on mce startup. */

if( !strcmp(entry->key, MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_SIZE) )
{
return "1";
}

if( !strcmp(entry->key, MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_COUNT) )
{
return "100";
}

if( !strcmp(entry->key, MCE_GCONF_DISPLAY_BRIGHTNESS) )
{
return "60";
}

/* But the rest just uses whatever the default config is */

return entry->def;
}

/* ========================================================================= *
*
* DATABASE
Expand Down Expand Up @@ -1121,20 +1151,17 @@ static const setting_t gconf_defaults[] =
.def = "0",
},
{
// MCE_GCONF_DISPLAY_BRIGHTNESS @ modules/display.h
.key = "/system/osso/dsm/display/display_brightness",
.key = MCE_GCONF_DISPLAY_BRIGHTNESS,
.type = "i",
.def = "3", // Note: Legacy value, migrated at mce startup
},
{
// MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_SIZE @ modules/display.h
.key = "/system/osso/dsm/display/display_brightness_level_step",
.key = MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_SIZE,
.type = "i",
.def = "1", // Note: Legacy value, migrated at mce startup
},
{
// MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_COUNT @ modules/display.h
.key = "/system/osso/dsm/display/max_display_brightness_levels",
.key = MCE_GCONF_DISPLAY_BRIGHTNESS_LEVEL_COUNT,
.type = "i",
.def = "5", // Note: Legacy value, migrated at mce startup
},
Expand Down Expand Up @@ -1796,6 +1823,53 @@ static void gconf_client_mark_defaults(GConfClient *self)
}
}

/** Reset to configured default values
*/
int gconf_client_reset_defaults(GConfClient *self, const char *keyish)
{
int result = 0;
GSList *changed = 0;

/* Reset all values first */
for( GSList *e_iter = self->entries; e_iter; e_iter = e_iter->next )
{
GConfEntry *entry = e_iter->data;

if( keyish && !strstr(entry->key, keyish) )
{
continue;
}

const char *def = gconf_entry_get_default(entry);

if( def )
{
char *str = gconf_value_str(entry->value);

if( !str || strcmp(str, def) )
{
mce_log(LL_DEBUG, "%s: %s -> %s", entry->key, str, def);

gconf_value_set_from_string(entry->value, def);
changed = g_slist_prepend(changed, entry->key);
++result;
}
}
}

/* Then send change notifications */

changed = g_slist_reverse(changed);
for( GSList *item = changed; item; item = item->next )
{
const char *key = item->data;
gconf_client_notify_change(self, key);
}
g_slist_free(changed);

return result;
}

static void gconf_client_free_default(void)
{
if( default_client )
Expand Down
1 change: 1 addition & 0 deletions builtin-gconf.h
Expand Up @@ -153,6 +153,7 @@ void gconf_value_set_list(GConfValue *self, GSList *list);
const char *gconf_entry_get_key(const GConfEntry *entry);
GConfValue *gconf_entry_get_value(const GConfEntry *entry);
GConfClient *gconf_client_get_default(void);
int gconf_client_reset_defaults(GConfClient *self, const char *keyish);
void gconf_client_add_dir(GConfClient *client, const gchar *dir, GConfClientPreloadType preload, GError **err);
GConfValue *gconf_client_get(GConfClient *self, const gchar *key, GError **err);
gboolean gconf_client_set_bool(GConfClient *client, const gchar *key, gboolean val, GError **err);
Expand Down
65 changes: 65 additions & 0 deletions mce-dbus.c
Expand Up @@ -1268,6 +1268,62 @@ static GSList *value_list_from_float_array(DBusMessageIter *iter)
return res;
}

/**
* D-Bus callback for the config reset method call
*
* @param msg The D-Bus message to reply to
*
* @return TRUE
*/
static gboolean config_reset_dbus_cb(DBusMessage *const msg)
{
GConfClient *client = 0;
DBusError error = DBUS_ERROR_INIT;
const char *keyish = 0;
dbus_int32_t count = -1;
DBusMessage *reply = 0;

mce_log(LL_DEVEL, "Received configuration reset request");

if( !(client = gconf_client_get_default()) )
goto EXIT;

if( !dbus_message_get_args(msg, &error,
DBUS_TYPE_STRING, &keyish,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "%s: %s", error.name, error.message);
goto EXIT;
}

count = gconf_client_reset_defaults(client, keyish);

/* sync to disk if we changed something */
if( count > 0 ) {
GError *err = 0;
gconf_client_suggest_sync(client, &err);
if( err )
mce_log(LL_ERR, "gconf_client_suggest_sync: %s",
err->message);
g_clear_error(&err);
}

EXIT:
if( dbus_message_get_no_reply(msg) )
goto NOREPLY;

if( !(reply = dbus_new_method_reply(msg)) )
goto NOREPLY;

dbus_message_append_args(reply,
DBUS_TYPE_INT32, &count,
DBUS_TYPE_INVALID);

dbus_send_message(reply), reply = 0;

NOREPLY:
return TRUE;
}

/**
* D-Bus callback for the config set method call
*
Expand Down Expand Up @@ -3517,6 +3573,15 @@ static mce_dbus_handler_t mce_dbus_handlers[] =
" <arg direction=\"in\" name=\"key_value\" type=\"v\"/>\n"
" <arg direction=\"out\" name=\"success\" type=\"b\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_CONFIG_RESET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = config_reset_dbus_cb,
.args =
" <arg direction=\"in\" name=\"key_part\" type=\"s\"/>\n"
" <arg direction=\"out\" name=\"count\" type=\"i\"/>\n"
},
{
.interface = DBUS_INTERFACE_INTROSPECTABLE,
.name = "Introspect",
Expand Down
2 changes: 1 addition & 1 deletion rpm/mce.spec
Expand Up @@ -18,7 +18,7 @@ BuildRequires: pkgconfig(dbus-1) >= 1.0.2
BuildRequires: pkgconfig(dbus-glib-1)
BuildRequires: pkgconfig(dsme) >= 0.58
BuildRequires: pkgconfig(glib-2.0) >= 2.36.0
BuildRequires: pkgconfig(mce) >= 1.14.0
BuildRequires: pkgconfig(mce) >= 1.15.0
BuildRequires: pkgconfig(libsystemd-daemon)
BuildRequires: kernel-headers >= 2.6.32
BuildRequires: systemd
Expand Down
27 changes: 27 additions & 0 deletions tools/mcetool.c
Expand Up @@ -2263,6 +2263,21 @@ static void xmce_get_cabc_mode(void)
free(str);
}

/* ------------------------------------------------------------------------- *
* config reset
* ------------------------------------------------------------------------- */

static bool xmce_reset_settings(const char *args)
{
if( !args )
args = "/";

xmce_ipc_no_reply(MCE_CONFIG_RESET,
DBUS_TYPE_STRING, &args,
DBUS_TYPE_INVALID);
return true;
}

/* ------------------------------------------------------------------------- *
* dim timeout
* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -4876,6 +4891,18 @@ static const mce_opt_t options[] =
.usage =
"set critical limit for active memory pages; zero=disabled\n"
},
{
.name = "reset-settings",
.without_arg = xmce_reset_settings,
.with_arg = xmce_reset_settings,
.values = "keyish",
.usage =
"reset matching settings back to configuration default.\n"
"\n"
"All settings whose key name contains the given subpart\n"
"will be reset to defaults set in /etc/mce/*.conf files.\n"
"If no keyish is given, all settings are reset.\n"
},

// sentinel
{
Expand Down

0 comments on commit 72f83bd

Please sign in to comment.