2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/modules/desktop_capture/window_capturer.h"
15 #include <X11/Xatom.h>
16 #include <X11/extensions/Xcomposite.h>
17 #include <X11/extensions/Xrender.h>
18 #include <X11/Xutil.h>
22 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
23 #include "webrtc/modules/desktop_capture/desktop_frame.h"
24 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
25 #include "webrtc/modules/desktop_capture/x11/x_error_trap.h"
26 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
27 #include "webrtc/system_wrappers/interface/logging.h"
28 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
29 #include "webrtc/system_wrappers/interface/scoped_refptr.h"
35 // Convenience wrapper for XGetWindowProperty() results.
36 template <class PropertyType>
37 class XWindowProperty {
39 XWindowProperty(Display* display, Window window, Atom property)
43 const int kBitsPerByte = 8;
46 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty
47 int status = XGetWindowProperty(display, window, property, 0L, ~0L, False,
48 AnyPropertyType, &actual_type,
49 &actual_format, &size_,
50 &bytes_after, &data_);
51 if (status != Success) {
55 if (sizeof(PropertyType) * kBitsPerByte != actual_format) {
68 // True if we got properly value successfully.
69 bool is_valid() const { return is_valid_; }
71 // Size and value of the property.
72 size_t size() const { return size_; }
73 const PropertyType* data() const {
74 return reinterpret_cast<PropertyType*>(data_);
76 PropertyType* data() {
77 return reinterpret_cast<PropertyType*>(data_);
82 unsigned long size_; // NOLINT: type required by XGetWindowProperty
85 DISALLOW_COPY_AND_ASSIGN(XWindowProperty);
88 class WindowCapturerLinux : public WindowCapturer,
89 public SharedXDisplay::XEventHandler {
91 WindowCapturerLinux(const DesktopCaptureOptions& options);
92 virtual ~WindowCapturerLinux();
94 // WindowCapturer interface.
95 virtual bool GetWindowList(WindowList* windows) OVERRIDE;
96 virtual bool SelectWindow(WindowId id) OVERRIDE;
97 virtual bool BringSelectedWindowToFront() OVERRIDE;
99 // DesktopCapturer interface.
100 virtual void Start(Callback* callback) OVERRIDE;
101 virtual void Capture(const DesktopRegion& region) OVERRIDE;
103 // SharedXDisplay::XEventHandler interface.
104 virtual bool HandleXEvent(const XEvent& event) OVERRIDE;
107 Display* display() { return x_display_->display(); }
109 // Iterates through |window| hierarchy to find first visible window, i.e. one
110 // that has WM_STATE property set to NormalState.
111 // See http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.3.1 .
112 ::Window GetApplicationWindow(::Window window);
114 // Returns true if the |window| is a desktop element.
115 bool IsDesktopElement(::Window window);
117 // Returns window title for the specified X |window|.
118 bool GetWindowTitle(::Window window, std::string* title);
122 scoped_refptr<SharedXDisplay> x_display_;
125 Atom window_type_atom_;
126 Atom normal_window_type_atom_;
127 bool has_composite_extension_;
129 ::Window selected_window_;
130 XServerPixelBuffer x_server_pixel_buffer_;
132 DISALLOW_COPY_AND_ASSIGN(WindowCapturerLinux);
135 WindowCapturerLinux::WindowCapturerLinux(const DesktopCaptureOptions& options)
137 x_display_(options.x_display()),
138 has_composite_extension_(false),
139 selected_window_(0) {
140 // Create Atoms so we don't need to do it every time they are used.
141 wm_state_atom_ = XInternAtom(display(), "WM_STATE", True);
142 window_type_atom_ = XInternAtom(display(), "_NET_WM_WINDOW_TYPE", True);
143 normal_window_type_atom_ = XInternAtom(
144 display(), "_NET_WM_WINDOW_TYPE_NORMAL", True);
146 int event_base, error_base, major_version, minor_version;
147 if (XCompositeQueryExtension(display(), &event_base, &error_base) &&
148 XCompositeQueryVersion(display(), &major_version, &minor_version) &&
149 // XCompositeNameWindowPixmap() requires version 0.2
150 (major_version > 0 || minor_version >= 2)) {
151 has_composite_extension_ = true;
153 LOG(LS_INFO) << "Xcomposite extension not available or too old.";
156 x_display_->AddEventHandler(ConfigureNotify, this);
159 WindowCapturerLinux::~WindowCapturerLinux() {
160 x_display_->RemoveEventHandler(ConfigureNotify, this);
163 bool WindowCapturerLinux::GetWindowList(WindowList* windows) {
166 XErrorTrap error_trap(display());
168 int num_screens = XScreenCount(display());
169 for (int screen = 0; screen < num_screens; ++screen) {
170 ::Window root_window = XRootWindow(display(), screen);
173 unsigned int num_children;
174 int status = XQueryTree(display(), root_window, &root_window, &parent,
175 &children, &num_children);
177 LOG(LS_ERROR) << "Failed to query for child windows for screen "
182 for (unsigned int i = 0; i < num_children; ++i) {
183 // Iterate in reverse order to return windows from front to back.
184 ::Window app_window =
185 GetApplicationWindow(children[num_children - 1 - i]);
186 if (app_window && !IsDesktopElement(app_window)) {
189 if (GetWindowTitle(app_window, &w.title))
198 windows->swap(result);
203 bool WindowCapturerLinux::SelectWindow(WindowId id) {
204 if (!x_server_pixel_buffer_.Init(display(), id))
207 // Tell the X server to send us window resizing events.
208 XSelectInput(display(), id, StructureNotifyMask);
210 selected_window_ = id;
212 // In addition to needing X11 server-side support for Xcomposite, it actually
213 // needs to be turned on for the window. If the user has modern
214 // hardware/drivers but isn't using a compositing window manager, that won't
215 // be the case. Here we automatically turn it on.
217 // Redirect drawing to an offscreen buffer (ie, turn on compositing). X11
218 // remembers who has requested this and will turn it off for us when we exit.
219 XCompositeRedirectWindow(display(), id, CompositeRedirectAutomatic);
224 bool WindowCapturerLinux::BringSelectedWindowToFront() {
225 if (!selected_window_)
228 unsigned int num_children;
232 // Find the root window to pass event to.
233 int status = XQueryTree(
234 display(), selected_window_, &root, &parent, &children, &num_children);
236 LOG(LS_ERROR) << "Failed to query for the root window.";
243 XRaiseWindow(display(), selected_window_);
245 // Some window managers (e.g., metacity in GNOME) consider it illegal to
246 // raise a window without also giving it input focus with
247 // _NET_ACTIVE_WINDOW, so XRaiseWindow() on its own isn't enough.
248 Atom atom = XInternAtom(display(), "_NET_ACTIVE_WINDOW", True);
251 xev.xclient.type = ClientMessage;
252 xev.xclient.serial = 0;
253 xev.xclient.send_event = True;
254 xev.xclient.window = selected_window_;
255 xev.xclient.message_type = atom;
257 // The format member is set to 8, 16, or 32 and specifies whether the
258 // data should be viewed as a list of bytes, shorts, or longs.
259 xev.xclient.format = 32;
261 memset(xev.xclient.data.l, 0, sizeof(xev.xclient.data.l));
263 XSendEvent(display(),
266 SubstructureRedirectMask | SubstructureNotifyMask,
273 void WindowCapturerLinux::Start(Callback* callback) {
277 callback_ = callback;
280 void WindowCapturerLinux::Capture(const DesktopRegion& region) {
281 x_display_->ProcessPendingXEvents();
283 if (!has_composite_extension_) {
284 // Without the Xcomposite extension we capture when the whole window is
285 // visible on screen and not covered by any other window. This is not
286 // something we want so instead, just bail out.
287 LOG(LS_INFO) << "No Xcomposite extension detected.";
288 callback_->OnCaptureCompleted(NULL);
292 DesktopFrame* frame =
293 new BasicDesktopFrame(x_server_pixel_buffer_.window_size());
295 x_server_pixel_buffer_.Synchronize();
296 x_server_pixel_buffer_.CaptureRect(DesktopRect::MakeSize(frame->size()),
299 callback_->OnCaptureCompleted(frame);
302 bool WindowCapturerLinux::HandleXEvent(const XEvent& event) {
303 if (event.type == ConfigureNotify) {
304 XConfigureEvent xce = event.xconfigure;
305 if (!DesktopSize(xce.width, xce.height).equals(
306 x_server_pixel_buffer_.window_size())) {
307 if (!x_server_pixel_buffer_.Init(display(), selected_window_)) {
308 LOG(LS_ERROR) << "Failed to initialize pixel buffer after resizing.";
316 ::Window WindowCapturerLinux::GetApplicationWindow(::Window window) {
317 // Get WM_STATE property of the window.
318 XWindowProperty<uint32_t> window_state(display(), window, wm_state_atom_);
320 // WM_STATE is considered to be set to WithdrawnState when it missing.
321 int32_t state = window_state.is_valid() ?
322 *window_state.data() : WithdrawnState;
324 if (state == NormalState) {
325 // Window has WM_STATE==NormalState. Return it.
327 } else if (state == IconicState) {
328 // Window is in minimized. Skip it.
332 // If the window is in WithdrawnState then look at all of its children.
333 ::Window root, parent;
335 unsigned int num_children;
336 if (!XQueryTree(display(), window, &root, &parent, &children,
338 LOG(LS_ERROR) << "Failed to query for child windows although window"
339 << "does not have a valid WM_STATE.";
342 ::Window app_window = 0;
343 for (unsigned int i = 0; i < num_children; ++i) {
344 app_window = GetApplicationWindow(children[i]);
354 bool WindowCapturerLinux::IsDesktopElement(::Window window) {
358 // First look for _NET_WM_WINDOW_TYPE. The standard
359 // (http://standards.freedesktop.org/wm-spec/latest/ar01s05.html#id2760306)
360 // says this hint *should* be present on all windows, and we use the existence
361 // of _NET_WM_WINDOW_TYPE_NORMAL in the property to indicate a window is not
362 // a desktop element (that is, only "normal" windows should be shareable).
363 XWindowProperty<uint32_t> window_type(display(), window, window_type_atom_);
364 if (window_type.is_valid() && window_type.size() > 0) {
365 uint32_t* end = window_type.data() + window_type.size();
366 bool is_normal = (end != std::find(
367 window_type.data(), end, normal_window_type_atom_));
371 // Fall back on using the hint.
372 XClassHint class_hint;
373 Status status = XGetClassHint(display(), window, &class_hint);
376 // No hints, assume this is a normal application window.
380 if (strcmp("gnome-panel", class_hint.res_name) == 0 ||
381 strcmp("desktop_window", class_hint.res_name) == 0) {
384 XFree(class_hint.res_name);
385 XFree(class_hint.res_class);
389 bool WindowCapturerLinux::GetWindowTitle(::Window window, std::string* title) {
392 XTextProperty window_name;
393 window_name.value = NULL;
395 status = XGetWMName(display(), window, &window_name);
396 if (status && window_name.value && window_name.nitems) {
399 status = Xutf8TextPropertyToTextList(display(), &window_name, &list,
401 if (status >= Success && cnt && *list) {
403 LOG(LS_INFO) << "Window has " << cnt
404 << " text properties, only using the first one.";
410 XFreeStringList(list);
412 if (window_name.value)
413 XFree(window_name.value);
421 WindowCapturer* WindowCapturer::Create(const DesktopCaptureOptions& options) {
422 if (!options.x_display())
424 return new WindowCapturerLinux(options);
427 } // namespace webrtc