OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include "webrtc/base/macwindowpicker.h" | 10 #include "webrtc/base/macwindowpicker.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 bool visible = false; | 99 bool visible = false; |
100 if (is_visible != NULL) { | 100 if (is_visible != NULL) { |
101 visible = CFBooleanGetValue(is_visible); | 101 visible = CFBooleanGetValue(is_visible); |
102 } | 102 } |
103 CFRelease(window_id_array); | 103 CFRelease(window_id_array); |
104 CFRelease(window_array); | 104 CFRelease(window_array); |
105 return visible; | 105 return visible; |
106 } | 106 } |
107 | 107 |
108 bool MacWindowPicker::MoveToFront(const WindowId& id) { | 108 bool MacWindowPicker::MoveToFront(const WindowId& id) { |
109 return false; | 109 // Init if we're not already initialized. |
| 110 if (get_window_list_desc_ == NULL && !Init()) { |
| 111 return false; |
| 112 } |
| 113 CGWindowID ids[1]; |
| 114 ids[0] = id.id(); |
| 115 CFArrayRef window_id_array = |
| 116 CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL); |
| 117 |
| 118 CFArrayRef window_array = |
| 119 reinterpret_cast<CGWindowListCreateDescriptionFromArrayProc>( |
| 120 get_window_list_desc_)(window_id_array); |
| 121 if (window_array == NULL || 0 == CFArrayGetCount(window_array)) { |
| 122 // Could not find the window. It might have been closed. |
| 123 LOG(LS_INFO) << "Window not found"; |
| 124 CFRelease(window_id_array); |
| 125 return false; |
| 126 } |
| 127 |
| 128 CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>( |
| 129 CFArrayGetValueAtIndex(window_array, 0)); |
| 130 CFStringRef window_name_ref = reinterpret_cast<CFStringRef>( |
| 131 CFDictionaryGetValue(window, kCGWindowName)); |
| 132 CFNumberRef application_pid = reinterpret_cast<CFNumberRef>( |
| 133 CFDictionaryGetValue(window, kCGWindowOwnerPID)); |
| 134 |
| 135 int pid_val; |
| 136 CFNumberGetValue(application_pid, kCFNumberIntType, &pid_val); |
| 137 std::string window_name; |
| 138 ToUtf8(window_name_ref, &window_name); |
| 139 |
| 140 // Build an applescript that sets the selected window to front |
| 141 // within the application. Then set the application to front. |
| 142 bool result = true; |
| 143 std::stringstream ss; |
| 144 ss << "tell application \"System Events\"\n" |
| 145 << "set proc to the first item of (every process whose unix id is " |
| 146 << pid_val |
| 147 << ")\n" |
| 148 << "tell proc to perform action \"AXRaise\" of window \"" |
| 149 << window_name |
| 150 << "\"\n" |
| 151 << "set the frontmost of proc to true\n" |
| 152 << "end tell"; |
| 153 if (!RunAppleScript(ss.str())) { |
| 154 // This might happen to for example X applications where the X |
| 155 // server spawns of processes with their own PID but the X server |
| 156 // is still registered as owner to the application windows. As a |
| 157 // workaround, we put the X server process to front, meaning that |
| 158 // all X applications will show up. The drawback with this |
| 159 // workaround is that the application that we really wanted to set |
| 160 // to front might be behind another X application. |
| 161 ProcessSerialNumber psn; |
| 162 pid_t pid = pid_val; |
| 163 int res = GetProcessForPID(pid, &psn); |
| 164 if (res != 0) { |
| 165 LOG(LS_ERROR) << "Failed getting process for pid"; |
| 166 result = false; |
| 167 } |
| 168 res = SetFrontProcess(&psn); |
| 169 if (res != 0) { |
| 170 LOG(LS_ERROR) << "Failed setting process to front"; |
| 171 result = false; |
| 172 } |
| 173 } |
| 174 CFRelease(window_id_array); |
| 175 CFRelease(window_array); |
| 176 return result; |
110 } | 177 } |
111 | 178 |
112 bool MacWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) { | 179 bool MacWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) { |
113 const uint32_t kMaxDisplays = 128; | 180 const uint32_t kMaxDisplays = 128; |
114 CGDirectDisplayID active_displays[kMaxDisplays]; | 181 CGDirectDisplayID active_displays[kMaxDisplays]; |
115 uint32_t display_count = 0; | 182 uint32_t display_count = 0; |
116 | 183 |
117 CGError err = CGGetActiveDisplayList(kMaxDisplays, | 184 CGError err = CGGetActiveDisplayList(kMaxDisplays, |
118 active_displays, | 185 active_displays, |
119 &display_count); | 186 &display_count); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 descriptions->push_back(desc); | 247 descriptions->push_back(desc); |
181 } | 248 } |
182 } | 249 } |
183 } | 250 } |
184 | 251 |
185 CFRelease(window_array); | 252 CFRelease(window_array); |
186 return true; | 253 return true; |
187 } | 254 } |
188 | 255 |
189 } // namespace rtc | 256 } // namespace rtc |
OLD | NEW |