Repository navigation
Expand file tree
/
Copy pathcortex-control
More file actions
executable file
·426 lines (385 loc) · 17.4 KB
/
Copy pathcortex-control
File metadata and controls
executable file
·426 lines (385 loc) · 17.4 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
#!/usr/bin/env bash
# Cortex Control (Neural DSP) under Wine for the Quad Cortex and Quad Cortex mini.
# Usage: cortex-control {doctor|install [installer.exe]|run|stop|update|dxvk|desktop|uninstall}
set -euo pipefail
VID=152a # Thesycon, shared with other audio brands: devices are also matched by name
MIN_WINE=11 # Wine 10.0 lacks the cfgmgr32 calls Cortex Control uses to find HID devices
PREFIX="${CORTEX_PREFIX:-${XDG_DATA_HOME:-$HOME/.local/share}/wineprefixes/cortex-control}"
LOG="${XDG_STATE_HOME:-$HOME/.local/state}/cortex-control.log"
SELF="$(readlink -f "$0")"
REPO=T13nou/cortex-control-linux
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/cortex-control-linux"
VERSION_FILE="$PREFIX/cortex-control-version"
UDEV_RULE=/etc/udev/rules.d/70-quad-cortex.rules
# The Cortex Control version this project was tested with, and where Neural DSP lists its installers.
CC_TESTED=4.1.0
CC_TESTED_SHA256=c03942a0631afd195d3d70febf91384bf39cb7101014146f26f5ba65fd73228c
CC_DOWNLOADS=https://neuraldsp.com/downloads
export WINEPREFIX="$PREFIX"
export WINEARCH=win64
# No Wine file associations or shortcuts: we create our own (desktop command).
export WINEDLLOVERRIDES="winemenubuilder.exe=d${WINEDLLOVERRIDES:+;$WINEDLLOVERRIDES}"
export WINEDEBUG="${WINEDEBUG:--all}"
# Debian and Ubuntu keep wineserver out of PATH (only in Wine's library directory).
WINESERVER_BIN="$(type -P wineserver || compgen -G '/usr/lib/*/wine/wineserver' | head -1 || true)"
export WINESERVER="$WINESERVER_BIN" # winetricks finds it through this variable
wineserver() {
[[ -n "$WINESERVER_BIN" ]] || { printf 'Error: wineserver not found\n' >&2; exit 1; }
"$WINESERVER_BIN" "$@"
}
ok() { printf ' \e[32mOK\e[0m %s\n' "$*"; }
warn() { printf ' \e[33m!!\e[0m %s\n' "$*"; }
die() { printf '\e[31mError:\e[0m %s\n' "$*" >&2; exit 1; }
# Questions are read from the terminal, even when this runs from the one-line installer's pipe.
has_tty() { { : </dev/tty; } 2>/dev/null; }
ask() {
local ans
[[ "${CORTEX_YES:-}" == 1 ]] && return 0
has_tty || return 1
read -r -p " $1 [y/N] " ans </dev/tty
[[ "$ans" =~ ^[YyOo]$ ]]
}
# Prints "<hidraw name> <product id>" for the first Neural DSP device, nothing if none is plugged in.
qc_device() {
local h id
for h in /sys/class/hidraw/hidraw*; do
grep -q '^HID_NAME=Neural DSP' "$h/device/uevent" 2>/dev/null || continue
id="$(sed -n "s/^HID_ID=0003:0000${VID^^}:0000//Ip" "$h/device/uevent")"
[[ -n "$id" ]] || continue
echo "$(basename "$h") ${id,,}"
return
done
}
# /dev/hidrawN node of the Quad Cortex, empty if it is not plugged in.
qc_hidraw() {
local dev
dev="$(qc_device)"
[[ -n "$dev" ]] && echo "/dev/${dev%% *}"
return 0
}
wine_major() {
wine --version 2>/dev/null | sed -n 's/^wine-\([0-9]*\).*/\1/p'
}
# Cortex Control (JUCE 8) renders with Direct2D; with Wine's d3d11 the window stays black.
has_dxvk() {
grep -qa dxvk "$PREFIX/drive_c/windows/system32/d3d11.dll" 2>/dev/null
}
install_dxvk() {
command -v winetricks >/dev/null || die "winetricks not found: install it with your package manager"
mkdir -p "$(dirname "$LOG")"
echo "Installing DXVK into the prefix…"
winetricks -q dxvk >>"$LOG" 2>&1 || die "winetricks dxvk failed. Log: $LOG"
}
app_exe() {
find "$PREFIX/drive_c" -iname 'Cortex Control.exe' -print -quit 2>/dev/null
}
cmd_doctor() {
local bad=0 node v latest
v="$(installed_version)"
echo "Checks:"
if ! command -v wine >/dev/null; then
warn "Wine not found: install it with your package manager"; bad=1
elif (( $(wine_major || echo 0) < MIN_WINE )); then
warn "$(wine --version): device detection is known to fail before Wine $MIN_WINE (see README)"; bad=1
else
ok "Wine $(wine --version)"
fi
node="$(qc_hidraw)"
if [[ -z "$node" ]]; then
warn "Quad Cortex not detected: plug it in over USB and power it on"; bad=1
elif [[ -r "$node" && -w "$node" ]]; then
ok "HID channel accessible ($node)"
else
warn "$node is not readable/writable: install the udev rule (see README)"; bad=1
fi
if grep -qi 'Quad Cortex' /proc/asound/cards 2>/dev/null; then ok "ALSA sound card present"; else warn "No Quad Cortex sound card in ALSA"; fi
if [[ -d "$PREFIX/drive_c" ]]; then ok "Wine prefix: $PREFIX"; else warn "No prefix yet: run '$0 install'"; bad=1; fi
if has_dxvk; then ok "DXVK installed"; else warn "DXVK missing (black window): $0 dxvk"; bad=1; fi
if [[ -n "$(app_exe)" ]]; then ok "Cortex Control ${v:+v$v }installed: $(app_exe)"; else warn "Cortex Control not installed yet"; bad=1; fi
# Not counted as a failure: the installed version still works.
latest="$(latest_version)"
if [[ -z "$latest" ]]; then
warn "could not check for a newer Cortex Control ($CC_DOWNLOADS unreachable)"
elif [[ -n "$v" ]] && newer "$latest" "$v"; then
warn "Cortex Control v$latest is available: $0 update"
elif [[ -n "$v" ]]; then
ok "Cortex Control v$v is the latest version"
fi
return $bad
}
cc_url() {
echo "https://downloads.neuraldsp.com/file/cortex-control-installers/v_$1/windows/Cortex%20Control%20v$1.exe"
}
# Newest Windows installer listed on Neural DSP's downloads page, empty if the page cannot be read.
latest_version() {
curl -fsSL --max-time 10 -A 'Mozilla/5.0' "$CC_DOWNLOADS" 2>/dev/null \
| grep -oE 'cortex-control-installers/v_[0-9.]+/windows/' \
| sed -E 's|.*/v_([0-9.]+)/.*|\1|' | sort -uV | tail -1 || true
}
# Version in an installer's file name ("Cortex Control v4.1.0.exe" -> 4.1.0).
installer_version() {
basename "$1" | sed -nE 's/.*v([0-9]+(\.[0-9]+)+)\.exe$/\1/p'
}
# Installed version, recorded at install time; empty when unknown. The executable cannot tell:
# its version resource says 0.0.0.
installed_version() {
[[ -s "$VERSION_FILE" ]] && cat "$VERSION_FILE"
return 0
}
# True when version $1 is newer than version $2.
newer() {
[[ "$1" != "$2" && "$(printf '%s\n' "$1" "$2" | sort -V | tail -1)" == "$1" ]]
}
# Downloads an installer from Neural DSP into the cache and prints its path.
download_installer() {
local version="$1" file="$CACHE_DIR/Cortex Control v$1.exe"
mkdir -p "$CACHE_DIR"
if [[ ! -f "$file" ]]; then
echo "Downloading Cortex Control v$version from Neural DSP…" >&2
curl -fL --progress-bar -o "$file.part" "$(cc_url "$version")" \
|| die "download failed: get the installer from $CC_DOWNLOADS and pass its path"
mv "$file.part" "$file"
fi
if [[ "$version" == "$CC_TESTED" ]]; then
[[ "$(sha256sum "$file" | cut -d' ' -f1)" == "$CC_TESTED_SHA256" ]] \
|| { rm -f "$file"; die "checksum mismatch on the downloaded installer"; }
ok "Verified $file" >&2
else
warn "v$version is newer than the tested v$CC_TESTED; Neural DSP publishes no checksum to verify it" >&2
fi
echo "$file"
}
# Fingerprint of the installed executable, to tell whether an installer really changed it.
app_fingerprint() {
local exe
exe="$(app_exe)"
[[ -n "$exe" ]] && sha256sum "$exe" | cut -d' ' -f1
return 0
}
# Runs an installer; fails if it reported an error and left the executable as it was
# (wizard cancelled, or an error before anything was installed).
run_installer() {
local before rc=0
mkdir -p "$(dirname "$LOG")"
before="$(app_fingerprint)"
echo "Running the installer (accept the default options)…"
echo "If the installer offers the Neural DSP/Thesycon USB driver, untick it or ignore its failure: it is not used under Wine."
wine "$1" >>"$LOG" 2>&1 || rc=$?
wineserver -w
[[ -n "$(app_exe)" ]] || die "Cortex Control.exe not found in the prefix. Log: $LOG"
if (( rc != 0 )) && [[ "$(app_fingerprint)" == "$before" ]]; then
warn "the installer stopped with an error (code $rc) and changed nothing (cancelled?)"
return 1
fi
(( rc == 0 )) || warn "the installer returned an error code $rc (often the USB driver), but Cortex Control was installed"
installer_version "$1" >"$VERSION_FILE"
}
cmd_install() {
local exe="${1:-}"
command -v wine >/dev/null || die "Wine not found: install it with your package manager"
if [[ -z "$exe" ]]; then
exe="$(download_installer "$CC_TESTED")"
else
[[ -f "$exe" ]] || die "installer not found: $exe"
fi
mkdir -p "$(dirname "$PREFIX")" "$(dirname "$LOG")"
if [[ ! -d "$PREFIX/drive_c" ]]; then
echo "Creating prefix $PREFIX…"
# Without wine-mono on the system, Wine asks in a dialog to install it, which blocks the
# install. Cortex Control is a native C++ app and does not need .NET: Mono stays disabled
# (also for the prefix updates that follow Wine upgrades).
WINEDLLOVERRIDES="$WINEDLLOVERRIDES;mscoree=" wineboot -i >>"$LOG" 2>&1
wine reg add 'HKCU\Software\Wine\DllOverrides' /v mscoree /d '' /f >>"$LOG" 2>&1
wine winecfg -v win10 >>"$LOG" 2>&1
wineserver -w
fi
has_dxvk || install_dxvk
run_installer "$exe" || die "Cortex Control was not installed. Log: $LOG"
ok "Installed: $(app_exe)"
cmd_desktop
}
# Offers to install the udev rule shipped with the scripts when /etc has another version.
check_udev_rule() {
local src
src="$(dirname "$SELF")/udev/70-quad-cortex.rules"
[[ -f "$src" ]] && ! cmp -s "$src" "$UDEV_RULE" || return 0
warn "the udev rule changed in this version ($UDEV_RULE)"
if ask "Install the new one with sudo?"; then
sudo install -m 644 "$src" "$UDEV_RULE" </dev/tty
sudo udevadm control --reload </dev/tty
sudo udevadm trigger --subsystem-match=hidraw </dev/tty
ok "udev rule updated"
else
echo " Later: sudo install -m 644 '$src' $UDEV_RULE && sudo udevadm control --reload"
fi
}
# Replaces the scripts with the latest ones: git pull for a clone, otherwise a fresh copy from GitHub.
update_scripts() {
local dir tmp
dir="$(dirname "$SELF")"
if git -C "$dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git -C "$dir" pull --ff-only || die "git pull failed in $dir"
else
[[ -f "$dir/udev/70-quad-cortex.rules" ]] || die "$dir does not look like cortex-control-linux"
tmp="$(mktemp -d)"
curl -fsSL "https://github.com/$REPO/archive/refs/heads/main.tar.gz" | tar -xz -C "$tmp" --strip-components=1 \
|| { rm -rf "$tmp"; die "could not download the scripts from GitHub"; }
# The running script keeps reading its old copy: replacing the directory is safe.
rm -rf "$dir" && mv "$tmp" "$dir"
fi
ok "Scripts up to date"
}
# Installs a newer Cortex Control over the current one (settings are kept in the prefix).
update_app() {
local exe="${1:-}" current latest
[[ -n "$(app_exe)" ]] || die "Cortex Control is not installed: $0 install"
current="$(installed_version)"
if [[ -z "$exe" ]]; then
latest="$(latest_version)"
[[ -n "$latest" ]] || die "could not read the latest version on $CC_DOWNLOADS; pass an installer: $0 update --app <installer.exe>"
if [[ -z "$current" ]]; then
# Installed before versions were recorded: assume the tested one rather than reinstalling.
[[ "$latest" == "$CC_TESTED" ]] && { echo "$CC_TESTED" >"$VERSION_FILE"; current="$CC_TESTED"; }
fi
if [[ -n "$current" ]] && ! newer "$latest" "$current"; then
ok "Cortex Control v$current is the latest version"
return 0
fi
echo " Cortex Control v${current:-unknown} installed, v$latest available."
ask "Update Cortex Control now?" || { echo " Not updated."; return 0; }
exe="$(download_installer "$latest")"
else
[[ -f "$exe" ]] || die "installer not found: $exe"
fi
if pgrep -f 'Cortex Control\.exe' >/dev/null; then
ask "Cortex Control is running: close it to update?" || { echo " Not updated."; return 0; }
fi
wineserver -k 2>/dev/null || true
if ! run_installer "$exe"; then
echo " Cortex Control was not updated."
return 1
fi
ok "Cortex Control updated to v$(installed_version)"
cmd_desktop
}
# update: scripts first, then the rest runs from the new copy of this script.
cmd_update() {
if [[ "${1:-}" == --app ]]; then
shift
check_udev_rule
update_app "$@"
return
fi
update_scripts
exec "$SELF" update --app "$@"
}
# When the Quad Cortex re-enumerates (reboot, firmware update), Wine registers its HID channel
# again without dropping the old entry, and Cortex Control then lists a phantom "Quad Cortex".
# Removing the entries makes Wine register only the device actually plugged in.
forget_stale_devices() {
local dev pid bus
dev="$(qc_device)"
[[ -n "$dev" ]] || return 0
pid="${dev##* }"
for bus in HID USB; do
# Without the launcher's activation token: it belongs to the Cortex Control window,
# otherwise the desktop opens that window in the background.
env -u DESKTOP_STARTUP_ID -u XDG_ACTIVATION_TOKEN \
wine reg delete "HKLM\\System\\CurrentControlSet\\Enum\\$bus\\VID_${VID^^}&PID_${pid^^}&MI_05" /f >>"$LOG" 2>&1 || true
done
}
cmd_run() {
local exe node
exe="$(app_exe)"
[[ -n "$exe" ]] || die "Cortex Control is not installed: $0 install"
node="$(qc_hidraw)"
if [[ -z "$node" ]]; then
warn "Quad Cortex not detected: plug it in before launching, otherwise Cortex Control will not see it."
elif [[ ! -r "$node" || ! -w "$node" ]]; then
warn "$node is not accessible: Cortex Control will not be able to talk to the Quad Cortex (udev rule missing)."
fi
mkdir -p "$(dirname "$LOG")"
# Only while Cortex Control is closed: its device must not vanish under it.
pgrep -f 'Cortex Control\.exe' >/dev/null || forget_stale_devices
cd "$(dirname "$exe")" || die "cannot enter $(dirname "$exe")"
exec wine "$exe" "$@" >>"$LOG" 2>&1
}
ICON_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/icons/hicolor/256x256/apps"
# Extracts the largest icon of the executable into the user's icon theme.
extract_icon() {
local exe tmp src
exe="$(app_exe)"
[[ -n "$exe" ]] && command -v 7z >/dev/null && command -v magick >/dev/null || return 1
tmp="$(mktemp -d)"
7z x -y -o"$tmp" "$exe" '.rsrc' >/dev/null 2>&1
src="$(find "$tmp/.rsrc" -path '*/ICON/*' -type f -printf '%s %p\n' 2>/dev/null | sort -n | tail -1 | cut -d' ' -f2-)"
# Fallback: the icon winemenubuilder extracted when the installer created its shortcut.
[[ -n "$src" ]] || src="$(find "${XDG_DATA_HOME:-$HOME/.local/share}/icons/hicolor" -path '*/apps/*_Cortex Control.0.png' \
-printf '%s %p\n' 2>/dev/null | sort -n | tail -1 | cut -d' ' -f2-)"
mkdir -p "$ICON_DIR"
[[ -n "$src" ]] && magick "$src" -thumbnail 256x256 +repage "$ICON_DIR/cortex-control.png" 2>/dev/null
local rc=$?
rm -rf "$tmp"
return $rc
}
# Shortcuts the installer creates through Wine: they match the Cortex Control window too, so the
# desktop pins them instead of ours, and they launch the app without this script.
remove_wine_shortcuts() {
local data="${XDG_DATA_HOME:-$HOME/.local/share}" desk f removed=0
desk="$(xdg-user-dir DESKTOP 2>/dev/null || echo "$HOME/Desktop")"
if [[ -d "$data/applications/wine/Programs/Cortex Control" ]]; then
rm -rf "$data/applications/wine/Programs/Cortex Control"; removed=1
fi
for f in "$data/desktop-directories/wine-Programs-Cortex Control.directory" "$desk/Cortex Control.lnk"; do
[[ -e "$f" ]] && { rm -f "$f"; removed=1; }
done
f="$desk/Cortex Control.desktop"
if [[ -f "$f" ]] && grep -qi wine "$f"; then rm -f "$f"; removed=1; fi
(( removed )) && ok "Removed the shortcuts created by Wine (they conflicted with this one)"
return 0
}
cmd_desktop() {
local apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications" icon=audio-card
mkdir -p "$apps"
if extract_icon; then icon=cortex-control; else warn "icon not extracted, using a generic icon"; fi
cat >"$apps/cortex-control.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=Cortex Control
GenericName=Quad Cortex Editor
Comment=Edit presets and settings on the Neural DSP Quad Cortex (via Wine)
Exec="$SELF" run
Icon=$icon
Terminal=false
StartupNotify=true
StartupWMClass=cortex control.exe
Categories=AudioVideo;Audio;Music;
Keywords=Neural;DSP;Quad;Cortex;guitar;preset;editor;
EOF
remove_wine_shortcuts
command -v update-desktop-database >/dev/null && update-desktop-database "$apps" >/dev/null 2>&1 || true
command -v gtk-update-icon-cache >/dev/null && gtk-update-icon-cache -f -t "${ICON_DIR%/256x256/apps}" >/dev/null 2>&1 || true
command -v kbuildsycoca6 >/dev/null && kbuildsycoca6 >/dev/null 2>&1 || true
ok "Menu entry added: $apps/cortex-control.desktop (icon: $icon)"
}
cmd_stop() {
wineserver -k 2>/dev/null || true
ok "Wine processes of the prefix stopped"
}
cmd_uninstall() {
wineserver -k 2>/dev/null || true
rm -rf "$PREFIX" "$CACHE_DIR"
rm -f "${XDG_DATA_HOME:-$HOME/.local/share}/applications/cortex-control.desktop" "$ICON_DIR/cortex-control.png"
ok "Prefix, menu entry and icon removed"
}
case "${1:-}" in
doctor) cmd_doctor ;;
install) shift; cmd_install "$@" ;;
run) shift; cmd_run "$@" ;;
desktop) cmd_desktop ;;
stop) cmd_stop ;;
update) shift; cmd_update "$@" ;;
dxvk) install_dxvk ;;
uninstall) cmd_uninstall ;;
*) sed -n '2,3p' "$SELF" | sed 's/^# //'; exit 1 ;;
esac