-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cba577
commit 77cfaa9
Showing
10 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
VELLOGD_SERVER_PATH_ENVVAR <- "VELLOGD_SERVER_PATH" | ||
|
||
pkg_cache_dir <- function() { | ||
normalizePath(tools::R_user_dir("vellogd", "cache"), mustWork = FALSE) | ||
} | ||
|
||
server_path <- function() { | ||
server_path <- Sys.getenv(VELLOGD_SERVER_PATH_ENVVAR) | ||
|
||
# If the server path is provided by the user, return it. | ||
if (!identity(server_path, "")) { | ||
return(server_path) | ||
} | ||
|
||
server_path <- server_path_default() | ||
|
||
# TODO: check version | ||
if (!file.exists(server_path)) { | ||
download_server() | ||
} | ||
|
||
server_path | ||
} | ||
|
||
server_path_default <- function() { | ||
bin <- if (Sys.info()[["sysname"]] == "Windows") { | ||
"vellogd-server.exe" | ||
} else { | ||
"vellogd-server" | ||
} | ||
|
||
path <- file.path(pkg_cache_dir(), bin) | ||
} | ||
|
||
URL_BASE <- "https://github.com/yutannihilation/vellogd/releases/download" | ||
|
||
get_latest_release <- function() { | ||
jsonlite::read_json("https://api.github.com/repos/yutannihilation/vellogd/releases/latest")[["tag_name"]] | ||
} | ||
|
||
get_download_url <- function() { | ||
latest_release <- get_latest_release() | ||
|
||
os <- Sys.info()[["sysname"]] | ||
arch <- Sys.info()[["machine"]] | ||
|
||
binary <- switch(os, | ||
Windows = "server-Windows-X64.tar.gz", | ||
Linux = "server-Linux-X64.tar.gz", | ||
Darwin = "server-macOS-ARM64.tar.gz" | ||
) | ||
|
||
paste(URL_BASE, latest_release, binary, sep = "/") | ||
} | ||
|
||
download_server <- function() { | ||
download_tmp_dir <- tempfile() | ||
extract_tmp_dir <- tempfile() | ||
on.exit(unlink(download_tmp_dir, recursive = TRUE, force = TRUE), add = TRUE) | ||
on.exit(unlink(extract_tmp_dir, recursive = TRUE, force = TRUE), add = TRUE) | ||
|
||
# download | ||
dir.create(download_tmp_dir) | ||
download_url <- get_download_url() | ||
archive_file <- file.path(download_tmp_dir, basename(download_url)) | ||
utils::download.file(download_url, destfile = archive_file, mode = "wb") | ||
|
||
# extract and copy | ||
dst <- server_path() | ||
dir.create(dirname(dst), showWarnings = FALSE) | ||
|
||
utils::untar(archive_file, exdir = extract_tmp_dir) | ||
if (Sys.info()[["sysname"]] == "Windows") { | ||
file.copy(file.path(extract_tmp_dir, "vellogd-server.exe"), dst, overwrite = TRUE) | ||
} else { | ||
file.copy(file.path(extract_tmp_dir, "vellogd-server"), dst, overwrite = TRUE) | ||
} | ||
|
||
invisible(NULL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
#' Open A 'Vello' Graphics Device. | ||
#' | ||
#' @export | ||
vellogd <- function(filename = "Rplot%03d.png", width = 480, height = 480) { | ||
vellogd_impl(filename, as.numeric(width), as.numeric(height)) | ||
} | ||
|
||
#' Open A 'Vello' Graphics Device With Server. | ||
#' | ||
#' @export | ||
vellogd <- function(filename = "Rplot%03d.png", width = 480, height = 480) { | ||
server <- server_path() | ||
vellogd_with_server_impl(filename, as.numeric(width), as.numeric(height), server) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
SEXP savvy_vellogd_impl__ffi(SEXP c_arg__filename, SEXP c_arg__width, SEXP c_arg__height); | ||
SEXP savvy_vellogd_with_server_impl__ffi(SEXP c_arg__filename, SEXP c_arg__width, SEXP c_arg__height, SEXP c_arg__server); | ||
SEXP savvy_debuggd__ffi(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; | ||
use vellogd_shared::protocol::{UserEvent, UserResponse}; | ||
|
||
use crate::{graphics::DeviceDriver, WindowController}; | ||
|
||
pub struct VelloGraphicsDeviceWithServer { | ||
filename: String, | ||
layout: parley::Layout<vello::peniko::Brush>, | ||
process: Option<std::process::Child>, | ||
tx: IpcSender<UserEvent>, | ||
rx: IpcReceiver<UserResponse>, | ||
} | ||
|
||
impl VelloGraphicsDeviceWithServer { | ||
pub(crate) fn new(filename: &str, server: Option<&str>) -> savvy::Result<Self> { | ||
// server -> controller | ||
let (rx_server, rx_server_name) = IpcOneShotServer::<UserResponse>::new().unwrap(); | ||
|
||
let server_process = if let Some(server_bin) = server { | ||
// spawn a server process | ||
let res = std::process::Command::new(server_bin) | ||
.arg(rx_server_name) | ||
// .stdout(std::process::Stdio::piped()) | ||
.spawn(); | ||
|
||
match res { | ||
Ok(c) => { | ||
savvy::r_eprintln!("Server runs at PID {}", c.id()); | ||
Some(c) | ||
} | ||
Err(e) => { | ||
let msg = format!("failed to spawn the process: {e}"); | ||
return Err(savvy::Error::new(&msg)); | ||
} | ||
} | ||
} else { | ||
savvy::r_eprintln!("rx_server_name: {rx_server_name}"); | ||
None | ||
}; | ||
|
||
// establish connections of both direction | ||
let (tx, rx) = match rx_server.accept() { | ||
Ok((rx, UserResponse::Connect { server_name })) => { | ||
savvy::r_eprint!("Connecting to {server_name}..."); | ||
let tx: IpcSender<UserEvent> = IpcSender::connect(server_name).unwrap(); | ||
tx.send(UserEvent::ConnectionReady).unwrap(); | ||
(tx, rx) | ||
} | ||
Ok((_, data)) => panic!("got unexpected data: {data:?}"), | ||
Err(e) => panic!("failed to accept connection: {e}"), | ||
}; | ||
savvy::r_eprintln!("connected!"); | ||
|
||
Ok(Self { | ||
filename: filename.into(), | ||
layout: parley::Layout::new(), | ||
process: server_process, | ||
tx, | ||
rx, | ||
}) | ||
} | ||
} | ||
|
||
impl WindowController for VelloGraphicsDeviceWithServer { | ||
fn send_event(&self, event: vellogd_shared::protocol::UserEvent) -> savvy::Result<()> { | ||
self.tx.send(event).map_err(|e| e.to_string().into()) | ||
} | ||
|
||
fn recv_response(&self) -> savvy::Result<vellogd_shared::protocol::UserResponse> { | ||
self.rx.recv().map_err(|e| e.to_string().into()) | ||
} | ||
} | ||
|
||
impl DeviceDriver for VelloGraphicsDeviceWithServer {} |