-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
install_calc_toggle.sh
154 lines (126 loc) · 8.38 KB
/
install_calc_toggle.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
#!/usr/bin/env bash
source non_sudo_check.sh
# ENV VARS
if [ -z "$INSTALL_DIR_PATH" ]; then
INSTALL_DIR_PATH="/usr/share/asus-numberpad-driver"
fi
echo "Calculator app"
echo
if [[ $(type gsettings 2>/dev/null) ]]; then
read -r -p "Do you want try to install toggling script for XF86Calculator key? [y/N]" response
case "$response" in [yY][eE][sS]|[yY])
echo
CALC_TOGGLE=1
EXISTING_SHORTCUT_STRING=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
NEW_SHORTCUT_INDEX=0
filtered_existing_shortcut_string="["
if [[ "$EXISTING_SHORTCUT_STRING" != "@as []" ]]; then
IFS=', ' read -ra existing_shortcut_array <<< "$EXISTING_SHORTCUT_STRING"
for shortcut_index in "${!existing_shortcut_array[@]}"; do
shortcut="${existing_shortcut_array[$shortcut_index]}"
shortcut_index=$( echo $shortcut | cut -d/ -f 8 | sed 's/[^0-9]//g')
# looking for first free highest index (gaps will not be used for sure)
if [[ "$shortcut_index" -gt "$NEW_SHORTCUT_INDEX" ]]; then
NEW_SHORTCUT_INDEX=$shortcut_index
fi
# filter out already added the same shortcuts by this driver (can be caused by running install script multiple times so clean and then add only 1 new - we want no duplicates)
command=$(gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$shortcut_index/ 'command')
if [[ "$command" != "'bash $INSTALL_DIR_PATH/scripts/calculator_toggle.sh'" ]]; then
if [[ "$filtered_existing_shortcut_string" != "[" ]]; then
filtered_existing_shortcut_string="$filtered_existing_shortcut_string"", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$shortcut_index/'"
else
filtered_existing_shortcut_string="$filtered_existing_shortcut_string""'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$shortcut_index/'"
fi
else
echo "Found already existing duplicated shortcut for toggling calculator, will be removed"
gsettings reset-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$shortcut_index/
fi
done
((NEW_SHORTCUT_INDEX=NEW_SHORTCUT_INDEX+1))
filtered_existing_shortcut_string="$filtered_existing_shortcut_string"']'
if [[ "$filtered_existing_shortcut_string" != "[" ]]; then
new_shortcut_string=${filtered_existing_shortcut_string::-2}"', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/']"
else
# after filtering duplicated shortcuts array of shortcuts is completely empty
new_shortcut_string=" ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
fi
else
# array of shortcuts is completely empty
new_shortcut_string=" ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
fi
IS_INSTALLED_ELEMENTARY_OS_CALCULATOR=$((type io.elementary.calculator || flatpak list | grep io.elementary.calculator) &>/dev/null ; echo $?)
if [ $IS_INSTALLED_ELEMENTARY_OS_CALCULATOR == "0" ]; then
CALC_TOGGLE_SUPPORTED_CALC=1
echo "Detected io.elementary.calculator"
fi
IS_INSTALLED_GNOME_OS_CALCULATOR=$((type gnome-calculator || flatpak list | grep org.gnome.Calculator) &>/dev/null ; echo $?)
if [ $IS_INSTALLED_GNOME_OS_CALCULATOR == "0" ]; then
CALC_TOGGLE_SUPPORTED_CALC=1
echo "Detected gnome-calculator"
fi
if [ $IS_INSTALLED_GNOME_OS_CALCULATOR == "1" ] && [ $IS_INSTALLED_GNOME_OS_CALCULATOR == "1" ]; then
echo "Automatic installing of toggling script for XF86Calculator key failed. Was not detected any supported calculator (gnome-calculator && io.elementary.calculator). You must associate the toggling script with the key EV_KEY.KEY_CALC by your own for this feature."
echo
echo "Example of the script for ElementaryOS calculator:"
echo
echo "\`\`\`"
cat scripts/io_elementary_calculator_toggle.sh
echo
echo "\`\`\`"
echo
echo "or Gnome calculator:"
echo
echo "\`\`\`"
cat scripts/gnome_calculator_toggle.sh
echo
echo "\`\`\`"
elif [[ $IS_INSTALLED_ELEMENTARY_OS_CALCULATOR -eq 0 ]]; then
echo "Setting up for io.elementary.calculator"
sudo mkdir -p $INSTALL_DIR_PATH/scripts
sudo cp scripts/io_elementary_calculator_toggle.sh $INSTALL_DIR_PATH/scripts/calculator_toggle.sh
sudo chmod +x $INSTALL_DIR_PATH/scripts/calculator_toggle.sh
# this has to be empty (no doubled XF86Calculator)
gsettings set org.gnome.settings-daemon.plugins.media-keys calculator [\'\']
gsettings set org.gnome.settings-daemon.plugins.media-keys calculator-static [\'\']
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "${new_shortcut_string}"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "name" "Calculator"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "command" "bash $INSTALL_DIR_PATH/scripts/calculator_toggle.sh"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "binding" "XF86Calculator"
EXISTING_SHORTCUT_STRING=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
echo "Toggling script for calculator app io.elementary.calculator has been installed."
elif [[ $IS_INSTALLED_GNOME_OS_CALCULATOR -eq 0 ]]; then
echo "Setting up for gnome-calculator"
sudo mkdir -p $INSTALL_DIR_PATH/scripts
sudo cp scripts/gnome_calculator_toggle.sh $INSTALL_DIR_PATH/scripts/calculator_toggle.sh
sudo chmod +x $INSTALL_DIR_PATH/scripts/calculator_toggle.sh
# this has to be empty (no doubled XF86Calculator)
gsettings set org.gnome.settings-daemon.plugins.media-keys calculator [\'\']
gsettings set org.gnome.settings-daemon.plugins.media-keys calculator-static [\'\']
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "${new_shortcut_string}"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "name" "Calculator"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "command" "bash $INSTALL_DIR_PATH/scripts/calculator_toggle.sh"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$NEW_SHORTCUT_INDEX/ "binding" "XF86Calculator"
EXISTING_SHORTCUT_STRING=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
echo "Toggling script for calculator app gnome-calculator has been installed."
fi
;;
*)
;;
esac
else
echo "Automatic installing of toggling script for XF86Calculator key failed. Gsettings was not found. You must associate the toggling script with the key EV_KEY.KEY_CALC by your own for this feature."
echo
echo "Example of the script for ElementaryOS calculator:"
echo
echo "\`\`\`"
cat scripts/io_elementary_calculator_toggle.sh
echo
echo "\`\`\`"
echo
echo "or Gnome calculator:"
echo
echo "\`\`\`"
cat scripts/gnome_calculator_toggle.sh
echo
echo "\`\`\`"
fi