Fix double unlock bug and some memory leaks

pull/3/head
Werner Sembach 4 years ago
parent 3f349b2c8c
commit 6ae444aa1d

@ -28,7 +28,10 @@
using std::cerr; using std::cerr;
using std::endl; using std::endl;
static int lockfile = -1; static int lockfile;
static GSettings *touchpad_settings = NULL;
static GDBusProxy *session_manager_properties = NULL;
static GDBusProxy *display_config_properties = NULL;
static void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) { static void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) {
const gchar *send_events_string = g_settings_get_string(settings, key); const gchar *send_events_string = g_settings_get_string(settings, key);
@ -90,7 +93,7 @@ int setup_gnome(int lockfile_arg) {
lockfile = lockfile_arg; lockfile = lockfile_arg;
// get a new glib settings context to read the touchpad configuration of the current user // get a new glib settings context to read the touchpad configuration of the current user
GSettings *touchpad_settings = g_settings_new("org.gnome.desktop.peripherals.touchpad"); touchpad_settings = g_settings_new("org.gnome.desktop.peripherals.touchpad");
if (!touchpad_settings) { if (!touchpad_settings) {
cerr << "main(...): g_settings_new(...) failed." << endl; cerr << "main(...): g_settings_new(...) failed." << endl;
return EXIT_FAILURE; return EXIT_FAILURE;
@ -140,3 +143,9 @@ int setup_gnome(int lockfile_arg) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void clean_gnome() {
g_clear_object(&session_manager_properties);
g_clear_object(&display_config_properties);
g_clear_object(&touchpad_settings);
}

@ -18,3 +18,4 @@
#pragma once #pragma once
int setup_gnome(int lockfile); int setup_gnome(int lockfile);
void clean_gnome();

@ -31,6 +31,8 @@ using std::endl;
int lockfile; int lockfile;
gboolean isMousePluggedInPrev; gboolean isMousePluggedInPrev;
gboolean isEnabledSave; gboolean isEnabledSave;
GDBusProxy *kded5_modules_touchpad = NULL;
GDBusProxy *solid_power_management = NULL;
static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unused)) char *sender_name, char *signal_name, GVariant *parameters, __attribute__((unused)) gpointer user_data) { static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unused)) char *sender_name, char *signal_name, GVariant *parameters, __attribute__((unused)) gpointer user_data) {
if (!strcmp("enabledChanged", signal_name) && g_variant_is_of_type(parameters, (const GVariantType *)"(b)") && g_variant_n_children(parameters)) { if (!strcmp("enabledChanged", signal_name) && g_variant_is_of_type(parameters, (const GVariantType *)"(b)") && g_variant_n_children(parameters)) {
@ -53,7 +55,7 @@ static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unu
GVariant *isMousePluggedInParam = g_dbus_proxy_call_sync(proxy, "isMousePluggedIn", NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, NULL); GVariant *isMousePluggedInParam = g_dbus_proxy_call_sync(proxy, "isMousePluggedIn", NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, NULL);
if (isMousePluggedInParam != NULL && g_variant_is_of_type(isMousePluggedInParam, (const GVariantType *)"(b)") && g_variant_n_children(isMousePluggedInParam)) { if (isMousePluggedInParam != NULL && g_variant_is_of_type(isMousePluggedInParam, (const GVariantType *)"(b)") && g_variant_n_children(isMousePluggedInParam)) {
GVariant *isMousePluggedIn = g_variant_get_child_value(isMousePluggedInParam, 0); GVariant *isMousePluggedIn = g_variant_get_child_value(isMousePluggedInParam, 0);
if (!g_variant_get_boolean(isMousePluggedIn)) { if (isMousePluggedInPrev && !g_variant_get_boolean(isMousePluggedIn)) {
if (set_touchpad_state(1)) { if (set_touchpad_state(1)) {
cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl;
} }
@ -61,7 +63,7 @@ static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unu
cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl;
} }
} }
else if (!isMousePluggedInPrev) { else if (!isMousePluggedInPrev && g_variant_get_boolean(isMousePluggedIn)) {
if (flock(lockfile, LOCK_EX)) { if (flock(lockfile, LOCK_EX)) {
cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl;
} }
@ -199,3 +201,8 @@ int setup_kde(int lockfile_arg) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void clean_kde() {
g_clear_object(&kded5_modules_touchpad);
g_clear_object(&solid_power_management);
}

@ -18,3 +18,4 @@
#pragma once #pragma once
int setup_kde(int lockfile); int setup_kde(int lockfile);
void clean_kde();

@ -38,6 +38,10 @@ static int lockfile = -1;
static void gracefull_exit(int signum = 0) { static void gracefull_exit(int signum = 0) {
int result = EXIT_SUCCESS; int result = EXIT_SUCCESS;
clean_gnome();
clean_kde();
if (signum < 0) { if (signum < 0) {
result = EXIT_FAILURE; result = EXIT_FAILURE;
} }
@ -108,6 +112,7 @@ int main() {
g_main_loop_run(app); g_main_loop_run(app);
// g_main_loop_run only returns on error // g_main_loop_run only returns on error
g_clear_object(&app);
cerr << "main(...): g_main_loop_run(...) failed." << endl; cerr << "main(...): g_main_loop_run(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE); gracefull_exit(-EXIT_FAILURE);
} }

Loading…
Cancel
Save