mpdart/mpdart.c

485 lines
11 KiB
C
Raw Normal View History

2021-01-27 14:55:01 +01:00
#include <dirent.h>
#include <stdarg.h>
#include <stdbool.h>
2021-01-24 02:38:51 +01:00
#include <stdlib.h>
#include <stdio.h>
2021-01-27 14:55:01 +01:00
#include <string.h>
2021-01-24 02:38:51 +01:00
2021-01-27 14:55:01 +01:00
#include <poll.h>
#include <sys/types.h>
2021-01-24 02:38:51 +01:00
#include <unistd.h>
2021-01-27 14:55:01 +01:00
#include <Imlib2.h>
2021-01-24 02:38:51 +01:00
#include <mpd/client.h>
2021-01-27 14:55:01 +01:00
#include <X11/Xlib.h>
#include <X11/Xutil.h>
2021-01-24 02:38:51 +01:00
/* let user define a size on the compiler command line if they want to */
#ifndef DEFAULTSIZE
#define DEFAULTSIZE 256
#endif
/* TODO image metadata images */
2021-01-27 14:55:01 +01:00
/* mpd globals */
struct mpd_connection* connection = 0;
int mpd_fd = 0;
char* mpd_db_dir;
char* mpd_host;
unsigned mpd_port;
unsigned mpd_timeout;
2021-01-24 02:38:51 +01:00
2021-01-27 23:39:47 +01:00
/* x globals */
2021-01-27 14:55:01 +01:00
Display* xdisplay;
int xscreen;
Visual* xvisual;
Colormap xcolormap;
int xdepth;
Window xwindow;
GC gc;
unsigned int ww = DEFAULTSIZE, wh = DEFAULTSIZE;
2021-01-27 14:55:01 +01:00
2021-01-27 23:39:47 +01:00
/* imlib globals */
2021-01-27 14:55:01 +01:00
Imlib_Updates im_updates;
2021-01-27 20:16:13 +01:00
Imlib_Image im_buffer, im_image = 0;
2021-01-27 14:55:01 +01:00
Imlib_Color_Range range;
char* im_image_path;
int im_w = DEFAULTSIZE, im_h = DEFAULTSIZE;
2021-01-27 14:55:01 +01:00
2021-01-27 23:39:47 +01:00
/* print to stderr and exit */
2021-01-27 14:55:01 +01:00
void die(const char* msg) {
fprintf(stderr, "FATAL: %s\n", msg);
2021-01-27 14:55:01 +01:00
exit(1);
}
2021-01-27 23:39:47 +01:00
/* print to stderr */
void warn(const char* msg) {
fprintf(stderr, "WARNING: %s\n", msg);
}
2021-01-27 14:55:01 +01:00
/* error checking malloc */
void* xmalloc(size_t size) {
void* ret = malloc(size);
if (!ret)
die("Unable to allocate memory");
return ret;
}
/* returns char pointer instead of writing to predefined one */
char* asprintf(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
/* +1 for null byte */
int len = vsnprintf(0, 0, fmt, ap) + 1;
va_end(ap);
char* ret = xmalloc(len);
/* vsnprintf fucks up ap, so reopen it for the second call */
va_start(ap, fmt);
vsnprintf(ret, len, fmt, ap);
va_end(ap);
return ret;
}
/* set the name of the x window */
2021-01-27 14:55:01 +01:00
void set_window_name(char* name) {
int len = strlen(name);
XTextProperty prop;
Xutf8TextListToTextProperty(xdisplay, &name, 1, XUTF8StringStyle,
&prop);
XSetWMName(xdisplay, xwindow, &prop);
XSetTextProperty(xdisplay, xwindow, &prop, XInternAtom(xdisplay, "_NET_WM_NAME", False));
XFree(prop.value);
2021-01-27 19:00:05 +01:00
XFlush(xdisplay);
2021-01-27 14:55:01 +01:00
}
/* path must be dynamically allocated */
void imlib_update(char* path) {
/* if we already have a path we might want to free it */
if (im_image_path) {
/* if the path is the same, just free the new path and return */
if (path && !strcmp(path, im_image_path)) {
free(path);
return;
}
/* otherwise just free the old path */
2021-01-27 14:55:01 +01:00
free(im_image_path);
}
2021-01-27 14:55:01 +01:00
im_image_path = path;
2021-01-27 20:16:13 +01:00
if (im_image) {
imlib_context_set_image(im_image);
imlib_free_image();
}
if (!im_image_path) {
warn("No image path");
XClearWindow(xdisplay, xwindow);
XFlush(xdisplay);
2021-01-27 20:16:13 +01:00
return;
}
im_image = imlib_load_image_immediately(im_image_path);
2021-01-27 20:16:13 +01:00
if (!im_image) {
warn("Unable to open image");
2021-01-27 20:45:21 +01:00
printf("%s\n", im_image_path);
2021-01-27 20:16:13 +01:00
XClearWindow(xdisplay, xwindow);
return;
}
imlib_context_set_image(im_image);
im_w = imlib_image_get_width();
im_h = imlib_image_get_height();
imlib_context_set_image(im_buffer);
2021-01-27 20:16:13 +01:00
}
2021-01-27 23:39:47 +01:00
void imlib_render(void) {
2021-01-27 20:16:13 +01:00
2021-01-27 20:45:21 +01:00
if (!im_image || !im_image_path)
return;
2021-01-27 20:16:13 +01:00
imlib_blend_image_onto_image(im_image, 0,
0, 0, im_w, im_h,
2021-01-27 23:39:47 +01:00
0, 0, ww, wh);
2021-01-27 20:16:13 +01:00
imlib_render_image_on_drawable(0, 0);
2021-01-27 20:16:13 +01:00
}
2021-01-27 14:55:01 +01:00
/* get currently playing song from mpd and update X window */
void update_mpd_song(void) {
2021-01-27 14:55:01 +01:00
static int song_id;
static int old_song_id = -1;
struct mpd_song* song;
mpd_send_current_song(connection);
song = mpd_recv_song(connection);
if (!song) {
fprintf(stderr, "Failed to get song from mpd\n");
imlib_update(0);
2021-01-27 14:55:01 +01:00
set_window_name("None");
return;
}
song_id = mpd_song_get_id(song);
bool updated = false;
2021-01-27 14:55:01 +01:00
if (song_id != old_song_id) {
char* pretty_name = asprintf("%s - %s",
mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
char* path = asprintf("%s/%s",
mpd_db_dir, mpd_song_get_uri(song));
set_window_name(pretty_name);
printf("Now playing: '%s' from '%s'\n", pretty_name, path);
/* equivalent to shell dirname="${path%/*}" */
size_t len = strrchr(path, '/') - path;
char* dirname = xmalloc(len + 1);
strncpy(dirname, path, len);
dirname[len] = '\0';
/* account for .cue files */
for (char* cue = strstr(dirname, ".cue");
cue >= dirname; cue--) {
if (*cue == '/') {
*cue = '\0';
break;
2021-01-27 14:55:01 +01:00
}
}
DIR* dir = opendir(dirname);
if (!dir) {
warn("Unable to open dir");
} else {
struct dirent* ent;
while (ent = readdir(dir)) {
char* extension = strrchr(ent->d_name, '.');
/* TODO add more extensions and match multiple extension
and select one based on list of strings such as cover COVER
art album etc */
if (extension && (!strcmp(extension, ".jpg") || !strcmp(extension, ".png"))) {
printf("Using '%s' as album art.\n", ent->d_name);
imlib_update(asprintf("%s/%s", dirname, ent->d_name));
updated = true;
break;
}
2021-01-27 14:55:01 +01:00
}
}
if (!updated)
imlib_update(0);
2021-01-27 14:55:01 +01:00
closedir(dir);
free(pretty_name);
free(path);
2021-01-27 14:55:01 +01:00
}
old_song_id = song_id;
mpd_song_free(song);
}
2021-01-27 23:39:47 +01:00
/* check and handle errors */
void mpd_check_error(void) {
switch (mpd_connection_get_error(connection)) {
case MPD_ERROR_OOM:
case MPD_ERROR_ARGUMENT:
case MPD_ERROR_SYSTEM:
die(mpd_connection_get_error_message(connection));
case MPD_ERROR_SERVER:
case MPD_ERROR_MALFORMED:
warn(mpd_connection_get_error_message(connection));
case MPD_ERROR_RESOLVER:
case MPD_ERROR_CLOSED:
while(mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
warn("Unable to connect to mpd, retrying in 5 seconds");
sleep(5);
mpd_connection_free(connection);
connection = mpd_connection_new(mpd_host, mpd_port, mpd_timeout);
if (!connection)
die("Unable to allocate memory for mpd_connection struct");
}
printf("Successfully connected to mpd again!\n");
}
}
2021-01-24 02:38:51 +01:00
int main(int argc, char** argv) {
/* get args from environment */
mpd_db_dir = getenv("MPDART_DIR");
mpd_host = getenv("MPDART_HOST");
char* port = getenv("MPDART_PORT");
mpd_port = port ? atoi(port) : 0;
char* timeout = getenv("MPDART_TIMEOUT");
mpd_timeout = timeout ? atoi(timeout) : 0;
2021-01-24 02:38:51 +01:00
/* parse args */
while (*++argv) {
if (!strcmp(*argv, "-d"))
mpd_db_dir = *++argv;
else if (!strcmp(*argv, "-h"))
mpd_host = *++argv;
else if (!strcmp(*argv, "-p") && argv[1])
mpd_port = atoi(*++argv);
else if (!strcmp(*argv, "-t") && argv[1])
mpd_timeout = atoi(*++argv);
}
2021-01-27 14:55:01 +01:00
if (!mpd_db_dir)
die("Please specify mpd music directory with -d, or in the MPDART_DIR environment variable");
2021-01-24 02:38:51 +01:00
2021-01-27 14:55:01 +01:00
/* strip all '/'es from the end of mpd_db_dir */
2021-01-24 02:38:51 +01:00
{
int i = 0;
for (; mpd_db_dir[i]; i++);
while (mpd_db_dir[--i] == '/');
mpd_db_dir[i+1] = '\0';
}
2021-01-27 14:55:01 +01:00
/* set up mpd connection */
2021-01-24 02:38:51 +01:00
connection = mpd_connection_new(mpd_host, mpd_port, mpd_timeout);
2021-01-27 14:55:01 +01:00
if (!connection)
die("Unable to allocate memory for mpd_connection struct");
2021-01-24 02:38:51 +01:00
2021-01-27 14:55:01 +01:00
if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
die(mpd_connection_get_error_message(connection));
2021-01-24 02:38:51 +01:00
2021-01-27 23:43:30 +01:00
const unsigned int* version = mpd_connection_get_server_version(connection);
2021-01-24 02:38:51 +01:00
printf("Connected to mpd server version %d.%d.%d\n", version[0], version[1], version[2]);
2021-01-27 14:55:01 +01:00
/* setup x */
XInitThreads();
2021-01-24 02:38:51 +01:00
2021-01-27 14:55:01 +01:00
xdisplay = XOpenDisplay(0);
if (!xdisplay)
die("Cannot open display");
xscreen = XDefaultScreen(xdisplay);
gc = DefaultGC(xdisplay, xscreen);
xvisual = DefaultVisual(xdisplay, xscreen);
xdepth = DefaultDepth(xdisplay, xscreen);
xcolormap = DefaultColormap(xdisplay, xscreen);
Window xparent = XRootWindow(xdisplay, xscreen);
2021-01-24 02:38:51 +01:00
2021-01-27 23:39:47 +01:00
unsigned x = 0, y = 0;
2021-01-27 14:55:01 +01:00
unsigned int border_width = 0;
/* are these two needed when border_width is 0? */
unsigned int border_color = BlackPixel(xdisplay, xscreen);
unsigned int background_color = WhitePixel(xdisplay, xscreen);
2021-01-24 02:38:51 +01:00
2021-01-27 14:55:01 +01:00
xwindow = XCreateSimpleWindow(
xdisplay,
xparent,
x,
y,
2021-01-27 19:00:05 +01:00
ww,
wh,
2021-01-27 14:55:01 +01:00
border_width,
border_color,
background_color);
2021-01-24 02:38:51 +01:00
XSizeHints* size_hints = XAllocSizeHints();
if (!size_hints)
die("Unable to allocate memory");
size_hints->flags = PAspect | PMinSize | PMaxSize;
size_hints->min_width = 64;
size_hints->min_height = 64;
size_hints->max_width = 1024;
size_hints->max_height = 1024;
size_hints->min_aspect.x = 1;
size_hints->max_aspect.x = 1;
size_hints->min_aspect.y = 1;
size_hints->max_aspect.y = 1;
XSetWMNormalHints(xdisplay, xwindow, size_hints);
2021-01-27 17:03:42 +01:00
XFree(size_hints);
XSelectInput(xdisplay, xwindow, ExposureMask
| StructureNotifyMask
| ButtonPressMask);
2021-01-27 14:55:01 +01:00
XMapWindow(xdisplay, xwindow);
set_window_name("mpdart");
Atom wm_delete = XInternAtom(xdisplay, "WM_DELETE_WINDOW", True);
XSetWMProtocols(xdisplay, xwindow, &wm_delete, 1);
/* setup Imlib */
imlib_set_cache_size(2048 * 1024);
imlib_set_color_usage(128);
imlib_context_set_dither(1);
imlib_context_set_display(xdisplay);
imlib_context_set_visual(xvisual);
imlib_context_set_colormap(xcolormap);
imlib_context_set_drawable(xwindow);
im_updates = imlib_updates_init();
2021-01-27 20:16:13 +01:00
/* 1024 is max size */
im_buffer = imlib_create_image(1024, 1024);
if (!im_buffer) {
die("Unable to create buffer");
XClearWindow(xdisplay, xwindow);
}
2021-01-27 14:55:01 +01:00
/* get currently playing song before waiting for new ones */
update_mpd_song();
2021-01-27 23:39:47 +01:00
imlib_render();
2021-01-27 14:55:01 +01:00
mpd_fd = mpd_connection_get_fd(connection);
2021-01-27 23:39:47 +01:00
mpd_check_error();
2021-01-27 14:55:01 +01:00
int xfd = ConnectionNumber(xdisplay);
struct pollfd fds[2] = {
{
.fd = xfd,
.events = POLLIN
},
{
.fd = mpd_fd,
.events = POLLIN
2021-01-24 02:38:51 +01:00
}
2021-01-27 14:55:01 +01:00
};
2021-01-24 02:38:51 +01:00
2021-01-27 23:39:47 +01:00
mpd_send_idle_mask(connection, MPD_IDLE_PLAYER);
mpd_check_error();
2021-01-27 14:55:01 +01:00
/* mpd event loop */
while (1) {
/* sleep for a day at a time */
int ready_fds = poll(fds, 2, 86400);
2021-01-27 14:55:01 +01:00
if (ready_fds < 0) {
die("Error in poll");
} else if (ready_fds > 0) {
/* X event loop */
if (fds[0].revents & POLLIN) {
bool render = false;
while (XPending(xdisplay)) {
XEvent ev;
XNextEvent(xdisplay, &ev);
2021-01-24 02:38:51 +01:00
switch (ev.type) {
/* close window */
case ClientMessage:
2021-01-27 14:55:01 +01:00
XCloseDisplay(xdisplay);
die("Window Closed");
break;
/* redraw when off screen */
case Expose:
render = true;
break;
/* respond to resize */
2021-01-27 19:00:05 +01:00
case ConfigureNotify:
if (ww != ev.xconfigure.width || wh != ev.xconfigure.height) {
ww = ev.xconfigure.width;
wh = ev.xconfigure.height;
render = true;
}
break;
/* toggle pause on press */
case ButtonPress:
printf("Toggling pause\n");
mpd_run_noidle(connection);
mpd_check_error();
/* deprecated but they provide nothing better so fuck them */
mpd_run_toggle_pause(connection);
mpd_check_error();
mpd_send_idle_mask(connection, MPD_IDLE_PLAYER);
mpd_check_error();
2021-01-27 19:00:05 +01:00
break;
}
2021-01-27 14:55:01 +01:00
}
if (render)
imlib_render();
2021-01-27 14:55:01 +01:00
}
/* MPD event loop */
if (fds[1].revents & POLLIN) {
mpd_run_noidle(connection);
2021-01-27 23:39:47 +01:00
mpd_check_error();
2021-01-27 14:55:01 +01:00
update_mpd_song();
2021-01-27 23:39:47 +01:00
imlib_render();
2021-01-27 14:55:01 +01:00
mpd_send_idle_mask(connection, MPD_IDLE_PLAYER);
2021-01-27 23:39:47 +01:00
mpd_check_error();
2021-01-27 14:55:01 +01:00
}
2021-01-24 02:38:51 +01:00
}
2021-01-27 14:55:01 +01:00
}
2021-01-24 02:38:51 +01:00
}