Skip to content

Commit 5ccf2d4

Browse files
authored
Update main.rs
1 parent 00f77ba commit 5ccf2d4

1 file changed

Lines changed: 119 additions & 91 deletions

File tree

source-code/backend/src/main.rs

Lines changed: 119 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,66 @@
1-
use smithay::{
2-
backend::{
3-
renderer::{
4-
gles2::Gles2Renderer,
5-
utils::on_commit_buffer_handler,
6-
},
7-
winit::{self, WinitEvent},
8-
},
9-
delegate_compositor, delegate_seat, delegate_shm, delegate_xdg_shell,
10-
input::{
11-
pointer::{CursorImageStatus, PointerHandler},
12-
Seat, SeatHandler, SeatState,
13-
},
14-
reexports::{
15-
wayland_server::{
16-
backend::{ClientData, ClientId, DisconnectReason},
17-
protocol::{wl_buffer, wl_seat, wl_surface},
18-
Display, DisplayHandle, Resource,
19-
},
20-
},
21-
utils::{Logical, Point, Rectangle, Serial, Size, Transform},
22-
wayland::{
23-
buffer::BufferHandler,
24-
compositor::{
25-
with_states, CompositorClientState, CompositorHandler, CompositorState, TraversalAction,
26-
},
27-
shell::xdg::{
28-
PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler, XdgShellState,
29-
},
30-
shm::{ShmHandler, ShmState},
31-
},
1+
use smithay::backend::renderer::gles::GlesRenderer;
2+
use smithay::backend::renderer::utils::on_commit_buffer_handler;
3+
use smithay::backend::renderer::{Frame, Renderer};
4+
use smithay::backend::winit::{self, WinitEvent};
5+
use smithay::reexports::winit::platform::pump_events::PumpStatus;
6+
use smithay::delegate_compositor;
7+
use smithay::delegate_seat;
8+
use smithay::delegate_shm;
9+
use smithay::delegate_xdg_shell;
10+
use smithay::input::pointer::{CursorImageStatus};
11+
use smithay::input::{Seat, SeatHandler, SeatState};
12+
use smithay::reexports::wayland_server::backend::{ClientData, ClientId, DisconnectReason};
13+
use smithay::reexports::wayland_server::protocol::{wl_buffer, wl_seat, wl_surface};
14+
use smithay::reexports::wayland_server::{Display, DisplayHandle, ListeningSocket};
15+
use smithay::utils::{Rectangle, Transform, Size};
16+
use smithay::utils::Serial;
17+
use smithay::wayland::buffer::BufferHandler;
18+
use smithay::wayland::compositor::{
19+
with_states, CompositorClientState, CompositorHandler, CompositorState,
3220
};
21+
use smithay::wayland::shell::xdg::{
22+
PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler, XdgShellState,
23+
};
24+
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::State as XdgState;
25+
use smithay::wayland::shm::{ShmHandler, ShmState};
3326
use std::sync::Arc;
3427

35-
struct AppState {
36-
compositor_state: CompositorState,
37-
xdg_shell_state: XdgShellState,
38-
shm_state: ShmState,
39-
seat_state: SeatState<AppState>,
40-
seat: Seat<AppState>,
41-
42-
// Track windows
43-
windows: Vec<ToplevelSurface>,
28+
pub struct AppState {
29+
pub compositor_state: CompositorState,
30+
pub xdg_shell_state: XdgShellState,
31+
pub shm_state: ShmState,
32+
pub seat_state: SeatState<AppState>,
33+
pub seat: Seat<AppState>,
34+
pub windows: Vec<ToplevelSurface>,
4435
}
4536

4637
impl AppState {
47-
fn new(dh: &DisplayHandle) -> Self {
38+
pub fn new(dh: &DisplayHandle) -> Self {
4839
let mut seat_state = SeatState::new();
49-
let seat = seat_state.new_wl_seat(dh, "winit-seat");
40+
let _seat = seat_state.new_wl_seat(dh, "winit-seat");
5041

5142
Self {
5243
compositor_state: CompositorState::new::<Self>(dh),
5344
xdg_shell_state: XdgShellState::new::<Self>(dh),
5445
shm_state: ShmState::new::<Self>(dh, vec![]),
5546
seat_state,
56-
seat,
47+
seat: _seat,
5748
windows: Vec::new(),
5849
}
5950
}
6051
}
6152

62-
// --- Handler Implementations ---
53+
// --- Handlery ---
6354

6455
impl CompositorHandler for AppState {
6556
fn compositor_state(&mut self) -> &mut CompositorState {
6657
&mut self.compositor_state
6758
}
59+
6860
fn client_compositor_state<'a>(&self, client: &'a smithay::reexports::wayland_server::Client) -> &'a CompositorClientState {
6961
&client.get_data::<ClientState>().unwrap().compositor_state
7062
}
63+
7164
fn commit(&mut self, surface: &wl_surface::WlSurface) {
7265
on_commit_buffer_handler::<Self>(surface);
7366
}
@@ -77,29 +70,32 @@ impl XdgShellHandler for AppState {
7770
fn xdg_shell_state(&mut self) -> &mut XdgShellState {
7871
&mut self.xdg_shell_state
7972
}
73+
8074
fn new_toplevel(&mut self, surface: ToplevelSurface) {
8175
surface.with_pending_state(|state| {
82-
state.states.set(smithay::wayland::shell::xdg::ToplevelState::Activated);
76+
state.states.set(XdgState::Activated);
8377
});
8478
surface.send_configure();
8579
self.windows.push(surface);
8680
}
81+
8782
fn new_popup(&mut self, _surface: PopupSurface, _positioner: PositionerState) {}
8883
fn grab(&mut self, _surface: PopupSurface, _seat: wl_seat::WlSeat, _serial: Serial) {}
84+
85+
fn reposition_request(&mut self, _surface: PopupSurface, _positioner: PositionerState, _token: u32) {}
8986
}
9087

9188
impl SeatHandler for AppState {
9289
type KeyboardFocus = wl_surface::WlSurface;
9390
type PointerFocus = wl_surface::WlSurface;
91+
type TouchFocus = wl_surface::WlSurface;
9492

9593
fn seat_state(&mut self) -> &mut SeatState<AppState> {
9694
&mut self.seat_state
9795
}
98-
fn cursor_image(&mut self, _seat: &Seat<Self>, _image: CursorImageStatus) {}
99-
}
10096

101-
impl PointerHandler for AppState {
102-
fn pointer_frame(&mut self, _seat: &Seat<Self>, _event: &smithay::input::pointer::PointerFrameEvent) {}
97+
fn cursor_image(&mut self, _seat: &Seat<Self>, _image: CursorImageStatus) {}
98+
fn focus_changed(&mut self, _seat: &Seat<Self>, _focused: Option<&Self::KeyboardFocus>) {}
10399
}
104100

105101
impl ShmHandler for AppState {
@@ -112,86 +108,118 @@ impl BufferHandler for AppState {
112108
fn buffer_destroyed(&mut self, _buffer: &wl_buffer::WlBuffer) {}
113109
}
114110

115-
// --- Glue Code ---
111+
// --- Struktura Klienta ---
116112

117-
struct ClientState {
118-
compositor_state: CompositorClientState,
113+
pub struct ClientState {
114+
pub compositor_state: CompositorClientState,
119115
}
116+
120117
impl ClientData for ClientState {
121118
fn initialized(&self, _client_id: ClientId) {}
122119
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) {}
123120
}
124121

122+
// --- Makra Delegate ---
125123
delegate_compositor!(AppState);
126124
delegate_xdg_shell!(AppState);
127125
delegate_shm!(AppState);
128126
delegate_seat!(AppState);
129127

130128
fn main() {
131129
if let Err(_) = std::env::var("RUST_LOG") {
132-
std::env::set_var("RUST_LOG", "info");
130+
std::env::set_var("RUST_LOG", "info,smithay=info");
133131
}
134132
env_logger::init();
135133

136-
log::info!("Starting Blue Environment Compositor (Smithay Powered)...");
134+
log::info!("Starting Blue Environment Compositor...");
137135

136+
// 1. Inicjalizacja Display (Wayland Server)
138137
let mut display: Display<AppState> = Display::new().unwrap();
139138
let dh = display.handle();
140139

141-
let (mut backend, mut winit) = winit::init::<Gles2Renderer>().unwrap();
140+
// 2. Inicjalizacja Winit (Backend)
141+
// Jawne podanie typu renderera pomaga w inferencji.
142+
let (mut backend, mut winit) = winit::init::<GlesRenderer>().unwrap();
142143

143144
let mut state = AppState::new(&dh);
144145

145-
// Listen on Wayland socket
146-
let socket = display.add_socket_auto().unwrap();
147-
log::info!("Listening on wayland socket: {:?}", socket.into_string());
146+
// Socket
147+
let listening_socket = ListeningSocket::bind_auto("wayland", 1..32).unwrap();
148+
let socket_name = listening_socket.socket_name().unwrap().to_string_lossy().into_owned();
149+
log::info!("Listening on wayland socket: {:?}", socket_name);
148150

149151
loop {
150-
// 1. Dispatch Wayland clients
152+
// Accept connection
153+
if let Ok(Some(stream)) = listening_socket.accept() {
154+
log::info!("New client connected");
155+
if let Err(e) = display.handle().insert_client(stream, Arc::new(ClientState {
156+
compositor_state: CompositorClientState::default(),
157+
})) {
158+
log::error!("Failed to insert client: {:?}", e);
159+
}
160+
}
161+
162+
// Dispatch Wayland
151163
if let Err(e) = display.dispatch_clients(&mut state) {
152164
log::error!("Dispatch error: {:?}", e);
153165
}
154166

155-
// 2. Dispatch Winit events (Input)
156-
winit.dispatch_new_events(|event| {
167+
// Dispatch Winit
168+
let res = winit.dispatch_new_events(|event| {
157169
match event {
158170
WinitEvent::Resized { size, .. } => {
159-
// Handle output resize
171+
log::debug!("Resized to {:?}", size);
160172
}
161-
WinitEvent::Input(event) => {
162-
// In a full implementation, pass input to Seat
173+
WinitEvent::Input(_event) => {
174+
// Placeholder input handling
163175
}
164176
_ => {},
165177
};
166-
}).unwrap();
167-
168-
// 3. Render Loop
169-
backend.bind().unwrap();
170-
171-
let renderer = backend.renderer();
172-
173-
// Clear background to Blue-ish
174-
use smithay::backend::renderer::Frame;
175-
let mut frame = renderer.render(
176-
(800, 600).into(),
177-
Transform::Normal,
178-
).unwrap();
179-
180-
frame.clear(
181-
[0.1, 0.1, 0.2, 1.0], // Dark Blue
182-
&[Rectangle::from_loc_and_size((0, 0), (800, 600))]
183-
).unwrap();
184-
185-
// Draw windows
186-
for window in &state.windows {
187-
let surface = window.wl_surface();
188-
with_states(surface, |states| {
189-
// Render logic would go here using `smithay::backend::renderer::element::surface`
190-
});
178+
});
179+
180+
// Obsługa PumpStatus
181+
match res {
182+
PumpStatus::Exit(_) => break,
183+
PumpStatus::Continue => {},
191184
}
192185

193-
frame.finish().unwrap();
186+
// Renderowanie
187+
// Blok { ... } jest konieczny, aby zwolnić pożyczkę (borrow) 'backend' przed wywołaniem 'submit'.
188+
{
189+
// Pobieramy renderer i target z backend.bind().
190+
// bind() zwraca krotkę (&mut Renderer, Target).
191+
let (renderer, mut target) = match backend.bind() {
192+
Ok(res) => res,
193+
Err(e) => {
194+
log::error!("Failed to bind backend: {:?}", e);
195+
continue;
196+
}
197+
};
198+
199+
// Renderujemy używając pobranego renderera.
200+
let mut frame = renderer.render(
201+
&mut target,
202+
Size::from((800, 600)),
203+
Transform::Normal,
204+
).unwrap();
205+
206+
frame.clear(
207+
[0.1, 0.1, 0.4, 1.0].into(),
208+
&[Rectangle::new((0, 0).into(), (800, 600).into())]
209+
).unwrap();
210+
211+
for window in &state.windows {
212+
let surface = window.wl_surface();
213+
with_states(surface, |_states| {
214+
// Render logic placeholder
215+
});
216+
}
217+
218+
frame.finish().unwrap();
219+
}
194220

195-
backend.submit(None).unwrap();
221+
if let Err(e) = backend.submit(None) {
222+
log::error!("Failed to submit buffer: {:?}", e);
223+
}
196224
}
197225
}

0 commit comments

Comments
 (0)