This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.sh
92 lines (74 loc) · 3.02 KB
/
fix.sh
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
#!/bin/sh
# Exit on any error
set -e
echo "Installing required packages..." >&2
# Install CLI utils to work with clipboard: "wl-clipboard" for Wayland and "xclip" for X11
if pacman --help > /dev/null 2>&1; then
# For Arch-based
sudo pacman -Sy --noconfirm --needed wl-clipboard xclip || exit "$?"
elif apt-get --help > /dev/null 2>&1; then
# For Debian-based
sudo apt-get update && sudo apt-get install -y wl-clipboard xclip || exit "$?"
else
echo "No pacman or apt-get found!" >&2
exit 1
fi
echo "Required packages installed!" >&2
# ========================================
# Create script for the service.
# The purpose here is to sync clipboard content when working in VirtualBox.
# Wayland clipboard is not syncing, but X11 clipboard does.
# So the script here watches Wayland clipboard content, and when it changes, it copies it to the X11 clipboard content.
# ========================================
echo "Creating script..." >&2
# shellcheck disable=SC2016
echo '#!/bin/sh
# Set initial clipboard value
previous_clipboard_xclip="$(xclip -selection clipboard -o)"
previous_clipboard_wayland="$(wl-paste)"
while true; do
# Get current clipboard value
current_clipboard_xclip="$(xclip -selection clipboard -o)"
# If X11 clipboard value has changed, copy it to Wayland clipboard
if [ "${current_clipboard_xclip}" != "${previous_clipboard_xclip}" ]; then
echo -n "${current_clipboard_xclip}" | wl-copy
previous_clipboard_xclip="${current_clipboard_xclip}"
previous_clipboard_wayland="${previous_clipboard_xclip}"
fi
current_clipboard_wayland="$(wl-paste)"
# If Wayland clipboard value has changed, copy it to X11 clipboard
if [ "${current_clipboard_wayland}" != "${previous_clipboard_wayland}" ]; then
echo -n "${current_clipboard_wayland}" | xclip -selection clipboard
previous_clipboard_wayland="${current_clipboard_wayland}"
previous_clipboard_xclip="${previous_clipboard_wayland}"
fi
# Wait before checking again
sleep 0.2
done' | sudo tee /usr/local/bin/clipboard-watcher.sh > /dev/null
# Make the script executable
sudo chmod +x /usr/local/bin/clipboard-watcher.sh
echo "Script created!" >&2
# ========================================
# ========================================
# Create service file
# ========================================
echo "Creating user service..." >&2
mkdir --parents "${HOME}/.config/systemd/user"
echo '[Unit]
Description=Clipboard Watcher Service
After=graphical-session.target
[Service]
Type=simple
ExecStart=/usr/local/bin/clipboard-watcher.sh
Restart=on-failure
[Install]
WantedBy=default.target' > "${HOME}/.config/systemd/user/clipboard-watcher.service"
echo "User service created!" >&2
# ========================================
echo "Enabling and starting service..." >&2
# Reload systemd manager configuration
systemctl --user daemon-reload
# Enable and start the service
systemctl --user enable --now clipboard-watcher.service
echo "Service enabled and started!" >&2
echo "Fix successfully installed!" >&2