kplayer_rust_wrap/kplayer/
mod.rs

1pub mod plugin;
2pub mod proto;
3pub mod util;
4
5super::version!();
6
7#[allow(dead_code)]
8extern "C" {
9    fn GetValidateArgIterator() -> i32;
10    fn ResetValidateArgIterator() -> i32;
11    fn NewTimerTask(tid: i32, milliseconds: i32) -> i32;
12    fn RegisterMessageAction(action: i32) -> i32;
13    fn GetHistoryEventMessage(action: i32) -> i32;
14}
15
16static mut ARGS_INDEX: usize = 0;
17static mut ALLOW_ARGS_INDEX: usize = 0;
18static mut INSTANCES: Vec<Box<dyn plugin::BasePlugin>> = Vec::new();
19static mut INSTANCES_INDEX: usize = 0;
20
21pub fn export_plugin(p: Box<dyn plugin::BasePlugin>) {
22    unsafe {
23        INSTANCES.push(p);
24    }
25}
26
27pub fn register_task() {
28    unsafe {
29        let task = INSTANCES[INSTANCES_INDEX].register_task();
30        for item in task {
31            NewTimerTask(item.get_tid(), item.get_milliseconds());
32        }
33    }
34}
35
36pub fn register_message() {
37    unsafe {
38        let task = INSTANCES[INSTANCES_INDEX].register_message_keys();
39        for item in task {
40            RegisterMessageAction(item as i32);
41        }
42    }
43}
44
45pub fn get_history_message(action: proto::keys::EventMessageAction) -> String {
46    unsafe {
47        let msg_index = GetHistoryEventMessage(action as i32);
48        util::string::DynamicString::receive(msg_index).unwrap()
49    }
50}
51
52#[no_mangle]
53pub extern "C" fn GetName() -> i32 {
54    unsafe {
55        util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_name().as_bytes())
56            .get_index()
57    }
58}
59
60#[no_mangle]
61pub extern "C" fn GetFilterName() -> i32 {
62    unsafe {
63        util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_filter_name().as_bytes())
64            .get_index()
65    }
66}
67
68#[no_mangle]
69pub extern "C" fn GetAuthor() -> i32 {
70    unsafe {
71        util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_author().as_bytes())
72            .get_index()
73    }
74}
75
76#[no_mangle]
77pub extern "C" fn GetMediaType() -> i32 {
78    unsafe {
79        let media_type: plugin::MediaType = INSTANCES[INSTANCES_INDEX].get_media_type();
80        media_type as i32
81    }
82}
83
84#[no_mangle]
85pub extern "C" fn GetArgIterator() -> i32 {
86    let mut custom_args: Vec<String> = Vec::new();
87
88    unsafe {
89        // get custom args
90        loop {
91            let re_index = GetValidateArgIterator();
92            if re_index == 0 {
93                break;
94            }
95
96            match util::string::DynamicString::receive(re_index) {
97                Ok(_ok) => custom_args.push(_ok),
98                Err(_err) => {
99                    return util::string::DynamicString::from(
100                        String::from("plugin receive args error").as_bytes(),
101                    )
102                    .get_index()
103                }
104            }
105        }
106
107        // get plugin args
108        let args = INSTANCES[INSTANCES_INDEX]
109            .get_args(util::argument::args_vec_to_map(custom_args).unwrap());
110
111        if ARGS_INDEX >= args.len() {
112            ARGS_INDEX = 0;
113            return 0;
114        }
115
116        let iterator = util::string::DynamicString::from(args[ARGS_INDEX].as_bytes()).get_index();
117        ARGS_INDEX = ARGS_INDEX + 1;
118
119        iterator
120    }
121}
122
123#[no_mangle]
124pub extern "C" fn GetAllowArgIterator() -> i32 {
125    unsafe {
126        // get plugin allow override args
127        let args = INSTANCES[INSTANCES_INDEX].get_allow_custom_args();
128
129        if ALLOW_ARGS_INDEX >= args.len() {
130            return 0;
131        }
132
133        let iterator =
134            util::string::DynamicString::from(args[ALLOW_ARGS_INDEX].as_bytes()).get_index();
135        ALLOW_ARGS_INDEX = ALLOW_ARGS_INDEX + 1;
136
137        iterator
138    }
139}
140
141#[no_mangle]
142pub extern "C" fn ValidateUserArgs() -> i32 {
143    let mut args: Vec<String> = Vec::new();
144
145    unsafe {
146        // get warp args
147        loop {
148            let re_index = GetValidateArgIterator();
149            if re_index == 0 {
150                break;
151            }
152
153            match util::string::DynamicString::receive(re_index) {
154                Ok(_ok) => args.push(_ok),
155                Err(_err) => {
156                    return util::string::DynamicString::from(
157                        String::from("plugin receive args error").as_bytes(),
158                    )
159                    .get_index()
160                }
161            }
162        }
163
164        match INSTANCES[INSTANCES_INDEX]
165            .validate_user_args(util::argument::args_vec_to_map(args).unwrap())
166        {
167            Ok(_s) => 0,
168            Err(_err) => util::string::DynamicString::from(_err.as_bytes()).get_index(),
169        }
170    }
171}
172
173#[no_mangle]
174pub extern "C" fn NotifyTask(_tid: i32) -> i32 {
175    unsafe {
176        INSTANCES[INSTANCES_INDEX].execute_task(_tid as u32);
177    }
178    0
179}
180
181#[no_mangle]
182pub extern "C" fn NotifyMessage(action: i32, message: i32) -> i32 {
183    unsafe {
184        let body = util::string::DynamicString::receive(message).unwrap();
185        INSTANCES[INSTANCES_INDEX].execute_message(action, body);
186    }
187    0
188}
189
190#[no_mangle]
191pub extern "C" fn GetInstanceCount() -> i64 {
192    unsafe { INSTANCES.len() as i64 }
193}
194
195#[no_mangle]
196pub extern "C" fn SetInstanceIndex(index: i64) -> i64 {
197    unsafe {
198        let count = INSTANCES.len() as i64;
199        if index > count - 1 {
200            return -1;
201        }
202
203        INSTANCES_INDEX = index as usize;
204        index
205    }
206}
207
208#[no_mangle]
209pub extern "C" fn GetInstanceType(index: i64) -> i32 {
210    unsafe {
211        let count = INSTANCES.len() as i64;
212        if index > count - 1 {
213            return -1;
214        }
215
216        let instance_type = INSTANCES[index as usize].get_instance_type();
217        instance_type as i32
218    }
219}
220
221#[no_mangle]
222pub extern "C" fn HookCreated() -> i32 {
223    unsafe { INSTANCES[INSTANCES_INDEX].hook_created() }
224}