xous-core/services/gam/src/api.rs

122 lines
4.2 KiB
Rust
Raw Normal View History

use xous::{Message, ScalarMessage};
use graphics_server::api::{Rectangle, TextView, Gid, Line, RoundedRectangle, Circle, Point};
use xous_ipc::String;
2021-01-17 09:02:40 +00:00
2021-04-04 21:47:17 +00:00
pub(crate) const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_";
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub enum GamObjectType {
Line(Line),
Circ(Circle),
Rect(Rectangle),
RoundRect(RoundedRectangle),
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct GamObject {
pub canvas: Gid,
pub obj: GamObjectType,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct SetCanvasBoundsRequest {
pub canvas: Gid,
pub requested: Point,
pub granted: Option<Point>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ContentCanvasRequest {
// return value of the canvas Gid
pub canvas: Gid,
// name of the server requesting the content canvas
pub servername: String<256>,
}
2021-04-04 21:47:17 +00:00
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum Opcode {
// clears a canvas with a given GID
2021-04-04 21:47:17 +00:00
ClearCanvas, //(Gid),
// return the dimensions of a canvas as a Point (the top left is always (0,0))
2021-04-04 21:47:17 +00:00
GetCanvasBounds, //(Gid),
// request a new size for my canvas.
// This normally will be denied, unless the requested Gid corresponds to a special canvas that allows resizing.
2021-04-04 21:47:17 +00:00
SetCanvasBounds, //(SetCanvasBoundsRequest),
// draws an object
2021-04-04 21:47:17 +00:00
RenderObject, //(GamObject),
// renders a TextView
2021-04-04 21:47:17 +00:00
RenderTextView, //(TextView),
// forces a redraw (which also does defacement, etc.)
Redraw,
// returns a GID to the "content" Canvas; currently, anyone can request it and draw to it, but maybe that policy should be stricter.
// the Gid argument is the rkyv return value.
2021-04-04 21:47:17 +00:00
RequestContentCanvas, //(ContentCanvasRequest),
// Requests setting the UI to the power down screen
PowerDownRequest,
/////// planned
// hides a canvas with a given GID
2021-04-04 21:47:17 +00:00
//HideCanvas(Gid),
// indicates if the current UI layout requires an input field
2021-04-04 21:47:17 +00:00
//HasInput(bool),
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) enum Return {
RenderReturn(TextView),
SetCanvasBoundsReturn(SetCanvasBoundsRequest),
ContentCanvasReturn(ContentCanvasRequest),
Failure,
}
2021-04-04 21:47:17 +00:00
/*
impl core::convert::TryFrom<&Message> for Opcode {
type Error = &'static str;
fn try_from(message: &Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
2021-01-17 13:58:02 +00:00
0 => Ok(Opcode::ClearCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
1 => Ok(Opcode::Redraw),
_ => Err("GAM api: unknown Scalar ID"),
},
Message::BlockingScalar(m) => match m.id {
0 => Ok(Opcode::GetCanvasBounds(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
1 => Ok(Opcode::PowerDownRequest),
_ => Err("GAM api: unknown BlockingScalar ID"),
}
_ => Err("GAM api: unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
// scalars
Opcode::ClearCanvas(gid) => Message::Scalar(ScalarMessage {
2021-01-18 20:54:54 +00:00
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
Opcode::Redraw => Message::Scalar(ScalarMessage {
id: 1, arg1: 0, arg2: 0, arg3: 0, arg4: 0
}),
// blocking scalars
Opcode::GetCanvasBounds(gid) => Message::BlockingScalar(ScalarMessage {
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
Opcode::PowerDownRequest => Message::BlockingScalar(ScalarMessage {
id: 1, arg1: 0, arg2: 0, arg3: 0, arg4: 0
}),
_ => panic!("GAM api: Opcode type not handled by Into(), maybe you meant to use a helper method?"),
}
}
}
2021-04-04 21:47:17 +00:00
*/