2021-01-17 13:58:02 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
|
use api::*;
|
|
|
|
|
|
2021-03-03 14:38:20 +00:00
|
|
|
use graphics_server::api::{TextOp, TextView};
|
2021-01-17 13:58:02 +00:00
|
|
|
|
2021-06-12 20:40:50 +00:00
|
|
|
use graphics_server::api::{Point, Gid, Line, Rectangle, Circle, RoundedRectangle, TokenClaim};
|
2021-01-19 19:18:13 +00:00
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
use api::Opcode; // if you prefer to map the api into your local namespace
|
2021-04-05 15:50:00 +00:00
|
|
|
use xous::{send_message, CID, Message};
|
2021-04-04 21:47:17 +00:00
|
|
|
use xous_ipc::{String, Buffer};
|
2021-04-05 15:50:00 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2021-04-04 21:47:17 +00:00
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
#[derive(Debug)]
|
2021-04-04 21:47:17 +00:00
|
|
|
pub struct Gam {
|
|
|
|
|
conn: CID,
|
2021-03-08 18:32:36 +00:00
|
|
|
}
|
2021-04-04 21:47:17 +00:00
|
|
|
impl Gam {
|
|
|
|
|
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
2021-05-21 08:24:30 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2021-04-04 21:47:17 +00:00
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_GAM).expect("Can't connect to GAM");
|
2021-04-05 15:50:00 +00:00
|
|
|
Ok(Gam {
|
2021-04-04 21:47:17 +00:00
|
|
|
conn,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-03-08 18:32:36 +00:00
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
pub fn redraw(&self) -> Result<(), xous::Error> {
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::Redraw.to_usize().unwrap(), 0, 0, 0, 0)
|
|
|
|
|
).map(|_|())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn powerdown_request(&self) -> Result<bool, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::PowerDownRequest.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(confirmed) = response {
|
|
|
|
|
if confirmed != 0 {
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
2021-03-10 17:45:54 +00:00
|
|
|
} else {
|
2021-04-04 21:47:17 +00:00
|
|
|
panic!("GAM_API: unexpected return value: {:#?}", response);
|
2021-03-10 17:45:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 10:07:41 +00:00
|
|
|
pub fn shipmode_blank_request(&self) -> Result<bool, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::ShipModeBlankRequest.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(confirmed) = response {
|
|
|
|
|
if confirmed != 0 {
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic!("GAM_API: unexpected return value: {:#?}", response);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-10 17:45:54 +00:00
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
/// this "posts" a textview -- it's not a "draw" as the update is neither guaranteed nor instantaneous
|
|
|
|
|
/// the GAM first has to check that the textview is allowed to be updated, and then it will decide when
|
|
|
|
|
/// the actual screen update is allowed
|
|
|
|
|
pub fn post_textview(&self, tv: &mut TextView) -> Result<(), xous::Error> {
|
|
|
|
|
tv.set_op(TextOp::Render);
|
2021-04-05 15:50:00 +00:00
|
|
|
let mut buf = Buffer::into_buf(tv.clone()).or(Err(xous::Error::InternalError))?;
|
2021-04-04 21:47:17 +00:00
|
|
|
buf.lend_mut(self.conn, Opcode::RenderTextView.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
|
|
|
|
|
match buf.to_original().unwrap() {
|
|
|
|
|
api::Return::RenderReturn(tvr) => {
|
|
|
|
|
tv.bounds_computed = tvr.bounds_computed;
|
|
|
|
|
tv.cursor = tvr.cursor;
|
|
|
|
|
}
|
|
|
|
|
_ => panic!("GAM_API: post_textview got a return value from the server that isn't expected or handled")
|
|
|
|
|
}
|
|
|
|
|
tv.set_op(TextOp::Nop);
|
|
|
|
|
Ok(())
|
2021-02-17 18:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
pub fn draw_line(&self, gid: Gid, line: Line) -> Result<(), xous::Error> {
|
|
|
|
|
let go = GamObject {
|
2021-02-28 20:01:33 +00:00
|
|
|
canvas: gid,
|
|
|
|
|
obj: GamObjectType::Line(line),
|
2021-04-04 21:47:17 +00:00
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|())
|
|
|
|
|
}
|
|
|
|
|
pub fn draw_rectangle(&self, gid: Gid, rect: Rectangle) -> Result<(), xous::Error> {
|
|
|
|
|
let go = GamObject {
|
2021-03-05 18:45:47 +00:00
|
|
|
canvas: gid,
|
|
|
|
|
obj: GamObjectType::Rect(rect),
|
2021-04-04 21:47:17 +00:00
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|())
|
|
|
|
|
}
|
|
|
|
|
pub fn draw_rounded_rectangle(&self, gid: Gid, rr: RoundedRectangle) -> Result<(), xous::Error> {
|
|
|
|
|
let go = GamObject {
|
2021-03-05 18:45:47 +00:00
|
|
|
canvas: gid,
|
|
|
|
|
obj: GamObjectType::RoundRect(rr),
|
2021-04-04 21:47:17 +00:00
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|())
|
2021-02-28 18:33:13 +00:00
|
|
|
}
|
2021-04-04 21:47:17 +00:00
|
|
|
pub fn draw_circle(&self, gid: Gid, circ: Circle) -> Result<(), xous::Error> {
|
|
|
|
|
let go = GamObject {
|
|
|
|
|
canvas: gid,
|
|
|
|
|
obj: GamObjectType::Circ(circ),
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|())
|
2021-03-07 10:58:48 +00:00
|
|
|
}
|
2021-03-09 16:25:14 +00:00
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
pub fn get_canvas_bounds(&self, gid: Gid) -> Result<Point, xous::Error> {
|
|
|
|
|
log::trace!("GAM_API: get_canvas_bounds");
|
|
|
|
|
let response = send_message(self.conn,
|
2021-04-05 15:50:00 +00:00
|
|
|
Message::new_blocking_scalar(Opcode::GetCanvasBounds.to_usize().unwrap(),
|
|
|
|
|
gid.gid()[0] as _, gid.gid()[1] as _, gid.gid()[2] as _, gid.gid()[3] as _))
|
|
|
|
|
.expect("GAM_API: can't get canvas bounds from GAM");
|
2021-04-04 21:47:17 +00:00
|
|
|
if let xous::Result::Scalar2(tl, br) = response {
|
|
|
|
|
// note that the result should always be normalized so the rectangle's "tl" should be (0,0)
|
|
|
|
|
log::trace!("GAM_API: tl:{}, br:{}", tl, br);
|
|
|
|
|
assert!(tl == 0, "GAM_API: api call returned non-zero top left for canvas bounds");
|
|
|
|
|
Ok(br.into())
|
|
|
|
|
} else {
|
|
|
|
|
panic!("GAM_API: can't get canvas bounds")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 19:54:48 +00:00
|
|
|
|
2021-04-04 21:47:17 +00:00
|
|
|
pub fn set_canvas_bounds_request(&self, req: &mut SetCanvasBoundsRequest) -> Result<(), xous::Error> {
|
2021-04-05 15:50:00 +00:00
|
|
|
let mut buf = Buffer::into_buf(req.clone()).or(Err(xous::Error::InternalError))?;
|
2021-04-04 21:47:17 +00:00
|
|
|
buf.lend_mut(self.conn, Opcode::SetCanvasBounds.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
match buf.to_original().unwrap() {
|
|
|
|
|
api::Return::SetCanvasBoundsReturn(ret) => {
|
|
|
|
|
req.granted = ret.granted;
|
|
|
|
|
}
|
|
|
|
|
_ => panic!("GAM_API: set_canvas_bounds_request view got a return value from the server that isn't expected or handled")
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-03-09 16:25:14 +00:00
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
pub fn request_content_canvas(&self, requestor_name: &str, redraw_id: usize) -> Result<Gid, xous::Error> {
|
2021-04-04 21:47:17 +00:00
|
|
|
let mut server = String::<256>::new();
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
write!(server, "{}", requestor_name).expect("GAM_API: couldn't write request_content_canvas server name");
|
|
|
|
|
let req = ContentCanvasRequest {
|
|
|
|
|
canvas: Gid::new([0,0,0,0]),
|
|
|
|
|
servername: server,
|
2021-04-05 15:50:00 +00:00
|
|
|
redraw_scalar_id: redraw_id,
|
2021-04-04 21:47:17 +00:00
|
|
|
};
|
|
|
|
|
let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(self.conn, Opcode::RequestContentCanvas.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
|
|
|
|
|
match buf.to_original().unwrap() {
|
|
|
|
|
api::Return::ContentCanvasReturn(ret) => {
|
|
|
|
|
Ok(ret.canvas)
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::error!("GAM_API: request_content_canvas got a return value from the server that isn't expected or handled");
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
2021-06-12 20:40:50 +00:00
|
|
|
|
|
|
|
|
pub fn claim_token(&self, name: &str) -> Result<Option<[u32; 4]>, xous::Error> {
|
|
|
|
|
let tokenclaim = TokenClaim {
|
|
|
|
|
token: None,
|
|
|
|
|
name: String::<128>::from_str(name),
|
|
|
|
|
};
|
|
|
|
|
let mut buf = Buffer::into_buf(tokenclaim).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(self.conn, Opcode::ClaimToken.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
let returned_claim = buf.to_original::<TokenClaim, _>().unwrap();
|
|
|
|
|
|
|
|
|
|
Ok(returned_claim.token)
|
|
|
|
|
}
|
|
|
|
|
pub fn allow_less_trusted_code(&self) -> Result<bool, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AllowLessTrustedCode.to_usize().unwrap(), 0, 0, 0, 0)
|
|
|
|
|
).expect("couldn't run allow trusted code check");
|
|
|
|
|
if let xous::Result::Scalar1(result) = response {
|
|
|
|
|
if result == 1 {
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 08:24:30 +00:00
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-04-05 15:50:00 +00:00
|
|
|
impl Drop for Gam {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
|
2021-05-21 08:24:30 +00:00
|
|
|
if REFCOUNT.load(Ordering::Relaxed) == 0 {
|
|
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
}
|