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

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"
20 #include "third_party/skia/include/core/SkUnPreMultiply.h"
17 #include "ui/aura/client/user_gesture_client.h" 21 #include "ui/aura/client/user_gesture_client.h"
18 #include "ui/aura/dispatcher_linux.h" 22 #include "ui/aura/dispatcher_linux.h"
19 #include "ui/aura/env.h" 23 #include "ui/aura/env.h"
20 #include "ui/aura/event.h" 24 #include "ui/aura/event.h"
21 #include "ui/aura/root_window.h" 25 #include "ui/aura/root_window.h"
22 #include "ui/base/cursor/cursor.h" 26 #include "ui/base/cursor/cursor.h"
23 #include "ui/base/keycodes/keyboard_codes.h" 27 #include "ui/base/keycodes/keyboard_codes.h"
28 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/touch/touch_factory.h" 29 #include "ui/base/touch/touch_factory.h"
25 #include "ui/base/x/x11_util.h" 30 #include "ui/base/x/x11_util.h"
26 #include "ui/base/view_prop.h" 31 #include "ui/base/view_prop.h"
27 #include "ui/gfx/compositor/layer.h" 32 #include "ui/gfx/compositor/layer.h"
33 #include "ui/gfx/image/image.h"
28 34
29 using std::max; 35 using std::max;
30 using std::min; 36 using std::min;
31 37
32 namespace aura { 38 namespace aura {
33 39
34 namespace { 40 namespace {
35 41
36 // Standard Linux mouse buttons for going back and forward. 42 // Standard Linux mouse buttons for going back and forward.
37 const int kBackMouseButton = 8; 43 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 291 // X11 server. Must be kept in sync with RootWindowHostLinux::AtomList
286 const char* kAtomList[] = { 292 const char* kAtomList[] = {
287 "WM_DELETE_WINDOW", 293 "WM_DELETE_WINDOW",
288 "_NET_WM_PING", 294 "_NET_WM_PING",
289 "_NET_WM_PID", 295 "_NET_WM_PID",
290 "WM_S0" 296 "WM_S0"
291 }; 297 };
292 298
293 } // namespace 299 } // namespace
294 300
301 // A utility class that provides X Cursor for NativeCursors for which we have
302 // image resources.
303 class RootWindowHostLinux::ImageCursors {
304 public:
305 explicit ImageCursors(Display* xdisplay)
306 : xdisplay_(xdisplay) {
307 LoadImageCursor(ui::kCursorNoDrop, IDR_AURA_CURSOR_NO_DROP);
308 LoadImageCursor(ui::kCursorCopy, IDR_AURA_CURSOR_COPY);
309 // TODO (varunjain): add more cursors once we have assets.
310 }
311
312 virtual ~ImageCursors() {
sky 2012/05/03 19:12:52 no virtual.
varunjain 2012/05/03 19:48:54 Done.
313 std::map<int, std::pair<Cursor, XcursorImage*> >::const_iterator it;
314 for (it = cursors_.begin(); it != cursors_.end(); it++) {
sky 2012/05/03 19:12:52 ++it
varunjain 2012/05/03 19:48:54 Done.
315 XFreeCursor(xdisplay_, it->second.first);
sail 2012/05/03 18:11:05 instead of doing your own reference counting can y
varunjain 2012/05/03 19:48:54 Done with ui::CreateReffedXCursor()
316 XcursorImageDestroy(it->second.second);
317 }
318 }
319
320 // Returns true if we have an image resource loaded for the |native_cursor|.
321 bool IsImageCursor(gfx::NativeCursor native_cursor) {
322 return cursors_.find(native_cursor.native_type()) != cursors_.end();
323 }
324
325 // Gets the X Cursor corresponding to the |native_cursor|.
326 ::Cursor ImageCursorFromNative(gfx::NativeCursor native_cursor) {
327 DCHECK(cursors_.find(native_cursor.native_type()) != cursors_.end());
328 return cursors_[native_cursor.native_type()].first;
329 }
330
331 private:
332 // Creates an X Cursor from an image resource and puts it in the cursor map.
333 void LoadImageCursor(int id, int resource_id) {
334 const SkBitmap* bitmap =
335 ui::ResourceBundle::GetSharedInstance().GetBitmapNamed(resource_id);
sky 2012/05/03 19:12:52 GetBitmapNamed is deprecated, and as far as I know
varunjain 2012/05/03 19:48:54 Done.
336 if (bitmap->isNull())
337 return;
338
339 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
340 XcursorImage* image = XcursorImageCreate(bitmap->width(), bitmap->height());
341 image->xhot = 0;
342 image->yhot = 0;
343 uint32* pixels = image->pixels;
344
345 bitmap->lockPixels();
346 if (bitmap->width() && bitmap->height()) {
347 int height = bitmap->height(), width = bitmap->width();
sail 2012/05/03 18:11:05 width,height should be on separate lines
varunjain 2012/05/03 19:48:54 Done.
348 for (int y = 0, i = 0; y < height; y++) {
349 for (int x = 0; x < width; x++) {
350 uint32 pixel = bitmap->getAddr32(0, y)[x];
sail 2012/05/03 18:11:05 this code seems to duplicate WebCursor::GetPlatfor
varunjain 2012/05/03 19:48:54 Done.
351 int alpha = SkColorGetA(pixel);
352 if (alpha != 0 && alpha != 255)
353 pixels[i] = SkUnPreMultiply::PMColorToColor(pixel);
sky 2012/05/03 19:12:52 Don't we have code like this some where already?
varunjain 2012/05/03 19:48:54 Done.
354 else
355 pixels[i] = pixel;
356 ++i;
357 }
358 }
359 }
360 bitmap->unlockPixels();
361
362 cursors_[id] = std::make_pair(
363 XcursorImageLoadCursor(xdisplay_, image), image);
364
365 // |bitmap| is owned by the resource bundle. So we do not need to free it.
366 }
367
368 // A map to hold all image cursors. It maps the cursor ID to the cursor
369 // object, where, the cursor object consists of the actual X Cursor and the
370 // XcursorImage. We need to keep the images as well to be able to free them
371 // on destruction.
372 std::map<int, std::pair<Cursor, XcursorImage*> > cursors_;
373
374 Display* xdisplay_;
375
376 DISALLOW_COPY_AND_ASSIGN(ImageCursors);
377 };
378
295 RootWindowHostLinux::RootWindowHostLinux(const gfx::Rect& bounds) 379 RootWindowHostLinux::RootWindowHostLinux(const gfx::Rect& bounds)
296 : root_window_(NULL), 380 : root_window_(NULL),
297 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), 381 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()),
298 xwindow_(0), 382 xwindow_(0),
299 x_root_window_(DefaultRootWindow(xdisplay_)), 383 x_root_window_(DefaultRootWindow(xdisplay_)),
300 current_cursor_(ui::kCursorNull), 384 current_cursor_(ui::kCursorNull),
301 cursor_shown_(true), 385 cursor_shown_(true),
302 bounds_(bounds), 386 bounds_(bounds),
303 focus_when_shown_(false), 387 focus_when_shown_(false),
304 pointer_barriers_(NULL) { 388 pointer_barriers_(NULL),
389 image_cursors_(new ImageCursors(xdisplay_)) {
305 XSetWindowAttributes swa; 390 XSetWindowAttributes swa;
306 memset(&swa, 0, sizeof(swa)); 391 memset(&swa, 0, sizeof(swa));
307 swa.background_pixmap = None; 392 swa.background_pixmap = None;
308 xwindow_ = XCreateWindow( 393 xwindow_ = XCreateWindow(
309 xdisplay_, x_root_window_, 394 xdisplay_, x_root_window_,
310 bounds.x(), bounds.y(), bounds.width(), bounds.height(), 395 bounds.x(), bounds.y(), bounds.width(), bounds.height(),
311 0, // border width 396 0, // border width
312 CopyFromParent, // depth 397 CopyFromParent, // depth
313 InputOutput, 398 InputOutput,
314 CopyFromParent, // visual 399 CopyFromParent, // visual
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 } 873 }
789 874
790 bool RootWindowHostLinux::IsWindowManagerPresent() { 875 bool RootWindowHostLinux::IsWindowManagerPresent() {
791 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership 876 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership
792 // of WM_Sn selections (where n is a screen number). 877 // of WM_Sn selections (where n is a screen number).
793 return XGetSelectionOwner(xdisplay_, cached_atoms_[ATOM_WM_S0]) != None; 878 return XGetSelectionOwner(xdisplay_, cached_atoms_[ATOM_WM_S0]) != None;
794 } 879 }
795 880
796 void RootWindowHostLinux::SetCursorInternal(gfx::NativeCursor cursor) { 881 void RootWindowHostLinux::SetCursorInternal(gfx::NativeCursor cursor) {
797 ::Cursor xcursor = 882 ::Cursor xcursor =
883 image_cursors_->IsImageCursor(cursor) ?
884 image_cursors_->ImageCursorFromNative(cursor) :
798 cursor == ui::kCursorNone ? 885 cursor == ui::kCursorNone ?
799 invisible_cursor_ : 886 invisible_cursor_ :
800 cursor == ui::kCursorCustom ? 887 cursor == ui::kCursorCustom ?
801 cursor.platform() : 888 cursor.platform() :
802 ui::GetXCursor(CursorShapeFromNative(cursor)); 889 ui::GetXCursor(CursorShapeFromNative(cursor));
803 XDefineCursor(xdisplay_, xwindow_, xcursor); 890 XDefineCursor(xdisplay_, xwindow_, xcursor);
804 } 891 }
805 892
806 // static 893 // static
807 RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) { 894 RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) {
808 return new RootWindowHostLinux(bounds); 895 return new RootWindowHostLinux(bounds);
809 } 896 }
810 897
811 // static 898 // static
812 RootWindowHost* RootWindowHost::GetForAcceleratedWidget( 899 RootWindowHost* RootWindowHost::GetForAcceleratedWidget(
813 gfx::AcceleratedWidget accelerated_widget) { 900 gfx::AcceleratedWidget accelerated_widget) {
814 return reinterpret_cast<RootWindowHost*>( 901 return reinterpret_cast<RootWindowHost*>(
815 ui::ViewProp::GetValue(accelerated_widget, kRootWindowHostLinuxKey)); 902 ui::ViewProp::GetValue(accelerated_widget, kRootWindowHostLinuxKey));
816 } 903 }
817 904
818 // static 905 // static
819 gfx::Size RootWindowHost::GetNativeScreenSize() { 906 gfx::Size RootWindowHost::GetNativeScreenSize() {
820 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); 907 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay();
821 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); 908 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0));
822 } 909 }
823 910
824 } // namespace aura 911 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698