2023-06-25 17:18:50 -05:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2023-06-25 17:49:06 -05:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2023-06-25 17:18:50 -05:00
|
|
|
|
2023-06-17 18:55:04 -05:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
2023-06-29 11:58:33 -05:00
|
|
|
ffi::OsString,
|
2023-06-17 18:55:04 -05:00
|
|
|
os::{fd::AsRawFd, unix::net::UnixStream},
|
2023-06-21 14:48:38 -05:00
|
|
|
process::Stdio,
|
2023-06-28 16:41:36 -05:00
|
|
|
sync::{Arc, Mutex},
|
2023-06-17 18:55:04 -05:00
|
|
|
};
|
2023-06-04 18:11:14 -05:00
|
|
|
|
2023-06-17 21:02:58 -05:00
|
|
|
use crate::{
|
2023-06-21 14:48:38 -05:00
|
|
|
api::{
|
2023-06-28 16:41:36 -05:00
|
|
|
msg::{Args, CallbackId, Msg, OutgoingMsg, Request, RequestResponse},
|
2023-06-21 14:48:38 -05:00
|
|
|
PinnacleSocketSource,
|
|
|
|
},
|
2023-06-17 21:02:58 -05:00
|
|
|
focus::FocusState,
|
2023-06-30 21:34:07 -05:00
|
|
|
window::{window_state::WindowState, WindowProperties}, output::OutputState, tag::{TagState, Tag}, layout::Layout,
|
2023-06-17 21:02:58 -05:00
|
|
|
};
|
2023-06-29 11:58:33 -05:00
|
|
|
use calloop::futures::Scheduler;
|
|
|
|
use futures_lite::AsyncBufReadExt;
|
2023-06-02 16:01:48 -05:00
|
|
|
use smithay::{
|
2023-06-09 20:29:17 -05:00
|
|
|
backend::renderer::element::RenderElementStates,
|
|
|
|
desktop::{
|
|
|
|
utils::{
|
|
|
|
surface_presentation_feedback_flags_from_states, surface_primary_scanout_output,
|
|
|
|
OutputPresentationFeedback,
|
|
|
|
},
|
|
|
|
PopupManager, Space, Window,
|
|
|
|
},
|
|
|
|
input::{keyboard::XkbConfig, pointer::CursorImageStatus, Seat, SeatState},
|
|
|
|
output::Output,
|
2023-06-02 16:01:48 -05:00
|
|
|
reexports::{
|
2023-06-17 18:55:04 -05:00
|
|
|
calloop::{
|
|
|
|
self, channel::Event, generic::Generic, Interest, LoopHandle, LoopSignal, Mode,
|
|
|
|
PostAction,
|
|
|
|
},
|
2023-06-02 16:01:48 -05:00
|
|
|
wayland_server::{
|
|
|
|
backend::{ClientData, ClientId, DisconnectReason},
|
|
|
|
Display,
|
|
|
|
},
|
|
|
|
},
|
2023-06-19 16:24:27 -05:00
|
|
|
utils::{Clock, Logical, Monotonic, Point},
|
2023-06-02 16:01:48 -05:00
|
|
|
wayland::{
|
2023-06-26 18:48:29 -05:00
|
|
|
compositor::{self, CompositorClientState, CompositorState},
|
2023-06-02 16:01:48 -05:00
|
|
|
data_device::DataDeviceState,
|
2023-06-09 20:29:17 -05:00
|
|
|
dmabuf::DmabufFeedback,
|
2023-06-04 18:11:14 -05:00
|
|
|
fractional_scale::FractionalScaleManagerState,
|
2023-06-02 16:01:48 -05:00
|
|
|
output::OutputManagerState,
|
2023-06-26 18:48:29 -05:00
|
|
|
shell::xdg::{XdgShellState, XdgToplevelSurfaceData},
|
2023-06-02 16:01:48 -05:00
|
|
|
shm::ShmState,
|
2023-06-09 20:29:17 -05:00
|
|
|
socket::ListeningSocketSource,
|
2023-06-04 18:11:14 -05:00
|
|
|
viewporter::ViewporterState,
|
2023-06-02 16:01:48 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-15 16:43:33 -05:00
|
|
|
use crate::{backend::Backend, input::InputState};
|
2023-06-02 16:01:48 -05:00
|
|
|
|
2023-06-21 18:58:49 -05:00
|
|
|
/// The main state of the application.
|
2023-06-02 16:01:48 -05:00
|
|
|
pub struct State<B: Backend> {
|
|
|
|
pub backend_data: B,
|
2023-06-07 12:12:54 -05:00
|
|
|
|
2023-06-02 16:01:48 -05:00
|
|
|
pub loop_signal: LoopSignal,
|
2023-06-09 20:29:17 -05:00
|
|
|
pub loop_handle: LoopHandle<'static, CalloopData<B>>,
|
2023-06-02 16:01:48 -05:00
|
|
|
pub clock: Clock<Monotonic>,
|
2023-06-07 12:12:54 -05:00
|
|
|
|
|
|
|
pub space: Space<Window>,
|
|
|
|
pub move_mode: bool,
|
2023-06-09 20:29:17 -05:00
|
|
|
pub socket_name: String,
|
|
|
|
|
|
|
|
pub seat: Seat<State<B>>,
|
2023-06-07 12:12:54 -05:00
|
|
|
|
2023-06-02 16:01:48 -05:00
|
|
|
pub compositor_state: CompositorState,
|
|
|
|
pub data_device_state: DataDeviceState,
|
|
|
|
pub seat_state: SeatState<Self>,
|
|
|
|
pub shm_state: ShmState,
|
|
|
|
pub output_manager_state: OutputManagerState,
|
|
|
|
pub xdg_shell_state: XdgShellState,
|
2023-06-04 18:11:14 -05:00
|
|
|
pub viewporter_state: ViewporterState,
|
|
|
|
pub fractional_scale_manager_state: FractionalScaleManagerState,
|
2023-06-15 16:43:33 -05:00
|
|
|
pub input_state: InputState,
|
2023-06-17 18:55:04 -05:00
|
|
|
pub api_state: ApiState,
|
2023-06-17 21:02:58 -05:00
|
|
|
pub focus_state: FocusState,
|
2023-06-30 21:34:07 -05:00
|
|
|
pub tag_state: TagState,
|
2023-06-02 16:01:48 -05:00
|
|
|
|
2023-06-07 12:12:54 -05:00
|
|
|
pub popup_manager: PopupManager,
|
|
|
|
|
|
|
|
pub cursor_status: CursorImageStatus,
|
|
|
|
pub pointer_location: Point<f64, Logical>,
|
2023-06-30 21:34:07 -05:00
|
|
|
pub windows: Vec<Window>,
|
2023-06-29 11:58:33 -05:00
|
|
|
|
|
|
|
pub async_scheduler: Scheduler<()>,
|
2023-06-02 16:01:48 -05:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:08:37 -05:00
|
|
|
impl<B: Backend> State<B> {
|
2023-06-28 16:41:36 -05:00
|
|
|
/// Create the main [`State`].
|
2023-06-21 18:58:49 -05:00
|
|
|
///
|
|
|
|
/// This will set the WAYLAND_DISPLAY environment variable, insert Wayland necessary sources
|
|
|
|
/// into the event loop, and run an implementation of the config API (currently Lua).
|
2023-06-09 20:29:17 -05:00
|
|
|
pub fn init(
|
|
|
|
backend_data: B,
|
2023-06-15 12:42:34 -05:00
|
|
|
display: &mut Display<Self>,
|
2023-06-09 20:29:17 -05:00
|
|
|
loop_signal: LoopSignal,
|
|
|
|
loop_handle: LoopHandle<'static, CalloopData<B>>,
|
|
|
|
) -> Result<Self, Box<dyn Error>> {
|
|
|
|
let socket = ListeningSocketSource::new_auto()?;
|
|
|
|
let socket_name = socket.socket_name().to_os_string();
|
|
|
|
|
|
|
|
std::env::set_var("WAYLAND_DISPLAY", socket_name.clone());
|
|
|
|
|
2023-06-28 16:41:36 -05:00
|
|
|
// Opening a new process will use up a few file descriptors, around 10 for Alacritty, for
|
|
|
|
// example. Because of this, opening up only around 100 processes would exhaust the file
|
|
|
|
// descriptor limit on my system (Arch btw) and cause a "Too many open files" crash.
|
|
|
|
//
|
|
|
|
// To fix this, I just set the limit to be higher. As Pinnacle is the whole graphical
|
|
|
|
// environment, I *think* this is ok.
|
2023-06-29 17:41:08 -05:00
|
|
|
if let Err(err) = smithay::reexports::nix::sys::resource::setrlimit(
|
|
|
|
smithay::reexports::nix::sys::resource::Resource::RLIMIT_NOFILE,
|
|
|
|
65536,
|
|
|
|
65536 * 2,
|
|
|
|
) {
|
|
|
|
tracing::error!("Could not raise fd limit: errno {err}");
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
|
2023-06-09 20:29:17 -05:00
|
|
|
loop_handle.insert_source(socket, |stream, _metadata, data| {
|
|
|
|
data.display
|
|
|
|
.handle()
|
|
|
|
.insert_client(stream, Arc::new(ClientState::default()))
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Could not insert client into loop handle");
|
2023-06-09 20:29:17 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
loop_handle.insert_source(
|
|
|
|
Generic::new(
|
|
|
|
display.backend().poll_fd().as_raw_fd(),
|
|
|
|
Interest::READ,
|
|
|
|
Mode::Level,
|
|
|
|
),
|
|
|
|
|_readiness, _metadata, data| {
|
|
|
|
data.display.dispatch_clients(&mut data.state)?;
|
|
|
|
Ok(PostAction::Continue)
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2023-06-17 18:55:04 -05:00
|
|
|
let (tx_channel, rx_channel) = calloop::channel::channel::<Msg>();
|
|
|
|
loop_handle.insert_source(rx_channel, |msg, _, data| match msg {
|
|
|
|
Event::Msg(msg) => {
|
2023-06-21 17:36:51 -05:00
|
|
|
// TODO: move this into its own function
|
2023-06-25 20:26:52 -05:00
|
|
|
// TODO: no like seriously this is getting a bit unwieldy
|
2023-06-28 16:41:36 -05:00
|
|
|
// TODO: no like rustfmt literally refuses to format the code below
|
2023-06-17 18:55:04 -05:00
|
|
|
match msg {
|
|
|
|
Msg::SetKeybind {
|
|
|
|
key,
|
|
|
|
modifiers,
|
|
|
|
callback_id,
|
|
|
|
} => {
|
|
|
|
tracing::info!("set keybind: {:?}, {}", modifiers, key);
|
|
|
|
data.state
|
|
|
|
.input_state
|
|
|
|
.keybinds
|
2023-06-24 17:39:40 -05:00
|
|
|
.insert((modifiers.into(), key), callback_id);
|
2023-06-17 18:55:04 -05:00
|
|
|
}
|
|
|
|
Msg::SetMousebind { button } => todo!(),
|
2023-06-17 21:02:58 -05:00
|
|
|
Msg::CloseWindow { client_id } => {
|
2023-06-21 14:48:38 -05:00
|
|
|
// TODO: client_id
|
2023-06-17 21:02:58 -05:00
|
|
|
tracing::info!("CloseWindow {:?}", client_id);
|
2023-06-18 19:30:52 -05:00
|
|
|
if let Some(window) = data.state.focus_state.current_focus() {
|
2023-06-17 21:02:58 -05:00
|
|
|
window.toplevel().send_close();
|
|
|
|
}
|
|
|
|
}
|
2023-06-18 19:30:52 -05:00
|
|
|
Msg::ToggleFloating { client_id } => {
|
|
|
|
// TODO: add client_ids
|
|
|
|
if let Some(window) = data.state.focus_state.current_focus() {
|
|
|
|
crate::window::toggle_floating(&mut data.state, &window);
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 14:48:38 -05:00
|
|
|
|
|
|
|
Msg::Spawn {
|
|
|
|
command,
|
|
|
|
callback_id,
|
|
|
|
} => {
|
2023-06-28 16:41:36 -05:00
|
|
|
data.state.handle_spawn(command, callback_id);
|
2023-06-21 14:48:38 -05:00
|
|
|
}
|
2023-06-26 18:48:29 -05:00
|
|
|
|
|
|
|
Msg::SetWindowSize { window_id, size } => {
|
|
|
|
let Some(window) = data.state.space.elements().find(|&win| {
|
|
|
|
WindowState::with_state(win, |state| state.id == window_id)
|
|
|
|
}) else { return; };
|
|
|
|
|
|
|
|
// TODO: tiled vs floating
|
|
|
|
window.toplevel().with_pending_state(|state| {
|
|
|
|
state.size = Some(size.into());
|
|
|
|
});
|
|
|
|
window.toplevel().send_pending_configure();
|
|
|
|
}
|
2023-06-30 21:34:07 -05:00
|
|
|
Msg::MoveToTag { tag_id } => todo!(),
|
|
|
|
Msg::ToggleTag { tag_id } => {
|
|
|
|
let windows = OutputState::with(data.state.focus_state.focused_output.as_ref().unwrap(), |state| {
|
|
|
|
if state.focused_tags
|
|
|
|
.iter()
|
|
|
|
.any(|tg| tg == &tag_id)
|
|
|
|
{
|
|
|
|
tracing::info!("Toggle tag {tag_id:?} off");
|
|
|
|
state.focused_tags.retain(|tg| tg != &tag_id);
|
|
|
|
} else {
|
|
|
|
tracing::info!("Toggle tag {tag_id:?} on");
|
|
|
|
state.focused_tags.push(tag_id.clone());
|
|
|
|
}
|
|
|
|
// re-layout
|
|
|
|
for window in data.state.space.elements().cloned().collect::<Vec<_>>() {
|
|
|
|
let should_render = WindowState::with_state(&window, |win_state| {
|
|
|
|
for tag_id in win_state.tags.iter() {
|
|
|
|
if state.focused_tags.contains(tag_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
});
|
|
|
|
if !should_render {
|
|
|
|
data.state.space.unmap_elem(&window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.state.windows.iter().filter(|&win| {
|
|
|
|
WindowState::with_state(win, |win_state| {
|
|
|
|
for tag_id in win_state.tags.iter() {
|
|
|
|
if state.focused_tags.contains(tag_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}).cloned().collect::<Vec<_>>()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
tracing::info!("Laying out {} windows", windows.len());
|
|
|
|
|
|
|
|
Layout::master_stack(&mut data.state, windows, crate::layout::Direction::Left);
|
|
|
|
},
|
|
|
|
Msg::AddTags { tags } => {
|
|
|
|
data.state.tag_state.tags.extend(tags.into_iter().map(|tag| Tag { id: tag, windows: vec![] }));
|
|
|
|
},
|
|
|
|
Msg::RemoveTags { tags } => {
|
|
|
|
data.state.tag_state.tags.retain(|tag| !tags.contains(&tag.id));
|
|
|
|
},
|
2023-06-26 18:48:29 -05:00
|
|
|
|
2023-06-21 17:36:51 -05:00
|
|
|
Msg::Quit => {
|
|
|
|
data.state.loop_signal.stop();
|
|
|
|
}
|
2023-06-26 18:48:29 -05:00
|
|
|
|
|
|
|
Msg::Request(request) => match request {
|
|
|
|
Request::GetWindowByAppId { id, app_id } => todo!(),
|
|
|
|
Request::GetWindowByTitle { id, title } => todo!(),
|
|
|
|
Request::GetWindowByFocus { id } => {
|
|
|
|
let Some(current_focus) = data.state.focus_state.current_focus() else { return; };
|
|
|
|
let (app_id, title) = compositor::with_states(
|
|
|
|
current_focus.toplevel().wl_surface(),
|
|
|
|
|states| {
|
|
|
|
let lock = states.
|
|
|
|
data_map
|
|
|
|
.get::<XdgToplevelSurfaceData>()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("XdgToplevelSurfaceData doesn't exist")
|
2023-06-26 18:48:29 -05:00
|
|
|
.lock()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't lock XdgToplevelSurfaceData");
|
2023-06-26 18:48:29 -05:00
|
|
|
(lock.app_id.clone(), lock.title.clone())
|
|
|
|
}
|
|
|
|
);
|
|
|
|
let (window_id, floating) = WindowState::with_state(¤t_focus, |state| {
|
|
|
|
(state.id, state.floating.is_floating())
|
|
|
|
});
|
|
|
|
// TODO: unwrap
|
|
|
|
let location = data.state.space.element_location(¤t_focus).unwrap();
|
|
|
|
let props = WindowProperties {
|
|
|
|
id: window_id,
|
|
|
|
app_id,
|
|
|
|
title,
|
|
|
|
size: current_focus.geometry().size.into(),
|
|
|
|
location: location.into(),
|
|
|
|
floating,
|
|
|
|
};
|
2023-06-29 17:41:08 -05:00
|
|
|
let stream = data.state.api_state.stream.as_ref().expect("Stream doesn't exist");
|
|
|
|
let mut stream = stream.lock().expect("Couldn't lock stream");
|
2023-06-26 18:48:29 -05:00
|
|
|
crate::api::send_to_client(
|
|
|
|
&mut stream,
|
|
|
|
&OutgoingMsg::RequestResponse {
|
|
|
|
request_id: id,
|
|
|
|
response: RequestResponse::Window { window: props }
|
|
|
|
}
|
|
|
|
)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Send to client failed");
|
2023-06-26 18:48:29 -05:00
|
|
|
},
|
|
|
|
Request::GetAllWindows { id } => {
|
|
|
|
let window_props = data.state.space.elements().map(|win| {
|
|
|
|
|
|
|
|
let (app_id, title) = compositor::with_states(
|
|
|
|
win.toplevel().wl_surface(),
|
|
|
|
|states| {
|
|
|
|
let lock = states.
|
|
|
|
data_map
|
|
|
|
.get::<XdgToplevelSurfaceData>()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("XdgToplevelSurfaceData doesn't exist")
|
2023-06-26 18:48:29 -05:00
|
|
|
.lock()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't lock XdgToplevelSurfaceData");
|
2023-06-26 18:48:29 -05:00
|
|
|
(lock.app_id.clone(), lock.title.clone())
|
|
|
|
}
|
|
|
|
);
|
|
|
|
let (window_id, floating) = WindowState::with_state(win, |state| {
|
|
|
|
(state.id, state.floating.is_floating())
|
|
|
|
});
|
|
|
|
// TODO: unwrap
|
2023-06-29 17:41:08 -05:00
|
|
|
let location = data.state.space.element_location(win).expect("Window location doesn't exist");
|
2023-06-26 18:48:29 -05:00
|
|
|
WindowProperties {
|
|
|
|
id: window_id,
|
|
|
|
app_id,
|
|
|
|
title,
|
|
|
|
size: win.geometry().size.into(),
|
|
|
|
location: location.into(),
|
|
|
|
floating,
|
|
|
|
}
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
2023-06-29 17:41:08 -05:00
|
|
|
// FIXME: figure out what to do if error
|
|
|
|
let stream = data.state.api_state.stream.as_ref().expect("Stream doesn't exist");
|
|
|
|
let mut stream = stream.lock().expect("Couldn't lock stream");
|
2023-06-26 18:48:29 -05:00
|
|
|
crate::api::send_to_client(
|
|
|
|
&mut stream,
|
|
|
|
&OutgoingMsg::RequestResponse {
|
|
|
|
request_id: id,
|
|
|
|
response: RequestResponse::GetAllWindows { windows: window_props },
|
|
|
|
}
|
|
|
|
)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't send to client");
|
2023-06-26 18:48:29 -05:00
|
|
|
}
|
|
|
|
},
|
2023-06-17 18:55:04 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Event::Closed => todo!(),
|
|
|
|
})?;
|
|
|
|
|
2023-06-19 16:24:27 -05:00
|
|
|
// We want to replace the client if a new one pops up
|
2023-06-29 11:58:33 -05:00
|
|
|
// TODO: there should only ever be one client working at a time, and creating a new client
|
|
|
|
// | when one is already running should be impossible.
|
2023-06-17 21:02:58 -05:00
|
|
|
// INFO: this source try_clone()s the stream
|
2023-06-17 18:55:04 -05:00
|
|
|
loop_handle.insert_source(PinnacleSocketSource::new(tx_channel)?, |stream, _, data| {
|
2023-06-21 14:48:38 -05:00
|
|
|
if let Some(old_stream) = data
|
|
|
|
.state
|
|
|
|
.api_state
|
|
|
|
.stream
|
|
|
|
.replace(Arc::new(Mutex::new(stream)))
|
|
|
|
{
|
|
|
|
old_stream
|
|
|
|
.lock()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't lock old stream")
|
2023-06-21 14:48:38 -05:00
|
|
|
.shutdown(std::net::Shutdown::Both)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't shutdown old stream");
|
2023-06-17 18:55:04 -05:00
|
|
|
}
|
2023-06-15 16:43:33 -05:00
|
|
|
})?;
|
|
|
|
|
2023-06-29 17:41:08 -05:00
|
|
|
let (executor, sched) = calloop::futures::executor::<()>().expect("Couldn't create executor");
|
2023-06-29 11:58:33 -05:00
|
|
|
loop_handle.insert_source(executor, |_, _, _| {})?;
|
|
|
|
|
2023-06-19 16:24:27 -05:00
|
|
|
// TODO: move all this into the lua api
|
|
|
|
let config_path = std::env::var("PINNACLE_CONFIG").unwrap_or_else(|_| {
|
|
|
|
let mut default_path =
|
|
|
|
std::env::var("XDG_CONFIG_HOME").unwrap_or("~/.config".to_string());
|
|
|
|
default_path.push_str("/pinnacle/init.lua");
|
|
|
|
default_path
|
|
|
|
});
|
|
|
|
|
|
|
|
let lua_path = std::env::var("LUA_PATH").expect("Lua is not installed!");
|
|
|
|
let mut local_lua_path = std::env::current_dir()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Couldn't get current dir")
|
2023-06-19 16:24:27 -05:00
|
|
|
.to_string_lossy()
|
|
|
|
.to_string();
|
2023-06-19 19:07:45 -05:00
|
|
|
local_lua_path.push_str("/api/lua"); // TODO: get from crate root and do dynamically
|
2023-06-19 16:24:27 -05:00
|
|
|
let new_lua_path =
|
|
|
|
format!("{local_lua_path}/?.lua;{local_lua_path}/?/init.lua;{local_lua_path}/lib/?.lua;{local_lua_path}/lib/?/init.lua;{lua_path}");
|
|
|
|
|
|
|
|
let lua_cpath = std::env::var("LUA_CPATH").expect("Lua is not installed!");
|
|
|
|
let new_lua_cpath = format!("{local_lua_path}/lib/?.so;{lua_cpath}");
|
|
|
|
|
|
|
|
std::process::Command::new("lua5.4")
|
2023-06-19 19:07:45 -05:00
|
|
|
.arg(config_path)
|
2023-06-19 16:24:27 -05:00
|
|
|
.env("LUA_PATH", new_lua_path)
|
|
|
|
.env("LUA_CPATH", new_lua_cpath)
|
|
|
|
.spawn()
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Could not start config process");
|
2023-06-19 16:24:27 -05:00
|
|
|
|
2023-06-09 20:29:17 -05:00
|
|
|
let display_handle = display.handle();
|
|
|
|
let mut seat_state = SeatState::new();
|
|
|
|
let mut seat = seat_state.new_wl_seat(&display_handle, backend_data.seat_name());
|
|
|
|
seat.add_pointer();
|
|
|
|
seat.add_keyboard(XkbConfig::default(), 200, 25)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
backend_data,
|
|
|
|
loop_signal,
|
|
|
|
loop_handle,
|
|
|
|
clock: Clock::<Monotonic>::new()?,
|
2023-06-15 12:42:34 -05:00
|
|
|
compositor_state: CompositorState::new::<Self>(&display_handle),
|
|
|
|
data_device_state: DataDeviceState::new::<Self>(&display_handle),
|
2023-06-09 20:29:17 -05:00
|
|
|
seat_state,
|
|
|
|
pointer_location: (0.0, 0.0).into(),
|
2023-06-15 12:42:34 -05:00
|
|
|
shm_state: ShmState::new::<Self>(&display_handle, vec![]),
|
2023-06-09 20:29:17 -05:00
|
|
|
space: Space::<Window>::default(),
|
|
|
|
cursor_status: CursorImageStatus::Default,
|
2023-06-15 12:42:34 -05:00
|
|
|
output_manager_state: OutputManagerState::new_with_xdg_output::<Self>(&display_handle),
|
|
|
|
xdg_shell_state: XdgShellState::new::<Self>(&display_handle),
|
|
|
|
viewporter_state: ViewporterState::new::<Self>(&display_handle),
|
|
|
|
fractional_scale_manager_state: FractionalScaleManagerState::new::<Self>(
|
2023-06-09 20:29:17 -05:00
|
|
|
&display_handle,
|
|
|
|
),
|
2023-06-17 21:02:58 -05:00
|
|
|
input_state: InputState::new(),
|
|
|
|
api_state: ApiState::new(),
|
|
|
|
focus_state: FocusState::new(),
|
2023-06-30 21:34:07 -05:00
|
|
|
tag_state: TagState::new(),
|
2023-06-09 20:29:17 -05:00
|
|
|
|
|
|
|
seat,
|
|
|
|
|
|
|
|
move_mode: false,
|
|
|
|
socket_name: socket_name.to_string_lossy().to_string(),
|
|
|
|
|
|
|
|
popup_manager: PopupManager::default(),
|
2023-06-29 11:58:33 -05:00
|
|
|
|
|
|
|
async_scheduler: sched,
|
2023-06-30 21:34:07 -05:00
|
|
|
|
|
|
|
windows: vec![],
|
2023-06-09 20:29:17 -05:00
|
|
|
})
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
|
|
|
|
pub fn handle_spawn(&self, command: Vec<String>, callback_id: Option<CallbackId>) {
|
2023-06-29 17:41:08 -05:00
|
|
|
let mut command = command.into_iter();
|
|
|
|
let Some(program) = command.next() else {
|
2023-06-28 16:41:36 -05:00
|
|
|
// TODO: notify that command was nothing
|
|
|
|
return;
|
2023-06-29 17:41:08 -05:00
|
|
|
};
|
2023-06-28 16:41:36 -05:00
|
|
|
|
2023-06-29 17:41:08 -05:00
|
|
|
let program = OsString::from(program);
|
|
|
|
let Ok(mut child) = async_process::Command::new(&program)
|
2023-06-28 16:41:36 -05:00
|
|
|
.env("WAYLAND_DISPLAY", self.socket_name.clone())
|
|
|
|
.stdin(if callback_id.is_some() {
|
|
|
|
Stdio::piped()
|
|
|
|
} else {
|
|
|
|
// piping to null because foot won't open without a callback_id
|
|
|
|
// otherwise
|
|
|
|
Stdio::null()
|
|
|
|
})
|
|
|
|
.stdout(if callback_id.is_some() {
|
|
|
|
Stdio::piped()
|
|
|
|
} else {
|
|
|
|
Stdio::null()
|
|
|
|
})
|
|
|
|
.stderr(if callback_id.is_some() {
|
|
|
|
Stdio::piped()
|
|
|
|
} else {
|
|
|
|
Stdio::null()
|
|
|
|
})
|
|
|
|
.args(command)
|
|
|
|
.spawn()
|
2023-06-29 17:41:08 -05:00
|
|
|
else {
|
|
|
|
// TODO: notify user that program doesn't exist
|
|
|
|
tracing::warn!("tried to run {}, but it doesn't exist", program.to_string_lossy());
|
|
|
|
return;
|
|
|
|
};
|
2023-06-28 16:41:36 -05:00
|
|
|
|
|
|
|
if let Some(callback_id) = callback_id {
|
|
|
|
let stdout = child.stdout.take();
|
|
|
|
let stderr = child.stderr.take();
|
2023-06-29 17:41:08 -05:00
|
|
|
let stream_out = self.api_state.stream.as_ref().expect("Stream doesn't exist").clone();
|
2023-06-28 16:41:36 -05:00
|
|
|
let stream_err = stream_out.clone();
|
|
|
|
let stream_exit = stream_out.clone();
|
|
|
|
|
|
|
|
if let Some(stdout) = stdout {
|
2023-06-29 11:58:33 -05:00
|
|
|
let future = async move {
|
|
|
|
// TODO: use BufReader::new().lines()
|
|
|
|
let mut reader = futures_lite::io::BufReader::new(stdout);
|
2023-06-28 16:41:36 -05:00
|
|
|
loop {
|
|
|
|
let mut buf = String::new();
|
2023-06-29 11:58:33 -05:00
|
|
|
match reader.read_line(&mut buf).await {
|
|
|
|
Ok(0) => break,
|
2023-06-28 16:41:36 -05:00
|
|
|
Ok(_) => {
|
2023-06-29 17:41:08 -05:00
|
|
|
let mut stream = stream_out.lock().expect("Couldn't lock stream");
|
2023-06-28 16:41:36 -05:00
|
|
|
crate::api::send_to_client(
|
|
|
|
&mut stream,
|
|
|
|
&OutgoingMsg::CallCallback {
|
|
|
|
callback_id,
|
|
|
|
args: Some(Args::Spawn {
|
|
|
|
stdout: Some(buf.trim_end_matches('\n').to_string()),
|
|
|
|
stderr: None,
|
|
|
|
exit_code: None,
|
|
|
|
exit_msg: None,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Send to client failed"); // TODO: notify instead of crash
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2023-06-29 11:58:33 -05:00
|
|
|
tracing::warn!("child read err: {err}");
|
2023-06-28 16:41:36 -05:00
|
|
|
break;
|
2023-06-29 11:58:33 -05:00
|
|
|
},
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 11:58:33 -05:00
|
|
|
};
|
2023-06-29 17:41:08 -05:00
|
|
|
|
|
|
|
// This is not important enough to crash on error, so just print the error instead
|
|
|
|
if let Err(err) = self.async_scheduler.schedule(future) {
|
|
|
|
tracing::error!("Failed to schedule future: {err}");
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
if let Some(stderr) = stderr {
|
2023-06-29 11:58:33 -05:00
|
|
|
let future = async move {
|
|
|
|
let mut reader = futures_lite::io::BufReader::new(stderr);
|
2023-06-28 16:41:36 -05:00
|
|
|
loop {
|
|
|
|
let mut buf = String::new();
|
2023-06-29 11:58:33 -05:00
|
|
|
match reader.read_line(&mut buf).await {
|
|
|
|
Ok(0) => break,
|
2023-06-28 16:41:36 -05:00
|
|
|
Ok(_) => {
|
2023-06-29 17:41:08 -05:00
|
|
|
let mut stream = stream_err.lock().expect("Couldn't lock stream");
|
2023-06-28 16:41:36 -05:00
|
|
|
crate::api::send_to_client(
|
|
|
|
&mut stream,
|
|
|
|
&OutgoingMsg::CallCallback {
|
|
|
|
callback_id,
|
|
|
|
args: Some(Args::Spawn {
|
|
|
|
stdout: None,
|
|
|
|
stderr: Some(buf.trim_end_matches('\n').to_string()),
|
|
|
|
exit_code: None,
|
|
|
|
exit_msg: None,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Send to client failed"); // TODO: notify instead of crash
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2023-06-29 11:58:33 -05:00
|
|
|
tracing::warn!("child read err: {err}");
|
2023-06-28 16:41:36 -05:00
|
|
|
break;
|
2023-06-29 11:58:33 -05:00
|
|
|
},
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 11:58:33 -05:00
|
|
|
};
|
2023-06-29 17:41:08 -05:00
|
|
|
if let Err(err) = self.async_scheduler.schedule(future) {
|
|
|
|
tracing::error!("Failed to schedule future: {err}");
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
2023-06-29 11:58:33 -05:00
|
|
|
|
|
|
|
let future = async move {
|
|
|
|
match child.status().await {
|
|
|
|
Ok(exit_status) => {
|
2023-06-29 17:41:08 -05:00
|
|
|
let mut stream = stream_exit.lock().expect("Couldn't lock stream");
|
2023-06-29 11:58:33 -05:00
|
|
|
crate::api::send_to_client(
|
|
|
|
&mut stream,
|
|
|
|
&OutgoingMsg::CallCallback {
|
|
|
|
callback_id,
|
|
|
|
args: Some(Args::Spawn {
|
|
|
|
stdout: None,
|
|
|
|
stderr: None,
|
|
|
|
exit_code: exit_status.code(),
|
|
|
|
exit_msg: Some(exit_status.to_string()),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
)
|
2023-06-29 17:41:08 -05:00
|
|
|
.expect("Send to client failed"); // TODO: notify instead of crash
|
2023-06-29 11:58:33 -05:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
tracing::warn!("child wait() err: {err}");
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
2023-06-29 11:58:33 -05:00
|
|
|
};
|
2023-06-29 17:41:08 -05:00
|
|
|
if let Err(err) = self.async_scheduler.schedule(future) {
|
|
|
|
tracing::error!("Failed to schedule future: {err}");
|
|
|
|
}
|
2023-06-28 16:41:36 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 21:08:37 -05:00
|
|
|
}
|
|
|
|
|
2023-06-09 20:29:17 -05:00
|
|
|
pub struct CalloopData<B: Backend> {
|
|
|
|
pub display: Display<State<B>>,
|
|
|
|
pub state: State<B>,
|
2023-06-02 16:01:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ClientState {
|
|
|
|
pub compositor_state: CompositorClientState,
|
|
|
|
}
|
|
|
|
impl ClientData for ClientState {
|
|
|
|
fn initialized(&self, _client_id: ClientId) {}
|
|
|
|
|
|
|
|
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) {}
|
|
|
|
}
|
2023-06-09 20:29:17 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct SurfaceDmabufFeedback<'a> {
|
|
|
|
pub render_feedback: &'a DmabufFeedback,
|
|
|
|
pub scanout_feedback: &'a DmabufFeedback,
|
|
|
|
}
|
|
|
|
|
2023-06-21 19:08:29 -05:00
|
|
|
// TODO: docs
|
2023-06-09 20:29:17 -05:00
|
|
|
pub fn take_presentation_feedback(
|
|
|
|
output: &Output,
|
|
|
|
space: &Space<Window>,
|
|
|
|
render_element_states: &RenderElementStates,
|
|
|
|
) -> OutputPresentationFeedback {
|
|
|
|
let mut output_presentation_feedback = OutputPresentationFeedback::new(output);
|
|
|
|
|
|
|
|
space.elements().for_each(|window| {
|
|
|
|
if space.outputs_for_element(window).contains(output) {
|
|
|
|
window.take_presentation_feedback(
|
|
|
|
&mut output_presentation_feedback,
|
|
|
|
surface_primary_scanout_output,
|
|
|
|
|surface, _| {
|
|
|
|
surface_presentation_feedback_flags_from_states(surface, render_element_states)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2023-06-11 17:56:34 -05:00
|
|
|
// let map = smithay::desktop::layer_map_for_output(output);
|
|
|
|
// for layer_surface in map.layers() {
|
|
|
|
// layer_surface.take_presentation_feedback(
|
|
|
|
// &mut output_presentation_feedback,
|
|
|
|
// surface_primary_scanout_output,
|
|
|
|
// |surface, _| {
|
|
|
|
// surface_presentation_feedback_flags_from_states(surface, render_element_states)
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// }
|
2023-06-09 20:29:17 -05:00
|
|
|
|
|
|
|
output_presentation_feedback
|
|
|
|
}
|
2023-06-17 18:55:04 -05:00
|
|
|
|
2023-06-21 19:08:29 -05:00
|
|
|
/// State containing the config API's stream.
|
2023-06-17 21:02:58 -05:00
|
|
|
#[derive(Default)]
|
2023-06-17 18:55:04 -05:00
|
|
|
pub struct ApiState {
|
2023-06-21 14:48:38 -05:00
|
|
|
pub stream: Option<Arc<Mutex<UnixStream>>>,
|
2023-06-17 18:55:04 -05:00
|
|
|
}
|
2023-06-17 21:02:58 -05:00
|
|
|
|
|
|
|
impl ApiState {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
}
|