platform-nix-dbus.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2015 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <assert.h>
  17. #include <dbus/dbus.h>
  18. #include "bmem.h"
  19. /* NOTE: This is basically just the VLC implementation from its d-bus power
  20. * management inhibition code. Credit is theirs for this. */
  21. enum service_type {
  22. FREEDESKTOP_SS, /* freedesktop screensaver (KDE >= 4, GNOME >= 3.10) */
  23. FREEDESKTOP_PM, /* freedesktop power management (KDE, gnome <= 2.26) */
  24. MATE_SM, /* MATE (>= 1.0) session manager */
  25. GNOME_SM, /* GNOME 2.26 - 3.4 sessopm mamager */
  26. };
  27. struct service_info {
  28. const char *name;
  29. const char *path;
  30. const char *uninhibit;
  31. };
  32. static const struct service_info services[] = {
  33. [FREEDESKTOP_SS] = {
  34. .name = "org.freedesktop.ScreenSaver",
  35. .path = "/ScreenSaver",
  36. .uninhibit = "UnInhibit",
  37. },
  38. [FREEDESKTOP_PM] = {
  39. .name = "org.freedesktop.PowerManagement.Inhibit",
  40. .path = "/org/freedesktop/PowerManagement",
  41. .uninhibit = "UnInhibit",
  42. },
  43. [MATE_SM] = {
  44. .name = "org.mate.SessionManager",
  45. .path = "/org/mate/SessionManager",
  46. .uninhibit = "Uninhibit",
  47. },
  48. [GNOME_SM] = {
  49. .name = "org.gnome.SessionManager",
  50. .path = "/org/gnome/SessionManager",
  51. .uninhibit = "Uninhibit",
  52. },
  53. };
  54. static const size_t num_services =
  55. (sizeof(services) / sizeof(struct service_info));
  56. struct dbus_sleep_info {
  57. const struct service_info *service;
  58. DBusPendingCall *pending;
  59. DBusConnection *c;
  60. dbus_uint32_t id;
  61. enum service_type type;
  62. };
  63. void dbus_sleep_info_destroy(struct dbus_sleep_info *info)
  64. {
  65. if (info) {
  66. if (info->pending) {
  67. dbus_pending_call_cancel(info->pending);
  68. dbus_pending_call_unref(info->pending);
  69. }
  70. dbus_connection_close(info->c);
  71. dbus_connection_unref(info->c);
  72. bfree(info);
  73. }
  74. }
  75. struct dbus_sleep_info *dbus_sleep_info_create(void)
  76. {
  77. struct dbus_sleep_info *info = bzalloc(sizeof(*info));
  78. DBusError err;
  79. dbus_error_init(&err);
  80. info->c = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
  81. if (!info->c) {
  82. blog(LOG_ERROR, "Could not create dbus connection: %s",
  83. err.message);
  84. bfree(info);
  85. return NULL;
  86. }
  87. for (size_t i = 0; i < num_services; i++) {
  88. const struct service_info *service = &services[i];
  89. if (!service->name)
  90. continue;
  91. if (dbus_bus_name_has_owner(info->c, service->name, NULL)) {
  92. blog(LOG_DEBUG, "Found dbus service: %s",
  93. service->name);
  94. info->service = service;
  95. info->type = (enum service_type)i;
  96. return info;
  97. }
  98. }
  99. dbus_sleep_info_destroy(info);
  100. return NULL;
  101. }
  102. void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
  103. bool active)
  104. {
  105. DBusMessage *reply;
  106. const char *method;
  107. dbus_bool_t success;
  108. if (info->pending) {
  109. dbus_pending_call_block(info->pending);
  110. reply = dbus_pending_call_steal_reply(info->pending);
  111. dbus_pending_call_unref(info->pending);
  112. info->pending = NULL;
  113. if (reply) {
  114. success = dbus_message_get_args(reply, NULL,
  115. DBUS_TYPE_UINT32, &info->id,
  116. DBUS_TYPE_INVALID);
  117. if (!success)
  118. info->id = 0;
  119. dbus_message_unref(reply);
  120. }
  121. }
  122. if (active == !!info->id)
  123. return;
  124. method = active ? "Inhibit" : info->service->uninhibit;
  125. reply = dbus_message_new_method_call(info->service->name,
  126. info->service->path, info->service->name, method);
  127. if (reply == NULL) {
  128. blog(LOG_ERROR, "dbus_message_new_method_call failed");
  129. return;
  130. }
  131. if (active) {
  132. const char *program = "libobs";
  133. dbus_uint32_t flags = 0xC;
  134. dbus_uint32_t xid = 0;
  135. assert(info->id == 0);
  136. switch (info->type) {
  137. case MATE_SM:
  138. case GNOME_SM:
  139. success = dbus_message_append_args(reply,
  140. DBUS_TYPE_STRING, &program,
  141. DBUS_TYPE_UINT32, &xid,
  142. DBUS_TYPE_STRING, &reason,
  143. DBUS_TYPE_UINT32, &flags,
  144. DBUS_TYPE_INVALID);
  145. break;
  146. default:
  147. success = dbus_message_append_args(reply,
  148. DBUS_TYPE_STRING, &program,
  149. DBUS_TYPE_STRING, &reason,
  150. DBUS_TYPE_INVALID);
  151. }
  152. if (success) {
  153. success = dbus_connection_send_with_reply(info->c,
  154. reply, &info->pending, -1);
  155. if (!success)
  156. info->pending = NULL;
  157. }
  158. } else {
  159. assert(info->id != 0);
  160. success = dbus_message_append_args(reply,
  161. DBUS_TYPE_UINT32, &info->id,
  162. DBUS_TYPE_INVALID);
  163. if (success)
  164. success = dbus_connection_send(info->c, reply, NULL);
  165. if (!success)
  166. info->id = 0;
  167. }
  168. dbus_connection_flush(info->c);
  169. dbus_message_unref(reply);
  170. }