Skip to content

Commit

Permalink
Make mce_gconf_notifier_remove() friendlier for normal uses
Browse files Browse the repository at this point in the history
No need to litter code with GPOINTER_TO_INT() and dummy parameters
just to make the function compatible with g_slist_foreach()

Add separate helper function mce_gconf_notifier_remove_cb() for the
one exceptional case that actually uses g_slist_foreach().

Also make sure mce_gconf_notifier_add() always sets the resulting
notification id before returning to the caller and allow calling
mce_gconf_notifier_remove() with zero id without ill effects.
  • Loading branch information
spiiroin committed Jun 16, 2014
1 parent 2fd8c71 commit 14bc236
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 45 deletions.
8 changes: 2 additions & 6 deletions event-input.c
Expand Up @@ -1374,10 +1374,8 @@ static void ts_grab_init(void)
*/
static void ts_grab_quit(void)
{
if( ts_grab_release_delay_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(ts_grab_release_delay_id), 0);
mce_gconf_notifier_remove(ts_grab_release_delay_id),
ts_grab_release_delay_id = 0;
}

input_grab_reset(&ts_grab_state);
}
Expand Down Expand Up @@ -2682,10 +2680,8 @@ void mce_input_exit(void)
{
#ifdef ENABLE_DOUBLETAP_EMULATION
/* Remove fake doubletap policy change notifier */
if( fake_doubletap_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(fake_doubletap_id), 0);
mce_gconf_notifier_remove(fake_doubletap_id),
fake_doubletap_id = 0;
}
#endif

/* Remove triggers/filters from datapipes */
Expand Down
47 changes: 33 additions & 14 deletions mce-gconf.c
Expand Up @@ -297,7 +297,8 @@ gboolean mce_gconf_get_string(const gchar *const key, gchar **value)
* @param path The GConf directory to watch
* @param key The GConf key to add the notifier for
* @param callback The callback function
* @param[out] cb_id Will contain the callback ID on return
* @param[out] cb_id Will contain the callback ID or zero on return
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_gconf_notifier_add(const gchar *path, const gchar *key,
Expand All @@ -306,6 +307,7 @@ gboolean mce_gconf_notifier_add(const gchar *path, const gchar *key,
{
GError *error = NULL;
gboolean status = FALSE;
guint id = 0;

if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s notifier", key);
Expand All @@ -329,45 +331,62 @@ gboolean mce_gconf_notifier_add(const gchar *path, const gchar *key,

g_clear_error(&error);

*cb_id = gconf_client_notify_add(gconf_client, key, callback,
NULL, NULL, &error);
id = gconf_client_notify_add(gconf_client, key, callback,
NULL, NULL, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not register notifier for %s; %s",
key, error->message);
//goto EXIT;
}

gconf_notifiers = g_slist_prepend(gconf_notifiers,
GINT_TO_POINTER(*cb_id));
if( id )
gconf_notifiers = g_slist_prepend(gconf_notifiers,
GINT_TO_POINTER(id));

// FIXME: fix the bogus success return
status = TRUE;

EXIT:
g_clear_error(&error);

return status;
return *cb_id = id, status;
}

/**
* Remove a GConf notifier
*
* @param cb_id The ID of the notifier to remove
* @param user_data Unused
* Calling with zero id is allowed and does nothing
*
* @param id The ID of the notifier to remove, or zero
*/
void mce_gconf_notifier_remove(gpointer cb_id, gpointer user_data)
void mce_gconf_notifier_remove(guint id)
{
(void)user_data;

if( gconf_disabled )
goto EXIT;

gconf_client_notify_remove(gconf_client, GPOINTER_TO_INT(cb_id));
gconf_notifiers = g_slist_remove(gconf_notifiers, cb_id);
if( !id )
goto EXIT;

gconf_client_notify_remove(gconf_client, id);
gconf_notifiers = g_slist_remove(gconf_notifiers, GINT_TO_POINTER(id));

EXIT:
return;
}

/** Helper callback for removing GConf notifiers with g_slist_foreach
*
* @param cb_id The ID of the notifier to remove
* @param user_data Unused
*/
void mce_gconf_notifier_remove_cb(gpointer cb_id, gpointer user_data)
{
(void)user_data;

mce_gconf_notifier_remove(GPOINTER_TO_INT(cb_id));
}

/**
* Init function for the mce-gconf component
*
Expand Down Expand Up @@ -396,7 +415,7 @@ void mce_gconf_exit(void)
{
if( gconf_client ) {
/* Free the list of GConf notifiers */
g_slist_foreach(gconf_notifiers, mce_gconf_notifier_remove, 0);
g_slist_foreach(gconf_notifiers, mce_gconf_notifier_remove_cb, 0);
gconf_notifiers = 0;

/* Forget builtin-gconf client reference */
Expand Down
3 changes: 2 additions & 1 deletion mce-gconf.h
Expand Up @@ -32,7 +32,8 @@ gboolean mce_gconf_set_string(const gchar *const key, const gchar *const value);
gboolean mce_gconf_notifier_add(const gchar *path, const gchar *key,
const GConfClientNotifyFunc callback,
guint *cb_id);
void mce_gconf_notifier_remove(gpointer cb_id, gpointer user_data);
void mce_gconf_notifier_remove(guint id);
void mce_gconf_notifier_remove_cb(gpointer cb_id, gpointer user_data);

gboolean mce_gconf_init(void);
void mce_gconf_exit(void);
Expand Down
8 changes: 2 additions & 6 deletions modules/display.c
Expand Up @@ -7538,18 +7538,14 @@ void g_module_unload(GModule *module)

#ifdef ENABLE_WAKELOCKS
/* Remove suspend policy change notifier */
if( mdy_suspend_policy_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(mdy_suspend_policy_id), 0);
mce_gconf_notifier_remove(mdy_suspend_policy_id),
mdy_suspend_policy_id = 0;
}
#endif

#ifdef ENABLE_CPU_GOVERNOR
/* Remove cpu scaling governor change notifier */
if( mdy_governor_conf_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(mdy_governor_conf_id), 0);
mce_gconf_notifier_remove(mdy_governor_conf_id),
mdy_governor_conf_id = 0;
}

/* Switch back to defaults */
mdy_governor_rethink();
Expand Down
11 changes: 4 additions & 7 deletions modules/led.c
Expand Up @@ -2783,15 +2783,12 @@ static void sw_breathing_gconf_cb(GConfClient *const gcc, const guint id,
*/
static void sw_breathing_quit(void)
{
if( sw_breathing_battery_limit_gconf_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(sw_breathing_battery_limit_gconf_id), 0);
mce_gconf_notifier_remove(sw_breathing_battery_limit_gconf_id),
sw_breathing_battery_limit_gconf_id = 0;
}

if( sw_breathing_enabled_gconf_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(sw_breathing_enabled_gconf_id), 0);
mce_gconf_notifier_remove(sw_breathing_enabled_gconf_id),
sw_breathing_enabled_gconf_id = 0;
}

allow_sw_breathing(false);
}

Expand Down Expand Up @@ -3026,7 +3023,7 @@ void g_module_unload(GModule *module)
pattern_struct *psp;

while ((psp = g_queue_pop_head(pattern_stack)) != NULL) {
mce_gconf_notifier_remove(GINT_TO_POINTER(psp->gconf_cb_id), NULL);
mce_gconf_notifier_remove(psp->gconf_cb_id);
free(psp->name);
psp->name = NULL;
g_slice_free(pattern_struct, psp);
Expand Down
4 changes: 1 addition & 3 deletions powerkey.c
Expand Up @@ -174,10 +174,8 @@ static void powerkey_gconf_init(void)
static void powerkey_gconf_quit(void)
{
/* Power key press handling mode */
if( powerkey_action_mode_cb_id ) {
mce_gconf_notifier_remove(GINT_TO_POINTER(powerkey_action_mode_cb_id), 0);
mce_gconf_notifier_remove(powerkey_action_mode_cb_id),
powerkey_action_mode_cb_id = 0;
}
}

/** Helper for sending powerkey feedback dbus signal
Expand Down
16 changes: 8 additions & 8 deletions tklock.c
Expand Up @@ -3246,17 +3246,17 @@ static void tklock_gconf_init(void)
*/
static void tklock_gconf_quit(void)
{
if( doubletap_gesture_policy_cb_id )
mce_gconf_notifier_remove(GINT_TO_POINTER(doubletap_gesture_policy_cb_id), 0);
mce_gconf_notifier_remove(doubletap_gesture_policy_cb_id),
doubletap_gesture_policy_cb_id = 0;

if( tk_autolock_enabled_cb_id )
mce_gconf_notifier_remove(GINT_TO_POINTER(tk_autolock_enabled_cb_id), 0);
mce_gconf_notifier_remove(tk_autolock_enabled_cb_id),
tk_autolock_enabled_cb_id = 0;

if( tklock_blank_disable_id )
mce_gconf_notifier_remove(GINT_TO_POINTER(tklock_blank_disable_id), 0);
mce_gconf_notifier_remove(tklock_blank_disable_id),
tklock_blank_disable_id = 0;

if( doubletap_enable_mode_cb_id )
mce_gconf_notifier_remove(GINT_TO_POINTER(doubletap_enable_mode_cb_id), 0);
mce_gconf_notifier_remove(doubletap_enable_mode_cb_id),
doubletap_enable_mode_cb_id = 0;
}

/* ========================================================================= *
Expand Down

0 comments on commit 14bc236

Please sign in to comment.