summaryrefslogtreecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/start_windowmanager.inc
blob: 556aafd38ec8c8bb2c3ffc0b561dfe86770a3e41 (plain) (blame)
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
#!/bin/bash
####################################################
# Include: Start windowmanager for easier handling #
####################################################
## Functions ##
# Some problems may arise with windows opening in background when
# using eg. vmware without a window manager.
start_wm() {
	# xfwm4 and metacity make the VM appear frozen after openslx.exe
	# tries to change the resolution. for xfwm4 the fix is to disable
	# composition. xfwm4 is better suited for mutli-screen. openbox
	# sometimes puts both virtual screens on the same physical screen,
	# vmware outright acts up if the window manager doesn't expose the
	# _NET_WM_FULLSCREEN_MONITORS atom (which openbox doesn't).
	if [ "$DISPLAY_COUNT" -gt 1 ]; then
		set -- "$@" xfwm4 openbox
	else
		set -- "$@" openbox xfwm4
	fi
	local i dm
	dm=
	for i in "$@" blackbox kwin; do
		if command -v "${i}" &> /dev/null ; then
			dm="$i"
			break
		fi
	done
	if isempty dm; then
		# not fatal, so do not exit but report via slxlog
		slxlog "runvirt-wm" "Could not find any window manager to use!"
		notify_user "Konnte keinen Window Manager finden. (Das ist schlecht!)"
		return 1
	fi

	# If it's the same, do nothing
	if ! is_wm_running "${dm}"; then
		# start DM determined above
		declare -a OPTS=()
		case "${dm}" in
		openbox|kwin|metacity)
			OPTS+=( "--replace" )
			;;
		xfwm4)
			OPTS+=( "--replace" "--compositor=off" )
			;;
		blackbox)
			# Don't replace background, toolbar not always on top
			OPTS+=( "-rc" "/opt/openslx/vmchooser/config/blackbox.rc" )
			;;
		fvwm2)
			echo "EdgeScroll 0 0" > "${TMPDIR}/fvwm"
			OPTS+=( "-replace" "-f" "${TMPDIR}/fvwm" )
			;;
		esac
		stop_wm
		writelog "Starting ${dm} ${OPTS[*]}."
		declare -g RUNVIRT_DM="${dm}"
		{
			trap '' HUP
			trap '[ -n "$wpid" ] && kill "$wpid"' EXIT
			trap '[ -n "$wpid" ] && kill "$wpid"; exit 5' INT
			trap '[ -n "$wpid" ] && kill "$wpid"; exit 6' TERM
			dc=0
			while true; do
				s="$( date +%s )"
				"${RUNVIRT_DM}" "${OPTS[@]}" &> /dev/null &
				wpid=$!
				wait
				ret=$?
				wpid=
				e="$( date +%s )"
				duration="$(( e - s ))"
				case "$ret" in
				0|129|130|143)
					writelog "WM exited normally"
					exit 0 ;;
				esac
				if [ "$duration" -ge 3 ]; then
					dc=0
				elif [ "$(( dc++ ))" -gt 5 ]; then
					notify_user "$RUNVIRT_DM crasht immer wieder."
					writelog "$RUNVIRT_DM keeps crashing"
					exit 1
				fi
			done
		} &
		declare -g RUNVIRT_DM_PID="$!"
	fi

	# run very simple taskbar in case user minimizes VM somehow and doesn't know Alt+Tab
	if [ -z "$RUNVIRT_TASKBAR_PID" ] || ! kill -0 "$RUNVIRT_TASKBAR_PID"; then
		( sleep 1; exec fspanel --background ) &
		declare -g RUNVIRT_TASKBAR_PID="$!"
	fi

	# This script conditionally runs useful stuff, like xfce4's power manager that can handle
	# backlight control keys, in case it sees there is a backlight
	/opt/openslx/scripts/start-useful-session-daemons

	if isempty HAVE_WM_CLEANUP_HOOK; then
		add_cleanup stop_wm
		declare -rg HAVE_WM_CLEANUP_HOOK=yes
	fi
	return 0
}

is_wm_running() {
	isempty RUNVIRT_DM_PID && return 1
	! isempty "$1" && [ "$RUNVIRT_DM" != "$1" ] && return 1
	kill -0 "$RUNVIRT_DM_PID"
}

stop_wm() {
	if ! isempty RUNVIRT_TASKBAR_PID; then
		kill "$RUNVIRT_TASKBAR_PID"
		declare -g RUNVIRT_TASKBAR_PID=
	fi
	if ! isempty RUNVIRT_DM_PID; then
		kill "$RUNVIRT_DM_PID"
		usleep 100000
		kill -9 "$RUNVIRT_DM_PID"
		declare -g RUNVIRT_DM_PID=
		declare -g RUNVIRT_DM=
	fi
}

## MAIN ##
call_post_source start_wm