multiseat-controller 6.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

# Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of le-multiterminal
#
# le-multiterminal is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

#### Description: Prepares the environment and launches the seat configuration scripts.
#### Xorg that communicates with the Thinnetworks card (fake-seat) should already be running.
25
#### Written by: Stephanie Briere Americo - sba16@c3sl.inf.ufpr.br on 2017.
26
27
28
29

set -x

## Auxiliary scripts
30
31
source /usr/lib/find-devices
source /usr/lib/window-acess
32
33

## Path constants
34
35
MC3SL_DEVICES="/usr/sbin/devices" # shortcut to devices that have already been paired
#TODO: This path is correctly??
36
MC3SL_CONF="/etc/le-multiterminal/98-xephyr-multi-seat.conf" # lightdm settings file (associates seat to output)
37
LIGHTDM_CONF="/etc/xdg/lightdm/lightdm.conf.d/98-xephyr-multi-seat.conf"
38

39
## Script/function in other file
40
41
42
FIND_KEYBOARD="find_keyboard" # "find-devices"
CREATE_WINDOW="create_window" # "window-acess"
WRITE_WINDOW="write_window" # "window-acess"
43
44
45

## Macros
FAKE_DISPLAY=:90 # display to access fake-seat (secondary card)
46
FAKE_SEAT="__fake-seat-1__"
47
OUTPUTS=("LVDS" "VGA") # output options
48
MAX_ATTEMPTS=100
49

50
## Variables
51
52
53
54
55
WINDOW_ID_INIT=0 # if the onboard board, the start is 0; otherwise it is 1
WINDOW_COUNTER=0 # how many windows were created
N_SEATS_LISTED=0 # how many seats are there in the system
ONBOARD=0 # if the onboard is connected
declare -a DISPLAY_XORGS # saves the display of the Xorg launched processes
56
declare -a ID_WINDOWS # saves the created window ids (used in window-acess)
57
58
59
60
declare -a PID_FIND_DEVICES # saves the pid from the launched configuration processes

create_onboard_window () {
	# Checks if there is a device connected to the onboard card
61
	if test "$(cat "/sys$(udevadm info /sys/class/drm/card0 | grep "P:" | cut -d " " -f2)/card0-VGA-1/status")" == "connected"; then
62
63
64
65
66
		# Runs Xorg and creates the window for the onboard card
		DISPLAY_XORGS[$WINDOW_COUNTER]=:$(($WINDOW_COUNTER+10))
		export DISPLAY=${DISPLAY_XORGS[$WINDOW_COUNTER]}

		Xorg ${DISPLAY_XORGS[$WINDOW_COUNTER]} &
67
		pid=$!
68

69
70
71
		# Guarantees the Xorg execution
		xdpyinfo -display ${DISPLAY_XORGS[$WINDOW_COUNTER]}
		EXIT_CODE=$?
72
		N_ATTEMPT=0
73
		while test $EXIT_CODE -ne 0; do
74
75
76
77
78
79
80
			sleep 0.5
			xdpyinfo -display ${DISPLAY_XORGS[$WINDOW_COUNTER]}
			EXIT_CODE=$?
			if ! kill -0 "${pid}" >/dev/null 2>&1; then
				Xorg ${DISPLAY_XORGS[$WINDOW_COUNTER]} &
				pid=$!
			fi
81

82
			if test $N_ATTEMPT -gt $MAX_ATTEMPTS; then
83
84
85
86
87
				echo "[Error] Run Xorg ${DISPLAY_XORGS[$WINDOW_COUNTER]} failed."
				exit 1
	    fi

			N_ATTEMPT=$(($N_ATTEMPT+1))
88
		done
89
90

		$CREATE_WINDOW
91

92
93
94
95
96
97
98
99
100
101
		ONBOARD=1
	else
		WINDOW_ID_INIT=1
		WINDOW_COUNTER=1
	fi
}

create_secundarycard_windows () {
	# The fake_seat display needs to be exported to run the Xephyr that communicate with it
	export DISPLAY=$FAKE_DISPLAY
102

103
104
105
106
107
108
	for i in ${OUTPUTS[*]}; do
		# Display for each output
		DISPLAY_XORGS[$WINDOW_COUNTER]=:$((WINDOW_COUNTER+10))

		# Run Xephyr to type in this output
		Xephyr ${DISPLAY_XORGS[$WINDOW_COUNTER]} -output $i -noxv &
109
		pid=$!
110

111
112
113
		# Guarantees the Xephyr execution
		xdpyinfo -display ${DISPLAY_XORGS[$WINDOW_COUNTER]}
		EXIT_CODE=$?
114
		N_ATTEMPT=0
115
		while test $EXIT_CODE -ne 0; do
116
117
118
119
120
121
122
			sleep 0.5
			xdpyinfo -display ${DISPLAY_XORGS[$WINDOW_COUNTER]}
			EXIT_CODE=$?
			if ! kill -0 "${pid}" >/dev/null 2>&1; then
				Xephyr ${DISPLAY_XORGS[$WINDOW_COUNTER]} -output $i -noxv &
				pid=$!
			fi
123

124
			if test $N_ATTEMPT -gt $MAX_ATTEMPTS; then
125
126
127
128
129
				echo "[Error] Run Xephyr ${DISPLAY_XORGS[$WINDOW_COUNTER]} failed."
				exit 1
	    fi

			N_ATTEMPT=$(($N_ATTEMPT+1))
130
		done
131
132
133

		# Export display and create a window to write on this output
		export DISPLAY=${DISPLAY_XORGS[$WINDOW_COUNTER]}
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
		$CREATE_WINDOW

		# Again: the fake_seat display needs to be exported
		export DISPLAY=$FAKE_DISPLAY
	done
}

configure_devices () {
	COUNT=0
	for WINDOW in `seq $WINDOW_ID_INIT $(($WINDOW_COUNTER-1))`; do
		$FIND_KEYBOARD $(($WINDOW+1)) &
		PID_FIND_DEVICES[$COUNT]=$!
		COUNT=$(($COUNT+1))

		$WRITE_WINDOW press_key $WINDOW
	done
}

kill_jobs () {
154
	if test -n "$(ls | grep lock)"; then
155
156
157
		rm lock*
	fi

158
	if test -n "$(ls $MC3SL_DEVICES)"; then
159
160
161
		rm -rf $MC3SL_DEVICES/
	fi

162
163
	rm -rf $MC3SL_DEVICES

164
165
166
167
168
	pkill -P $$
}

############ BEGIN ############

169
# Guarantees the fake-seat execution, used to access the secondary board
170
Xorg $FAKE_DISPLAY -seat $FAKE_SEAT -dpms -s 0 -nocursor &
171
pid=$!
172

173
174
xdpyinfo -display $FAKE_DISPLAY
EXIT_CODE=$?
175
N_ATTEMPT=0
176
while test $EXIT_CODE -ne 0; do
177
178
179
180
	sleep 0.5
	xdpyinfo -display $FAKE_DISPLAY
	EXIT_CODE=$?
	if ! kill -0 "${pid}" >/dev/null 2>&1; then
181
		Xorg $FAKE_DISPLAY -seat $FAKE_SEAT -dpms -s 0 -nocursor &
182
183
		pid=$!
	fi
184

185
	if test $N_ATTEMPT -gt $MAX_ATTEMPTS; then
186
187
188
189
190
		echo "[Error] Run Xorg $FAKE_DISPLAY -seat $FAKE_SEAT failed."
		exit 1
	fi

	N_ATTEMPT=$(($N_ATTEMPT+1))
191
192
done

193
194
195
196
197
198
199
200
201
# If the onboard is connected, it creates a window to write on the screen
create_onboard_window

# Creates the necessary windows to write on the screen of each output of the secondary card
create_secundarycard_windows

# We'll use it to save a shortcut to devices that have already been paired
mkdir -p $MC3SL_DEVICES

202
# Run configuration script for each seat
203
204
205
206
207
configure_devices

# Wait until all seats are configured
N_SEATS_LISTED=$(($(loginctl list-seats | grep -c "seat-")+$ONBOARD))
CONFIGURED_SEATS=0
208
while test $CONFIGURED_SEATS -lt $N_SEATS_LISTED; do
209
210
211
	wait -n ${PID_FIND_DEVICES[*]}
	EXIT_CODE=$?

212
	if test $EXIT_CODE -eq 0; then
213
		CONFIGURED_SEATS=$(($CONFIGURED_SEATS+1))
214
	else if test $EXIT_CODE -eq 2; then
215
		echo "[Error] Can not configure output: find-devices failed."
216
217
218
		rm -f $MC3SL_CONF
		exit 1
	fi
219
	fi
220
221
done

222
223
224
# Move settings file created for lightdm folder
mv -f $MC3SL_CONF $LIGHTDM_CONF

225
226
227
# Cleans the system by killing all the processes and files it has created
kill_jobs

228

229
exit 0