commit f6e1664c254d3bf3d1413ffada1e4ae4a0321787
parent e41aba7c606ab17d01bbf206426f779a1ce0fe1a
Author: Louis Burda <quent.burda@gmail.com>
Date: Sat, 9 Mar 2024 22:54:28 +0100
Simplify status message format
Diffstat:
M | mplay.h | | | 126 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- |
1 file changed, 96 insertions(+), 30 deletions(-)
diff --git a/mplay.h b/mplay.h
@@ -1,38 +1,40 @@
#pragma once
+#include <assert.h>
+
#include <stdarg.h>
+#include <string.h>
#include <stdio.h>
#define MPLAY_API_VERSION "0.0.1"
#define MPLAY_FLAG_HEADLESS "-d"
-#define MPLAY_ACTION_STATUS_VERSION "version"
+#define MPLAY_INFO_STR_VERSION "version"
#define MPLAY_ACTION_KEY_VERSION '?'
-#define MPLAY_ACTION_STATUS_STATUS "status"
+#define MPLAY_INFO_STR_STATUS "status"
#define MPLAY_ACTION_KEY_STATUS 'S'
-#define MPLAY_ACTION_STATUS_VOL "vol"
-#define MPLAY_ACTION_STATUS_VOL_UP "vol+"
-#define MPLAY_ACTION_STATUS_VOL_DOWN "vol-"
-#define MPLAY_ACTION_KEY_VOL 'v'
-#define MPLAY_ACTION_KEY_VOL_UP '+'
-#define MPLAY_ACTION_KEY_VOL_DOWN '-'
+#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_ACTION_STATUS_SEEK "seek"
-#define MPLAY_ACTION_STATUS_SEEK_FWD "seek+"
-#define MPLAY_ACTION_STATUS_SEEK_BWD "seek-"
+#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_ACTION_STATUS_PLAYPAUSE "play"
-#define MPLAY_ACTION_KEY_PLAYPAUSE 'p'
+#define MPLAY_INFO_STR_PAUSE "play"
+#define MPLAY_ACTION_KEY_PAUSE 'p'
+
+#define MPLAY_INFO_STR_EXIT "exit"
+#define MPLAY_ACTION_KEY_EXIT 'q'
-#define MPLAY_STATUS_READY "ready"
-#define MPLAY_STATUS_INPUT "input"
-#define MPLAY_STATUS_EXIT "exit"
+#define MPLAY_INFO_STR_READY "ready"
+
+#define MPLAY_INFO_STR_INPUT "input"
enum mplay_key {
MPLAY_KEY_INV = 256,
@@ -50,14 +52,27 @@ enum mplay_action {
MPLAY_ACTION_SEEK,
MPLAY_ACTION_SEEK_FWD,
MPLAY_ACTION_SEEK_BWD,
- MPLAY_ACTION_VOL,
- MPLAY_ACTION_VOL_UP,
- MPLAY_ACTION_VOL_DOWN,
- MPLAY_ACTION_PLAYPAUSE,
+ 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;
@@ -80,6 +95,7 @@ mplay__getkey_esc(void)
}
static int
+__attribute__((unused))
mplay_getkey(void)
{
int c;
@@ -100,19 +116,20 @@ mplay_getkey(void)
}
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_VOL:
- return MPLAY_ACTION_VOL;
+ case MPLAY_ACTION_KEY_VOLUME:
+ return MPLAY_ACTION_VOLUME;
case MPLAY_KEY_UP:
- case MPLAY_ACTION_KEY_VOL_UP:
- return MPLAY_ACTION_VOL_UP;
+ case MPLAY_ACTION_KEY_VOLUME_UP:
+ return MPLAY_ACTION_VOLUME_UP;
case MPLAY_KEY_DOWN:
- case MPLAY_ACTION_KEY_VOL_DOWN:
- return MPLAY_ACTION_VOL_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:
@@ -121,8 +138,8 @@ mplay_action(int key)
case MPLAY_KEY_RIGHT:
case MPLAY_ACTION_KEY_SEEK_FWD:
return MPLAY_ACTION_SEEK_FWD;
- case MPLAY_ACTION_KEY_PLAYPAUSE:
- return MPLAY_ACTION_PLAYPAUSE;
+ case MPLAY_ACTION_KEY_PAUSE:
+ return MPLAY_ACTION_PAUSE;
case MPLAY_ACTION_KEY_STATUS:
return MPLAY_ACTION_STATUS;
default:
@@ -130,12 +147,61 @@ mplay_action(int key)
}
}
+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:
+ assert("bug" == NULL);
+ }
+}
+
+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
+ assert("bug" == NULL);
+}
+
static void
-mplay_status(const char *cmd, const char *fmt, ...)
+__attribute__((unused))
+mplay_status(enum mplay_info info, const char *fmt, ...)
{
va_list ap;
- printf("mplay!%s", cmd);
+ fputs(mplay_info_str(info), stdout);
if (fmt) {
fputc(':', stdout);
va_start(ap, fmt);