Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ippee committed Jul 16, 2021
1 parent c02389c commit 8f2eef4
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
138 changes: 138 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Cargo.toml
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"
30 changes: 30 additions & 0 deletions README.md
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...
55 changes: 55 additions & 0 deletions src/lib.rs
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);
}
}

0 comments on commit 8f2eef4

Please sign in to comment.