kwin_janitor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // linux-wallpaperengine-kwin-janitor: the engine starts one of these next to itself and tells it, over stdin, which
  2. // scripts it loads into KWin ("+<plugin name>\t<file>\n", "-<plugin name>\n" when it unloads one itself). When the pipe
  3. // closes the engine is gone, however it went (a SIGKILL runs no destructor), and the scripts it left in KWin are
  4. // unloaded: they would go on calling the dead engine's D-Bus service at every cursor move. Only libdbus on purpose, the
  5. // engine itself is over 100 MB resident.
  6. #include <dbus/dbus.h>
  7. #include <cerrno>
  8. #include <cstdio>
  9. #include <map>
  10. #include <string>
  11. #include <unistd.h>
  12. namespace {
  13. void unloadScript (DBusConnection* connection, const std::string& pluginName) {
  14. DBusMessage* call
  15. = dbus_message_new_method_call ("org.kde.KWin", "/Scripting", "org.kde.kwin.Scripting", "unloadScript");
  16. if (call == nullptr) {
  17. return;
  18. }
  19. const char* nameArg = pluginName.c_str ();
  20. dbus_message_append_args (call, DBUS_TYPE_STRING, &nameArg, DBUS_TYPE_INVALID);
  21. DBusError error;
  22. dbus_error_init (&error);
  23. if (DBusMessage* reply = dbus_connection_send_with_reply_and_block (connection, call, 2000, &error);
  24. reply != nullptr) {
  25. dbus_message_unref (reply);
  26. }
  27. dbus_error_free (&error);
  28. dbus_message_unref (call);
  29. }
  30. }
  31. int main () {
  32. std::map<std::string, std::string> scripts;
  33. std::string pending;
  34. char buffer[512];
  35. for (;;) {
  36. const ssize_t count = read (STDIN_FILENO, buffer, sizeof (buffer));
  37. if (count < 0 && errno == EINTR) {
  38. continue;
  39. }
  40. if (count <= 0) {
  41. break;
  42. }
  43. pending.append (buffer, static_cast<size_t> (count));
  44. for (size_t end; (end = pending.find ('\n')) != std::string::npos; pending.erase (0, end + 1)) {
  45. const std::string line = pending.substr (0, end);
  46. if (line.starts_with ("-")) {
  47. scripts.erase (line.substr (1));
  48. } else if (line.starts_with ("+")) {
  49. const auto tab = line.find ('\t');
  50. if (tab != std::string::npos) {
  51. scripts[line.substr (1, tab - 1)] = line.substr (tab + 1);
  52. }
  53. }
  54. }
  55. }
  56. if (scripts.empty ()) {
  57. return 0;
  58. }
  59. DBusError error;
  60. dbus_error_init (&error);
  61. DBusConnection* connection = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  62. if (connection == nullptr) {
  63. dbus_error_free (&error);
  64. return 1;
  65. }
  66. dbus_connection_set_exit_on_disconnect (connection, false);
  67. for (const auto& [name, path] : scripts) {
  68. unloadScript (connection, name);
  69. std::remove (path.c_str ());
  70. }
  71. dbus_connection_close (connection);
  72. dbus_connection_unref (connection);
  73. return 0;
  74. }