allow to reload file

This commit is contained in:
Markus Teich 2017-09-02 19:30:11 +02:00
parent ec431df440
commit 16e4843d05
3 changed files with 49 additions and 15 deletions

View File

@ -46,6 +46,7 @@ static Shortcut shortcuts[] = {
{ XK_Prior, advance, {.i = -1} }, { XK_Prior, advance, {.i = -1} },
{ XK_n, advance, {.i = +1} }, { XK_n, advance, {.i = +1} },
{ XK_p, advance, {.i = -1} }, { XK_p, advance, {.i = -1} },
{ XK_r, reload, {0} },
}; };
static Filter filters[] = { static Filter filters[] = {

2
sent.1
View File

@ -35,6 +35,8 @@ Go to previous slide, if existent.
.Bl -tag -width Ds .Bl -tag -width Ds
.It Sy Escape | q .It Sy Escape | q
Quit. Quit.
.It Sy r
Reload the slides. Only works on file input.
.It Sy Right | Return | Space | l | j | Down | Next | n .It Sy Right | Return | Space | l | j | Down | Next | n
Go to next slide, if existent. Go to next slide, if existent.
.It Sy Left | Backspace | h | k | Up | Prior | p .It Sy Left | Backspace | h | k | Up | Prior | p

43
sent.c
View File

@ -93,7 +93,8 @@ static void ffscale(Image *img);
static void ffdraw(Image *img); static void ffdraw(Image *img);
static void getfontsize(Slide *s, unsigned int *width, unsigned int *height); static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
static void cleanup(); static void cleanup(int slidesonly);
static void reload(const Arg *arg);
static void load(FILE *fp); static void load(FILE *fp);
static void advance(const Arg *arg); static void advance(const Arg *arg);
static void quit(const Arg *arg); static void quit(const Arg *arg);
@ -115,6 +116,7 @@ static void configure(XEvent *);
#include "config.h" #include "config.h"
/* Globals */ /* Globals */
static const char *fname = NULL;
static Slide *slides = NULL; static Slide *slides = NULL;
static int idx = 0; static int idx = 0;
static int slidecount = 0; static int slidecount = 0;
@ -346,10 +348,11 @@ getfontsize(Slide *s, unsigned int *width, unsigned int *height)
} }
void void
cleanup() cleanup(int slidesonly)
{ {
unsigned int i, j; unsigned int i, j;
if (!slidesonly) {
for (i = 0; i < NUMFONTSCALES; i++) for (i = 0; i < NUMFONTSCALES; i++)
drw_fontset_free(fonts[i]); drw_fontset_free(fonts[i]);
free(sc); free(sc);
@ -358,6 +361,8 @@ cleanup()
XDestroyWindow(xw.dpy, xw.win); XDestroyWindow(xw.dpy, xw.win);
XSync(xw.dpy, False); XSync(xw.dpy, False);
XCloseDisplay(xw.dpy); XCloseDisplay(xw.dpy);
}
if (slides) { if (slides) {
for (i = 0; i < slidecount; i++) { for (i = 0; i < slidecount; i++) {
for (j = 0; j < slides[i].linecount; j++) for (j = 0; j < slides[i].linecount; j++)
@ -366,10 +371,37 @@ cleanup()
if (slides[i].img) if (slides[i].img)
fffree(slides[i].img); fffree(slides[i].img);
} }
if (!slidesonly) {
free(slides); free(slides);
slides = NULL; slides = NULL;
} }
} }
}
void
reload(const Arg *arg)
{
FILE *fp = NULL;
unsigned int i;
if (!fname) {
fprintf(stderr, "sent: Cannot reload from stdin. Use a file!\n");
return;
}
cleanup(1);
slidecount = 0;
if (!(fp = fopen(fname, "r")))
die("sent: Unable to open '%s' for reading:", fname);
load(fp);
fclose(fp);
LIMIT(idx, 0, slidecount-1);
for (i = 0; i < slidecount; i++)
ffload(&slides[i]);
xdraw();
}
void void
load(FILE *fp) load(FILE *fp)
@ -658,9 +690,8 @@ main(int argc, char *argv[])
if (!argv[0] || !strcmp(argv[0], "-")) if (!argv[0] || !strcmp(argv[0], "-"))
fp = stdin; fp = stdin;
else if (!(fp = fopen(argv[0], "r"))) else if (!(fp = fopen(fname = argv[0], "r")))
die("sent: Unable to open '%s' for reading:", argv[0]); die("sent: Unable to open '%s' for reading:", fname);
load(fp); load(fp);
fclose(fp); fclose(fp);
@ -670,6 +701,6 @@ main(int argc, char *argv[])
xinit(); xinit();
run(); run();
cleanup(); cleanup(0);
return 0; return 0;
} }