diff --git a/setup-kde.cpp b/setup-kde.cpp index b4f9980..95fd141 100644 --- a/setup-kde.cpp +++ b/setup-kde.cpp @@ -31,22 +31,22 @@ using std::endl; int lockfile; gboolean isMousePluggedInPrev; gboolean isEnabledSave; -GDBusProxy *kded5_modules_touchpad = NULL; +GDBusProxy *kded_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 kded_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)) { GVariant *enabledChanged = g_variant_get_child_value(parameters, 0); isEnabledSave = g_variant_get_boolean(enabledChanged); if (isEnabledSave) { if (set_touchpad_state(1)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } } else { if (set_touchpad_state(0)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } } g_variant_unref(enabledChanged); @@ -57,18 +57,18 @@ static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unu GVariant *isMousePluggedIn = g_variant_get_child_value(isMousePluggedInParam, 0); if (isMousePluggedInPrev && !g_variant_get_boolean(isMousePluggedIn)) { if (set_touchpad_state(1)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } if (flock(lockfile, LOCK_UN)) { - cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): flock(...) failed." << endl; } } else if (!isMousePluggedInPrev && g_variant_get_boolean(isMousePluggedIn)) { if (flock(lockfile, LOCK_EX)) { - cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): flock(...) failed." << endl; } if (set_touchpad_state(isEnabledSave)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } } isMousePluggedInPrev = g_variant_get_boolean(isMousePluggedIn); @@ -76,7 +76,7 @@ static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unu g_variant_unref(isMousePluggedInParam); } else { - cerr << "kded5_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; } } } @@ -84,23 +84,23 @@ static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unu static void solid_power_management_handler(__attribute__((unused)) GDBusProxy *proxy, __attribute__((unused)) char *sender_name, char *signal_name, __attribute__((unused)) GVariant *parameters, __attribute__((unused)) gpointer user_data) { if (!strcmp("aboutToSuspend", signal_name)) { if (set_touchpad_state(1)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } if (flock(lockfile, LOCK_UN)) { - cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): flock(...) failed." << endl; } } else if (!strcmp("resumingFromSuspend", signal_name)) { if (flock(lockfile, LOCK_EX)) { - cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): flock(...) failed." << endl; } if (set_touchpad_state(isEnabledSave)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; } } } -static int kded5_modules_touchpad_init(GDBusProxy *proxy) { +static int kded_modules_touchpad_init(GDBusProxy *proxy) { GVariant *isEnabledParam = g_dbus_proxy_call_sync(proxy, "isEnabled", NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, NULL); if (isEnabledParam != NULL && g_variant_is_of_type(isEnabledParam, (const GVariantType *)"(b)") && g_variant_n_children(isEnabledParam)) { GVariant *isEnabled = g_variant_get_child_value(isEnabledParam, 0); @@ -109,13 +109,13 @@ static int kded5_modules_touchpad_init(GDBusProxy *proxy) { isEnabledSave = g_variant_get_boolean(isEnabled); if (isEnabledSave) { if (set_touchpad_state(1)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; return EXIT_FAILURE; } } else { if (set_touchpad_state(0)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; return EXIT_FAILURE; } } @@ -124,7 +124,7 @@ static int kded5_modules_touchpad_init(GDBusProxy *proxy) { g_variant_unref(isEnabledParam); } else { - cerr << "kded5_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; return EXIT_FAILURE; } @@ -138,11 +138,11 @@ static int kded5_modules_touchpad_init(GDBusProxy *proxy) { // isMousePluggedInPrev just got init so it holds the current value if (!isMousePluggedInPrev) { if (set_touchpad_state(1)) { - cerr << "kded5_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): set_touchpad_state(...) failed." << endl; return EXIT_FAILURE; } if (flock(lockfile, LOCK_UN)) { - cerr << "kded5_modules_touchpad_handler(...): flock(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): flock(...) failed." << endl; return EXIT_FAILURE; } } @@ -151,7 +151,7 @@ static int kded5_modules_touchpad_init(GDBusProxy *proxy) { g_variant_unref(isMousePluggedInParam); } else { - cerr << "kded5_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; + cerr << "kded_modules_touchpad_handler(...): g_dbus_proxy_call_sync(...) failed." << endl; return EXIT_FAILURE; } @@ -160,65 +160,59 @@ static int kded5_modules_touchpad_init(GDBusProxy *proxy) { int setup_kde(int lockfile_arg) { lockfile = lockfile_arg; - const char *object_path = NULL; - - GDBusProxy *kded5 = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, - G_DBUS_PROXY_FLAGS_NONE, NULL, - "org.kde.kded5", - "/kded", - "org.kde.kded5", - NULL, NULL); - if (kded5 == NULL) { - cerr << "setup_kde(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; - return EXIT_FAILURE; - } - GVariant *loadedModulesParam = g_dbus_proxy_call_sync(kded5, "loadedModules", NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, NULL); - if (loadedModulesParam != NULL && g_variant_is_of_type(loadedModulesParam, (const GVariantType *)"(as)") && g_variant_n_children(loadedModulesParam) ) { - GVariant *loadedModules = g_variant_get_child_value(loadedModulesParam, 0); - GVariantIter *iter; - gchar *str; - - g_variant_get(loadedModules, "as", &iter); - while (g_variant_iter_loop(iter, "s", &str)) { - if (strcmp(str, "kded_touchpad") == 0) { - object_path = "/modules/kded_touchpad"; - g_free(str); // g_variant_iter_loop needs freeing when break is used - break; - } - if (strcmp(str, "touchpad") == 0) { - object_path = "/modules/touchpad"; - g_free(str); // g_variant_iter_loop needs freeing when break is used - break; + + kded_modules_touchpad = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.kde.kded6", + "/modules/kded_touchpad", + "org.kde.touchpad", + NULL, NULL); + // call a random method to check if object exists + GVariant *isMousePluggedInParam = g_dbus_proxy_call_sync(kded_modules_touchpad, + "isMousePluggedIn", + NULL, G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, NULL, NULL); + if (isMousePluggedInParam == NULL) { + g_object_unref(kded_modules_touchpad); + g_variant_unref(isMousePluggedInParam); + kded_modules_touchpad = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.kde.kded5", + "/modules/kded_touchpad", + "org.kde.touchpad", + NULL, NULL); + // call a random method to check if object exists + isMousePluggedInParam = g_dbus_proxy_call_sync(kded_modules_touchpad, + "isMousePluggedIn", + NULL, G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, NULL, NULL); + if (isMousePluggedInParam == NULL) { + g_object_unref(kded_modules_touchpad); + g_variant_unref(isMousePluggedInParam); + kded_modules_touchpad = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.kde.kded5", + "/modules/touchpad", + "org.kde.touchpad", + NULL, NULL); + // call a random method to check if object exists + isMousePluggedInParam = g_dbus_proxy_call_sync(kded_modules_touchpad, + "isMousePluggedIn", + NULL, G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, NULL, NULL); + if (isMousePluggedInParam == NULL) { + g_clear_object(&kded_modules_touchpad); + g_variant_unref(isMousePluggedInParam); + cerr << "setup_kde(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; + return EXIT_FAILURE; } } - - g_variant_iter_free(iter); - g_variant_unref(loadedModules); - g_variant_unref(loadedModulesParam); - } else { - g_object_unref(kded5); // not needed anymore - cerr << "setup_kde(...): g_dbus_proxy_call_sync(...) failed." << endl; - return EXIT_FAILURE; - } - g_object_unref(kded5); // not needed anymore - if (object_path == NULL) { - cerr << "setup_kde(...): Could not find touchpad module in kded." << endl; - return EXIT_FAILURE; } + g_variant_unref(isMousePluggedInParam); - // sync on config and xsession change - kded5_modules_touchpad = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, - G_DBUS_PROXY_FLAGS_NONE, NULL, - "org.kde.kded5", - object_path, - "org.kde.touchpad", - NULL, NULL); - if (kded5_modules_touchpad == NULL) { - cerr << "setup_kde(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; - return EXIT_FAILURE; - } - if (g_signal_connect(kded5_modules_touchpad, "g-signal", G_CALLBACK(kded5_modules_touchpad_handler), NULL) < 1) { + if (g_signal_connect(kded_modules_touchpad, "g-signal", G_CALLBACK(kded_modules_touchpad_handler), NULL) < 1) { cerr << "setup_kde(...): g_signal_connect(...) failed." << endl; + g_clear_object(&kded_modules_touchpad); return EXIT_FAILURE; } @@ -231,16 +225,19 @@ int setup_kde(int lockfile_arg) { NULL, NULL); if (solid_power_management == NULL) { cerr << "setup_kde(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; + g_clear_object(&kded_modules_touchpad); return EXIT_FAILURE; } if (g_signal_connect(solid_power_management, "g-signal", G_CALLBACK(solid_power_management_handler), NULL) < 1) { cerr << "setup_kde(...): g_signal_connect(...) failed." << endl; + clean_kde(); return EXIT_FAILURE; } // sync on start - if (kded5_modules_touchpad_init(kded5_modules_touchpad) == EXIT_FAILURE) { - cerr << "setup_kde(...): kded5_modules_touchpad_init(...) failed." << endl; + if (kded_modules_touchpad_init(kded_modules_touchpad) == EXIT_FAILURE) { + cerr << "setup_kde(...): kded_modules_touchpad_init(...) failed." << endl; + clean_kde(); return EXIT_FAILURE; } @@ -248,6 +245,6 @@ int setup_kde(int lockfile_arg) { } void clean_kde() { - g_clear_object(&kded5_modules_touchpad); + g_clear_object(&kded_modules_touchpad); g_clear_object(&solid_power_management); } diff --git a/touchpad-control.cpp b/touchpad-control.cpp index a1bd06c..1f81c1c 100644 --- a/touchpad-control.cpp +++ b/touchpad-control.cpp @@ -32,7 +32,6 @@ #include -using std::cout; using std::cerr; using std::endl; @@ -141,7 +140,7 @@ int set_touchpad_state(int enabled) { return EXIT_FAILURE; } if (touchpad_count == 0) { - cout << "No compatible touchpads found." << endl; + cerr << "No compatible touchpads found." << endl; return EXIT_FAILURE; }