mplay

Controllable music player
git clone https://git.sinitax.com/sinitax/mplay
Log | Files | Refs | sfeed.txt

mplay.h (4364B)


      1#pragma once
      2
      3#include <stdarg.h>
      4#include <string.h>
      5#include <stdlib.h>
      6#include <stdio.h>
      7
      8#define MPLAY_API_VERSION "0.0.1"
      9
     10#define MPLAY_FLAG_HEADLESS "-d"
     11
     12#define MPLAY_INFO_STR_VERSION "version"
     13#define MPLAY_ACTION_KEY_VERSION '?'
     14
     15#define MPLAY_INFO_STR_STATUS "status"
     16#define MPLAY_ACTION_KEY_STATUS 'S'
     17
     18#define MPLAY_INFO_STR_VOLUME "volume"
     19#define MPLAY_ACTION_KEY_VOLUME 'v'
     20#define MPLAY_ACTION_KEY_VOLUME_UP '+'
     21#define MPLAY_ACTION_KEY_VOLUME_DOWN '-'
     22
     23#define MPLAY_INFO_STR_SEEK "seek"
     24#define MPLAY_ACTION_KEY_SEEK 's'
     25#define MPLAY_ACTION_KEY_SEEK_FWD '>'
     26#define MPLAY_ACTION_KEY_SEEK_BWD '<'
     27
     28#define MPLAY_INFO_STR_PAUSE "pause"
     29#define MPLAY_ACTION_KEY_PAUSE 'p'
     30
     31#define MPLAY_INFO_STR_EXIT "exit"
     32#define MPLAY_ACTION_KEY_EXIT 'q'
     33
     34#define MPLAY_INFO_STR_READY "ready"
     35
     36#define MPLAY_INFO_STR_INPUT "input"
     37
     38enum mplay_key {
     39	MPLAY_KEY_INV = 256,
     40	MPLAY_KEY_EOF,
     41	MPLAY_KEY_UP,
     42	MPLAY_KEY_DOWN,
     43	MPLAY_KEY_LEFT,
     44	MPLAY_KEY_RIGHT,
     45};
     46
     47enum mplay_action {
     48	MPLAY_ACTION_NONE,
     49	MPLAY_ACTION_VERSION,
     50	MPLAY_ACTION_STATUS,
     51	MPLAY_ACTION_SEEK,
     52	MPLAY_ACTION_SEEK_FWD,
     53	MPLAY_ACTION_SEEK_BWD,
     54	MPLAY_ACTION_VOLUME,
     55	MPLAY_ACTION_VOLUME_UP,
     56	MPLAY_ACTION_VOLUME_DOWN,
     57	MPLAY_ACTION_PAUSE,
     58	MPLAY_ACTION_EXIT,
     59};
     60
     61enum mplay_info {
     62	MPLAY_INFO_NONE,
     63	MPLAY_INFO_VERSION,
     64	MPLAY_INFO_STATUS,
     65	MPLAY_INFO_SEEK,
     66	MPLAY_INFO_VOLUME,
     67	MPLAY_INFO_PAUSE,
     68	MPLAY_INFO_READY,
     69	MPLAY_INFO_INPUT,
     70	MPLAY_INFO_EXIT	
     71};
     72
     73static int
     74__attribute__((unused))
     75mplay__getkey_esc(void)
     76{
     77	int c;
     78
     79	c = getchar();
     80	switch (c) {
     81	case 'A':
     82		return MPLAY_KEY_UP;
     83	case 'B':
     84		return MPLAY_KEY_DOWN;
     85	case 'C':
     86		return MPLAY_KEY_RIGHT;
     87	case 'D':
     88		return MPLAY_KEY_LEFT;
     89	case EOF:
     90		return MPLAY_KEY_EOF;
     91	default:
     92		return MPLAY_KEY_INV;
     93	}
     94}
     95
     96static int
     97__attribute__((unused))
     98mplay_getkey(void)
     99{
    100	int c;
    101
    102	c = getchar();
    103	switch (c) {
    104	case 0x1b:
    105		c = getchar();
    106		switch (c) {
    107		case '[':
    108			return mplay__getkey_esc();
    109		default:
    110			return MPLAY_KEY_INV;
    111		}
    112	case EOF:
    113		return MPLAY_KEY_EOF;
    114	default:
    115		return c > 0 ? (c & 0xff) : c;
    116	}
    117}
    118
    119static enum mplay_action
    120__attribute__((unused))
    121mplay_action(int key)
    122{
    123	switch (key) {
    124	case MPLAY_ACTION_KEY_VERSION:
    125		return MPLAY_ACTION_VERSION;
    126	case MPLAY_ACTION_KEY_VOLUME:
    127		return MPLAY_ACTION_VOLUME;
    128	case MPLAY_KEY_UP:
    129	case MPLAY_ACTION_KEY_VOLUME_UP:
    130		return MPLAY_ACTION_VOLUME_UP;
    131	case MPLAY_KEY_DOWN:
    132	case MPLAY_ACTION_KEY_VOLUME_DOWN:
    133		return MPLAY_ACTION_VOLUME_DOWN;
    134	case MPLAY_ACTION_KEY_SEEK:
    135		return MPLAY_ACTION_SEEK;
    136	case MPLAY_KEY_LEFT:
    137	case MPLAY_ACTION_KEY_SEEK_BWD:
    138		return MPLAY_ACTION_SEEK_BWD;
    139	case MPLAY_KEY_RIGHT:
    140	case MPLAY_ACTION_KEY_SEEK_FWD:
    141		return MPLAY_ACTION_SEEK_FWD;
    142	case MPLAY_ACTION_KEY_PAUSE:
    143		return MPLAY_ACTION_PAUSE;
    144	case MPLAY_ACTION_KEY_STATUS:
    145		return MPLAY_ACTION_STATUS;
    146	default:
    147		return MPLAY_ACTION_NONE;
    148	}
    149}
    150
    151static const char *
    152__attribute__((unused))
    153mplay_info_str(enum mplay_info info)
    154{
    155	switch (info) {
    156	case MPLAY_INFO_VERSION:
    157		return MPLAY_INFO_STR_VERSION;
    158	case MPLAY_INFO_SEEK:
    159		return MPLAY_INFO_STR_SEEK;
    160	case MPLAY_INFO_STATUS:
    161		return MPLAY_INFO_STR_STATUS;
    162	case MPLAY_INFO_VOLUME:
    163		return MPLAY_INFO_STR_VOLUME;
    164	case MPLAY_INFO_EXIT:
    165		return MPLAY_INFO_STR_EXIT;
    166	case MPLAY_INFO_INPUT:
    167		return MPLAY_INFO_STR_INPUT;
    168	case MPLAY_INFO_READY:
    169		return MPLAY_INFO_STR_READY;
    170	case MPLAY_INFO_PAUSE:
    171		return MPLAY_INFO_STR_PAUSE;
    172	default:
    173		abort();
    174	}
    175}
    176
    177static enum mplay_info
    178__attribute__((unused))
    179mplay_info_parse(const char *str)
    180{
    181	if (!strcmp(str, MPLAY_INFO_STR_VERSION))
    182		return MPLAY_INFO_VERSION;
    183	else if (!strcmp(str, MPLAY_INFO_STR_SEEK))
    184		return MPLAY_INFO_SEEK;
    185	else if (!strcmp(str, MPLAY_INFO_STR_STATUS))
    186		return MPLAY_INFO_STATUS;
    187	else if (!strcmp(str, MPLAY_INFO_STR_VOLUME))
    188		return MPLAY_INFO_VOLUME;
    189	else if (!strcmp(str, MPLAY_INFO_STR_EXIT))
    190		return MPLAY_INFO_EXIT;
    191	else if (!strcmp(str, MPLAY_INFO_STR_INPUT))
    192		return MPLAY_INFO_INPUT;
    193	else if (!strcmp(str, MPLAY_INFO_STR_READY))
    194		return MPLAY_INFO_READY;
    195	else
    196		abort();
    197}
    198
    199static void
    200__attribute__((unused))
    201__attribute__((format(printf, 2, 3)))
    202mplay_status(enum mplay_info info, const char *fmt, ...)
    203{
    204	va_list ap;
    205
    206	fputs("mplay!", stdout);
    207	fputs(mplay_info_str(info), stdout);
    208	if (fmt) {
    209		fputc(':', stdout);
    210		va_start(ap, fmt);
    211		vfprintf(stdout, fmt, ap);
    212		va_end(ap);
    213	}
    214	fputc('\n', stdout);
    215}