Skip to content

Commit

Permalink
[output.rs] Remove unsafe pointer operations.
Browse files Browse the repository at this point in the history
* Closes #2

Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
Shinyzenith committed Jun 21, 2022
1 parent 052114c commit 148fc11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

24 changes: 14 additions & 10 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use wayland_client::{protocol::wl_output, protocol::wl_output::WlOutput, Display

#[derive(Debug, Clone)]
pub struct OutputInfo {
pub wl_output: *mut WlOutput,
pub wl_output: WlOutput,
pub name: String,
}

Expand All @@ -12,12 +12,13 @@ pub fn get_all_outputs(display: Display) -> Vec<OutputInfo> {
let mut event_queue = display.create_event_queue();
let attached_display = (*display).clone().attach(event_queue.token());

let outputs: Rc<RefCell<Vec<OutputInfo>>> = Rc::new(RefCell::new(Vec::new()));

// Instantiating the global manager.
let globals = GlobalManager::new(&attached_display);
event_queue.sync_roundtrip(&mut (), |_, _, _| {}).unwrap();

let outputs: Rc<RefCell<Vec<OutputInfo>>> = Rc::new(RefCell::new(Vec::new()));

// Fetch all outputs and it's name.
globals
.instantiate_exact::<WlOutput>(4)
.expect("Failed to bind to wl_output global.")
Expand All @@ -26,24 +27,27 @@ pub fn get_all_outputs(display: Display) -> Vec<OutputInfo> {
move |output, event, _| {
if let wl_output::Event::Name { name } = event {
outputs.borrow_mut().push(OutputInfo {
wl_output: &mut output.detach(),
wl_output: output.detach(),
name,
});
}
}
});
event_queue.sync_roundtrip(&mut (), |_, _, _| {}).unwrap();
let x = outputs.borrow().to_vec();
x

if outputs.borrow().is_empty() {
println!("Compositor did not advertise any wl_output devices!");
exit(1);
}
println!("Outputs detected: {:#?}", outputs);
outputs.take()
}

/// Get a wl_output object from the output name.
pub fn get_wloutput(name: String, outputs: Vec<OutputInfo>) -> &'static WlOutput {
pub fn get_wloutput(name: String, outputs: Vec<OutputInfo>) -> WlOutput {
for output in outputs {
if output.name == name {
unsafe {
return &*output.wl_output;
}
return output.wl_output;
}
}
println!("Error: No output of name \"{}\" was found", name);
Expand Down
7 changes: 2 additions & 5 deletions src/wayout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wayland_client::{protocol::wl_output::WlOutput, Display, GlobalManager};
use wayland_client::{Display, GlobalManager};
use wayland_protocols::wlr::unstable::output_power_management::v1::client::{
zwlr_output_power_manager_v1::ZwlrOutputPowerManagerV1, zwlr_output_power_v1,
zwlr_output_power_v1::Mode,
Expand Down Expand Up @@ -89,10 +89,7 @@ pub fn get_head_states() -> Vec<HeadState> {

for output in valid_outputs {
let output_name = output.name;
let output_ptr: &WlOutput;
unsafe {
output_ptr = &*output.wl_output as &WlOutput;
}
let output_ptr = &output.wl_output;
output_power_manager
.as_ref()
.unwrap()
Expand Down

0 comments on commit 148fc11

Please sign in to comment.