auto-scene-switcher-nix.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <X11/Xlib.h>
  2. #include <X11/Xatom.h>
  3. #include <X11/Xutil.h>
  4. #undef Bool
  5. #undef CursorShape
  6. #undef Expose
  7. #undef KeyPress
  8. #undef KeyRelease
  9. #undef FocusIn
  10. #undef FocusOut
  11. #undef FontChange
  12. #undef None
  13. #undef Status
  14. #undef Unsorted
  15. #include <util/platform.h>
  16. #include "auto-scene-switcher.hpp"
  17. using namespace std;
  18. static Display *xdisplay = 0;
  19. Display *disp()
  20. {
  21. if (!xdisplay)
  22. xdisplay = XOpenDisplay(NULL);
  23. return xdisplay;
  24. }
  25. void CleanupSceneSwitcher()
  26. {
  27. if (!xdisplay)
  28. return;
  29. XCloseDisplay(xdisplay);
  30. xdisplay = 0;
  31. }
  32. static bool ewmhIsSupported()
  33. {
  34. Display *display = disp();
  35. Atom netSupportingWmCheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", true);
  36. Atom actualType;
  37. int format = 0;
  38. unsigned long num = 0, bytes = 0;
  39. unsigned char *data = NULL;
  40. Window ewmh_window = 0;
  41. Window root_window = 0;
  42. root_window = DefaultRootWindow(display);
  43. if (!root_window) {
  44. return false;
  45. }
  46. int status = XGetWindowProperty(display, root_window, netSupportingWmCheck, 0L, 1L, false, XA_WINDOW,
  47. &actualType, &format, &num, &bytes, &data);
  48. if (status == Success) {
  49. if (num > 0) {
  50. ewmh_window = ((Window *)data)[0];
  51. }
  52. if (data) {
  53. XFree(data);
  54. data = NULL;
  55. }
  56. }
  57. if (ewmh_window) {
  58. status = XGetWindowProperty(display, ewmh_window, netSupportingWmCheck, 0L, 1L, false, XA_WINDOW,
  59. &actualType, &format, &num, &bytes, &data);
  60. if (status != Success || num == 0 || ewmh_window != ((Window *)data)[0]) {
  61. ewmh_window = 0;
  62. }
  63. if (status == Success && data) {
  64. XFree(data);
  65. }
  66. }
  67. return ewmh_window != 0;
  68. }
  69. static std::vector<Window> getTopLevelWindows()
  70. {
  71. std::vector<Window> res;
  72. res.resize(0);
  73. if (!ewmhIsSupported()) {
  74. return res;
  75. }
  76. Atom netClList = XInternAtom(disp(), "_NET_CLIENT_LIST", true);
  77. Atom actualType;
  78. int format;
  79. unsigned long num, bytes;
  80. Window *data = 0;
  81. for (int i = 0; i < ScreenCount(disp()); ++i) {
  82. Window rootWin = RootWindow(disp(), i);
  83. if (!rootWin) {
  84. continue;
  85. }
  86. int status = XGetWindowProperty(disp(), rootWin, netClList, 0L, ~0L, false, AnyPropertyType,
  87. &actualType, &format, &num, &bytes, (uint8_t **)&data);
  88. if (status != Success) {
  89. continue;
  90. }
  91. for (unsigned long i = 0; i < num; ++i)
  92. res.emplace_back(data[i]);
  93. XFree(data);
  94. }
  95. return res;
  96. }
  97. static std::string GetWindowTitle(size_t i)
  98. {
  99. Window w = getTopLevelWindows().at(i);
  100. if (!w) {
  101. return "";
  102. }
  103. std::string windowTitle;
  104. char *name;
  105. int status = XFetchName(disp(), w, &name);
  106. if (status >= Success && name != nullptr) {
  107. std::string str(name);
  108. windowTitle = str;
  109. XFree(name);
  110. } else {
  111. XTextProperty xtp_new_name;
  112. if (XGetWMName(disp(), w, &xtp_new_name) != 0 && xtp_new_name.value != nullptr) {
  113. std::string str((const char *)xtp_new_name.value);
  114. windowTitle = str;
  115. XFree(xtp_new_name.value);
  116. }
  117. }
  118. return windowTitle;
  119. }
  120. void GetWindowList(vector<string> &windows)
  121. {
  122. windows.resize(0);
  123. for (size_t i = 0; i < getTopLevelWindows().size(); ++i) {
  124. if (GetWindowTitle(i) != "")
  125. windows.emplace_back(GetWindowTitle(i));
  126. }
  127. }
  128. void GetCurrentWindowTitle(string &title)
  129. {
  130. if (!ewmhIsSupported()) {
  131. return;
  132. }
  133. Atom active = XInternAtom(disp(), "_NET_ACTIVE_WINDOW", true);
  134. Atom actualType;
  135. int format;
  136. unsigned long num, bytes;
  137. Window *data = 0;
  138. char *name;
  139. Window rootWin = RootWindow(disp(), 0);
  140. if (!rootWin) {
  141. return;
  142. }
  143. XGetWindowProperty(disp(), rootWin, active, 0L, ~0L, false, AnyPropertyType, &actualType, &format, &num, &bytes,
  144. (uint8_t **)&data);
  145. if (!data[0]) {
  146. return;
  147. }
  148. int status = XFetchName(disp(), data[0], &name);
  149. if (status >= Success && name != nullptr) {
  150. std::string str(name);
  151. title = str;
  152. } else {
  153. XTextProperty xtp_new_name;
  154. if (XGetWMName(disp(), data[0], &xtp_new_name) != 0 && xtp_new_name.value != nullptr) {
  155. std::string str((const char *)xtp_new_name.value);
  156. title = str;
  157. XFree(xtp_new_name.value);
  158. }
  159. }
  160. XFree(name);
  161. }