[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; |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 28 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 29 | // MessageLoopForIO::Watcher interface. |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 30 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 31 | void OnFileCanWriteWithoutBlocking(int fd) override; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 32 | |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 33 | private: |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 34 | void OnClipboardChanged(const std::string& mime_type, |
| 35 | const std::string& data); |
| 36 | void PumpXEvents(); |
| 37 | |
| 38 | scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| 39 | |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 40 | // Underlying X11 clipboard implementation. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 41 | XServerClipboard x_server_clipboard_; |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 42 | |
| 43 | // Connection to the X server, used by |x_server_clipboard_|. This is created |
| 44 | // and owned by this class. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 45 | Display* display_; |
| 46 | |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 47 | // Watcher used to handle X11 events from |display_|. |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 48 | base::MessageLoopForIO::FileDescriptorWatcher x_connection_watcher_; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 49 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 50 | DISALLOW_COPY_AND_ASSIGN(ClipboardX11); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 51 | }; |
| 52 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 53 | ClipboardX11::ClipboardX11() |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 54 | : display_(nullptr) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 57 | ClipboardX11::~ClipboardX11() { |
sergeyu | 45f92d83 | 2015-02-19 01:24:19 | [diff] [blame^] | 58 | if (display_) |
| 59 | XCloseDisplay(display_); |
[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 | 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::OnFileCanReadWithoutBlocking(int fd) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 91 | PumpXEvents(); |
| 92 | } |
| 93 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 94 | void ClipboardX11::OnFileCanWriteWithoutBlocking(int fd) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 97 | void ClipboardX11::OnClipboardChanged(const std::string& mime_type, |
| 98 | const std::string& data) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 99 | protocol::ClipboardEvent event; |
| 100 | event.set_mime_type(mime_type); |
| 101 | event.set_data(data); |
| 102 | |
| 103 | if (client_clipboard_.get()) { |
| 104 | client_clipboard_->InjectClipboardEvent(event); |
| 105 | } |
| 106 | } |
| 107 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 108 | void ClipboardX11::PumpXEvents() { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 109 | DCHECK(display_); |
| 110 | |
| 111 | while (XPending(display_)) { |
| 112 | XEvent event; |
| 113 | XNextEvent(display_, &event); |
| 114 | x_server_clipboard_.ProcessXEvent(&event); |
| 115 | } |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | scoped_ptr<Clipboard> Clipboard::Create() { |
sergeyu | 2d69088 | 2014-10-01 02:36:43 | [diff] [blame] | 119 | return make_scoped_ptr(new ClipboardX11()); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace remoting |