freedesktop_at_spi_monitor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_ac = "IsEnabled";
  54. static const char *member_sr = "ScreenReaderEnabled";
  55. while (!mon->exit_thread.is_set()) {
  56. DBusMessage *message = dbus_message_new_method_call(BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE_PROPERTIES, "Get");
  57. dbus_message_append_args(
  58. message,
  59. DBUS_TYPE_STRING, &iface,
  60. DBUS_TYPE_STRING, &member_ac,
  61. DBUS_TYPE_INVALID);
  62. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  63. dbus_message_unref(message);
  64. if (!dbus_error_is_set(&error)) {
  65. DBusMessageIter iter, iter_variant, iter_struct;
  66. dbus_bool_t result;
  67. dbus_message_iter_init(reply, &iter);
  68. dbus_message_iter_recurse(&iter, &iter_variant);
  69. switch (dbus_message_iter_get_arg_type(&iter_variant)) {
  70. case DBUS_TYPE_STRUCT: {
  71. dbus_message_iter_recurse(&iter_variant, &iter_struct);
  72. if (dbus_message_iter_get_arg_type(&iter_struct) == DBUS_TYPE_BOOLEAN) {
  73. dbus_message_iter_get_basic(&iter_struct, &result);
  74. if (result) {
  75. mon->ac_enabled.set();
  76. } else {
  77. mon->ac_enabled.clear();
  78. }
  79. }
  80. } break;
  81. case DBUS_TYPE_BOOLEAN: {
  82. dbus_message_iter_get_basic(&iter_variant, &result);
  83. if (result) {
  84. mon->ac_enabled.set();
  85. } else {
  86. mon->ac_enabled.clear();
  87. }
  88. } break;
  89. default:
  90. break;
  91. }
  92. dbus_message_unref(reply);
  93. } else {
  94. dbus_error_free(&error);
  95. }
  96. message = dbus_message_new_method_call(BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE_PROPERTIES, "Get");
  97. dbus_message_append_args(
  98. message,
  99. DBUS_TYPE_STRING, &iface,
  100. DBUS_TYPE_STRING, &member_sr,
  101. DBUS_TYPE_INVALID);
  102. reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  103. dbus_message_unref(message);
  104. if (!dbus_error_is_set(&error)) {
  105. DBusMessageIter iter, iter_variant, iter_struct;
  106. dbus_bool_t result;
  107. dbus_message_iter_init(reply, &iter);
  108. dbus_message_iter_recurse(&iter, &iter_variant);
  109. switch (dbus_message_iter_get_arg_type(&iter_variant)) {
  110. case DBUS_TYPE_STRUCT: {
  111. dbus_message_iter_recurse(&iter_variant, &iter_struct);
  112. if (dbus_message_iter_get_arg_type(&iter_struct) == DBUS_TYPE_BOOLEAN) {
  113. dbus_message_iter_get_basic(&iter_struct, &result);
  114. if (result) {
  115. mon->sr_enabled.set();
  116. } else {
  117. mon->sr_enabled.clear();
  118. }
  119. }
  120. } break;
  121. case DBUS_TYPE_BOOLEAN: {
  122. dbus_message_iter_get_basic(&iter_variant, &result);
  123. if (result) {
  124. mon->sr_enabled.set();
  125. } else {
  126. mon->sr_enabled.clear();
  127. }
  128. } break;
  129. default:
  130. break;
  131. }
  132. dbus_message_unref(reply);
  133. } else {
  134. dbus_error_free(&error);
  135. }
  136. usleep(50000);
  137. }
  138. dbus_connection_unref(bus);
  139. }
  140. FreeDesktopAtSPIMonitor::FreeDesktopAtSPIMonitor() {
  141. supported.set();
  142. sr_enabled.clear();
  143. exit_thread.clear();
  144. thread.start(FreeDesktopAtSPIMonitor::monitor_thread_func, this);
  145. }
  146. FreeDesktopAtSPIMonitor::~FreeDesktopAtSPIMonitor() {
  147. exit_thread.set();
  148. if (thread.is_started()) {
  149. thread.wait_to_finish();
  150. }
  151. }
  152. #endif // DBUS_ENABLED