summaryrefslogtreecommitdiff
path: root/bin/midori-display
blob: 7acdeac4b5241f96cdfaf895d6a0db415ea4fe0b (plain)
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
#!/bin/bash

# uncomment for very verbose script debugging...
#set -x

# Copyright (C) 2012-2017 by Debian Edu project, http://wiki.debian.org/DebianEdu
#       2012-2017, Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#       2016, Daniel Teichmann <daniel.letsplay007@gmail.com>
#       2017, Benjamin Schlüter <bschlueter@posteo.de>

# Midori Display 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.
#
# Midori Display 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 St, Fifth Floor, Boston, MA 02110-1301, USA.

# dependencies:
#     wmctrl
#     matchbox-window-manager
#     pulseaudio-utils
#     xprintidle
#     xdotool
#     xmodmap
#
# recommendations:
#     unclutter
#     pulseaudio
#     x11-xserver-utils

VERSION="0.0.2"

# Can be configured at "/etc/default/midori-display"
DEFAULT_WWW_URI="file:///usr/share/midori-display/midori-display.html"
DEFAULT_RESOLUTION=""

# all displays that need to be changed
declare -A DISPLAYS

# launch pulseaudio daemon if not already running
WITH_PULSEAUDIO="yes"

# hide idle mouse pointer
HIDE_IDLE_POINTER="yes"

# disable right and middle pointer device click in Midori sessions while keeping
# scrolling wheels' functionality intact... (consider "pointer" subcommand on
# xmodmap man page for details).
POINTER_BUTTON_MAP="1 0 0 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"

# default screensaver settings
SCREENSAVER_SETTINGS=""

# don't use an HTTP proxy by default
HTTP_PROXY_URL=""

# URI block list, if any
unset URI_BLOCKLIST

# log to stderr, if not set to "yes" in one of the config files...
LOG_TO_SYSLOG=no

# interval of browser inactivity before the session is reset automatically (in seconds)
INACTIVITY_INTERVAL="300"

if type -p xrandr 1>/dev/null; then
	currentOutput=`echo "$(xrandr)" | grep -w 'connected' | sed -r -e 's/connected.*//' | head -n 1`
fi

if [ -r /etc/default/midori-display ] && [ ! -d /etc/default/midori-display ]; then
	. /etc/default/midori-display
fi

if [ -r $HOME/.midori-display/config ] && [ ! -d $HOME/.midori-display/config ]; then
	. $HOME/.midori-display/config
fi

if [ "x$LOG_TO_SYSLOG" = "xyes" ]; then
	OUTPUT="logger -t midori-display"
else
	OUTPUT="echo"
fi

workdir="$HOME/.midori-display"
mkdir -p "$workdir"

session_lock="$workdir/.session-lock.$$"
midori_lock="$workdir/.midori-lock"

midori_config_dir=`mktemp -d -p "$workdir" midori-display-profile-XXXXXX`

# provide pulseaudio support in the browser session, if not
# already available...
if ! pacmd stat 1>/dev/null 2>/dev/null; then
	if [ "x$WITH_PULSEAUDIO" = "xyes" ]; then
		if which pulseaudio 1>/dev/null; then
			pulseaudio -D -n \
			           -L 'module-udev-detect' \
			           --exit-idle-time=65535
		fi
	fi
fi

# launch matchbox manager
if ! wmctrl -m 1>/dev/null 2>/dev/null; then
	if which matchbox-window-manager 1>/dev/null; then
		matchbox-window-manager 1>/dev/null 2>/dev/null&
	fi
fi

# use unclutter to hide idle mouse pointers
if [ "x$HIDE_IDLE_POINTER" = "xyes" ]; then
	if which unclutter 1>/dev/null; then
		unclutter 1>/dev/null 2>/dev/null &
	fi
fi

# hack mouse pointer functionality to e.g. disable "right-click" in Midori session
sanitized_pointer_button_map=$(echo ${POINTER_BUTTON_MAP//[^0-9\ ]/} | sed -e 's/^\s*//' -e 's/\s*$//')
count_buttons_pointer_button_map=$(echo -n "${sanitized_pointer_button_map//[0-9]/}"| wc -m)

if which xmodmap 1>/dev/null; then
	if [ -n "$POINTER_BUTTON_MAP" ] && [ "x$POINTER_BUTTON_MAP" = "x$sanitized_pointer_button_map" ] && \
	  [ $count_buttons_pointer_button_map -lt 32 ]; then
		xmodmap -e "pointer = $POINTER_BUTTON_MAP"
	fi

	# furthermore... let's deactivate the "Control" modifier key to disable most hotkeys in Midori
	xmodmap -e "clear Control"

else
	$OUTPUT "WARNING: Cannot strip down mouse/pointer button functionality. Make sure 'xmodmap' is installed."
fi

# if FAKE_HOSTNAME is not empty, use that string for hostname
THIS_HOSTNAME=$(hostname -f)
if [ -n "$FAKE_HOSTNAME" ]; then
	THIS_HOSTNAME="$FAKE_HOSTNAME"
fi

if `echo ${!DISPLAYS[@]} | grep -q "$THIS_HOSTNAME"`; then 
	$OUTPUT "INFO: $THIS_HOSTNAME has extra properties. loading them.."

	if echo ${DISPLAYS[$THIS_HOSTNAME]} | grep -q "www_uri="; then
		WWW_URI=`echo ${DISPLAYS[$THIS_HOSTNAME]} | sed -r -e 's/(^|.*\|)www_uri=([^|]*).*/\2/'`
	else
		WWW_URI="$DEFAULT_WWW_URI"
	fi

	if echo ${DISPLAYS[$THIS_HOSTNAME]} | grep -q "res="; then
		RESOLUTION=`echo ${DISPLAYS[$THIS_HOSTNAME]} | sed -r -e 's/(^|.*\|)res=([^|]*).*/\2/'`
	else
		RESOLUTION="$DEFAULT_RESOLUTION"
	fi
else
	WWW_URI="$DEFAULT_WWW_URI"
	RESOLUTION="$DEFAULT_RESOLUTION"
fi

if ! echo "$WWW_URI" | grep -q -E "^(file://|http://|https://).*"; then
	$OUTPUT "ERROR: WWW_URI format not supported: $WWW_URI"
	$OUTPUT "       Use file://<path>/<index>.<html> or http(s)://<host>/<path>/<index>.<html>."
	$OUTPUT "       Doing nothing."
	exit -1
fi

# if URI_BLOCKLIST is not set, populate it and limit access to the WWW_SERVER that WWW_URI points to only.
if [ ! -v URI_BLOCKLIST ]; then
	URI_TYPE=`echo "$WWW_URI" | sed -r -e 's@(file://|http(|s)://)[^/]+/.*@\1@'`
	WWW_SERVER=""
	if echo "$URI_TYPE" | grep -q -E "^http(|s)://([^/]+)/.*"; then
		WWW_SERVER=`echo "$WWW_URI" | sed -r -e 's@^http(|s)://([^/]+)/.*@\2@'`
	fi
	URI_BLOCKLIST='^((?!'$URI_TYPE'.*'$WWW_SERVER'/).).*$'
fi

if [ -n "$URI_BLOCKLIST" ]; then
	URI_BLOCKLIST="-b $URI_BLOCKLIST"
fi

if [ -n "${RESOLUTION}" ]; then
	if type -p xrandr 1>/dev/null; then
		xrandr -d :0 --output "$(echo $currentOutput)" --mode $RESOLUTION
	fi
fi

# Set screensaver settings
if which xset 1>/dev/null; then
	xset s ${SCREENSAVER_SETTINGS}
fi

# set some proxy related env variables, if requested...
if [ -n "$HTTP_PROXY_URL" ]; then
	export http_proxy="$HTTP_PROXY_URL"
	export https_proxy="$HTTP_PROXY_URL"
	export ftp_proxy="$HTTP_PROXY_URL"
fi

function cleanup {
	if type -p xrandr 1>/dev/null; then
		$OUTPUT "Info: resetting resolution"
		xrandr -d :0 --output "$(echo $currentOutput)" --auto
	fi

	if [ -r "$midori_lock" ]; then
		midori_pid=$(cat "$midori_lock" | sed -e 's/[^0-9]*//g')
		if [ -n "$midori_pid" ]; then
			kill -0 $midori_pid 2>/dev/null && kill "$midori_pid"
		fi
	fi

	if [ -d "$midori_config_dir" ]; then
		rm -R "$midori_config_dir"
	fi

	for rmfile in $midori_lock \
	              $session_lock \
	              ;
	do
		if [ -e "$rmfile" ]; then
			rm "$rmfile"
		fi
	done

}
trap "cleanup" SIGINT SIGTERM ERR EXIT

function midori_loop {
	if [ "x$INACTIVITY_INTERVAL" != "x0" ]; then
		(
			while [ -e "$session_lock" ]; do
				sleep 1
				if [ `xprintidle 2>/dev/null || echo 0` -gt $(($INACTIVITY_INTERVAL*1000)) ]; then
					echo -n "RESTART" > $midori_lock
					$OUTPUT "INFO: Triggering browser restart, too much idling around..."
					# reset X11 idle counter by some harmless key event
					xdotool key Shift
				fi
			done
		) &
	fi
}


function midori_session {

	if which midori 1>/dev/null; then

		while [ -e "$session_lock" ]; do

			$OUTPUT "INFO: Starting a new instance of the midori application."
			midori								\
			            -e Fullscreen					\
			                -e enable-plugins=false				\
			                -e enable-page-cache=false			\
			                -e open-new-pages-in=MIDORI_NEW_PAGE_CURRENT	\
			                -e close-buttons-on-tabs=false			\
			                -e show-menubar=false				\
			                -e show-statusbar=false				\
			                -e show-bookmarkbar=false			\
			                -e show-panel=false				\
			                -e show-crash-dialog=false			\
			                -e open-tabs-in-the-background=false		\
			                -e open-tabs-next-to-current=false		\
			                -e open-popups-in-tabs=false			\
			                -e enable-xss-auditor=false			\
			                -e enable-javascript=true			\
			                -e enable-developer-extras=false		\
			                -e enable-html5-database=true			\
			                -e enable-running-of-insecure-content=false	\
			                -e enable-display-of-insecure-content=false	\
			                -e javascript-can-access-clipboard=false	\
			                -e print-without-dialog=true			\
			       -c "${midori_config_dir}"					\
			       ${URI_BLOCKLIST}						\
			       -a "${WWW_URI}" 1>/dev/null 2>/dev/null &

			midori_pid=$!

			echo -n "$midori_pid" > "$midori_lock"

			while [ -r "$midori_lock" ] && [ "$(cat $midori_lock)" != "RESTART" ] && [ -e "$session_lock" ]; do
				sleep 1
				if ! kill -0 "$midori_pid" 2>/dev/null; then
					if [ -e ${session_lock} ]; then
						rm "${session_lock}"
					fi
					if [ -d "${midori_config_dir}" ]; then
						rm -R "${midori_config_dir}"
					fi
					break

					midori_config_dir=`mktemp -d -p "$workdir" midori-display-profile-XXXXXX`

				fi
			done

			kill -0 "$midori_pid" 2>/dev/null && kill "$midori_pid"
			$OUTPUT "INFO: The midori application has terminated."

		done

	else
		$OUTPUT "ERROR: The midori application is not installed."
	fi

}


function create_lock {

	touch "$session_lock"

}

### MAIN ###

$OUTPUT "INFO: midori Display (version $VERSION)"
$OUTPUT "INFO: HOSTNAME set to: $THIS_HOSTNAME"

if [ -n "$WWW_URI" ]; then

	$OUTPUT "INFO: WWW_URI is configured. Using content from given URL: $WWW_URI"
	create_lock
	midori_loop

else

	$OUTPUT "ERROR: WWW_URI hasn't been configured. Doing nothing."
	exit 0

fi

midori_session

exit 0