Add video and pdf support

This commit is contained in:
Nikita Ivanov 2022-05-26 04:48:27 +05:00
parent 04fc61c901
commit 770d92769e
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
9 changed files with 93 additions and 29 deletions

View File

@ -22,7 +22,7 @@ install: ctpv ctpvclear
install $^ $(BINPREFIX)
uninstall:
$(RM) $(BINPREFIX)/ctpv
$(RM) $(BINPREFIX)/ctpv $(BINPREFIX)/ctpvclear
clean:
$(RM) ctpv $(OBJ) $(DEP) $(GEN)

View File

@ -1,7 +1,3 @@
fifo="$(get_fifo "$id")"
setup_fifo "" 1
[ -e "$fifo" ] || exit 1
fifo_open "$fifo" && {
printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo"
}

35
ctpv.c
View File

@ -4,6 +4,7 @@
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include "error.h"
#include "server.h"
@ -24,6 +25,7 @@ static struct {
MODE_END,
MODE_LIST,
MODE_MIME,
MODE_NEWER,
} mode;
char *server_id_s;
} ctpv = { .mode = MODE_PREVIEW };
@ -181,18 +183,43 @@ static int mime(int argc, char *argv[])
if (argc > 1)
printf("%s: ", f);
printf(".%s ", get_ext(f));
puts(mimetype);
}
return OK;
}
static int newer(int argc, char *argv[])
{
char *f1, *f2;
GET_PARG(f1, 0);
GET_PARG(f2, 1);
if (!f1 || !f2) {
print_error("2 file should be given");
return ERR;
}
struct stat stat1, stat2;
ERRCHK_RET(stat(f1, &stat1) == -1, FUNCFAILED("stat"), ERRNOS);
ERRCHK_RET(stat(f2, &stat2) == -1, FUNCFAILED("stat"), ERRNOS);
int sec_d = stat1.st_mtim.tv_sec - stat2.st_mtim.tv_sec;
if (sec_d < 0)
return ERR;
else if (sec_d == 0 && stat1.st_mtim.tv_nsec <= stat2.st_mtim.tv_nsec)
return ERR;
return OK;
}
int main(int argc, char *argv[])
{
program = argc > 0 ? argv[0] : "ctpv";
int c;
while ((c = getopt(argc, argv, "s:ce:lm")) != -1) {
while ((c = getopt(argc, argv, "s:ce:lmn")) != -1) {
switch (c) {
case 's':
ctpv.mode = MODE_SERVER;
@ -211,6 +238,9 @@ int main(int argc, char *argv[])
case 'm':
ctpv.mode = MODE_MIME;
break;
case 'n':
ctpv.mode = MODE_NEWER;
break;
default:
return EXIT_FAILURE;
}
@ -239,6 +269,9 @@ int main(int argc, char *argv[])
case MODE_MIME:
ret = mime(argc, argv);
break;
case MODE_NEWER:
ret = newer(argc, argv);
break;
default:
PRINTINTERR("unknowm mode: %d", ctpv.mode);
ret = ERR;

8
end.sh
View File

@ -1,6 +1,4 @@
fifo="$(get_fifo "$1")"
setup_fifo "$1" 1
[ -e "$fifo" ] || exit 1
# sending zero byte tells listener to stop
fifo_open "$fifo" && printf '\0' > "$fifo"
# tell ctpv server to exit
printf '\0' > "$fifo"

View File

@ -1,13 +1,40 @@
get_fifo() {
printf '/tmp/ctpvfifo.%s' "$1"
fifo_open() {
# https://unix.stackexchange.com/a/522940/183147
dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \
>/dev/null 2>/dev/null
}
setup_fifo() {
exit_code="${2:-127}"
fifo="$(printf '/tmp/ctpvfifo.%s' "${1:-$id}")"
[ -e "$fifo" ] || exit "$exit_code"
fifo_open "$fifo" || exit "$exit_code"
}
exists() {
command -v "$1" > /dev/null
}
fifo_open() {
# https://unix.stackexchange.com/a/522940/183147
dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \
>/dev/null 2>/dev/null
check_exist() {
[ $? = 127 ] && exit 127
}
cache() {
cache_d="${XDG_CACHE_HOME:-$HOME/.cache}/ctpv"
mkdir -p "$cache_d"
cache_f="$cache_d/$(printf '%s' "$f" | md5sum - | cut -d' ' -f1)"
test -e "$cache_f" || return
ctpv -n "$cache_f" "$f"
}
show_image() {
path="$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g')"
printf '{ "action": "add", "identifier": "preview", "x": %d, "y": %d, "width": %d, "height": %d, "scaler": "contain", "scaling_position_x": 0.5, "scaling_position_y": 0.5, "path": "%s"}\n' "$x" "$y" "$w" "$h" "$path" > "$fifo"
}
convert_and_show_image() {
setup_fifo
cache || "$@" || check_exist
show_image "$cache_f"
exit 1
}

View File

@ -1,13 +1,6 @@
fifo="$(get_fifo "$id")"
# exit 127 on error so ctpv fallbacks to another preview
setup_fifo
# tell ctpv to fallback to another preview
[ -e "$fifo" ] || exit 127
show_image "$f"
path="$(printf '%s' "$f" | sed 's/\\/\\\\/g; s/"/\\"/g')"
fifo_open "$fifo" && {
printf '{ "action": "add", "identifier": "preview", "x": %d, "y": %d, "width": %d, "height": %d, "scaler": "contain", "scaling_position_x": 1, "scaling_position_y": 1, "path": "%s"}\n' "$x" "$y" "$w" "$h" "$path" > "$fifo"
}
# tell lf to disable preview caching
exit 1

10
prev/pdf.sh Normal file
View File

@ -0,0 +1,10 @@
pdf() {
pdftoppm -f 1 -l 1 \
-scale-to-x 1920 \
-scale-to-y -1 \
-singlefile \
-jpeg -tiffcompression jpeg \
-- "$f" "$cache_f" && mv -- "$cache_f.jpg" "$cache_f"
}
convert_and_show_image pdf

5
prev/video.sh Normal file
View File

@ -0,0 +1,5 @@
video() {
ffmpegthumbnailer -i "$f" -o "$cache_f" -s 0 -t 50% 2>/dev/null
}
convert_and_show_image video

View File

@ -18,4 +18,6 @@ Preview previews[] = {
PR("md", NULL, NULL, markdown),
PR(NULL, "application", "json", json),
PR(NULL, "image", NULL, image),
PR(NULL, "video", NULL, video),
PR(NULL, "application", "pdf", pdf),
};