You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
8.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2010 Google Inc. All Rights Reserved
  2. #include "talk/base/macwindowpicker.h"
  3. #include <ApplicationServices/ApplicationServices.h>
  4. #include <CoreFoundation/CoreFoundation.h>
  5. #include <dlfcn.h>
  6. #include "talk/base/logging.h"
  7. #include "talk/base/macutils.h"
  8. namespace talk_base {
  9. static const char* kCoreGraphicsName =
  10. "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/"
  11. "CoreGraphics.framework/CoreGraphics";
  12. static const char* kWindowListCopyWindowInfo = "CGWindowListCopyWindowInfo";
  13. static const char* kWindowListCreateDescriptionFromArray =
  14. "CGWindowListCreateDescriptionFromArray";
  15. // Function pointer for holding the CGWindowListCopyWindowInfo function.
  16. typedef CFArrayRef(*CGWindowListCopyWindowInfoProc)(CGWindowListOption,
  17. CGWindowID);
  18. // Function pointer for holding the CGWindowListCreateDescriptionFromArray
  19. // function.
  20. typedef CFArrayRef(*CGWindowListCreateDescriptionFromArrayProc)(CFArrayRef);
  21. MacWindowPicker::MacWindowPicker() : lib_handle_(NULL), get_window_list_(NULL),
  22. get_window_list_desc_(NULL) {
  23. }
  24. MacWindowPicker::~MacWindowPicker() {
  25. if (lib_handle_ != NULL) {
  26. dlclose(lib_handle_);
  27. }
  28. }
  29. bool MacWindowPicker::Init() {
  30. // TODO: If this class grows to use more dynamically functions
  31. // from the CoreGraphics framework, consider using
  32. // talk/base/latebindingsymboltable.h.
  33. lib_handle_ = dlopen(kCoreGraphicsName, RTLD_NOW);
  34. if (lib_handle_ == NULL) {
  35. LOG(LS_ERROR) << "Could not load CoreGraphics";
  36. return false;
  37. }
  38. get_window_list_ = dlsym(lib_handle_, kWindowListCopyWindowInfo);
  39. get_window_list_desc_ =
  40. dlsym(lib_handle_, kWindowListCreateDescriptionFromArray);
  41. if (get_window_list_ == NULL || get_window_list_desc_ == NULL) {
  42. // The CGWindowListCopyWindowInfo and the
  43. // CGWindowListCreateDescriptionFromArray functions was introduced
  44. // in Leopard(10.5) so this is a normal failure on Tiger.
  45. LOG(LS_INFO) << "Failed to load Core Graphics symbols";
  46. dlclose(lib_handle_);
  47. lib_handle_ = NULL;
  48. return false;
  49. }
  50. return true;
  51. }
  52. bool MacWindowPicker::IsVisible(const WindowId& id) {
  53. // Init if we're not already inited.
  54. if (get_window_list_desc_ == NULL && !Init()) {
  55. return false;
  56. }
  57. CGWindowID ids[1];
  58. ids[0] = id.id();
  59. CFArrayRef window_id_array =
  60. CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL);
  61. CFArrayRef window_array =
  62. reinterpret_cast<CGWindowListCreateDescriptionFromArrayProc>(
  63. get_window_list_desc_)(window_id_array);
  64. if (window_array == NULL || 0 == CFArrayGetCount(window_array)) {
  65. // Could not find the window. It might have been closed.
  66. LOG(LS_INFO) << "Window not found";
  67. CFRelease(window_id_array);
  68. return false;
  69. }
  70. CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
  71. CFArrayGetValueAtIndex(window_array, 0));
  72. CFBooleanRef is_visible = reinterpret_cast<CFBooleanRef>(
  73. CFDictionaryGetValue(window, kCGWindowIsOnscreen));
  74. // Check that the window is visible. If not we might crash.
  75. bool visible = false;
  76. if (is_visible != NULL) {
  77. visible = CFBooleanGetValue(is_visible);
  78. }
  79. CFRelease(window_id_array);
  80. CFRelease(window_array);
  81. return visible;
  82. }
  83. bool MacWindowPicker::MoveToFront(const WindowId& id) {
  84. // Init if we're not already initialized.
  85. if (get_window_list_desc_ == NULL && !Init()) {
  86. return false;
  87. }
  88. CGWindowID ids[1];
  89. ids[0] = id.id();
  90. CFArrayRef window_id_array =
  91. CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL);
  92. CFArrayRef window_array =
  93. reinterpret_cast<CGWindowListCreateDescriptionFromArrayProc>(
  94. get_window_list_desc_)(window_id_array);
  95. if (window_array == NULL || 0 == CFArrayGetCount(window_array)) {
  96. // Could not find the window. It might have been closed.
  97. LOG(LS_INFO) << "Window not found";
  98. CFRelease(window_id_array);
  99. return false;
  100. }
  101. CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
  102. CFArrayGetValueAtIndex(window_array, 0));
  103. CFStringRef window_name_ref = reinterpret_cast<CFStringRef>(
  104. CFDictionaryGetValue(window, kCGWindowName));
  105. CFNumberRef application_pid = reinterpret_cast<CFNumberRef>(
  106. CFDictionaryGetValue(window, kCGWindowOwnerPID));
  107. int pid_val;
  108. CFNumberGetValue(application_pid, kCFNumberIntType, &pid_val);
  109. std::string window_name;
  110. ToUtf8(window_name_ref, &window_name);
  111. // Build an applescript that sets the selected window to front
  112. // within the application. Then set the application to front.
  113. bool result = true;
  114. std::stringstream ss;
  115. ss << "tell application \"System Events\"\n"
  116. << "set proc to the first item of (every process whose unix id is "
  117. << pid_val
  118. << ")\n"
  119. << "tell proc to perform action \"AXRaise\" of window \""
  120. << window_name
  121. << "\"\n"
  122. << "set the frontmost of proc to true\n"
  123. << "end tell";
  124. if (!RunAppleScript(ss.str())) {
  125. // This might happen to for example X applications where the X
  126. // server spawns of processes with their own PID but the X server
  127. // is still registered as owner to the application windows. As a
  128. // workaround, we put the X server process to front, meaning that
  129. // all X applications will show up. The drawback with this
  130. // workaround is that the application that we really wanted to set
  131. // to front might be behind another X application.
  132. ProcessSerialNumber psn;
  133. pid_t pid = pid_val;
  134. int res = GetProcessForPID(pid, &psn);
  135. if (res != 0) {
  136. LOG(LS_ERROR) << "Failed getting process for pid";
  137. result = false;
  138. }
  139. res = SetFrontProcess(&psn);
  140. if (res != 0) {
  141. LOG(LS_ERROR) << "Failed setting process to front";
  142. result = false;
  143. }
  144. }
  145. CFRelease(window_id_array);
  146. CFRelease(window_array);
  147. return result;
  148. }
  149. bool MacWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
  150. const uint32_t kMaxDisplays = 128;
  151. CGDirectDisplayID active_displays[kMaxDisplays];
  152. uint32_t display_count = 0;
  153. CGError err = CGGetActiveDisplayList(kMaxDisplays,
  154. active_displays,
  155. &display_count);
  156. if (err != kCGErrorSuccess) {
  157. LOG_E(LS_ERROR, OS, err) << "Failed to enumerate the active displays.";
  158. return false;
  159. }
  160. for (uint32_t i = 0; i < display_count; ++i) {
  161. DesktopId id(active_displays[i], static_cast<int>(i));
  162. // TODO: Figure out an appropriate desktop title.
  163. DesktopDescription desc(id, "");
  164. descriptions->push_back(desc);
  165. }
  166. return display_count > 0;
  167. }
  168. // TODO(dcaiafa): add dimensions to DesktopDescription. Remove it from picker.
  169. bool MacWindowPicker::GetDesktopDimensions(const DesktopId& id,
  170. int* width,
  171. int* height) {
  172. *width = CGDisplayPixelsWide(id.id());
  173. *height = CGDisplayPixelsHigh(id.id());
  174. return true;
  175. }
  176. bool MacWindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
  177. // Init if we're not already inited.
  178. if (get_window_list_ == NULL && !Init()) {
  179. return false;
  180. }
  181. // Only get onscreen, non-desktop windows.
  182. CFArrayRef window_array =
  183. reinterpret_cast<CGWindowListCopyWindowInfoProc>(get_window_list_)(
  184. kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements,
  185. kCGNullWindowID);
  186. if (window_array == NULL) {
  187. return false;
  188. }
  189. // Check windows to make sure they have an id, title, and use window layer 0.
  190. CFIndex i;
  191. CFIndex count = CFArrayGetCount(window_array);
  192. for (i = 0; i < count; ++i) {
  193. CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
  194. CFArrayGetValueAtIndex(window_array, i));
  195. CFStringRef window_title = reinterpret_cast<CFStringRef>(
  196. CFDictionaryGetValue(window, kCGWindowName));
  197. CFNumberRef window_id = reinterpret_cast<CFNumberRef>(
  198. CFDictionaryGetValue(window, kCGWindowNumber));
  199. CFNumberRef window_layer = reinterpret_cast<CFNumberRef>(
  200. CFDictionaryGetValue(window, kCGWindowLayer));
  201. if (window_title != NULL && window_id != NULL && window_layer != NULL) {
  202. std::string title_str;
  203. int id_val, layer_val;
  204. ToUtf8(window_title, &title_str);
  205. CFNumberGetValue(window_id, kCFNumberIntType, &id_val);
  206. CFNumberGetValue(window_layer, kCFNumberIntType, &layer_val);
  207. // Discard windows without a title.
  208. if (layer_val == 0 && title_str.length() > 0) {
  209. WindowId id(static_cast<CGWindowID>(id_val));
  210. WindowDescription desc(id, title_str);
  211. descriptions->push_back(desc);
  212. }
  213. }
  214. }
  215. CFRelease(window_array);
  216. return true;
  217. }
  218. } // namespace talk_base