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.

833 lines
27 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/linuxwindowpicker.h"
  28. #include <math.h>
  29. #include <string.h>
  30. #include <algorithm>
  31. #include <string>
  32. #include <X11/Xatom.h>
  33. #include <X11/extensions/Xcomposite.h>
  34. #include <X11/extensions/Xrender.h>
  35. #include <X11/Xutil.h>
  36. #include "talk/base/logging.h"
  37. namespace talk_base {
  38. // Convenience wrapper for XGetWindowProperty results.
  39. template <class PropertyType>
  40. class XWindowProperty {
  41. public:
  42. XWindowProperty(Display* display, Window window, Atom property)
  43. : data_(NULL) {
  44. const int kBitsPerByte = 8;
  45. Atom actual_type;
  46. int actual_format;
  47. unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty
  48. int status = XGetWindowProperty(display, window, property, 0L, ~0L, False,
  49. AnyPropertyType, &actual_type,
  50. &actual_format, &size_,
  51. &bytes_after, &data_);
  52. succeeded_ = (status == Success);
  53. if (!succeeded_) {
  54. data_ = NULL; // Ensure nothing is freed.
  55. } else if (sizeof(PropertyType) * kBitsPerByte != actual_format) {
  56. LOG(LS_WARNING) << "Returned type size differs from "
  57. "requested type size.";
  58. succeeded_ = false;
  59. // We still need to call XFree in this case, so leave data_ alone.
  60. }
  61. if (!succeeded_) {
  62. size_ = 0;
  63. }
  64. }
  65. ~XWindowProperty() {
  66. if (data_) {
  67. XFree(data_);
  68. }
  69. }
  70. bool succeeded() const { return succeeded_; }
  71. size_t size() const { return size_; }
  72. const PropertyType* data() const {
  73. return reinterpret_cast<PropertyType*>(data_);
  74. }
  75. PropertyType* data() {
  76. return reinterpret_cast<PropertyType*>(data_);
  77. }
  78. private:
  79. bool succeeded_;
  80. unsigned long size_; // NOLINT: type required by XGetWindowProperty
  81. unsigned char* data_;
  82. DISALLOW_COPY_AND_ASSIGN(XWindowProperty);
  83. };
  84. // Stupid X11. It seems none of the synchronous returns codes from X11 calls
  85. // are meaningful unless an asynchronous error handler is configured. This
  86. // RAII class registers and unregisters an X11 error handler.
  87. class XErrorSuppressor {
  88. public:
  89. explicit XErrorSuppressor(Display* display)
  90. : display_(display), original_error_handler_(NULL) {
  91. SuppressX11Errors();
  92. }
  93. ~XErrorSuppressor() {
  94. UnsuppressX11Errors();
  95. }
  96. private:
  97. static int ErrorHandler(Display* display, XErrorEvent* e) {
  98. char buf[256];
  99. XGetErrorText(display, e->error_code, buf, sizeof buf);
  100. LOG(LS_WARNING) << "Received X11 error \"" << buf << "\" for request code "
  101. << static_cast<unsigned int>(e->request_code);
  102. return 0;
  103. }
  104. void SuppressX11Errors() {
  105. XFlush(display_);
  106. XSync(display_, False);
  107. original_error_handler_ = XSetErrorHandler(&ErrorHandler);
  108. }
  109. void UnsuppressX11Errors() {
  110. XFlush(display_);
  111. XSync(display_, False);
  112. XErrorHandler handler = XSetErrorHandler(original_error_handler_);
  113. if (handler != &ErrorHandler) {
  114. LOG(LS_WARNING) << "Unbalanced XSetErrorHandler() calls detected. "
  115. << "Final error handler may not be what you expect!";
  116. }
  117. original_error_handler_ = NULL;
  118. }
  119. Display* display_;
  120. XErrorHandler original_error_handler_;
  121. DISALLOW_COPY_AND_ASSIGN(XErrorSuppressor);
  122. };
  123. // Hiding all X11 specifics inside its own class. This to avoid
  124. // conflicts between talk and X11 header declarations.
  125. class XWindowEnumerator {
  126. public:
  127. XWindowEnumerator()
  128. : display_(NULL),
  129. has_composite_extension_(false),
  130. has_render_extension_(false) {
  131. }
  132. ~XWindowEnumerator() {
  133. if (display_ != NULL) {
  134. XCloseDisplay(display_);
  135. }
  136. }
  137. bool Init() {
  138. if (display_ != NULL) {
  139. // Already initialized.
  140. return true;
  141. }
  142. display_ = XOpenDisplay(NULL);
  143. if (display_ == NULL) {
  144. LOG(LS_ERROR) << "Failed to open display.";
  145. return false;
  146. }
  147. XErrorSuppressor error_suppressor(display_);
  148. wm_state_ = XInternAtom(display_, "WM_STATE", True);
  149. net_wm_icon_ = XInternAtom(display_, "_NET_WM_ICON", False);
  150. int event_base, error_base, major_version, minor_version;
  151. if (XCompositeQueryExtension(display_, &event_base, &error_base) &&
  152. XCompositeQueryVersion(display_, &major_version, &minor_version) &&
  153. // XCompositeNameWindowPixmap() requires version 0.2
  154. (major_version > 0 || minor_version >= 2)) {
  155. has_composite_extension_ = true;
  156. } else {
  157. LOG(LS_INFO) << "Xcomposite extension not available or too old.";
  158. }
  159. if (XRenderQueryExtension(display_, &event_base, &error_base) &&
  160. XRenderQueryVersion(display_, &major_version, &minor_version) &&
  161. // XRenderSetPictureTransform() requires version 0.6
  162. (major_version > 0 || minor_version >= 6)) {
  163. has_render_extension_ = true;
  164. } else {
  165. LOG(LS_INFO) << "Xrender extension not available or too old.";
  166. }
  167. return true;
  168. }
  169. bool EnumerateWindows(WindowDescriptionList* descriptions) {
  170. if (!Init()) {
  171. return false;
  172. }
  173. XErrorSuppressor error_suppressor(display_);
  174. int num_screens = XScreenCount(display_);
  175. bool result = false;
  176. for (int i = 0; i < num_screens; ++i) {
  177. if (EnumerateScreenWindows(descriptions, i)) {
  178. // We know we succeded on at least one screen.
  179. result = true;
  180. }
  181. }
  182. return result;
  183. }
  184. bool EnumerateDesktops(DesktopDescriptionList* descriptions) {
  185. if (!Init()) {
  186. return false;
  187. }
  188. XErrorSuppressor error_suppressor(display_);
  189. int num_screens = XScreenCount(display_);
  190. for (int i = 0; i < num_screens; ++i) {
  191. Window root_window = XRootWindow(display_, i);
  192. DesktopId id(DesktopId(root_window, i));
  193. // TODO: Figure out an appropriate desktop title.
  194. DesktopDescription desc(id, "");
  195. descriptions->push_back(desc);
  196. }
  197. return num_screens > 0;
  198. }
  199. bool IsVisible(const WindowId& id) {
  200. if (!Init()) {
  201. return false;
  202. }
  203. XErrorSuppressor error_suppressor(display_);
  204. XWindowAttributes attr;
  205. if (!XGetWindowAttributes(display_, id.id(), &attr)) {
  206. LOG(LS_ERROR) << "XGetWindowAttributes() failed";
  207. return false;
  208. }
  209. return attr.map_state == IsViewable;
  210. }
  211. bool MoveToFront(const WindowId& id) {
  212. if (!Init()) {
  213. return false;
  214. }
  215. XErrorSuppressor error_suppressor(display_);
  216. unsigned int num_children;
  217. Window* children;
  218. Window parent;
  219. Window root;
  220. // Find root window to pass event to.
  221. int status = XQueryTree(display_, id.id(), &root, &parent, &children,
  222. &num_children);
  223. if (status == 0) {
  224. LOG(LS_WARNING) << "Failed to query for child windows.";
  225. return false;
  226. }
  227. if (children != NULL) {
  228. XFree(children);
  229. }
  230. // Move the window to front.
  231. XRaiseWindow(display_, id.id());
  232. // Some window managers (e.g., metacity in GNOME) consider it illegal to
  233. // raise a window without also giving it input focus with
  234. // _NET_ACTIVE_WINDOW, so XRaiseWindow() on its own isn't enough.
  235. Atom atom = XInternAtom(display_, "_NET_ACTIVE_WINDOW", True);
  236. if (atom != None) {
  237. XEvent xev;
  238. long event_mask;
  239. xev.xclient.type = ClientMessage;
  240. xev.xclient.serial = 0;
  241. xev.xclient.send_event = True;
  242. xev.xclient.window = id.id();
  243. xev.xclient.message_type = atom;
  244. // The format member is set to 8, 16, or 32 and specifies whether the
  245. // data should be viewed as a list of bytes, shorts, or longs.
  246. xev.xclient.format = 32;
  247. xev.xclient.data.l[0] = 0;
  248. xev.xclient.data.l[1] = 0;
  249. xev.xclient.data.l[2] = 0;
  250. xev.xclient.data.l[3] = 0;
  251. xev.xclient.data.l[4] = 0;
  252. event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
  253. XSendEvent(display_, root, False, event_mask, &xev);
  254. }
  255. XFlush(display_);
  256. return true;
  257. }
  258. uint8* GetWindowIcon(const WindowId& id, int* width, int* height) {
  259. if (!Init()) {
  260. return NULL;
  261. }
  262. XErrorSuppressor error_suppressor(display_);
  263. Atom ret_type;
  264. int format;
  265. unsigned long length, bytes_after, size;
  266. unsigned char* data = NULL;
  267. // Find out the size of the icon data.
  268. if (XGetWindowProperty(
  269. display_, id.id(), net_wm_icon_, 0, 0, False, XA_CARDINAL,
  270. &ret_type, &format, &length, &size, &data) == Success &&
  271. data) {
  272. XFree(data);
  273. } else {
  274. LOG(LS_ERROR) << "Failed to get size of the icon.";
  275. return NULL;
  276. }
  277. // Get the icon data, the format is one uint32 each for width and height,
  278. // followed by the actual pixel data.
  279. if (size >= 2 &&
  280. XGetWindowProperty(
  281. display_, id.id(), net_wm_icon_, 0, size, False, XA_CARDINAL,
  282. &ret_type, &format, &length, &bytes_after, &data) == Success &&
  283. data) {
  284. uint32* data_ptr = reinterpret_cast<uint32*>(data);
  285. int w, h;
  286. w = data_ptr[0];
  287. h = data_ptr[1];
  288. if (size < static_cast<unsigned long>(w * h + 2)) {
  289. XFree(data);
  290. LOG(LS_ERROR) << "Not a vaild icon.";
  291. return NULL;
  292. }
  293. uint8* rgba =
  294. ArgbToRgba(&data_ptr[2], 0, 0, w, h, w, h, true);
  295. XFree(data);
  296. *width = w;
  297. *height = h;
  298. return rgba;
  299. } else {
  300. LOG(LS_ERROR) << "Failed to get window icon data.";
  301. return NULL;
  302. }
  303. }
  304. uint8* GetWindowThumbnail(const WindowId& id, int width, int height) {
  305. if (!Init()) {
  306. return NULL;
  307. }
  308. if (!has_composite_extension_) {
  309. // Without the Xcomposite extension we would only get a good thumbnail if
  310. // the whole window is visible on screen and not covered by any
  311. // other window. This is not something we want so instead, just
  312. // bail out.
  313. LOG(LS_INFO) << "No Xcomposite extension detected.";
  314. return NULL;
  315. }
  316. XErrorSuppressor error_suppressor(display_);
  317. Window root;
  318. int x;
  319. int y;
  320. unsigned int src_width;
  321. unsigned int src_height;
  322. unsigned int border_width;
  323. unsigned int depth;
  324. // In addition to needing X11 server-side support for Xcomposite, it
  325. // actually needs to be turned on for this window in order to get a good
  326. // thumbnail. If the user has modern hardware/drivers but isn't using a
  327. // compositing window manager, that won't be the case. Here we
  328. // automatically turn it on for shareable windows so that we can get
  329. // thumbnails. We used to avoid it because the transition is visually ugly,
  330. // but recent window managers don't always redirect windows which led to
  331. // no thumbnails at all, which is a worse experience.
  332. // Redirect drawing to an offscreen buffer (ie, turn on compositing).
  333. // X11 remembers what has requested this and will turn it off for us when
  334. // we exit.
  335. XCompositeRedirectWindow(display_, id.id(), CompositeRedirectAutomatic);
  336. Pixmap src_pixmap = XCompositeNameWindowPixmap(display_, id.id());
  337. if (!src_pixmap) {
  338. // Even if the backing pixmap doesn't exist, this still should have
  339. // succeeded and returned a valid handle (it just wouldn't be a handle to
  340. // anything). So this is a real error path.
  341. LOG(LS_ERROR) << "XCompositeNameWindowPixmap() failed";
  342. return NULL;
  343. }
  344. if (!XGetGeometry(display_, src_pixmap, &root, &x, &y,
  345. &src_width, &src_height, &border_width,
  346. &depth)) {
  347. // If the window does not actually have a backing pixmap, this is the path
  348. // that will "fail", so it's a warning rather than an error.
  349. LOG(LS_WARNING) << "XGetGeometry() failed (probably composite is not in "
  350. << "use)";
  351. XFreePixmap(display_, src_pixmap);
  352. return NULL;
  353. }
  354. // If we get to here, then composite is in use for this window and it has a
  355. // valid backing pixmap.
  356. XWindowAttributes attr;
  357. if (!XGetWindowAttributes(display_, id.id(), &attr)) {
  358. LOG(LS_ERROR) << "XGetWindowAttributes() failed";
  359. XFreePixmap(display_, src_pixmap);
  360. return NULL;
  361. }
  362. uint8* data = GetDrawableThumbnail(src_pixmap,
  363. attr.visual,
  364. src_width,
  365. src_height,
  366. width,
  367. height);
  368. XFreePixmap(display_, src_pixmap);
  369. return data;
  370. }
  371. int GetNumDesktops() {
  372. if (!Init()) {
  373. return -1;
  374. }
  375. return XScreenCount(display_);
  376. }
  377. uint8* GetDesktopThumbnail(const DesktopId& id, int width, int height) {
  378. if (!Init()) {
  379. return NULL;
  380. }
  381. XErrorSuppressor error_suppressor(display_);
  382. Window root_window = id.id();
  383. XWindowAttributes attr;
  384. if (!XGetWindowAttributes(display_, root_window, &attr)) {
  385. LOG(LS_ERROR) << "XGetWindowAttributes() failed";
  386. return NULL;
  387. }
  388. return GetDrawableThumbnail(root_window,
  389. attr.visual,
  390. attr.width,
  391. attr.height,
  392. width,
  393. height);
  394. }
  395. bool GetDesktopDimensions(const DesktopId& id, int* width, int* height) {
  396. if (!Init()) {
  397. return false;
  398. }
  399. XErrorSuppressor error_suppressor(display_);
  400. XWindowAttributes attr;
  401. if (!XGetWindowAttributes(display_, id.id(), &attr)) {
  402. LOG(LS_ERROR) << "XGetWindowAttributes() failed";
  403. return false;
  404. }
  405. *width = attr.width;
  406. *height = attr.height;
  407. return true;
  408. }
  409. private:
  410. uint8* GetDrawableThumbnail(Drawable src_drawable,
  411. Visual* visual,
  412. int src_width,
  413. int src_height,
  414. int dst_width,
  415. int dst_height) {
  416. if (!has_render_extension_) {
  417. // Without the Xrender extension we would have to read the full window and
  418. // scale it down in our process. Xrender is over a decade old so we aren't
  419. // going to expend effort to support that situation. We still need to
  420. // check though because probably some virtual VNC displays are in this
  421. // category.
  422. LOG(LS_INFO) << "No Xrender extension detected.";
  423. return NULL;
  424. }
  425. XRenderPictFormat* format = XRenderFindVisualFormat(display_,
  426. visual);
  427. if (!format) {
  428. LOG(LS_ERROR) << "XRenderFindVisualFormat() failed";
  429. return NULL;
  430. }
  431. // Create a picture to reference the window pixmap.
  432. XRenderPictureAttributes pa;
  433. pa.subwindow_mode = IncludeInferiors; // Don't clip child widgets
  434. Picture src = XRenderCreatePicture(display_,
  435. src_drawable,
  436. format,
  437. CPSubwindowMode,
  438. &pa);
  439. if (!src) {
  440. LOG(LS_ERROR) << "XRenderCreatePicture() failed";
  441. return NULL;
  442. }
  443. // Create a picture to reference the destination pixmap.
  444. Pixmap dst_pixmap = XCreatePixmap(display_,
  445. src_drawable,
  446. dst_width,
  447. dst_height,
  448. format->depth);
  449. if (!dst_pixmap) {
  450. LOG(LS_ERROR) << "XCreatePixmap() failed";
  451. XRenderFreePicture(display_, src);
  452. return NULL;
  453. }
  454. Picture dst = XRenderCreatePicture(display_, dst_pixmap, format, 0, NULL);
  455. if (!dst) {
  456. LOG(LS_ERROR) << "XRenderCreatePicture() failed";
  457. XFreePixmap(display_, dst_pixmap);
  458. XRenderFreePicture(display_, src);
  459. return NULL;
  460. }
  461. // Clear the background.
  462. XRenderColor transparent = {0};
  463. XRenderFillRectangle(display_,
  464. PictOpSrc,
  465. dst,
  466. &transparent,
  467. 0,
  468. 0,
  469. dst_width,
  470. dst_height);
  471. // Calculate how much we need to scale the image.
  472. double scale_x = static_cast<double>(dst_width) /
  473. static_cast<double>(src_width);
  474. double scale_y = static_cast<double>(dst_height) /
  475. static_cast<double>(src_height);
  476. double scale = talk_base::_min(scale_y, scale_x);
  477. int scaled_width = round(src_width * scale);
  478. int scaled_height = round(src_height * scale);
  479. // Render the thumbnail centered on both axis.
  480. int centered_x = (dst_width - scaled_width) / 2;
  481. int centered_y = (dst_height - scaled_height) / 2;
  482. // Scaling matrix
  483. XTransform xform = { {
  484. { XDoubleToFixed(1), XDoubleToFixed(0), XDoubleToFixed(0) },
  485. { XDoubleToFixed(0), XDoubleToFixed(1), XDoubleToFixed(0) },
  486. { XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(scale) }
  487. } };
  488. XRenderSetPictureTransform(display_, src, &xform);
  489. // Apply filter to smooth out the image.
  490. XRenderSetPictureFilter(display_, src, FilterBest, NULL, 0);
  491. // Render the image to the destination picture.
  492. XRenderComposite(display_,
  493. PictOpSrc,
  494. src,
  495. None,
  496. dst,
  497. 0,
  498. 0,
  499. 0,
  500. 0,
  501. centered_x,
  502. centered_y,
  503. scaled_width,
  504. scaled_height);
  505. // Get the pixel data from the X server. TODO: XGetImage
  506. // might be slow here, compare with ShmGetImage.
  507. XImage* image = XGetImage(display_,
  508. dst_pixmap,
  509. 0,
  510. 0,
  511. dst_width,
  512. dst_height,
  513. AllPlanes, ZPixmap);
  514. uint8* data = ArgbToRgba(reinterpret_cast<uint32*>(image->data),
  515. centered_x,
  516. centered_y,
  517. scaled_width,
  518. scaled_height,
  519. dst_width,
  520. dst_height,
  521. false);
  522. XDestroyImage(image);
  523. XRenderFreePicture(display_, dst);
  524. XFreePixmap(display_, dst_pixmap);
  525. XRenderFreePicture(display_, src);
  526. return data;
  527. }
  528. uint8* ArgbToRgba(uint32* argb_data, int x, int y, int w, int h,
  529. int stride_x, int stride_y, bool has_alpha) {
  530. uint8* p;
  531. int len = stride_x * stride_y * 4;
  532. uint8* data = new uint8[len];
  533. memset(data, 0, len);
  534. p = data + 4 * (y * stride_x + x);
  535. for (int i = 0; i < h; ++i) {
  536. for (int j = 0; j < w; ++j) {
  537. uint32 argb;
  538. uint32 rgba;
  539. argb = argb_data[stride_x * (y + i) + x + j];
  540. rgba = (argb << 8) | (argb >> 24);
  541. *p = rgba >> 24;
  542. ++p;
  543. *p = (rgba >> 16) & 0xff;
  544. ++p;
  545. *p = (rgba >> 8) & 0xff;
  546. ++p;
  547. *p = has_alpha ? rgba & 0xFF : 0xFF;
  548. ++p;
  549. }
  550. p += (stride_x - w) * 4;
  551. }
  552. return data;
  553. }
  554. bool EnumerateScreenWindows(WindowDescriptionList* descriptions, int screen) {
  555. Window parent;
  556. Window *children;
  557. int status;
  558. unsigned int num_children;
  559. Window root_window = XRootWindow(display_, screen);
  560. status = XQueryTree(display_, root_window, &root_window, &parent, &children,
  561. &num_children);
  562. if (status == 0) {
  563. LOG(LS_ERROR) << "Failed to query for child windows.";
  564. return false;
  565. }
  566. for (unsigned int i = 0; i < num_children; ++i) {
  567. // Iterate in reverse order to display windows from front to back.
  568. #ifdef CHROMEOS
  569. // TODO(jhorwich): Short-term fix for crbug.com/120229: Don't need to
  570. // filter, just return all windows and let the picker scan through them.
  571. Window app_window = children[num_children - 1 - i];
  572. #else
  573. Window app_window = GetApplicationWindow(children[num_children - 1 - i]);
  574. #endif
  575. if (app_window &&
  576. !LinuxWindowPicker::IsDesktopElement(display_, app_window)) {
  577. std::string title;
  578. if (GetWindowTitle(app_window, &title)) {
  579. WindowId id(app_window);
  580. WindowDescription desc(id, title);
  581. descriptions->push_back(desc);
  582. }
  583. }
  584. }
  585. if (children != NULL) {
  586. XFree(children);
  587. }
  588. return true;
  589. }
  590. bool GetWindowTitle(Window window, std::string* title) {
  591. int status;
  592. bool result = false;
  593. XTextProperty window_name;
  594. window_name.value = NULL;
  595. if (window) {
  596. status = XGetWMName(display_, window, &window_name);
  597. if (status && window_name.value && window_name.nitems) {
  598. int cnt;
  599. char **list = NULL;
  600. status = Xutf8TextPropertyToTextList(display_, &window_name, &list,
  601. &cnt);
  602. if (status >= Success && cnt && *list) {
  603. if (cnt > 1) {
  604. LOG(LS_INFO) << "Window has " << cnt
  605. << " text properties, only using the first one.";
  606. }
  607. *title = *list;
  608. result = true;
  609. }
  610. if (list != NULL) {
  611. XFreeStringList(list);
  612. }
  613. }
  614. if (window_name.value != NULL) {
  615. XFree(window_name.value);
  616. }
  617. }
  618. return result;
  619. }
  620. Window GetApplicationWindow(Window window) {
  621. Window root, parent;
  622. Window app_window = 0;
  623. Window *children;
  624. unsigned int num_children;
  625. Atom type = None;
  626. int format;
  627. unsigned long nitems, after;
  628. unsigned char *data;
  629. int ret = XGetWindowProperty(display_, window,
  630. wm_state_, 0L, 2,
  631. False, wm_state_, &type, &format,
  632. &nitems, &after, &data);
  633. if (ret != Success) {
  634. LOG(LS_ERROR) << "XGetWindowProperty failed with return code " << ret
  635. << " for window " << window << ".";
  636. return 0;
  637. }
  638. if (type != None) {
  639. int64 state = static_cast<int64>(*data);
  640. XFree(data);
  641. return state == NormalState ? window : 0;
  642. }
  643. XFree(data);
  644. if (!XQueryTree(display_, window, &root, &parent, &children,
  645. &num_children)) {
  646. LOG(LS_ERROR) << "Failed to query for child windows although window"
  647. << "does not have a valid WM_STATE.";
  648. return 0;
  649. }
  650. for (unsigned int i = 0; i < num_children; ++i) {
  651. app_window = GetApplicationWindow(children[i]);
  652. if (app_window) {
  653. break;
  654. }
  655. }
  656. if (children != NULL) {
  657. XFree(children);
  658. }
  659. return app_window;
  660. }
  661. Atom wm_state_;
  662. Atom net_wm_icon_;
  663. Display* display_;
  664. bool has_composite_extension_;
  665. bool has_render_extension_;
  666. };
  667. LinuxWindowPicker::LinuxWindowPicker() : enumerator_(new XWindowEnumerator()) {
  668. }
  669. LinuxWindowPicker::~LinuxWindowPicker() {
  670. }
  671. bool LinuxWindowPicker::IsDesktopElement(_XDisplay* display, Window window) {
  672. if (window == 0) {
  673. LOG(LS_WARNING) << "Zero is never a valid window.";
  674. return false;
  675. }
  676. // First look for _NET_WM_WINDOW_TYPE. The standard
  677. // (http://standards.freedesktop.org/wm-spec/latest/ar01s05.html#id2760306)
  678. // says this hint *should* be present on all windows, and we use the existence
  679. // of _NET_WM_WINDOW_TYPE_NORMAL in the property to indicate a window is not
  680. // a desktop element (that is, only "normal" windows should be shareable).
  681. Atom window_type_atom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", True);
  682. XWindowProperty<uint32_t> window_type(display, window, window_type_atom);
  683. if (window_type.succeeded() && window_type.size() > 0) {
  684. Atom normal_window_type_atom = XInternAtom(
  685. display, "_NET_WM_WINDOW_TYPE_NORMAL", True);
  686. uint32_t* end = window_type.data() + window_type.size();
  687. bool is_normal = (end != std::find(
  688. window_type.data(), end, normal_window_type_atom));
  689. return !is_normal;
  690. }
  691. // Fall back on using the hint.
  692. XClassHint class_hint;
  693. Status s = XGetClassHint(display, window, &class_hint);
  694. bool result = false;
  695. if (s == 0) {
  696. // No hints, assume this is a normal application window.
  697. return result;
  698. }
  699. static const std::string gnome_panel("gnome-panel");
  700. static const std::string desktop_window("desktop_window");
  701. if (gnome_panel.compare(class_hint.res_name) == 0 ||
  702. desktop_window.compare(class_hint.res_name) == 0) {
  703. result = true;
  704. }
  705. XFree(class_hint.res_name);
  706. XFree(class_hint.res_class);
  707. return result;
  708. }
  709. bool LinuxWindowPicker::Init() {
  710. return enumerator_->Init();
  711. }
  712. bool LinuxWindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
  713. return enumerator_->EnumerateWindows(descriptions);
  714. }
  715. bool LinuxWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
  716. return enumerator_->EnumerateDesktops(descriptions);
  717. }
  718. bool LinuxWindowPicker::IsVisible(const WindowId& id) {
  719. return enumerator_->IsVisible(id);
  720. }
  721. bool LinuxWindowPicker::MoveToFront(const WindowId& id) {
  722. return enumerator_->MoveToFront(id);
  723. }
  724. uint8* LinuxWindowPicker::GetWindowIcon(const WindowId& id, int* width,
  725. int* height) {
  726. return enumerator_->GetWindowIcon(id, width, height);
  727. }
  728. uint8* LinuxWindowPicker::GetWindowThumbnail(const WindowId& id, int width,
  729. int height) {
  730. return enumerator_->GetWindowThumbnail(id, width, height);
  731. }
  732. int LinuxWindowPicker::GetNumDesktops() {
  733. return enumerator_->GetNumDesktops();
  734. }
  735. uint8* LinuxWindowPicker::GetDesktopThumbnail(const DesktopId& id,
  736. int width,
  737. int height) {
  738. return enumerator_->GetDesktopThumbnail(id, width, height);
  739. }
  740. bool LinuxWindowPicker::GetDesktopDimensions(const DesktopId& id, int* width,
  741. int* height) {
  742. return enumerator_->GetDesktopDimensions(id, width, height);
  743. }
  744. } // namespace talk_base