OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/clipboard.h" | 5 #include "remoting/host/clipboard.h" |
6 | 6 |
| 7 #include <X11/Xlib.h> |
| 8 |
| 9 #include "base/bind.h" |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "remoting/host/linux/x_server_clipboard.h" |
| 13 #include "remoting/proto/event.pb.h" |
| 14 #include "remoting/protocol/clipboard_stub.h" |
8 | 15 |
9 namespace remoting { | 16 namespace remoting { |
10 | 17 |
11 class ClipboardLinux : public Clipboard { | 18 // This code is expected to be called on the desktop thread only. |
| 19 class ClipboardLinux : public Clipboard, |
| 20 public MessageLoopForIO::Watcher { |
12 public: | 21 public: |
13 ClipboardLinux(); | 22 ClipboardLinux(); |
| 23 virtual ~ClipboardLinux(); |
14 | 24 |
15 // Must be called on the UI thread. | 25 // Clipboard interface. |
16 virtual void Start( | 26 virtual void Start( |
17 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; | 27 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
18 virtual void InjectClipboardEvent( | 28 virtual void InjectClipboardEvent( |
19 const protocol::ClipboardEvent& event) OVERRIDE; | 29 const protocol::ClipboardEvent& event) OVERRIDE; |
20 virtual void Stop() OVERRIDE; | 30 virtual void Stop() OVERRIDE; |
21 | 31 |
| 32 // MessageLoopForIO::Watcher interface. |
| 33 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 34 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 35 |
22 private: | 36 private: |
| 37 void OnClipboardChanged(const std::string& mime_type, |
| 38 const std::string& data); |
| 39 void PumpXEvents(); |
| 40 |
| 41 scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| 42 |
| 43 XServerClipboard x_server_clipboard_; |
| 44 Display* display_; |
| 45 |
| 46 MessageLoopForIO::FileDescriptorWatcher x_connection_watcher_; |
| 47 |
23 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); | 48 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); |
24 }; | 49 }; |
25 | 50 |
| 51 ClipboardLinux::ClipboardLinux() |
| 52 : display_(NULL) { |
| 53 } |
| 54 |
| 55 ClipboardLinux::~ClipboardLinux() { |
| 56 Stop(); |
| 57 } |
| 58 |
26 void ClipboardLinux::Start( | 59 void ClipboardLinux::Start( |
27 scoped_ptr<protocol::ClipboardStub> client_clipboard) { | 60 scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
28 NOTIMPLEMENTED(); | 61 // TODO(lambroslambrou): Share the X connection with EventExecutor. |
| 62 display_ = XOpenDisplay(NULL); |
| 63 if (!display_) { |
| 64 LOG(ERROR) << "Couldn't open X display"; |
| 65 return; |
| 66 } |
| 67 client_clipboard_.swap(client_clipboard); |
| 68 |
| 69 x_server_clipboard_.Init(display_, |
| 70 base::Bind(&ClipboardLinux::OnClipboardChanged, |
| 71 base::Unretained(this))); |
| 72 |
| 73 MessageLoopForIO::current()->WatchFileDescriptor( |
| 74 ConnectionNumber(display_), true, MessageLoopForIO::WATCH_READ, |
| 75 &x_connection_watcher_, this); |
| 76 PumpXEvents(); |
29 } | 77 } |
30 | 78 |
31 void ClipboardLinux::InjectClipboardEvent( | 79 void ClipboardLinux::InjectClipboardEvent( |
32 const protocol::ClipboardEvent& event) { | 80 const protocol::ClipboardEvent& event) { |
33 NOTIMPLEMENTED(); | 81 x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
34 } | 82 } |
35 | 83 |
36 void ClipboardLinux::Stop() { | 84 void ClipboardLinux::Stop() { |
37 NOTIMPLEMENTED(); | 85 client_clipboard_.reset(); |
| 86 x_connection_watcher_.StopWatchingFileDescriptor(); |
| 87 |
| 88 if (display_) { |
| 89 XCloseDisplay(display_); |
| 90 display_ = NULL; |
| 91 } |
| 92 } |
| 93 |
| 94 void ClipboardLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 95 PumpXEvents(); |
| 96 } |
| 97 |
| 98 void ClipboardLinux::OnFileCanWriteWithoutBlocking(int fd) { |
| 99 } |
| 100 |
| 101 void ClipboardLinux::OnClipboardChanged(const std::string& mime_type, |
| 102 const std::string& data) { |
| 103 protocol::ClipboardEvent event; |
| 104 event.set_mime_type(mime_type); |
| 105 event.set_data(data); |
| 106 |
| 107 if (client_clipboard_.get()) { |
| 108 client_clipboard_->InjectClipboardEvent(event); |
| 109 } |
| 110 } |
| 111 |
| 112 void ClipboardLinux::PumpXEvents() { |
| 113 DCHECK(display_); |
| 114 |
| 115 while (XPending(display_)) { |
| 116 XEvent event; |
| 117 XNextEvent(display_, &event); |
| 118 x_server_clipboard_.ProcessXEvent(&event); |
| 119 } |
38 } | 120 } |
39 | 121 |
40 scoped_ptr<Clipboard> Clipboard::Create() { | 122 scoped_ptr<Clipboard> Clipboard::Create() { |
41 return scoped_ptr<Clipboard>(new ClipboardLinux()); | 123 return scoped_ptr<Clipboard>(new ClipboardLinux()); |
42 } | 124 } |
43 | 125 |
44 } // namespace remoting | 126 } // namespace remoting |
OLD | NEW |