Working version, displays HTML and runs scripts.
[techtalk-pse.git] / techtalk-pse.pl
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3 # @configure_input@
4 #
5 # Tech Talk PSE
6 # Copyright (C) 2010 Red Hat Inc.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 use warnings;
23 use strict;
24 use utf8;
25
26 use Pod::Usage;
27 use Getopt::Long;
28 use Cwd qw(getcwd abs_path);
29 use Glib qw(TRUE FALSE);
30 use Gtk2 -init;
31 use Gtk2::MozEmbed;
32
33 =encoding utf8
34
35 =head1 NAME
36
37 techtalk-pse - superior technical demonstration software
38
39 =head1 SYNOPSIS
40
41  cd /path/to/talk/; techtalk-pse
42
43  techtalk-pse /path/to/talk/
44
45 =head1 DESCRIPTION
46
47 Tech Talk "Platinum Supreme Edition" (PSE) is Linux Presentation
48 Software designed by technical people to give technical software
49 demonstrations to other technical people.  It is designed to be simple
50 to use (for people who know how to use an editor and the command line)
51 and powerful, so that you can create informative, technically accurate
52 and entertaining talks and demonstrations.
53
54 Tech Talk PSE is good at opening editors at the right place, opening
55 shell prompts with preloaded history, compiling and running things
56 during the demonstration, displaying text, photos, figures and video.
57
58 Tech Talk PSE is I<bad> at slide effects, chart junk and bullet
59 points.
60
61 This manual page covers all the documentation you will need to use
62 Tech Talk PSE.  The next section covers running the tool from the
63 command line.  After that there is a L</TUTORIAL> section to get you
64 started.  Then there is a detailed L</REFERENCE> section.  Finally
65 there is a discussion on L<WHAT MAKES A GOOD TALK>.
66
67 =head1 RUNNING THE TOOL FROM THE COMMAND LINE
68
69 A Tech Talk PSE talk is not a single file, but a directory full of
70 files.  (If you want to start a new talk, see the L</TUTORIAL> section
71 below).  To display or run the talk, change into the directory
72 containing all those files and run the C<techtalk-pse> command:
73
74  cd /path/to/talk/; techtalk-pse
75
76 You can also run C<techtalk-pse> without changing directory, instead
77 specifying the path to the talk:
78
79  techtalk-pse /path/to/talk/
80
81 =head2 OPTIONS
82
83 =over 4
84
85 =cut
86
87 my $help;
88
89 =item B<--help>
90
91 Display brief help and exit.
92
93 =cut
94
95 my $last;
96
97 =item B<--last>
98
99 Start at the last slide.
100
101 You cannot use this with the B<-n> / B<--start> option.
102
103 =cut
104
105 my $start;
106
107 =item B<-n SLIDE> | B<--start SLIDE>
108
109 Start at the named slide.  I<SLIDE> is the shortest unique prefix of
110 the slide name, so to start at a slide named
111 I<00010-introduction.html>, you could use I<-n 00010> or I<-n 00010-intro>,
112 or give the full filename I<-n 00010-introduction.html>.
113
114 The default is to start at the first slide in the talk.
115
116 =cut
117
118 my $splash = 1;
119
120 =item B<--no-splash>
121
122 Don't display the initial "splash" screen which advertises Tech Talk
123 PSE to your audience.  Just go straight into the talk.
124
125 =cut
126
127 my $verbose;
128
129 =item B<--verbose>
130
131 Display verbose messages, useful for debugging or tracing
132 what the program is doing.
133
134 =cut
135
136 my $version;
137
138 =item B<--version>
139
140 Display version number and exit.
141
142 =cut
143
144 my $mozembed;
145
146 GetOptions ("help|?" => \$help,
147             "last" => \$last,
148             "mozembed" => \$mozembed,
149             "n=s" => \$start,
150             "splash!" => \$splash,
151             "start=s" => \$start,
152             "verbose" => \$verbose,
153             "version" => \$version,
154     ) or pod2usage (2);
155
156 =back
157
158 =cut
159
160 pod2usage (1) if $help;
161 if ($version) {
162     print "@PACKAGE@ @VERSION@\n";
163     exit
164 }
165 die "techtalk-pse: cannot use --start and --last options together\n"
166     if defined $last && defined $start;
167
168 # --mozembed runs Gtk2::MozEmbed as a subprocess, because MozEmbed
169 # is very crashy.
170 if ($mozembed) {
171     my $w = Gtk2::Window->new ();
172     my $moz = Gtk2::MozEmbed->new ();
173
174     $w->signal_connect (delete_event => sub {
175         Gtk2->main_quit;
176         return FALSE;
177                         });
178
179     $w->set_default_size (600, 400);
180     $w->add ($moz);
181     $w->show_all ();
182     $moz->load_url ($ARGV[0]);
183     Gtk2->main;
184
185     exit 0;
186 }
187
188 die "techtalk-pse: too many arguments\n" if @ARGV >= 2;
189
190 # Get the true name of the program.
191 $0 = abs_path ($0);
192
193 # Locate the talk.
194 if (@ARGV > 0) {
195     my $d = $ARGV[0];
196     if (-d $d) {
197         chdir $d or die "techtalk-pse: chdir: $d: $!";
198     } else {
199         # XXX In future allow people to specify an archive and unpack
200         # it for them.
201         die "techtalk-pse: argument is not a directory"
202     }
203 }
204
205 # Get the files.
206 my @files;
207 my %files;
208 my %groups;
209 sub reread_directory
210 {
211     @files = ();
212     %groups = ();
213
214     foreach (glob ("*")) {
215         if (/^(\d+)([A-Z])?(?:-.*)\.(html|sh)$/) {
216             print STDERR "reading $_\n" if $verbose;
217
218             my $seq = $1;
219             my $pos = $2 || "A";
220             my $ext = $3;
221             warn "techtalk-pse: $_: command file is not executable (+x)\n"
222                 if $ext eq "sh" && ! -x $_;
223
224             my $h = { name => $_, seq => $1, pos => $2, ext => $3 };
225             push @files, $h;
226             $files{$_} = $h;
227
228             $groups{$seq} = [] unless exists $groups{$seq};
229             push @{$groups{$seq}}, $h;
230         } else {
231             print STDERR "ignoring $_\n" if $verbose;
232         }
233     }
234 }
235 reread_directory ();
236 print STDERR "read ", 0+@files, " files\n" if $verbose;
237 if (@files == 0) {
238     warn "techtalk-pse: no files found, continuing anyway ...\n"
239 }
240
241 # Work out what slide we're starting on.
242 my $current;
243 if (defined $current) {
244     die "start slide not implemented yet XXX"
245 }
246 elsif (@files) {
247     $current = $files[0];
248 }
249 # else $current is undefined
250
251 if ($splash) {
252     my $w = Gtk2::AboutDialog->new;
253     $w->set_authors ("Richard W.M. Jones");
254     $w->set_comments (
255         "Superior technical demonstration software\n".
256         "\n".
257         "Keys\n".
258         "↑ — Go back one slide\n".
259         "↓ — Go forward one slide\n"
260         );
261     $w->set_program_name ("Tech Talk Platinum Supreme Edition (PSE)");
262     $w->set_version ("@VERSION@");
263     $w->set_website ("http://people.redhat.com/~rjones");
264     $w->set_license ("GNU General Public License v2 or above");
265     $w->signal_connect (destroy => sub { Gtk2->main_quit });
266     $w->show_all;
267     Gtk2->main;
268 }
269
270 my $quit;
271 while (!$quit) {
272     if (defined $current) {
273         my $go = show_slide ($current);
274         if (defined $go && ($go eq "PREV" || $go eq "NEXT")) {
275             print STDERR "go = $go\n" if $verbose;
276             my $i = 0;
277           FOUND: {
278               foreach (@files) {
279                   last FOUND if $files[$i]->{name} eq $current->{name};
280                   $i++;
281               }
282               die "internal error: cannot find \$current in \@files"
283             }
284             print STDERR "found current entry at i = $i\n" if $verbose;
285             $i-- if $go eq "PREV" && $i > 0;
286             $i++ if $go eq "NEXT" && $i+1 < @files;
287             $current = $files[$i];
288         }
289     } else {
290         print "No slides found.  Press any key to reload directory ...\n";
291         $_ = <STDIN>;
292     }
293
294     reread_directory ();
295
296     if (defined $current && !exists $files{$current->{name}}) {
297         # Current slide was deleted.
298         undef $current;
299         $current = $files[0] if @files;
300     }
301 }
302
303 sub show_slide {
304     my $slide = shift;
305
306     if ($slide->{ext} eq "html") {
307         # MozEmbed is incredibly crashy, so we run ourself as a
308         # subprocess, so when it segfaults we don't care.
309         my $cwd = getcwd;
310         my $url = "file://" . $cwd . "/" . $slide->{name};
311         my @cmd = ($0, "--mozembed", $url);
312         system (@cmd);
313         die "failed to execute subcommand: $!\n" if $? == -1;
314         if ($? & 127) {
315             # Subcommand probably segfaulted, just continue to next slide.
316             return "NEXT";
317         } else {
318             my $r = $? >> 8;
319             if ($r == 0) {
320                 return "NEXT";
321             } elsif ($r == 1) {
322                 return "PREV";
323             } elsif ($r == 2) {
324                 return "QUIT";
325             }
326         }
327     }
328     elsif ($slide->{ext} eq "sh") {
329         system ("PATH=.:\$PATH " . $slide->{name});
330         return "NEXT";
331     }
332 }
333
334 1;
335
336 =head1 TUTORIAL
337
338 =head1 REFERENCE
339
340 =head1 WHAT MAKES A GOOD TALK
341
342 =head1 SEE ALSO
343
344 The Cognitive Style of PowerPoint, Tufte, Edward R.
345
346 =head1 AUTHOR
347
348 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
349
350 =head1 COPYRIGHT
351
352 Copyright (C) 2010 Red Hat Inc.
353
354 This program is free software; you can redistribute it and/or modify
355 it under the terms of the GNU General Public License as published by
356 the Free Software Foundation; either version 2 of the License, or
357 (at your option) any later version.
358
359 This program is distributed in the hope that it will be useful,
360 but WITHOUT ANY WARRANTY; without even the implied warranty of
361 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
362 GNU General Public License for more details.
363
364 You should have received a copy of the GNU General Public License
365 along with this program; if not, write to the Free Software
366 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.