#pragma once #include #include #include #include #define MPLAY_API_VERSION "0.0.1" #define MPLAY_FLAG_HEADLESS "-d" #define MPLAY_INFO_STR_VERSION "version" #define MPLAY_ACTION_KEY_VERSION '?' #define MPLAY_INFO_STR_STATUS "status" #define MPLAY_ACTION_KEY_STATUS 'S' #define MPLAY_INFO_STR_VOLUME "volume" #define MPLAY_ACTION_KEY_VOLUME 'v' #define MPLAY_ACTION_KEY_VOLUME_UP '+' #define MPLAY_ACTION_KEY_VOLUME_DOWN '-' #define MPLAY_INFO_STR_SEEK "seek" #define MPLAY_ACTION_KEY_SEEK 's' #define MPLAY_ACTION_KEY_SEEK_FWD '>' #define MPLAY_ACTION_KEY_SEEK_BWD '<' #define MPLAY_INFO_STR_PAUSE "pause" #define MPLAY_ACTION_KEY_PAUSE 'p' #define MPLAY_INFO_STR_EXIT "exit" #define MPLAY_ACTION_KEY_EXIT 'q' #define MPLAY_INFO_STR_READY "ready" #define MPLAY_INFO_STR_INPUT "input" enum mplay_key { MPLAY_KEY_INV = 256, MPLAY_KEY_EOF, MPLAY_KEY_UP, MPLAY_KEY_DOWN, MPLAY_KEY_LEFT, MPLAY_KEY_RIGHT, }; enum mplay_action { MPLAY_ACTION_NONE, MPLAY_ACTION_VERSION, MPLAY_ACTION_STATUS, MPLAY_ACTION_SEEK, MPLAY_ACTION_SEEK_FWD, MPLAY_ACTION_SEEK_BWD, MPLAY_ACTION_VOLUME, MPLAY_ACTION_VOLUME_UP, MPLAY_ACTION_VOLUME_DOWN, MPLAY_ACTION_PAUSE, MPLAY_ACTION_EXIT, }; enum mplay_info { MPLAY_INFO_NONE, MPLAY_INFO_VERSION, MPLAY_INFO_STATUS, MPLAY_INFO_SEEK, MPLAY_INFO_VOLUME, MPLAY_INFO_PAUSE, MPLAY_INFO_READY, MPLAY_INFO_INPUT, MPLAY_INFO_EXIT }; static int __attribute__((unused)) mplay__getkey_esc(void) { int c; c = getchar(); switch (c) { case 'A': return MPLAY_KEY_UP; case 'B': return MPLAY_KEY_DOWN; case 'C': return MPLAY_KEY_RIGHT; case 'D': return MPLAY_KEY_LEFT; case EOF: return MPLAY_KEY_EOF; default: return MPLAY_KEY_INV; } } static int __attribute__((unused)) mplay_getkey(void) { int c; c = getchar(); switch (c) { case 0x1b: c = getchar(); switch (c) { case '[': return mplay__getkey_esc(); default: return MPLAY_KEY_INV; } case EOF: return MPLAY_KEY_EOF; default: return c > 0 ? (c & 0xff) : c; } } static enum mplay_action __attribute__((unused)) mplay_action(int key) { switch (key) { case MPLAY_ACTION_KEY_VERSION: return MPLAY_ACTION_VERSION; case MPLAY_ACTION_KEY_VOLUME: return MPLAY_ACTION_VOLUME; case MPLAY_KEY_UP: case MPLAY_ACTION_KEY_VOLUME_UP: return MPLAY_ACTION_VOLUME_UP; case MPLAY_KEY_DOWN: case MPLAY_ACTION_KEY_VOLUME_DOWN: return MPLAY_ACTION_VOLUME_DOWN; case MPLAY_ACTION_KEY_SEEK: return MPLAY_ACTION_SEEK; case MPLAY_KEY_LEFT: case MPLAY_ACTION_KEY_SEEK_BWD: return MPLAY_ACTION_SEEK_BWD; case MPLAY_KEY_RIGHT: case MPLAY_ACTION_KEY_SEEK_FWD: return MPLAY_ACTION_SEEK_FWD; case MPLAY_ACTION_KEY_PAUSE: return MPLAY_ACTION_PAUSE; case MPLAY_ACTION_KEY_STATUS: return MPLAY_ACTION_STATUS; default: return MPLAY_ACTION_NONE; } } static const char * __attribute__((unused)) mplay_info_str(enum mplay_info info) { switch (info) { case MPLAY_INFO_VERSION: return MPLAY_INFO_STR_VERSION; case MPLAY_INFO_SEEK: return MPLAY_INFO_STR_SEEK; case MPLAY_INFO_STATUS: return MPLAY_INFO_STR_STATUS; case MPLAY_INFO_VOLUME: return MPLAY_INFO_STR_VOLUME; case MPLAY_INFO_EXIT: return MPLAY_INFO_STR_EXIT; case MPLAY_INFO_INPUT: return MPLAY_INFO_STR_INPUT; case MPLAY_INFO_READY: return MPLAY_INFO_STR_READY; case MPLAY_INFO_PAUSE: return MPLAY_INFO_STR_PAUSE; default: abort(); } } static enum mplay_info __attribute__((unused)) mplay_info_parse(const char *str) { if (!strcmp(str, MPLAY_INFO_STR_VERSION)) return MPLAY_INFO_VERSION; else if (!strcmp(str, MPLAY_INFO_STR_SEEK)) return MPLAY_INFO_SEEK; else if (!strcmp(str, MPLAY_INFO_STR_STATUS)) return MPLAY_INFO_STATUS; else if (!strcmp(str, MPLAY_INFO_STR_VOLUME)) return MPLAY_INFO_VOLUME; else if (!strcmp(str, MPLAY_INFO_STR_EXIT)) return MPLAY_INFO_EXIT; else if (!strcmp(str, MPLAY_INFO_STR_INPUT)) return MPLAY_INFO_INPUT; else if (!strcmp(str, MPLAY_INFO_STR_READY)) return MPLAY_INFO_READY; else abort(); } static void __attribute__((unused)) __attribute__((format(printf, 2, 3))) mplay_status(enum mplay_info info, const char *fmt, ...) { va_list ap; fputs("mplay!", stdout); fputs(mplay_info_str(info), stdout); if (fmt) { fputc(':', stdout); va_start(ap, fmt); vfprintf(stdout, fmt, ap); va_end(ap); } fputc('\n', stdout); }