summaryrefslogtreecommitdiff
path: root/copypasta
diff options
context:
space:
mode:
authorChristian Duerr <chrisduerr@users.noreply.github.com>2019-06-09 11:46:31 +0000
committerGitHub <noreply@github.com>2019-06-09 11:46:31 +0000
commitbc2c34eb7f9eea251822d94ea534a1d2de03c5a1 (patch)
tree8aebbcb1f31dac6f9439f71e784605ca1133ea35 /copypasta
parent4cd55acd7820a7358f9175c106c91e0945fb15b2 (diff)
downloadalacritty-bc2c34eb7f9eea251822d94ea534a1d2de03c5a1.tar.gz
alacritty-bc2c34eb7f9eea251822d94ea534a1d2de03c5a1.zip
Add wayland primary selection clipboard support
Diffstat (limited to 'copypasta')
-rw-r--r--copypasta/Cargo.toml25
-rw-r--r--copypasta/LICENSE.apache2201
-rw-r--r--copypasta/LICENSE.mit25
-rw-r--r--copypasta/README.md34
-rw-r--r--copypasta/examples/hello_world.rs12
-rw-r--r--copypasta/examples/primary_selection.rs19
-rw-r--r--copypasta/examples/wayland.rs227
-rw-r--r--copypasta/src/common.rs24
-rw-r--r--copypasta/src/lib.rs69
-rw-r--r--copypasta/src/nop_clipboard.rs42
-rw-r--r--copypasta/src/osx_clipboard.rs84
-rw-r--r--copypasta/src/wayland_clipboard.rs70
-rw-r--r--copypasta/src/windows_clipboard.rs36
-rw-r--r--copypasta/src/x11_clipboard.rs72
14 files changed, 940 insertions, 0 deletions
diff --git a/copypasta/Cargo.toml b/copypasta/Cargo.toml
new file mode 100644
index 00000000..229404c3
--- /dev/null
+++ b/copypasta/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+name = "copypasta"
+version = "0.6.0"
+authors = ["Christian Duerr <contact@christianduerr.com>"]
+description = "copypasta is a cross-platform library for getting and setting the contents of the OS-level clipboard."
+repository = "https://github.com/jwilm/alacritty"
+license = "MIT / Apache-2.0"
+keywords = ["clipboard"]
+
+[target.'cfg(windows)'.dependencies]
+clipboard-win = "2.1"
+
+[target.'cfg(target_os = "macos")'.dependencies]
+objc = "0.2"
+objc_id = "0.1"
+objc-foundation = "0.1"
+
+[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
+x11-clipboard = "0.3"
+smithay-clipboard = "0.3.0"
+wayland-client = { version = "0.22", features = ["dlopen"] }
+
+[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dev-dependencies]
+andrew = "0.2.1"
+smithay-client-toolkit = "0.5"
diff --git a/copypasta/LICENSE.apache2 b/copypasta/LICENSE.apache2
new file mode 100644
index 00000000..ad410e11
--- /dev/null
+++ b/copypasta/LICENSE.apache2
@@ -0,0 +1,201 @@
+Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. \ No newline at end of file
diff --git a/copypasta/LICENSE.mit b/copypasta/LICENSE.mit
new file mode 100644
index 00000000..c8b9dd03
--- /dev/null
+++ b/copypasta/LICENSE.mit
@@ -0,0 +1,25 @@
+Copyright (c) 2017 Avraham Weinstock
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/copypasta/README.md b/copypasta/README.md
new file mode 100644
index 00000000..e2502c0d
--- /dev/null
+++ b/copypasta/README.md
@@ -0,0 +1,34 @@
+# copypasta
+
+copypasta is a (rust-clipboard)[https://github.com/aweinstock314/rust-clipboard] fork, adding support for the Wayland clipboard.
+
+rust-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard.
+
+## Example
+
+```rust
+extern crate copypasta;
+
+use copypasta::ClipboardContext;
+
+fn example() {
+ let mut ctx = ClipboardContext::new().unwrap();
+ println!("{:?}", ctx.get_contents());
+ ctx.set_contents("some string".to_owned()).unwrap();
+}
+```
+
+## API
+
+The `ClipboardProvider` trait has the following functions:
+
+```rust
+fn get_contents(&mut self) -> Result<String, Box<Error>>;
+fn set_contents(&mut self, String) -> Result<(), Box<Error>>;
+```
+
+`ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `X11ClipboardContext`, `NopClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation).
+
+## License
+
+`rust-clipboard` is dual-licensed under MIT and Apache2.
diff --git a/copypasta/examples/hello_world.rs b/copypasta/examples/hello_world.rs
new file mode 100644
index 00000000..7d637f82
--- /dev/null
+++ b/copypasta/examples/hello_world.rs
@@ -0,0 +1,12 @@
+extern crate copypasta;
+
+use copypasta::ClipboardContext;
+use copypasta::ClipboardProvider;
+
+fn main() {
+ let mut ctx = ClipboardContext::new().unwrap();
+
+ let the_string = "Hello, world!";
+
+ ctx.set_contents(the_string.to_owned()).unwrap();
+}
diff --git a/copypasta/examples/primary_selection.rs b/copypasta/examples/primary_selection.rs
new file mode 100644
index 00000000..5525935e
--- /dev/null
+++ b/copypasta/examples/primary_selection.rs
@@ -0,0 +1,19 @@
+extern crate copypasta;
+
+#[cfg(target_os = "linux")]
+use copypasta::x11_clipboard::{Primary, X11ClipboardContext};
+use copypasta::ClipboardProvider;
+
+#[cfg(target_os = "linux")]
+fn main() {
+ let mut ctx = X11ClipboardContext::<Primary>::new().unwrap();
+
+ let the_string = "Hello, world!";
+
+ ctx.set_contents(the_string.to_owned()).unwrap();
+}
+
+#[cfg(not(target_os = "linux"))]
+fn main() {
+ println!("Primary selection is only available under linux!");
+}
diff --git a/copypasta/examples/wayland.rs b/copypasta/examples/wayland.rs
new file mode 100644
index 00000000..a718a3e8
--- /dev/null
+++ b/copypasta/examples/wayland.rs
@@ -0,0 +1,227 @@
+#[cfg(any(not(unix), target_os = "macos", target_os = "android", target_os = "emscripten"))]
+fn main() {
+ unimplemented!()
+}
+
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+fn main() {
+ wayland::main();
+}
+
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+mod wayland {
+ extern crate andrew;
+ extern crate copypasta;
+ extern crate smithay_client_toolkit as sctk;
+
+ use wayland::copypasta::wayland_clipboard::{Clipboard, WaylandClipboardContext};
+ use wayland::copypasta::ClipboardProvider;
+
+ use std::io::{Read, Seek, SeekFrom, Write};
+ use std::sync::{atomic, Arc, Mutex};
+
+ use wayland::sctk::keyboard::{map_keyboard_auto, Event as KbEvent, KeyState};
+ use wayland::sctk::utils::{DoubleMemPool, MemPool};
+ use wayland::sctk::window::{ConceptFrame, Event as WEvent, Window};
+ use wayland::sctk::Environment;
+
+ use wayland::sctk::reexports::client::protocol::{wl_shm, wl_surface};
+ use wayland::sctk::reexports::client::{Display, NewProxy};
+
+ use wayland::andrew::shapes::rectangle;
+ use wayland::andrew::text;
+ use wayland::andrew::text::fontconfig;
+
+ pub fn main() {
+ let (display, mut event_queue) =
+ Display::connect_to_env().expect("Failed to connect to the wayland server.");
+ let env = Environment::from_display(&*display, &mut event_queue).unwrap();
+
+ let mut ctx = WaylandClipboardContext::<Clipboard>::new(&display);
+ let cb_contents = Arc::new(Mutex::new(String::new()));
+
+ let seat = env.manager.instantiate_range(2, 6, NewProxy::implement_dummy).unwrap();
+
+ let need_redraw = Arc::new(atomic::AtomicBool::new(false));
+ let need_redraw_clone = need_redraw.clone();
+ let cb_contents_clone = cb_contents.clone();
+ map_keyboard_auto(&seat, move |event: KbEvent, _| {
+ if let KbEvent::Key { state: KeyState::Pressed, utf8: Some(text), .. } = event {
+ if text == " " {
+ *cb_contents_clone.lock().unwrap() = ctx.get_contents().unwrap();
+ need_redraw_clone.store(true, atomic::Ordering::Relaxed)
+ } else if text == "s" {
+ ctx.set_contents(
+ "This is an example text thats been copied to the wayland clipboard :)"
+ .to_string(),
+ )
+ .unwrap();
+ } else if text == "t" {
+ ctx.set_contents("Alternative text :)".to_string()).unwrap();
+ }
+ }
+ })
+ .unwrap();
+
+ let mut dimensions = (320u32, 240u32);
+ let surface = env.compositor.create_surface(NewProxy::implement_dummy).unwrap();
+
+ let next_action = Arc::new(Mutex::new(None::<WEvent>));
+
+ let waction = next_action.clone();
+ let mut window =
+ Window::<ConceptFrame>::init_from_env(&env, surface, dimensions, move |evt| {
+ let mut next_action = waction.lock().unwrap();
+ // Keep last event in priority order : Close > Configure > Refresh
+ let replace = match (&evt, &*next_action) {
+ (_, &None)
+ | (_, &Some(WEvent::Refresh))
+ | (&WEvent::Configure { .. }, &Some(WEvent::Configure { .. }))
+ | (&WEvent::Close, _) => true,
+ _ => false,
+ };
+ if replace {
+ *next_action = Some(evt);
+ }
+ })
+ .expect("Failed to create a window !");
+
+ window.new_seat(&seat);
+ window.set_title("Clipboard".to_string());
+
+ let mut pools =
+ DoubleMemPool::new(&env.shm, || {}).expect("Failed to create a memory pool !");
+
+ let mut font_data = Vec::new();
+ std::fs::File::open(
+ &fontconfig::FontConfig::new().unwrap().get_regular_family_fonts("sans").unwrap()[0],
+ )
+ .unwrap()
+ .read_to_end(&mut font_data)
+ .unwrap();
+
+ if !env.shell.needs_configure() {
+ // initial draw to bootstrap on wl_shell
+ if let Some(pool) = pools.pool() {
+ redraw(pool, window.surface(), dimensions, &font_data, "".to_string());
+ }
+ window.refresh();
+ }
+
+ loop {
+ match next_action.lock().unwrap().take() {
+ Some(WEvent::Close) => break,
+ Some(WEvent::Refresh) => {
+ window.refresh();
+ window.surface().commit();
+ },
+ Some(WEvent::Configure { new_size, .. }) => {
+ if let Some((w, h)) = new_size {
+ window.resize(w, h);
+ dimensions = (w, h)
+ }
+ window.refresh();
+ if let Some(pool) = pools.pool() {
+ redraw(
+ pool,
+ window.surface(),
+ dimensions,
+ &font_data,
+ cb_contents.lock().unwrap().clone(),
+ );
+ }
+ },
+ None => {},
+ }
+
+ if need_redraw.swap(false, atomic::Ordering::Relaxed) {
+ if let Some(pool) = pools.pool() {
+ redraw(
+ pool,
+ window.surface(),
+ dimensions,
+ &font_data,
+ cb_contents.lock().unwrap().clone(),
+ );
+ }
+ window.surface().damage_buffer(0, 0, dimensions.0 as i32, dimensions.1 as i32);
+ window.surface().commit();
+ }
+
+ event_queue.dispatch().unwrap();
+ }
+ }
+
+ fn redraw(
+ pool: &mut MemPool,
+ surface: &wl_surface::WlSurface,
+ dimensions: (u32, u32),
+ font_data: &[u8],
+ cb_contents: String,
+ ) {
+ let (buf_x, buf_y) = (dimensions.0 as usize, dimensions.1 as usize);
+
+ pool.resize(4 * buf_x * buf_y).expect("Failed to resize the memory pool.");
+
+ let mut buf = vec![0; 4 * buf_x * buf_y];
+ let mut canvas =
+ andrew::Canvas::new(&mut buf, buf_x, buf_y, 4 * buf_x, andrew::Endian::native());
+
+ let bg = rectangle::Rectangle::new((0, 0), (buf_x, buf_y), None, Some([255, 170, 20, 45]));
+ canvas.draw(&bg);
+
+ let text_box = rectangle::Rectangle::new(
+ (buf_x / 30, buf_y / 35),
+ (buf_x - 2 * (buf_x / 30), (buf_x as f32 / 14.) as usize),
+ Some((3, [255, 255, 255, 255], rectangle::Sides::ALL, Some(4))),
+ None,
+ );
+ canvas.draw(&text_box);
+
+ let helper_text = text::Text::new(
+ (buf_x / 25, buf_y / 30),
+ [255, 255, 255, 255],
+ font_data,
+ buf_x as f32 / 40.,
+ 2.0,
+ "Press space to draw clipboard contents",
+ );
+ canvas.draw(&helper_text);
+
+ let helper_text = text::Text::new(
+ (buf_x / 25, buf_y / 15),
+ [255, 255, 255, 255],
+ font_data,
+ buf_x as f32 / 40.,
+ 2.0,
+ "Press 's' to store example text to clipboard",
+ );
+ canvas.draw(&helper_text);
+
+ for i in (0..cb_contents.len()).step_by(36) {
+ let content = if cb_contents.len() < i + 36 {
+ cb_contents[i..].to_string()
+ } else {
+ cb_contents[i..i + 36].to_string()
+ };
+ let text = text::Text::new(
+ (buf_x / 10, buf_y / 8 + (i as f32 * buf_y as f32 / 1000.) as usize),
+ [255, 255, 255, 255],
+ font_data,
+ buf_x as f32 / 40.,
+ 2.0,
+ content,
+ );
+ canvas.draw(&text);
+ }
+
+ pool.seek(SeekFrom::Start(0)).unwrap();
+ pool.write_all(canvas.buffer).unwrap();
+ pool.flush().unwrap();
+
+ let new_buffer =
+ pool.buffer(0, buf_x as i32, buf_y as i32, 4 * buf_x as i32, wl_shm::Format::Argb8888);
+ surface.attach(Some(&new_buffer), 0, 0);
+ surface.commit();
+ }
+}
diff --git a/copypasta/src/common.rs b/copypasta/src/common.rs
new file mode 100644
index 00000000..79cf1fb7
--- /dev/null
+++ b/copypasta/src/common.rs
@@ -0,0 +1,24 @@
+// Copyright 2016 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::error::Error;
+
+// TODO: come up with some platform-agnostic API for richer types
+/// Trait for clipboard access
+pub trait ClipboardProvider: Send {
+ /// Method to get the clipboard contents as a String
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>>;
+ /// Method to set the clipboard contents as a String
+ fn set_contents(&mut self, String) -> Result<(), Box<dyn Error>>;
+}
diff --git a/copypasta/src/lib.rs b/copypasta/src/lib.rs
new file mode 100644
index 00000000..64b44219
--- /dev/null
+++ b/copypasta/src/lib.rs
@@ -0,0 +1,69 @@
+// Copyright 2016 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#![crate_name = "copypasta"]
+#![crate_type = "lib"]
+#![crate_type = "dylib"]
+#![crate_type = "rlib"]
+
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+extern crate smithay_clipboard;
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+extern crate wayland_client;
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+extern crate x11_clipboard as x11_clipboard_crate;
+
+#[cfg(windows)]
+extern crate clipboard_win;
+
+#[cfg(target_os = "macos")]
+#[macro_use]
+extern crate objc;
+#[cfg(target_os = "macos")]
+extern crate objc_foundation;
+#[cfg(target_os = "macos")]
+extern crate objc_id;
+
+mod common;
+pub use common::ClipboardProvider;
+
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+pub mod wayland_clipboard;
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+pub mod x11_clipboard;
+
+#[cfg(windows)]
+pub mod windows_clipboard;
+
+#[cfg(target_os = "macos")]
+pub mod osx_clipboard;
+
+pub mod nop_clipboard;
+
+#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
+pub type ClipboardContext = x11_clipboard::X11ClipboardContext;
+#[cfg(windows)]
+pub type ClipboardContext = windows_clipboard::WindowsClipboardContext;
+#[cfg(target_os = "macos")]
+pub type ClipboardContext = osx_clipboard::OSXClipboardContext;
+#[cfg(target_os = "android")]
+pub type ClipboardContext = nop_clipboard::NopClipboardContext; // TODO: implement AndroidClipboardContext
+#[cfg(not(any(
+ unix,
+ windows,
+ target_os = "macos",
+ target_os = "android",
+ target_os = "emscripten"
+)))]
+pub type ClipboardContext = nop_clipboard::NopClipboardContext;
diff --git a/copypasta/src/nop_clipboard.rs b/copypasta/src/nop_clipboard.rs
new file mode 100644
index 00000000..bdb79e20
--- /dev/null
+++ b/copypasta/src/nop_clipboard.rs
@@ -0,0 +1,42 @@
+// Copyright 2016 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use common::ClipboardProvider;
+use std::error::Error;
+
+pub struct NopClipboardContext;
+
+impl NopClipboardContext {
+ pub fn new() -> Result<NopClipboardContext, Box<dyn Error>> {
+ Ok(NopClipboardContext)
+ }
+}
+
+impl ClipboardProvider for NopClipboardContext {
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ println!(
+ "Attempting to get the contents of the clipboard, which hasn't yet been implemented \
+ on this platform."
+ );
+ Ok("".to_string())
+ }
+
+ fn set_contents(&mut self, _: String) -> Result<(), Box<dyn Error>> {
+ println!(
+ "Attempting to set the contents of the clipboard, which hasn't yet been implemented \
+ on this platform."
+ );
+ Ok(())
+ }
+}
diff --git a/copypasta/src/osx_clipboard.rs b/copypasta/src/osx_clipboard.rs
new file mode 100644
index 00000000..0d65679d
--- /dev/null
+++ b/copypasta/src/osx_clipboard.rs
@@ -0,0 +1,84 @@
+// Copyright 2016 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use common::*;
+use objc::runtime::{Class, Object};
+use objc_foundation::{INSArray, INSObject, INSString};
+use objc_foundation::{NSArray, NSDictionary, NSObject, NSString};
+use objc_id::{Id, Owned};
+use std::error::Error;
+use std::mem::transmute;
+
+pub struct OSXClipboardContext {
+ pasteboard: Id<Object>,
+}
+
+// required to bring NSPasteboard into the path of the class-resolver
+#[link(name = "AppKit", kind = "framework")]
+extern "C" {}
+
+impl OSXClipboardContext {
+ pub fn new() -> Result<OSXClipboardContext, Box<dyn Error>> {
+ let cls = Class::get("NSPasteboard").ok_or("Class::get(\"NSPasteboard\")")?;
+ let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] };
+ if pasteboard.is_null() {
+ return Err("NSPasteboard#generalPasteboard returned null".into());
+ }
+ let pasteboard: Id<Object> = unsafe { Id::from_ptr(pasteboard) };
+ Ok(OSXClipboardContext { pasteboard })
+ }
+}
+
+impl ClipboardProvider for OSXClipboardContext {
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ let string_class: Id<NSObject> = {
+ let cls: Id<Class> = unsafe { Id::from_ptr(class("NSString")) };
+ unsafe { transmute(cls) }
+ };
+ let classes: Id<NSArray<NSObject, Owned>> = NSArray::from_vec(vec![string_class]);
+ let options: Id<NSDictionary<NSObject, NSObject>> = NSDictionary::new();
+ let string_array: Id<NSArray<NSString>> = unsafe {
+ let obj: *mut NSArray<NSString> =
+ msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options];
+ if obj.is_null() {
+ return Err("pasteboard#readObjectsForClasses:options: returned null".into());
+ }
+ Id::from_ptr(obj)
+ };
+ if string_array.count() == 0 {
+ Err("pasteboard#readObjectsForClasses:options: returned empty".into())
+ } else {
+ Ok(string_array[0].as_str().to_owned())
+ }
+ }
+
+ fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
+ let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]);
+ let _: usize = unsafe { msg_send![self.pasteboard, clearContents] };
+ let success: bool = unsafe { msg_send![self.pasteboard, writeObjects: string_array] };
+ return if success {
+ Ok(())
+ } else {
+ Err("NSPasteboard#writeObjects: returned false".into())
+ };
+ }
+}
+
+// this is a convenience function that both cocoa-rs and
+// glutin define, which seems to depend on the fact that
+// Option::None has the same representation as a null pointer
+#[inline]
+pub fn class(name: &str) -> *mut Class {
+ unsafe { transmute(Class::get(name)) }
+}
diff --git a/copypasta/src/wayland_clipboard.rs b/copypasta/src/wayland_clipboard.rs
new file mode 100644
index 00000000..cb450725
--- /dev/null
+++ b/copypasta/src/wayland_clipboard.rs
@@ -0,0 +1,70 @@
+// Copyright 2017 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::error::Error;
+use std::ffi::c_void;
+use std::marker::PhantomData;
+
+use smithay_clipboard::WaylandClipboard;
+use wayland_client::sys::client::wl_display;
+use wayland_client::Display;
+
+use common::ClipboardProvider;
+
+pub trait ClipboardType: Send {}
+
+pub struct Clipboard;
+impl ClipboardType for Clipboard {}
+
+pub struct Primary;
+impl ClipboardType for Primary {}
+
+pub struct WaylandClipboardContext<T: ClipboardType>(WaylandClipboard, PhantomData<T>);
+
+impl<T: ClipboardType> WaylandClipboardContext<T> {
+ /// Create a new clipboard context.
+ pub fn new(display: &Display) -> Self {
+ WaylandClipboardContext(WaylandClipboard::new(display), PhantomData)
+ }
+
+ /// Create a new clipboard context from an external pointer.
+ pub unsafe fn new_from_external(display: *mut c_void) -> Self {
+ WaylandClipboardContext(
+ WaylandClipboard::new_from_external(display as *mut wl_display),
+ PhantomData,
+ )
+ }
+}
+
+impl ClipboardProvider for WaylandClipboardContext<Clipboard> {
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ Ok(self.0.load(None))
+ }
+
+ fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
+ self.0.store(None, data);
+ Ok(())
+ }
+}
+
+impl ClipboardProvider for WaylandClipboardContext<Primary> {
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ Ok(self.0.load_primary(None))
+ }
+
+ fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
+ self.0.store_primary(None, data);
+ Ok(())
+ }
+}
diff --git a/copypasta/src/windows_clipboard.rs b/copypasta/src/windows_clipboard.rs
new file mode 100644
index 00000000..e35d3130
--- /dev/null
+++ b/copypasta/src/windows_clipboard.rs
@@ -0,0 +1,36 @@
+// Copyright 2016 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use clipboard_win::{get_clipboard_string, set_clipboard_string};
+
+use common::ClipboardProvider;
+use std::error::Error;
+
+pub struct WindowsClipboardContext;
+
+impl WindowsClipboardContext {
+ pub fn new() -> Result<Self, Box<dyn Error>> {
+ Ok(WindowsClipboardContext)
+ }
+}
+
+impl ClipboardProvider for WindowsClipboardContext {
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ Ok(get_clipboard_string()?)
+ }
+
+ fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
+ Ok(set_clipboard_string(&data)?)
+ }
+}
diff --git a/copypasta/src/x11_clipboard.rs b/copypasta/src/x11_clipboard.rs
new file mode 100644
index 00000000..1b7bdff4
--- /dev/null
+++ b/copypasta/src/x11_clipboard.rs
@@ -0,0 +1,72 @@
+// Copyright 2017 Avraham Weinstock
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use common::*;
+use std::error::Error;
+use std::marker::PhantomData;
+use std::time::Duration;
+use x11_clipboard_crate::xcb::xproto::Atom;
+use x11_clipboard_crate::Atoms;
+use x11_clipboard_crate::Clipboard as X11Clipboard;
+
+pub trait Selection: Send {
+ fn atom(atoms: &Atoms) -> Atom;
+}
+
+pub struct Primary;
+
+impl Selection for Primary {
+ fn atom(atoms: &Atoms) -> Atom {
+ atoms.primary
+ }
+}
+
+pub struct Clipboard;
+
+impl Selection for Clipboard {
+ fn atom(atoms: &Atoms) -> Atom {
+ atoms.clipboard
+ }
+}
+
+pub struct X11ClipboardContext<S = Clipboard>(X11Clipboard, PhantomData<S>)
+where
+ S: Selection;
+
+impl<S> X11ClipboardContext<S>
+where
+ S: Selection,
+{
+ pub fn new() -> Result<X11ClipboardContext<S>, Box<dyn Error>> {
+ Ok(X11ClipboardContext(X11Clipboard::new()?, PhantomData))
+ }
+}
+
+impl<S> ClipboardProvider for X11ClipboardContext<S>
+where
+ S: Selection,
+{
+ fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
+ Ok(String::from_utf8(self.0.load(
+ S::atom(&self.0.getter.atoms),
+ self.0.getter.atoms.utf8_string,
+ self.0.getter.atoms.property,
+ Duration::from_secs(3),
+ )?)?)
+ }
+
+ fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
+ Ok(self.0.store(S::atom(&self.0.setter.atoms), self.0.setter.atoms.utf8_string, data)?)
+ }
+}