refactor code + add DiyaglWayland struct

This commit is contained in:
DL
2026-04-02 18:39:02 +02:00
parent 38391e1d28
commit 2f8d9d9b3a
5 changed files with 109 additions and 77 deletions

2
src/core.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod protocols;
pub mod wayland;

View File

@@ -29,13 +29,17 @@ pub mod wl_foreign_toplevel_management {
pub mod __interfaces {
// import the interfaces from the core protocol if needed
use wayland_client::protocol::__interfaces::*;
wayland_scanner::generate_interfaces!("./protocols/wlr-foreign-toplevel-management-unstable-v1.xml");
wayland_scanner::generate_interfaces!(
"./protocols/wlr-foreign-toplevel-management-unstable-v1.xml"
);
}
use self::__interfaces::*;
// This macro generates the actual types that represent the wayland objects of
// your custom protocol
wayland_scanner::generate_client_code!("./protocols/wlr-foreign-toplevel-management-unstable-v1.xml");
wayland_scanner::generate_client_code!(
"./protocols/wlr-foreign-toplevel-management-unstable-v1.xml"
);
}
pub mod wl_output_power_management {
@@ -49,13 +53,17 @@ pub mod wl_output_power_management {
pub mod __interfaces {
// import the interfaces from the core protocol if needed
use wayland_client::protocol::__interfaces::*;
wayland_scanner::generate_interfaces!("./protocols/wlr-output-power-management-unstable-v1.xml");
wayland_scanner::generate_interfaces!(
"./protocols/wlr-output-power-management-unstable-v1.xml"
);
}
use self::__interfaces::*;
// This macro generates the actual types that represent the wayland objects of
// your custom protocol
wayland_scanner::generate_client_code!("./protocols/wlr-output-power-management-unstable-v1.xml");
wayland_scanner::generate_client_code!(
"./protocols/wlr-output-power-management-unstable-v1.xml"
);
}
pub mod wl_screencopy {
@@ -76,4 +84,4 @@ pub mod wl_screencopy {
// This macro generates the actual types that represent the wayland objects of
// your custom protocol
wayland_scanner::generate_client_code!("./protocols/wlr-screencopy-unstable-v1.xml");
}
}

87
src/core/wayland.rs Normal file
View File

@@ -0,0 +1,87 @@
use wayland_client::{protocol::{wl_registry, wl_compositor, wl_shm, wl_seat}, Connection, Dispatch, QueueHandle};
use crate::core::protocols::{wl_foreign_toplevel_management};
// use wayland_protocols::ext::{idle_notify};
pub struct DiyaglWayland {
compositor: Option<wl_compositor::WlCompositor>,
shm: Option<wl_shm::WlShm>,
seat: Option<wl_seat::WlSeat>,
foreign_toplevel_management: Option<wl_foreign_toplevel_management::zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1>,
// idle_notifier: Option<idle_notify::v1>,
}
// Implement `Dispatch<WlRegistry, ()> for our state. This provides the logic
// to be able to process events for the wl_registry interface.
//
// The second type parameter is the user-data of our implementation. It is a
// mechanism that allows you to associate a value to each particular Wayland
// object, and allow different dispatching logic depending on the type of the
// associated value.
//
// In this example, we just use () as we don't have any value to associate. See
// the `Dispatch` documentation for more details about this.
impl Dispatch<wl_registry::WlRegistry, ()> for DiyaglWayland {
fn event(
_state: &mut Self,
_: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &Connection,
_: &QueueHandle<DiyaglWayland>,
) {
// When receiving events from the wl_registry, we are only interested in the
// `global` event, which signals a new available global.
// When receiving this event, we just print its characteristics in this example.
if let wl_registry::Event::Global {
name,
interface,
version,
} = event
{
println!("[{}] {} (v{})", name, interface, version);
}
}
}
impl DiyaglWayland {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
// Create a Wayland connection by connecting to the server through the
// environment-provided configuration.
let connection = Connection::connect_to_env()?;
// Retrieve the WlDisplay Wayland object from the connection. This object is
// the starting point of any Wayland program, from which all other objects will
// be created.
let display = connection.display();
// Create an event queue for our event processing
let mut event_queue = connection.new_event_queue();
// And get its handle to associate new objects to it
let qh = event_queue.handle();
// Create a wl_registry object by sending the wl_display.get_registry request.
// This method takes two arguments: a handle to the queue that the newly created
// wl_registry will be assigned to, and the user-data that should be associated
// with this registry (here it is () as we don't need user-data).
let _registry = display.get_registry(&qh, ());
// At this point everything is ready, and we just need to wait to receive the events
// from the wl_registry. Our callback will print the advertised globals.
println!("Advertised globals:");
// To actually receive the events, we invoke the `roundtrip` method. This method
// is special and you will generally only invoke it during the setup of your program:
// it will block until the server has received and processed all the messages you've
// sent up to now.
//
// In our case, that means it'll block until the server has received our
// wl_display.get_registry request, and as a reaction has sent us a batch of
// wl_registry.global events.
//
// `roundtrip` will then empty the internal buffer of the queue it has been invoked
// on, and thus invoke our `Dispatch` implementation that prints the list of advertised
// globals.
let mut object = Self {
compositor: None,
shm: None,
seat: None,
foreign_toplevel_management: None,
};
event_queue.roundtrip(&mut object)?;
Ok(object)
}
}

View File

@@ -1 +1 @@
pub mod protocols;
pub mod core;

View File

@@ -1,74 +1,9 @@
// use diyagl_rs::protocols;
use wayland_client::{protocol::wl_registry, Connection, Dispatch, QueueHandle};
// This struct represents the state of our app. This simple app does not
// need any state, but this type still supports the `Dispatch` implementations.
struct AppData;
// Implement `Dispatch<WlRegistry, ()> for our state. This provides the logic
// to be able to process events for the wl_registry interface.
//
// The second type parameter is the user-data of our implementation. It is a
// mechanism that allows you to associate a value to each particular Wayland
// object, and allow different dispatching logic depending on the type of the
// associated value.
//
// In this example, we just use () as we don't have any value to associate. See
// the `Dispatch` documentation for more details about this.
impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
fn event(
_state: &mut Self,
_: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &Connection,
_: &QueueHandle<AppData>,
) {
// When receiving events from the wl_registry, we are only interested in the
// `global` event, which signals a new available global.
// When receiving this event, we just print its characteristics in this example.
if let wl_registry::Event::Global { name, interface, version } = event {
println!("[{}] {} (v{})", name, interface, version);
}
}
}
// The main function of our program
fn main() {
// Create a Wayland connection by connecting to the server through the
// environment-provided configuration.
let conn = Connection::connect_to_env().unwrap();
// Retrieve the WlDisplay Wayland object from the connection. This object is
// the starting point of any Wayland program, from which all other objects will
// be created.
let display = conn.display();
// Create an event queue for our event processing
let mut event_queue = conn.new_event_queue();
// And get its handle to associate new objects to it
let qh = event_queue.handle();
// Create a wl_registry object by sending the wl_display.get_registry request.
// This method takes two arguments: a handle to the queue that the newly created
// wl_registry will be assigned to, and the user-data that should be associated
// with this registry (here it is () as we don't need user-data).
let _registry = display.get_registry(&qh, ());
// At this point everything is ready, and we just need to wait to receive the events
// from the wl_registry. Our callback will print the advertised globals.
println!("Advertised globals:");
// To actually receive the events, we invoke the `roundtrip` method. This method
// is special and you will generally only invoke it during the setup of your program:
// it will block until the server has received and processed all the messages you've
// sent up to now.
//
// In our case, that means it'll block until the server has received our
// wl_display.get_registry request, and as a reaction has sent us a batch of
// wl_registry.global events.
//
// `roundtrip` will then empty the internal buffer of the queue it has been invoked
// on, and thus invoke our `Dispatch` implementation that prints the list of advertised
// globals.
event_queue.roundtrip(&mut AppData).unwrap();
}
// We just create a new instance of our Wayland state, which will print the
// advertised globals when created.
print!("Creating Wayland state... ");
let _data = diyagl_rs::core::wayland::DiyaglWayland::new().unwrap();
println!("Done!");
}