-
Notifications
You must be signed in to change notification settings - Fork 26
/
clipboard-adapter.sh
executable file
·170 lines (152 loc) · 3.33 KB
/
clipboard-adapter.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
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
#!/bin/sh
usage() {
cat <<EOF
Clipboard adapter for rofi-emoji.
Usage:
echo "the text" | clipboard-adapter.sh copy
Set clipboard to "the text"
echo "the text" | clipboard-adapter.sh insert
Try to write "the text" to the focused window. Will also copy the text in
case insertion fails.
Detects Wayland and X and finds the appropriate tool for the current
environment.
When rofi is run from a terminal, the output both on stderr and stdout should
be visible to the user and makes it possible to do some debugging.
If stderr is bound to /dev/null, the script will try to send any error messages
to a notification service.
EOF
}
command=
main() {
while [ "$#" -gt 0 ]; do
case "$1" in
--help)
shift
command=help
break
;;
copy | insert | insert_no_copy | help)
command="$1"
shift
;;
--)
shift
break
;;
*)
show_error "Unknown option: $1"
exit 1
;;
esac
done
case "$command" in
help)
usage
exit 0
;;
copy)
perform_copy
;;
insert)
# Also copy when doing insert to give users a fallback in case automatic
# insertion didn't work. Inserting unicode characters in X windows is a
# very erratic process that works only in some UI toolkits.
#
# Since stdin should be sent to two different processes, read it first and
# replicate manually.
input="$(cat -)"
printf "%s" "$input" | perform_copy
printf "%s" "$input" | perform_insert
;;
insert_no_copy)
# Same as 'insert' but without the copying fallback.
perform_insert
;;
*)
usage >&2
exit 1
;;
esac
}
stderr_is_null() {
test /proc/self/fd/2 -ef /dev/null
}
show_error() {
if stderr_is_null && hash notify-send 2>/dev/null; then
notify-send rofi-emoji "$@"
else
echo "$@" >&2
fi
}
perform_copy() {
tool=$(find_copy_tool)
case "$tool" in
xsel)
xsel --clipboard --input
;;
xclip)
xclip -selection clipboard -in
;;
copyq)
copyq copy -
;;
wl-copy)
wl-copy
;;
"")
show_error "Could not find any tool to handle copying. Please install a clipboard handler."
exit 1
;;
*)
show_error "$tool has no implementation for copying yet"
exit 2
;;
esac
}
perform_insert() {
tool=$(find_insert_tool)
case "$tool" in
xdotool)
xdotool type --clearmodifiers --file -
;;
wtype)
wtype -
;;
"")
show_error "Could not find any tool to handle insertion. Please install xdotool or wtype."
exit 1
;;
*)
show_error "$tool has no implementation for insertion yet"
exit 2
;;
esac
}
# Print out the first argument and return true if that argument is an installed
# command. Prints nothing and returns false if the argument is not an installed
# command.
try_tool() {
if hash "$1" 2>/dev/null; then
echo "$1"
return 0
else
return 1
fi
}
# Find the best clipboard tool to use.
find_copy_tool() {
if [ "$XDG_SESSION_TYPE" = wayland ] || [ -n "$WAYLAND_DISPLAY" ]; then
try_tool wl-copy || return 1
else
try_tool xsel || try_tool xclip || try_tool copyq || return 1
fi
}
# Find the best insertion tool to use.
find_insert_tool() {
if [ "$XDG_SESSION_TYPE" = wayland ] || [ -n "$WAYLAND_DISPLAY" ]; then
try_tool wtype || return 1
else
try_tool xdotool || return 1
fi
}
main "$@"