-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_presentation.pl
executable file
·77 lines (63 loc) · 1.91 KB
/
pi_presentation.pl
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
#!/usr/bin/perl
use strict;
use warnings;
# Feel free to adjust these values to fit your RaspberryPi distro
my $mount_path = '/media/PI_PRESENTATION';
my $mount_cmd = '/bin/mount';
my $umount_cmd = '/bin/umount';
my $libreoffice_cmd = '/usr/bin/libreoffice';
my $pid_path = '/var/run/pi_presentation.pid';
my $x_user = 'pi';
# If you need help with something below consider making an issue: https://github.com/jjshoe/pi_presentation/issues/new
my $device = $ARGV[0];
my $command = $ARGV[1];
# Let root(udev user) display to the end user's display
$ENV{'DISPLAY'} = ':0';
$ENV{'XAUTHORITY'} = '/home/' . $x_user . '/.Xauthority';
if ($command eq 'start')
{
# We need to write out the pid file so we can stop the presentation before umounting
my $pid = fork();
if ($pid == 0)
{
# Cheaper to call mkdir then check if it exists
mkdir($mount_path);
# Make sure we can kill off our children easily
setpgrp(0, 0);
# Mount up the usb device
system("$mount_cmd -o ro /dev/$device $mount_path");
# Start impress (LibreOffice's answer to powerpoint) without a splash screen, don't ask us to recover anything, and go straight into the presentation
system("$libreoffice_cmd -show $mount_path/presentation/current.* --nologo --nolockcheck --norestore");
}
else
{
# Write the PID to a pid file on disk
open(my $pid_fh, '>', $pid_path);
print $pid_fh $pid;
close($pid_fh);
}
}
else
{
my $pid;
# Open up the PID file and read it in
open(my $pid_fh, '<', $pid_path);
{
local $/;
$pid = <$pid_fh>;
}
close($pid_fh);
# Try to stop the presentation. The - is to kill the process group and is needed.
kill '-15', $pid;
# Unlink the PID file
unlink($pid_path);
while (my $umount_result = system("$umount_cmd $mount_path"))
{
if ($umount_result == 0)
{
exit;
}
# Device is busy, try again shortly
sleep(5);
}
}