Skip to content

Commit 248b12c

Browse files
authored
Create Switch_To_Other_Session.sh
1 parent 64c5b81 commit 248b12c

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
LOGFILE="/var/log/switch_session.log"
6+
exec 3>&1 1>>"$LOGFILE" 2>&1
7+
8+
# Helpers
9+
log() { echo "$(date +'%F %T') - $*" >&3; }
10+
user_name() {
11+
# Prefer SUDO_USER when run with sudo, otherwise $USER
12+
if [ -n "${SUDO_USER:-}" ]; then
13+
echo "$SUDO_USER"
14+
else
15+
echo "${USER:-$(whoami)}"
16+
fi
17+
}
18+
19+
current_vt() {
20+
if command -v fgconsole >/dev/null 2>&1; then
21+
fgconsole 2>/dev/null || echo "$(who | awk '/:0/{print $2; exit}' || true)"
22+
else
23+
who | awk '/:0/{print $2; exit}' || true
24+
fi
25+
}
26+
27+
is_running() {
28+
# is_running <process-name-or-fullpath>
29+
pkill -0 -f "$1" >/dev/null 2>&1
30+
}
31+
32+
prompt() {
33+
zenity --info --width=450 --text="$1"
34+
}
35+
36+
error_box() {
37+
zenity --error --width=520 --text="$1"
38+
}
39+
40+
confirm() {
41+
zenity --question --width=520 --text="$1" && return 0 || return 1
42+
}
43+
44+
# Start a command on a specific vt as the original user
45+
start_on_vt() {
46+
local vt="$1"; shift
47+
local cmd="$*"
48+
local u="$(user_name)"
49+
50+
log "Starting on VT${vt}: $cmd (user: $u)"
51+
52+
# openvt starts a program on the given VT. Use su to run as the user.
53+
if command -v openvt >/dev/null 2>&1; then
54+
openvt -s -w -- su - "$u" -c "(exec setsid $cmd)" >/dev/null 2>&1 || return 1
55+
else
56+
# fallback: use chvt + nohup
57+
su - "$u" -c "nohup sh -c '$cmd' >/dev/null 2>&1 &" || return 1
58+
fi
59+
}
60+
61+
switch_vt() {
62+
local vt="$1"
63+
log "Switching to VT$vt"
64+
if [ "$(id -u)" -ne 0 ]; then
65+
# try pkexec to run chvt as root
66+
if command -v pkexec >/dev/null 2>&1; then
67+
pkexec chvt "$vt" || return 1
68+
else
69+
sudo chvt "$vt" || return 1
70+
fi
71+
else
72+
chvt "$vt" || return 1
73+
fi
74+
}
75+
76+
# Detect sessions
77+
detect_desktop() {
78+
if is_running plasmashell || is_running startplasma || is_running kwin; then
79+
echo "kde"
80+
elif is_running wayfire || is_running wf-shell || is_running wayfired; then
81+
echo "wayfire"
82+
elif [ -x "/usr/share/HackerOS/Scripts/Bin/hacker_mode.sh" ] || [ -x "/usr/share/HackerOS/Scripts/HackerOS-Apps/Hacker-Mode" ]; then
83+
echo "hacker-capable"
84+
else
85+
echo "unknown"
86+
fi
87+
}
88+
89+
# Commands to launch
90+
plasma_cmd() {
91+
# prefer startplasma-wayland if present, else startplasma-x11, else plasmashell
92+
if command -v startplasma-wayland >/dev/null 2>&1; then
93+
echo "startplasma-wayland"
94+
elif command -v startplasma-x11 >/dev/null 2>&1; then
95+
echo "startplasma-x11"
96+
elif command -v plasmashell >/dev/null 2>&1; then
97+
echo "plasmashell"
98+
else
99+
echo ""
100+
fi
101+
}
102+
103+
wayfire_cmd() {
104+
if command -v wayfire >/dev/null 2>&1; then
105+
echo "wayfire"
106+
else
107+
echo ""
108+
fi
109+
}
110+
111+
hacker_cmd() {
112+
if [ -x "/usr/share/HackerOS/Scripts/HackerOS-Apps/Hacker-Mode" ]; then
113+
echo "/usr/share/HackerOS/Scripts/HackerOS-Apps/Hacker-Mode"
114+
elif [ -x "/usr/share/HackerOS/Scripts/Bin/hacker_mode.sh" ]; then
115+
echo "/usr/share/HackerOS/Scripts/Bin/hacker_mode.sh"
116+
else
117+
echo ""
118+
fi
119+
}
120+
121+
# GUI selection
122+
CHOICE=$(zenity --list --title="Wybierz sesję" --height=240 --width=420 --column="Opcja" "KDE (Plasma)" "Wayfire" "Hacker Mode" --text="Wybierz na jaką sesję chcesz się przełączyć:") || exit 1
123+
124+
log "User chose: $CHOICE"
125+
126+
CURRENT_DESKTOP=$(detect_desktop)
127+
CURRENT_VT=$(current_vt || echo "unknown")
128+
log "Detected current desktop: $CURRENT_DESKTOP, VT: $CURRENT_VT"
129+
130+
case "$CHOICE" in
131+
"KDE (Plasma)")
132+
# Target: VT2. Current: if on KDE then don't show option? The user asked: if already KDE then don't show 'przelacz na kde' - but we can't re-prompt now, so check and warn.
133+
if [ "$CURRENT_DESKTOP" = "kde" ]; then
134+
zenity --info --text="Wygląda na to, że jesteś już w KDE (Plasma). Nic do zrobienia." || true
135+
exit 0
136+
fi
137+
138+
TARGET_VT=2
139+
PL_CMD="$(plasma_cmd)"
140+
if [ -z "$PL_CMD" ]; then
141+
error_box "Nie znaleziono polecenia uruchamiającego Plasma (search: startplasma-wayland / startplasma-x11 / plasmashell). Upewnij się, że masz zainstalowane pakiety Plasma."
142+
exit 1
143+
fi
144+
145+
# Start plasma on VT2 if not running
146+
if ! is_running plasmashell && ! is_running startplasma; then
147+
if confirm "Plasma nie jest uruchomiona — chcesz ją uruchomić na tty${TARGET_VT}?"; then
148+
start_on_vt "$TARGET_VT" "$PL_CMD" || error_box "Nie udało się uruchomić Plasma na vt${TARGET_VT}. Sprawdź logi."
149+
sleep 2
150+
else
151+
exit 0
152+
fi
153+
fi
154+
switch_vt "$TARGET_VT" || error_box "Nie udało się przełączyć na vt${TARGET_VT}."
155+
;;
156+
157+
"Wayfire")
158+
if [ "$CURRENT_DESKTOP" = "wayfire" ]; then
159+
zenity --info --text="Wygląda na to, że jesteś już w Wayfire. Nic do zrobienia." || true
160+
exit 0
161+
fi
162+
163+
TARGET_VT=4
164+
WF_CMD="$(wayfire_cmd)"
165+
if [ -z "$WF_CMD" ]; then
166+
error_box "Nie znaleziono programu wayfire. Zainstaluj wayfire lub popraw ścieżkę."; exit 1
167+
fi
168+
169+
if ! is_running wayfire; then
170+
if confirm "Wayfire nie jest uruchomiony — chcesz go uruchomić na tty${TARGET_VT}?"; then
171+
start_on_vt "$TARGET_VT" "$WF_CMD" || error_box "Nie udało się uruchomić Wayfire na vt${TARGET_VT}."
172+
sleep 2
173+
else
174+
exit 0
175+
fi
176+
fi
177+
switch_vt "$TARGET_VT" || error_box "Nie udało się przełączyć na vt${TARGET_VT}."
178+
;;
179+
180+
"Hacker Mode")
181+
# Always show Hacker Mode option
182+
H_CMD="$(hacker_cmd)"
183+
if [ -z "$H_CMD" ]; then
184+
error_box "Nie znaleziono skryptu Hacker Mode w /usr/share/HackerOS/Scripts/."; exit 1
185+
fi
186+
187+
# Determine which vt we are switching from per spec: "przelacz z tty2 (jezeli plasma) lub z tty4 jezeli wayfire"
188+
if [ "$CURRENT_DESKTOP" = "kde" ]; then
189+
FROM_VT=2
190+
elif [ "$CURRENT_DESKTOP" = "wayfire" ]; then
191+
FROM_VT=4
192+
else
193+
# If unknown, ask user
194+
if confirm "Nie wykryto aktywnej sesji KDE/Wayfire. Przełączyć na Hacker Mode na tty3?"; then
195+
:
196+
else
197+
exit 0
198+
fi
199+
FROM_VT="auto"
200+
fi
201+
202+
TARGET_VT=3
203+
204+
# Start hacker mode if not running
205+
if ! is_running "$H_CMD" && ! is_running hacker_mode.sh; then
206+
if confirm "Hacker Mode nie jest uruchomiony — chcesz go uruchomić na tty${TARGET_VT}?"; then
207+
start_on_vt "$TARGET_VT" "$H_CMD" || error_box "Nie udało się uruchomić Hacker Mode.";
208+
sleep 1
209+
else
210+
exit 0
211+
fi
212+
fi
213+
214+
switch_vt "$TARGET_VT" || error_box "Nie udało się przełączyć na vt${TARGET_VT}."
215+
;;
216+
*)
217+
exit 0
218+
;;
219+
esac
220+
221+
zenity --info --text="Przełączanie zakończone. Sprawdź $LOGFILE dla szczegółów." || true
222+
exit 0

0 commit comments

Comments
 (0)