freedesktop_at_spi_monitor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**************************************************************************/
  2. /* freedesktop_at_spi_monitor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "freedesktop_at_spi_monitor.h"
  31. #ifdef DBUS_ENABLED
  32. #include "core/os/os.h"
  33. #ifdef SOWRAP_ENABLED
  34. #include "dbus-so_wrap.h"
  35. #else
  36. #include <dbus/dbus.h>
  37. #endif
  38. #include <unistd.h>
  39. #define BUS_OBJECT_NAME "org.a11y.Bus"
  40. #define BUS_OBJECT_PATH "/org/a11y/bus"
  41. #define BUS_INTERFACE_PROPERTIES "org.freedesktop.DBus.Properties"
  42. void FreeDesktopAtSPIMonitor::monitor_thread_func(void *p_userdata) {
  43. FreeDesktopAtSPIMonitor *mon = (FreeDesktopAtSPIMonitor *)p_userdata;
  44. DBusError error;
  45. dbus_error_init(&error);
  46. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  47. if (dbus_error_is_set(&error)) {
  48. dbus_error_free(&error);
  49. mon->supported.clear();
  50. return;
  51. }
  52. static const char *iface = "org.a11y.Status";
  53. static const char *member = "IsEnabled";
  54. while (!mon->exit_thread.is_set()) {
  55. DBusMessage *message = dbus_message_new_method_call(BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE_PROPERTIES, "Get");
  56. dbus_message_append_args(
  57. message,
  58. DBUS_TYPE_STRING, &iface,
  59. DBUS_TYPE_STRING, &member,
  60. DBUS_TYPE_INVALID);
  61. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  62. dbus_message_unref(message);
  63. if (!dbus_error_is_set(&error)) {
  64. DBusMessageIter iter, iter_variant, iter_struct;
  65. dbus_bool_t result;
  66. dbus_message_iter_init(reply, &iter);
  67. dbus_message_iter_recurse(&iter, &iter_variant);
  68. switch (dbus_message_iter_get_arg_type(&iter_variant)) {
  69. case DBUS_TYPE_STRUCT: {
  70. dbus_message_iter_recurse(&iter_variant, &iter_struct);
  71. if (dbus_message_iter_get_arg_type(&iter_struct) == DBUS_TYPE_BOOLEAN) {
  72. dbus_message_iter_get_basic(&iter_struct, &result);
  73. if (result) {
  74. mon->sr_enabled.set();
  75. } else {
  76. mon->sr_enabled.clear();
  77. }
  78. }
  79. } break;
  80. case DBUS_TYPE_BOOLEAN: {
  81. dbus_message_iter_get_basic(&iter_variant, &result);
  82. if (result) {
  83. mon->sr_enabled.set();
  84. } else {
  85. mon->sr_enabled.clear();
  86. }
  87. } break;
  88. default:
  89. break;
  90. }
  91. dbus_message_unref(reply);
  92. } else {
  93. dbus_error_free(&error);
  94. }
  95. usleep(50000);
  96. }
  97. dbus_connection_unref(bus);
  98. }
  99. FreeDesktopAtSPIMonitor::FreeDesktopAtSPIMonitor() {
  100. #ifdef SOWRAP_ENABLED
  101. #ifdef DEBUG_ENABLED
  102. int dylibloader_verbose = 1;
  103. #else
  104. int dylibloader_verbose = 0;
  105. #endif
  106. if (initialize_dbus(dylibloader_verbose) != 0) {
  107. print_verbose("AT-SPI2: Failed to load DBus library!");
  108. supported.clear();
  109. return;
  110. }
  111. #endif
  112. bool ver_ok = false;
  113. int version_major = 0;
  114. int version_minor = 0;
  115. int version_rev = 0;
  116. dbus_get_version(&version_major, &version_minor, &version_rev);
  117. ver_ok = (version_major == 1 && version_minor >= 10) || (version_major > 1); // 1.10.0
  118. print_verbose(vformat("AT-SPI2: DBus %d.%d.%d detected.", version_major, version_minor, version_rev));
  119. if (!ver_ok) {
  120. print_verbose("AT-SPI2: Unsupported DBus library version!");
  121. supported.clear();
  122. return;
  123. }
  124. supported.set();
  125. sr_enabled.clear();
  126. exit_thread.clear();
  127. thread.start(FreeDesktopAtSPIMonitor::monitor_thread_func, this);
  128. }
  129. FreeDesktopAtSPIMonitor::~FreeDesktopAtSPIMonitor() {
  130. exit_thread.set();
  131. if (thread.is_started()) {
  132. thread.wait_to_finish();
  133. }
  134. }
  135. #endif // DBUS_ENABLED