[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 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 | #include "remoting/host/clipboard.h" |
| 6 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 7 | #include <X11/Xlib.h> |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 8 | |
| 9 | #include "base/memory/ptr_util.h" |
pkasting | dea81dc3 | 2015-08-17 20:30:57 | [diff] [blame] | 10 | #undef Status // Xlib.h #defines this, which breaks protobuf headers. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 11 | |
| 12 | #include "base/bind.h" |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | f368eeb | 2013-07-17 23:53:32 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 16 | #include "remoting/host/linux/x_server_clipboard.h" |
| 17 | #include "remoting/proto/event.pb.h" |
| 18 | #include "remoting/protocol/clipboard_stub.h" |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 19 | |
| 20 | namespace remoting { |
| 21 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 22 | // This code is expected to be called on the desktop thread only. |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 23 | class ClipboardX11 : public Clipboard, |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 24 | public base::MessageLoopForIO::Watcher { |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 25 | public: |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 26 | ClipboardX11(); |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 27 | ~ClipboardX11() override; |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 28 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 29 | // Clipboard interface. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 30 | void Start( |
| 31 | std::unique_ptr<protocol::ClipboardStub> client_clipboard) override; |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 32 | void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 33 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 34 | // MessageLoopForIO::Watcher interface. |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 35 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 36 | void OnFileCanWriteWithoutBlocking(int fd) override; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 37 | |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 38 | private: |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 39 | void OnClipboardChanged(const std::string& mime_type, |
| 40 | const std::string& data); |
| 41 | void PumpXEvents(); |
| 42 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 43 | std::unique_ptr<protocol::ClipboardStub> client_clipboard_; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 44 | |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 45 | // Underlying X11 clipboard implementation. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 46 | XServerClipboard x_server_clipboard_; |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 47 | |
| 48 | // Connection to the X server, used by |x_server_clipboard_|. This is created |
| 49 | // and owned by this class. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 50 | Display* display_; |
| 51 | |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 52 | // Watcher used to handle X11 events from |display_|. |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 53 | base::MessageLoopForIO::FileDescriptorWatcher x_connection_watcher_; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 54 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 55 | DISALLOW_COPY_AND_ASSIGN(ClipboardX11); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 56 | }; |
| 57 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 58 | ClipboardX11::ClipboardX11() |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 59 | : display_(nullptr) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 62 | ClipboardX11::~ClipboardX11() { |
sergeyu | 45f92d83 | 2015-02-19 01:24:19 | [diff] [blame] | 63 | if (display_) |
| 64 | XCloseDisplay(display_); |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 67 | void ClipboardX11::Start( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 68 | std::unique_ptr<protocol::ClipboardStub> client_clipboard) { |
[email protected] | b0b72f11 | 2013-03-24 03:42:42 | [diff] [blame] | 69 | // TODO(lambroslambrou): Share the X connection with InputInjector. |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 70 | display_ = XOpenDisplay(nullptr); |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 71 | if (!display_) { |
| 72 | LOG(ERROR) << "Couldn't open X display"; |
| 73 | return; |
| 74 | } |
| 75 | client_clipboard_.swap(client_clipboard); |
| 76 | |
| 77 | x_server_clipboard_.Init(display_, |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 78 | base::Bind(&ClipboardX11::OnClipboardChanged, |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 79 | base::Unretained(this))); |
| 80 | |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 81 | base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 82 | ConnectionNumber(display_), |
| 83 | true, |
| 84 | base::MessageLoopForIO::WATCH_READ, |
| 85 | &x_connection_watcher_, |
| 86 | this); |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 87 | PumpXEvents(); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 90 | void ClipboardX11::InjectClipboardEvent( |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 91 | const protocol::ClipboardEvent& event) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 92 | x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 95 | void ClipboardX11::OnFileCanReadWithoutBlocking(int fd) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 96 | PumpXEvents(); |
| 97 | } |
| 98 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 99 | void ClipboardX11::OnFileCanWriteWithoutBlocking(int fd) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 102 | void ClipboardX11::OnClipboardChanged(const std::string& mime_type, |
| 103 | const std::string& data) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 104 | protocol::ClipboardEvent event; |
| 105 | event.set_mime_type(mime_type); |
| 106 | event.set_data(data); |
| 107 | |
| 108 | if (client_clipboard_.get()) { |
| 109 | client_clipboard_->InjectClipboardEvent(event); |
| 110 | } |
| 111 | } |
| 112 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 113 | void ClipboardX11::PumpXEvents() { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 114 | DCHECK(display_); |
| 115 | |
| 116 | while (XPending(display_)) { |
| 117 | XEvent event; |
| 118 | XNextEvent(display_, &event); |
| 119 | x_server_clipboard_.ProcessXEvent(&event); |
| 120 | } |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 121 | } |
| 122 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 123 | std::unique_ptr<Clipboard> Clipboard::Create() { |
| 124 | return base::WrapUnique(new ClipboardX11()); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace remoting |