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