2020-12-20 14:38:14 +01:00
|
|
|
/* See LICENSE file for license details. */
|
2015-08-27 23:54:58 +02:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
2020-12-20 15:38:15 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2015-08-27 23:54:58 +02:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <fcntl.h>
|
2020-12-20 15:38:15 +01:00
|
|
|
#include <signal.h>
|
2015-08-27 23:54:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
2018-07-08 14:54:37 +02:00
|
|
|
|
2020-12-20 15:25:52 +01:00
|
|
|
char *currentslidep, **slidefiles; /* the slides */
|
|
|
|
int nslides, currentslide, currentslidelen;
|
2018-07-08 14:54:37 +02:00
|
|
|
|
2018-07-08 14:59:13 +02:00
|
|
|
void
|
2020-12-20 15:25:52 +01:00
|
|
|
unloadcurrentslide(void)
|
2018-07-08 14:54:37 +02:00
|
|
|
{
|
2020-12-20 15:25:52 +01:00
|
|
|
if (currentslidep == NULL)
|
|
|
|
return;
|
2020-12-20 11:44:12 +01:00
|
|
|
|
2020-12-20 15:25:52 +01:00
|
|
|
if (munmap(currentslidep, currentslidelen) < 0)
|
|
|
|
err(1, "munmap: %s", slidefiles[currentslide]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(int sig)
|
|
|
|
{
|
|
|
|
unloadcurrentslide();
|
2018-07-08 14:54:37 +02:00
|
|
|
|
|
|
|
endwin(); /* restore terminal */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-12-20 11:44:12 +01:00
|
|
|
void
|
2020-12-20 15:25:52 +01:00
|
|
|
loadcurrentslide(char **argv, int slide)
|
2020-12-20 11:44:12 +01:00
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
int fd;
|
|
|
|
|
2020-12-20 15:25:52 +01:00
|
|
|
unloadcurrentslide();
|
2020-12-20 11:44:12 +01:00
|
|
|
|
2020-12-20 15:25:52 +01:00
|
|
|
fd = open(slidefiles[slide], O_RDONLY, 0);
|
2020-12-20 11:44:12 +01:00
|
|
|
if (fd < 0)
|
2020-12-20 15:25:52 +01:00
|
|
|
err(1, "open: %s", slidefiles[slide]);
|
2020-12-20 11:44:12 +01:00
|
|
|
if (fstat(fd, &statbuf) < 0)
|
2020-12-20 15:25:52 +01:00
|
|
|
err(1, "fstat: %s", slidefiles[slide]);
|
|
|
|
currentslidep = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (currentslidep == MAP_FAILED)
|
2020-12-20 11:44:12 +01:00
|
|
|
err(1, "mmap");
|
2020-12-20 15:25:52 +01:00
|
|
|
currentslidelen = statbuf.st_size;
|
2020-12-20 11:44:12 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2020-12-20 14:57:45 +01:00
|
|
|
void
|
|
|
|
reloadcurrentslide(int sig)
|
|
|
|
{
|
2020-12-20 15:25:52 +01:00
|
|
|
loadcurrentslide(slidefiles, currentslide);
|
2020-12-20 14:57:45 +01:00
|
|
|
|
|
|
|
if (sig == SIGHUP) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
2020-12-20 15:25:52 +01:00
|
|
|
printw("%s", currentslidep);
|
2020-12-20 14:57:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 14:54:37 +02:00
|
|
|
void
|
|
|
|
setsignal()
|
|
|
|
{
|
2020-12-20 14:57:45 +01:00
|
|
|
signal(SIGHUP, reloadcurrentslide);
|
|
|
|
|
2018-07-08 14:54:37 +02:00
|
|
|
signal(SIGINT, cleanup);
|
|
|
|
signal(SIGQUIT, cleanup);
|
|
|
|
signal(SIGTERM, cleanup);
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:54:58 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2020-12-20 14:57:45 +01:00
|
|
|
int c;
|
2015-08-27 23:54:58 +02:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
errx(1, "usage: %s file ...", argv[0]);
|
2020-12-20 14:57:45 +01:00
|
|
|
slidefiles = ++argv;
|
|
|
|
nslides = --argc;
|
2015-08-27 23:54:58 +02:00
|
|
|
|
2018-07-08 14:54:37 +02:00
|
|
|
setsignal();
|
2015-08-27 23:54:58 +02:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2020-12-20 14:57:45 +01:00
|
|
|
/* start */
|
|
|
|
currentslide = 0;
|
2020-12-20 15:25:52 +01:00
|
|
|
currentslidep = NULL;
|
|
|
|
currentslidelen = 0;
|
2015-08-27 23:54:58 +02:00
|
|
|
|
|
|
|
/* init curses */
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
nonl();
|
|
|
|
intrflush(stdscr, FALSE);
|
|
|
|
keypad(stdscr, TRUE);
|
|
|
|
curs_set(FALSE); /* hide cursor */
|
|
|
|
|
|
|
|
show:
|
|
|
|
/* display slide */
|
2020-12-20 15:25:52 +01:00
|
|
|
loadcurrentslide(slidefiles, currentslide);
|
2015-08-27 23:54:58 +02:00
|
|
|
clear();
|
2020-12-20 11:44:12 +01:00
|
|
|
refresh();
|
2020-12-20 15:25:52 +01:00
|
|
|
printw("%s", currentslidep);
|
2020-12-20 14:57:45 +01:00
|
|
|
|
2015-08-27 23:54:58 +02:00
|
|
|
again:
|
|
|
|
c = getch();
|
|
|
|
switch (c) {
|
2017-03-04 16:49:33 +01:00
|
|
|
/* powerpoint remote presenter shortcuts */
|
|
|
|
case 27:
|
|
|
|
case KEY_F(5):
|
|
|
|
/* end presentation */
|
2015-08-27 23:54:58 +02:00
|
|
|
case 'q':
|
|
|
|
break;
|
|
|
|
/* next */
|
|
|
|
case 'l':
|
|
|
|
case 'j':
|
|
|
|
case KEY_RIGHT:
|
|
|
|
case KEY_DOWN:
|
|
|
|
case KEY_NPAGE:
|
2020-12-20 14:57:45 +01:00
|
|
|
if (currentslide < nslides - 1) {
|
|
|
|
currentslide++;
|
2015-08-27 23:54:58 +02:00
|
|
|
goto show;
|
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
/* prev */
|
|
|
|
case 'h':
|
|
|
|
case 'k':
|
|
|
|
case KEY_LEFT:
|
|
|
|
case KEY_UP:
|
|
|
|
case KEY_PPAGE:
|
2020-12-20 14:57:45 +01:00
|
|
|
if (currentslide > 0) {
|
|
|
|
currentslide--;
|
2015-08-27 23:54:58 +02:00
|
|
|
goto show;
|
|
|
|
}
|
|
|
|
goto again;
|
2017-03-04 16:49:33 +01:00
|
|
|
/* shortcut from powerpoint. Needed for remote presenters. */
|
|
|
|
case '.':
|
2015-08-27 23:54:58 +02:00
|
|
|
/* first */
|
|
|
|
case 'u':
|
|
|
|
case KEY_BEG:
|
2017-02-26 17:21:51 +01:00
|
|
|
case KEY_HOME:
|
2020-12-20 14:57:45 +01:00
|
|
|
currentslide = 0;
|
2015-08-27 23:54:58 +02:00
|
|
|
goto show;
|
|
|
|
/* last */
|
|
|
|
case 'i':
|
|
|
|
case KEY_END:
|
2020-12-20 14:57:45 +01:00
|
|
|
currentslide = nslides - 1;
|
2015-08-27 23:54:58 +02:00
|
|
|
goto show;
|
2020-12-20 11:44:12 +01:00
|
|
|
/* reload */
|
|
|
|
case 'r':
|
2020-12-20 14:57:45 +01:00
|
|
|
reloadcurrentslide(0);
|
2020-12-20 11:44:12 +01:00
|
|
|
goto show;
|
2020-12-20 15:42:42 +01:00
|
|
|
case KEY_RESIZE: /* resize / SIGWINCH */
|
|
|
|
goto show;
|
2015-08-27 23:54:58 +02:00
|
|
|
default:
|
2017-03-04 16:49:33 +01:00
|
|
|
/* printf("key pressed = '%d'\n", c); */
|
2015-08-27 23:54:58 +02:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap mem */
|
2020-12-20 14:41:04 +01:00
|
|
|
cleanup(0);
|
2015-08-27 23:54:58 +02:00
|
|
|
|
2020-12-20 14:41:04 +01:00
|
|
|
return 0;
|
2015-08-27 23:54:58 +02:00
|
|
|
}
|
2020-12-20 14:57:45 +01:00
|
|
|
|