commit a08e917dbb2310fbc5852e675992b6fd5499cb86
parent 4efa0d0f0303be076d013c61a83b2d769778f37d
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 17 Feb 2023 22:58:37 +0100
Use SIGINT to stop player and update player status less frequently
Diffstat:
4 files changed, 50 insertions(+), 19 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -4,12 +4,24 @@
#include "player.h"
#include "tui.h"
+#include <curses.h>
#include <locale.h>
+static void stop(int sig);
static void init(void);
static void cleanup(int code, void *arg);
void
+stop(int sig)
+{
+ if (player.loaded) {
+ player_stop();
+ } else {
+ exit(0);
+ }
+}
+
+void
init(void)
{
setlocale(LC_ALL, "");
@@ -26,7 +38,7 @@ init(void)
dbus_init();
on_exit(cleanup, NULL);
- signal(SIGINT, exit);
+ signal(SIGINT, stop);
signal(SIGTERM, exit);
signal(SIGKILL, exit);
}
diff --git a/src/player_mplay.c b/src/player_mplay.c
@@ -27,6 +27,7 @@ struct mplay_player {
FILE *stdin;
FILE *stdout;
pid_t pid;
+ uint64_t update_ms;
};
struct player player;
@@ -182,25 +183,28 @@ player_update(void)
if (!player.loaded) return;
- fprintf(mplay.stdin, "status\n");
- line = mplay_readline();
- if (!line || strncmp(line, "+STATUS:", 8)) {
- MPLAY_STATUS(line);
- return;
- }
+ if (current_ms() >= mplay.update_ms + 330) {
+ mplay.update_ms = current_ms();
+ fprintf(mplay.stdin, "status\n");
+ line = mplay_readline();
+ if (!line || strncmp(line, "+STATUS:", 8)) {
+ MPLAY_STATUS(line);
+ return;
+ }
- tok = line;
- while ((tok = strchr(tok, ' '))) {
- if (!strncmp(tok + 1, "vol:", 4)) {
- player.volume = atoi(tok + 5);
- } else if (!strncmp(tok + 1, "pause:", 6)) {
- player.state = atoi(tok + 7)
- ? PLAYER_STATE_PAUSED : PLAYER_STATE_PLAYING;
- } else if (!strncmp(tok + 1, "pos:", 4)) {
- player.time_pos = atoi(tok + 5);
- player.time_end = MAX(player.time_pos, player.time_end);
+ tok = line;
+ while ((tok = strchr(tok, ' '))) {
+ if (!strncmp(tok + 1, "vol:", 4)) {
+ player.volume = atoi(tok + 5);
+ } else if (!strncmp(tok + 1, "pause:", 6)) {
+ player.state = atoi(tok + 7)
+ ? PLAYER_STATE_PAUSED : PLAYER_STATE_PLAYING;
+ } else if (!strncmp(tok + 1, "pos:", 4)) {
+ player.time_pos = atoi(tok + 5);
+ player.time_end = MAX(player.time_pos, player.time_end);
+ }
+ tok += 1;
}
- tok += 1;
}
}
diff --git a/src/util.c b/src/util.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <bits/time.h>
#define _XOPEN_SOURCE 600
#define _GNU_SOURCE
@@ -6,6 +6,7 @@
#include "util.h"
#include "log.h"
+#include <time.h>
#include <execinfo.h>
#include <errno.h>
#include <stdarg.h>
@@ -188,3 +189,14 @@ timestr(unsigned int secs)
return buf;
}
+uint64_t
+current_ms(void)
+{
+ struct timespec tp;
+ uint64_t ms;
+
+ clock_gettime(CLOCK_REALTIME, &tp);
+ ms = tp.tv_sec * 1000UL + tp.tv_nsec / 1000000UL;
+
+ return ms;
+}
diff --git a/src/util.h b/src/util.h
@@ -1,6 +1,7 @@
#pragma once
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -34,3 +35,5 @@ char *appendstrf(char *alloc, const char *fmtstr, ...);
char *sanitized(const char *instr);
const char *timestr(unsigned int seconds);
+
+uint64_t current_ms(void);