Skip to content

Commit

Permalink
Fix copy pasta for wayland #110
Browse files Browse the repository at this point in the history
  • Loading branch information
kepet19 authored and PureTryOut committed Sep 11, 2021
1 parent f5065b4 commit 0dcb82e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
39 changes: 39 additions & 0 deletions src/main.rs
Expand Up @@ -17,6 +17,11 @@
#![allow(clippy::many_single_char_names)] // short variable names provide concise clarity
#![allow(clippy::float_cmp)] // float comparison used to check if changed

use copypasta::nop_clipboard;
use copypasta::ClipboardContext;
use copypasta::ClipboardProvider;
#[cfg(target_os = "linux")]
use glutin::platform::unix::EventLoopWindowTargetExtUnix;
use instant::{Duration, Instant};
use log::{debug, error, info, warn};
use std::fs;
Expand Down Expand Up @@ -74,6 +79,7 @@ pub struct Game {
renderer: Arc<RwLock<render::Renderer>>,
screen_sys: screen::ScreenSystem,
resource_manager: Arc<RwLock<resources::Manager>>,
clipboard_provider: Arc<RwLock<Box<dyn copypasta::ClipboardProvider>>>,
console: Arc<Mutex<console::Console>>,
vars: Rc<console::Vars>,
should_close: bool,
Expand Down Expand Up @@ -293,6 +299,22 @@ fn main() {
opt.default_protocol_version
.unwrap_or_else(|| "".to_string()),
);

#[cfg(target_os = "linux")]
let clipboard: Box<dyn ClipboardProvider> = match events_loop.wayland_display() {
Some(display) => {
debug!("Configured with wayland clipboard");
// NOTE: Since this takes a pointer to the winit event loop, it MUST be dropped first.
unsafe {
Box::new(copypasta::wayland_clipboard::create_clipboards_from_external(display).1)
}
}
None => create_clipboard(),
};

#[cfg(not(target_os = "linux"))]
let clipboard = create_clipboard();

let game = Game {
server: None,
focused: false,
Expand All @@ -313,6 +335,7 @@ fn main() {
is_logo_pressed: false,
is_fullscreen: false,
default_protocol_version,
clipboard_provider: Arc::new(RwLock::new(clipboard)),
};
game.renderer.write().camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5);
if opt.network_debug {
Expand Down Expand Up @@ -782,3 +805,19 @@ fn handle_window_event<T>(

false
}

fn create_clipboard() -> Box<dyn ClipboardProvider> {
match ClipboardContext::new() {
Ok(clipboard) => {
debug!("Configured with normal clipboard");
Box::new(clipboard)
}
Err(_) => {
debug!("Could not create clipboard running with no operation clipboard");
Box::new(
nop_clipboard::NopClipboardContext::new()
.expect("no operation clipboard can never fail"),
)
}
}
}
11 changes: 4 additions & 7 deletions src/ui/mod.rs
Expand Up @@ -16,7 +16,6 @@ pub mod logo;

use crate::format;
use crate::render;
use copypasta::{ClipboardContext, ClipboardProvider};
use parking_lot::RwLock;
use std::cell::{RefCell, RefMut};
use std::rc::{Rc, Weak};
Expand Down Expand Up @@ -1619,12 +1618,10 @@ impl UIElement for TextBox {
}
self.submit_funcs.append(&mut temp);
}
(VirtualKeyCode::V, true) => {
if ctrl_pressed {
let mut clipboard: ClipboardContext = ClipboardContext::new().unwrap();
if let Ok(text) = clipboard.get_contents() {
self.input.push_str(&text)
}
(VirtualKeyCode::V, true) if ctrl_pressed => {
let mut clipboard = game.clipboard_provider.write();
if let Ok(text) = clipboard.get_contents() {
self.input.push_str(&text)
}
}
_ => {}
Expand Down

0 comments on commit 0dcb82e

Please sign in to comment.