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_export.h"
21 #include "ui/events/event_constants.h"
22 #include "ui/events/keycodes/keyboard_codes.h"
23 #include "ui/gfx/point.h"
24 #include "ui/gfx/x/x11_types.h"
26 typedef unsigned long Atom;
27 typedef unsigned long XSharedMemoryId; // ShmSeg in the X headers.
28 typedef unsigned long Cursor;
29 typedef struct _XcursorImage XcursorImage;
30 typedef union _XEvent XEvent;
32 #if defined(TOOLKIT_GTK)
33 typedef struct _GdkDrawable GdkWindow;
34 typedef struct _GtkWidget GtkWidget;
35 typedef struct _GtkWindow GtkWindow;
47 // These functions use the default display and this /must/ be called from
48 // the UI thread. Thus, they don't support multiple displays.
50 // These functions cache their results ---------------------------------
52 // Check if there's an open connection to an X server.
53 UI_EXPORT bool XDisplayExists();
54 // Return an X11 connection for the current, primary display.
56 // X shared memory comes in three flavors:
59 // 3) SHM pixmaps + putimage.
60 enum SharedMemorySupport {
62 SHARED_MEMORY_PUTIMAGE,
65 // Return the shared memory type of our X connection.
66 UI_EXPORT SharedMemorySupport QuerySharedMemorySupport(XDisplay* dpy);
68 // Return true iff the display supports Xrender
69 UI_EXPORT bool QueryRenderSupport(XDisplay* dpy);
71 // Return the default screen number for the display
72 int GetDefaultScreen(XDisplay* display);
74 // Returns an X11 Cursor, sharable across the process.
75 // |cursor_shape| is an X font cursor shape, see XCreateFontCursor().
76 UI_EXPORT ::Cursor GetXCursor(int cursor_shape);
78 // Resets the cache used by GetXCursor(). Only useful for tests that may delete
80 UI_EXPORT void ResetXCursorCache();
83 // Creates a custom X cursor from the image. This takes ownership of image. The
84 // caller must not free/modify the image. The refcount of the newly created
85 // cursor is set to 1.
86 UI_EXPORT ::Cursor CreateReffedCustomXCursor(XcursorImage* image);
88 // Increases the refcount of the custom cursor.
89 UI_EXPORT void RefCustomXCursor(::Cursor cursor);
91 // Decreases the refcount of the custom cursor, and destroys it if it reaches 0.
92 UI_EXPORT void UnrefCustomXCursor(::Cursor cursor);
94 // Creates a XcursorImage and copies the SkBitmap |bitmap| on it. |bitmap|
95 // should be non-null. Caller owns the returned object.
96 UI_EXPORT XcursorImage* SkBitmapToXcursorImage(const SkBitmap* bitmap,
97 const gfx::Point& hotspot);
99 // Coalesce all pending motion events (touch or mouse) that are at the top of
100 // the queue, and return the number eliminated, storing the last one in
102 UI_EXPORT int CoalescePendingMotionEvents(const XEvent* xev,
106 // Hides the host cursor.
107 UI_EXPORT void HideHostCursor();
109 // Returns an invisible cursor.
110 UI_EXPORT ::Cursor CreateInvisibleCursor();
112 // These functions do not cache their results --------------------------
114 // Get the X window id for the default root window
115 UI_EXPORT XID GetX11RootWindow();
117 // Returns the user's current desktop.
118 bool GetCurrentDesktop(int* desktop);
120 #if defined(TOOLKIT_GTK)
121 // Get the X window id for the given GTK widget.
122 UI_EXPORT XID GetX11WindowFromGtkWidget(GtkWidget* widget);
123 XID GetX11WindowFromGdkWindow(GdkWindow* window);
125 // Get the GtkWindow* wrapping a given XID, if any.
126 // Returns NULL if there isn't already a GtkWindow* wrapping this XID;
127 // see gdk_window_foreign_new() etc. to wrap arbitrary XIDs.
128 UI_EXPORT GtkWindow* GetGtkWindowFromX11Window(XID xid);
130 // Get a Visual from the given widget. Since we don't include the Xlib
131 // headers, this is returned as a void*.
132 UI_EXPORT void* GetVisualFromGtkWidget(GtkWidget* widget);
133 #endif // defined(TOOLKIT_GTK)
135 enum HideTitlebarWhenMaximized {
136 SHOW_TITLEBAR_WHEN_MAXIMIZED = 0,
137 HIDE_TITLEBAR_WHEN_MAXIMIZED = 1,
139 // Sets _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED on |window|.
140 UI_EXPORT void SetHideTitlebarWhenMaximizedProperty(
142 HideTitlebarWhenMaximized property);
144 // Clears all regions of X11's default root window by filling black pixels.
145 UI_EXPORT void ClearX11DefaultRootWindow();
147 // Returns true if |window| is visible.
148 UI_EXPORT bool IsWindowVisible(XID window);
150 // Returns the bounds of |window|.
151 UI_EXPORT bool GetWindowRect(XID window, gfx::Rect* rect);
153 // Returns true if |window| contains the point |screen_loc|.
154 UI_EXPORT bool WindowContainsPoint(XID window, gfx::Point screen_loc);
156 // Return true if |window| has any property with |property_name|.
157 UI_EXPORT bool PropertyExists(XID window, const std::string& property_name);
159 // Returns the raw bytes from a property with minimal
160 // interpretation. |out_data| should be freed by XFree() after use.
161 UI_EXPORT bool GetRawBytesOfProperty(
164 scoped_refptr<base::RefCountedMemory>* out_data,
165 size_t* out_data_bytes,
166 size_t* out_data_items,
169 // Get the value of an int, int array, atom array or string property. On
170 // success, true is returned and the value is stored in |value|.
172 // TODO(erg): Once we remove the gtk port and are 100% aura, all of these
173 // should accept an Atom instead of a string.
174 UI_EXPORT bool GetIntProperty(XID window, const std::string& property_name,
176 UI_EXPORT bool GetXIDProperty(XID window, const std::string& property_name,
178 UI_EXPORT bool GetIntArrayProperty(XID window, const std::string& property_name,
179 std::vector<int>* value);
180 UI_EXPORT bool GetAtomArrayProperty(XID window,
181 const std::string& property_name,
182 std::vector<Atom>* value);
183 UI_EXPORT bool GetStringProperty(
184 XID window, const std::string& property_name, std::string* value);
186 // These setters all make round trips.
187 UI_EXPORT bool SetIntProperty(XID window,
188 const std::string& name,
189 const std::string& type,
191 UI_EXPORT bool SetIntArrayProperty(XID window,
192 const std::string& name,
193 const std::string& type,
194 const std::vector<int>& value);
195 UI_EXPORT bool SetAtomArrayProperty(XID window,
196 const std::string& name,
197 const std::string& type,
198 const std::vector<Atom>& value);
200 // Gets the X atom for default display corresponding to atom_name.
201 Atom GetAtom(const char* atom_name);
203 // Sets the WM_CLASS attribute for a given X11 window.
204 UI_EXPORT void SetWindowClassHint(XDisplay* display,
206 std::string res_name,
207 std::string res_class);
209 // Get |window|'s parent window, or None if |window| is the root window.
210 UI_EXPORT XID GetParentWindow(XID window);
212 // Walk up |window|'s hierarchy until we find a direct child of |root|.
213 XID GetHighestAncestorWindow(XID window, XID root);
215 static const int kAllDesktops = -1;
216 // Queries the desktop |window| is on, kAllDesktops if sticky. Returns false if
217 // property not found.
218 bool GetWindowDesktop(XID window, int* desktop);
220 // Translates an X11 error code into a printable string.
221 UI_EXPORT std::string GetX11ErrorString(XDisplay* display, int err);
223 // Implementers of this interface receive a notification for every X window of
225 class EnumerateWindowsDelegate {
227 // |xid| is the X Window ID of the enumerated window. Return true to stop
228 // further iteration.
229 virtual bool ShouldStopIterating(XID xid) = 0;
232 virtual ~EnumerateWindowsDelegate() {}
235 // Enumerates all windows in the current display. Will recurse into child
236 // windows up to a depth of |max_depth|.
237 UI_EXPORT bool EnumerateAllWindows(EnumerateWindowsDelegate* delegate,
240 // Enumerates the top-level windows of the current display.
241 UI_EXPORT void EnumerateTopLevelWindows(ui::EnumerateWindowsDelegate* delegate);
243 // Returns all children windows of a given window in top-to-bottom stacking
245 UI_EXPORT bool GetXWindowStack(XID window, std::vector<XID>* windows);
247 // Restack a window in relation to one of its siblings. If |above| is true,
248 // |window| will be stacked directly above |sibling|; otherwise it will stacked
249 // directly below it. Both windows must be immediate children of the same
251 void RestackWindow(XID window, XID sibling, bool above);
253 // Return a handle to a X ShmSeg. |shared_memory_key| is a SysV
254 // IPC key. The shared memory region must contain 32-bit pixels.
255 UI_EXPORT XSharedMemoryId AttachSharedMemory(XDisplay* display,
256 int shared_memory_support);
257 UI_EXPORT void DetachSharedMemory(XDisplay* display, XSharedMemoryId shmseg);
259 // Copies |source_bounds| from |drawable| to |canvas| at offset |dest_offset|.
260 // |source_bounds| is in physical pixels, while |dest_offset| is relative to
261 // the canvas's scale. Note that this function is slow since it uses
262 // XGetImage() to copy the data from the X server to this process before
263 // copying it to |canvas|.
264 UI_EXPORT bool CopyAreaToCanvas(XID drawable,
265 gfx::Rect source_bounds,
266 gfx::Point dest_offset,
267 gfx::Canvas* canvas);
269 // Return a handle to an XRender picture where |pixmap| is a handle to a
270 // pixmap containing Skia ARGB data.
271 UI_EXPORT XID CreatePictureFromSkiaPixmap(XDisplay* display, XID pixmap);
273 void FreePicture(XDisplay* display, XID picture);
274 void FreePixmap(XDisplay* display, XID pixmap);
276 enum WindowManagerName {
290 // Attempts to guess the window maager. Returns WM_UNKNOWN if we can't
291 // determine it for one reason or another.
292 UI_EXPORT WindowManagerName GuessWindowManager();
294 // Change desktop for |window| to the desktop of |destination| window.
295 UI_EXPORT bool ChangeWindowDesktop(XID window, XID destination);
297 // Enable the default X error handlers. These will log the error and abort
298 // the process if called. Use SetX11ErrorHandlers() from x11_util_internal.h
299 // to set your own error handlers.
300 UI_EXPORT void SetDefaultX11ErrorHandlers();
302 // Return true if a given window is in full-screen mode.
303 UI_EXPORT bool IsX11WindowFullScreen(XID window);
305 // Manages a piece of X11 allocated memory as a RefCountedMemory segment. This
306 // object takes ownership over the passed in memory and will free it with the
307 // X11 allocator when done.
308 class UI_EXPORT XRefcountedMemory : public base::RefCountedMemory {
310 XRefcountedMemory(unsigned char* x11_data, size_t length)
311 : x11_data_(length ? x11_data : NULL),
315 // Overridden from RefCountedMemory:
316 virtual const unsigned char* front() const OVERRIDE;
317 virtual size_t size() const OVERRIDE;
320 virtual ~XRefcountedMemory();
322 unsigned char* x11_data_;
325 DISALLOW_COPY_AND_ASSIGN(XRefcountedMemory);
328 // Keeps track of a string returned by an X function (e.g. XGetAtomName) and
329 // makes sure it's XFree'd.
330 class UI_EXPORT XScopedString {
332 explicit XScopedString(char* str) : string_(str) {}
335 const char* string() const { return string_; }
340 DISALLOW_COPY_AND_ASSIGN(XScopedString);
343 // Keeps track of an image returned by an X function (e.g. XGetImage) and
344 // makes sure it's XDestroyImage'd.
345 class UI_EXPORT XScopedImage {
347 explicit XScopedImage(XImage* image) : image_(image) {}
350 XImage* get() const {
354 XImage* operator->() const {
358 void reset(XImage* image);
363 DISALLOW_COPY_AND_ASSIGN(XScopedImage);
366 // Keeps track of a cursor returned by an X function and makes sure it's
368 class UI_EXPORT XScopedCursor {
370 // Keeps track of |cursor| created with |display|.
371 XScopedCursor(::Cursor cursor, XDisplay* display);
374 ::Cursor get() const;
375 void reset(::Cursor cursor);
381 DISALLOW_COPY_AND_ASSIGN(XScopedCursor);
386 #endif // UI_BASE_X_X11_UTIL_H_