From 2f8d9d9b3ab299c4b20df9e1a498cdcb15cfc3e0 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Thu, 2 Apr 2026 18:39:02 +0200 Subject: [PATCH] refactor code + add DiyaglWayland struct --- src/core.rs | 2 + src/{ => core}/protocols.rs | 18 +++++--- src/core/wayland.rs | 87 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/main.rs | 77 +++----------------------------- 5 files changed, 109 insertions(+), 77 deletions(-) create mode 100644 src/core.rs rename src/{ => core}/protocols.rs (85%) create mode 100644 src/core/wayland.rs diff --git a/src/core.rs b/src/core.rs new file mode 100644 index 0000000..cc9d857 --- /dev/null +++ b/src/core.rs @@ -0,0 +1,2 @@ +pub mod protocols; +pub mod wayland; diff --git a/src/protocols.rs b/src/core/protocols.rs similarity index 85% rename from src/protocols.rs rename to src/core/protocols.rs index 936c3ea..b4a83d8 100644 --- a/src/protocols.rs +++ b/src/core/protocols.rs @@ -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"); -} \ No newline at end of file +} diff --git a/src/core/wayland.rs b/src/core/wayland.rs new file mode 100644 index 0000000..fb71509 --- /dev/null +++ b/src/core/wayland.rs @@ -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, + shm: Option, + seat: Option, + foreign_toplevel_management: Option, + // idle_notifier: Option, +} + +// Implement `Dispatch 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 for DiyaglWayland { + fn event( + _state: &mut Self, + _: &wl_registry::WlRegistry, + event: wl_registry::Event, + _: &(), + _: &Connection, + _: &QueueHandle, + ) { + // 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> { + // 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) + } +} diff --git a/src/lib.rs b/src/lib.rs index 534c207..5a7ca06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1 @@ -pub mod protocols; \ No newline at end of file +pub mod core; diff --git a/src/main.rs b/src/main.rs index 6b94beb..626efd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 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 for AppData { - fn event( - _state: &mut Self, - _: &wl_registry::WlRegistry, - event: wl_registry::Event, - _: &(), - _: &Connection, - _: &QueueHandle, - ) { - // 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(); -} \ No newline at end of file + // 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!"); +}