-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmwb-desktop-ui.sh
More file actions
executable file
·1339 lines (1147 loc) · 52.6 KB
/
Copy pathmwb-desktop-ui.sh
File metadata and controls
executable file
·1339 lines (1147 loc) · 52.6 KB
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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="InputFlow"
SERVICE_NAME="mwb-client.service"
CONFIG_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/mwb-client/config.ini"
STATE_PATH="${XDG_STATE_HOME:-$HOME/.local/state}/mwb-client/state.ini"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AUTO_CONNECT_CONFIG_KEY="${MWB_AUTO_CONNECT_CONFIG_KEY:-auto_connect_enabled}"
RECONNECT_INITIAL_CONFIG_KEY="${MWB_RECONNECT_INITIAL_CONFIG_KEY:-reconnect_initial_backoff_ms}"
RECONNECT_MAX_CONFIG_KEY="${MWB_RECONNECT_MAX_CONFIG_KEY:-reconnect_max_backoff_ms}"
RECONNECT_IDLE_CONFIG_KEY="${MWB_RECONNECT_IDLE_CONFIG_KEY:-reconnect_idle_retry_ms}"
MPRIS_MEDIA_KEYS_CONFIG_KEY="${MWB_MPRIS_MEDIA_KEYS_CONFIG_KEY:-mpris_media_keys_enabled}"
MPRIS_PLAYER_CONFIG_KEY="${MWB_MPRIS_PLAYER_CONFIG_KEY:-mpris_player}"
LATENCY_REPORT_CONFIG_KEY="${MWB_LATENCY_REPORT_CONFIG_KEY:-latency_report}"
DIAGNOSTICS_BUNDLE_SCRIPT="$SCRIPT_DIR/scripts/inputflow-diagnostics-bundle.sh"
DEFAULT_AUTO_CONNECT_ENABLED="${MWB_DEFAULT_AUTO_CONNECT_ENABLED:-true}"
DEFAULT_RECONNECT_INITIAL_MS="${MWB_DEFAULT_RECONNECT_INITIAL_MS:-1000}"
DEFAULT_RECONNECT_MAX_MS="${MWB_DEFAULT_RECONNECT_MAX_MS:-300000}"
DEFAULT_RECONNECT_IDLE_MS="${MWB_DEFAULT_RECONNECT_IDLE_MS:-900000}"
SECRET_ID_CONFIG_KEY="${MWB_SECRET_ID_CONFIG_KEY:-key_secret_id}"
SECRET_STORE_SET_COMMAND="${MWB_SECRET_STORE_SET_COMMAND:-secret-store}"
SECRET_STORE_CLEAR_COMMAND="${MWB_SECRET_STORE_CLEAR_COMMAND:-secret-clear}"
SECRET_STORE_ID_OPTION="${MWB_SECRET_STORE_ID_OPTION:---secret-id}"
SECRET_STORE_STDIN_OPTION="${MWB_SECRET_STORE_STDIN_OPTION:---stdin}"
SECRET_ID_CONFIG_KEY_ALIASES=("$SECRET_ID_CONFIG_KEY" "secret_id" "key_secret_id" "key_secret" "secret_service_id")
resolve_repo_binary() {
local binary_name="$1"
local best_candidate="" best_mtime=-1 candidate mtime
for candidate in "$SCRIPT_DIR"/build*/"$binary_name"; do
[[ "$candidate" == *sanitize* ]] && continue
if [[ -x "$candidate" ]]; then
mtime="$(stat -c '%Y' "$candidate" 2>/dev/null || printf '0')"
if (( mtime > best_mtime )); then
best_candidate="$candidate"
best_mtime="$mtime"
fi
fi
done
[[ -n "$best_candidate" ]] && printf '%s\n' "$best_candidate" && return 0
for candidate in "$SCRIPT_DIR"/build*/"$binary_name"; do
if [[ -x "$candidate" ]]; then
printf '%s\n' "$candidate"
return 0
fi
done
command -v "$binary_name" 2>/dev/null || return 1
}
resolve_binary() {
resolve_repo_binary "mwb_client"
}
resolve_tray_binary() {
resolve_repo_binary "mwb_tray"
}
resolve_repo_icon() {
local candidate
for candidate in \
"$SCRIPT_DIR/assets/icons/inputflow-desktop.svg" \
"$SCRIPT_DIR/assets/icons/inputflow-tray.svg"; do
if [[ -f "$candidate" ]]; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
APP_BIN="$(resolve_binary || true)"
TRAY_BIN="$(resolve_tray_binary || true)"
APP_ICON_PATH="$(resolve_repo_icon || true)"
require_ui() {
if ! command -v zenity >/dev/null 2>&1; then
printf 'zenity is required for %s desktop UI.\n' "$APP_NAME" >&2
exit 1
fi
if ! python3 -c "import gi; gi.require_version('Gtk', '3.0')" >/dev/null 2>&1; then
printf 'python3-gi and GTK3 are required for %s desktop UI.\n' "$APP_NAME" >&2
exit 1
fi
}
require_client_binary() {
if [[ -z "${APP_BIN:-}" ]]; then
zenity --error --text="Could not find a built mwb_client binary. Build the project first."
return 1
fi
}
require_tray_binary() {
if [[ -z "${TRAY_BIN:-}" ]]; then
zenity --error --text="Could not find a built mwb_tray binary. Build the tray target first."
return 1
fi
}
start_tray() {
require_tray_binary || return 1
if pgrep -x "mwb_tray" >/dev/null; then
printf 'Restarting existing InputFlow tray...\n'
pkill -x "mwb_tray" || true
# Give the lock file a moment to be released
sleep 0.5
fi
exec "$TRAY_BIN"
}
trim_whitespace() {
local value="$1"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
printf '%s\n' "$value"
}
format_yes_no() {
case "${1:-}" in
true|TRUE|yes|YES|1) printf 'Yes' ;;
*) printf 'No' ;;
esac
}
format_paired_label() {
if [[ "${1:-}" == "true" ]]; then
printf 'Paired'
else
printf 'Not yet'
fi
}
format_epoch_label() {
local epoch="${1:-0}"
if [[ -z "$epoch" || "$epoch" == "0" ]]; then
printf 'Never'
return 0
fi
date -d "@$epoch" '+%Y-%m-%d %H:%M:%S' 2>/dev/null || printf '%s' "$epoch"
}
strip_matching_quotes() {
local value="$1"
if [[ "${#value}" -ge 2 ]]; then
local first_char="${value:0:1}"
local last_char="${value: -1}"
if [[ "$first_char" == "$last_char" && ( "$first_char" == '"' || "$first_char" == "'" ) ]]; then
value="${value:1:${#value}-2}"
fi
fi
printf '%s\n' "$value"
}
config_has_key() {
local wanted_key="$1"
local line line_key
[[ -f "$CONFIG_PATH" ]] || return 1
while IFS= read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^[[:space:]]*([A-Za-z0-9_.-]+)[[:space:]]*= ]] || continue
line_key="${BASH_REMATCH[1]}"
[[ "$line_key" == "$wanted_key" ]] && return 0
done <"$CONFIG_PATH"
return 1
}
read_config_value() {
local key="$1"
local line line_key line_value value=""
[[ -f "$CONFIG_PATH" ]] || return 0
while IFS= read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^[[:space:]]*([A-Za-z0-9_.-]+)[[:space:]]*=(.*)$ ]] || continue
line_key="${BASH_REMATCH[1]}"
[[ "$line_key" == "$key" ]] || continue
line_value="${BASH_REMATCH[2]}"
line_value="$(trim_whitespace "$line_value")"
value="$(strip_matching_quotes "$line_value")"
done <"$CONFIG_PATH"
printf '%s\n' "$value"
}
read_secret_id_value() {
local key_name
for key_name in "${SECRET_ID_CONFIG_KEY_ALIASES[@]}"; do
if config_has_key "$key_name"; then
read_config_value "$key_name"
return 0
fi
done
return 0
}
detect_secret_id_key_name() {
local key_name
for key_name in "${SECRET_ID_CONFIG_KEY_ALIASES[@]}"; do
if config_has_key "$key_name"; then
printf '%s\n' "$key_name"
return 0
fi
done
printf '%s\n' "$SECRET_ID_CONFIG_KEY"
}
canonical_managed_key() {
local input_key="$1"
local secret_key_name="$2"
local candidate
case "$input_key" in
host|key|key_file|machine_name|port|screen_width|screen_height|clipboard_enabled|clipboard_send_enabled|clipboard_force_poll|clipboard_poll_ms|"$MPRIS_MEDIA_KEYS_CONFIG_KEY"|"$MPRIS_PLAYER_CONFIG_KEY"|"$LATENCY_REPORT_CONFIG_KEY"|"$AUTO_CONNECT_CONFIG_KEY"|"$RECONNECT_INITIAL_CONFIG_KEY"|"$RECONNECT_MAX_CONFIG_KEY"|"$RECONNECT_IDLE_CONFIG_KEY")
printf '%s\n' "$input_key"
return 0
;;
esac
for candidate in "${SECRET_ID_CONFIG_KEY_ALIASES[@]}"; do
if [[ "$input_key" == "$candidate" ]]; then
printf '%s\n' "$secret_key_name"
return 0
fi
done
return 1
}
write_config() {
local host="$1" key="$2" key_file="$3" secret_id="$4" machine_name="$5" port="$6" auto_connect_enabled="$7" reconnect_initial_backoff_ms="$8" reconnect_max_backoff_ms="$9" reconnect_idle_retry_ms="${10}" clipboard_enabled="${11}" clipboard_send_enabled="${12}" clipboard_force_poll="${13}" clipboard_poll_ms="${14}" screen_width="${15}" screen_height="${16}" mpris_media_keys_enabled="${17}" mpris_player="${18}" latency_report="${19}"
local secret_key_name="${20:-$(detect_secret_id_key_name)}"
local tmp_path line existing_key managed_key
local -a existing_lines=()
local -a ordered_keys=("host" "key" "key_file" "$secret_key_name" "machine_name" "port" "screen_width" "screen_height" "$AUTO_CONNECT_CONFIG_KEY" "$RECONNECT_INITIAL_CONFIG_KEY" "$RECONNECT_MAX_CONFIG_KEY" "$RECONNECT_IDLE_CONFIG_KEY" "clipboard_enabled" "clipboard_send_enabled" "clipboard_force_poll" "clipboard_poll_ms" "$MPRIS_MEDIA_KEYS_CONFIG_KEY" "$MPRIS_PLAYER_CONFIG_KEY" "$LATENCY_REPORT_CONFIG_KEY")
local -A values=(
[host]="$host"
[key]="$key"
[key_file]="$key_file"
["$secret_key_name"]="$secret_id"
[machine_name]="$machine_name"
[port]="$port"
[screen_width]="$screen_width"
[screen_height]="$screen_height"
["$AUTO_CONNECT_CONFIG_KEY"]="$auto_connect_enabled"
["$RECONNECT_INITIAL_CONFIG_KEY"]="$reconnect_initial_backoff_ms"
["$RECONNECT_MAX_CONFIG_KEY"]="$reconnect_max_backoff_ms"
["$RECONNECT_IDLE_CONFIG_KEY"]="$reconnect_idle_retry_ms"
[clipboard_enabled]="$clipboard_enabled"
[clipboard_send_enabled]="$clipboard_send_enabled"
[clipboard_force_poll]="$clipboard_force_poll"
[clipboard_poll_ms]="$clipboard_poll_ms"
["$MPRIS_MEDIA_KEYS_CONFIG_KEY"]="$mpris_media_keys_enabled"
["$MPRIS_PLAYER_CONFIG_KEY"]="$mpris_player"
["$LATENCY_REPORT_CONFIG_KEY"]="$latency_report"
)
local -A seen=()
mkdir -p "$(dirname "$CONFIG_PATH")"
tmp_path="$(mktemp "${CONFIG_PATH}.tmp.XXXXXX")"
if [[ -f "$CONFIG_PATH" ]]; then
mapfile -t existing_lines <"$CONFIG_PATH"
fi
for line in "${existing_lines[@]}"; do
if [[ "$line" =~ ^[[:space:]]*([A-Za-z0-9_.-]+)[[:space:]]*= ]]; then
existing_key="${BASH_REMATCH[1]}"
if managed_key="$(canonical_managed_key "$existing_key" "$secret_key_name")"; then
if [[ -z "${seen[$managed_key]+x}" ]]; then
printf '%s=%s\n' "$managed_key" "${values[$managed_key]}" >>"$tmp_path"
seen["$managed_key"]=1
fi
continue
fi
fi
printf '%s\n' "$line" >>"$tmp_path"
done
for managed_key in "${ordered_keys[@]}"; do
if [[ -z "${seen[$managed_key]+x}" ]]; then
printf '%s=%s\n' "$managed_key" "${values[$managed_key]}" >>"$tmp_path"
fi
done
mv "$tmp_path" "$CONFIG_PATH"
}
set_configured_host() {
local new_host="$1"
local current_host key key_file secret_id secret_key_name machine_name port auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms clipboard_enabled clipboard_send_enabled clipboard_force_poll clipboard_poll_ms screen_width screen_height mpris_media_keys_enabled mpris_player latency_report
current_host="$(read_config_value host)"
if [[ "$new_host" == "$current_host" ]]; then
zenity --info --text="$new_host is already the configured Windows host."
return 0
fi
key="$(read_config_value key)"
key_file="$(read_config_value key_file)"
secret_id="$(read_secret_id_value)"
secret_key_name="$(detect_secret_id_key_name)"
machine_name="$(read_config_value machine_name)"
port="$(read_config_value port)"; [[ -n "$port" ]] || port="15101"
IFS=$'\t' read -r auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms < <(read_connection_behavior_values)
clipboard_enabled="$(read_config_value clipboard_enabled)"; [[ -n "$clipboard_enabled" ]] || clipboard_enabled="true"
clipboard_send_enabled="$(read_config_value clipboard_send_enabled)"; [[ -n "$clipboard_send_enabled" ]] || clipboard_send_enabled="true"
clipboard_force_poll="$(read_config_value clipboard_force_poll)"; [[ -n "$clipboard_force_poll" ]] || clipboard_force_poll="false"
clipboard_poll_ms="$(read_config_value clipboard_poll_ms)"; [[ -n "$clipboard_poll_ms" ]] || clipboard_poll_ms="1000"
screen_width="$(read_config_value screen_width)"
screen_height="$(read_config_value screen_height)"
mpris_media_keys_enabled="$(read_config_value "$MPRIS_MEDIA_KEYS_CONFIG_KEY")"; [[ -n "$mpris_media_keys_enabled" ]] || mpris_media_keys_enabled="true"
mpris_player="$(read_config_value "$MPRIS_PLAYER_CONFIG_KEY")"
latency_report="$(read_config_value "$LATENCY_REPORT_CONFIG_KEY")"; [[ -n "$latency_report" ]] || latency_report="false"
write_config "$new_host" "$key" "$key_file" "$secret_id" "$machine_name" "$port" "$auto_connect_enabled" "$reconnect_initial_backoff_ms" "$reconnect_max_backoff_ms" "$reconnect_idle_retry_ms" "$clipboard_enabled" "$clipboard_send_enabled" "$clipboard_force_poll" "$clipboard_poll_ms" "$screen_width" "$screen_height" "$mpris_media_keys_enabled" "$mpris_player" "$latency_report" "$secret_key_name"
zenity --info --text="Configured Windows host updated to $new_host."
offer_service_restart_if_active "The configured Windows host changed while the background service is running."
}
remove_peer_state() {
local target_host="$1" target_port="$2"
local tmp_path line host port
[[ -f "$STATE_PATH" ]] || return 0
mkdir -p "$(dirname "$STATE_PATH")"
tmp_path="$(mktemp "${STATE_PATH}.tmp.XXXXXX")"
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == peer=* ]]; then
IFS=$'\t' read -r host _name port _rest <<<"${line#peer=}"
if [[ "$host" == "$target_host" && "$port" == "$target_port" ]]; then
continue
fi
fi
printf '%s\n' "$line" >>"$tmp_path"
done <"$STATE_PATH"
mv "$tmp_path" "$STATE_PATH"
}
read_connection_behavior_values() {
local auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms
auto_connect_enabled="$(read_config_value "$AUTO_CONNECT_CONFIG_KEY")"
reconnect_initial_backoff_ms="$(read_config_value "$RECONNECT_INITIAL_CONFIG_KEY")"
reconnect_max_backoff_ms="$(read_config_value "$RECONNECT_MAX_CONFIG_KEY")"
reconnect_idle_retry_ms="$(read_config_value "$RECONNECT_IDLE_CONFIG_KEY")"
[[ -n "$auto_connect_enabled" ]] || auto_connect_enabled="$DEFAULT_AUTO_CONNECT_ENABLED"
[[ -n "$reconnect_initial_backoff_ms" ]] || reconnect_initial_backoff_ms="$DEFAULT_RECONNECT_INITIAL_MS"
[[ -n "$reconnect_max_backoff_ms" ]] || reconnect_max_backoff_ms="$DEFAULT_RECONNECT_MAX_MS"
[[ -n "$reconnect_idle_retry_ms" ]] || reconnect_idle_retry_ms="$DEFAULT_RECONNECT_IDLE_MS"
printf '%s\t%s\t%s\t%s\n' \
"$auto_connect_enabled" \
"$reconnect_initial_backoff_ms" \
"$reconnect_max_backoff_ms" \
"$reconnect_idle_retry_ms"
}
connection_behavior_mode_label() {
local auto_connect_enabled="$1"
if [[ "$auto_connect_enabled" == "true" ]]; then
printf 'Auto-connect to configured host'
return 0
fi
printf 'Manual start only'
}
connection_behavior_summary() {
local auto_connect_enabled="$1" reconnect_initial_backoff_ms="$2" reconnect_max_backoff_ms="$3" reconnect_idle_retry_ms="$4"
printf '%s\nRetry: %s ms to %s ms\nOffline idle retry: %s ms' \
"$(connection_behavior_mode_label "$auto_connect_enabled")" \
"$reconnect_initial_backoff_ms" \
"$reconnect_max_backoff_ms" \
"$reconnect_idle_retry_ms"
}
configured_auth_source_count() {
local key="$1" key_file="$2" secret_id="$3"
local count=0
[[ -n "$key" ]] && (( count += 1 ))
[[ -n "$key_file" ]] && (( count += 1 ))
[[ -n "$secret_id" ]] && (( count += 1 ))
printf '%s\n' "$count"
}
configured_auth_mode() {
local key="$1" key_file="$2" secret_id="$3"
if [[ -n "$secret_id" ]]; then
printf 'Secret Service entry\n'
return 0
fi
if [[ -n "$key_file" ]]; then
printf 'Key file path\n'
return 0
fi
printf 'Inline security key\n'
}
configured_auth_label() {
local key="$1" key_file="$2" secret_id="$3"
local count
count="$(configured_auth_source_count "$key" "$key_file" "$secret_id")"
if (( count > 1 )); then
printf 'Conflicting key sources'
return 0
fi
if [[ -n "$secret_id" ]]; then
printf 'Secret Service'
return 0
fi
if [[ -n "$key_file" ]]; then
printf 'Key file'
return 0
fi
if [[ -n "$key" ]]; then
printf 'Inline key'
return 0
fi
printf 'Not configured'
}
is_integer_in_range() {
local value="$1" min_value="$2" max_value="$3"
[[ "$value" =~ ^[0-9]+$ ]] && (( value >= min_value && value <= max_value ))
}
read_peer_state() {
local wanted_host="$1" wanted_port="$2"
local line host name port approved connected_now last_seen last_connected
[[ -f "$STATE_PATH" ]] || return 1
while IFS= read -r line; do
[[ "$line" == peer=* ]] || continue
IFS=$'\t' read -r host name port approved connected_now last_seen last_connected <<<"${line#peer=}"
[[ "$host" == "$wanted_host" && "$port" == "$wanted_port" ]] || continue
if [[ -z "$last_connected" ]]; then
last_connected="${last_seen:-0}"
last_seen="${connected_now:-0}"
connected_now="false"
fi
printf '%s\t%s\t%s\t%s\t%s\n' "${name:-unknown}" "${approved:-false}" "${connected_now:-false}" "${last_seen:-0}" "${last_connected:-0}"
return 0
done <"$STATE_PATH"
return 1
}
resolve_config_relative_path() {
local path_value="$1"
if [[ "$path_value" = /* ]]; then
printf '%s\n' "$path_value"
return 0
fi
printf '%s\n' "$(dirname "$CONFIG_PATH")/$path_value"
}
store_secret_service_key() {
local secret_id="$1" secret_value="$2"
local output error_text
local -a command=("$APP_BIN" "$SECRET_STORE_SET_COMMAND" "$SECRET_STORE_ID_OPTION" "$secret_id")
require_client_binary || return 1
if [[ -n "$SECRET_STORE_STDIN_OPTION" ]]; then
command+=("$SECRET_STORE_STDIN_OPTION")
fi
if output="$(printf '%s' "$secret_value" | "${command[@]}" 2>&1)"; then
return 0
fi
printf -v error_text "Failed to store the Secret Service key for identifier '%s'.\n\n%s" "$secret_id" "$output"
zenity --error --width=700 --text="$error_text"
return 1
}
clear_secret_service_key() {
local secret_id="$1"
local output error_text
require_client_binary || return 1
if output="$("$APP_BIN" "$SECRET_STORE_CLEAR_COMMAND" "$SECRET_STORE_ID_OPTION" "$secret_id" 2>&1)"; then
return 0
fi
printf -v error_text "Saved settings, but failed to clear the previous Secret Service entry '%s'.\n\n%s" "$secret_id" "$output"
zenity --error --width=700 --text="$error_text"
return 1
}
read_key_file_value() {
local key_file="$1"
local resolved_path key_value
[[ -n "$key_file" ]] || return 1
resolved_path="$(resolve_config_relative_path "$key_file")"
if [[ ! -r "$resolved_path" ]]; then
zenity --error --width=700 --text="Cannot read the configured key file.\n\n$resolved_path"
return 1
fi
key_value="$(<"$resolved_path")"
if [[ -z "$key_value" ]]; then
zenity --error --width=700 --text="The configured key file is empty.\n\n$resolved_path"
return 1
fi
printf '%s\n' "$key_value"
}
read_secret_service_key() {
local secret_id="$1"
local secret_tool key_value
[[ -n "$secret_id" ]] || return 1
secret_tool="$(command -v secret-tool || true)"
if [[ -z "$secret_tool" ]]; then
zenity --error --width=700 --text="secret-tool is not available, so the stored Secret Service key cannot be revealed."
return 1
fi
if ! key_value="$("$secret_tool" lookup application mwb-client-linux secret-id "$secret_id" 2>/dev/null)" || [[ -z "$key_value" ]]; then
zenity --error --width=700 --text="Could not read the Secret Service entry '$secret_id'."
return 1
fi
printf '%s\n' "$key_value"
}
show_current_security_key() {
local key="$1" key_file="$2" secret_id="$3" current_auth_mode="$4"
local revealed_key="" detail_text=""
case "$current_auth_mode" in
"Inline security key")
if [[ -z "$key" ]]; then
zenity --error --text="No inline security key is currently configured."
return 1
fi
revealed_key="$key"
detail_text="Source: Inline security key"
;;
"Key file path")
revealed_key="$(read_key_file_value "$key_file")" || return 1
detail_text="Source: Key file path\nPath: $(resolve_config_relative_path "$key_file")"
;;
"Secret Service entry")
revealed_key="$(read_secret_service_key "$secret_id")" || return 1
detail_text="Source: Secret Service entry\nIdentifier: $secret_id"
;;
*)
zenity --error --text="No security key is currently configured."
return 1
;;
esac
if ! zenity --question --title="$APP_NAME authentication" --width=520 \
--text="Reveal the current security key?\n\nAnyone looking at this screen will be able to read it."; then
return 0
fi
zenity --entry --title="$APP_NAME current security key" --width=640 \
--text="$detail_text\n\nCopy the current security key below." \
--entry-text="$revealed_key" >/dev/null || true
}
choose_secret_cleanup_target() {
local secret_id="$1" next_mode="$2" next_secret_id="$3"
local choice
[[ -n "$secret_id" ]] || return 1
if [[ "$next_mode" == "Secret Service entry" && "$next_secret_id" == "$secret_id" ]]; then
return 1
fi
choice="$(zenity --list --radiolist --title="$APP_NAME secret cleanup" --width=560 --height=220 \
--text="Authentication will no longer use Secret Service identifier '$secret_id'. Choose what to do with the stored key." \
--column="Use" --column="Action" \
TRUE "Keep the stored Secret Service key" \
FALSE "Clear the stored Secret Service key" || true)"
if [[ "$choice" == "Clear the stored Secret Service key" ]]; then
printf '%s\n' "$secret_id"
return 0
fi
return 1
}
service_exists() {
systemctl --user cat "$SERVICE_NAME" >/dev/null 2>&1
}
service_active() {
systemctl --user is-active --quiet "$SERVICE_NAME"
}
offer_service_restart_if_active() {
local restart_reason="$1"
if ! service_active; then
return 0
fi
if zenity --question --title="$APP_NAME" --width=480 \
--text="$restart_reason\n\nRestart the background service now to apply the updated settings?"; then
systemctl --user restart "$SERVICE_NAME" >/dev/null
zenity --info --text="Restarted the $APP_NAME background service."
fi
}
install_service() {
require_client_binary || return 1
"$APP_BIN" install-user-service --config "$CONFIG_PATH" --force >/dev/null
systemctl --user daemon-reload
}
service_state() {
systemctl --user is-active "$SERVICE_NAME" 2>/dev/null || printf 'unknown'
}
service_state_label() {
case "$1" in
active) printf 'Running' ;;
activating) printf 'Starting' ;;
deactivating) printf 'Stopping' ;;
reloading) printf 'Restarting' ;;
failed) printf 'Needs attention' ;;
inactive) printf 'Stopped' ;;
*) printf 'Unavailable' ;;
esac
}
menu_summary_text() {
local state host auth_label auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms
state="$(service_state)"
host="$(read_config_value host)"
key="$(read_config_value key)"
key_file="$(read_config_value key_file)"
secret_id="$(read_secret_id_value)"
IFS=$'\t' read -r auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms < <(read_connection_behavior_values)
auth_label="$(configured_auth_label "$key" "$key_file" "$secret_id")"
[[ -n "$host" ]] || host="None"
printf 'Status: %s\nHost: %s\nAuth: %s\nReconnect: %s' \
"$(service_state_label "$state")" \
"$host" \
"$auth_label" \
"$( [[ "$auto_connect_enabled" == "true" ]] && printf 'Auto' || printf 'Manual' )"
}
show_status() {
local status_text doctor_text
status_text="$(systemctl --user status --no-pager "$SERVICE_NAME" 2>&1 || true)"
doctor_text="$("$APP_BIN" doctor --config "$CONFIG_PATH" --state "$STATE_PATH" 2>&1 || true)"
zenity --text-info --title="$APP_NAME service status" --width=900 --height=600 <<<"$doctor_text
----
$status_text"
}
append_check_line() {
local status="$1" name="$2" detail="$3"
printf '%-5s %-28s %s\n' "$status" "$name" "$detail"
}
probe_tcp_port() {
local host="$1" port="$2"
MWB_PROBE_HOST="$host" MWB_PROBE_PORT="$port" timeout 2 bash -c ':</dev/tcp/$MWB_PROBE_HOST/$MWB_PROBE_PORT' >/dev/null 2>&1
}
health_check() {
require_client_binary || return 1
local host port key key_file secret_id auth_count service_status health_text doctor_text
host="$(read_config_value host)"
port="$(read_config_value port)"; [[ -n "$port" ]] || port="15101"
key="$(read_config_value key)"
key_file="$(read_config_value key_file)"
secret_id="$(read_secret_id_value)"
auth_count="$(configured_auth_source_count "$key" "$key_file" "$secret_id")"
service_status="$(service_state)"
health_text="$(
append_check_line "$([[ -f "$CONFIG_PATH" ]] && printf OK || printf WARN)" "config file" "$CONFIG_PATH"
append_check_line "$([[ -e /dev/uinput ]] && printf OK || printf WARN)" "uinput device" "$([[ -e /dev/uinput ]] && ls -l /dev/uinput 2>/dev/null || printf 'missing; install udev rule and reload')"
append_check_line "$([[ "$service_status" == "active" ]] && printf OK || printf WARN)" "user service" "$(service_state_label "$service_status")"
append_check_line "$([[ -n "$host" ]] && printf OK || printf WARN)" "Windows host" "${host:-not configured}"
append_check_line "$([[ "$auth_count" == "1" ]] && printf OK || printf WARN)" "authentication" "$(configured_auth_label "$key" "$key_file" "$secret_id")"
if [[ -n "$host" ]] && command -v timeout >/dev/null 2>&1; then
if probe_tcp_port "$host" "$port"; then
append_check_line OK "input port" "$host:$port reachable"
else
append_check_line WARN "input port" "$host:$port not reachable"
fi
if probe_tcp_port "$host" "15100"; then
append_check_line OK "clipboard port" "$host:15100 reachable"
else
append_check_line WARN "clipboard port" "$host:15100 not reachable"
fi
else
append_check_line WARN "port probe" "host or timeout command unavailable"
fi
)"
doctor_text="$("$APP_BIN" doctor --config "$CONFIG_PATH" --state "$STATE_PATH" 2>&1 || true)"
zenity --text-info --title="$APP_NAME health check" --width=900 --height=620 <<<"$health_text
----
Client doctor
----
$doctor_text"
}
connection_quality() {
local host port state auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms
local clipboard_enabled clipboard_send_enabled clipboard_force_poll clipboard_poll_ms latency_report quality_text peer_lines
host="$(read_config_value host)"
port="$(read_config_value port)"; [[ -n "$port" ]] || port="15101"
state="$(service_state)"
IFS=$'\t' read -r auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms < <(read_connection_behavior_values)
clipboard_enabled="$(read_config_value clipboard_enabled)"; [[ -n "$clipboard_enabled" ]] || clipboard_enabled="true"
clipboard_send_enabled="$(read_config_value clipboard_send_enabled)"; [[ -n "$clipboard_send_enabled" ]] || clipboard_send_enabled="true"
clipboard_force_poll="$(read_config_value clipboard_force_poll)"; [[ -n "$clipboard_force_poll" ]] || clipboard_force_poll="false"
clipboard_poll_ms="$(read_config_value clipboard_poll_ms)"; [[ -n "$clipboard_poll_ms" ]] || clipboard_poll_ms="1000"
latency_report="$(read_config_value "$LATENCY_REPORT_CONFIG_KEY")"; [[ -n "$latency_report" ]] || latency_report="false"
peer_lines="No peer state has been recorded yet."
if [[ -f "$STATE_PATH" ]]; then
peer_lines="$(awk -F'\t' '
/^peer=/ {
sub(/^peer=/, "", $1)
name=$2; approved=$3; connected=$4; last_seen=$5; last_connected=$6
if (name == "") name="unknown"
printf "- %s (%s): paired=%s connected=%s last_seen=%s last_connected=%s\n", name, $1, approved, connected, last_seen, last_connected
}
' "$STATE_PATH")"
[[ -n "$peer_lines" ]] || peer_lines="No peer entries found in $STATE_PATH."
fi
quality_text="Service: $(service_state_label "$state") ($state)
Configured host: ${host:-not configured}
Input port: $port
Clipboard port: 15100
Reconnect mode: $( [[ "$auto_connect_enabled" == "true" ]] && printf 'Auto' || printf 'Manual' )
Reconnect timing: initial=${reconnect_initial_backoff_ms}ms max=${reconnect_max_backoff_ms}ms idle=${reconnect_idle_retry_ms}ms
Clipboard: enabled=$clipboard_enabled send_local=$clipboard_send_enabled force_poll=$clipboard_force_poll poll=${clipboard_poll_ms}ms
Latency report logging: $latency_report
Known peers:
$peer_lines"
zenity --text-info --title="$APP_NAME connection quality" --width=840 --height=520 <<<"$quality_text"
}
diagnostics_bundle() {
if [[ ! -x "$DIAGNOSTICS_BUNDLE_SCRIPT" ]]; then
zenity --error --text="Diagnostics bundle script is not available:
$DIAGNOSTICS_BUNDLE_SCRIPT"
return 1
fi
local output_dir result
output_dir="$(zenity --file-selection --directory --title="$APP_NAME diagnostics output folder" || true)"
[[ -n "$output_dir" ]] || return 1
result="$("$DIAGNOSTICS_BUNDLE_SCRIPT" --config "$CONFIG_PATH" --state "$STATE_PATH" --output "$output_dir" 2>&1 || true)"
zenity --text-info --title="$APP_NAME diagnostics bundle" --width=760 --height=420 <<<"$result"
}
show_peers() {
local rows=() configured_host selected_peer selected_host selected_port selected_name selected_action
configured_host="$(read_config_value host)"
if [[ -f "$STATE_PATH" ]]; then
local paired_label connected_label configured_label last_seen_label last_connected_label service_running connected_now
if service_active; then
service_running="true"
else
service_running="false"
fi
while IFS= read -r line; do
[[ "$line" == peer=* ]] || continue
IFS=$'\t' read -r host name port approved connected_now last_seen last_connected <<<"${line#peer=}"
if [[ -z "$last_connected" ]]; then
last_connected="${last_seen:-0}"
last_seen="${connected_now:-0}"
connected_now="false"
fi
paired_label="$(format_paired_label "$approved")"
connected_label="$(format_yes_no "$([[ "$service_running" == "true" && "$connected_now" == "true" ]] && printf 'true' || printf 'false')")"
configured_label="$(format_yes_no "$([[ "$host" == "$configured_host" ]] && printf 'true' || printf 'false')")"
last_seen_label="$(format_epoch_label "$last_seen")"
last_connected_label="$(format_epoch_label "$last_connected")"
rows+=("${host}|${port}|${name:-unknown}" "$host" "${name:-unknown}" "$port" "$paired_label" "$connected_label" "$configured_label" "$last_seen_label" "$last_connected_label")
done <"$STATE_PATH"
fi
if [[ "${#rows[@]}" -eq 0 ]]; then
zenity --info --text="No peer state has been recorded yet."
return 0
fi
selected_peer="$(zenity --list --title="Known peers" --width=980 --height=420 \
--text="Select a peer to manage it." \
--print-column=1 --hide-column=1 \
--column="Key" --column="Host" --column="Name" --column="Port" --column="Paired" --column="Connected now" --column="Configured host" --column="Last seen" --column="Last connected" \
"${rows[@]}" || true)"
[[ -n "$selected_peer" ]] || return 0
IFS='|' read -r selected_host selected_port selected_name <<<"$selected_peer"
selected_action="$(zenity --list --radiolist --title="$APP_NAME peer actions" --width=520 --height=220 \
--text="Peer: ${selected_name:-unknown} ($selected_host:$selected_port)" \
--column="Use" --column="Action" \
TRUE "Use as configured Windows host" \
FALSE "Edit settings for this peer" \
FALSE "Forget this peer" || true)"
[[ -n "$selected_action" ]] || return 0
case "$selected_action" in
"Use as configured Windows host")
set_configured_host "$selected_host"
;;
"Edit settings for this peer")
edit_settings "$selected_host"
;;
"Forget this peer")
if zenity --question --title="$APP_NAME peer actions" --width=480 \
--text="Forget peer ${selected_name:-unknown} at $selected_host:$selected_port?\n\nThis removes it from saved peer state only."; then
remove_peer_state "$selected_host" "$selected_port"
zenity --info --text="Removed $selected_host:$selected_port from known peers."
fi
;;
esac
}
discover_peers() {
require_client_binary || return 1
local output port selected_ip configured_host service_running
port="$(read_config_value port)"
configured_host="$(read_config_value host)"
[[ -n "$port" ]] || port="15101"
if service_active; then
service_running="true"
else
service_running="false"
fi
output="$("$APP_BIN" discover --state "$STATE_PATH" --port "$port" --timeout-ms 200 --max-hosts 256 2>&1 || true)"
mapfile -t candidates < <(printf '%s\n' "$output" | awk '
/^ / {
ip = $1
name = "(unknown)"
network = "(default)"
for (i = 2; i <= NF; i++) {
if ($i ~ /^name=/) {
name = substr($i, 6)
} else if ($i ~ /^iface=/) {
network = substr($i, 7)
}
}
print ip "|" name "|" network
}
')
if [[ "${#candidates[@]}" -eq 0 ]]; then
zenity --info --title="$APP_NAME discovery" --text="$output"
return 1
fi
local rows=()
local ip item name network paired_label connected_label configured_label last_connected_label state_name state_approved state_connected state_last_seen state_last_connected
for item in "${candidates[@]}"; do
IFS='|' read -r ip name network <<< "$item"
state_name=""
state_approved="false"
state_connected="false"
state_last_seen="0"
state_last_connected="0"
if IFS=$'\t' read -r state_name state_approved state_connected state_last_seen state_last_connected < <(read_peer_state "$ip" "$port" || true); then
:
fi
paired_label="$(format_paired_label "$state_approved")"
connected_label="$(format_yes_no "$([[ "$service_running" == "true" && "$state_connected" == "true" ]] && printf 'true' || printf 'false')")"
configured_label="$(format_yes_no "$([[ "$ip" == "$configured_host" ]] && printf 'true' || printf 'false')")"
last_connected_label="$(format_epoch_label "$state_last_connected")"
rows+=("$ip" "$name" "$network" "$paired_label" "$connected_label" "$configured_label" "$last_connected_label")
done
selected_ip="$(zenity --list --title="Discovered peers" --width=900 --height=320 \
--text="Choose a Windows peer to make it the configured host for InputFlow." \
--print-column=1 --column="IP" --column="PC name" --column="Network" --column="Paired" --column="Connected now" --column="Configured host" --column="Last connected" "${rows[@]}" || true)"
[[ -n "$selected_ip" ]] || return 1
printf '%s\n' "$selected_ip"
}
discover_and_save_peer() {
local selected host key key_file secret_id auth_count action
selected="$(discover_peers || true)"
if [[ -z "$selected" ]]; then
return 1
fi
# Always prompt for settings to ensure key is entered/verified
edit_settings "$selected" || return 1
if ! service_active; then
if zenity --question --title="$APP_NAME" --width=480 \
--text="Peer setup is saved.\n\nStart the $APP_NAME background service now?"; then
start_session
fi
fi
}
export_windows_helper() {
require_client_binary || return 1
local output_dir position result
output_dir="$(zenity --file-selection --directory --title="$APP_NAME Windows helper output folder" || true)"
[[ -n "$output_dir" ]] || return 1
position="$(zenity --list --radiolist --title="$APP_NAME Windows helper" --width=560 --height=260 \
--text="Choose where the Linux desktop sits relative to the Windows host." \
--column="Use" --column="Position" \
TRUE "auto" \
FALSE "top-left" \
FALSE "top-right" \
FALSE "bottom-left" \
FALSE "bottom-right" || true)"
[[ -n "$position" ]] || return 1
result="$("$APP_BIN" export-windows-pair --config "$CONFIG_PATH" --output "$output_dir" --position "$position" --force 2>&1 || true)"
zenity --text-info --title="$APP_NAME Windows pairing helper" --width=820 --height=440 <<<"$result
Next steps:
1. Copy the exported .ps1 file to the Windows machine.
2. Start PowerToys once so Mouse Without Borders creates its settings file.
3. Run the helper in PowerShell.
4. Return here and run Health Check."
}
guided_pairing() {
while true; do
local choice
choice="$(zenity --list --title="$APP_NAME guided pairing" --width=620 --height=360 \
--text="Use this flow to discover Windows, save Linux settings, export the Windows helper, then verify the setup." \
--column="Step" \
"1. Discover Windows peer and save settings" \
"2. Edit settings manually" \
"3. Export Windows helper" \
"4. Start service" \
"5. Run health check" \
"Back" || true)"
case "$choice" in
"1. Discover Windows peer and save settings") discover_and_save_peer ;;
"2. Edit settings manually") edit_settings ;;
"3. Export Windows helper") export_windows_helper ;;
"4. Start service") start_session ;;
"5. Run health check") health_check ;;
""|"Back") return 0 ;;
esac
done
}
edit_settings() {
local preset_host="${1:-}"
local host key key_file secret_id secret_key_name machine_name port screen_width screen_height auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms clipboard_enabled clipboard_force_poll clipboard_poll_ms
local clipboard_send_enabled current_auth_mode auth_action key_mode cleanup_secret_id saved_message
local mpris_media_keys_enabled mpris_player latency_report gui_output
host="$(read_config_value host)"
key="$(read_config_value key)"
key_file="$(read_config_value key_file)"
secret_id="$(read_secret_id_value)"
secret_key_name="$(detect_secret_id_key_name)"
machine_name="$(read_config_value machine_name)"
port="$(read_config_value port)"; [[ -n "$port" ]] || port="15101"
screen_width="$(read_config_value screen_width)"
screen_height="$(read_config_value screen_height)"
IFS=$'\t' read -r auto_connect_enabled reconnect_initial_backoff_ms reconnect_max_backoff_ms reconnect_idle_retry_ms < <(read_connection_behavior_values)
clipboard_enabled="$(read_config_value clipboard_enabled)"; [[ -n "$clipboard_enabled" ]] || clipboard_enabled="true"