Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: app/x11_util.cc

Issue 6257006: Move a bunch of random other files to src/ui/base... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « app/x11_util.h ('k') | app/x11_util_internal.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines utility functions for X11 (Linux only). This code has been
6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support
7 // remains woefully incomplete.
8
9 #include "app/x11_util.h"
10
11 #include <gdk/gdk.h>
12 #include <gdk/gdkx.h>
13 #include <gtk/gtk.h>
14
15 #include <sys/ipc.h>
16 #include <sys/shm.h>
17
18 #include <list>
19 #include <set>
20
21 #include "base/command_line.h"
22 #include "base/logging.h"
23 #include "base/stringprintf.h"
24 #include "base/string_number_conversions.h"
25 #include "base/threading/thread.h"
26 #include "app/x11_util_internal.h"
27 #include "gfx/rect.h"
28 #include "gfx/size.h"
29
30 namespace x11_util {
31
32 namespace {
33
34 // Used to cache the XRenderPictFormat for a visual/display pair.
35 struct CachedPictFormat {
36 bool equals(Display* display, Visual* visual) const {
37 return display == this->display && visual == this->visual;
38 }
39
40 Display* display;
41 Visual* visual;
42 XRenderPictFormat* format;
43 };
44
45 typedef std::list<CachedPictFormat> CachedPictFormats;
46
47 // Returns the cache of pict formats.
48 CachedPictFormats* get_cached_pict_formats() {
49 static CachedPictFormats* formats = NULL;
50 if (!formats)
51 formats = new CachedPictFormats();
52 return formats;
53 }
54
55 // Maximum number of CachedPictFormats we keep around.
56 const size_t kMaxCacheSize = 5;
57
58 int DefaultX11ErrorHandler(Display* d, XErrorEvent* e) {
59 LOG(ERROR) << GetErrorEventDescription(d, e);
60 return 0;
61 }
62
63 int DefaultX11IOErrorHandler(Display* d) {
64 // If there's an IO error it likely means the X server has gone away
65 LOG(ERROR) << "X IO Error detected";
66 _exit(1);
67 }
68
69 } // namespace
70
71 bool XDisplayExists() {
72 return (gdk_display_get_default() != NULL);
73 }
74
75 Display* GetXDisplay() {
76 static Display* display = NULL;
77
78 if (!display)
79 display = gdk_x11_get_default_xdisplay();
80
81 return display;
82 }
83
84 static SharedMemorySupport DoQuerySharedMemorySupport(Display* dpy) {
85 // A temporary flag for tracking down shared memory problems.
86 // TODO(evanm): remove this.
87 if (CommandLine::ForCurrentProcess()->HasSwitch("disable-xshm"))
88 return SHARED_MEMORY_NONE;
89
90 int dummy;
91 Bool pixmaps_supported;
92 // Query the server's support for XSHM.
93 if (!XShmQueryVersion(dpy, &dummy, &dummy, &pixmaps_supported))
94 return SHARED_MEMORY_NONE;
95
96 // Next we probe to see if shared memory will really work
97 int shmkey = shmget(IPC_PRIVATE, 1, 0666);
98 if (shmkey == -1)
99 return SHARED_MEMORY_NONE;
100 void* address = shmat(shmkey, NULL, 0);
101 // Mark the shared memory region for deletion
102 shmctl(shmkey, IPC_RMID, NULL);
103
104 XShmSegmentInfo shminfo;
105 memset(&shminfo, 0, sizeof(shminfo));
106 shminfo.shmid = shmkey;
107
108 gdk_error_trap_push();
109 bool result = XShmAttach(dpy, &shminfo);
110 XSync(dpy, False);
111 if (gdk_error_trap_pop())
112 result = false;
113 shmdt(address);
114 if (!result)
115 return SHARED_MEMORY_NONE;
116
117 XShmDetach(dpy, &shminfo);
118 return pixmaps_supported ? SHARED_MEMORY_PIXMAP : SHARED_MEMORY_PUTIMAGE;
119 }
120
121 SharedMemorySupport QuerySharedMemorySupport(Display* dpy) {
122 static SharedMemorySupport shared_memory_support = SHARED_MEMORY_NONE;
123 static bool shared_memory_support_cached = false;
124
125 if (shared_memory_support_cached)
126 return shared_memory_support;
127
128 shared_memory_support = DoQuerySharedMemorySupport(dpy);
129 shared_memory_support_cached = true;
130
131 return shared_memory_support;
132 }
133
134 bool QueryRenderSupport(Display* dpy) {
135 static bool render_supported = false;
136 static bool render_supported_cached = false;
137
138 if (render_supported_cached)
139 return render_supported;
140
141 // We don't care about the version of Xrender since all the features which
142 // we use are included in every version.
143 int dummy;
144 render_supported = XRenderQueryExtension(dpy, &dummy, &dummy);
145 render_supported_cached = true;
146
147 return render_supported;
148 }
149
150 int GetDefaultScreen(Display* display) {
151 return XDefaultScreen(display);
152 }
153
154 XID GetX11RootWindow() {
155 return GDK_WINDOW_XID(gdk_get_default_root_window());
156 }
157
158 bool GetCurrentDesktop(int* desktop) {
159 return GetIntProperty(GetX11RootWindow(), "_NET_CURRENT_DESKTOP", desktop);
160 }
161
162 XID GetX11WindowFromGtkWidget(GtkWidget* widget) {
163 return GDK_WINDOW_XID(widget->window);
164 }
165
166 XID GetX11WindowFromGdkWindow(GdkWindow* window) {
167 return GDK_WINDOW_XID(window);
168 }
169
170 void* GetVisualFromGtkWidget(GtkWidget* widget) {
171 return GDK_VISUAL_XVISUAL(gtk_widget_get_visual(widget));
172 }
173
174 int BitsPerPixelForPixmapDepth(Display* dpy, int depth) {
175 int count;
176 XPixmapFormatValues* formats = XListPixmapFormats(dpy, &count);
177 if (!formats)
178 return -1;
179
180 int bits_per_pixel = -1;
181 for (int i = 0; i < count; ++i) {
182 if (formats[i].depth == depth) {
183 bits_per_pixel = formats[i].bits_per_pixel;
184 break;
185 }
186 }
187
188 XFree(formats);
189 return bits_per_pixel;
190 }
191
192 bool IsWindowVisible(XID window) {
193 XWindowAttributes win_attributes;
194 XGetWindowAttributes(GetXDisplay(), window, &win_attributes);
195 if (win_attributes.map_state != IsViewable)
196 return false;
197 // Some compositing window managers (notably kwin) do not actually unmap
198 // windows on desktop switch, so we also must check the current desktop.
199 int window_desktop, current_desktop;
200 return (!GetWindowDesktop(window, &window_desktop) ||
201 !GetCurrentDesktop(&current_desktop) ||
202 window_desktop == kAllDesktops ||
203 window_desktop == current_desktop);
204 }
205
206 bool GetWindowRect(XID window, gfx::Rect* rect) {
207 Window root, child;
208 int x, y;
209 unsigned int width, height;
210 unsigned int border_width, depth;
211
212 if (!XGetGeometry(GetXDisplay(), window, &root, &x, &y,
213 &width, &height, &border_width, &depth))
214 return false;
215
216 if (!XTranslateCoordinates(GetSecondaryDisplay(), window, root,
217 0, 0, &x, &y, &child))
218 return false;
219
220 *rect = gfx::Rect(x, y, width, height);
221 return true;
222 }
223
224 bool GetIntProperty(XID window, const std::string& property_name, int* value) {
225 Atom property_atom = gdk_x11_get_xatom_by_name_for_display(
226 gdk_display_get_default(), property_name.c_str());
227
228 Atom type = None;
229 int format = 0; // size in bits of each item in 'property'
230 long unsigned int num_items = 0, remaining_bytes = 0;
231 unsigned char* property = NULL;
232
233 int result = XGetWindowProperty(GetXDisplay(),
234 window,
235 property_atom,
236 0, // offset into property data to read
237 1, // max length to get
238 False, // deleted
239 AnyPropertyType,
240 &type,
241 &format,
242 &num_items,
243 &remaining_bytes,
244 &property);
245 if (result != Success)
246 return false;
247
248 if (format != 32 || num_items != 1) {
249 XFree(property);
250 return false;
251 }
252
253 *value = *(reinterpret_cast<int*>(property));
254 XFree(property);
255 return true;
256 }
257
258 bool GetIntArrayProperty(XID window,
259 const std::string& property_name,
260 std::vector<int>* value) {
261 Atom property_atom = gdk_x11_get_xatom_by_name_for_display(
262 gdk_display_get_default(), property_name.c_str());
263
264 Atom type = None;
265 int format = 0; // size in bits of each item in 'property'
266 long unsigned int num_items = 0, remaining_bytes = 0;
267 unsigned char* properties = NULL;
268
269 int result = XGetWindowProperty(GetXDisplay(),
270 window,
271 property_atom,
272 0, // offset into property data to read
273 (~0L), // max length to get (all of them)
274 False, // deleted
275 AnyPropertyType,
276 &type,
277 &format,
278 &num_items,
279 &remaining_bytes,
280 &properties);
281 if (result != Success)
282 return false;
283
284 if (format != 32) {
285 XFree(properties);
286 return false;
287 }
288
289 int* int_properties = reinterpret_cast<int*>(properties);
290 value->clear();
291 value->insert(value->begin(), int_properties, int_properties + num_items);
292 XFree(properties);
293 return true;
294 }
295
296 bool GetStringProperty(
297 XID window, const std::string& property_name, std::string* value) {
298 Atom property_atom = gdk_x11_get_xatom_by_name_for_display(
299 gdk_display_get_default(), property_name.c_str());
300
301 Atom type = None;
302 int format = 0; // size in bits of each item in 'property'
303 long unsigned int num_items = 0, remaining_bytes = 0;
304 unsigned char* property = NULL;
305
306 int result = XGetWindowProperty(GetXDisplay(),
307 window,
308 property_atom,
309 0, // offset into property data to read
310 1024, // max length to get
311 False, // deleted
312 AnyPropertyType,
313 &type,
314 &format,
315 &num_items,
316 &remaining_bytes,
317 &property);
318 if (result != Success)
319 return false;
320
321 if (format != 8) {
322 XFree(property);
323 return false;
324 }
325
326 value->assign(reinterpret_cast<char*>(property), num_items);
327 XFree(property);
328 return true;
329 }
330
331 XID GetParentWindow(XID window) {
332 XID root = None;
333 XID parent = None;
334 XID* children = NULL;
335 unsigned int num_children = 0;
336 XQueryTree(GetXDisplay(), window, &root, &parent, &children, &num_children);
337 if (children)
338 XFree(children);
339 return parent;
340 }
341
342 XID GetHighestAncestorWindow(XID window, XID root) {
343 while (true) {
344 XID parent = x11_util::GetParentWindow(window);
345 if (parent == None)
346 return None;
347 if (parent == root)
348 return window;
349 window = parent;
350 }
351 }
352
353 bool GetWindowDesktop(XID window, int* desktop) {
354 return GetIntProperty(window, "_NET_WM_DESKTOP", desktop);
355 }
356
357 // Returns true if |window| is a named window.
358 bool IsWindowNamed(XID window) {
359 XTextProperty prop;
360 if (!XGetWMName(GetXDisplay(), window, &prop) || !prop.value)
361 return false;
362
363 XFree(prop.value);
364 return true;
365 }
366
367 bool EnumerateChildren(EnumerateWindowsDelegate* delegate, XID window,
368 const int max_depth, int depth) {
369 if (depth > max_depth)
370 return false;
371
372 XID root, parent, *children;
373 unsigned int num_children;
374 int status = XQueryTree(GetXDisplay(), window, &root, &parent, &children,
375 &num_children);
376 if (status == 0)
377 return false;
378
379 std::set<XID> windows;
380 for (unsigned int i = 0; i < num_children; i++)
381 windows.insert(children[i]);
382
383 XFree(children);
384
385 // XQueryTree returns the children of |window| in bottom-to-top order, so
386 // reverse-iterate the list to check the windows from top-to-bottom.
387 std::set<XID>::reverse_iterator iter;
388 for (iter = windows.rbegin(); iter != windows.rend(); iter++) {
389 if (IsWindowNamed(*iter) && delegate->ShouldStopIterating(*iter))
390 return true;
391 }
392
393 // If we're at this point, we didn't find the window we're looking for at the
394 // current level, so we need to recurse to the next level. We use a second
395 // loop because the recursion and call to XQueryTree are expensive and is only
396 // needed for a small number of cases.
397 if (++depth <= max_depth) {
398 for (iter = windows.rbegin(); iter != windows.rend(); iter++) {
399 if (EnumerateChildren(delegate, *iter, max_depth, depth))
400 return true;
401 }
402 }
403
404 return false;
405 }
406
407 bool EnumerateAllWindows(EnumerateWindowsDelegate* delegate, int max_depth) {
408 XID root = GetX11RootWindow();
409 return EnumerateChildren(delegate, root, max_depth, 0);
410 }
411
412 bool GetXWindowStack(std::vector<XID>* windows) {
413 windows->clear();
414
415 static Atom atom = XInternAtom(GetXDisplay(),
416 "_NET_CLIENT_LIST_STACKING", False);
417
418 Atom type;
419 int format;
420 unsigned long count;
421 unsigned long bytes_after;
422 unsigned char *data = NULL;
423 if (XGetWindowProperty(GetXDisplay(),
424 GetX11RootWindow(),
425 atom,
426 0, // offset
427 ~0L, // length
428 False, // delete
429 AnyPropertyType, // requested type
430 &type,
431 &format,
432 &count,
433 &bytes_after,
434 &data) != Success) {
435 return false;
436 }
437
438 bool result = false;
439 if (type == XA_WINDOW && format == 32 && data && count > 0) {
440 result = true;
441 XID* stack = reinterpret_cast<XID*>(data);
442 for (unsigned long i = 0; i < count; i++)
443 windows->insert(windows->begin(), stack[i]);
444 }
445
446 if (data)
447 XFree(data);
448
449 return result;
450 }
451
452 void RestackWindow(XID window, XID sibling, bool above) {
453 XWindowChanges changes;
454 changes.sibling = sibling;
455 changes.stack_mode = above ? Above : Below;
456 XConfigureWindow(GetXDisplay(), window, CWSibling | CWStackMode, &changes);
457 }
458
459 XSharedMemoryId AttachSharedMemory(Display* display, int shared_memory_key) {
460 DCHECK(QuerySharedMemorySupport(display));
461
462 XShmSegmentInfo shminfo;
463 memset(&shminfo, 0, sizeof(shminfo));
464 shminfo.shmid = shared_memory_key;
465
466 // This function is only called if QuerySharedMemorySupport returned true. In
467 // which case we've already succeeded in having the X server attach to one of
468 // our shared memory segments.
469 if (!XShmAttach(display, &shminfo))
470 NOTREACHED();
471
472 return shminfo.shmseg;
473 }
474
475 void DetachSharedMemory(Display* display, XSharedMemoryId shmseg) {
476 DCHECK(QuerySharedMemorySupport(display));
477
478 XShmSegmentInfo shminfo;
479 memset(&shminfo, 0, sizeof(shminfo));
480 shminfo.shmseg = shmseg;
481
482 if (!XShmDetach(display, &shminfo))
483 NOTREACHED();
484 }
485
486 XID CreatePictureFromSkiaPixmap(Display* display, XID pixmap) {
487 XID picture = XRenderCreatePicture(
488 display, pixmap, GetRenderARGB32Format(display), 0, NULL);
489
490 return picture;
491 }
492
493 void PutARGBImage(Display* display, void* visual, int depth, XID pixmap,
494 void* pixmap_gc, const uint8* data, int width, int height) {
495 // TODO(scherkus): potential performance impact... consider passing in as a
496 // parameter.
497 int pixmap_bpp = BitsPerPixelForPixmapDepth(display, depth);
498
499 XImage image;
500 memset(&image, 0, sizeof(image));
501
502 image.width = width;
503 image.height = height;
504 image.format = ZPixmap;
505 image.byte_order = LSBFirst;
506 image.bitmap_unit = 8;
507 image.bitmap_bit_order = LSBFirst;
508 image.depth = depth;
509 image.bits_per_pixel = pixmap_bpp;
510 image.bytes_per_line = width * pixmap_bpp / 8;
511
512 if (pixmap_bpp == 32) {
513 image.red_mask = 0xff0000;
514 image.green_mask = 0xff00;
515 image.blue_mask = 0xff;
516
517 // If the X server depth is already 32-bits and the color masks match,
518 // then our job is easy.
519 Visual* vis = static_cast<Visual*>(visual);
520 if (image.red_mask == vis->red_mask &&
521 image.green_mask == vis->green_mask &&
522 image.blue_mask == vis->blue_mask) {
523 image.data = const_cast<char*>(reinterpret_cast<const char*>(data));
524 XPutImage(display, pixmap, static_cast<GC>(pixmap_gc), &image,
525 0, 0 /* source x, y */, 0, 0 /* dest x, y */,
526 width, height);
527 } else {
528 // Otherwise, we need to shuffle the colors around. Assume red and blue
529 // need to be swapped.
530 //
531 // It's possible to use some fancy SSE tricks here, but since this is the
532 // slow path anyway, we do it slowly.
533
534 uint8_t* bitmap32 = static_cast<uint8_t*>(malloc(4 * width * height));
535 if (!bitmap32)
536 return;
537 uint8_t* const orig_bitmap32 = bitmap32;
538 const uint32_t* bitmap_in = reinterpret_cast<const uint32_t*>(data);
539 for (int y = 0; y < height; ++y) {
540 for (int x = 0; x < width; ++x) {
541 const uint32_t pixel = *(bitmap_in++);
542 bitmap32[0] = (pixel >> 16) & 0xff; // Red
543 bitmap32[1] = (pixel >> 8) & 0xff; // Green
544 bitmap32[2] = pixel & 0xff; // Blue
545 bitmap32[3] = (pixel >> 24) & 0xff; // Alpha
546 bitmap32 += 4;
547 }
548 }
549 image.data = reinterpret_cast<char*>(orig_bitmap32);
550 XPutImage(display, pixmap, static_cast<GC>(pixmap_gc), &image,
551 0, 0 /* source x, y */, 0, 0 /* dest x, y */,
552 width, height);
553 free(orig_bitmap32);
554 }
555 } else if (pixmap_bpp == 16) {
556 // Some folks have VNC setups which still use 16-bit visuals and VNC
557 // doesn't include Xrender.
558
559 uint16_t* bitmap16 = static_cast<uint16_t*>(malloc(2 * width * height));
560 if (!bitmap16)
561 return;
562 uint16_t* const orig_bitmap16 = bitmap16;
563 const uint32_t* bitmap_in = reinterpret_cast<const uint32_t*>(data);
564 for (int y = 0; y < height; ++y) {
565 for (int x = 0; x < width; ++x) {
566 const uint32_t pixel = *(bitmap_in++);
567 uint16_t out_pixel = ((pixel >> 8) & 0xf800) |
568 ((pixel >> 5) & 0x07e0) |
569 ((pixel >> 3) & 0x001f);
570 *(bitmap16++) = out_pixel;
571 }
572 }
573
574 image.data = reinterpret_cast<char*>(orig_bitmap16);
575 image.red_mask = 0xf800;
576 image.green_mask = 0x07e0;
577 image.blue_mask = 0x001f;
578
579 XPutImage(display, pixmap, static_cast<GC>(pixmap_gc), &image,
580 0, 0 /* source x, y */, 0, 0 /* dest x, y */,
581 width, height);
582 free(orig_bitmap16);
583 } else {
584 LOG(FATAL) << "Sorry, we don't support your visual depth without "
585 "Xrender support (depth:" << depth
586 << " bpp:" << pixmap_bpp << ")";
587 }
588 }
589
590 void FreePicture(Display* display, XID picture) {
591 XRenderFreePicture(display, picture);
592 }
593
594 void FreePixmap(Display* display, XID pixmap) {
595 XFreePixmap(display, pixmap);
596 }
597
598 // Called on BACKGROUND_X11 thread.
599 Display* GetSecondaryDisplay() {
600 static Display* display = NULL;
601 if (!display) {
602 display = XOpenDisplay(NULL);
603 CHECK(display);
604 }
605
606 return display;
607 }
608
609 // Called on BACKGROUND_X11 thread.
610 bool GetWindowGeometry(int* x, int* y, unsigned* width, unsigned* height,
611 XID window) {
612 Window root_window, child_window;
613 unsigned border_width, depth;
614 int temp;
615
616 if (!XGetGeometry(GetSecondaryDisplay(), window, &root_window, &temp, &temp,
617 width, height, &border_width, &depth))
618 return false;
619 if (!XTranslateCoordinates(GetSecondaryDisplay(), window, root_window,
620 0, 0 /* input x, y */, x, y /* output x, y */,
621 &child_window))
622 return false;
623
624 return true;
625 }
626
627 // Called on BACKGROUND_X11 thread.
628 bool GetWindowParent(XID* parent_window, bool* parent_is_root, XID window) {
629 XID root_window, *children;
630 unsigned num_children;
631
632 Status s = XQueryTree(GetSecondaryDisplay(), window, &root_window,
633 parent_window, &children, &num_children);
634 if (!s)
635 return false;
636
637 if (children)
638 XFree(children);
639
640 *parent_is_root = root_window == *parent_window;
641 return true;
642 }
643
644 bool GetWindowManagerName(std::string* wm_name) {
645 DCHECK(wm_name);
646 int wm_window = 0;
647 if (!x11_util::GetIntProperty(x11_util::GetX11RootWindow(),
648 "_NET_SUPPORTING_WM_CHECK",
649 &wm_window)) {
650 return false;
651 }
652
653 // It's possible that a window manager started earlier in this X session left
654 // a stale _NET_SUPPORTING_WM_CHECK property when it was replaced by a
655 // non-EWMH window manager, so we trap errors in the following requests to
656 // avoid crashes (issue 23860).
657
658 // EWMH requires the supporting-WM window to also have a
659 // _NET_SUPPORTING_WM_CHECK property pointing to itself (to avoid a stale
660 // property referencing an ID that's been recycled for another window), so we
661 // check that too.
662 gdk_error_trap_push();
663 int wm_window_property = 0;
664 bool result = x11_util::GetIntProperty(
665 wm_window, "_NET_SUPPORTING_WM_CHECK", &wm_window_property);
666 gdk_flush();
667 bool got_error = gdk_error_trap_pop();
668 if (got_error || !result || wm_window_property != wm_window)
669 return false;
670
671 gdk_error_trap_push();
672 result = x11_util::GetStringProperty(
673 static_cast<XID>(wm_window), "_NET_WM_NAME", wm_name);
674 gdk_flush();
675 got_error = gdk_error_trap_pop();
676 return !got_error && result;
677 }
678
679 static cairo_status_t SnapshotCallback(
680 void *closure, const unsigned char *data, unsigned int length) {
681 std::vector<unsigned char>* png_representation =
682 static_cast<std::vector<unsigned char>*>(closure);
683
684 size_t old_size = png_representation->size();
685 png_representation->resize(old_size + length);
686 memcpy(&(*png_representation)[old_size], data, length);
687 return CAIRO_STATUS_SUCCESS;
688 }
689
690 void GrabWindowSnapshot(GtkWindow* gtk_window,
691 std::vector<unsigned char>* png_representation) {
692 GdkWindow* gdk_window = GTK_WIDGET(gtk_window)->window;
693 Display* display = GDK_WINDOW_XDISPLAY(gdk_window);
694 XID win = GDK_WINDOW_XID(gdk_window);
695 XWindowAttributes attr;
696 if (XGetWindowAttributes(display, win, &attr) == 0) {
697 LOG(ERROR) << "Couldn't get window attributes";
698 return;
699 }
700 XImage* image = XGetImage(
701 display, win, 0, 0, attr.width, attr.height, AllPlanes, ZPixmap);
702 if (!image) {
703 LOG(ERROR) << "Couldn't get image";
704 return;
705 }
706 if (image->depth != 24) {
707 LOG(ERROR)<< "Unsupported image depth " << image->depth;
708 return;
709 }
710 cairo_surface_t* surface =
711 cairo_image_surface_create_for_data(
712 reinterpret_cast<unsigned char*>(image->data),
713 CAIRO_FORMAT_RGB24,
714 image->width,
715 image->height,
716 image->bytes_per_line);
717
718 if (!surface) {
719 LOG(ERROR) << "Unable to create Cairo surface from XImage data";
720 return;
721 }
722 cairo_surface_write_to_png_stream(
723 surface, SnapshotCallback, png_representation);
724 cairo_surface_destroy(surface);
725 }
726
727 bool ChangeWindowDesktop(XID window, XID destination) {
728 int desktop;
729 if (!GetWindowDesktop(destination, &desktop))
730 return false;
731
732 // If |window| is sticky, use the current desktop.
733 if (desktop == kAllDesktops &&
734 !GetCurrentDesktop(&desktop))
735 return false;
736
737 XEvent event;
738 event.xclient.type = ClientMessage;
739 event.xclient.window = window;
740 event.xclient.message_type = gdk_x11_get_xatom_by_name_for_display(
741 gdk_display_get_default(), "_NET_WM_DESKTOP");
742 event.xclient.format = 32;
743 event.xclient.data.l[0] = desktop;
744 event.xclient.data.l[1] = 1; // source indication
745
746 int result = XSendEvent(GetXDisplay(), GetX11RootWindow(), False,
747 SubstructureNotifyMask, &event);
748 return result == Success;
749 }
750
751 void SetDefaultX11ErrorHandlers() {
752 SetX11ErrorHandlers(NULL, NULL);
753 }
754
755 // ----------------------------------------------------------------------------
756 // These functions are declared in x11_util_internal.h because they require
757 // XLib.h to be included, and it conflicts with many other headers.
758 XRenderPictFormat* GetRenderARGB32Format(Display* dpy) {
759 static XRenderPictFormat* pictformat = NULL;
760 if (pictformat)
761 return pictformat;
762
763 // First look for a 32-bit format which ignores the alpha value
764 XRenderPictFormat templ;
765 templ.depth = 32;
766 templ.type = PictTypeDirect;
767 templ.direct.red = 16;
768 templ.direct.green = 8;
769 templ.direct.blue = 0;
770 templ.direct.redMask = 0xff;
771 templ.direct.greenMask = 0xff;
772 templ.direct.blueMask = 0xff;
773 templ.direct.alphaMask = 0;
774
775 static const unsigned long kMask =
776 PictFormatType | PictFormatDepth |
777 PictFormatRed | PictFormatRedMask |
778 PictFormatGreen | PictFormatGreenMask |
779 PictFormatBlue | PictFormatBlueMask |
780 PictFormatAlphaMask;
781
782 pictformat = XRenderFindFormat(dpy, kMask, &templ, 0 /* first result */);
783
784 if (!pictformat) {
785 // Not all X servers support xRGB32 formats. However, the XRENDER spec says
786 // that they must support an ARGB32 format, so we can always return that.
787 pictformat = XRenderFindStandardFormat(dpy, PictStandardARGB32);
788 CHECK(pictformat) << "XRENDER ARGB32 not supported.";
789 }
790
791 return pictformat;
792 }
793
794 XRenderPictFormat* GetRenderVisualFormat(Display* dpy, Visual* visual) {
795 DCHECK(QueryRenderSupport(dpy));
796
797 CachedPictFormats* formats = get_cached_pict_formats();
798
799 for (CachedPictFormats::const_iterator i = formats->begin();
800 i != formats->end(); ++i) {
801 if (i->equals(dpy, visual))
802 return i->format;
803 }
804
805 // Not cached, look up the value.
806 XRenderPictFormat* pictformat = XRenderFindVisualFormat(dpy, visual);
807 CHECK(pictformat) << "XRENDER does not support default visual";
808
809 // And store it in the cache.
810 CachedPictFormat cached_value;
811 cached_value.visual = visual;
812 cached_value.display = dpy;
813 cached_value.format = pictformat;
814 formats->push_front(cached_value);
815
816 if (formats->size() == kMaxCacheSize) {
817 formats->pop_back();
818 // We should really only have at most 2 display/visual combinations:
819 // one for normal browser windows, and possibly another for an argb window
820 // created to display a menu.
821 //
822 // If we get here it's not fatal, we just need to make sure we aren't
823 // always blowing away the cache. If we are, then we should figure out why
824 // and make it bigger.
825 NOTREACHED();
826 }
827
828 return pictformat;
829 }
830
831 void SetX11ErrorHandlers(XErrorHandler error_handler,
832 XIOErrorHandler io_error_handler) {
833 XSetErrorHandler(error_handler ? error_handler : DefaultX11ErrorHandler);
834 XSetIOErrorHandler(
835 io_error_handler ? io_error_handler : DefaultX11IOErrorHandler);
836 }
837
838 std::string GetErrorEventDescription(Display *dpy,
839 XErrorEvent *error_event) {
840 char error_str[256];
841 char request_str[256];
842
843 XGetErrorText(dpy, error_event->error_code, error_str, sizeof(error_str));
844
845 strncpy(request_str, "Unknown", sizeof(request_str));
846 if (error_event->request_code < 128) {
847 std::string num = base::UintToString(error_event->request_code);
848 XGetErrorDatabaseText(
849 dpy, "XRequest", num.c_str(), "Unknown", request_str,
850 sizeof(request_str));
851 } else {
852 int num_ext;
853 char **ext_list = XListExtensions(dpy, &num_ext);
854
855 for (int i = 0; i < num_ext; i++) {
856 int ext_code, first_event, first_error;
857 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error);
858 if (error_event->request_code == ext_code) {
859 std::string msg = StringPrintf(
860 "%s.%d", ext_list[i], error_event->minor_code);
861 XGetErrorDatabaseText(
862 dpy, "XRequest", msg.c_str(), "Unknown", request_str,
863 sizeof(request_str));
864 break;
865 }
866 }
867 XFreeExtensionList(ext_list);
868 }
869
870 return base::StringPrintf(
871 "X Error detected: serial %lu, error_code %u (%s), "
872 "request_code %u minor_code %u (%s)",
873 error_event->serial, error_event->error_code, error_str,
874 error_event->request_code, error_event->minor_code, request_str);
875 }
876 // ----------------------------------------------------------------------------
877 // End of x11_util_internal.h
878
879
880 } // namespace x11_util
OLDNEW
« no previous file with comments | « app/x11_util.h ('k') | app/x11_util_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698