Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ui / base / x / x11_util.h
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.
4
5 #ifndef UI_BASE_X_X11_UTIL_H_
6 #define UI_BASE_X_X11_UTIL_H_
7
8 // This file declares utility functions for X11 (Linux only).
9 //
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.
13
14 #include <string>
15 #include <vector>
16
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/point.h"
24 #include "ui/gfx/x/x11_types.h"
25
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;
31
32 #if defined(TOOLKIT_GTK)
33 typedef struct _GdkDrawable GdkWindow;
34 typedef struct _GtkWidget GtkWidget;
35 typedef struct _GtkWindow GtkWindow;
36 #endif
37
38 namespace gfx {
39 class Canvas;
40 class Point;
41 class Rect;
42 }
43 class SkBitmap;
44
45 namespace ui {
46
47 // These functions use the default display and this /must/ be called from
48 // the UI thread. Thus, they don't support multiple displays.
49
50 // These functions cache their results ---------------------------------
51
52 // Check if there's an open connection to an X server.
53 UI_BASE_EXPORT bool XDisplayExists();
54
55 // Returns true if the system supports XINPUT2.
56 UI_BASE_EXPORT bool IsXInput2Available();
57
58 // X shared memory comes in three flavors:
59 // 1) No SHM support,
60 // 2) SHM putimage,
61 // 3) SHM pixmaps + putimage.
62 enum SharedMemorySupport {
63   SHARED_MEMORY_NONE,
64   SHARED_MEMORY_PUTIMAGE,
65   SHARED_MEMORY_PIXMAP
66 };
67 // Return the shared memory type of our X connection.
68 UI_BASE_EXPORT SharedMemorySupport QuerySharedMemorySupport(XDisplay* dpy);
69
70 // Return true iff the display supports Xrender
71 UI_BASE_EXPORT bool QueryRenderSupport(XDisplay* dpy);
72
73 // Return the default screen number for the display
74 int GetDefaultScreen(XDisplay* display);
75
76 // Returns an X11 Cursor, sharable across the process.
77 // |cursor_shape| is an X font cursor shape, see XCreateFontCursor().
78 UI_BASE_EXPORT ::Cursor GetXCursor(int cursor_shape);
79
80 // Resets the cache used by GetXCursor(). Only useful for tests that may delete
81 // the display.
82 UI_BASE_EXPORT void ResetXCursorCache();
83
84 #if defined(USE_AURA)
85 // Creates a custom X cursor from the image. This takes ownership of image. The
86 // caller must not free/modify the image. The refcount of the newly created
87 // cursor is set to 1.
88 UI_BASE_EXPORT ::Cursor CreateReffedCustomXCursor(XcursorImage* image);
89
90 // Increases the refcount of the custom cursor.
91 UI_BASE_EXPORT void RefCustomXCursor(::Cursor cursor);
92
93 // Decreases the refcount of the custom cursor, and destroys it if it reaches 0.
94 UI_BASE_EXPORT void UnrefCustomXCursor(::Cursor cursor);
95
96 // Creates a XcursorImage and copies the SkBitmap |bitmap| on it. |bitmap|
97 // should be non-null. Caller owns the returned object.
98 UI_BASE_EXPORT XcursorImage* SkBitmapToXcursorImage(const SkBitmap* bitmap,
99                                                     const gfx::Point& hotspot);
100
101 // Coalesce all pending motion events (touch or mouse) that are at the top of
102 // the queue, and return the number eliminated, storing the last one in
103 // |last_event|.
104 UI_BASE_EXPORT int CoalescePendingMotionEvents(const XEvent* xev,
105                                                XEvent* last_event);
106 #endif
107
108 // Hides the host cursor.
109 UI_BASE_EXPORT void HideHostCursor();
110
111 // Returns an invisible cursor.
112 UI_BASE_EXPORT ::Cursor CreateInvisibleCursor();
113
114 // These functions do not cache their results --------------------------
115
116 // Get the X window id for the default root window
117 UI_BASE_EXPORT XID GetX11RootWindow();
118
119 // Returns the user's current desktop.
120 bool GetCurrentDesktop(int* desktop);
121
122 #if defined(TOOLKIT_GTK)
123 // Get the X window id for the given GTK widget.
124 UI_BASE_EXPORT XID GetX11WindowFromGtkWidget(GtkWidget* widget);
125 XID GetX11WindowFromGdkWindow(GdkWindow* window);
126
127 // Get the GtkWindow* wrapping a given XID, if any.
128 // Returns NULL if there isn't already a GtkWindow* wrapping this XID;
129 // see gdk_window_foreign_new() etc. to wrap arbitrary XIDs.
130 UI_BASE_EXPORT GtkWindow* GetGtkWindowFromX11Window(XID xid);
131
132 // Get a Visual from the given widget. Since we don't include the Xlib
133 // headers, this is returned as a void*.
134 UI_BASE_EXPORT void* GetVisualFromGtkWidget(GtkWidget* widget);
135 #endif  // defined(TOOLKIT_GTK)
136
137 enum HideTitlebarWhenMaximized {
138   SHOW_TITLEBAR_WHEN_MAXIMIZED = 0,
139   HIDE_TITLEBAR_WHEN_MAXIMIZED = 1,
140 };
141 // Sets _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED on |window|.
142 UI_BASE_EXPORT void SetHideTitlebarWhenMaximizedProperty(
143     XID window,
144     HideTitlebarWhenMaximized property);
145
146 // Clears all regions of X11's default root window by filling black pixels.
147 UI_BASE_EXPORT void ClearX11DefaultRootWindow();
148
149 // Returns true if |window| is visible.
150 UI_BASE_EXPORT bool IsWindowVisible(XID window);
151
152 // Returns the bounds of |window|.
153 UI_BASE_EXPORT bool GetWindowRect(XID window, gfx::Rect* rect);
154
155 // Returns true if |window| contains the point |screen_loc|.
156 UI_BASE_EXPORT bool WindowContainsPoint(XID window, gfx::Point screen_loc);
157
158 // Return true if |window| has any property with |property_name|.
159 UI_BASE_EXPORT bool PropertyExists(XID window,
160                                    const std::string& property_name);
161
162 // Returns the raw bytes from a property with minimal
163 // interpretation. |out_data| should be freed by XFree() after use.
164 UI_BASE_EXPORT bool GetRawBytesOfProperty(
165     XID window,
166     Atom property,
167     scoped_refptr<base::RefCountedMemory>* out_data,
168     size_t* out_data_bytes,
169     size_t* out_data_items,
170     Atom* out_type);
171
172 // Get the value of an int, int array, atom array or string property.  On
173 // success, true is returned and the value is stored in |value|.
174 //
175 // TODO(erg): Once we remove the gtk port and are 100% aura, all of these
176 // should accept an Atom instead of a string.
177 UI_BASE_EXPORT bool GetIntProperty(XID window,
178                                    const std::string& property_name,
179                                    int* value);
180 UI_BASE_EXPORT bool GetXIDProperty(XID window,
181                                    const std::string& property_name,
182                                    XID* value);
183 UI_BASE_EXPORT bool GetIntArrayProperty(XID window,
184                                         const std::string& property_name,
185                                         std::vector<int>* value);
186 UI_BASE_EXPORT bool GetAtomArrayProperty(XID window,
187                                          const std::string& property_name,
188                                          std::vector<Atom>* value);
189 UI_BASE_EXPORT bool GetStringProperty(XID window,
190                                       const std::string& property_name,
191                                       std::string* value);
192
193 // These setters all make round trips.
194 UI_BASE_EXPORT bool SetIntProperty(XID window,
195                                    const std::string& name,
196                                    const std::string& type,
197                                    int value);
198 UI_BASE_EXPORT bool SetIntArrayProperty(XID window,
199                                         const std::string& name,
200                                         const std::string& type,
201                                         const std::vector<int>& value);
202 UI_BASE_EXPORT bool SetAtomArrayProperty(XID window,
203                                          const std::string& name,
204                                          const std::string& type,
205                                          const std::vector<Atom>& value);
206 UI_BASE_EXPORT bool SetStringProperty(XID window,
207                                       Atom property,
208                                       Atom type,
209                                       const std::string& value);
210
211 // Gets the X atom for default display corresponding to atom_name.
212 Atom GetAtom(const char* atom_name);
213
214 // Sets the WM_CLASS attribute for a given X11 window.
215 UI_BASE_EXPORT void SetWindowClassHint(XDisplay* display,
216                                        XID window,
217                                        const std::string& res_name,
218                                        const std::string& res_class);
219
220 // Sets the WM_WINDOW_ROLE attribute for a given X11 window.
221 UI_BASE_EXPORT void SetWindowRole(XDisplay* display,
222                                   XID window,
223                                   const std::string& role);
224
225 // Get |window|'s parent window, or None if |window| is the root window.
226 UI_BASE_EXPORT XID GetParentWindow(XID window);
227
228 // Walk up |window|'s hierarchy until we find a direct child of |root|.
229 XID GetHighestAncestorWindow(XID window, XID root);
230
231 // Determine whether we should default to native decorations or the custom
232 // frame based on the currently-running window manager.
233 UI_BASE_EXPORT bool GetCustomFramePrefDefault();
234
235 static const int kAllDesktops = -1;
236 // Queries the desktop |window| is on, kAllDesktops if sticky. Returns false if
237 // property not found.
238 bool GetWindowDesktop(XID window, int* desktop);
239
240 // Translates an X11 error code into a printable string.
241 UI_BASE_EXPORT std::string GetX11ErrorString(XDisplay* display, int err);
242
243 // Implementers of this interface receive a notification for every X window of
244 // the main display.
245 class EnumerateWindowsDelegate {
246  public:
247   // |xid| is the X Window ID of the enumerated window.  Return true to stop
248   // further iteration.
249   virtual bool ShouldStopIterating(XID xid) = 0;
250
251  protected:
252   virtual ~EnumerateWindowsDelegate() {}
253 };
254
255 // Enumerates all windows in the current display.  Will recurse into child
256 // windows up to a depth of |max_depth|.
257 UI_BASE_EXPORT bool EnumerateAllWindows(EnumerateWindowsDelegate* delegate,
258                                         int max_depth);
259
260 // Enumerates the top-level windows of the current display.
261 UI_BASE_EXPORT void EnumerateTopLevelWindows(
262     ui::EnumerateWindowsDelegate* delegate);
263
264 // Returns all children windows of a given window in top-to-bottom stacking
265 // order.
266 UI_BASE_EXPORT bool GetXWindowStack(XID window, std::vector<XID>* windows);
267
268 // Restack a window in relation to one of its siblings.  If |above| is true,
269 // |window| will be stacked directly above |sibling|; otherwise it will stacked
270 // directly below it.  Both windows must be immediate children of the same
271 // window.
272 void RestackWindow(XID window, XID sibling, bool above);
273
274 // Return a handle to a X ShmSeg. |shared_memory_key| is a SysV
275 // IPC key. The shared memory region must contain 32-bit pixels.
276 UI_BASE_EXPORT XSharedMemoryId
277     AttachSharedMemory(XDisplay* display, int shared_memory_support);
278 UI_BASE_EXPORT void DetachSharedMemory(XDisplay* display,
279                                        XSharedMemoryId shmseg);
280
281 // Copies |source_bounds| from |drawable| to |canvas| at offset |dest_offset|.
282 // |source_bounds| is in physical pixels, while |dest_offset| is relative to
283 // the canvas's scale. Note that this function is slow since it uses
284 // XGetImage() to copy the data from the X server to this process before
285 // copying it to |canvas|.
286 UI_BASE_EXPORT bool CopyAreaToCanvas(XID drawable,
287                                      gfx::Rect source_bounds,
288                                      gfx::Point dest_offset,
289                                      gfx::Canvas* canvas);
290
291 // Return a handle to an XRender picture where |pixmap| is a handle to a
292 // pixmap containing Skia ARGB data.
293 UI_BASE_EXPORT XID CreatePictureFromSkiaPixmap(XDisplay* display, XID pixmap);
294
295 void FreePicture(XDisplay* display, XID picture);
296 void FreePixmap(XDisplay* display, XID pixmap);
297
298 enum WindowManagerName {
299   WM_UNKNOWN,
300   WM_BLACKBOX,
301   WM_CHROME_OS,
302   WM_COMPIZ,
303   WM_ENLIGHTENMENT,
304   WM_ICE_WM,
305   WM_KWIN,
306   WM_METACITY,
307   WM_MUFFIN,
308   WM_MUTTER,
309   WM_OPENBOX,
310   WM_XFWM4,
311 };
312 // Attempts to guess the window maager. Returns WM_UNKNOWN if we can't
313 // determine it for one reason or another.
314 UI_BASE_EXPORT WindowManagerName GuessWindowManager();
315
316 // Change desktop for |window| to the desktop of |destination| window.
317 UI_BASE_EXPORT bool ChangeWindowDesktop(XID window, XID destination);
318
319 // Enable the default X error handlers. These will log the error and abort
320 // the process if called. Use SetX11ErrorHandlers() from x11_util_internal.h
321 // to set your own error handlers.
322 UI_BASE_EXPORT void SetDefaultX11ErrorHandlers();
323
324 // Return true if a given window is in full-screen mode.
325 UI_BASE_EXPORT bool IsX11WindowFullScreen(XID window);
326
327 // Returns true if a given size is in list of bogus sizes in mm that X detects
328 // that should be ignored.
329 UI_BASE_EXPORT bool IsXDisplaySizeBlackListed(unsigned long mm_width,
330                                               unsigned long mm_height);
331
332 // Manages a piece of X11 allocated memory as a RefCountedMemory segment. This
333 // object takes ownership over the passed in memory and will free it with the
334 // X11 allocator when done.
335 class UI_BASE_EXPORT XRefcountedMemory : public base::RefCountedMemory {
336  public:
337   XRefcountedMemory(unsigned char* x11_data, size_t length)
338       : x11_data_(length ? x11_data : NULL), length_(length) {}
339
340   // Overridden from RefCountedMemory:
341   virtual const unsigned char* front() const OVERRIDE;
342   virtual size_t size() const OVERRIDE;
343
344  private:
345   virtual ~XRefcountedMemory();
346
347   unsigned char* x11_data_;
348   size_t length_;
349
350   DISALLOW_COPY_AND_ASSIGN(XRefcountedMemory);
351 };
352
353 // Keeps track of a string returned by an X function (e.g. XGetAtomName) and
354 // makes sure it's XFree'd.
355 class UI_BASE_EXPORT XScopedString {
356  public:
357   explicit XScopedString(char* str) : string_(str) {}
358   ~XScopedString();
359
360   const char* string() const { return string_; }
361
362  private:
363   char* string_;
364
365   DISALLOW_COPY_AND_ASSIGN(XScopedString);
366 };
367
368 // Keeps track of an image returned by an X function (e.g. XGetImage) and
369 // makes sure it's XDestroyImage'd.
370 class UI_BASE_EXPORT XScopedImage {
371  public:
372   explicit XScopedImage(XImage* image) : image_(image) {}
373   ~XScopedImage();
374
375   XImage* get() const { return image_; }
376
377   XImage* operator->() const { return image_; }
378
379   void reset(XImage* image);
380
381  private:
382   XImage* image_;
383
384   DISALLOW_COPY_AND_ASSIGN(XScopedImage);
385 };
386
387 // Keeps track of a cursor returned by an X function and makes sure it's
388 // XFreeCursor'd.
389 class UI_BASE_EXPORT XScopedCursor {
390  public:
391   // Keeps track of |cursor| created with |display|.
392   XScopedCursor(::Cursor cursor, XDisplay* display);
393   ~XScopedCursor();
394
395   ::Cursor get() const;
396   void reset(::Cursor cursor);
397
398  private:
399   ::Cursor cursor_;
400   XDisplay* display_;
401
402   DISALLOW_COPY_AND_ASSIGN(XScopedCursor);
403 };
404
405 }  // namespace ui
406
407 #endif  // UI_BASE_X_X11_UTIL_H_