niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·66583cd·Ivan Molodetskikh·1 year ago·2489 commits
| 1 | use std::os::fd::AsFd as _; |
| 2 | use std::os::unix::net::UnixStream; |
| 3 | use std::sync::atomic::Ordering; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use calloop::generic::Generic; |
| 7 | use calloop::{EventLoop, Interest, LoopHandle, Mode, PostAction}; |
| 8 | use niri_config::Config; |
| 9 | use smithay::output::Output; |
| 10 | |
| 11 | use super::client::{Client, ClientId}; |
| 12 | use super::server::Server; |
| 13 | use crate::niri::{NewClient, Niri}; |
| 14 | |
| 15 | pub struct Fixture { |
| 16 | pub event_loop: EventLoop<'static, State>, |
| 17 | pub handle: LoopHandle<'static, State>, |
| 18 | pub state: State, |
| 19 | } |
| 20 | |
| 21 | pub struct State { |
| 22 | pub server: Server, |
| 23 | pub clients: Vec<Client>, |
| 24 | } |
| 25 | |
| 26 | impl 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 niri_complete_animations(&mut self) { |
| 86 | let niri = self.niri(); |
| 87 | niri.clock.set_complete_instantly(true); |
| 88 | niri.advance_animations(); |
| 89 | niri.clock.set_complete_instantly(false); |
| 90 | } |
| 91 | |
| 92 | pub fn add_output(&mut self, n: u8, size: (u16, u16)) { |
| 93 | let state = self.niri_state(); |
| 94 | let niri = &mut state.niri; |
| 95 | state.backend.headless().add_output(niri, n, size); |
| 96 | } |
| 97 | |
| 98 | pub fn add_client(&mut self) -> ClientId { |
| 99 | let (sock1, sock2) = UnixStream::pair().unwrap(); |
| 100 | self.niri().insert_client(NewClient { |
| 101 | client: sock1, |
| 102 | restricted: false, |
| 103 | credentials_unknown: false, |
| 104 | }); |
| 105 | |
| 106 | let client = Client::new(sock2); |
| 107 | let id = client.id; |
| 108 | |
| 109 | let fd = client.event_loop.as_fd().try_clone_to_owned().unwrap(); |
| 110 | let source = Generic::new(fd, Interest::READ, Mode::Level); |
| 111 | self.handle |
| 112 | .insert_source(source, move |_, _, state: &mut State| { |
| 113 | state.client(id).dispatch(); |
| 114 | Ok(PostAction::Continue) |
| 115 | }) |
| 116 | .unwrap(); |
| 117 | |
| 118 | self.state.clients.push(client); |
| 119 | self.roundtrip(id); |
| 120 | id |
| 121 | } |
| 122 | |
| 123 | pub fn client(&mut self, id: ClientId) -> &mut Client { |
| 124 | self.state.client(id) |
| 125 | } |
| 126 | |
| 127 | pub fn roundtrip(&mut self, id: ClientId) { |
| 128 | let client = self.state.client(id); |
| 129 | let data = client.send_sync(); |
| 130 | while !data.done.load(Ordering::Relaxed) { |
| 131 | self.dispatch(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Roundtrip twice in a row. |
| 136 | /// |
| 137 | /// For some reason, when running tests on many threads at once, a single roundtrip is |
| 138 | /// sometimes not sufficient to get the configure events to the client. |
| 139 | /// |
| 140 | /// I suspect that this is because these configure events are sent from the niri loop callback, |
| 141 | /// so they arrive after the sync done event and don't get processed in that client dispatch |
| 142 | /// cycle. I'm not sure why this would be dependent on multithreading. But if this is indeed |
| 143 | /// the issue, then a double roundtrip fixes it. |
| 144 | pub fn double_roundtrip(&mut self, id: ClientId) { |
| 145 | self.roundtrip(id); |
| 146 | self.roundtrip(id); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl State { |
| 151 | pub fn client(&mut self, id: ClientId) -> &mut Client { |
| 152 | self.clients.iter_mut().find(|c| c.id == id).unwrap() |
| 153 | } |
| 154 | } |