1
// Copyright 2010 Google Inc. All Rights Reserved
4
#include "talk/base/macwindowpicker.h"
6
#include <ApplicationServices/ApplicationServices.h>
7
#include <CoreFoundation/CoreFoundation.h>
10
#include "talk/base/logging.h"
11
#include "talk/base/macutils.h"
15
static const char* kCoreGraphicsName =
16
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/"
17
"CoreGraphics.framework/CoreGraphics";
19
static const char* kWindowListCopyWindowInfo = "CGWindowListCopyWindowInfo";
20
static const char* kWindowListCreateDescriptionFromArray =
21
"CGWindowListCreateDescriptionFromArray";
23
// Function pointer for holding the CGWindowListCopyWindowInfo function.
24
typedef CFArrayRef(*CGWindowListCopyWindowInfoProc)(CGWindowListOption,
27
// Function pointer for holding the CGWindowListCreateDescriptionFromArray
29
typedef CFArrayRef(*CGWindowListCreateDescriptionFromArrayProc)(CFArrayRef);
31
MacWindowPicker::MacWindowPicker() : lib_handle_(NULL), get_window_list_(NULL),
32
get_window_list_desc_(NULL) {
35
MacWindowPicker::~MacWindowPicker() {
36
if (lib_handle_ != NULL) {
41
bool MacWindowPicker::Init() {
42
// TODO: If this class grows to use more dynamically functions
43
// from the CoreGraphics framework, consider using
44
// talk/base/latebindingsymboltable.h.
45
lib_handle_ = dlopen(kCoreGraphicsName, RTLD_NOW);
46
if (lib_handle_ == NULL) {
47
LOG(LS_ERROR) << "Could not load CoreGraphics";
51
get_window_list_ = dlsym(lib_handle_, kWindowListCopyWindowInfo);
52
get_window_list_desc_ =
53
dlsym(lib_handle_, kWindowListCreateDescriptionFromArray);
54
if (get_window_list_ == NULL || get_window_list_desc_ == NULL) {
55
// The CGWindowListCopyWindowInfo and the
56
// CGWindowListCreateDescriptionFromArray functions was introduced
57
// in Leopard(10.5) so this is a normal failure on Tiger.
58
LOG(LS_INFO) << "Failed to load Core Graphics symbols";
67
bool MacWindowPicker::IsVisible(const WindowId& id) {
68
// Init if we're not already inited.
69
if (get_window_list_desc_ == NULL && !Init()) {
74
CFArrayRef window_id_array =
75
CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL);
77
CFArrayRef window_array =
78
reinterpret_cast<CGWindowListCreateDescriptionFromArrayProc>(
79
get_window_list_desc_)(window_id_array);
80
if (window_array == NULL || 0 == CFArrayGetCount(window_array)) {
81
// Could not find the window. It might have been closed.
82
LOG(LS_INFO) << "Window not found";
83
CFRelease(window_id_array);
87
CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
88
CFArrayGetValueAtIndex(window_array, 0));
89
CFBooleanRef is_visible = reinterpret_cast<CFBooleanRef>(
90
CFDictionaryGetValue(window, kCGWindowIsOnscreen));
92
// Check that the window is visible. If not we might crash.
94
if (is_visible != NULL) {
95
visible = CFBooleanGetValue(is_visible);
97
CFRelease(window_id_array);
98
CFRelease(window_array);
102
bool MacWindowPicker::MoveToFront(const WindowId& id) {
103
// Init if we're not already initialized.
104
if (get_window_list_desc_ == NULL && !Init()) {
109
CFArrayRef window_id_array =
110
CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL);
112
CFArrayRef window_array =
113
reinterpret_cast<CGWindowListCreateDescriptionFromArrayProc>(
114
get_window_list_desc_)(window_id_array);
115
if (window_array == NULL || 0 == CFArrayGetCount(window_array)) {
116
// Could not find the window. It might have been closed.
117
LOG(LS_INFO) << "Window not found";
118
CFRelease(window_id_array);
122
CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
123
CFArrayGetValueAtIndex(window_array, 0));
124
CFStringRef window_name_ref = reinterpret_cast<CFStringRef>(
125
CFDictionaryGetValue(window, kCGWindowName));
126
CFNumberRef application_pid = reinterpret_cast<CFNumberRef>(
127
CFDictionaryGetValue(window, kCGWindowOwnerPID));
130
CFNumberGetValue(application_pid, kCFNumberIntType, &pid_val);
131
std::string window_name;
132
ToUtf8(window_name_ref, &window_name);
134
// Build an applescript that sets the selected window to front
135
// within the application. Then set the application to front.
137
std::stringstream ss;
138
ss << "tell application \"System Events\"\n"
139
<< "set proc to the first item of (every process whose unix id is "
142
<< "tell proc to perform action \"AXRaise\" of window \""
145
<< "set the frontmost of proc to true\n"
147
if (!RunAppleScript(ss.str())) {
148
// This might happen to for example X applications where the X
149
// server spawns of processes with their own PID but the X server
150
// is still registered as owner to the application windows. As a
151
// workaround, we put the X server process to front, meaning that
152
// all X applications will show up. The drawback with this
153
// workaround is that the application that we really wanted to set
154
// to front might be behind another X application.
155
ProcessSerialNumber psn;
157
int res = GetProcessForPID(pid, &psn);
159
LOG(LS_ERROR) << "Failed getting process for pid";
162
res = SetFrontProcess(&psn);
164
LOG(LS_ERROR) << "Failed setting process to front";
168
CFRelease(window_id_array);
169
CFRelease(window_array);
173
bool MacWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
174
const uint32_t kMaxDisplays = 128;
175
CGDirectDisplayID active_displays[kMaxDisplays];
176
uint32_t display_count = 0;
178
CGError err = CGGetActiveDisplayList(kMaxDisplays,
181
if (err != kCGErrorSuccess) {
182
LOG_E(LS_ERROR, OS, err) << "Failed to enumerate the active displays.";
185
for (uint32_t i = 0; i < display_count; ++i) {
186
DesktopId id(active_displays[i], static_cast<int>(i));
187
// TODO: Figure out an appropriate desktop title.
188
DesktopDescription desc(id, "");
189
descriptions->push_back(desc);
191
return display_count > 0;
194
bool MacWindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
195
// Init if we're not already inited.
196
if (get_window_list_ == NULL && !Init()) {
200
// Only get onscreen, non-desktop windows.
201
CFArrayRef window_array =
202
reinterpret_cast<CGWindowListCopyWindowInfoProc>(get_window_list_)(
203
kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements,
205
if (window_array == NULL) {
209
// Check windows to make sure they have an id, title, and use window layer 0.
211
CFIndex count = CFArrayGetCount(window_array);
212
for (i = 0; i < count; ++i) {
213
CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
214
CFArrayGetValueAtIndex(window_array, i));
215
CFStringRef window_title = reinterpret_cast<CFStringRef>(
216
CFDictionaryGetValue(window, kCGWindowName));
217
CFNumberRef window_id = reinterpret_cast<CFNumberRef>(
218
CFDictionaryGetValue(window, kCGWindowNumber));
219
CFNumberRef window_layer = reinterpret_cast<CFNumberRef>(
220
CFDictionaryGetValue(window, kCGWindowLayer));
221
if (window_title != NULL && window_id != NULL && window_layer != NULL) {
222
std::string title_str;
223
int id_val, layer_val;
224
ToUtf8(window_title, &title_str);
225
CFNumberGetValue(window_id, kCFNumberIntType, &id_val);
226
CFNumberGetValue(window_layer, kCFNumberIntType, &layer_val);
228
// Discard windows without a title.
229
if (layer_val == 0 && title_str.length() > 0) {
230
WindowId id(static_cast<CGWindowID>(id_val));
231
WindowDescription desc(id, title_str);
232
descriptions->push_back(desc);
237
CFRelease(window_array);
241
} // namespace talk_base