Massive refactoring for features (SIGHUP, variable names).
* Now the global variable names are easier to use. * Only the global variables are used. * A new loadcurrentslide() function is used for SIGHUP. * This allows slideshows to be programmed using SIGHUP. * Thanks Bob for the suggestion!
This commit is contained in:
parent
a4f575be61
commit
5672cc0e59
76
catpoint.c
76
catpoint.c
@ -13,16 +13,16 @@
|
|||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
char **p; /* the slides */
|
char **slides, **slidefiles; /* the slides */
|
||||||
int n; /* the number of slides */
|
int nslides, currentslide;
|
||||||
|
|
||||||
void
|
void
|
||||||
cleanup(int sig)
|
cleanup(int sig)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < nslides; i++)
|
||||||
munmap(p[i], 0x1000);
|
munmap(slides[i], 0x1000);
|
||||||
|
|
||||||
endwin(); /* restore terminal */
|
endwin(); /* restore terminal */
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -34,26 +34,39 @@ reload(char **argv, int i)
|
|||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (p[i] != NULL) {
|
if (slides[i] != NULL) {
|
||||||
if (munmap(p[i], 0x1000) < 0)
|
if (munmap(slides[i], 0x1000) < 0)
|
||||||
err(1, "munmap: %s", argv[i]);
|
err(1, "munmap: %s", slidefiles[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(argv[i], O_RDONLY, 0);
|
fd = open(slidefiles[i], O_RDONLY, 0);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
err(1, "open: %s", argv[i]);
|
err(1, "open: %s", slidefiles[i]);
|
||||||
if (fstat(fd, &statbuf) < 0)
|
if (fstat(fd, &statbuf) < 0)
|
||||||
err(1, "fstat: %s", argv[i]);
|
err(1, "fstat: %s", slidefiles[i]);
|
||||||
p[i] = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
slides[i] = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
if (p[i] == MAP_FAILED)
|
if (slides[i] == MAP_FAILED)
|
||||||
err(1, "mmap");
|
err(1, "mmap");
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
reloadcurrentslide(int sig)
|
||||||
|
{
|
||||||
|
reload(slidefiles, currentslide);
|
||||||
|
|
||||||
|
if (sig == SIGHUP) {
|
||||||
|
clear();
|
||||||
|
refresh();
|
||||||
|
printw("%s", slides[currentslide]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
setsignal()
|
setsignal()
|
||||||
{
|
{
|
||||||
signal(SIGHUP, cleanup);
|
signal(SIGHUP, reloadcurrentslide);
|
||||||
|
|
||||||
signal(SIGINT, cleanup);
|
signal(SIGINT, cleanup);
|
||||||
signal(SIGQUIT, cleanup);
|
signal(SIGQUIT, cleanup);
|
||||||
signal(SIGABRT, cleanup);
|
signal(SIGABRT, cleanup);
|
||||||
@ -64,22 +77,25 @@ setsignal()
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, i;
|
int c;
|
||||||
|
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
errx(1, "usage: %s file ...", argv[0]);
|
errx(1, "usage: %s file ...", argv[0]);
|
||||||
argv++;
|
slidefiles = ++argv;
|
||||||
argc--;
|
nslides = --argc;
|
||||||
|
|
||||||
setsignal();
|
setsignal();
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
|
||||||
p = calloc(argc, sizeof(char *));
|
slides = calloc(nslides, sizeof(char *));
|
||||||
n = argc;
|
|
||||||
|
|
||||||
/* map files to mem */
|
/* map files to mem */
|
||||||
for (i = 0; argv[i] != NULL; i++)
|
for (currentslide = 0; slidefiles[currentslide] != NULL;
|
||||||
reload(argv, i);
|
currentslide++) {
|
||||||
|
reload(slidefiles, currentslide);
|
||||||
|
}
|
||||||
|
/* start */
|
||||||
|
currentslide = 0;
|
||||||
|
|
||||||
/* init curses */
|
/* init curses */
|
||||||
initscr();
|
initscr();
|
||||||
@ -90,13 +106,12 @@ main(int argc, char *argv[])
|
|||||||
keypad(stdscr, TRUE);
|
keypad(stdscr, TRUE);
|
||||||
curs_set(FALSE); /* hide cursor */
|
curs_set(FALSE); /* hide cursor */
|
||||||
|
|
||||||
/* start */
|
|
||||||
i = 0;
|
|
||||||
show:
|
show:
|
||||||
/* display slide */
|
/* display slide */
|
||||||
clear();
|
clear();
|
||||||
refresh();
|
refresh();
|
||||||
printw("%s", p[i]);
|
printw("%s", slides[currentslide]);
|
||||||
|
|
||||||
again:
|
again:
|
||||||
c = getch();
|
c = getch();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
@ -112,8 +127,8 @@ again:
|
|||||||
case KEY_RIGHT:
|
case KEY_RIGHT:
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
case KEY_NPAGE:
|
case KEY_NPAGE:
|
||||||
if (i < argc - 1) {
|
if (currentslide < nslides - 1) {
|
||||||
i++;
|
currentslide++;
|
||||||
goto show;
|
goto show;
|
||||||
}
|
}
|
||||||
goto again;
|
goto again;
|
||||||
@ -123,8 +138,8 @@ again:
|
|||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
case KEY_PPAGE:
|
case KEY_PPAGE:
|
||||||
if (i > 0) {
|
if (currentslide > 0) {
|
||||||
i--;
|
currentslide--;
|
||||||
goto show;
|
goto show;
|
||||||
}
|
}
|
||||||
goto again;
|
goto again;
|
||||||
@ -134,16 +149,16 @@ again:
|
|||||||
case 'u':
|
case 'u':
|
||||||
case KEY_BEG:
|
case KEY_BEG:
|
||||||
case KEY_HOME:
|
case KEY_HOME:
|
||||||
i = 0;
|
currentslide = 0;
|
||||||
goto show;
|
goto show;
|
||||||
/* last */
|
/* last */
|
||||||
case 'i':
|
case 'i':
|
||||||
case KEY_END:
|
case KEY_END:
|
||||||
i = argc - 1;
|
currentslide = nslides - 1;
|
||||||
goto show;
|
goto show;
|
||||||
/* reload */
|
/* reload */
|
||||||
case 'r':
|
case 'r':
|
||||||
reload(argv, i);
|
reloadcurrentslide(0);
|
||||||
goto show;
|
goto show;
|
||||||
default:
|
default:
|
||||||
/* printf("key pressed = '%d'\n", c); */
|
/* printf("key pressed = '%d'\n", c); */
|
||||||
@ -155,3 +170,4 @@ again:
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user