| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/clipboard/clipboard.h" | 5 #include "ui/base/clipboard/clipboard.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 #include <X11/extensions/Xfixes.h> | |
| 9 #include <X11/Xatom.h> | |
| 10 #include <map> | 8 #include <map> |
| 11 #include <set> | 9 #include <set> |
| 12 #include <string> | 10 #include <string> |
| 13 #include <utility> | 11 #include <utility> |
| 14 | 12 |
| 15 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 16 #include "base/logging.h" | 14 #include "base/logging.h" |
| 17 #include "base/memory/singleton.h" | 15 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "ui/base/gtk/gtk_signal.h" | |
| 21 #include "ui/base/x/x11_util.h" | |
| 22 #include "ui/gfx/canvas_skia.h" | 18 #include "ui/gfx/canvas_skia.h" |
| 23 #include "ui/gfx/gtk_util.h" | 19 #include "ui/gfx/gtk_util.h" |
| 24 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
| 25 | 21 |
| 26 namespace ui { | 22 namespace ui { |
| 27 | 23 |
| 28 namespace { | 24 namespace { |
| 29 | 25 |
| 30 class SelectionChangeObserver { | |
| 31 public: | |
| 32 static SelectionChangeObserver* GetInstance(); | |
| 33 | |
| 34 uint64 clipboard_sequence_number() const { | |
| 35 return clipboard_sequence_number_; | |
| 36 } | |
| 37 uint64 primary_sequence_number() const { return primary_sequence_number_; } | |
| 38 | |
| 39 private: | |
| 40 friend struct DefaultSingletonTraits<SelectionChangeObserver>; | |
| 41 | |
| 42 SelectionChangeObserver(); | |
| 43 ~SelectionChangeObserver(); | |
| 44 | |
| 45 CHROMEG_CALLBACK_1(SelectionChangeObserver, GdkFilterReturn, OnXEvent, | |
| 46 GdkXEvent*, GdkEvent*); | |
| 47 | |
| 48 int event_base_; | |
| 49 Atom clipboard_atom_; | |
| 50 uint64 clipboard_sequence_number_; | |
| 51 uint64 primary_sequence_number_; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(SelectionChangeObserver); | |
| 54 }; | |
| 55 | |
| 56 SelectionChangeObserver::SelectionChangeObserver() | |
| 57 : event_base_(-1), | |
| 58 clipboard_atom_(None), | |
| 59 clipboard_sequence_number_(0), | |
| 60 primary_sequence_number_(0) { | |
| 61 int ignored; | |
| 62 if (XFixesQueryExtension(GetXDisplay(), &event_base_, &ignored)) { | |
| 63 clipboard_atom_ = XInternAtom(GetXDisplay(), "CLIPBOARD", false); | |
| 64 XFixesSelectSelectionInput(GetXDisplay(), GetX11RootWindow(), | |
| 65 clipboard_atom_, | |
| 66 XFixesSetSelectionOwnerNotifyMask | | |
| 67 XFixesSelectionWindowDestroyNotifyMask | | |
| 68 XFixesSelectionClientCloseNotifyMask); | |
| 69 // This seems to be semi-optional. For some reason, registering for any | |
| 70 // selection notify events seems to subscribe us to events for both the | |
| 71 // primary and the clipboard buffers. Register anyway just to be safe. | |
| 72 XFixesSelectSelectionInput(GetXDisplay(), GetX11RootWindow(), | |
| 73 XA_PRIMARY, | |
| 74 XFixesSetSelectionOwnerNotifyMask | | |
| 75 XFixesSelectionWindowDestroyNotifyMask | | |
| 76 XFixesSelectionClientCloseNotifyMask); | |
| 77 gdk_window_add_filter(NULL, &SelectionChangeObserver::OnXEventThunk, this); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 SelectionChangeObserver::~SelectionChangeObserver() { | |
| 82 } | |
| 83 | |
| 84 SelectionChangeObserver* SelectionChangeObserver::GetInstance() { | |
| 85 return Singleton<SelectionChangeObserver>::get(); | |
| 86 } | |
| 87 | |
| 88 GdkFilterReturn SelectionChangeObserver::OnXEvent(GdkXEvent* xevent, | |
| 89 GdkEvent* event) { | |
| 90 XEvent* xev = static_cast<XEvent*>(xevent); | |
| 91 | |
| 92 if (xev->type == event_base_ + XFixesSelectionNotify) { | |
| 93 XFixesSelectionNotifyEvent* ev = | |
| 94 reinterpret_cast<XFixesSelectionNotifyEvent*>(xev); | |
| 95 if (ev->selection == clipboard_atom_) { | |
| 96 clipboard_sequence_number_++; | |
| 97 } else if (ev->selection == XA_PRIMARY) { | |
| 98 primary_sequence_number_++; | |
| 99 } else { | |
| 100 DLOG(ERROR) << "Unexpected selection atom: " << ev->selection; | |
| 101 } | |
| 102 } | |
| 103 return GDK_FILTER_CONTINUE; | |
| 104 } | |
| 105 | |
| 106 const char kMimeTypeBitmap[] = "image/bmp"; | 26 const char kMimeTypeBitmap[] = "image/bmp"; |
| 107 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; | 27 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; |
| 108 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; | 28 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; |
| 109 | 29 |
| 110 std::string GdkAtomToString(const GdkAtom& atom) { | 30 std::string GdkAtomToString(const GdkAtom& atom) { |
| 111 gchar* name = gdk_atom_name(atom); | 31 gchar* name = gdk_atom_name(atom); |
| 112 std::string rv(name); | 32 std::string rv(name); |
| 113 g_free(name); | 33 g_free(name); |
| 114 return rv; | 34 return rv; |
| 115 } | 35 } |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 void Clipboard::ReadData(const std::string& format, std::string* result) { | 416 void Clipboard::ReadData(const std::string& format, std::string* result) { |
| 497 GtkSelectionData* data = | 417 GtkSelectionData* data = |
| 498 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format)); | 418 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format)); |
| 499 if (!data) | 419 if (!data) |
| 500 return; | 420 return; |
| 501 result->assign(reinterpret_cast<char*>(data->data), data->length); | 421 result->assign(reinterpret_cast<char*>(data->data), data->length); |
| 502 gtk_selection_data_free(data); | 422 gtk_selection_data_free(data); |
| 503 } | 423 } |
| 504 | 424 |
| 505 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 425 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
| 506 if (buffer == BUFFER_STANDARD) | 426 // TODO(cdn): implement this. For now this interface will advertise |
| 507 return SelectionChangeObserver::GetInstance()->clipboard_sequence_number(); | 427 // that the Linux clipboard never changes. That's fine as long as we |
| 508 else | 428 // don't rely on this signal. |
| 509 return SelectionChangeObserver::GetInstance()->primary_sequence_number(); | 429 return 0; |
| 510 } | 430 } |
| 511 | 431 |
| 512 // static | 432 // static |
| 513 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 433 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { |
| 514 return GdkAtomToString(GDK_TARGET_STRING); | 434 return GdkAtomToString(GDK_TARGET_STRING); |
| 515 } | 435 } |
| 516 | 436 |
| 517 // static | 437 // static |
| 518 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 438 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { |
| 519 return GetPlainTextFormatType(); | 439 return GetPlainTextFormatType(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 547 return clipboard_; | 467 return clipboard_; |
| 548 case BUFFER_SELECTION: | 468 case BUFFER_SELECTION: |
| 549 return primary_selection_; | 469 return primary_selection_; |
| 550 default: | 470 default: |
| 551 NOTREACHED(); | 471 NOTREACHED(); |
| 552 return NULL; | 472 return NULL; |
| 553 } | 473 } |
| 554 } | 474 } |
| 555 | 475 |
| 556 } // namespace ui | 476 } // namespace ui |
| OLD | NEW |