1
0

auto-scene-switcher-nix.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 cleanupDisplay()
  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 =
  36. XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", true);
  37. Atom actualType;
  38. int format = 0;
  39. unsigned long num = 0, bytes = 0;
  40. unsigned char *data = NULL;
  41. Window ewmh_window = 0;
  42. int status = XGetWindowProperty(display, DefaultRootWindow(display),
  43. netSupportingWmCheck, 0L, 1L, false,
  44. XA_WINDOW, &actualType, &format, &num,
  45. &bytes, &data);
  46. if (status == Success) {
  47. if (num > 0) {
  48. ewmh_window = ((Window *)data)[0];
  49. }
  50. if (data) {
  51. XFree(data);
  52. data = NULL;
  53. }
  54. }
  55. if (ewmh_window) {
  56. status = XGetWindowProperty(display, ewmh_window,
  57. netSupportingWmCheck, 0L, 1L, false,
  58. XA_WINDOW, &actualType, &format,
  59. &num, &bytes, &data);
  60. if (status != Success || num == 0 ||
  61. ewmh_window != ((Window *)data)[0]) {
  62. ewmh_window = 0;
  63. }
  64. if (status == Success && data) {
  65. XFree(data);
  66. }
  67. }
  68. return ewmh_window != 0;
  69. }
  70. static std::vector<Window> getTopLevelWindows()
  71. {
  72. std::vector<Window> res;
  73. res.resize(0);
  74. if (!ewmhIsSupported()) {
  75. return res;
  76. }
  77. Atom netClList = XInternAtom(disp(), "_NET_CLIENT_LIST", true);
  78. Atom actualType;
  79. int format;
  80. unsigned long num, bytes;
  81. Window *data = 0;
  82. for (int i = 0; i < ScreenCount(disp()); ++i) {
  83. Window rootWin = RootWindow(disp(), i);
  84. int status = XGetWindowProperty(disp(), rootWin, netClList, 0L,
  85. ~0L, false, AnyPropertyType,
  86. &actualType, &format, &num,
  87. &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. std::string windowTitle;
  101. char *name;
  102. int status = XFetchName(disp(), w, &name);
  103. if (status >= Success && name != nullptr) {
  104. std::string str(name);
  105. windowTitle = str;
  106. }
  107. XFree(name);
  108. return windowTitle;
  109. }
  110. void GetWindowList(vector<string> &windows)
  111. {
  112. windows.resize(0);
  113. for (size_t i = 0; i < getTopLevelWindows().size(); ++i) {
  114. if (GetWindowTitle(i) != "")
  115. windows.emplace_back(GetWindowTitle(i));
  116. }
  117. }
  118. void GetCurrentWindowTitle(string &title)
  119. {
  120. if (!ewmhIsSupported()) {
  121. return;
  122. }
  123. Atom active = XInternAtom(disp(), "_NET_ACTIVE_WINDOW", true);
  124. Atom actualType;
  125. int format;
  126. unsigned long num, bytes;
  127. Window *data = 0;
  128. char *name;
  129. Window rootWin = RootWindow(disp(), 0);
  130. XGetWindowProperty(disp(), rootWin, active, 0L, ~0L, false,
  131. AnyPropertyType, &actualType, &format, &num, &bytes,
  132. (uint8_t **)&data);
  133. int status = XFetchName(disp(), data[0], &name);
  134. if (status >= Success && name != nullptr) {
  135. std::string str(name);
  136. title = str;
  137. }
  138. XFree(name);
  139. }