- add sources.
[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_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_EXPORT bool XDisplayExists();
54 // Return an X11 connection for the current, primary display.
55
56 // X shared memory comes in three flavors:
57 // 1) No SHM support,
58 // 2) SHM putimage,
59 // 3) SHM pixmaps + putimage.
60 enum SharedMemorySupport {
61   SHARED_MEMORY_NONE,
62   SHARED_MEMORY_PUTIMAGE,
63   SHARED_MEMORY_PIXMAP
64 };
65 // Return the shared memory type of our X connection.
66 UI_EXPORT SharedMemorySupport QuerySharedMemorySupport(XDisplay* dpy);
67
68 // Return true iff the display supports Xrender
69 UI_EXPORT bool QueryRenderSupport(XDisplay* dpy);
70
71 // Return the default screen number for the display
72 int GetDefaultScreen(XDisplay* display);
73
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);
77
78 // Resets the cache used by GetXCursor(). Only useful for tests that may delete
79 // the display.
80 UI_EXPORT void ResetXCursorCache();
81
82 #if defined(USE_AURA)
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);
87
88 // Increases the refcount of the custom cursor.
89 UI_EXPORT void RefCustomXCursor(::Cursor cursor);
90
91 // Decreases the refcount of the custom cursor, and destroys it if it reaches 0.
92 UI_EXPORT void UnrefCustomXCursor(::Cursor cursor);
93
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);
98
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
101 // |last_event|.
102 UI_EXPORT int CoalescePendingMotionEvents(const XEvent* xev,
103                                           XEvent* last_event);
104 #endif
105
106 // Hides the host cursor.
107 UI_EXPORT void HideHostCursor();
108
109 // Returns an invisible cursor.
110 UI_EXPORT ::Cursor CreateInvisibleCursor();
111
112 // These functions do not cache their results --------------------------
113
114 // Get the X window id for the default root window
115 UI_EXPORT XID GetX11RootWindow();
116
117 // Returns the user's current desktop.
118 bool GetCurrentDesktop(int* desktop);
119
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);
124
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);
129
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)
134
135 enum HideTitlebarWhenMaximized {
136   SHOW_TITLEBAR_WHEN_MAXIMIZED = 0,
137   HIDE_TITLEBAR_WHEN_MAXIMIZED = 1,
138 };
139 // Sets _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED on |window|.
140 UI_EXPORT void SetHideTitlebarWhenMaximizedProperty(
141     XID window,
142     HideTitlebarWhenMaximized property);
143
144 // Clears all regions of X11's default root window by filling black pixels.
145 UI_EXPORT void ClearX11DefaultRootWindow();
146
147 // Returns true if |window| is visible.
148 UI_EXPORT bool IsWindowVisible(XID window);
149
150 // Returns the bounds of |window|.
151 UI_EXPORT bool GetWindowRect(XID window, gfx::Rect* rect);
152
153 // Returns true if |window| contains the point |screen_loc|.
154 UI_EXPORT bool WindowContainsPoint(XID window, gfx::Point screen_loc);
155
156 // Return true if |window| has any property with |property_name|.
157 UI_EXPORT bool PropertyExists(XID window, const std::string& property_name);
158
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(
162     XID window,
163     Atom property,
164     scoped_refptr<base::RefCountedMemory>* out_data,
165     size_t* out_data_bytes,
166     size_t* out_data_items,
167     Atom* out_type);
168
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|.
171 //
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,
175                               int* value);
176 UI_EXPORT bool GetXIDProperty(XID window, const std::string& property_name,
177                               XID* value);
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);
185
186 // These setters all make round trips.
187 UI_EXPORT bool SetIntProperty(XID window,
188                               const std::string& name,
189                               const std::string& type,
190                               int value);
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);
199
200 // Gets the X atom for default display corresponding to atom_name.
201 Atom GetAtom(const char* atom_name);
202
203 // Sets the WM_CLASS attribute for a given X11 window.
204 UI_EXPORT void SetWindowClassHint(XDisplay* display,
205                                   XID window,
206                                   std::string res_name,
207                                   std::string res_class);
208
209 // Get |window|'s parent window, or None if |window| is the root window.
210 UI_EXPORT XID GetParentWindow(XID window);
211
212 // Walk up |window|'s hierarchy until we find a direct child of |root|.
213 XID GetHighestAncestorWindow(XID window, XID root);
214
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);
219
220 // Translates an X11 error code into a printable string.
221 UI_EXPORT std::string GetX11ErrorString(XDisplay* display, int err);
222
223 // Implementers of this interface receive a notification for every X window of
224 // the main display.
225 class EnumerateWindowsDelegate {
226  public:
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;
230
231  protected:
232   virtual ~EnumerateWindowsDelegate() {}
233 };
234
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,
238                                    int max_depth);
239
240 // Enumerates the top-level windows of the current display.
241 UI_EXPORT void EnumerateTopLevelWindows(ui::EnumerateWindowsDelegate* delegate);
242
243 // Returns all children windows of a given window in top-to-bottom stacking
244 // order.
245 UI_EXPORT bool GetXWindowStack(XID window, std::vector<XID>* windows);
246
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
250 // window.
251 void RestackWindow(XID window, XID sibling, bool above);
252
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);
258
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);
268
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);
272
273 void FreePicture(XDisplay* display, XID picture);
274 void FreePixmap(XDisplay* display, XID pixmap);
275
276 enum WindowManagerName {
277   WM_UNKNOWN,
278   WM_BLACKBOX,
279   WM_CHROME_OS,
280   WM_COMPIZ,
281   WM_ENLIGHTENMENT,
282   WM_ICE_WM,
283   WM_KWIN,
284   WM_METACITY,
285   WM_MUFFIN,
286   WM_MUTTER,
287   WM_OPENBOX,
288   WM_XFWM4,
289 };
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();
293
294 // Change desktop for |window| to the desktop of |destination| window.
295 UI_EXPORT bool ChangeWindowDesktop(XID window, XID destination);
296
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();
301
302 // Return true if a given window is in full-screen mode.
303 UI_EXPORT bool IsX11WindowFullScreen(XID window);
304
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 {
309  public:
310   XRefcountedMemory(unsigned char* x11_data, size_t length)
311       : x11_data_(length ? x11_data : NULL),
312         length_(length) {
313   }
314
315   // Overridden from RefCountedMemory:
316   virtual const unsigned char* front() const OVERRIDE;
317   virtual size_t size() const OVERRIDE;
318
319  private:
320   virtual ~XRefcountedMemory();
321
322   unsigned char* x11_data_;
323   size_t length_;
324
325   DISALLOW_COPY_AND_ASSIGN(XRefcountedMemory);
326 };
327
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 {
331  public:
332   explicit XScopedString(char* str) : string_(str) {}
333   ~XScopedString();
334
335   const char* string() const { return string_; }
336
337  private:
338   char* string_;
339
340   DISALLOW_COPY_AND_ASSIGN(XScopedString);
341 };
342
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 {
346  public:
347   explicit XScopedImage(XImage* image) : image_(image) {}
348   ~XScopedImage();
349
350   XImage* get() const {
351     return image_;
352   }
353
354   XImage* operator->() const {
355     return image_;
356   }
357
358   void reset(XImage* image);
359
360  private:
361   XImage* image_;
362
363   DISALLOW_COPY_AND_ASSIGN(XScopedImage);
364 };
365
366 // Keeps track of a cursor returned by an X function and makes sure it's
367 // XFreeCursor'd.
368 class UI_EXPORT XScopedCursor {
369  public:
370   // Keeps track of |cursor| created with |display|.
371   XScopedCursor(::Cursor cursor, XDisplay* display);
372   ~XScopedCursor();
373
374   ::Cursor get() const;
375   void reset(::Cursor cursor);
376
377  private:
378   ::Cursor cursor_;
379   XDisplay* display_;
380
381   DISALLOW_COPY_AND_ASSIGN(XScopedCursor);
382 };
383
384 }  // namespace ui
385
386 #endif  // UI_BASE_X_X11_UTIL_H_