1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pub mod jswm_con_error;
use ipcon_sys::ipcon::{Ipcon, IPF_SND_IF};
use serde_derive::{Deserialize, Serialize};
use std::ffi::CString;
#[allow(unused)]
use {
error_stack::{IntoReport, Report, Result, ResultExt},
jlogger::{jerror, jinfo, jtrace, jwarn, JloggerBuilder},
log::{error, info, trace, warn},
};
use jswm_con_error::JswmConError;
#[derive(Serialize, Deserialize, Debug)]
pub struct CmdShowRole {
cmd: String,
role: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CmdSetRolePos {
cmd: String,
role: String,
x: i32,
y: i32,
w: i32,
h: i32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CmdSetRoleRelativePos {
cmd: String,
role: String,
off_x: i32,
off_y: i32,
w: i32,
h: i32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CmdSetRoleVisibility {
cmd: String,
role: String,
visible: String,
}
pub enum JswmConCmd {
CmdShowRole(CmdShowRole),
CmdSetRolePos(CmdSetRolePos),
CmdSetRoleRelativePos(CmdSetRoleRelativePos),
CmdSetRoleVisibility(CmdSetRoleVisibility),
}
pub struct JswmCon {}
impl JswmCon {
fn send_request(ih: Option<&Ipcon>, cmd: JswmConCmd) -> Result<(), JswmConError> {
let buf = match cmd {
JswmConCmd::CmdShowRole(c) => serde_json::to_string(&c),
JswmConCmd::CmdSetRolePos(c) => serde_json::to_string(&c),
JswmConCmd::CmdSetRoleRelativePos(c) => serde_json::to_string(&c),
JswmConCmd::CmdSetRoleVisibility(c) => serde_json::to_string(&c),
}
.into_report()
.change_context(JswmConError::InvalidData)
.attach_printable("send_request(): invalid command data")?;
let c_buf = CString::new(buf)
.into_report()
.change_context(JswmConError::InvalidData)?;
if let Some(ih) = ih {
ih.send_unicast_msg("Jswm", c_buf.as_bytes())
.change_context(JswmConError::IOError)
} else {
let ih = Ipcon::new(None, Some(IPF_SND_IF)).change_context(JswmConError::IpconError)?;
ih.send_unicast_msg("Jswm", c_buf.as_bytes())
.change_context(JswmConError::IOError)
}
}
pub fn show_role(ih: Option<&Ipcon>, role: String) -> Result<(), JswmConError> {
JswmCon::send_request(
ih,
JswmConCmd::CmdShowRole(CmdShowRole {
cmd: String::from("cmdShowRole"),
role,
}),
)
}
pub fn set_role_pos(
ih: Option<&Ipcon>,
role: String,
x: i32,
y: i32,
w: i32,
h: i32,
) -> Result<(), JswmConError> {
if w < 0 || h < 0 {
return Err(JswmConError::InvalidData)
.into_report()
.attach_printable("Width/Height can not be negative");
}
JswmCon::send_request(
ih,
JswmConCmd::CmdSetRolePos(CmdSetRolePos {
cmd: String::from("cmdSetRolePos"),
role,
x,
y,
w,
h,
}),
)
}
pub fn set_role_relative_pos(
ih: Option<&Ipcon>,
role: String,
off_x: i32,
off_y: i32,
w: i32,
h: i32,
) -> Result<(), JswmConError> {
if w < 0 || h < 0 {
return Err(JswmConError::InvalidData)
.into_report()
.attach_printable("Width/Height can not be negative");
}
JswmCon::send_request(
ih,
JswmConCmd::CmdSetRoleRelativePos(CmdSetRoleRelativePos {
cmd: String::from("cmdSetRoleRelativePos"),
role,
off_x,
off_y,
w,
h,
}),
)
}
pub fn set_role_visibility(
ih: Option<&Ipcon>,
role: String,
on_off: bool,
) -> Result<(), JswmConError> {
let visible = if !on_off {
"off".to_string()
} else {
"on".to_string()
};
JswmCon::send_request(
ih,
JswmConCmd::CmdSetRoleVisibility(CmdSetRoleVisibility {
cmd: String::from("cmdSetRoleVisibility"),
role,
visible,
}),
)
}
}