clipboard-0.5.0/.gitignore010064400017500001750000000000221335052500000136470ustar0000000000000000Cargo.lock target clipboard-0.5.0/.travis.yml010064400017500001750000000003711335052500000137770ustar0000000000000000language: rust os: - linux - osx # TODO: figure out how to get headless X11 working script: | #!/bin/bash cargo build --verbose if [[ $TRAVIS_OS_NAME == 'linux' ]]; then cargo build --tests --verbose else cargo test --verbose fi clipboard-0.5.0/Cargo.toml.orig010064400017500001750000000011501335052500000145510ustar0000000000000000[package] name = "clipboard" version = "0.5.0" authors = ["Avi Weinstock "] description = "rust-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard." repository = "https://github.com/aweinstock314/rust-clipboard" 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"))))'.dependencies] x11-clipboard = "0.3" clipboard-0.5.0/Cargo.toml0000644000000024070000000000000110350ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "clipboard" version = "0.5.0" authors = ["Avi Weinstock "] description = "rust-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard." keywords = ["clipboard"] license = "MIT / Apache-2.0" repository = "https://github.com/aweinstock314/rust-clipboard" [target."cfg(all(unix, not(any(target_os=\"macos\", target_os=\"android\"))))".dependencies.x11-clipboard] version = "0.3" [target."cfg(target_os = \"macos\")".dependencies.objc] version = "0.2" [target."cfg(target_os = \"macos\")".dependencies.objc-foundation] version = "0.1" [target."cfg(target_os = \"macos\")".dependencies.objc_id] version = "0.1" [target."cfg(windows)".dependencies.clipboard-win] version = "2.1" clipboard-0.5.0/LICENSE.apache2010064400017500001750000000260731335052500000142040ustar0000000000000000Apache 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.clipboard-0.5.0/LICENSE.mit010064400017500001750000000020451335052500000134630ustar0000000000000000Copyright (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. clipboard-0.5.0/README.md010064400017500001750000000031711335052500000131460ustar0000000000000000# rust-clipboard rust-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard. It has been tested on Windows, Mac OSX, GNU/Linux, and FreeBSD. It is used in Mozilla Servo. [![](http://meritbadge.herokuapp.com/clipboard)](https://crates.io/crates/clipboard) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/github/aweinstock314/rust-clipboard)](https://ci.appveyor.com/project/aweinstock314/rust-clipboard) [![Travis Build Status](https://travis-ci.org/aweinstock314/rust-clipboard.svg?branch=master)](https://travis-ci.org/aweinstock314/rust-clipboard) ## Prerequisites On Linux you need the x11 library, install it with something like: ```bash sudo apt-get install xorg-dev ``` ## Example ```rust extern crate clipboard; use clipboard::ClipboardProvider; use clipboard::ClipboardContext; fn example() { let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); println!("{:?}", ctx.get_contents()); ctx.set_contents("some string".to_owned()).unwrap(); } ``` ## API The `ClipboardProvider` trait has the following functions: ```rust fn new() -> Result>; fn get_contents(&mut self) -> Result>; fn set_contents(&mut self, String) -> Result<(), Box>; ``` `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. clipboard-0.5.0/appveyor.yml010064400017500001750000000102011335052500000142470ustar0000000000000000# Appveyor configuration template for Rust using rustup for Rust installation # https://github.com/starkat99/appveyor-rust ## Operating System (VM environment) ## # Rust needs at least Visual Studio 2013 Appveyor OS for MSVC targets. os: Visual Studio 2015 ## Build Matrix ## # This configuration will setup a build for each channel & target combination (12 windows # combinations in all). # # There are 3 channels: stable, beta, and nightly. # # Alternatively, the full version may be specified for the channel to build using that specific # version (e.g. channel: 1.5.0) # # The values for target are the set of windows Rust build targets. Each value is of the form # # ARCH-pc-windows-TOOLCHAIN # # Where ARCH is the target architecture, either x86_64 or i686, and TOOLCHAIN is the linker # toolchain to use, either msvc or gnu. See https://www.rust-lang.org/downloads.html#win-foot for # a description of the toolchain differences. # See https://github.com/rust-lang-nursery/rustup.rs/#toolchain-specification for description of # toolchains and host triples. # # Comment out channel/target combos you do not wish to build in CI. # # You may use the `cargoflags` and `RUSTFLAGS` variables to set additional flags for cargo commands # and rustc, respectively. For instance, you can uncomment the cargoflags lines in the nightly # channels to enable unstable features when building for nightly. Or you could add additional # matrix entries to test different combinations of features. environment: matrix: ### MSVC Toolchains ### # Stable 64-bit MSVC - channel: stable target: x86_64-pc-windows-msvc # Stable 32-bit MSVC - channel: stable target: i686-pc-windows-msvc # Beta 64-bit MSVC - channel: beta target: x86_64-pc-windows-msvc # Beta 32-bit MSVC - channel: beta target: i686-pc-windows-msvc # Nightly 64-bit MSVC - channel: nightly target: x86_64-pc-windows-msvc #cargoflags: --features "unstable" # Nightly 32-bit MSVC - channel: nightly target: i686-pc-windows-msvc #cargoflags: --features "unstable" ### GNU Toolchains ### # Stable 64-bit GNU - channel: stable target: x86_64-pc-windows-gnu # Stable 32-bit GNU - channel: stable target: i686-pc-windows-gnu # Beta 64-bit GNU - channel: beta target: x86_64-pc-windows-gnu # Beta 32-bit GNU - channel: beta target: i686-pc-windows-gnu # Nightly 64-bit GNU - channel: nightly target: x86_64-pc-windows-gnu #cargoflags: --features "unstable" # Nightly 32-bit GNU - channel: nightly target: i686-pc-windows-gnu #cargoflags: --features "unstable" ### Allowed failures ### # See Appveyor documentation for specific details. In short, place any channel or targets you wish # to allow build failures on (usually nightly at least is a wise choice). This will prevent a build # or test failure in the matching channels/targets from failing the entire build. matrix: allow_failures: - channel: nightly # If you only care about stable channel build failures, uncomment the following line: #- channel: beta ## Install Script ## # This is the most important part of the Appveyor configuration. This installs the version of Rust # specified by the 'channel' and 'target' environment variables from the build matrix. This uses # rustup to install Rust. # # For simple configurations, instead of using the build matrix, you can simply set the # default-toolchain and default-host manually here. install: - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - rustup-init -yv --default-toolchain %channel% --default-host %target% - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - rustc -vV - cargo -vV ## Build Script ## # 'cargo test' takes care of building for us, so disable Appveyor's build stage. This prevents # the "directory does not contain a project or solution file" error. build: false # Uses 'cargo test' to run tests and build. Alternatively, the project may call compiled programs #directly or perform other testing commands. Rust will automatically be placed in the PATH # environment variable. test_script: - cargo test --verbose %cargoflags% clipboard-0.5.0/examples/hello_world.rs010064400017500001750000000004201335052500000163570ustar0000000000000000extern crate clipboard; use clipboard::ClipboardProvider; use clipboard::ClipboardContext; fn main() { let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); let the_string = "Hello, world!"; ctx.set_contents(the_string.to_owned()).unwrap(); } clipboard-0.5.0/examples/primary_selection.rs010064400017500001750000000007431335052500000176050ustar0000000000000000extern crate clipboard; use clipboard::ClipboardProvider; #[cfg(target_os = "linux")] use clipboard::x11_clipboard::{X11ClipboardContext, Primary}; #[cfg(target_os = "linux")] fn main() { let mut ctx: X11ClipboardContext = ClipboardProvider::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!"); } clipboard-0.5.0/src/common.rs010064400017500001750000000023551335052500000143170ustar0000000000000000/* 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; pub fn err(s: &str) -> Box { Box::::from(s) } /// Trait for clipboard access pub trait ClipboardProvider: Sized { /// Create a context with which to access the clipboard // TODO: consider replacing Box with an associated type? fn new() -> Result>; /// Method to get the clipboard contents as a String fn get_contents(&mut self) -> Result>; /// Method to set the clipboard contents as a String fn set_contents(&mut self, String) -> Result<(), Box>; // TODO: come up with some platform-agnostic API for richer types // than just strings (c.f. issue #31) } clipboard-0.5.0/src/lib.rs010064400017500001750000000037741335052500000136030ustar0000000000000000/* 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 = "clipboard"] #![crate_type = "lib"] #![crate_type = "dylib"] #![crate_type = "rlib"] #[cfg(all(unix, not(any(target_os="macos", target_os="android"))))] 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_id; #[cfg(target_os="macos")] extern crate objc_foundation; mod common; pub use common::ClipboardProvider; #[cfg(all(unix, not(any(target_os="macos", target_os="android"))))] 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"))))] 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 (see #52) #[cfg(not(any(unix, windows, target_os="macos", target_os="android")))] pub type ClipboardContext = nop_clipboard::NopClipboardContext; #[test] fn test_clipboard() { let mut ctx = ClipboardContext::new().unwrap(); ctx.set_contents("some string".to_owned()).unwrap(); assert!(ctx.get_contents().unwrap() == "some string"); } clipboard-0.5.0/src/nop_clipboard.rs010064400017500001750000000023661335052500000156440ustar0000000000000000/* 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 ClipboardProvider for NopClipboardContext { fn new() -> Result> { Ok(NopClipboardContext) } fn get_contents(&mut self) -> Result> { 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> { println!("Attempting to set the contents of the clipboard, which hasn't yet been \ implemented on this platform."); Ok(()) } } clipboard-0.5.0/src/osx_clipboard.rs010064400017500001750000000063251335052500000156600ustar0000000000000000/* 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::{Object, Class}; use objc_foundation::{INSArray, INSString, INSObject}; use objc_foundation::{NSArray, NSDictionary, NSString, NSObject}; use objc_id::{Id, Owned}; use std::error::Error; use std::mem::transmute; pub struct OSXClipboardContext { pasteboard: Id, } // required to bring NSPasteboard into the path of the class-resolver #[link(name = "AppKit", kind = "framework")] extern "C" {} impl ClipboardProvider for OSXClipboardContext { fn new() -> Result> { let cls = try!(Class::get("NSPasteboard").ok_or(err("Class::get(\"NSPasteboard\")"))); let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] }; if pasteboard.is_null() { return Err(err("NSPasteboard#generalPasteboard returned null")); } let pasteboard: Id = unsafe { Id::from_ptr(pasteboard) }; Ok(OSXClipboardContext { pasteboard: pasteboard }) } fn get_contents(&mut self) -> Result> { let string_class: Id = { let cls: Id = unsafe { Id::from_ptr(class("NSString")) }; unsafe { transmute(cls) } }; let classes: Id> = NSArray::from_vec(vec![string_class]); let options: Id> = NSDictionary::new(); let string_array: Id> = unsafe { let obj: *mut NSArray = msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options]; if obj.is_null() { return Err(err("pasteboard#readObjectsForClasses:options: returned null")); } Id::from_ptr(obj) }; if string_array.count() == 0 { Err(err("pasteboard#readObjectsForClasses:options: returned empty")) } else { Ok(string_array[0].as_str().to_owned()) } } fn set_contents(&mut self, data: String) -> Result<(), Box> { 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(err("NSPasteboard#writeObjects: returned false")) }; } } // 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)) } } clipboard-0.5.0/src/windows_clipboard.rs010064400017500001750000000020771335052500000165410ustar0000000000000000/* 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 ClipboardProvider for WindowsClipboardContext { fn new() -> Result> { Ok(WindowsClipboardContext) } fn get_contents(&mut self) -> Result> { Ok(get_clipboard_string()?) } fn set_contents(&mut self, data: String) -> Result<(), Box> { Ok(set_clipboard_string(&data)?) } } clipboard-0.5.0/src/x11_clipboard.rs010064400017500001750000000036001335052500000154510ustar0000000000000000/* 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::time::Duration; use std::marker::PhantomData; use common::*; use x11_clipboard_crate::Atoms; use x11_clipboard_crate::Clipboard as X11Clipboard; use x11_clipboard_crate::xcb::xproto::Atom; pub trait Selection { 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(X11Clipboard, PhantomData) where S: Selection; impl ClipboardProvider for X11ClipboardContext where S: Selection, { fn new() -> Result, Box> { Ok(X11ClipboardContext(X11Clipboard::new()?, PhantomData)) } fn get_contents(&mut self) -> Result> { 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> { Ok(self.0.store( S::atom(&self.0.setter.atoms), self.0.setter.atoms.utf8_string, data, )?) } }