forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 commits

fixture.rs147 lines, 4.3 KiBHistoryRawDownload
1use std::os::fd::AsFd as _;
2use std::os::unix::net::UnixStream;
3use std::sync::atomic::Ordering;
4use std::time::Duration;
5
6use calloop::generic::Generic;
7use calloop::{EventLoop, Interest, LoopHandle, Mode, PostAction};
8use niri_config::Config;
9use smithay::output::Output;
10
11use super::client::{Client, ClientId};
12use super::server::Server;
13use crate::niri::{NewClient, Niri};
14
15pub struct Fixture {
16 pub event_loop: EventLoop<'static, State>,
17 pub handle: LoopHandle<'static, State>,
18 pub state: State,
19}
20
21pub struct State {
22 pub server: Server,
23 pub clients: Vec<Client>,
24}
25
26impl Fixture {
27 pub fn new() -> Self {
28 Self::with_config(Config::default())
29 }
30
31 pub fn with_config(config: Config) -> Self {
32 let event_loop = EventLoop::try_new().unwrap();
33 let handle = event_loop.handle();
34
35 let server = Server::new(config);
36 let fd = server.event_loop.as_fd().try_clone_to_owned().unwrap();
37 let source = Generic::new(fd, Interest::READ, Mode::Level);
38 handle
39 .insert_source(source, |_, _, state: &mut State| {
40 state.server.dispatch();
41 Ok(PostAction::Continue)
42 })
43 .unwrap();
44
45 let state = State {
46 server,
47 clients: Vec::new(),
48 };
49
50 Self {
51 event_loop,
52 handle,
53 state,
54 }
55 }
56
57 pub fn dispatch(&mut self) {
58 self.event_loop
59 .dispatch(Duration::ZERO, &mut self.state)
60 .unwrap();
61 }
62
63 pub fn niri_state(&mut self) -> &mut crate::niri::State {
64 &mut self.state.server.state
65 }
66
67 pub fn niri(&mut self) -> &mut Niri {
68 &mut self.niri_state().niri
69 }
70
71 pub fn niri_output(&self, n: u8) -> Output {
72 let niri = &self.state.server.state.niri;
73 let idx = usize::from(n - 1);
74 let output = niri.global_space.outputs().nth(idx).unwrap();
75 output.clone()
76 }
77
78 pub fn niri_focus_output(&mut self, n: u8) {
79 let niri = &mut self.state.server.state.niri;
80 let idx = usize::from(n - 1);
81 let output = niri.global_space.outputs().nth(idx).unwrap();
82 niri.layout.focus_output(output);
83 }
84
85 pub fn add_output(&mut self, n: u8, size: (u16, u16)) {
86 let state = self.niri_state();
87 let niri = &mut state.niri;
88 state.backend.headless().add_output(niri, n, size);
89 }
90
91 pub fn add_client(&mut self) -> ClientId {
92 let (sock1, sock2) = UnixStream::pair().unwrap();
93 self.niri().insert_client(NewClient {
94 client: sock1,
95 restricted: false,
96 credentials_unknown: false,
97 });
98
99 let client = Client::new(sock2);
100 let id = client.id;
101
102 let fd = client.event_loop.as_fd().try_clone_to_owned().unwrap();
103 let source = Generic::new(fd, Interest::READ, Mode::Level);
104 self.handle
105 .insert_source(source, move |_, _, state: &mut State| {
106 state.client(id).dispatch();
107 Ok(PostAction::Continue)
108 })
109 .unwrap();
110
111 self.state.clients.push(client);
112 self.roundtrip(id);
113 id
114 }
115
116 pub fn client(&mut self, id: ClientId) -> &mut Client {
117 self.state.client(id)
118 }
119
120 pub fn roundtrip(&mut self, id: ClientId) {
121 let client = self.state.client(id);
122 let data = client.send_sync();
123 while !data.done.load(Ordering::Relaxed) {
124 self.dispatch();
125 }
126 }
127
128 /// Roundtrip twice in a row.
129 ///
130 /// For some reason, when running tests on many threads at once, a single roundtrip is
131 /// sometimes not sufficient to get the configure events to the client.
132 ///
133 /// I suspect that this is because these configure events are sent from the niri loop callback,
134 /// so they arrive after the sync done event and don't get processed in that client dispatch
135 /// cycle. I'm not sure why this would be dependent on multithreading. But if this is indeed
136 /// the issue, then a double roundtrip fixes it.
137 pub fn double_roundtrip(&mut self, id: ClientId) {
138 self.roundtrip(id);
139 self.roundtrip(id);
140 }
141}
142
143impl State {
144 pub fn client(&mut self, id: ClientId) -> &mut Client {
145 self.clients.iter_mut().find(|c| c.id == id).unwrap()
146 }
147}