maa_framework/custom/
custom_controller.rs

1use std::ffi::c_void;
2
3use crate::{
4    internal,
5    string,
6};
7
8#[allow(unused)]
9pub trait MaaCustomController {
10    fn connect(&mut self) -> bool {
11        false
12    }
13    fn request_uuid(&mut self) -> Option<String> {
14        None
15    }
16
17    /// Return value
18    ///
19    /// (width, height)
20    fn request_resolution(&mut self) -> Option<(i32, i32)> {
21        None
22    }
23    fn start_app(&mut self, intent: String) -> bool {
24        false
25    }
26    fn stop_app(&mut self, intent: String) -> bool {
27        false
28    }
29
30    /// # Return value
31    ///
32    /// (rows,cols,typ,data)
33    fn screencap(&mut self) -> Option<(i32, i32, i32, *mut c_void)> {
34        None
35    }
36
37    fn click(&mut self, x: i32, y: i32) -> bool {
38        false
39    }
40    fn swipe(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, duration: i32) -> bool {
41        false
42    }
43    fn touch_down(&mut self, contact: i32, x: i32, y: i32, pressure: i32) -> bool {
44        false
45    }
46    fn touch_move(&mut self, contact: i32, x: i32, y: i32, pressure: i32) -> bool {
47        false
48    }
49    fn touch_up(&mut self, contact: i32) -> bool {
50        false
51    }
52    fn press_key(&mut self, key: i32) -> bool {
53        false
54    }
55    fn input_text(&mut self, text: String) -> bool {
56        false
57    }
58}
59
60pub(crate) unsafe extern "C" fn custom_controller_connect<C>(
61    controller: internal::MaaTransparentArg,
62) -> internal::MaaBool
63where
64    C: MaaCustomController,
65{
66    let controller = &mut *(controller as *mut C);
67    internal::MaaBool::from(controller.connect())
68}
69
70pub(crate) unsafe extern "C" fn custom_controller_request_uuid<C>(
71    controller: internal::MaaTransparentArg,
72    buffer: internal::MaaStringBufferHandle,
73) -> internal::MaaBool
74where
75    C: MaaCustomController,
76{
77    let controller = &mut *(controller as *mut C);
78    let ret = match controller.request_uuid() {
79        Some(uuid) => {
80            let uuid = internal::to_cstring(&uuid);
81            internal::MaaSetString(buffer, uuid);
82            true
83        }
84        None => false,
85    };
86
87    internal::MaaBool::from(ret)
88}
89
90pub(crate) unsafe extern "C" fn custom_controller_request_resolution<C>(
91    controller: internal::MaaTransparentArg,
92    width: *mut i32,
93    height: *mut i32,
94) -> internal::MaaBool
95where
96    C: MaaCustomController,
97{
98    let controller = &mut *(controller as *mut C);
99    let ret = match controller.request_resolution() {
100        Some((w, h)) => {
101            *width = w;
102            *height = h;
103            true
104        }
105        None => false,
106    };
107
108    internal::MaaBool::from(ret)
109}
110
111pub(crate) unsafe extern "C" fn custom_controller_start_app<C>(
112    intent: internal::MaaStringView,
113    controller: internal::MaaTransparentArg,
114) -> internal::MaaBool
115where
116    C: MaaCustomController,
117{
118    let controller = &mut *(controller as *mut C);
119    let intent = string!(intent);
120    internal::MaaBool::from(controller.start_app(intent))
121}
122
123pub(crate) unsafe extern "C" fn custom_controller_stop_app<C>(
124    intent: internal::MaaStringView,
125    controller: internal::MaaTransparentArg,
126) -> internal::MaaBool
127where
128    C: MaaCustomController,
129{
130    let controller = &mut *(controller as *mut C);
131    let intent = string!(intent);
132    internal::MaaBool::from(controller.stop_app(intent))
133}
134
135pub(crate) unsafe extern "C" fn custom_controller_screencap<C>(
136    controller: internal::MaaTransparentArg,
137    buffer: internal::MaaImageBufferHandle,
138) -> internal::MaaBool
139where
140    C: MaaCustomController,
141{
142    let controller = &mut *(controller as *mut C);
143    let ret = match controller.screencap() {
144        Some((rows, cols, typ, data)) => {
145            internal::MaaSetImageRawData(buffer, data, cols, rows, typ);
146            true
147        }
148        None => false,
149    };
150
151    internal::MaaBool::from(ret)
152}
153
154pub(crate) unsafe extern "C" fn custom_controller_click<C>(
155    x: i32,
156    y: i32,
157    controller: internal::MaaTransparentArg,
158) -> internal::MaaBool
159where
160    C: MaaCustomController,
161{
162    let controller = &mut *(controller as *mut C);
163    internal::MaaBool::from(controller.click(x, y))
164}
165
166pub(crate) unsafe extern "C" fn custom_controller_swipe<C>(
167    x1: i32,
168    y1: i32,
169    x2: i32,
170    y2: i32,
171    duration: i32,
172    controller: internal::MaaTransparentArg,
173) -> internal::MaaBool
174where
175    C: MaaCustomController,
176{
177    let controller = &mut *(controller as *mut C);
178    internal::MaaBool::from(controller.swipe(x1, y1, x2, y2, duration))
179}
180
181pub(crate) unsafe extern "C" fn custom_controller_touch_down<C>(
182    contact: i32,
183    x: i32,
184    y: i32,
185    pressure: i32,
186    controller: internal::MaaTransparentArg,
187) -> internal::MaaBool
188where
189    C: MaaCustomController,
190{
191    let controller = &mut *(controller as *mut C);
192    internal::MaaBool::from(controller.touch_down(contact, x, y, pressure))
193}
194
195pub(crate) unsafe extern "C" fn custom_controller_touch_move<C>(
196    contact: i32,
197    x: i32,
198    y: i32,
199    pressure: i32,
200    controller: internal::MaaTransparentArg,
201) -> internal::MaaBool
202where
203    C: MaaCustomController,
204{
205    let controller = &mut *(controller as *mut C);
206    internal::MaaBool::from(controller.touch_move(contact, x, y, pressure))
207}
208
209pub(crate) unsafe extern "C" fn custom_controller_touch_up<C>(
210    contact: i32,
211    controller: internal::MaaTransparentArg,
212) -> internal::MaaBool
213where
214    C: MaaCustomController,
215{
216    let controller = &mut *(controller as *mut C);
217    internal::MaaBool::from(controller.touch_up(contact))
218}
219
220pub(crate) unsafe extern "C" fn custom_controller_press_key<C>(
221    key: i32,
222    controller: internal::MaaTransparentArg,
223) -> internal::MaaBool
224where
225    C: MaaCustomController,
226{
227    let controller = &mut *(controller as *mut C);
228    internal::MaaBool::from(controller.press_key(key))
229}
230
231pub(crate) unsafe extern "C" fn custom_controller_input_text<C>(
232    text: internal::MaaStringView,
233    controller: internal::MaaTransparentArg,
234) -> internal::MaaBool
235where
236    C: MaaCustomController,
237{
238    let controller = &mut *(controller as *mut C);
239    let text = string!(text);
240    internal::MaaBool::from(controller.input_text(text))
241}