Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: ui/aura/root_window_host_linux.cc

Issue 10316019: Aura: Adds custom cursors for drag and drop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/aura/root_window_host_linux.h" 5 #include "ui/aura/root_window_host_linux.h"
6 6
7 #include <X11/Xatom.h> 7 #include <X11/Xatom.h>
8 #include <X11/Xcursor/Xcursor.h>
8 #include <X11/cursorfont.h> 9 #include <X11/cursorfont.h>
9 #include <X11/extensions/XInput2.h> 10 #include <X11/extensions/XInput2.h>
10 #include <X11/extensions/Xfixes.h> 11 #include <X11/extensions/Xfixes.h>
11 #include <X11/extensions/Xrandr.h> 12 #include <X11/extensions/Xrandr.h>
12 #include <algorithm> 13 #include <algorithm>
13 14
14 #include "base/message_pump_x.h" 15 #include "base/message_pump_x.h"
15 #include "base/stl_util.h" 16 #include "base/stl_util.h"
16 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "grit/ui_resources_standard.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/aura/client/user_gesture_client.h" 20 #include "ui/aura/client/user_gesture_client.h"
18 #include "ui/aura/dispatcher_linux.h" 21 #include "ui/aura/dispatcher_linux.h"
19 #include "ui/aura/env.h" 22 #include "ui/aura/env.h"
20 #include "ui/aura/event.h" 23 #include "ui/aura/event.h"
21 #include "ui/aura/root_window.h" 24 #include "ui/aura/root_window.h"
22 #include "ui/base/cursor/cursor.h" 25 #include "ui/base/cursor/cursor.h"
23 #include "ui/base/keycodes/keyboard_codes.h" 26 #include "ui/base/keycodes/keyboard_codes.h"
27 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/touch/touch_factory.h" 28 #include "ui/base/touch/touch_factory.h"
25 #include "ui/base/x/x11_util.h" 29 #include "ui/base/x/x11_util.h"
26 #include "ui/base/view_prop.h" 30 #include "ui/base/view_prop.h"
27 #include "ui/gfx/compositor/layer.h" 31 #include "ui/gfx/compositor/layer.h"
32 #include "ui/gfx/image/image.h"
28 33
29 using std::max; 34 using std::max;
30 using std::min; 35 using std::min;
31 36
32 namespace aura { 37 namespace aura {
33 38
34 namespace { 39 namespace {
35 40
36 // Standard Linux mouse buttons for going back and forward. 41 // Standard Linux mouse buttons for going back and forward.
37 const int kBackMouseButton = 8; 42 const int kBackMouseButton = 8;
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 // X11 server. Must be kept in sync with RootWindowHostLinux::AtomList 290 // X11 server. Must be kept in sync with RootWindowHostLinux::AtomList
286 const char* kAtomList[] = { 291 const char* kAtomList[] = {
287 "WM_DELETE_WINDOW", 292 "WM_DELETE_WINDOW",
288 "_NET_WM_PING", 293 "_NET_WM_PING",
289 "_NET_WM_PID", 294 "_NET_WM_PID",
290 "WM_S0" 295 "WM_S0"
291 }; 296 };
292 297
293 } // namespace 298 } // namespace
294 299
300 // A utility class that provides X Cursor for NativeCursors for which we have
301 // image resources.
302 class RootWindowHostLinux::ImageCursors {
303 public:
304 ImageCursors() {
305 LoadImageCursor(ui::kCursorNoDrop, IDR_AURA_CURSOR_NO_DROP);
306 LoadImageCursor(ui::kCursorCopy, IDR_AURA_CURSOR_COPY);
307 // TODO (varunjain): add more cursors once we have assets.
308 }
309
310 ~ImageCursors() {
311 std::map<int, Cursor>::const_iterator it;
312 for (it = cursors_.begin(); it != cursors_.end(); ++it)
313 ui::UnrefCustomXCursor(it->second);
sail 2012/05/03 20:00:47 I was thinking that you could just use XCustomCurs
314 }
315
316 // Returns true if we have an image resource loaded for the |native_cursor|.
317 bool IsImageCursor(gfx::NativeCursor native_cursor) {
318 return cursors_.find(native_cursor.native_type()) != cursors_.end();
319 }
320
321 // Gets the X Cursor corresponding to the |native_cursor|.
322 ::Cursor ImageCursorFromNative(gfx::NativeCursor native_cursor) {
323 DCHECK(cursors_.find(native_cursor.native_type()) != cursors_.end());
324 return cursors_[native_cursor.native_type()];
325 }
326
327 private:
328 // Creates an X Cursor from an image resource and puts it in the cursor map.
329 void LoadImageCursor(int id, int resource_id) {
330 const SkBitmap* bitmap =
331 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
332 resource_id).ToSkBitmap();
333
334 XcursorImage* image = ui::SkBitmapToXcursorImage(bitmap, gfx::Point(0, 0));
335 cursors_[id] = ui::CreateReffedCustomXCursor(image);
336 // |bitmap| is owned by the resource bundle. So we do not need to free it.
337 }
338
339 // A map to hold all image cursors. It maps the cursor ID to the cursor
340 // object, where, the cursor object consists of the actual X Cursor and the
341 // XcursorImage. We need to keep the images as well to be able to free them
342 // on destruction.
343 std::map<int, Cursor> cursors_;
344
345 DISALLOW_COPY_AND_ASSIGN(ImageCursors);
346 };
347
295 RootWindowHostLinux::RootWindowHostLinux(const gfx::Rect& bounds) 348 RootWindowHostLinux::RootWindowHostLinux(const gfx::Rect& bounds)
296 : root_window_(NULL), 349 : root_window_(NULL),
297 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), 350 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()),
298 xwindow_(0), 351 xwindow_(0),
299 x_root_window_(DefaultRootWindow(xdisplay_)), 352 x_root_window_(DefaultRootWindow(xdisplay_)),
300 current_cursor_(ui::kCursorNull), 353 current_cursor_(ui::kCursorNull),
301 cursor_shown_(true), 354 cursor_shown_(true),
302 bounds_(bounds), 355 bounds_(bounds),
303 focus_when_shown_(false), 356 focus_when_shown_(false),
304 pointer_barriers_(NULL) { 357 pointer_barriers_(NULL),
358 image_cursors_(new ImageCursors) {
305 XSetWindowAttributes swa; 359 XSetWindowAttributes swa;
306 memset(&swa, 0, sizeof(swa)); 360 memset(&swa, 0, sizeof(swa));
307 swa.background_pixmap = None; 361 swa.background_pixmap = None;
308 xwindow_ = XCreateWindow( 362 xwindow_ = XCreateWindow(
309 xdisplay_, x_root_window_, 363 xdisplay_, x_root_window_,
310 bounds.x(), bounds.y(), bounds.width(), bounds.height(), 364 bounds.x(), bounds.y(), bounds.width(), bounds.height(),
311 0, // border width 365 0, // border width
312 CopyFromParent, // depth 366 CopyFromParent, // depth
313 InputOutput, 367 InputOutput,
314 CopyFromParent, // visual 368 CopyFromParent, // visual
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 } 842 }
789 843
790 bool RootWindowHostLinux::IsWindowManagerPresent() { 844 bool RootWindowHostLinux::IsWindowManagerPresent() {
791 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership 845 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership
792 // of WM_Sn selections (where n is a screen number). 846 // of WM_Sn selections (where n is a screen number).
793 return XGetSelectionOwner(xdisplay_, cached_atoms_[ATOM_WM_S0]) != None; 847 return XGetSelectionOwner(xdisplay_, cached_atoms_[ATOM_WM_S0]) != None;
794 } 848 }
795 849
796 void RootWindowHostLinux::SetCursorInternal(gfx::NativeCursor cursor) { 850 void RootWindowHostLinux::SetCursorInternal(gfx::NativeCursor cursor) {
797 ::Cursor xcursor = 851 ::Cursor xcursor =
852 image_cursors_->IsImageCursor(cursor) ?
853 image_cursors_->ImageCursorFromNative(cursor) :
798 cursor == ui::kCursorNone ? 854 cursor == ui::kCursorNone ?
799 invisible_cursor_ : 855 invisible_cursor_ :
800 cursor == ui::kCursorCustom ? 856 cursor == ui::kCursorCustom ?
801 cursor.platform() : 857 cursor.platform() :
802 ui::GetXCursor(CursorShapeFromNative(cursor)); 858 ui::GetXCursor(CursorShapeFromNative(cursor));
803 XDefineCursor(xdisplay_, xwindow_, xcursor); 859 XDefineCursor(xdisplay_, xwindow_, xcursor);
804 } 860 }
805 861
806 // static 862 // static
807 RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) { 863 RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) {
808 return new RootWindowHostLinux(bounds); 864 return new RootWindowHostLinux(bounds);
809 } 865 }
810 866
811 // static 867 // static
812 RootWindowHost* RootWindowHost::GetForAcceleratedWidget( 868 RootWindowHost* RootWindowHost::GetForAcceleratedWidget(
813 gfx::AcceleratedWidget accelerated_widget) { 869 gfx::AcceleratedWidget accelerated_widget) {
814 return reinterpret_cast<RootWindowHost*>( 870 return reinterpret_cast<RootWindowHost*>(
815 ui::ViewProp::GetValue(accelerated_widget, kRootWindowHostLinuxKey)); 871 ui::ViewProp::GetValue(accelerated_widget, kRootWindowHostLinuxKey));
816 } 872 }
817 873
818 // static 874 // static
819 gfx::Size RootWindowHost::GetNativeScreenSize() { 875 gfx::Size RootWindowHost::GetNativeScreenSize() {
820 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); 876 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay();
821 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); 877 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0));
822 } 878 }
823 879
824 } // namespace aura 880 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698