-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ippee
committed
Jul 16, 2021
1 parent
c02389c
commit 8f2eef4
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "libclipboard" | ||
version = "0.1.0" | ||
authors = ["ippee <ippee.music@gmail.com>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "libclipboard" | ||
crate-type = ["cdylib", "staticlib"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clipboard = "0.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# libclipboard | ||
|
||
A dynamic library to operate clipboard, for [ippee / dart_clipboard](https://github.com/ippee/dart_clipboard). | ||
|
||
## Installation | ||
|
||
### Case A: Use your own build files | ||
|
||
On your Dart project directory, run the following commands. | ||
|
||
```shell | ||
mkdir ./tool/ | ||
git submodule add git@github.com:ippee/libclipboard.git ./tool/libclipboard/ | ||
|
||
cd ./tool/libclipboard/ | ||
cargo build | ||
|
||
# on Windows | ||
cp ./target/debug/libclipboard.dll ../../ | ||
|
||
# on macOS | ||
cp ./target/debug/libclipboard.dylib ../../ | ||
|
||
# on Linux | ||
cp ./target/debug/libclipboard.so ../../ | ||
``` | ||
|
||
### Case B: Use the existing build files | ||
|
||
Coming soon... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2021 ippee | ||
// This source code is under the MIT License. | ||
// See http://opensource.org/licenses/mit-license.php | ||
|
||
extern crate clipboard; | ||
|
||
use clipboard::ClipboardProvider; | ||
use clipboard::ClipboardContext; | ||
|
||
use std::ffi::CStr; | ||
use std::ffi::CString; | ||
use std::os::raw::c_char; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn get_contents() -> *const c_char { | ||
let mut ctx: ClipboardContext = ClipboardProvider::new().expect("ClipboardProvider::new failed"); | ||
|
||
match ctx.get_contents() { | ||
Ok(v) => { | ||
let cstring = CString::new(v).expect("CString::new failed"); | ||
return cstring.into_raw(); | ||
}, | ||
Err(_e) => panic!("Failed to get the clipboard contents"), | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn set_contents(data: *const c_char) { | ||
let cstr = unsafe { CStr::from_ptr(data) }; | ||
let rstr = cstr.to_str().expect("Failed to cast of CStr to &str"); | ||
|
||
let mut ctx: ClipboardContext = ClipboardProvider::new().expect("ClipboardProvider::new failed"); | ||
ctx.set_contents(rstr.to_owned()).expect("ctx.set_contents failed"); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{get_contents, set_contents}; | ||
use std::ffi::CStr; | ||
use std::ffi::CString; | ||
|
||
#[test] | ||
fn set_get_clipboard_contents() { | ||
let contents = CString::new("All the world's a stage").expect("CString::new failed"); | ||
let expected_ptr = contents.into_raw(); | ||
|
||
set_contents(expected_ptr); | ||
let actual_ptr = get_contents(); | ||
|
||
let expected = unsafe { CStr::from_ptr(expected_ptr) }; | ||
let actual = unsafe { CStr::from_ptr(actual_ptr) }; | ||
|
||
assert_eq!(actual, expected); | ||
} | ||
} |