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> |
8 #include <map> | 10 #include <map> |
9 #include <set> | 11 #include <set> |
10 #include <string> | 12 #include <string> |
11 #include <utility> | 13 #include <utility> |
12 | 14 |
13 #include "base/file_path.h" | 15 #include "base/file_path.h" |
14 #include "base/logging.h" | 16 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/singleton.h" |
16 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "ui/base/gtk/gtk_signal.h" |
| 21 #include "ui/base/x/x11_util.h" |
18 #include "ui/gfx/canvas_skia.h" | 22 #include "ui/gfx/canvas_skia.h" |
19 #include "ui/gfx/gtk_util.h" | 23 #include "ui/gfx/gtk_util.h" |
20 #include "ui/gfx/size.h" | 24 #include "ui/gfx/size.h" |
21 | 25 |
22 namespace ui { | 26 namespace ui { |
23 | 27 |
24 namespace { | 28 namespace { |
25 | 29 |
| 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 |
26 const char kMimeTypeBitmap[] = "image/bmp"; | 106 const char kMimeTypeBitmap[] = "image/bmp"; |
27 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; | 107 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; |
28 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; | 108 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; |
29 | 109 |
30 std::string GdkAtomToString(const GdkAtom& atom) { | 110 std::string GdkAtomToString(const GdkAtom& atom) { |
31 gchar* name = gdk_atom_name(atom); | 111 gchar* name = gdk_atom_name(atom); |
32 std::string rv(name); | 112 std::string rv(name); |
33 g_free(name); | 113 g_free(name); |
34 return rv; | 114 return rv; |
35 } | 115 } |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 void Clipboard::ReadData(const std::string& format, std::string* result) { | 496 void Clipboard::ReadData(const std::string& format, std::string* result) { |
417 GtkSelectionData* data = | 497 GtkSelectionData* data = |
418 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format)); | 498 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format)); |
419 if (!data) | 499 if (!data) |
420 return; | 500 return; |
421 result->assign(reinterpret_cast<char*>(data->data), data->length); | 501 result->assign(reinterpret_cast<char*>(data->data), data->length); |
422 gtk_selection_data_free(data); | 502 gtk_selection_data_free(data); |
423 } | 503 } |
424 | 504 |
425 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 505 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
426 // TODO(cdn): implement this. For now this interface will advertise | 506 if (buffer == BUFFER_STANDARD) |
427 // that the Linux clipboard never changes. That's fine as long as we | 507 return SelectionChangeObserver::GetInstance()->clipboard_sequence_number(); |
428 // don't rely on this signal. | 508 else |
429 return 0; | 509 return SelectionChangeObserver::GetInstance()->primary_sequence_number(); |
430 } | 510 } |
431 | 511 |
432 // static | 512 // static |
433 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 513 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { |
434 return GdkAtomToString(GDK_TARGET_STRING); | 514 return GdkAtomToString(GDK_TARGET_STRING); |
435 } | 515 } |
436 | 516 |
437 // static | 517 // static |
438 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 518 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { |
439 return GetPlainTextFormatType(); | 519 return GetPlainTextFormatType(); |
(...skipping 27 matching lines...) Expand all Loading... |
467 return clipboard_; | 547 return clipboard_; |
468 case BUFFER_SELECTION: | 548 case BUFFER_SELECTION: |
469 return primary_selection_; | 549 return primary_selection_; |
470 default: | 550 default: |
471 NOTREACHED(); | 551 NOTREACHED(); |
472 return NULL; | 552 return NULL; |
473 } | 553 } |
474 } | 554 } |
475 | 555 |
476 } // namespace ui | 556 } // namespace ui |
OLD | NEW |