forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflutter_ffi.rs
2106 lines (1822 loc) · 59.2 KB
/
flutter_ffi.rs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::{
client::file_trait::FileManager,
common::is_keyboard_mode_supported,
common::make_fd_to_json,
flutter::{self, session_add, session_add_existed, session_start_, sessions},
input::*,
ui_interface::{self, *},
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::{
common::get_default_sound_input,
keyboard::input_source::{change_input_source, get_cur_session_input_source},
};
use flutter_rust_bridge::{StreamSink, SyncReturn};
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::allow_err;
use hbb_common::{
config::{self, LocalConfig, PeerConfig, PeerInfoSerde},
fs, lazy_static, log,
message_proto::KeyboardMode,
rendezvous_proto::ConnType,
ResultType,
};
use std::{
collections::HashMap,
str::FromStr,
sync::{
atomic::{AtomicI32, Ordering},
Arc,
},
time::SystemTime,
};
pub type SessionID = uuid::Uuid;
lazy_static::lazy_static! {
static ref TEXTURE_RENDER_KEY: Arc<AtomicI32> = Arc::new(AtomicI32::new(0));
}
fn initialize(app_dir: &str) {
flutter::async_tasks::start_flutter_async_runner();
*config::APP_DIR.write().unwrap() = app_dir.to_owned();
#[cfg(target_os = "android")]
{
// flexi_logger can't work when android_logger initialized.
#[cfg(debug_assertions)]
android_logger::init_once(
android_logger::Config::default()
.with_max_level(log::LevelFilter::Debug) // limit log level
.with_tag("ffi"), // logs will show under mytag tag
);
#[cfg(not(debug_assertions))]
hbb_common::init_log(false, "");
#[cfg(feature = "mediacodec")]
scrap::mediacodec::check_mediacodec();
crate::common::test_rendezvous_server();
crate::common::test_nat_type();
crate::common::check_software_update();
}
#[cfg(target_os = "ios")]
{
use hbb_common::env_logger::*;
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "debug"));
}
}
#[inline]
pub fn start_global_event_stream(s: StreamSink<String>, app_type: String) -> ResultType<()> {
super::flutter::start_global_event_stream(s, app_type)
}
#[inline]
pub fn stop_global_event_stream(app_type: String) {
super::flutter::stop_global_event_stream(app_type)
}
pub enum EventToUI {
Event(String),
Rgba(usize),
Texture(usize),
}
pub fn host_stop_system_key_propagate(_stopped: bool) {
#[cfg(windows)]
crate::platform::windows::stop_system_key_propagate(_stopped);
}
// This function is only used to count the number of control sessions.
pub fn peer_get_default_sessions_count(id: String) -> SyncReturn<usize> {
SyncReturn(sessions::get_session_count(id, ConnType::DEFAULT_CONN))
}
pub fn session_add_existed_sync(id: String, session_id: SessionID) -> SyncReturn<String> {
if let Err(e) = session_add_existed(id.clone(), session_id) {
SyncReturn(format!("Failed to add session with id {}, {}", &id, e))
} else {
SyncReturn("".to_owned())
}
}
pub fn session_try_add_display(session_id: SessionID, displays: Vec<i32>) -> SyncReturn<()> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.capture_displays(displays, vec![], vec![]);
}
SyncReturn(())
}
pub fn session_add_sync(
session_id: SessionID,
id: String,
is_file_transfer: bool,
is_port_forward: bool,
is_rdp: bool,
switch_uuid: String,
force_relay: bool,
password: String,
) -> SyncReturn<String> {
if let Err(e) = session_add(
&session_id,
&id,
is_file_transfer,
is_port_forward,
is_rdp,
&switch_uuid,
force_relay,
password,
) {
SyncReturn(format!("Failed to add session with id {}, {}", &id, e))
} else {
SyncReturn("".to_owned())
}
}
pub fn session_start(
events2ui: StreamSink<EventToUI>,
session_id: SessionID,
id: String,
) -> ResultType<()> {
session_start_(&session_id, &id, events2ui)
}
pub fn session_get_remember(session_id: SessionID) -> Option<bool> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_remember())
} else {
None
}
}
pub fn session_get_toggle_option(session_id: SessionID, arg: String) -> Option<bool> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_toggle_option(arg))
} else {
None
}
}
pub fn session_get_toggle_option_sync(session_id: SessionID, arg: String) -> SyncReturn<bool> {
let res = session_get_toggle_option(session_id, arg) == Some(true);
SyncReturn(res)
}
pub fn session_get_option(session_id: SessionID, arg: String) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_option(arg))
} else {
None
}
}
pub fn session_login(
session_id: SessionID,
os_username: String,
os_password: String,
password: String,
remember: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.login(os_username, os_password, password, remember);
}
}
pub fn session_send2fa(
session_id: SessionID,
code: String,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.send2fa(code);
}
}
pub fn session_close(session_id: SessionID) {
if let Some(session) = sessions::remove_session_by_session_id(&session_id) {
session.close_event_stream(session_id);
session.close();
}
}
pub fn session_refresh(session_id: SessionID, display: usize) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.refresh_video(display as _);
}
}
pub fn session_record_screen(
session_id: SessionID,
start: bool,
display: usize,
width: usize,
height: usize,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.record_screen(start, display as _, width as _, height as _);
}
}
pub fn session_record_status(session_id: SessionID, status: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.record_status(status);
}
}
pub fn session_reconnect(session_id: SessionID, force_relay: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.reconnect(force_relay);
}
session_on_waiting_for_image_dialog_show(session_id);
}
pub fn session_toggle_option(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
log::warn!("toggle option {}", &value);
session.toggle_option(value.clone());
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if sessions::get_session_by_session_id(&session_id).is_some() && value == "disable-clipboard" {
crate::flutter::update_text_clipboard_required();
}
}
pub fn session_toggle_privacy_mode(session_id: SessionID, impl_key: String, on: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.toggle_privacy_mode(impl_key, on);
}
}
pub fn session_get_flutter_option(session_id: SessionID, k: String) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_flutter_option(k))
} else {
None
}
}
pub fn session_set_flutter_option(session_id: SessionID, k: String, v: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_flutter_option(k, v);
}
}
// This function is only used for the default connection session.
pub fn session_get_flutter_option_by_peer_id(id: String, k: String) -> Option<String> {
if let Some(session) = sessions::get_session_by_peer_id(id, ConnType::DEFAULT_CONN) {
Some(session.get_flutter_option(k))
} else {
None
}
}
pub fn get_next_texture_key() -> SyncReturn<i32> {
let k = TEXTURE_RENDER_KEY.fetch_add(1, Ordering::SeqCst) + 1;
SyncReturn(k)
}
pub fn get_local_flutter_option(k: String) -> SyncReturn<String> {
SyncReturn(ui_interface::get_local_flutter_option(k))
}
pub fn set_local_flutter_option(k: String, v: String) {
ui_interface::set_local_flutter_option(k, v);
}
pub fn get_local_kb_layout_type() -> SyncReturn<String> {
SyncReturn(ui_interface::get_kb_layout_type())
}
pub fn set_local_kb_layout_type(kb_layout_type: String) {
ui_interface::set_kb_layout_type(kb_layout_type)
}
pub fn session_get_view_style(session_id: SessionID) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_view_style())
} else {
None
}
}
pub fn session_set_view_style(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_view_style(value);
}
}
pub fn session_get_scroll_style(session_id: SessionID) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_scroll_style())
} else {
None
}
}
pub fn session_set_scroll_style(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_scroll_style(value);
}
}
pub fn session_get_image_quality(session_id: SessionID) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_image_quality())
} else {
None
}
}
pub fn session_set_image_quality(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_image_quality(value);
}
}
pub fn session_get_keyboard_mode(session_id: SessionID) -> Option<String> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_keyboard_mode())
} else {
None
}
}
pub fn session_set_keyboard_mode(session_id: SessionID, value: String) {
let mut _mode_updated = false;
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_keyboard_mode(value.clone());
_mode_updated = true;
}
#[cfg(windows)]
if _mode_updated {
crate::keyboard::update_grab_get_key_name(&value);
}
}
pub fn session_get_reverse_mouse_wheel_sync(session_id: SessionID) -> SyncReturn<Option<String>> {
let res = if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_reverse_mouse_wheel())
} else {
None
};
SyncReturn(res)
}
pub fn session_set_reverse_mouse_wheel(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_reverse_mouse_wheel(value);
}
}
pub fn session_get_displays_as_individual_windows(
session_id: SessionID,
) -> SyncReturn<Option<String>> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
SyncReturn(Some(session.get_displays_as_individual_windows()))
} else {
SyncReturn(None)
}
}
pub fn session_set_displays_as_individual_windows(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_displays_as_individual_windows(value);
}
}
pub fn session_get_use_all_my_displays_for_the_remote_session(
session_id: SessionID,
) -> SyncReturn<Option<String>> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
SyncReturn(Some(
session.get_use_all_my_displays_for_the_remote_session(),
))
} else {
SyncReturn(None)
}
}
pub fn session_set_use_all_my_displays_for_the_remote_session(
session_id: SessionID,
value: String,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_use_all_my_displays_for_the_remote_session(value);
}
}
pub fn session_get_custom_image_quality(session_id: SessionID) -> Option<Vec<i32>> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
Some(session.get_custom_image_quality())
} else {
None
}
}
pub fn session_is_keyboard_mode_supported(session_id: SessionID, mode: String) -> SyncReturn<bool> {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
if let Ok(mode) = KeyboardMode::from_str(&mode[..]) {
SyncReturn(is_keyboard_mode_supported(
&mode,
session.get_peer_version(),
&session.peer_platform(),
))
} else {
SyncReturn(false)
}
} else {
SyncReturn(false)
}
}
pub fn session_set_custom_image_quality(session_id: SessionID, value: i32) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.save_custom_image_quality(value);
}
}
pub fn session_set_custom_fps(session_id: SessionID, fps: i32) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.set_custom_fps(fps);
}
}
pub fn session_lock_screen(session_id: SessionID) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.lock_screen();
}
}
pub fn session_ctrl_alt_del(session_id: SessionID) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.ctrl_alt_del();
}
}
pub fn session_switch_display(is_desktop: bool, session_id: SessionID, value: Vec<i32>) {
sessions::session_switch_display(is_desktop, session_id, value);
}
pub fn session_handle_flutter_key_event(
session_id: SessionID,
name: String,
platform_code: i32,
position_code: i32,
lock_modes: i32,
down_or_up: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
let keyboard_mode = session.get_keyboard_mode();
session.handle_flutter_key_event(
&keyboard_mode,
&name,
platform_code,
position_code,
lock_modes,
down_or_up,
);
}
}
// SyncReturn<()> is used to make sure enter() and leave() are executed in the sequence this function is called.
//
// If the cursor jumps between remote page of two connections, leave view and enter view will be called.
// session_enter_or_leave() will be called then.
// As rust is multi-thread, it is possible that enter() is called before leave().
// This will cause the keyboard input to take no effect.
pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Some(session) = sessions::get_session_by_session_id(&_session_id) {
let keyboard_mode = session.get_keyboard_mode();
if _enter {
set_cur_session_id_(_session_id, &keyboard_mode);
session.enter(keyboard_mode);
} else {
session.leave(keyboard_mode);
}
}
SyncReturn(())
}
pub fn session_input_key(
session_id: SessionID,
name: String,
down: bool,
press: bool,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
// #[cfg(any(target_os = "android", target_os = "ios"))]
session.input_key(&name, down, press, alt, ctrl, shift, command);
}
}
pub fn session_input_string(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
// #[cfg(any(target_os = "android", target_os = "ios"))]
session.input_string(&value);
}
}
// chat_client_mode
pub fn session_send_chat(session_id: SessionID, text: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.send_chat(text);
}
}
pub fn session_peer_option(session_id: SessionID, name: String, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.set_option(name, value);
}
}
pub fn session_get_peer_option(session_id: SessionID, name: String) -> String {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
return session.get_option(name);
}
"".to_string()
}
pub fn session_input_os_password(session_id: SessionID, value: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.input_os_password(value, true);
}
}
// File Action
pub fn session_read_remote_dir(session_id: SessionID, path: String, include_hidden: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.read_remote_dir(path, include_hidden);
}
}
pub fn session_send_files(
session_id: SessionID,
act_id: i32,
path: String,
to: String,
file_num: i32,
include_hidden: bool,
is_remote: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.send_files(act_id, path, to, file_num, include_hidden, is_remote);
}
}
pub fn session_set_confirm_override_file(
session_id: SessionID,
act_id: i32,
file_num: i32,
need_override: bool,
remember: bool,
is_upload: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.set_confirm_override_file(act_id, file_num, need_override, remember, is_upload);
}
}
pub fn session_remove_file(
session_id: SessionID,
act_id: i32,
path: String,
file_num: i32,
is_remote: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.remove_file(act_id, path, file_num, is_remote);
}
}
pub fn session_read_dir_recursive(
session_id: SessionID,
act_id: i32,
path: String,
is_remote: bool,
show_hidden: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.remove_dir_all(act_id, path, is_remote, show_hidden);
}
}
pub fn session_remove_all_empty_dirs(
session_id: SessionID,
act_id: i32,
path: String,
is_remote: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.remove_dir(act_id, path, is_remote);
}
}
pub fn session_cancel_job(session_id: SessionID, act_id: i32) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.cancel_job(act_id);
}
}
pub fn session_create_dir(session_id: SessionID, act_id: i32, path: String, is_remote: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.create_dir(act_id, path, is_remote);
}
}
pub fn session_read_local_dir_sync(
_session_id: SessionID,
path: String,
show_hidden: bool,
) -> String {
if let Ok(fd) = fs::read_dir(&fs::get_path(&path), show_hidden) {
return make_fd_to_json(fd.id, path, &fd.entries);
}
"".to_string()
}
pub fn session_get_platform(session_id: SessionID, is_remote: bool) -> String {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
return session.get_platform(is_remote);
}
"".to_string()
}
pub fn session_load_last_transfer_jobs(session_id: SessionID) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
return session.load_last_jobs();
} else {
// a tip for flutter dev
eprintln!(
"cannot load last transfer job from non-existed session. Please ensure session \
is connected before calling load last transfer jobs."
);
}
}
pub fn session_add_job(
session_id: SessionID,
act_id: i32,
path: String,
to: String,
file_num: i32,
include_hidden: bool,
is_remote: bool,
) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.add_job(act_id, path, to, file_num, include_hidden, is_remote);
}
}
pub fn session_resume_job(session_id: SessionID, act_id: i32, is_remote: bool) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.resume_job(act_id, is_remote);
}
}
pub fn session_elevate_direct(session_id: SessionID) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.elevate_direct();
}
}
pub fn session_elevate_with_logon(session_id: SessionID, username: String, password: String) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.elevate_with_logon(username, password);
}
}
pub fn session_switch_sides(session_id: SessionID) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.switch_sides();
}
}
pub fn session_change_resolution(session_id: SessionID, display: i32, width: i32, height: i32) {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.change_resolution(display, width, height);
}
}
pub fn session_set_size(_session_id: SessionID, _display: usize, _width: usize, _height: usize) {
#[cfg(feature = "flutter_texture_render")]
super::flutter::session_set_size(_session_id, _display, _width, _height)
}
pub fn main_get_sound_inputs() -> Vec<String> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return get_sound_inputs();
#[cfg(any(target_os = "android", target_os = "ios"))]
vec![String::from("")]
}
pub fn main_get_default_sound_input() -> Option<String> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return get_default_sound_input();
#[cfg(any(target_os = "android", target_os = "ios"))]
None
}
pub fn main_get_login_device_info() -> SyncReturn<String> {
SyncReturn(get_login_device_info_json())
}
pub fn main_change_id(new_id: String) {
change_id(new_id)
}
pub fn main_get_async_status() -> String {
get_async_job_status()
}
pub fn main_get_option(key: String) -> String {
get_option(key)
}
pub fn main_get_option_sync(key: String) -> SyncReturn<String> {
SyncReturn(get_option(key))
}
pub fn main_get_error() -> String {
get_error()
}
pub fn main_show_option(_key: String) -> SyncReturn<bool> {
#[cfg(all(target_os = "linux", feature = "linux_headless"))]
#[cfg(not(any(feature = "flatpak", feature = "appimage")))]
if _key.eq(config::CONFIG_OPTION_ALLOW_LINUX_HEADLESS) {
return SyncReturn(true);
}
SyncReturn(false)
}
pub fn main_set_option(key: String, value: String) {
if key.eq("custom-rendezvous-server") {
set_option(key, value);
#[cfg(target_os = "android")]
crate::rendezvous_mediator::RendezvousMediator::restart();
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
crate::common::test_rendezvous_server();
} else {
set_option(key, value);
}
}
pub fn main_get_options() -> String {
get_options()
}
pub fn main_get_options_sync() -> SyncReturn<String> {
SyncReturn(get_options())
}
pub fn main_set_options(json: String) {
let map: HashMap<String, String> = serde_json::from_str(&json).unwrap_or(HashMap::new());
if !map.is_empty() {
set_options(map)
}
}
pub fn main_test_if_valid_server(server: String) -> String {
test_if_valid_server(server)
}
pub fn main_set_socks(proxy: String, username: String, password: String) {
set_socks(proxy, username, password)
}
pub fn main_get_socks() -> Vec<String> {
get_socks()
}
pub fn main_get_app_name() -> String {
get_app_name()
}
pub fn main_get_app_name_sync() -> SyncReturn<String> {
SyncReturn(get_app_name())
}
pub fn main_get_license() -> String {
get_license()
}
pub fn main_get_version() -> String {
get_version()
}
pub fn main_get_fav() -> Vec<String> {
get_fav()
}
pub fn main_store_fav(favs: Vec<String>) {
store_fav(favs)
}
pub fn main_get_peer_sync(id: String) -> SyncReturn<String> {
let conf = get_peer(id);
SyncReturn(serde_json::to_string(&conf).unwrap_or("".to_string()))
}
pub fn main_get_lan_peers() -> String {
serde_json::to_string(&get_lan_peers()).unwrap_or_default()
}
pub fn main_get_connect_status() -> String {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
serde_json::to_string(&get_connect_status()).unwrap_or("".to_string())
}
#[cfg(any(target_os = "android", target_os = "ios"))]
{
let mut state = hbb_common::config::get_online_state();
if state > 0 {
state = 1;
}
serde_json::json!({ "status_num": state }).to_string()
}
}
pub fn main_check_connect_status() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
start_option_status_sync(); // avoid multi calls
}
pub fn main_is_using_public_server() -> bool {
using_public_server()
}
pub fn main_discover() {
discover();
}
pub fn main_get_api_server() -> String {
get_api_server()
}
pub fn main_post_request(url: String, body: String, header: String) {
post_request(url, body, header)
}
pub fn main_get_local_option(key: String) -> SyncReturn<String> {
SyncReturn(get_local_option(key))
}
pub fn main_get_env(key: String) -> SyncReturn<String> {
SyncReturn(std::env::var(key).unwrap_or_default())
}
pub fn main_set_local_option(key: String, value: String) {
set_local_option(key, value)
}
pub fn main_get_input_source() -> SyncReturn<String> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let input_source = get_cur_session_input_source();
#[cfg(any(target_os = "android", target_os = "ios"))]
let input_source = "".to_owned();
SyncReturn(input_source)
}
pub fn main_set_input_source(session_id: SessionID, value: String) {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
change_input_source(session_id, value);
}
pub fn main_get_my_id() -> String {
get_id()
}
pub fn main_get_uuid() -> String {
get_uuid()
}
pub fn main_get_peer_option(id: String, key: String) -> String {
get_peer_option(id, key)
}
pub fn main_get_peer_option_sync(id: String, key: String) -> SyncReturn<String> {
SyncReturn(get_peer_option(id, key))
}
// Sometimes we need to get the flutter option of a peer by reading the file.
// Because the session may not be established yet.
pub fn main_get_peer_flutter_option_sync(id: String, k: String) -> SyncReturn<String> {
SyncReturn(get_peer_flutter_option(id, k))
}
pub fn main_set_peer_flutter_option_sync(id: String, k: String, v: String) -> SyncReturn<()> {
set_peer_flutter_option(id, k, v);
SyncReturn(())
}
pub fn main_set_peer_option(id: String, key: String, value: String) {
set_peer_option(id, key, value)
}
pub fn main_set_peer_option_sync(id: String, key: String, value: String) -> SyncReturn<bool> {
set_peer_option(id, key, value);
SyncReturn(true)
}
pub fn main_set_peer_alias(id: String, alias: String) {
set_peer_option(id, "alias".to_owned(), alias)
}
pub fn main_get_new_stored_peers() -> String {
let peers: Vec<String> = config::NEW_STORED_PEER_CONFIG
.lock()
.unwrap()
.drain()
.collect();
serde_json::to_string(&peers).unwrap_or_default()
}
pub fn main_forget_password(id: String) {
forget_password(id)
}
pub fn main_peer_has_password(id: String) -> bool {
peer_has_password(id)
}
pub fn main_peer_exists(id: String) -> bool {
peer_exists(&id)
}
pub fn main_load_recent_peers() {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers(None)
.drain(..)
.map(|(id, _, p)| peer_to_map(id, p))
.collect();
let data = HashMap::from([
("name", "load_recent_peers".to_owned()),
(
"peers",
serde_json::ser::to_string(&peers).unwrap_or("".to_owned()),
),
]);
let _res = flutter::push_global_event(
flutter::APP_TYPE_MAIN,
serde_json::ser::to_string(&data).unwrap_or("".to_owned()),
);
}
}
pub fn main_load_recent_peers_sync() -> SyncReturn<String> {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers(None)
.drain(..)
.map(|(id, _, p)| peer_to_map(id, p))
.collect();
let data = HashMap::from([
("name", "load_recent_peers".to_owned()),
(
"peers",
serde_json::ser::to_string(&peers).unwrap_or("".to_owned()),
),
]);
return SyncReturn(serde_json::ser::to_string(&data).unwrap_or("".to_owned()));
}
SyncReturn("".to_string())
}
pub fn main_load_lan_peers_sync() -> SyncReturn<String> {
let data = HashMap::from([
("name", "load_lan_peers".to_owned()),
(
"peers",
serde_json::to_string(&get_lan_peers()).unwrap_or_default(),
),
]);
return SyncReturn(serde_json::ser::to_string(&data).unwrap_or("".to_owned()));
}
pub fn main_load_ab_sync() -> SyncReturn<String> {
return SyncReturn(serde_json::to_string(&config::Ab::load()).unwrap_or_default());