1 // Copyright (c) 2012 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.
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.
9 #include "ui/base/x/x11_util.h"
20 #include <X11/extensions/shape.h>
21 #include <X11/extensions/XInput2.h>
23 #include "base/bind.h"
24 #include "base/command_line.h"
25 #include "base/logging.h"
26 #include "base/memory/scoped_ptr.h"
27 #include "base/memory/singleton.h"
28 #include "base/message_loop/message_loop.h"
29 #include "base/metrics/histogram.h"
30 #include "base/strings/string_number_conversions.h"
31 #include "base/strings/string_util.h"
32 #include "base/strings/stringprintf.h"
33 #include "base/sys_byteorder.h"
34 #include "base/threading/thread.h"
35 #include "base/x11/x11_error_tracker.h"
36 #include "third_party/skia/include/core/SkBitmap.h"
37 #include "third_party/skia/include/core/SkPostConfig.h"
38 #include "ui/base/x/x11_util_internal.h"
39 #include "ui/events/event_utils.h"
40 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
41 #include "ui/events/x/device_data_manager.h"
42 #include "ui/events/x/touch_factory_x11.h"
43 #include "ui/gfx/canvas.h"
44 #include "ui/gfx/image/image_skia.h"
45 #include "ui/gfx/image/image_skia_rep.h"
46 #include "ui/gfx/point.h"
47 #include "ui/gfx/point_conversions.h"
48 #include "ui/gfx/rect.h"
49 #include "ui/gfx/size.h"
51 #if defined(OS_FREEBSD)
52 #include <sys/sysctl.h>
53 #include <sys/types.h>
57 #include <X11/Xcursor/Xcursor.h>
58 #include "skia/ext/image_operations.h"
59 #include "ui/gfx/skia_util.h"
62 #if defined(TOOLKIT_GTK)
65 #include "ui/gfx/gdk_compat.h"
66 #include "ui/gfx/gtk_compat.h"
73 // Used to cache the XRenderPictFormat for a visual/display pair.
74 struct CachedPictFormat {
75 bool equals(XDisplay* display, Visual* visual) const {
76 return display == this->display && visual == this->visual;
81 XRenderPictFormat* format;
84 typedef std::list<CachedPictFormat> CachedPictFormats;
86 // Returns the cache of pict formats.
87 CachedPictFormats* get_cached_pict_formats() {
88 static CachedPictFormats* formats = NULL;
90 formats = new CachedPictFormats();
94 // Maximum number of CachedPictFormats we keep around.
95 const size_t kMaxCacheSize = 5;
97 int DefaultX11ErrorHandler(XDisplay* d, XErrorEvent* e) {
98 if (base::MessageLoop::current()) {
99 base::MessageLoop::current()->PostTask(
100 FROM_HERE, base::Bind(&LogErrorEventDescription, d, *e));
103 << "X error received: "
104 << "serial " << e->serial << ", "
105 << "error_code " << static_cast<int>(e->error_code) << ", "
106 << "request_code " << static_cast<int>(e->request_code) << ", "
107 << "minor_code " << static_cast<int>(e->minor_code);
112 int DefaultX11IOErrorHandler(XDisplay* d) {
113 // If there's an IO error it likely means the X server has gone away
114 LOG(ERROR) << "X IO error received (X server probably went away)";
118 // Note: The caller should free the resulting value data.
119 bool GetProperty(XID window, const std::string& property_name, long max_length,
120 Atom* type, int* format, unsigned long* num_items,
121 unsigned char** property) {
122 Atom property_atom = GetAtom(property_name.c_str());
123 unsigned long remaining_bytes = 0;
124 return XGetWindowProperty(gfx::GetXDisplay(),
127 0, // offset into property data to read
128 max_length, // max length to get
138 // A process wide singleton that manages the usage of X cursors.
146 ::Cursor GetCursor(int cursor_shape) {
147 // Lookup cursor by attempting to insert a null value, which avoids
148 // a second pass through the map after a cache miss.
149 std::pair<std::map<int, ::Cursor>::iterator, bool> it = cache_.insert(
150 std::make_pair(cursor_shape, 0));
152 XDisplay* display = base::MessagePumpForUI::GetDefaultXDisplay();
153 it.first->second = XCreateFontCursor(display, cursor_shape);
155 return it.first->second;
159 XDisplay* display = base::MessagePumpForUI::GetDefaultXDisplay();
160 for (std::map<int, ::Cursor>::iterator it =
161 cache_.begin(); it != cache_.end(); ++it) {
162 XFreeCursor(display, it->second);
168 // Maps X11 font cursor shapes to Cursor IDs.
169 std::map<int, ::Cursor> cache_;
171 DISALLOW_COPY_AND_ASSIGN(XCursorCache);
174 XCursorCache* cursor_cache = NULL;
176 #if defined(USE_AURA)
177 // A process wide singleton cache for custom X cursors.
178 class XCustomCursorCache {
180 static XCustomCursorCache* GetInstance() {
181 return Singleton<XCustomCursorCache>::get();
184 ::Cursor InstallCustomCursor(XcursorImage* image) {
185 XCustomCursor* custom_cursor = new XCustomCursor(image);
186 ::Cursor xcursor = custom_cursor->cursor();
187 cache_[xcursor] = custom_cursor;
191 void Ref(::Cursor cursor) {
192 cache_[cursor]->Ref();
195 void Unref(::Cursor cursor) {
196 if (cache_[cursor]->Unref())
197 cache_.erase(cursor);
205 friend struct DefaultSingletonTraits<XCustomCursorCache>;
207 class XCustomCursor {
209 // This takes ownership of the image.
210 XCustomCursor(XcursorImage* image)
213 cursor_ = XcursorImageLoadCursor(gfx::GetXDisplay(), image);
217 XcursorImageDestroy(image_);
218 XFreeCursor(gfx::GetXDisplay(), cursor_);
221 ::Cursor cursor() const { return cursor_; }
227 // Returns true if the cursor was destroyed because of the unref.
237 XcursorImage* image_;
241 DISALLOW_COPY_AND_ASSIGN(XCustomCursor);
244 XCustomCursorCache() {}
245 ~XCustomCursorCache() {
249 std::map< ::Cursor, XCustomCursor*> cache_;
250 DISALLOW_COPY_AND_ASSIGN(XCustomCursorCache);
252 #endif // defined(USE_AURA)
254 bool IsShapeAvailable() {
256 static bool is_shape_available =
257 XShapeQueryExtension(gfx::GetXDisplay(), &dummy, &dummy);
258 return is_shape_available;
264 bool XDisplayExists() {
265 return (gfx::GetXDisplay() != NULL);
268 static SharedMemorySupport DoQuerySharedMemorySupport(XDisplay* dpy) {
270 Bool pixmaps_supported;
271 // Query the server's support for XSHM.
272 if (!XShmQueryVersion(dpy, &dummy, &dummy, &pixmaps_supported))
273 return SHARED_MEMORY_NONE;
275 #if defined(OS_FREEBSD)
276 // On FreeBSD we can't access the shared memory after it was marked for
277 // deletion, unless this behaviour is explicitly enabled by the user.
278 // In case it's not enabled disable shared memory support.
280 size_t length = sizeof(allow_removed);
282 if ((sysctlbyname("kern.ipc.shm_allow_removed", &allow_removed, &length,
283 NULL, 0) < 0) || allow_removed < 1) {
284 return SHARED_MEMORY_NONE;
288 // Next we probe to see if shared memory will really work
289 int shmkey = shmget(IPC_PRIVATE, 1, 0600);
291 LOG(WARNING) << "Failed to get shared memory segment.";
292 return SHARED_MEMORY_NONE;
294 VLOG(1) << "Got shared memory segment " << shmkey;
297 void* address = shmat(shmkey, NULL, 0);
298 // Mark the shared memory region for deletion
299 shmctl(shmkey, IPC_RMID, NULL);
301 XShmSegmentInfo shminfo;
302 memset(&shminfo, 0, sizeof(shminfo));
303 shminfo.shmid = shmkey;
305 base::X11ErrorTracker err_tracker;
306 bool result = XShmAttach(dpy, &shminfo);
308 VLOG(1) << "X got shared memory segment " << shmkey;
310 LOG(WARNING) << "X failed to attach to shared memory segment " << shmkey;
311 if (err_tracker.FoundNewError())
315 LOG(WARNING) << "X failed to attach to shared memory segment " << shmkey;
316 return SHARED_MEMORY_NONE;
319 VLOG(1) << "X attached to shared memory segment " << shmkey;
321 XShmDetach(dpy, &shminfo);
322 return pixmaps_supported ? SHARED_MEMORY_PIXMAP : SHARED_MEMORY_PUTIMAGE;
325 SharedMemorySupport QuerySharedMemorySupport(XDisplay* dpy) {
326 static SharedMemorySupport shared_memory_support = SHARED_MEMORY_NONE;
327 static bool shared_memory_support_cached = false;
329 if (shared_memory_support_cached)
330 return shared_memory_support;
332 shared_memory_support = DoQuerySharedMemorySupport(dpy);
333 shared_memory_support_cached = true;
335 return shared_memory_support;
338 bool QueryRenderSupport(XDisplay* dpy) {
339 static bool render_supported = false;
340 static bool render_supported_cached = false;
342 if (render_supported_cached)
343 return render_supported;
345 // We don't care about the version of Xrender since all the features which
346 // we use are included in every version.
348 render_supported = XRenderQueryExtension(dpy, &dummy, &dummy);
349 render_supported_cached = true;
351 return render_supported;
354 int GetDefaultScreen(XDisplay* display) {
355 return XDefaultScreen(display);
358 ::Cursor GetXCursor(int cursor_shape) {
360 cursor_cache = new XCursorCache;
361 return cursor_cache->GetCursor(cursor_shape);
364 void ResetXCursorCache() {
369 #if defined(USE_AURA)
370 ::Cursor CreateReffedCustomXCursor(XcursorImage* image) {
371 return XCustomCursorCache::GetInstance()->InstallCustomCursor(image);
374 void RefCustomXCursor(::Cursor cursor) {
375 XCustomCursorCache::GetInstance()->Ref(cursor);
378 void UnrefCustomXCursor(::Cursor cursor) {
379 XCustomCursorCache::GetInstance()->Unref(cursor);
382 XcursorImage* SkBitmapToXcursorImage(const SkBitmap* cursor_image,
383 const gfx::Point& hotspot) {
384 DCHECK(cursor_image->config() == SkBitmap::kARGB_8888_Config);
385 gfx::Point hotspot_point = hotspot;
388 // X11 seems to have issues with cursors when images get larger than 64
389 // pixels. So rescale the image if necessary.
390 const float kMaxPixel = 64.f;
391 bool needs_scale = false;
392 if (cursor_image->width() > kMaxPixel || cursor_image->height() > kMaxPixel) {
394 if (cursor_image->width() > cursor_image->height())
395 scale = kMaxPixel / cursor_image->width();
397 scale = kMaxPixel / cursor_image->height();
399 scaled = skia::ImageOperations::Resize(*cursor_image,
400 skia::ImageOperations::RESIZE_BETTER,
401 static_cast<int>(cursor_image->width() * scale),
402 static_cast<int>(cursor_image->height() * scale));
403 hotspot_point = gfx::ToFlooredPoint(gfx::ScalePoint(hotspot, scale));
407 const SkBitmap* bitmap = needs_scale ? &scaled : cursor_image;
408 XcursorImage* image = XcursorImageCreate(bitmap->width(), bitmap->height());
409 image->xhot = std::min(bitmap->width() - 1, hotspot_point.x());
410 image->yhot = std::min(bitmap->height() - 1, hotspot_point.y());
412 if (bitmap->width() && bitmap->height()) {
413 bitmap->lockPixels();
414 // The |bitmap| contains ARGB image, so just copy it.
415 memcpy(image->pixels,
417 bitmap->width() * bitmap->height() * 4);
418 bitmap->unlockPixels();
425 int CoalescePendingMotionEvents(const XEvent* xev,
426 XEvent* last_event) {
427 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(xev->xcookie.data);
428 int num_coalesced = 0;
429 XDisplay* display = xev->xany.display;
430 int event_type = xev->xgeneric.evtype;
432 DCHECK_EQ(event_type, XI_Motion);
434 while (XPending(display)) {
436 XPeekEvent(display, &next_event);
438 // If we can't get the cookie, abort the check.
439 if (!XGetEventData(next_event.xgeneric.display, &next_event.xcookie))
440 return num_coalesced;
442 // If this isn't from a valid device, throw the event away, as
443 // that's what the message pump would do. Device events come in pairs
444 // with one from the master and one from the slave so there will
445 // always be at least one pending.
446 if (!ui::TouchFactory::GetInstance()->ShouldProcessXI2Event(&next_event)) {
447 XFreeEventData(display, &next_event.xcookie);
448 XNextEvent(display, &next_event);
452 if (next_event.type == GenericEvent &&
453 next_event.xgeneric.evtype == event_type &&
454 !ui::DeviceDataManager::GetInstance()->IsCMTGestureEvent(
456 XIDeviceEvent* next_xievent =
457 static_cast<XIDeviceEvent*>(next_event.xcookie.data);
458 // Confirm that the motion event is targeted at the same window
459 // and that no buttons or modifiers have changed.
460 if (xievent->event == next_xievent->event &&
461 xievent->child == next_xievent->child &&
462 xievent->buttons.mask_len == next_xievent->buttons.mask_len &&
463 (memcmp(xievent->buttons.mask,
464 next_xievent->buttons.mask,
465 xievent->buttons.mask_len) == 0) &&
466 xievent->mods.base == next_xievent->mods.base &&
467 xievent->mods.latched == next_xievent->mods.latched &&
468 xievent->mods.locked == next_xievent->mods.locked &&
469 xievent->mods.effective == next_xievent->mods.effective) {
470 XFreeEventData(display, &next_event.xcookie);
471 // Free the previous cookie.
472 if (num_coalesced > 0)
473 XFreeEventData(display, &last_event->xcookie);
474 // Get the event and its cookie data.
475 XNextEvent(display, last_event);
476 XGetEventData(display, &last_event->xcookie);
480 // This isn't an event we want so free its cookie data.
481 XFreeEventData(display, &next_event.xcookie);
487 if (num_coalesced > 0) {
488 base::TimeDelta delta = ui::EventTimeFromNative(last_event) -
489 ui::EventTimeFromNative(const_cast<XEvent*>(xev));
490 UMA_HISTOGRAM_COUNTS_10000("Event.CoalescedCount.Mouse", num_coalesced);
491 UMA_HISTOGRAM_TIMES("Event.CoalescedLatency.Mouse", delta);
493 return num_coalesced;
497 void HideHostCursor() {
498 CR_DEFINE_STATIC_LOCAL(XScopedCursor, invisible_cursor,
499 (CreateInvisibleCursor(), gfx::GetXDisplay()));
500 XDefineCursor(gfx::GetXDisplay(), DefaultRootWindow(gfx::GetXDisplay()),
501 invisible_cursor.get());
504 ::Cursor CreateInvisibleCursor() {
505 XDisplay* xdisplay = gfx::GetXDisplay();
506 ::Cursor invisible_cursor;
507 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
509 black.red = black.green = black.blue = 0;
510 Pixmap blank = XCreateBitmapFromData(xdisplay,
511 DefaultRootWindow(xdisplay),
513 invisible_cursor = XCreatePixmapCursor(xdisplay, blank, blank,
514 &black, &black, 0, 0);
515 XFreePixmap(xdisplay, blank);
516 return invisible_cursor;
519 XID GetX11RootWindow() {
520 return DefaultRootWindow(gfx::GetXDisplay());
523 bool GetCurrentDesktop(int* desktop) {
524 return GetIntProperty(GetX11RootWindow(), "_NET_CURRENT_DESKTOP", desktop);
527 #if defined(TOOLKIT_GTK)
528 XID GetX11WindowFromGtkWidget(GtkWidget* widget) {
529 return GDK_WINDOW_XID(gtk_widget_get_window(widget));
532 XID GetX11WindowFromGdkWindow(GdkWindow* window) {
533 return GDK_WINDOW_XID(window);
536 GtkWindow* GetGtkWindowFromX11Window(XID xid) {
537 GdkWindow* gdk_window =
538 gdk_x11_window_lookup_for_display(gdk_display_get_default(), xid);
541 GtkWindow* gtk_window = NULL;
542 gdk_window_get_user_data(gdk_window,
543 reinterpret_cast<gpointer*>(>k_window));
549 void* GetVisualFromGtkWidget(GtkWidget* widget) {
550 return GDK_VISUAL_XVISUAL(gtk_widget_get_visual(widget));
552 #endif // defined(TOOLKIT_GTK)
554 void SetHideTitlebarWhenMaximizedProperty(XID window,
555 HideTitlebarWhenMaximized property) {
556 // XChangeProperty() expects "hide" to be long.
557 unsigned long hide = property;
558 XChangeProperty(gfx::GetXDisplay(),
560 GetAtom("_GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED"),
564 reinterpret_cast<unsigned char*>(&hide),
568 void ClearX11DefaultRootWindow() {
569 XDisplay* display = gfx::GetXDisplay();
570 XID root_window = GetX11RootWindow();
571 gfx::Rect root_bounds;
572 if (!GetWindowRect(root_window, &root_bounds)) {
573 LOG(ERROR) << "Failed to get the bounds of the X11 root window";
577 XGCValues gc_values = {0};
578 gc_values.foreground = BlackPixel(display, DefaultScreen(display));
579 GC gc = XCreateGC(display, root_window, GCForeground, &gc_values);
580 XFillRectangle(display, root_window, gc,
584 root_bounds.height());
585 XFreeGC(display, gc);
588 bool IsWindowVisible(XID window) {
589 XWindowAttributes win_attributes;
590 if (!XGetWindowAttributes(gfx::GetXDisplay(), window, &win_attributes))
592 if (win_attributes.map_state != IsViewable)
594 // Some compositing window managers (notably kwin) do not actually unmap
595 // windows on desktop switch, so we also must check the current desktop.
596 int window_desktop, current_desktop;
597 return (!GetWindowDesktop(window, &window_desktop) ||
598 !GetCurrentDesktop(¤t_desktop) ||
599 window_desktop == kAllDesktops ||
600 window_desktop == current_desktop);
603 bool GetWindowRect(XID window, gfx::Rect* rect) {
606 unsigned int width, height;
607 unsigned int border_width, depth;
609 if (!XGetGeometry(gfx::GetXDisplay(), window, &root, &x, &y,
610 &width, &height, &border_width, &depth))
613 if (!XTranslateCoordinates(gfx::GetXDisplay(), window, root,
614 0, 0, &x, &y, &child))
617 *rect = gfx::Rect(x, y, width, height);
622 bool WindowContainsPoint(XID window, gfx::Point screen_loc) {
623 gfx::Rect window_rect;
624 if (!GetWindowRect(window, &window_rect))
627 if (!window_rect.Contains(screen_loc))
630 if (!IsShapeAvailable())
633 // According to http://www.x.org/releases/X11R7.6/doc/libXext/shapelib.html,
634 // if an X display supports the shape extension the bounds of a window are
635 // defined as the intersection of the window bounds and the interior
636 // rectangles. This means to determine if a point is inside a window for the
637 // purpose of input handling we have to check the rectangles in the ShapeInput
640 int input_rects_size = 0;
641 XRectangle* input_rects = XShapeGetRectangles(
642 gfx::GetXDisplay(), window, ShapeInput, &input_rects_size, &dummy);
645 bool is_in_input_rects = false;
646 for (int i = 0; i < input_rects_size; ++i) {
647 // The ShapeInput rects appear to be in window space, so we have to
648 // translate by the window_rect's offset to map to screen space.
649 gfx::Rect input_rect =
650 gfx::Rect(input_rects[i].x + window_rect.x(),
651 input_rects[i].y + window_rect.y(),
652 input_rects[i].width, input_rects[i].height);
653 if (input_rect.Contains(screen_loc)) {
654 is_in_input_rects = true;
659 return is_in_input_rects;
663 bool PropertyExists(XID window, const std::string& property_name) {
665 int format = 0; // size in bits of each item in 'property'
666 unsigned long num_items = 0;
667 unsigned char* property = NULL;
669 int result = GetProperty(window, property_name, 1,
670 &type, &format, &num_items, &property);
671 if (result != Success)
675 return num_items > 0;
678 bool GetRawBytesOfProperty(XID window,
680 scoped_refptr<base::RefCountedMemory>* out_data,
681 size_t* out_data_bytes,
682 size_t* out_data_items,
684 // Retrieve the data from our window.
685 unsigned long nitems = 0;
686 unsigned long nbytes = 0;
687 Atom prop_type = None;
689 unsigned char* property_data = NULL;
690 if (XGetWindowProperty(gfx::GetXDisplay(), window, property,
691 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
692 AnyPropertyType, &prop_type, &prop_format,
693 &nitems, &nbytes, &property_data) != Success) {
697 if (prop_type == None)
701 // So even though we should theoretically have nbytes (and we can't
702 // pass NULL there), we need to manually calculate the byte length here
703 // because nbytes always returns zero.
704 switch (prop_format) {
709 bytes = sizeof(short) * nitems;
712 bytes = sizeof(long) * nitems;
720 *out_data_bytes = bytes;
723 *out_data = new XRefcountedMemory(property_data, bytes);
725 XFree(property_data);
728 *out_data_items = nitems;
731 *out_type = prop_type;
736 bool GetIntProperty(XID window, const std::string& property_name, int* value) {
738 int format = 0; // size in bits of each item in 'property'
739 unsigned long num_items = 0;
740 unsigned char* property = NULL;
742 int result = GetProperty(window, property_name, 1,
743 &type, &format, &num_items, &property);
744 if (result != Success)
747 if (format != 32 || num_items != 1) {
752 *value = static_cast<int>(*(reinterpret_cast<long*>(property)));
757 bool GetXIDProperty(XID window, const std::string& property_name, XID* value) {
759 int format = 0; // size in bits of each item in 'property'
760 unsigned long num_items = 0;
761 unsigned char* property = NULL;
763 int result = GetProperty(window, property_name, 1,
764 &type, &format, &num_items, &property);
765 if (result != Success)
768 if (format != 32 || num_items != 1) {
773 *value = *(reinterpret_cast<XID*>(property));
778 bool GetIntArrayProperty(XID window,
779 const std::string& property_name,
780 std::vector<int>* value) {
782 int format = 0; // size in bits of each item in 'property'
783 unsigned long num_items = 0;
784 unsigned char* properties = NULL;
786 int result = GetProperty(window, property_name,
787 (~0L), // (all of them)
788 &type, &format, &num_items, &properties);
789 if (result != Success)
797 long* int_properties = reinterpret_cast<long*>(properties);
799 for (unsigned long i = 0; i < num_items; ++i) {
800 value->push_back(static_cast<int>(int_properties[i]));
806 bool GetAtomArrayProperty(XID window,
807 const std::string& property_name,
808 std::vector<Atom>* value) {
810 int format = 0; // size in bits of each item in 'property'
811 unsigned long num_items = 0;
812 unsigned char* properties = NULL;
814 int result = GetProperty(window, property_name,
815 (~0L), // (all of them)
816 &type, &format, &num_items, &properties);
817 if (result != Success)
820 if (type != XA_ATOM) {
825 Atom* atom_properties = reinterpret_cast<Atom*>(properties);
827 value->insert(value->begin(), atom_properties, atom_properties + num_items);
832 bool GetStringProperty(
833 XID window, const std::string& property_name, std::string* value) {
835 int format = 0; // size in bits of each item in 'property'
836 unsigned long num_items = 0;
837 unsigned char* property = NULL;
839 int result = GetProperty(window, property_name, 1024,
840 &type, &format, &num_items, &property);
841 if (result != Success)
849 value->assign(reinterpret_cast<char*>(property), num_items);
854 bool SetIntProperty(XID window,
855 const std::string& name,
856 const std::string& type,
858 std::vector<int> values(1, value);
859 return SetIntArrayProperty(window, name, type, values);
862 bool SetIntArrayProperty(XID window,
863 const std::string& name,
864 const std::string& type,
865 const std::vector<int>& value) {
866 DCHECK(!value.empty());
867 Atom name_atom = GetAtom(name.c_str());
868 Atom type_atom = GetAtom(type.c_str());
870 // XChangeProperty() expects values of type 32 to be longs.
871 scoped_ptr<long[]> data(new long[value.size()]);
872 for (size_t i = 0; i < value.size(); ++i)
875 base::X11ErrorTracker err_tracker;
876 XChangeProperty(gfx::GetXDisplay(),
880 32, // size in bits of items in 'value'
882 reinterpret_cast<const unsigned char*>(data.get()),
883 value.size()); // num items
884 return !err_tracker.FoundNewError();
887 bool SetAtomArrayProperty(XID window,
888 const std::string& name,
889 const std::string& type,
890 const std::vector<Atom>& value) {
891 DCHECK(!value.empty());
892 Atom name_atom = GetAtom(name.c_str());
893 Atom type_atom = GetAtom(type.c_str());
895 // XChangeProperty() expects values of type 32 to be longs.
896 scoped_ptr<Atom[]> data(new Atom[value.size()]);
897 for (size_t i = 0; i < value.size(); ++i)
900 base::X11ErrorTracker err_tracker;
901 XChangeProperty(gfx::GetXDisplay(),
905 32, // size in bits of items in 'value'
907 reinterpret_cast<const unsigned char*>(data.get()),
908 value.size()); // num items
909 return !err_tracker.FoundNewError();
912 Atom GetAtom(const char* name) {
913 #if defined(TOOLKIT_GTK)
914 return gdk_x11_get_xatom_by_name_for_display(
915 gdk_display_get_default(), name);
917 // TODO(derat): Cache atoms to avoid round-trips to the server.
918 return XInternAtom(gfx::GetXDisplay(), name, false);
922 void SetWindowClassHint(XDisplay* display,
924 std::string res_name,
925 std::string res_class) {
926 XClassHint class_hints;
927 // const_cast is safe because XSetClassHint does not modify the strings.
928 // Just to be safe, the res_name and res_class parameters are local copies,
929 // not const references.
930 class_hints.res_name = const_cast<char*>(res_name.c_str());
931 class_hints.res_class = const_cast<char*>(res_class.c_str());
932 XSetClassHint(display, window, &class_hints);
935 XID GetParentWindow(XID window) {
938 XID* children = NULL;
939 unsigned int num_children = 0;
940 XQueryTree(gfx::GetXDisplay(), window, &root, &parent, &children, &num_children);
946 XID GetHighestAncestorWindow(XID window, XID root) {
948 XID parent = GetParentWindow(window);
957 bool GetWindowDesktop(XID window, int* desktop) {
958 return GetIntProperty(window, "_NET_WM_DESKTOP", desktop);
961 std::string GetX11ErrorString(XDisplay* display, int err) {
963 XGetErrorText(display, err, buffer, arraysize(buffer));
967 // Returns true if |window| is a named window.
968 bool IsWindowNamed(XID window) {
970 if (!XGetWMName(gfx::GetXDisplay(), window, &prop) || !prop.value)
977 bool EnumerateChildren(EnumerateWindowsDelegate* delegate, XID window,
978 const int max_depth, int depth) {
979 if (depth > max_depth)
982 XID root, parent, *children;
983 unsigned int num_children;
984 int status = XQueryTree(gfx::GetXDisplay(), window, &root, &parent, &children,
989 std::vector<XID> windows;
990 for (int i = static_cast<int>(num_children) - 1; i >= 0; i--)
991 windows.push_back(children[i]);
995 // XQueryTree returns the children of |window| in bottom-to-top order, so
996 // reverse-iterate the list to check the windows from top-to-bottom.
997 std::vector<XID>::iterator iter;
998 for (iter = windows.begin(); iter != windows.end(); iter++) {
999 if (IsWindowNamed(*iter) && delegate->ShouldStopIterating(*iter))
1003 // If we're at this point, we didn't find the window we're looking for at the
1004 // current level, so we need to recurse to the next level. We use a second
1005 // loop because the recursion and call to XQueryTree are expensive and is only
1006 // needed for a small number of cases.
1007 if (++depth <= max_depth) {
1008 for (iter = windows.begin(); iter != windows.end(); iter++) {
1009 if (EnumerateChildren(delegate, *iter, max_depth, depth))
1017 bool EnumerateAllWindows(EnumerateWindowsDelegate* delegate, int max_depth) {
1018 XID root = GetX11RootWindow();
1019 return EnumerateChildren(delegate, root, max_depth, 0);
1022 void EnumerateTopLevelWindows(ui::EnumerateWindowsDelegate* delegate) {
1023 std::vector<XID> stack;
1024 if (!ui::GetXWindowStack(ui::GetX11RootWindow(), &stack)) {
1025 // Window Manager doesn't support _NET_CLIENT_LIST_STACKING, so fall back
1026 // to old school enumeration of all X windows. Some WMs parent 'top-level'
1027 // windows in unnamed actual top-level windows (ion WM), so extend the
1028 // search depth to all children of top-level windows.
1029 const int kMaxSearchDepth = 1;
1030 ui::EnumerateAllWindows(delegate, kMaxSearchDepth);
1034 std::vector<XID>::iterator iter;
1035 for (iter = stack.begin(); iter != stack.end(); iter++) {
1036 if (delegate->ShouldStopIterating(*iter))
1041 bool GetXWindowStack(Window window, std::vector<XID>* windows) {
1046 unsigned long count;
1047 unsigned char *data = NULL;
1048 if (GetProperty(window,
1049 "_NET_CLIENT_LIST_STACKING",
1054 &data) != Success) {
1058 bool result = false;
1059 if (type == XA_WINDOW && format == 32 && data && count > 0) {
1061 XID* stack = reinterpret_cast<XID*>(data);
1062 for (long i = static_cast<long>(count) - 1; i >= 0; i--)
1063 windows->push_back(stack[i]);
1072 void RestackWindow(XID window, XID sibling, bool above) {
1073 XWindowChanges changes;
1074 changes.sibling = sibling;
1075 changes.stack_mode = above ? Above : Below;
1076 XConfigureWindow(gfx::GetXDisplay(), window, CWSibling | CWStackMode, &changes);
1079 XSharedMemoryId AttachSharedMemory(XDisplay* display, int shared_memory_key) {
1080 DCHECK(QuerySharedMemorySupport(display));
1082 XShmSegmentInfo shminfo;
1083 memset(&shminfo, 0, sizeof(shminfo));
1084 shminfo.shmid = shared_memory_key;
1086 // This function is only called if QuerySharedMemorySupport returned true. In
1087 // which case we've already succeeded in having the X server attach to one of
1088 // our shared memory segments.
1089 if (!XShmAttach(display, &shminfo)) {
1090 LOG(WARNING) << "X failed to attach to shared memory segment "
1094 VLOG(1) << "X attached to shared memory segment " << shminfo.shmid;
1097 return shminfo.shmseg;
1100 void DetachSharedMemory(XDisplay* display, XSharedMemoryId shmseg) {
1101 DCHECK(QuerySharedMemorySupport(display));
1103 XShmSegmentInfo shminfo;
1104 memset(&shminfo, 0, sizeof(shminfo));
1105 shminfo.shmseg = shmseg;
1107 if (!XShmDetach(display, &shminfo))
1111 bool CopyAreaToCanvas(XID drawable,
1112 gfx::Rect source_bounds,
1113 gfx::Point dest_offset,
1114 gfx::Canvas* canvas) {
1115 ui::XScopedImage scoped_image(
1116 XGetImage(gfx::GetXDisplay(), drawable,
1117 source_bounds.x(), source_bounds.y(),
1118 source_bounds.width(), source_bounds.height(),
1119 AllPlanes, ZPixmap));
1120 XImage* image = scoped_image.get();
1122 LOG(ERROR) << "XGetImage failed";
1126 if (image->bits_per_pixel == 32) {
1127 if ((0xff << SK_R32_SHIFT) != image->red_mask ||
1128 (0xff << SK_G32_SHIFT) != image->green_mask ||
1129 (0xff << SK_B32_SHIFT) != image->blue_mask) {
1130 LOG(WARNING) << "XImage and Skia byte orders differ";
1134 // Set the alpha channel before copying to the canvas. Otherwise, areas of
1135 // the framebuffer that were cleared by ply-image rather than being obscured
1136 // by an image during boot may end up transparent.
1137 // TODO(derat|marcheu): Remove this if/when ply-image has been updated to
1138 // set the framebuffer's alpha channel regardless of whether the device
1139 // claims to support alpha or not.
1140 for (int i = 0; i < image->width * image->height * 4; i += 4)
1141 image->data[i + 3] = 0xff;
1144 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
1145 image->width, image->height,
1146 image->bytes_per_line);
1147 bitmap.setPixels(image->data);
1148 gfx::ImageSkia image_skia;
1149 gfx::ImageSkiaRep image_rep(bitmap, canvas->image_scale());
1150 image_skia.AddRepresentation(image_rep);
1151 canvas->DrawImageInt(image_skia, dest_offset.x(), dest_offset.y());
1153 NOTIMPLEMENTED() << "Unsupported bits-per-pixel " << image->bits_per_pixel;
1160 XID CreatePictureFromSkiaPixmap(XDisplay* display, XID pixmap) {
1161 XID picture = XRenderCreatePicture(
1162 display, pixmap, GetRenderARGB32Format(display), 0, NULL);
1167 void FreePicture(XDisplay* display, XID picture) {
1168 XRenderFreePicture(display, picture);
1171 void FreePixmap(XDisplay* display, XID pixmap) {
1172 XFreePixmap(display, pixmap);
1175 bool GetWindowManagerName(std::string* wm_name) {
1178 if (!GetIntProperty(GetX11RootWindow(),
1179 "_NET_SUPPORTING_WM_CHECK",
1184 // It's possible that a window manager started earlier in this X session left
1185 // a stale _NET_SUPPORTING_WM_CHECK property when it was replaced by a
1186 // non-EWMH window manager, so we trap errors in the following requests to
1187 // avoid crashes (issue 23860).
1189 // EWMH requires the supporting-WM window to also have a
1190 // _NET_SUPPORTING_WM_CHECK property pointing to itself (to avoid a stale
1191 // property referencing an ID that's been recycled for another window), so we
1193 base::X11ErrorTracker err_tracker;
1194 int wm_window_property = 0;
1195 bool result = GetIntProperty(
1196 wm_window, "_NET_SUPPORTING_WM_CHECK", &wm_window_property);
1197 if (err_tracker.FoundNewError() || !result ||
1198 wm_window_property != wm_window) {
1202 result = GetStringProperty(
1203 static_cast<XID>(wm_window), "_NET_WM_NAME", wm_name);
1204 return !err_tracker.FoundNewError() && result;
1207 WindowManagerName GuessWindowManager() {
1209 if (GetWindowManagerName(&name)) {
1210 // These names are taken from the WMs' source code.
1211 if (name == "Blackbox")
1213 if (name == "chromeos-wm")
1214 return WM_CHROME_OS;
1215 if (name == "Compiz" || name == "compiz")
1218 return WM_ENLIGHTENMENT;
1219 if (StartsWithASCII(name, "IceWM", true))
1223 if (name == "Metacity")
1225 if (name == "Mutter (Muffin)")
1227 if (name == "GNOME Shell")
1228 return WM_MUTTER; // GNOME Shell uses Mutter
1229 if (name == "Mutter")
1231 if (name == "Openbox")
1233 if (name == "Xfwm4")
1239 bool ChangeWindowDesktop(XID window, XID destination) {
1241 if (!GetWindowDesktop(destination, &desktop))
1244 // If |window| is sticky, use the current desktop.
1245 if (desktop == kAllDesktops &&
1246 !GetCurrentDesktop(&desktop))
1250 event.xclient.type = ClientMessage;
1251 event.xclient.window = window;
1252 event.xclient.message_type = GetAtom("_NET_WM_DESKTOP");
1253 event.xclient.format = 32;
1254 event.xclient.data.l[0] = desktop;
1255 event.xclient.data.l[1] = 1; // source indication
1257 int result = XSendEvent(gfx::GetXDisplay(), GetX11RootWindow(), False,
1258 SubstructureNotifyMask, &event);
1259 return result == Success;
1262 void SetDefaultX11ErrorHandlers() {
1263 SetX11ErrorHandlers(NULL, NULL);
1266 bool IsX11WindowFullScreen(XID window) {
1267 // If _NET_WM_STATE_FULLSCREEN is in _NET_SUPPORTED, use the presence or
1268 // absence of _NET_WM_STATE_FULLSCREEN in _NET_WM_STATE to determine
1269 // whether we're fullscreen.
1270 std::vector<Atom> supported_atoms;
1271 if (GetAtomArrayProperty(GetX11RootWindow(),
1273 &supported_atoms)) {
1274 Atom atom = GetAtom("_NET_WM_STATE_FULLSCREEN");
1276 if (std::find(supported_atoms.begin(), supported_atoms.end(), atom)
1277 != supported_atoms.end()) {
1278 std::vector<Atom> atom_properties;
1279 if (GetAtomArrayProperty(window,
1281 &atom_properties)) {
1282 return std::find(atom_properties.begin(), atom_properties.end(), atom)
1283 != atom_properties.end();
1288 gfx::Rect window_rect;
1289 if (!ui::GetWindowRect(window, &window_rect))
1292 #if defined(TOOLKIT_GTK)
1293 // As the last resort, check if the window size is as large as the main
1295 GdkRectangle monitor_rect;
1296 gdk_screen_get_monitor_geometry(gdk_screen_get_default(), 0, &monitor_rect);
1298 return monitor_rect.x == window_rect.x() &&
1299 monitor_rect.y == window_rect.y() &&
1300 monitor_rect.width == window_rect.width() &&
1301 monitor_rect.height == window_rect.height();
1303 // We can't use gfx::Screen here because we don't have an aura::Window. So
1304 // instead just look at the size of the default display.
1306 // TODO(erg): Actually doing this correctly would require pulling out xrandr,
1307 // which we don't even do in the desktop screen yet.
1308 ::XDisplay* display = gfx::GetXDisplay();
1309 ::Screen* screen = DefaultScreenOfDisplay(display);
1310 int width = WidthOfScreen(screen);
1311 int height = HeightOfScreen(screen);
1312 return window_rect.size() == gfx::Size(width, height);
1316 const unsigned char* XRefcountedMemory::front() const {
1320 size_t XRefcountedMemory::size() const {
1324 XRefcountedMemory::~XRefcountedMemory() {
1328 XScopedString::~XScopedString() {
1332 XScopedImage::~XScopedImage() {
1336 void XScopedImage::reset(XImage* image) {
1337 if (image_ == image)
1340 XDestroyImage(image_);
1344 XScopedCursor::XScopedCursor(::Cursor cursor, XDisplay* display)
1349 XScopedCursor::~XScopedCursor() {
1353 ::Cursor XScopedCursor::get() const {
1357 void XScopedCursor::reset(::Cursor cursor) {
1359 XFreeCursor(display_, cursor_);
1363 // ----------------------------------------------------------------------------
1364 // These functions are declared in x11_util_internal.h because they require
1365 // XLib.h to be included, and it conflicts with many other headers.
1366 XRenderPictFormat* GetRenderARGB32Format(XDisplay* dpy) {
1367 static XRenderPictFormat* pictformat = NULL;
1371 // First look for a 32-bit format which ignores the alpha value
1372 XRenderPictFormat templ;
1374 templ.type = PictTypeDirect;
1375 templ.direct.red = 16;
1376 templ.direct.green = 8;
1377 templ.direct.blue = 0;
1378 templ.direct.redMask = 0xff;
1379 templ.direct.greenMask = 0xff;
1380 templ.direct.blueMask = 0xff;
1381 templ.direct.alphaMask = 0;
1383 static const unsigned long kMask =
1384 PictFormatType | PictFormatDepth |
1385 PictFormatRed | PictFormatRedMask |
1386 PictFormatGreen | PictFormatGreenMask |
1387 PictFormatBlue | PictFormatBlueMask |
1388 PictFormatAlphaMask;
1390 pictformat = XRenderFindFormat(dpy, kMask, &templ, 0 /* first result */);
1393 // Not all X servers support xRGB32 formats. However, the XRENDER spec says
1394 // that they must support an ARGB32 format, so we can always return that.
1395 pictformat = XRenderFindStandardFormat(dpy, PictStandardARGB32);
1396 CHECK(pictformat) << "XRENDER ARGB32 not supported.";
1402 XRenderPictFormat* GetRenderVisualFormat(XDisplay* dpy, Visual* visual) {
1403 DCHECK(QueryRenderSupport(dpy));
1405 CachedPictFormats* formats = get_cached_pict_formats();
1407 for (CachedPictFormats::const_iterator i = formats->begin();
1408 i != formats->end(); ++i) {
1409 if (i->equals(dpy, visual))
1413 // Not cached, look up the value.
1414 XRenderPictFormat* pictformat = XRenderFindVisualFormat(dpy, visual);
1415 CHECK(pictformat) << "XRENDER does not support default visual";
1417 // And store it in the cache.
1418 CachedPictFormat cached_value;
1419 cached_value.visual = visual;
1420 cached_value.display = dpy;
1421 cached_value.format = pictformat;
1422 formats->push_front(cached_value);
1424 if (formats->size() == kMaxCacheSize) {
1425 formats->pop_back();
1426 // We should really only have at most 2 display/visual combinations:
1427 // one for normal browser windows, and possibly another for an argb window
1428 // created to display a menu.
1430 // If we get here it's not fatal, we just need to make sure we aren't
1431 // always blowing away the cache. If we are, then we should figure out why
1432 // and make it bigger.
1439 void SetX11ErrorHandlers(XErrorHandler error_handler,
1440 XIOErrorHandler io_error_handler) {
1441 XSetErrorHandler(error_handler ? error_handler : DefaultX11ErrorHandler);
1443 io_error_handler ? io_error_handler : DefaultX11IOErrorHandler);
1446 void LogErrorEventDescription(XDisplay* dpy,
1447 const XErrorEvent& error_event) {
1448 char error_str[256];
1449 char request_str[256];
1451 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str));
1453 strncpy(request_str, "Unknown", sizeof(request_str));
1454 if (error_event.request_code < 128) {
1455 std::string num = base::UintToString(error_event.request_code);
1456 XGetErrorDatabaseText(
1457 dpy, "XRequest", num.c_str(), "Unknown", request_str,
1458 sizeof(request_str));
1461 char** ext_list = XListExtensions(dpy, &num_ext);
1463 for (int i = 0; i < num_ext; i++) {
1464 int ext_code, first_event, first_error;
1465 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error);
1466 if (error_event.request_code == ext_code) {
1467 std::string msg = base::StringPrintf(
1468 "%s.%d", ext_list[i], error_event.minor_code);
1469 XGetErrorDatabaseText(
1470 dpy, "XRequest", msg.c_str(), "Unknown", request_str,
1471 sizeof(request_str));
1475 XFreeExtensionList(ext_list);
1479 << "X error received: "
1480 << "serial " << error_event.serial << ", "
1481 << "error_code " << static_cast<int>(error_event.error_code)
1482 << " (" << error_str << "), "
1483 << "request_code " << static_cast<int>(error_event.request_code) << ", "
1484 << "minor_code " << static_cast<int>(error_event.minor_code)
1485 << " (" << request_str << ")";
1488 // ----------------------------------------------------------------------------
1489 // End of x11_util_internal.h