2019-10-14 13:38:14 -06:00
|
|
|
#include <mpd/client.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2019-10-16 08:47:59 -06:00
|
|
|
#include <glib.h>
|
2019-10-14 13:38:14 -06:00
|
|
|
|
|
|
|
struct worker_meta {
|
|
|
|
long long update_interval;
|
|
|
|
bool stop;
|
2019-10-15 07:54:08 -06:00
|
|
|
int scroll_length;
|
2019-10-16 16:01:19 -06:00
|
|
|
int scroll_step;
|
2019-10-15 07:54:08 -06:00
|
|
|
int scroll_index;
|
|
|
|
char* label;
|
2019-10-14 13:38:14 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static long long
|
|
|
|
current_timestamp() {
|
|
|
|
struct timeval te;
|
|
|
|
gettimeofday(&te, NULL);
|
|
|
|
return te.tv_sec*1000LL + te.tv_usec/1000;
|
|
|
|
}
|
|
|
|
|
2019-10-15 07:54:08 -06:00
|
|
|
static void
|
2019-10-16 11:32:41 -06:00
|
|
|
scroll_text(const char *text_to_scroll, const size_t text_len, char *buffer,
|
2019-10-16 08:47:59 -06:00
|
|
|
int *index, const int max_length) {
|
|
|
|
if (*index - text_len > 0) *index %= text_len;
|
|
|
|
|
|
|
|
char* first_char = g_utf8_offset_to_pointer(text_to_scroll, *index);
|
|
|
|
const int overflow = text_len - *index - max_length;
|
|
|
|
if (overflow <= 0) {
|
|
|
|
g_utf8_strncpy(buffer, first_char, text_len-*index);
|
|
|
|
g_utf8_strncpy(g_utf8_offset_to_pointer(buffer, text_len-*index),
|
|
|
|
text_to_scroll, max_length-(text_len-*index));
|
|
|
|
} else {
|
|
|
|
g_utf8_strncpy(buffer, first_char, max_length);
|
|
|
|
}
|
2019-10-15 07:54:08 -06:00
|
|
|
}
|
|
|
|
|
2019-10-14 13:38:14 -06:00
|
|
|
static int
|
2019-10-16 15:52:32 -06:00
|
|
|
handle_error(struct mpd_connection *conn, bool renew)
|
2019-10-14 13:38:14 -06:00
|
|
|
{
|
2019-10-16 15:52:32 -06:00
|
|
|
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
|
|
|
|
if (!renew) {
|
|
|
|
fprintf(stderr, "%s\n", mpd_connection_get_error_message(conn));
|
|
|
|
}
|
|
|
|
if (!mpd_connection_clear_error(conn)) {
|
|
|
|
if (renew) {
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
conn = mpd_connection_new(NULL, 0, 0);
|
|
|
|
return handle_error(conn, false);
|
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2019-10-14 13:38:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_tag(struct mpd_song *song, char *tag, enum mpd_tag_type type)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
unsigned i = 0;
|
|
|
|
while ((value = mpd_song_get_tag(song, type, i++)) != NULL) {
|
|
|
|
strcpy(tag, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-10-15 07:54:08 -06:00
|
|
|
print_status(struct mpd_connection *conn, struct worker_meta *meta)
|
2019-10-14 13:38:14 -06:00
|
|
|
{
|
2019-10-16 11:32:41 -06:00
|
|
|
struct mpd_status *status = mpd_run_status(conn);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
|
|
|
if (status == NULL)
|
2019-10-16 15:52:32 -06:00
|
|
|
return handle_error(conn, false);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
|
|
|
const enum mpd_state state = mpd_status_get_state(status);
|
|
|
|
|
2019-10-16 11:47:42 -06:00
|
|
|
char *play_icon = NULL;
|
|
|
|
switch (state) {
|
|
|
|
case MPD_STATE_PLAY:
|
|
|
|
play_icon = "";
|
|
|
|
break;
|
|
|
|
case MPD_STATE_PAUSE:
|
|
|
|
play_icon = "";
|
|
|
|
break;
|
|
|
|
default:
|
2019-10-16 12:29:05 -06:00
|
|
|
meta->scroll_index = 0;
|
2019-10-16 11:47:42 -06:00
|
|
|
printf("\n");
|
|
|
|
mpd_status_free(status);
|
|
|
|
fflush(stdout);
|
2019-10-16 12:29:05 -06:00
|
|
|
return 1;
|
2019-10-15 07:54:08 -06:00
|
|
|
}
|
|
|
|
|
2019-10-14 13:38:14 -06:00
|
|
|
const bool repeat = mpd_status_get_repeat(status);
|
|
|
|
const bool random = mpd_status_get_random(status);
|
|
|
|
|
2019-10-16 11:47:42 -06:00
|
|
|
char state_icon[16];
|
|
|
|
strcpy(state_icon, "");
|
|
|
|
if (repeat)
|
|
|
|
strcat(state_icon, " ");
|
|
|
|
if (random)
|
|
|
|
strcat(state_icon, " ");
|
|
|
|
|
2019-10-14 13:38:14 -06:00
|
|
|
const unsigned queue_pos = mpd_status_get_song_pos(status);
|
|
|
|
const unsigned queue_length = mpd_status_get_queue_length(status);
|
|
|
|
const unsigned elapsed = mpd_status_get_elapsed_time(status);
|
|
|
|
const unsigned remaining = mpd_status_get_total_time(status) - elapsed;
|
|
|
|
const unsigned remaining_mins = remaining / 60;
|
|
|
|
const unsigned remaining_secs = remaining % 60;
|
|
|
|
|
|
|
|
mpd_status_free(status);
|
2019-10-16 14:18:24 -06:00
|
|
|
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
|
2019-10-16 15:52:32 -06:00
|
|
|
return handle_error(conn, false);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
|
|
|
char artist[256];
|
|
|
|
char album[256];
|
|
|
|
char title[256];
|
|
|
|
|
2019-10-16 11:32:41 -06:00
|
|
|
struct mpd_song *song = mpd_run_current_song(conn);
|
|
|
|
get_tag(song, artist, MPD_TAG_ARTIST);
|
|
|
|
get_tag(song, album, MPD_TAG_ALBUM);
|
|
|
|
get_tag(song, title, MPD_TAG_TITLE);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
2019-10-16 11:32:41 -06:00
|
|
|
mpd_song_free(song);
|
2019-10-16 14:18:24 -06:00
|
|
|
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
|
2019-10-16 15:52:32 -06:00
|
|
|
return handle_error(conn, false);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
2019-10-15 07:54:08 -06:00
|
|
|
// the full song label. would be cool if this was customizable at runtime
|
|
|
|
char full_label[strlen(artist) + strlen(title) + 4];
|
2019-10-16 16:01:19 -06:00
|
|
|
sprintf(full_label, "%s - %s", title, artist);
|
2019-10-15 07:54:08 -06:00
|
|
|
|
|
|
|
// compare current label against previous update's label
|
|
|
|
if (strcmp(meta->label, full_label) != 0) {
|
|
|
|
// The song changed, reset scroll and update meta
|
|
|
|
meta->scroll_index = 0;
|
|
|
|
strcpy(meta->label, full_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The label we'll actually print
|
2019-10-16 08:47:59 -06:00
|
|
|
// Allocate scroll_length * 4 chars(bytes) since each UTF-8 character may
|
|
|
|
// consume up to 4 bytes.
|
|
|
|
char label[(meta->scroll_length*4)+1];
|
|
|
|
strncpy(label, "", sizeof(label));
|
|
|
|
|
|
|
|
if (g_utf8_strlen(full_label, -1) > meta->scroll_length) {
|
2019-10-15 07:54:08 -06:00
|
|
|
// If length of full label > meta->scroll_length, we'll scroll it
|
|
|
|
|
|
|
|
// Pad the text with a separator
|
2019-10-16 08:47:59 -06:00
|
|
|
char padded_text[strlen(full_label) + 3 + 1];
|
|
|
|
padded_text[sizeof(padded_text)-1] = '\0';
|
|
|
|
|
2019-10-15 07:54:08 -06:00
|
|
|
sprintf(padded_text, "%s | ", full_label);
|
|
|
|
|
2019-10-16 08:47:59 -06:00
|
|
|
scroll_text(padded_text, g_utf8_strlen(padded_text, -1), label,
|
2019-10-15 07:54:08 -06:00
|
|
|
&meta->scroll_index, meta->scroll_length);
|
|
|
|
} else {
|
2019-10-16 16:01:19 -06:00
|
|
|
// Else we'll just use the full label
|
|
|
|
strcpy(label, full_label);
|
2019-10-15 07:54:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s%s %s (-%u:%02u) [%u/%u]\n",
|
|
|
|
play_icon, state_icon, label,
|
|
|
|
remaining_mins, remaining_secs,
|
|
|
|
queue_pos+1, queue_length);
|
2019-10-14 13:38:14 -06:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
status_loop(void* worker_meta)
|
|
|
|
{
|
|
|
|
struct worker_meta *meta = (struct worker_meta*)worker_meta;
|
|
|
|
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
|
|
|
|
2019-10-16 15:52:32 -06:00
|
|
|
while (!meta->stop) {
|
2019-10-14 13:38:14 -06:00
|
|
|
long long start = current_timestamp();
|
2019-10-15 07:54:08 -06:00
|
|
|
|
2019-10-16 15:52:32 -06:00
|
|
|
if (handle_error(conn, true) == 0 && print_status(conn, meta) == 0) {
|
2019-10-16 16:01:19 -06:00
|
|
|
meta->scroll_index += meta->scroll_step;
|
2019-10-16 12:29:05 -06:00
|
|
|
}
|
2019-10-15 07:54:08 -06:00
|
|
|
|
2019-10-16 15:52:32 -06:00
|
|
|
long long elapsed = current_timestamp() - start;
|
|
|
|
if (meta->update_interval - elapsed >= 0) {
|
|
|
|
usleep((meta->update_interval - elapsed) * 1000);
|
|
|
|
}
|
2019-10-14 13:38:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:50:56 -06:00
|
|
|
enum click_command {
|
|
|
|
TOGGLE_PAUSE, NEXT, PREVIOUS, SEEK_FWD, SEEK_BCKWD
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(struct worker_meta *meta, enum click_command command) {
|
|
|
|
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
2019-10-16 08:50:56 -06:00
|
|
|
switch (command) {
|
|
|
|
case TOGGLE_PAUSE:
|
|
|
|
mpd_run_toggle_pause(conn);
|
|
|
|
break;
|
|
|
|
case NEXT:
|
|
|
|
mpd_run_next(conn);
|
|
|
|
break;
|
|
|
|
case PREVIOUS:
|
|
|
|
mpd_run_previous(conn);
|
|
|
|
break;
|
|
|
|
case SEEK_FWD:
|
|
|
|
mpd_run_seek_current(conn, 3, true);
|
|
|
|
break;
|
|
|
|
case SEEK_BCKWD:
|
|
|
|
mpd_run_seek_current(conn, -3, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
print_status(conn, meta);
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_connection_free(conn);
|
2019-10-16 08:50:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-15 07:54:08 -06:00
|
|
|
int main(void) {
|
2019-10-16 16:02:40 -06:00
|
|
|
struct worker_meta meta = {950, false, 25, 3, 0, malloc(1024)};
|
2019-10-14 13:38:14 -06:00
|
|
|
|
2019-10-16 15:52:32 -06:00
|
|
|
struct sigaction new_actn, old_actn;
|
|
|
|
new_actn.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset(&new_actn.sa_mask);
|
|
|
|
new_actn.sa_flags = 0;
|
|
|
|
sigaction(SIGPIPE, &new_actn, &old_actn);
|
|
|
|
|
2019-10-14 13:38:14 -06:00
|
|
|
pthread_t update_thread;
|
|
|
|
if(pthread_create(&update_thread, NULL, status_loop, &meta)) {
|
|
|
|
fprintf(stderr, "Error creating thread\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-16 14:18:24 -06:00
|
|
|
for (;;) {
|
2019-10-16 08:50:56 -06:00
|
|
|
char *line = NULL;
|
2019-10-14 13:38:14 -06:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (getline(&line, &size, stdin) != -1) {
|
|
|
|
if (strcmp(line, "1\n") == 0) {
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(&meta, PREVIOUS);
|
2019-10-14 13:38:14 -06:00
|
|
|
} else if (strcmp(line, "2\n") == 0) {
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(&meta, TOGGLE_PAUSE);
|
2019-10-14 13:38:14 -06:00
|
|
|
} else if (strcmp(line, "3\n") == 0) {
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(&meta, NEXT);
|
2019-10-14 13:38:14 -06:00
|
|
|
} else if (strcmp(line, "4\n") == 0) {
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(&meta, SEEK_FWD);
|
2019-10-14 13:38:14 -06:00
|
|
|
} else if (strcmp(line, "5\n") == 0) {
|
2019-10-16 14:18:24 -06:00
|
|
|
mpd_run_command(&meta, SEEK_BCKWD);
|
2019-10-14 13:38:14 -06:00
|
|
|
}
|
2019-10-16 08:50:56 -06:00
|
|
|
|
|
|
|
free(line);
|
2019-10-14 13:38:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pthread_join(update_thread, NULL)) {
|
|
|
|
fprintf(stderr, "Error joining thread\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|