Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // This file defines utility functions for X11 (Linux only). This code has been | 5 // This file defines utility functions for X11 (Linux only). This code has been |
| 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support | 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support |
| 7 // remains woefully incomplete. | 7 // remains woefully incomplete. |
| 8 | 8 |
| 9 #include "ui/base/x/x11_util.h" | 9 #include "ui/base/x/x11_util.h" |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | 28 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 29 #include "ui/base/x/x11_util_internal.h" | 29 #include "ui/base/x/x11_util_internal.h" |
| 30 #include "ui/gfx/rect.h" | 30 #include "ui/gfx/rect.h" |
| 31 #include "ui/gfx/size.h" | 31 #include "ui/gfx/size.h" |
| 32 | 32 |
| 33 #if defined(OS_FREEBSD) | 33 #if defined(OS_FREEBSD) |
| 34 #include <sys/types.h> | 34 #include <sys/types.h> |
| 35 #include <sys/sysctl.h> | 35 #include <sys/sysctl.h> |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 #if defined(USE_AURA) | |
| 39 #include <X11/Xcursor/Xcursor.h> | |
| 40 #endif | |
| 41 | |
| 38 #if defined(TOOLKIT_USES_GTK) | 42 #if defined(TOOLKIT_USES_GTK) |
| 39 #include <gdk/gdk.h> | 43 #include <gdk/gdk.h> |
| 40 #include <gdk/gdkx.h> | 44 #include <gdk/gdkx.h> |
| 41 #include <gtk/gtk.h> | 45 #include <gtk/gtk.h> |
| 42 #include "ui/base/gtk/gtk_compat.h" | 46 #include "ui/base/gtk/gtk_compat.h" |
| 43 #include "ui/base/gtk/gdk_x_compat.h" | 47 #include "ui/base/gtk/gdk_x_compat.h" |
| 44 #else | 48 #else |
| 45 // TODO(sad): Use the new way of handling X errors when | 49 // TODO(sad): Use the new way of handling X errors when |
| 46 // http://codereview.chromium.org/7889040/ lands. | 50 // http://codereview.chromium.org/7889040/ lands. |
| 47 #define gdk_error_trap_push() | 51 #define gdk_error_trap_push() |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 cache_.clear(); | 185 cache_.clear(); |
| 182 } | 186 } |
| 183 | 187 |
| 184 private: | 188 private: |
| 185 // Maps X11 font cursor shapes to Cursor IDs. | 189 // Maps X11 font cursor shapes to Cursor IDs. |
| 186 std::map<int, Cursor> cache_; | 190 std::map<int, Cursor> cache_; |
| 187 | 191 |
| 188 DISALLOW_COPY_AND_ASSIGN(XCursorCache); | 192 DISALLOW_COPY_AND_ASSIGN(XCursorCache); |
| 189 }; | 193 }; |
| 190 | 194 |
| 195 #if defined(USE_AURA) | |
| 196 // A process wide singleton cache for custom X cursors. | |
| 197 class XCustomCursorCache { | |
| 198 public: | |
| 199 static XCustomCursorCache* GetInstance() { | |
| 200 return Singleton<XCustomCursorCache>::get(); | |
| 201 } | |
| 202 | |
| 203 Cursor InstallCustomCursor(XcursorImage* image) { | |
| 204 Cursor xcursor = XcursorImageLoadCursor(GetXDisplay(), image); | |
| 205 XCustomCursor* custom_cursor = new XCustomCursor(image); | |
| 206 cache_[xcursor] = custom_cursor; | |
| 207 return xcursor; | |
| 208 } | |
| 209 | |
| 210 void Ref(Cursor cursor) { | |
| 211 cache_[cursor]->Ref(); | |
| 212 } | |
| 213 | |
| 214 void Unref(Cursor cursor) { | |
| 215 if (cache_[cursor]->Unref()) { | |
| 216 XFreeCursor(GetXDisplay(), cursor); | |
| 217 cache_.erase(cursor); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 void Clear() { | |
| 222 cache_.clear(); | |
|
Daniel Erat
2012/02/24 21:04:36
do you need to call XFreeCursor() on all of the ke
sadrul
2012/02/24 22:48:49
Good catch. This needs to happen. I have moved the
| |
| 223 } | |
| 224 | |
| 225 private: | |
| 226 friend struct DefaultSingletonTraits<XCustomCursorCache>; | |
| 227 | |
| 228 class XCustomCursor { | |
| 229 public: | |
| 230 XCustomCursor(XcursorImage* image) | |
|
Daniel Erat
2012/02/24 21:04:36
nit: document that it takes ownership of |image|
sadrul
2012/02/24 22:48:49
Done.
| |
| 231 : image_(image), | |
| 232 ref_(1) { | |
| 233 } | |
| 234 | |
| 235 ~XCustomCursor() { | |
| 236 XcursorImageDestroy(image_); | |
| 237 } | |
| 238 | |
| 239 void Ref() { | |
| 240 ++ref_; | |
| 241 } | |
| 242 | |
| 243 // Returns true if the cursor was destroyed because of the unref. | |
| 244 bool Unref() { | |
| 245 if (--ref_ == 0) { | |
| 246 delete this; | |
| 247 return true; | |
| 248 } | |
| 249 return false; | |
| 250 } | |
| 251 | |
| 252 private: | |
| 253 XcursorImage* image_; | |
| 254 int ref_; | |
| 255 | |
| 256 DISALLOW_COPY_AND_ASSIGN(XCustomCursor); | |
| 257 }; | |
| 258 | |
| 259 XCustomCursorCache() {} | |
| 260 ~XCustomCursorCache() { | |
| 261 Clear(); | |
| 262 } | |
| 263 | |
| 264 std::map<Cursor, XCustomCursor*> cache_; | |
| 265 DISALLOW_COPY_AND_ASSIGN(XCustomCursorCache); | |
| 266 }; | |
| 267 #endif // defined(USE_AURA) | |
| 268 | |
| 191 // A singleton object that remembers remappings of mouse buttons. | 269 // A singleton object that remembers remappings of mouse buttons. |
| 192 class XButtonMap { | 270 class XButtonMap { |
| 193 public: | 271 public: |
| 194 static XButtonMap* GetInstance() { | 272 static XButtonMap* GetInstance() { |
| 195 return Singleton<XButtonMap>::get(); | 273 return Singleton<XButtonMap>::get(); |
| 196 } | 274 } |
| 197 | 275 |
| 198 void UpdateMapping() { | 276 void UpdateMapping() { |
| 199 count_ = XGetPointerMapping(ui::GetXDisplay(), map_, arraysize(map_)); | 277 count_ = XGetPointerMapping(ui::GetXDisplay(), map_, arraysize(map_)); |
| 200 } | 278 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 CR_DEFINE_STATIC_LOCAL(XCursorCache, cache, ()); | 388 CR_DEFINE_STATIC_LOCAL(XCursorCache, cache, ()); |
| 311 | 389 |
| 312 if (cursor_shape == kCursorClearXCursorCache) { | 390 if (cursor_shape == kCursorClearXCursorCache) { |
| 313 cache.Clear(); | 391 cache.Clear(); |
| 314 return 0; | 392 return 0; |
| 315 } | 393 } |
| 316 | 394 |
| 317 return cache.GetCursor(cursor_shape); | 395 return cache.GetCursor(cursor_shape); |
| 318 } | 396 } |
| 319 | 397 |
| 398 #if defined(USE_AURA) | |
| 399 Cursor CreateReffedCustomXCursor(XcursorImage* image) { | |
| 400 return XCustomCursorCache::GetInstance()->InstallCustomCursor(image); | |
| 401 } | |
| 402 | |
| 403 void RefCustomXCursor(Cursor cursor) { | |
| 404 XCustomCursorCache::GetInstance()->Ref(cursor); | |
| 405 } | |
| 406 | |
| 407 void UnrefCustomXCursor(Cursor cursor) { | |
| 408 XCustomCursorCache::GetInstance()->Unref(cursor); | |
| 409 } | |
| 410 #endif | |
| 411 | |
| 320 XID GetX11RootWindow() { | 412 XID GetX11RootWindow() { |
| 321 return DefaultRootWindow(GetXDisplay()); | 413 return DefaultRootWindow(GetXDisplay()); |
| 322 } | 414 } |
| 323 | 415 |
| 324 bool GetCurrentDesktop(int* desktop) { | 416 bool GetCurrentDesktop(int* desktop) { |
| 325 return GetIntProperty(GetX11RootWindow(), "_NET_CURRENT_DESKTOP", desktop); | 417 return GetIntProperty(GetX11RootWindow(), "_NET_CURRENT_DESKTOP", desktop); |
| 326 } | 418 } |
| 327 | 419 |
| 328 #if defined(TOOLKIT_USES_GTK) | 420 #if defined(TOOLKIT_USES_GTK) |
| 329 XID GetX11WindowFromGtkWidget(GtkWidget* widget) { | 421 XID GetX11WindowFromGtkWidget(GtkWidget* widget) { |
| (...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1130 << "request_code " << static_cast<int>(error_event.request_code) << ", " | 1222 << "request_code " << static_cast<int>(error_event.request_code) << ", " |
| 1131 << "minor_code " << static_cast<int>(error_event.minor_code) | 1223 << "minor_code " << static_cast<int>(error_event.minor_code) |
| 1132 << " (" << request_str << ")"; | 1224 << " (" << request_str << ")"; |
| 1133 } | 1225 } |
| 1134 | 1226 |
| 1135 // ---------------------------------------------------------------------------- | 1227 // ---------------------------------------------------------------------------- |
| 1136 // End of x11_util_internal.h | 1228 // End of x11_util_internal.h |
| 1137 | 1229 |
| 1138 | 1230 |
| 1139 } // namespace ui | 1231 } // namespace ui |
| OLD | NEW |