platform-nix-dbus.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. {
  35. .name = "org.freedesktop.ScreenSaver",
  36. .path = "/ScreenSaver",
  37. .uninhibit = "UnInhibit",
  38. },
  39. [FREEDESKTOP_PM] =
  40. {
  41. .name = "org.freedesktop.PowerManagement.Inhibit",
  42. .path = "/org/freedesktop/PowerManagement",
  43. .uninhibit = "UnInhibit",
  44. },
  45. [MATE_SM] =
  46. {
  47. .name = "org.mate.SessionManager",
  48. .path = "/org/mate/SessionManager",
  49. .uninhibit = "Uninhibit",
  50. },
  51. [GNOME_SM] =
  52. {
  53. .name = "org.gnome.SessionManager",
  54. .path = "/org/gnome/SessionManager",
  55. .uninhibit = "Uninhibit",
  56. },
  57. };
  58. static const size_t num_services =
  59. (sizeof(services) / sizeof(struct service_info));
  60. struct dbus_sleep_info {
  61. const struct service_info *service;
  62. DBusPendingCall *pending;
  63. DBusConnection *c;
  64. dbus_uint32_t id;
  65. enum service_type type;
  66. };
  67. void dbus_sleep_info_destroy(struct dbus_sleep_info *info)
  68. {
  69. if (info) {
  70. if (info->pending) {
  71. dbus_pending_call_cancel(info->pending);
  72. dbus_pending_call_unref(info->pending);
  73. }
  74. dbus_connection_close(info->c);
  75. dbus_connection_unref(info->c);
  76. bfree(info);
  77. }
  78. }
  79. struct dbus_sleep_info *dbus_sleep_info_create(void)
  80. {
  81. struct dbus_sleep_info *info = bzalloc(sizeof(*info));
  82. DBusError err;
  83. dbus_error_init(&err);
  84. info->c = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
  85. if (!info->c) {
  86. blog(LOG_ERROR, "Could not create dbus connection: %s",
  87. err.message);
  88. bfree(info);
  89. return NULL;
  90. }
  91. for (size_t i = 0; i < num_services; i++) {
  92. const struct service_info *service = &services[i];
  93. if (!service->name)
  94. continue;
  95. if (dbus_bus_name_has_owner(info->c, service->name, NULL)) {
  96. blog(LOG_DEBUG, "Found dbus service: %s",
  97. service->name);
  98. info->service = service;
  99. info->type = (enum service_type)i;
  100. return info;
  101. }
  102. }
  103. dbus_sleep_info_destroy(info);
  104. return NULL;
  105. }
  106. void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
  107. bool active)
  108. {
  109. DBusMessage *reply;
  110. const char *method;
  111. dbus_bool_t success;
  112. if (info->pending) {
  113. dbus_pending_call_block(info->pending);
  114. reply = dbus_pending_call_steal_reply(info->pending);
  115. dbus_pending_call_unref(info->pending);
  116. info->pending = NULL;
  117. if (reply) {
  118. success = dbus_message_get_args(reply, NULL,
  119. DBUS_TYPE_UINT32,
  120. &info->id,
  121. DBUS_TYPE_INVALID);
  122. if (!success)
  123. info->id = 0;
  124. dbus_message_unref(reply);
  125. }
  126. }
  127. if (active == !!info->id)
  128. return;
  129. method = active ? "Inhibit" : info->service->uninhibit;
  130. reply = dbus_message_new_method_call(info->service->name,
  131. info->service->path,
  132. info->service->name, method);
  133. if (reply == NULL) {
  134. blog(LOG_ERROR, "dbus_message_new_method_call failed");
  135. return;
  136. }
  137. if (active) {
  138. const char *program = "libobs";
  139. dbus_uint32_t flags = 0xC;
  140. dbus_uint32_t xid = 0;
  141. assert(info->id == 0);
  142. switch (info->type) {
  143. case MATE_SM:
  144. case GNOME_SM:
  145. success = dbus_message_append_args(
  146. reply, DBUS_TYPE_STRING, &program,
  147. DBUS_TYPE_UINT32, &xid, DBUS_TYPE_STRING,
  148. &reason, DBUS_TYPE_UINT32, &flags,
  149. DBUS_TYPE_INVALID);
  150. break;
  151. default:
  152. success = dbus_message_append_args(
  153. reply, DBUS_TYPE_STRING, &program,
  154. DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
  155. }
  156. if (success) {
  157. success = dbus_connection_send_with_reply(
  158. info->c, reply, &info->pending, -1);
  159. if (!success)
  160. info->pending = NULL;
  161. }
  162. } else {
  163. assert(info->id != 0);
  164. success = dbus_message_append_args(
  165. reply, DBUS_TYPE_UINT32, &info->id, DBUS_TYPE_INVALID);
  166. if (success)
  167. success = dbus_connection_send(info->c, reply, NULL);
  168. if (!success)
  169. info->id = 0;
  170. }
  171. dbus_connection_flush(info->c);
  172. dbus_message_unref(reply);
  173. }