-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall-linux.sh
More file actions
executable file
·336 lines (283 loc) · 11.7 KB
/
Copy pathinstall-linux.sh
File metadata and controls
executable file
·336 lines (283 loc) · 11.7 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
#!/usr/bin/env bash
# install-linux.sh — Install screen-ruler binary, desktop entry, and keyboard shortcut.
#
# Usage:
# ./install-linux.sh [-d INSTALL_DIR]
#
# The script expects a built binary at dist/screen-ruler (run pyinstaller first).
# It copies the binary into INSTALL_DIR (default: current directory), installs a
# .desktop file, and registers a global Super+Shift+R shortcut for the detected
# desktop environment (GNOME, KDE Plasma, Hyprland, Sway).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_DIR="$PWD"
APP_NAME="Screen Ruler"
APP_ID="screen-ruler"
SHORTCUT_DISPLAY="Super+Shift+R"
usage() {
echo "Usage: $0 [-d INSTALL_DIR]"
echo
echo " -d DIR Install the binary into DIR (default: current directory)"
echo " -h Show this help"
exit 0
}
while getopts "d:h" opt; do
case "$opt" in
d) INSTALL_DIR="$OPTARG" ;;
h) usage ;;
*) usage ;;
esac
done
# --- Locate the built binary ------------------------------------------------
BINARY="$SCRIPT_DIR/dist/screen-ruler"
if [[ ! -f "$BINARY" ]]; then
echo "Error: $BINARY not found."
echo "Build it first: pyinstaller screen_ruler.spec"
exit 1
fi
# --- Copy binary to install dir ---------------------------------------------
mkdir -p "$INSTALL_DIR"
INSTALL_DIR="$(cd "$INSTALL_DIR" && pwd)" # resolve to absolute path
EXEC_PATH="$INSTALL_DIR/$APP_ID"
# Portable realpath fallback for systems without coreutils realpath.
resolve_path() { cd "$(dirname "$1")" && echo "$PWD/$(basename "$1")"; }
if [[ "$EXEC_PATH" != "$(resolve_path "$BINARY")" ]]; then
cp "$BINARY" "$EXEC_PATH"
echo "Installed binary → $EXEC_PATH"
else
echo "Binary already at $EXEC_PATH"
fi
chmod +x "$EXEC_PATH"
# --- Detect desktop environment ---------------------------------------------
detect_de() {
local de="${XDG_CURRENT_DESKTOP:-}"
de="${de,,}" # lowercase
case "$de" in
*gnome*) echo "gnome" ;;
*kde*) echo "kde" ;;
*hyprland*) echo "hyprland" ;;
*sway*) echo "sway" ;;
*)
# Fallback: check running processes.
if pgrep -x gnome-shell &>/dev/null; then echo "gnome"
elif pgrep -x plasmashell &>/dev/null; then echo "kde"
elif pgrep -x Hyprland &>/dev/null; then echo "hyprland"
elif pgrep -x sway &>/dev/null; then echo "sway"
else echo "unknown"
fi
;;
esac
}
DE="$(detect_de)"
echo "Detected desktop environment: $DE"
# --- Install .desktop file --------------------------------------------------
DESKTOP_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
mkdir -p "$DESKTOP_DIR"
DESKTOP_FILE="$DESKTOP_DIR/$APP_ID.desktop"
# Escape the path per the Desktop Entry spec: backslashes first, then spaces.
DESKTOP_EXEC="${EXEC_PATH//\\/\\\\}"
DESKTOP_EXEC="${DESKTOP_EXEC// /\\ }"
cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Type=Application
Name=$APP_NAME
Comment=Measure on-screen UI elements using edge detection
Exec=$DESKTOP_EXEC
Icon=utilities-terminal
Terminal=false
Categories=Utility;
Keywords=ruler;measure;pixel;screen;
EOF
echo "Installed desktop entry → $DESKTOP_FILE"
# Update the MIME-type database (not required for launcher visibility, but
# harmless and expected by some desktop environments).
if command -v update-desktop-database &>/dev/null; then
update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
fi
# --- Shortcut helpers per DE ------------------------------------------------
install_gnome_shortcut() {
local binding="<Super><Shift>r"
local schema="org.gnome.settings-daemon.plugins.media-keys"
local path_prefix="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings"
if ! command -v gsettings &>/dev/null \
|| ! gsettings list-schemas 2>/dev/null | grep -q "^${schema}$"; then
echo "gsettings or GNOME media-keys schema not available; skipping shortcut."
return 1
fi
# Read the current list of custom-keybinding slots.
local current
current="$(gsettings get "$schema" custom-keybindings)" || return 1
# Check if screen-ruler is already registered in any slot.
local slots
slots="$(echo "$current" | tr -d "[]' " | tr ',' '\n')"
for slot in $slots; do
if [[ -z "$slot" ]]; then continue; fi
local cmd
cmd="$(gsettings get "$schema.custom-keybinding:$slot" command 2>/dev/null || true)"
if [[ "$cmd" == *"$APP_ID"* ]]; then
gsettings set "$schema.custom-keybinding:$slot" name "'$APP_NAME'" || return 1
gsettings set "$schema.custom-keybinding:$slot" command "'\"$EXEC_PATH\"'" || return 1
gsettings set "$schema.custom-keybinding:$slot" binding "'$binding'" || return 1
echo "Updated existing GNOME shortcut (${slot}) → $SHORTCUT_DISPLAY"
return
fi
done
# Find the next unused slot index.
# Use a leading slash in the pattern so "custom1/" never matches "custom10/".
local idx=0
while echo "$current" | grep -qF "/custom${idx}/"; do
idx=$((idx + 1))
done
local new_slot="$path_prefix/custom${idx}/"
if [[ "$current" == "@as []" ]]; then
gsettings set "$schema" custom-keybindings "['$new_slot']" || return 1
else
local updated
updated="$(echo "$current" | sed "s|]$|, '$new_slot']|")"
gsettings set "$schema" custom-keybindings "$updated" || return 1
fi
gsettings set "$schema.custom-keybinding:$new_slot" name "'$APP_NAME'" || return 1
gsettings set "$schema.custom-keybinding:$new_slot" command "'\"$EXEC_PATH\"'" || return 1
gsettings set "$schema.custom-keybinding:$new_slot" binding "'$binding'" || return 1
echo "Registered GNOME shortcut ($new_slot) → $SHORTCUT_DISPLAY"
}
install_kde_shortcut() {
local kwrite=""
if command -v kwriteconfig6 &>/dev/null; then kwrite="kwriteconfig6"
elif command -v kwriteconfig5 &>/dev/null; then kwrite="kwriteconfig5"
fi
if [[ -z "$kwrite" ]]; then
echo "kwriteconfig not found; cannot register KDE shortcut automatically."
return 1
fi
local config_file="kglobalshortcutsrc"
local group="$APP_ID.desktop"
local binding="Meta+Shift+R"
# Write the shortcut entry.
"$kwrite" --file "$config_file" --group "$group" \
--key "_k_friendly_name" "$APP_NAME" || return 1
"$kwrite" --file "$config_file" --group "$group" \
--key "_launch" "${binding},none,Launch ${APP_NAME}" || return 1
# Also add an Actions line to the .desktop file so KDE can map it.
if ! grep -q "^Actions=" "$DESKTOP_FILE"; then
printf '\nActions=_launch;\n\n[Desktop Action _launch]\nName=Launch %s\nExec=%s\n' \
"$APP_NAME" "$DESKTOP_EXEC" >> "$DESKTOP_FILE" || return 1
fi
# Rebuild KDE's system config cache.
if command -v kbuildsycoca6 &>/dev/null; then kbuildsycoca6 2>/dev/null || true
elif command -v kbuildsycoca5 &>/dev/null; then kbuildsycoca5 2>/dev/null || true
fi
# Pick the correct global-accel restart commands for the detected Plasma version.
local restart_hint
if [[ "$kwrite" == "kwriteconfig6" ]]; then
restart_hint="kquitapp6 kglobalaccel && kstart6 kglobalaccel"
else
restart_hint="kquitapp5 kglobalaccel && kstart5 kglobalaccel"
fi
echo "Registered KDE Plasma shortcut → Meta+Shift+R"
echo "You may need to log out and back in (or run: $restart_hint) for the shortcut to activate."
}
install_hyprland_shortcut() {
local config="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/hyprland.conf"
local begin_marker="# BEGIN Screen Ruler"
local end_marker="# END Screen Ruler"
if [[ ! -f "$config" ]]; then
echo "Hyprland config not found at $config"
return 1
fi
# Already has a marked block — check if it's already up-to-date, else update it.
if grep -qF "$begin_marker" "$config"; then
if ! grep -qF "$end_marker" "$config"; then
echo "Hyprland: malformed Screen Ruler block in $config (missing END marker); skipping."
return 1
fi
local block
block="$(awk -v b="$begin_marker" -v e="$end_marker" \
'$0==b{f=1;next} $0==e{f=0;next} f{print}' "$config")"
if echo "$block" | grep -qF "$EXEC_PATH"; then
echo "Hyprland: shortcut already up to date in $config"
return
fi
# Block exists but points to a different path — update it below.
elif grep -qE "bind.*exec.*screen-ruler" "$config"; then
echo "Hyprland: screen-ruler binding already present in $config"
return
fi
local backup="${config}.bak.$(date +%Y%m%d%H%M%S)"
cp "$config" "$backup"
# Remove any existing marked block, then append a fresh one.
local tmp
tmp="$(mktemp)"
awk -v begin="$begin_marker" -v end="$end_marker" '
$0 == begin { in_block=1; next }
$0 == end { in_block=0; next }
!in_block { print }
' "$config" > "$tmp"
printf '\n%s\nbind = $mainMod SHIFT, R, exec, "%s"\n%s\n' \
"$begin_marker" "$EXEC_PATH" "$end_marker" >> "$tmp"
# Write back to the original path so symlinks and file permissions are preserved.
cat "$tmp" > "$config" && rm -f "$tmp"
echo "Added Hyprland binding to $config → $SHORTCUT_DISPLAY"
echo "Backup saved at $backup"
echo "Reload your config (hyprctl reload) to activate."
}
install_sway_shortcut() {
local config="${XDG_CONFIG_HOME:-$HOME/.config}/sway/config"
local begin_marker="# BEGIN Screen Ruler"
local end_marker="# END Screen Ruler"
if [[ ! -f "$config" ]]; then
echo "Sway config not found at $config"
return 1
fi
if grep -qF "$begin_marker" "$config"; then
if ! grep -qF "$end_marker" "$config"; then
echo "Sway: malformed Screen Ruler block in $config (missing END marker); skipping."
return 1
fi
local block
block="$(awk -v b="$begin_marker" -v e="$end_marker" \
'$0==b{f=1;next} $0==e{f=0;next} f{print}' "$config")"
if echo "$block" | grep -qF "$EXEC_PATH"; then
echo "Sway: shortcut already up to date in $config"
return
fi
elif grep -qE "bindsym.*exec.*screen-ruler" "$config"; then
echo "Sway: screen-ruler binding already present in $config"
return
fi
local backup="${config}.bak.$(date +%Y%m%d%H%M%S)"
cp "$config" "$backup"
local tmp
tmp="$(mktemp)"
awk -v begin="$begin_marker" -v end="$end_marker" '
$0 == begin { in_block=1; next }
$0 == end { in_block=0; next }
!in_block { print }
' "$config" > "$tmp"
# shellcheck disable=SC2016
printf '\n%s\nbindsym $mod+Shift+r exec "%s"\n%s\n' \
"$begin_marker" "$EXEC_PATH" "$end_marker" >> "$tmp"
# Write back to the original path so symlinks and file permissions are preserved.
cat "$tmp" > "$config" && rm -f "$tmp"
echo "Added Sway binding to $config → $SHORTCUT_DISPLAY"
echo "Backup saved at $backup"
echo "Run 'swaymsg reload' to activate."
}
# --- Register shortcut ------------------------------------------------------
shortcut_ok=true
case "$DE" in
gnome) install_gnome_shortcut || shortcut_ok=false ;;
kde) install_kde_shortcut || shortcut_ok=false ;;
hyprland) install_hyprland_shortcut || shortcut_ok=false ;;
sway) install_sway_shortcut || shortcut_ok=false ;;
*) shortcut_ok=false ;;
esac
if [[ "$shortcut_ok" == false ]]; then
echo
echo "Could not register shortcut automatically for this desktop environment."
echo "Bind '$EXEC_PATH' to your preferred shortcut manually."
echo "See docs/manual-shortcut-setup.md for detailed instructions."
else
echo
echo "Done. Press $SHORTCUT_DISPLAY to launch $APP_NAME."
fi