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 #ifndef UI_BASE_X_X11_UTIL_H_
6 #define UI_BASE_X_X11_UTIL_H_
8 // This file declares utility functions for X11 (Linux only).
10 // These functions do not require the Xlib headers to be included (which is why
11 // we use a void* for Visual*). The Xlib headers are highly polluting so we try
12 // hard to limit their spread into the rest of the code.
17 #include "base/basictypes.h"
18 #include "base/event_types.h"
19 #include "base/memory/ref_counted_memory.h"
20 #include "ui/base/ui_base_export.h"
21 #include "ui/events/event_constants.h"
22 #include "ui/events/keycodes/keyboard_codes.h"
23 #include "ui/gfx/x/x11_types.h"
25 typedef unsigned long XSharedMemoryId
; // ShmSeg in the X headers.
26 typedef unsigned long Cursor
;
27 typedef struct _XcursorImage XcursorImage
;
28 typedef union _XEvent XEvent
;
40 // These functions use the default display and this /must/ be called from
41 // the UI thread. Thus, they don't support multiple displays.
43 // These functions cache their results ---------------------------------
45 // Returns true if the system supports XINPUT2.
46 UI_BASE_EXPORT
bool IsXInput2Available();
48 // Return true iff the display supports Xrender
49 UI_BASE_EXPORT
bool QueryRenderSupport(XDisplay
* dpy
);
51 // Returns an X11 Cursor, sharable across the process.
52 // |cursor_shape| is an X font cursor shape, see XCreateFontCursor().
53 UI_BASE_EXPORT ::Cursor
GetXCursor(int cursor_shape
);
55 // Creates a custom X cursor from the image. This takes ownership of image. The
56 // caller must not free/modify the image. The refcount of the newly created
57 // cursor is set to 1.
58 UI_BASE_EXPORT ::Cursor
CreateReffedCustomXCursor(XcursorImage
* image
);
60 // Increases the refcount of the custom cursor.
61 UI_BASE_EXPORT
void RefCustomXCursor(::Cursor cursor
);
63 // Decreases the refcount of the custom cursor, and destroys it if it reaches 0.
64 UI_BASE_EXPORT
void UnrefCustomXCursor(::Cursor cursor
);
66 // Creates a XcursorImage and copies the SkBitmap |bitmap| on it. |bitmap|
67 // should be non-null. Caller owns the returned object.
68 UI_BASE_EXPORT XcursorImage
* SkBitmapToXcursorImage(const SkBitmap
* bitmap
,
69 const gfx::Point
& hotspot
);
71 // Coalesce all pending motion events (touch or mouse) that are at the top of
72 // the queue, and return the number eliminated, storing the last one in
74 UI_BASE_EXPORT
int CoalescePendingMotionEvents(const XEvent
* xev
,
77 // Hides the host cursor.
78 UI_BASE_EXPORT
void HideHostCursor();
80 // Returns an invisible cursor.
81 UI_BASE_EXPORT ::Cursor
CreateInvisibleCursor();
83 // Sets whether |window| should use the OS window frame.
84 UI_BASE_EXPORT
void SetUseOSWindowFrame(XID window
, bool use_os_window_frame
);
86 // These functions do not cache their results --------------------------
88 // Returns true if the shape extension is supported.
89 UI_BASE_EXPORT
bool IsShapeExtensionAvailable();
91 // Get the X window id for the default root window
92 UI_BASE_EXPORT XID
GetX11RootWindow();
94 // Returns the user's current desktop.
95 UI_BASE_EXPORT
bool GetCurrentDesktop(int* desktop
);
97 enum HideTitlebarWhenMaximized
{
98 SHOW_TITLEBAR_WHEN_MAXIMIZED
= 0,
99 HIDE_TITLEBAR_WHEN_MAXIMIZED
= 1,
101 // Sets _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED on |window|.
102 UI_BASE_EXPORT
void SetHideTitlebarWhenMaximizedProperty(
104 HideTitlebarWhenMaximized property
);
106 // Clears all regions of X11's default root window by filling black pixels.
107 UI_BASE_EXPORT
void ClearX11DefaultRootWindow();
109 // Returns true if |window| is visible.
110 UI_BASE_EXPORT
bool IsWindowVisible(XID window
);
112 // Returns the inner bounds of |window| (excluding the non-client area).
113 UI_BASE_EXPORT
bool GetInnerWindowBounds(XID window
, gfx::Rect
* rect
);
115 // Returns the non-client area extents of |window|. This is a negative inset; it
116 // represents the negative size of the window border on all sides.
117 // InnerWindowBounds.Inset(WindowExtents) = OuterWindowBounds.
118 // Returns false if the window manager does not provide extents information.
119 UI_BASE_EXPORT
bool GetWindowExtents(XID window
, gfx::Insets
* extents
);
121 // Returns the outer bounds of |window| (including the non-client area).
122 UI_BASE_EXPORT
bool GetOuterWindowBounds(XID window
, gfx::Rect
* rect
);
124 // Returns true if |window| contains the point |screen_loc|.
125 UI_BASE_EXPORT
bool WindowContainsPoint(XID window
, gfx::Point screen_loc
);
127 // Return true if |window| has any property with |property_name|.
128 UI_BASE_EXPORT
bool PropertyExists(XID window
,
129 const std::string
& property_name
);
131 // Returns the raw bytes from a property with minimal
132 // interpretation. |out_data| should be freed by XFree() after use.
133 UI_BASE_EXPORT
bool GetRawBytesOfProperty(
136 scoped_refptr
<base::RefCountedMemory
>* out_data
,
137 size_t* out_data_items
,
140 // Get the value of an int, int array, atom array or string property. On
141 // success, true is returned and the value is stored in |value|.
143 // TODO(erg): Once we remove the gtk port and are 100% aura, all of these
144 // should accept an XAtom instead of a string.
145 UI_BASE_EXPORT
bool GetIntProperty(XID window
,
146 const std::string
& property_name
,
148 UI_BASE_EXPORT
bool GetXIDProperty(XID window
,
149 const std::string
& property_name
,
151 UI_BASE_EXPORT
bool GetIntArrayProperty(XID window
,
152 const std::string
& property_name
,
153 std::vector
<int>* value
);
154 UI_BASE_EXPORT
bool GetAtomArrayProperty(XID window
,
155 const std::string
& property_name
,
156 std::vector
<XAtom
>* value
);
157 UI_BASE_EXPORT
bool GetStringProperty(XID window
,
158 const std::string
& property_name
,
161 // These setters all make round trips.
162 UI_BASE_EXPORT
bool SetIntProperty(XID window
,
163 const std::string
& name
,
164 const std::string
& type
,
166 UI_BASE_EXPORT
bool SetIntArrayProperty(XID window
,
167 const std::string
& name
,
168 const std::string
& type
,
169 const std::vector
<int>& value
);
170 UI_BASE_EXPORT
bool SetAtomProperty(XID window
,
171 const std::string
& name
,
172 const std::string
& type
,
174 UI_BASE_EXPORT
bool SetAtomArrayProperty(XID window
,
175 const std::string
& name
,
176 const std::string
& type
,
177 const std::vector
<XAtom
>& value
);
178 UI_BASE_EXPORT
bool SetStringProperty(XID window
,
181 const std::string
& value
);
183 // Gets the X atom for default display corresponding to atom_name.
184 UI_BASE_EXPORT XAtom
GetAtom(const char* atom_name
);
186 // Sets the WM_CLASS attribute for a given X11 window.
187 UI_BASE_EXPORT
void SetWindowClassHint(XDisplay
* display
,
189 const std::string
& res_name
,
190 const std::string
& res_class
);
192 // Sets the WM_WINDOW_ROLE attribute for a given X11 window.
193 UI_BASE_EXPORT
void SetWindowRole(XDisplay
* display
,
195 const std::string
& role
);
197 // Determine whether we should default to native decorations or the custom
198 // frame based on the currently-running window manager.
199 UI_BASE_EXPORT
bool GetCustomFramePrefDefault();
201 static const int kAllDesktops
= -1;
202 // Queries the desktop |window| is on, kAllDesktops if sticky. Returns false if
203 // property not found.
204 bool GetWindowDesktop(XID window
, int* desktop
);
206 // Translates an X11 error code into a printable string.
207 UI_BASE_EXPORT
std::string
GetX11ErrorString(XDisplay
* display
, int err
);
209 // Implementers of this interface receive a notification for every X window of
211 class EnumerateWindowsDelegate
{
213 // |xid| is the X Window ID of the enumerated window. Return true to stop
214 // further iteration.
215 virtual bool ShouldStopIterating(XID xid
) = 0;
218 virtual ~EnumerateWindowsDelegate() {}
221 // Enumerates all windows in the current display. Will recurse into child
222 // windows up to a depth of |max_depth|.
223 UI_BASE_EXPORT
bool EnumerateAllWindows(EnumerateWindowsDelegate
* delegate
,
226 // Enumerates the top-level windows of the current display.
227 UI_BASE_EXPORT
void EnumerateTopLevelWindows(
228 ui::EnumerateWindowsDelegate
* delegate
);
230 // Returns all children windows of a given window in top-to-bottom stacking
232 UI_BASE_EXPORT
bool GetXWindowStack(XID window
, std::vector
<XID
>* windows
);
234 // Copies |source_bounds| from |drawable| to |canvas| at offset |dest_offset|.
235 // |source_bounds| is in physical pixels, while |dest_offset| is relative to
236 // the canvas's scale. Note that this function is slow since it uses
237 // XGetImage() to copy the data from the X server to this process before
238 // copying it to |canvas|.
239 UI_BASE_EXPORT
bool CopyAreaToCanvas(XID drawable
,
240 gfx::Rect source_bounds
,
241 gfx::Point dest_offset
,
242 gfx::Canvas
* canvas
);
244 enum WindowManagerName
{
268 // Attempts to guess the window maager. Returns WM_UNKNOWN if we can't
269 // determine it for one reason or another.
270 UI_BASE_EXPORT WindowManagerName
GuessWindowManager();
272 // The same as GuessWindowManager(), but returns the raw string. If we
273 // can't determine it, return "Unknown".
274 UI_BASE_EXPORT
std::string
GuessWindowManagerName();
276 // Enable the default X error handlers. These will log the error and abort
277 // the process if called. Use SetX11ErrorHandlers() from x11_util_internal.h
278 // to set your own error handlers.
279 UI_BASE_EXPORT
void SetDefaultX11ErrorHandlers();
281 // Returns true if a given window is in full-screen mode.
282 UI_BASE_EXPORT
bool IsX11WindowFullScreen(XID window
);
284 // Returns true if the window manager supports the given hint.
285 UI_BASE_EXPORT
bool WmSupportsHint(XAtom atom
);
287 // Manages a piece of X11 allocated memory as a RefCountedMemory segment. This
288 // object takes ownership over the passed in memory and will free it with the
289 // X11 allocator when done.
290 class UI_BASE_EXPORT XRefcountedMemory
: public base::RefCountedMemory
{
292 XRefcountedMemory(unsigned char* x11_data
, size_t length
);
294 // Overridden from RefCountedMemory:
295 const unsigned char* front() const override
;
296 size_t size() const override
;
299 ~XRefcountedMemory() override
;
301 gfx::XScopedPtr
<unsigned char> x11_data_
;
304 DISALLOW_COPY_AND_ASSIGN(XRefcountedMemory
);
307 // Keeps track of a cursor returned by an X function and makes sure it's
309 class UI_BASE_EXPORT XScopedCursor
{
311 // Keeps track of |cursor| created with |display|.
312 XScopedCursor(::Cursor cursor
, XDisplay
* display
);
315 ::Cursor
get() const;
316 void reset(::Cursor cursor
);
322 DISALLOW_COPY_AND_ASSIGN(XScopedCursor
);
326 // Resets the cache used by GetXCursor(). Only useful for tests that may delete
328 UI_BASE_EXPORT
void ResetXCursorCache();
330 // Returns the cached XcursorImage for |cursor|.
331 UI_BASE_EXPORT
const XcursorImage
* GetCachedXcursorImage(::Cursor cursor
);
337 #endif // UI_BASE_X_X11_UTIL_H_