Set $fifo variable when ctpv is started

This commit is contained in:
Nikita Ivanov 2022-05-31 22:53:07 +05:00
parent ea3a852acc
commit 6717a5f62e
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
6 changed files with 39 additions and 6 deletions

View File

@ -1,3 +1,3 @@
setup_fifo "$1" 1 setup_fifo 1
printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo" printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo"

2
end.sh
View File

@ -1,4 +1,4 @@
setup_fifo "$1" 1 setup_fifo 1
# tell ctpv server to exit # tell ctpv server to exit
printf '\0' > "$fifo" printf '\0' > "$fifo"

View File

@ -1,3 +1,7 @@
echo_err() {
echo "$@" >&2
}
fifo_open() { fifo_open() {
# https://unix.stackexchange.com/a/522940/183147 # https://unix.stackexchange.com/a/522940/183147
dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \ dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \
@ -5,8 +9,12 @@ fifo_open() {
} }
setup_fifo() { setup_fifo() {
fifo="$(printf '/tmp/ctpvfifo.%s' "${1:-$id}")" if [ -z "$fifo" ]; then
exit_code="${2:-127}" echo_err '$fifo is empty!'
exit 1
fi
exit_code="${1:-127}"
[ -e "$fifo" ] || exit "$exit_code" [ -e "$fifo" ] || exit "$exit_code"
fifo_open "$fifo" || exit "$exit_code" fifo_open "$fifo" || exit "$exit_code"
} }

View File

@ -4,6 +4,7 @@
#include "utils.h" #include "utils.h"
#include "error.h" #include "error.h"
#include "shell.h" #include "shell.h"
#include "server.h"
#include "preview.h" #include "preview.h"
#define FAILED_PREVIEW_EC NOTEXIST_EC #define FAILED_PREVIEW_EC NOTEXIST_EC
@ -157,6 +158,9 @@ static int run(Preview *p, int *exitcode)
int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa) int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa)
{ {
if (pa->id || (pa->id = getenv("id")))
ERRCHK_RET_OK(server_set_fifo_var(pa->id));
SET_PENV("ctpv", pa->ctpv); SET_PENV("ctpv", pa->ctpv);
SET_PENV("f", pa->f); SET_PENV("f", pa->f);
SET_PENV("w", pa->w); SET_PENV("w", pa->w);

View File

@ -11,6 +11,8 @@
#include "shell.h" #include "shell.h"
#include "gen/server.h" #include "gen/server.h"
#define FIFO_FILENAME_SIZE 256
static pid_t ueberzug_pid; static pid_t ueberzug_pid;
static void kill_ueberzug(void) static void kill_ueberzug(void)
@ -99,6 +101,11 @@ static int check_ueberzug(int *exitcode)
return spawn(args, NULL, exitcode, NULL, NULL); return spawn(args, NULL, exitcode, NULL, NULL);
} }
static void get_fifo_name(char *buf, size_t len, const char *id_s)
{
snprintf(buf, len-1, "/tmp/ctpvfifo.%s", id_s);
}
int server_listen(char const *id_s) int server_listen(char const *id_s)
{ {
int ret = OK; int ret = OK;
@ -111,8 +118,8 @@ int server_listen(char const *id_s)
goto exit; goto exit;
} }
char fifo[256]; char fifo[FIFO_FILENAME_SIZE];
snprintf(fifo, LEN(fifo)-1, "/tmp/ctpvfifo.%s", id_s); get_fifo_name(fifo, LEN(fifo), id_s);
ERRCHK_GOTO(mkfifo(fifo, 0600) == -1 && errno != EEXIST, ret, exit, ERRCHK_GOTO(mkfifo(fifo, 0600) == -1 && errno != EEXIST, ret, exit,
FUNCFAILED("mkfifo"), ERRNOS); FUNCFAILED("mkfifo"), ERRNOS);
@ -148,12 +155,25 @@ cleanup:
return ret; return ret;
} }
int server_set_fifo_var(const char *id_s)
{
char fifo[FIFO_FILENAME_SIZE];
get_fifo_name(fifo, LEN(fifo), id_s);
ERRCHK_RET(setenv("fifo", fifo, 1) != 0, FUNCFAILED("setenv"), ERRNOS);
return OK;
}
int server_clear(const char *id_s) int server_clear(const char *id_s)
{ {
ERRCHK_RET_OK(server_set_fifo_var(id_s));
return run_script(scr_clear_sh, LEN(scr_clear_sh)-1, (char *)id_s); return run_script(scr_clear_sh, LEN(scr_clear_sh)-1, (char *)id_s);
} }
int server_end(const char *id_s) int server_end(const char *id_s)
{ {
ERRCHK_RET_OK(server_set_fifo_var(id_s));
return run_script(scr_end_sh, LEN(scr_end_sh)-1, (char *)id_s); return run_script(scr_end_sh, LEN(scr_end_sh)-1, (char *)id_s);
} }

View File

@ -2,6 +2,7 @@
#define SERVER_H #define SERVER_H
int server_listen(char const *id_s); int server_listen(char const *id_s);
int server_set_fifo_var(const char *id_s);
int server_clear(const char *id_s); int server_clear(const char *id_s);
int server_end(const char *id_s); int server_end(const char *id_s);