summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-17 15:28:34 -0500
committerpommicket <pommicket@gmail.com>2022-12-17 15:28:34 -0500
commitc54c5900588eb682f206aa5b92156dd868b9cd32 (patch)
treea47ef5d7f216f185560526ef6299313192d01027
parentb0c8e4cef46c752bdf4ed83361197eb592d269e3 (diff)
clipboard api
-rw-r--r--Cargo.toml6
-rw-r--r--src/main.rs2
-rw-r--r--src/sdl.rs36
-rw-r--r--src/win.rs8
4 files changed, 48 insertions, 4 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 0d3cd5d..aff3626 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,11 +6,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-gl = "0.14"
-nalgebra = "0.31"
gen_random_proc_macro = { path = "./gen_random_proc_macro" }
gen_random = { path = "./gen_random" }
-rand = { version = "0.8", features = ["small_rng"] }
+rand = { version = "0.8.0", features = ["small_rng"] }
serde_cbor = "0.11.2"
serde = "1.0.150"
serde_derive = "1.0.150"
+gl = "0.14.0"
+nalgebra = "0.31.4"
diff --git a/src/main.rs b/src/main.rs
index 346331c..f010914 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -295,7 +295,7 @@ fn try_main() -> Result<(), String> {
let mut window = win::Window::new("AutoSDF", 1280, 720, true)
.map_err(|e| format!("Error creating window: {e}"))?;
let mut program = window.new_program();
- gen_program(&mut window, &mut program)?;
+ gen_program_with_seed(&mut window, &mut program, 1)?;
//gen_program_from_string(&mut window, &mut program, "a263736466a167436f6d706f736583a1695472616e736c61746583a163463332fa3ea4c00ca163463332fa3e85dc00a163463332fa3f2bbdaea167436f6d706f736583a166526f7461746583a163463332fa3f750dc2a163463332fa3f5a7f0ea163463332fa3f2df98ca1634d696e82a167436f6d706f736583a167436f6d706f736582a16353696ea163463332fa3f7cc2a0a167436f6d706f736582684964656e74697479684964656e74697479a166537068657265a163463332fa3f26f8f6684964656e74697479a167436f6d706f736583a166526f7461746583a163463332fa3f1bfed8a163463332fa3f1e1e30a163463332fa3eddc6b0a1634d697883a167436f6d706f736583684964656e74697479a166537068657265a163463332fa3ea149ec684964656e74697479a167436f6d706f736583684964656e74697479a166537068657265a163463332fa3f6b0018684964656e74697479a163463332fa3e60a8d8684964656e74697479684964656e74697479684964656e746974796e636f6c6f725f66756e6374696f6ea165537153696ea163463332fa3ebaa7ec")?;
let mut buffer = window.create_buffer();
diff --git a/src/sdl.rs b/src/sdl.rs
index 00d90b2..05a647a 100644
--- a/src/sdl.rs
+++ b/src/sdl.rs
@@ -1,6 +1,8 @@
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#![allow(non_snake_case)]
+/// this module provides SDL type definitions, and more rust-y wrappers around
+/// SDL functions.
use std::ffi::{c_char, c_float, c_int, c_void, CStr, CString};
use std::mem;
@@ -699,6 +701,9 @@ extern "C" {
) -> SDL_AudioDeviceID;
fn SDL_PauseAudioDevice(dev: SDL_AudioDeviceID, pause_on: c_int);
fn SDL_CloseAudioDevice(dev: SDL_AudioDeviceID);
+ fn SDL_GetClipboardText() -> *mut c_char;
+ fn SDL_SetClipboardText(text: *const c_char) -> c_int;
+ fn SDL_free(mem: *mut c_void);
}
pub mod scancode {
@@ -1120,3 +1125,34 @@ pub unsafe fn pause_audio_device(dev: SDL_AudioDeviceID, pause_on: bool) {
pub unsafe fn close_audio_device(dev: SDL_AudioDeviceID) {
SDL_CloseAudioDevice(dev);
}
+
+pub unsafe fn get_clipboard_text() -> Result<String, String> {
+ let ptr = SDL_GetClipboardText();
+ if ptr.is_null() {
+ // according to some sources, SDL_GetClipboardText returns NULL on failure.
+ // according to the wiki, it returns an empty string.
+ return Err(get_err());
+ }
+
+ let cstr = CStr::from_ptr(ptr);
+ let Ok(r#str) = cstr.to_str() else {
+ // this should never happen since SDL should always give back valid UTF-8.
+ return Err("clipboard doesn't contain UTF-8".to_string());
+ };
+ let string = r#str.to_string();
+ SDL_free(ptr as *mut c_void);
+ Ok(string)
+}
+
+pub unsafe fn set_clipboard_text(s: &str) -> Result<(), String> {
+ let Ok(cstring) = CString::new(s) else {
+ return Err("can't put null bytes in clipboard text.".to_string());
+ };
+
+ let result = SDL_SetClipboardText(cstring.as_ptr());
+ if result == 0 {
+ Ok(())
+ } else {
+ Err(get_err())
+ }
+}
diff --git a/src/win.rs b/src/win.rs
index eab7b8f..393faee 100644
--- a/src/win.rs
+++ b/src/win.rs
@@ -1325,6 +1325,14 @@ impl Window {
pub fn swap(&mut self) {
unsafe { sdl::gl_swap_window(self.sdlwin) };
}
+
+ pub fn get_clipboard_text(&mut self) -> Result<String, String> {
+ unsafe { sdl::get_clipboard_text() }
+ }
+
+ pub fn set_clipboard_text(&mut self, s: &str) -> Result<(), String> {
+ unsafe { sdl::set_clipboard_text(s) }
+ }
}
impl Drop for Window {