OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Don't include this file in any .h files because it pulls in some X headers. |
| 6 |
| 7 #ifndef REMOTING_HOST_X_SERVER_CLIPBOARD_H_ |
| 8 #define REMOTING_HOST_X_SERVER_CLIPBOARD_H_ |
| 9 |
| 10 #include <X11/Xatom.h> |
| 11 #include <X11/Xlib.h> |
| 12 |
| 13 #include <map> |
| 14 #include <set> |
| 15 #include <string> |
| 16 |
| 17 #include "base/basictypes.h" |
| 18 #include "base/callback_forward.h" |
| 19 #include "base/timer.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 // A class to allow manipulation of the X clipboard, using low-level X |
| 24 // API calls. |
| 25 class XServerClipboard { |
| 26 public: |
| 27 typedef base::Callback<void(const std::string& mime_type, |
| 28 const std::string& data)> |
| 29 ClipboardChangedCallback; |
| 30 |
| 31 XServerClipboard(); |
| 32 ~XServerClipboard(); |
| 33 |
| 34 // The caller should ensure |display| remains valid as long as any other |
| 35 // methods are called on this object. |
| 36 void Init(Display* display, const ClipboardChangedCallback& callback); |
| 37 |
| 38 void SetClipboard(const std::string& mime_type, const std::string& data); |
| 39 |
| 40 // This method allows this object to be hooked up to the program's main event |
| 41 // loop. It should be called when the event loop receives an X event, and |
| 42 // all methods of this object should be called on this thread. |
| 43 void ProcessXEvent(XEvent* event); |
| 44 |
| 45 private: |
| 46 // This checks the state of the X selections. If there is new text selected |
| 47 // by another application, this will eventually trigger the registered |
| 48 // ClipboardChangedCallback. Of the two selections (PRIMARY and CLIPBOARD), |
| 49 // the PRIMARY selection is tried first, and the CLIPBOARD selection is only |
| 50 // used if it is newer than PRIMARY. If there is no new text since the |
| 51 // previous call, then no callback will be made. Duplicate text is filtered |
| 52 // out, so that the same text being selected again will not trigger the |
| 53 // callback. |
| 54 // The Init() method calls this to get the initial state of the X selections; |
| 55 // no callback will be triggered in this case. |
| 56 // If |selection| is CLIPBOARD or PRIMARY, it is checked. If |selection| is |
| 57 // None, both CLIPBOARD and PRIMARY are checked. Other values of |selection| |
| 58 // are ignored. |
| 59 // If |timestamp| is non-zero, it is recorded as the "current selection time", |
| 60 // and is treated as new. |
| 61 void GetSelections(Atom selection, Time timestamp); |
| 62 |
| 63 void FinishGetSelections(); |
| 64 |
| 65 void OnPropertyNotify(XEvent* event); |
| 66 void OnSelectionNotify(XEvent* event); |
| 67 void OnSelectionRequest(XEvent* event); |
| 68 |
| 69 // This is called when the selection owner has replied to a request for |
| 70 // information about a selection. |
| 71 void DoSelectionNotify(XSelectionEvent* event, |
| 72 Atom type, |
| 73 int format, |
| 74 int item_count, |
| 75 void* data); |
| 76 |
| 77 bool GotSelectionTimestamp(XSelectionEvent* event, |
| 78 int format, |
| 79 int item_count, |
| 80 void* data); |
| 81 bool GotSelectionTargets(XSelectionEvent* event, |
| 82 int format, |
| 83 int item_count, |
| 84 void* data); |
| 85 bool GotSelectionString(XSelectionEvent* event, |
| 86 int format, |
| 87 int item_count, |
| 88 void* data); |
| 89 void GotCutTextUtf8(const std::string& text); |
| 90 bool IsSelectionOwner(Atom selection); |
| 91 void GetSelectionTimestamp(Atom selection, Time time); |
| 92 void GetSelectionTargets(Atom selection); |
| 93 void GetSelectionString(Atom selection, Atom target); |
| 94 |
| 95 // Request that the window owns the given selection from the given time (the |
| 96 // time should be taken from an X event). |
| 97 void OwnSelection(Atom selection, Time time); |
| 98 |
| 99 Display* display_; |
| 100 Window clipboard_window_; |
| 101 bool have_xfixes_; |
| 102 int xfixes_event_base_; |
| 103 int xfixes_error_base_; |
| 104 Atom clipboard_atom_; |
| 105 Atom large_selection_atom_; |
| 106 Atom selection_string_atom_; |
| 107 Atom targets_atom_; |
| 108 Atom timestamp_atom_; |
| 109 Atom utf8_string_atom_; |
| 110 std::set<Atom> selections_owned_; |
| 111 std::map<Atom, Time> selection_own_time_; |
| 112 std::string data_; |
| 113 Atom large_selection_property_; |
| 114 Time current_selection_time_; |
| 115 Atom new_selection_; |
| 116 bool getting_initial_selection_; |
| 117 bool new_cut_text_; |
| 118 bool had_valid_timestamp_; |
| 119 base::TimeTicks get_selections_time_; |
| 120 ClipboardChangedCallback callback_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(XServerClipboard); |
| 123 }; |
| 124 |
| 125 } // namespace remoting |
| 126 |
| 127 #endif // REMOTING_HOST_X_SERVER_CLIPBOARD_H_ |
OLD | NEW |