2 * Copyright 2010 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/base/linuxwindowpicker.h"
19 #include <X11/Xatom.h>
20 #include <X11/extensions/Xcomposite.h>
21 #include <X11/extensions/Xrender.h>
22 #include <X11/Xutil.h>
24 #include "webrtc/base/logging.h"
28 // Convenience wrapper for XGetWindowProperty results.
29 template <class PropertyType>
30 class XWindowProperty {
32 XWindowProperty(Display* display, Window window, Atom property)
34 const int kBitsPerByte = 8;
37 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty
38 int status = XGetWindowProperty(display, window, property, 0L, ~0L, False,
39 AnyPropertyType, &actual_type,
40 &actual_format, &size_,
41 &bytes_after, &data_);
42 succeeded_ = (status == Success);
44 data_ = NULL; // Ensure nothing is freed.
45 } else if (sizeof(PropertyType) * kBitsPerByte != actual_format) {
46 LOG(LS_WARNING) << "Returned type size differs from "
47 "requested type size.";
49 // We still need to call XFree in this case, so leave data_ alone.
62 bool succeeded() const { return succeeded_; }
63 size_t size() const { return size_; }
64 const PropertyType* data() const {
65 return reinterpret_cast<PropertyType*>(data_);
67 PropertyType* data() {
68 return reinterpret_cast<PropertyType*>(data_);
73 unsigned long size_; // NOLINT: type required by XGetWindowProperty
76 DISALLOW_COPY_AND_ASSIGN(XWindowProperty);
79 // Stupid X11. It seems none of the synchronous returns codes from X11 calls
80 // are meaningful unless an asynchronous error handler is configured. This
81 // RAII class registers and unregisters an X11 error handler.
82 class XErrorSuppressor {
84 explicit XErrorSuppressor(Display* display)
85 : display_(display), original_error_handler_(NULL) {
89 UnsuppressX11Errors();
93 static int ErrorHandler(Display* display, XErrorEvent* e) {
95 XGetErrorText(display, e->error_code, buf, sizeof buf);
96 LOG(LS_WARNING) << "Received X11 error \"" << buf << "\" for request code "
97 << static_cast<unsigned int>(e->request_code);
101 void SuppressX11Errors() {
103 XSync(display_, False);
104 original_error_handler_ = XSetErrorHandler(&ErrorHandler);
107 void UnsuppressX11Errors() {
109 XSync(display_, False);
110 XErrorHandler handler = XSetErrorHandler(original_error_handler_);
111 if (handler != &ErrorHandler) {
112 LOG(LS_WARNING) << "Unbalanced XSetErrorHandler() calls detected. "
113 << "Final error handler may not be what you expect!";
115 original_error_handler_ = NULL;
119 XErrorHandler original_error_handler_;
121 DISALLOW_COPY_AND_ASSIGN(XErrorSuppressor);
124 // Hiding all X11 specifics inside its own class. This to avoid
125 // conflicts between talk and X11 header declarations.
126 class XWindowEnumerator {
130 has_composite_extension_(false),
131 has_render_extension_(false) {
134 ~XWindowEnumerator() {
135 if (display_ != NULL) {
136 XCloseDisplay(display_);
141 if (display_ != NULL) {
142 // Already initialized.
145 display_ = XOpenDisplay(NULL);
146 if (display_ == NULL) {
147 LOG(LS_ERROR) << "Failed to open display.";
151 XErrorSuppressor error_suppressor(display_);
153 wm_state_ = XInternAtom(display_, "WM_STATE", True);
154 net_wm_icon_ = XInternAtom(display_, "_NET_WM_ICON", False);
156 int event_base, error_base, major_version, minor_version;
157 if (XCompositeQueryExtension(display_, &event_base, &error_base) &&
158 XCompositeQueryVersion(display_, &major_version, &minor_version) &&
159 // XCompositeNameWindowPixmap() requires version 0.2
160 (major_version > 0 || minor_version >= 2)) {
161 has_composite_extension_ = true;
163 LOG(LS_INFO) << "Xcomposite extension not available or too old.";
166 if (XRenderQueryExtension(display_, &event_base, &error_base) &&
167 XRenderQueryVersion(display_, &major_version, &minor_version) &&
168 // XRenderSetPictureTransform() requires version 0.6
169 (major_version > 0 || minor_version >= 6)) {
170 has_render_extension_ = true;
172 LOG(LS_INFO) << "Xrender extension not available or too old.";
177 bool EnumerateWindows(WindowDescriptionList* descriptions) {
181 XErrorSuppressor error_suppressor(display_);
182 int num_screens = XScreenCount(display_);
184 for (int i = 0; i < num_screens; ++i) {
185 if (EnumerateScreenWindows(descriptions, i)) {
186 // We know we succeded on at least one screen.
193 bool EnumerateDesktops(DesktopDescriptionList* descriptions) {
197 XErrorSuppressor error_suppressor(display_);
198 Window default_root_window = XDefaultRootWindow(display_);
199 int num_screens = XScreenCount(display_);
200 for (int i = 0; i < num_screens; ++i) {
201 Window root_window = XRootWindow(display_, i);
202 DesktopId id(DesktopId(root_window, i));
203 // TODO: Figure out an appropriate desktop title.
204 DesktopDescription desc(id, "");
205 desc.set_primary(root_window == default_root_window);
206 descriptions->push_back(desc);
208 return num_screens > 0;
211 bool IsVisible(const WindowId& id) {
215 XErrorSuppressor error_suppressor(display_);
216 XWindowAttributes attr;
217 if (!XGetWindowAttributes(display_, id.id(), &attr)) {
218 LOG(LS_ERROR) << "XGetWindowAttributes() failed";
221 return attr.map_state == IsViewable;
224 bool MoveToFront(const WindowId& id) {
228 XErrorSuppressor error_suppressor(display_);
229 unsigned int num_children;
234 // Find root window to pass event to.
235 int status = XQueryTree(display_, id.id(), &root, &parent, &children,
238 LOG(LS_WARNING) << "Failed to query for child windows.";
241 if (children != NULL) {
245 // Move the window to front.
246 XRaiseWindow(display_, id.id());
248 // Some window managers (e.g., metacity in GNOME) consider it illegal to
249 // raise a window without also giving it input focus with
250 // _NET_ACTIVE_WINDOW, so XRaiseWindow() on its own isn't enough.
251 Atom atom = XInternAtom(display_, "_NET_ACTIVE_WINDOW", True);
256 xev.xclient.type = ClientMessage;
257 xev.xclient.serial = 0;
258 xev.xclient.send_event = True;
259 xev.xclient.window = id.id();
260 xev.xclient.message_type = atom;
262 // The format member is set to 8, 16, or 32 and specifies whether the
263 // data should be viewed as a list of bytes, shorts, or longs.
264 xev.xclient.format = 32;
266 xev.xclient.data.l[0] = 0;
267 xev.xclient.data.l[1] = 0;
268 xev.xclient.data.l[2] = 0;
269 xev.xclient.data.l[3] = 0;
270 xev.xclient.data.l[4] = 0;
272 event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
274 XSendEvent(display_, root, False, event_mask, &xev);
280 uint8* GetWindowIcon(const WindowId& id, int* width, int* height) {
284 XErrorSuppressor error_suppressor(display_);
287 unsigned long length, bytes_after, size;
288 unsigned char* data = NULL;
290 // Find out the size of the icon data.
291 if (XGetWindowProperty(
292 display_, id.id(), net_wm_icon_, 0, 0, False, XA_CARDINAL,
293 &ret_type, &format, &length, &size, &data) == Success &&
297 LOG(LS_ERROR) << "Failed to get size of the icon.";
300 // Get the icon data, the format is one uint32 each for width and height,
301 // followed by the actual pixel data.
304 display_, id.id(), net_wm_icon_, 0, size, False, XA_CARDINAL,
305 &ret_type, &format, &length, &bytes_after, &data) == Success &&
307 uint32* data_ptr = reinterpret_cast<uint32*>(data);
311 if (size < static_cast<unsigned long>(w * h + 2)) {
313 LOG(LS_ERROR) << "Not a vaild icon.";
317 ArgbToRgba(&data_ptr[2], 0, 0, w, h, w, h, true);
323 LOG(LS_ERROR) << "Failed to get window icon data.";
328 uint8* GetWindowThumbnail(const WindowId& id, int width, int height) {
333 if (!has_composite_extension_) {
334 // Without the Xcomposite extension we would only get a good thumbnail if
335 // the whole window is visible on screen and not covered by any
336 // other window. This is not something we want so instead, just
338 LOG(LS_INFO) << "No Xcomposite extension detected.";
341 XErrorSuppressor error_suppressor(display_);
346 unsigned int src_width;
347 unsigned int src_height;
348 unsigned int border_width;
351 // In addition to needing X11 server-side support for Xcomposite, it
352 // actually needs to be turned on for this window in order to get a good
353 // thumbnail. If the user has modern hardware/drivers but isn't using a
354 // compositing window manager, that won't be the case. Here we
355 // automatically turn it on for shareable windows so that we can get
356 // thumbnails. We used to avoid it because the transition is visually ugly,
357 // but recent window managers don't always redirect windows which led to
358 // no thumbnails at all, which is a worse experience.
360 // Redirect drawing to an offscreen buffer (ie, turn on compositing).
361 // X11 remembers what has requested this and will turn it off for us when
363 XCompositeRedirectWindow(display_, id.id(), CompositeRedirectAutomatic);
364 Pixmap src_pixmap = XCompositeNameWindowPixmap(display_, id.id());
366 // Even if the backing pixmap doesn't exist, this still should have
367 // succeeded and returned a valid handle (it just wouldn't be a handle to
368 // anything). So this is a real error path.
369 LOG(LS_ERROR) << "XCompositeNameWindowPixmap() failed";
372 if (!XGetGeometry(display_, src_pixmap, &root, &x, &y,
373 &src_width, &src_height, &border_width,
375 // If the window does not actually have a backing pixmap, this is the path
376 // that will "fail", so it's a warning rather than an error.
377 LOG(LS_WARNING) << "XGetGeometry() failed (probably composite is not in "
379 XFreePixmap(display_, src_pixmap);
383 // If we get to here, then composite is in use for this window and it has a
384 // valid backing pixmap.
386 XWindowAttributes attr;
387 if (!XGetWindowAttributes(display_, id.id(), &attr)) {
388 LOG(LS_ERROR) << "XGetWindowAttributes() failed";
389 XFreePixmap(display_, src_pixmap);
393 uint8* data = GetDrawableThumbnail(src_pixmap,
399 XFreePixmap(display_, src_pixmap);
403 int GetNumDesktops() {
408 return XScreenCount(display_);
411 uint8* GetDesktopThumbnail(const DesktopId& id, int width, int height) {
415 XErrorSuppressor error_suppressor(display_);
417 Window root_window = id.id();
418 XWindowAttributes attr;
419 if (!XGetWindowAttributes(display_, root_window, &attr)) {
420 LOG(LS_ERROR) << "XGetWindowAttributes() failed";
424 return GetDrawableThumbnail(root_window,
432 bool GetDesktopDimensions(const DesktopId& id, int* width, int* height) {
436 XErrorSuppressor error_suppressor(display_);
437 XWindowAttributes attr;
438 if (!XGetWindowAttributes(display_, id.id(), &attr)) {
439 LOG(LS_ERROR) << "XGetWindowAttributes() failed";
443 *height = attr.height;
448 uint8* GetDrawableThumbnail(Drawable src_drawable,
454 if (!has_render_extension_) {
455 // Without the Xrender extension we would have to read the full window and
456 // scale it down in our process. Xrender is over a decade old so we aren't
457 // going to expend effort to support that situation. We still need to
458 // check though because probably some virtual VNC displays are in this
460 LOG(LS_INFO) << "No Xrender extension detected.";
464 XRenderPictFormat* format = XRenderFindVisualFormat(display_,
467 LOG(LS_ERROR) << "XRenderFindVisualFormat() failed";
471 // Create a picture to reference the window pixmap.
472 XRenderPictureAttributes pa;
473 pa.subwindow_mode = IncludeInferiors; // Don't clip child widgets
474 Picture src = XRenderCreatePicture(display_,
480 LOG(LS_ERROR) << "XRenderCreatePicture() failed";
484 // Create a picture to reference the destination pixmap.
485 Pixmap dst_pixmap = XCreatePixmap(display_,
491 LOG(LS_ERROR) << "XCreatePixmap() failed";
492 XRenderFreePicture(display_, src);
496 Picture dst = XRenderCreatePicture(display_, dst_pixmap, format, 0, NULL);
498 LOG(LS_ERROR) << "XRenderCreatePicture() failed";
499 XFreePixmap(display_, dst_pixmap);
500 XRenderFreePicture(display_, src);
504 // Clear the background.
505 XRenderColor transparent = {0};
506 XRenderFillRectangle(display_,
515 // Calculate how much we need to scale the image.
516 double scale_x = static_cast<double>(dst_width) /
517 static_cast<double>(src_width);
518 double scale_y = static_cast<double>(dst_height) /
519 static_cast<double>(src_height);
520 double scale = rtc::_min(scale_y, scale_x);
522 int scaled_width = round(src_width * scale);
523 int scaled_height = round(src_height * scale);
525 // Render the thumbnail centered on both axis.
526 int centered_x = (dst_width - scaled_width) / 2;
527 int centered_y = (dst_height - scaled_height) / 2;
530 XTransform xform = { {
531 { XDoubleToFixed(1), XDoubleToFixed(0), XDoubleToFixed(0) },
532 { XDoubleToFixed(0), XDoubleToFixed(1), XDoubleToFixed(0) },
533 { XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(scale) }
535 XRenderSetPictureTransform(display_, src, &xform);
537 // Apply filter to smooth out the image.
538 XRenderSetPictureFilter(display_, src, FilterBest, NULL, 0);
540 // Render the image to the destination picture.
541 XRenderComposite(display_,
555 // Get the pixel data from the X server. TODO: XGetImage
556 // might be slow here, compare with ShmGetImage.
557 XImage* image = XGetImage(display_,
564 uint8* data = ArgbToRgba(reinterpret_cast<uint32*>(image->data),
572 XDestroyImage(image);
573 XRenderFreePicture(display_, dst);
574 XFreePixmap(display_, dst_pixmap);
575 XRenderFreePicture(display_, src);
579 uint8* ArgbToRgba(uint32* argb_data, int x, int y, int w, int h,
580 int stride_x, int stride_y, bool has_alpha) {
582 int len = stride_x * stride_y * 4;
583 uint8* data = new uint8[len];
584 memset(data, 0, len);
585 p = data + 4 * (y * stride_x + x);
586 for (int i = 0; i < h; ++i) {
587 for (int j = 0; j < w; ++j) {
590 argb = argb_data[stride_x * (y + i) + x + j];
591 rgba = (argb << 8) | (argb >> 24);
594 *p = (rgba >> 16) & 0xff;
596 *p = (rgba >> 8) & 0xff;
598 *p = has_alpha ? rgba & 0xFF : 0xFF;
601 p += (stride_x - w) * 4;
606 bool EnumerateScreenWindows(WindowDescriptionList* descriptions, int screen) {
610 unsigned int num_children;
611 Window root_window = XRootWindow(display_, screen);
612 status = XQueryTree(display_, root_window, &root_window, &parent, &children,
615 LOG(LS_ERROR) << "Failed to query for child windows.";
618 for (unsigned int i = 0; i < num_children; ++i) {
619 // Iterate in reverse order to display windows from front to back.
621 // TODO(jhorwich): Short-term fix for crbug.com/120229: Don't need to
622 // filter, just return all windows and let the picker scan through them.
623 Window app_window = children[num_children - 1 - i];
625 Window app_window = GetApplicationWindow(children[num_children - 1 - i]);
628 !LinuxWindowPicker::IsDesktopElement(display_, app_window)) {
630 if (GetWindowTitle(app_window, &title)) {
631 WindowId id(app_window);
632 WindowDescription desc(id, title);
633 descriptions->push_back(desc);
637 if (children != NULL) {
643 bool GetWindowTitle(Window window, std::string* title) {
646 XTextProperty window_name;
647 window_name.value = NULL;
649 status = XGetWMName(display_, window, &window_name);
650 if (status && window_name.value && window_name.nitems) {
653 status = Xutf8TextPropertyToTextList(display_, &window_name, &list,
655 if (status >= Success && cnt && *list) {
657 LOG(LS_INFO) << "Window has " << cnt
658 << " text properties, only using the first one.";
664 XFreeStringList(list);
667 if (window_name.value != NULL) {
668 XFree(window_name.value);
674 Window GetApplicationWindow(Window window) {
676 Window app_window = 0;
678 unsigned int num_children;
681 unsigned long nitems, after;
684 int ret = XGetWindowProperty(display_, window,
686 False, wm_state_, &type, &format,
687 &nitems, &after, &data);
688 if (ret != Success) {
689 LOG(LS_ERROR) << "XGetWindowProperty failed with return code " << ret
690 << " for window " << window << ".";
694 int64 state = static_cast<int64>(*data);
696 return state == NormalState ? window : 0;
699 if (!XQueryTree(display_, window, &root, &parent, &children,
701 LOG(LS_ERROR) << "Failed to query for child windows although window"
702 << "does not have a valid WM_STATE.";
705 for (unsigned int i = 0; i < num_children; ++i) {
706 app_window = GetApplicationWindow(children[i]);
711 if (children != NULL) {
720 bool has_composite_extension_;
721 bool has_render_extension_;
724 LinuxWindowPicker::LinuxWindowPicker() : enumerator_(new XWindowEnumerator()) {
727 LinuxWindowPicker::~LinuxWindowPicker() {
730 bool LinuxWindowPicker::IsDesktopElement(_XDisplay* display, Window window) {
732 LOG(LS_WARNING) << "Zero is never a valid window.";
736 // First look for _NET_WM_WINDOW_TYPE. The standard
737 // (http://standards.freedesktop.org/wm-spec/latest/ar01s05.html#id2760306)
738 // says this hint *should* be present on all windows, and we use the existence
739 // of _NET_WM_WINDOW_TYPE_NORMAL in the property to indicate a window is not
740 // a desktop element (that is, only "normal" windows should be shareable).
741 Atom window_type_atom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", True);
742 XWindowProperty<uint32_t> window_type(display, window, window_type_atom);
743 if (window_type.succeeded() && window_type.size() > 0) {
744 Atom normal_window_type_atom = XInternAtom(
745 display, "_NET_WM_WINDOW_TYPE_NORMAL", True);
746 uint32_t* end = window_type.data() + window_type.size();
747 bool is_normal = (end != std::find(
748 window_type.data(), end, normal_window_type_atom));
752 // Fall back on using the hint.
753 XClassHint class_hint;
754 Status s = XGetClassHint(display, window, &class_hint);
757 // No hints, assume this is a normal application window.
760 static const std::string gnome_panel("gnome-panel");
761 static const std::string desktop_window("desktop_window");
763 if (gnome_panel.compare(class_hint.res_name) == 0 ||
764 desktop_window.compare(class_hint.res_name) == 0) {
767 XFree(class_hint.res_name);
768 XFree(class_hint.res_class);
772 bool LinuxWindowPicker::Init() {
773 return enumerator_->Init();
776 bool LinuxWindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
777 return enumerator_->EnumerateWindows(descriptions);
780 bool LinuxWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
781 return enumerator_->EnumerateDesktops(descriptions);
784 bool LinuxWindowPicker::IsVisible(const WindowId& id) {
785 return enumerator_->IsVisible(id);
788 bool LinuxWindowPicker::MoveToFront(const WindowId& id) {
789 return enumerator_->MoveToFront(id);
793 uint8* LinuxWindowPicker::GetWindowIcon(const WindowId& id, int* width,
795 return enumerator_->GetWindowIcon(id, width, height);
798 uint8* LinuxWindowPicker::GetWindowThumbnail(const WindowId& id, int width,
800 return enumerator_->GetWindowThumbnail(id, width, height);
803 int LinuxWindowPicker::GetNumDesktops() {
804 return enumerator_->GetNumDesktops();
807 uint8* LinuxWindowPicker::GetDesktopThumbnail(const DesktopId& id,
810 return enumerator_->GetDesktopThumbnail(id, width, height);
813 bool LinuxWindowPicker::GetDesktopDimensions(const DesktopId& id, int* width,
815 return enumerator_->GetDesktopDimensions(id, width, height);