Permalink
Browse files

Configureable format strings

Closes #88
  • Loading branch information...
1 parent e8c314c commit 41f1a727c1b7ebbabf84652bab20361ee7631642 @PromyLOPh committed Apr 22, 2011
Showing with 345 additions and 160 deletions.
  1. +20 −16 src/main.c
  2. +12 −8 src/player.c
  3. +4 −0 src/player.h
  4. +63 −2 src/settings.c
  5. +10 −0 src/settings.h
  6. +150 −79 src/ui.c
  7. +5 −9 src/ui.h
  8. +41 −44 src/ui_act.c
  9. +2 −2 src/ui_dispatch.c
  10. +38 −0 src/ui_types.h
View
@@ -85,7 +85,7 @@ static bool BarMainLoginUser (BarApp_t *app) {
reqData.password = app->settings.password;
reqData.step = 0;
- BarUiMsg (MSG_INFO, "Login... ");
+ BarUiMsg (&app->settings, MSG_INFO, "Login... ");
ret = BarUiPianoCall (app, PIANO_REQUEST_LOGIN, &reqData, &pRet, &wRet);
BarUiStartEventCmd (&app->settings, "userlogin", NULL, NULL, &app->player,
NULL, pRet, wRet);
@@ -98,13 +98,13 @@ static void BarMainGetLoginCredentials (BarSettings_t *settings,
BarReadlineFds_t *input) {
if (settings->username == NULL) {
char nameBuf[100];
- BarUiMsg (MSG_QUESTION, "Username: ");
+ BarUiMsg (settings, MSG_QUESTION, "Username: ");
BarReadlineStr (nameBuf, sizeof (nameBuf), input, BAR_RL_DEFAULT);
settings->username = strdup (nameBuf);
}
if (settings->password == NULL) {
char passBuf[100];
- BarUiMsg (MSG_QUESTION, "Password: ");
+ BarUiMsg (settings, MSG_QUESTION, "Password: ");
BarReadlineStr (passBuf, sizeof (passBuf), input, BAR_RL_NOECHO);
write (STDIN_FILENO, "\n", 1);
settings->password = strdup (passBuf);
@@ -118,7 +118,7 @@ static bool BarMainGetStations (BarApp_t *app) {
WaitressReturn_t wRet;
bool ret;
- BarUiMsg (MSG_INFO, "Get stations... ");
+ BarUiMsg (&app->settings, MSG_INFO, "Get stations... ");
ret = BarUiPianoCall (app, PIANO_REQUEST_GET_STATIONS, NULL, &pRet, &wRet);
BarUiStartEventCmd (&app->settings, "usergetstations", NULL, NULL, &app->player,
app->ph.stations, pRet, wRet);
@@ -133,16 +133,16 @@ static void BarMainGetInitialStation (BarApp_t *app) {
app->curStation = PianoFindStationById (app->ph.stations,
app->settings.autostartStation);
if (app->curStation == NULL) {
- BarUiMsg (MSG_ERR, "Error: Autostart station not found.\n");
+ BarUiMsg (&app->settings, MSG_ERR,
+ "Error: Autostart station not found.\n");
}
}
/* no autostart? ask the user */
if (app->curStation == NULL) {
- app->curStation = BarUiSelectStation (&app->ph, "Select station: ",
- app->settings.sortOrder, &app->input);
+ app->curStation = BarUiSelectStation (app, "Select station: ");
}
if (app->curStation != NULL) {
- BarUiPrintStation (app->curStation);
+ BarUiPrintStation (&app->settings, app->curStation);
}
}
@@ -166,14 +166,14 @@ static void BarMainGetPlaylist (BarApp_t *app) {
reqData.station = app->curStation;
reqData.format = app->settings.audioFormat;
- BarUiMsg (MSG_INFO, "Receiving new playlist... ");
+ BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... ");
if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST,
&reqData, &pRet, &wRet)) {
app->curStation = NULL;
} else {
app->playlist = reqData.retPlaylist;
if (app->playlist == NULL) {
- BarUiMsg (MSG_INFO, "No tracks left.\n");
+ BarUiMsg (&app->settings, MSG_INFO, "No tracks left.\n");
app->curStation = NULL;
}
}
@@ -190,7 +190,7 @@ static void BarMainStartPlayback (BarApp_t *app, pthread_t *playerThread) {
app->playlist->stationId) : NULL);
if (app->playlist->audioUrl == NULL) {
- BarUiMsg (MSG_ERR, "Invalid song url.\n");
+ BarUiMsg (&app->settings, MSG_ERR, "Invalid song url.\n");
} else {
/* setup player */
memset (&app->player, 0, sizeof (app->player));
@@ -212,6 +212,7 @@ static void BarMainStartPlayback (BarApp_t *app, pthread_t *playerThread) {
app->player.gain = app->playlist->fileGain;
app->player.scale = BarPlayerCalcScale (app->player.gain + app->settings.volume);
app->player.audioFormat = app->playlist->audioFormat;
+ app->player.settings = &app->settings;
/* throw event */
BarUiStartEventCmd (&app->settings, "songstart",
@@ -261,7 +262,7 @@ static void BarMainPrintTime (BarApp_t *app) {
sign = POSITIVE;
songRemaining = -songRemaining;
}
- BarUiMsg (MSG_TIME, "%c%02i:%02i/%02i:%02i\r",
+ BarUiMsg (&app->settings, MSG_TIME, "%c%02i:%02i/%02i:%02i\r",
(sign == POSITIVE ? '+' : '-'),
songRemaining / 60, songRemaining % 60,
app->player.songDuration / BAR_PLAYER_MS_TO_S_FACTOR / 60,
@@ -356,11 +357,13 @@ int main (int argc, char **argv) {
BarSettingsInit (&app.settings);
BarSettingsRead (&app.settings);
- BarUiMsg (MSG_NONE, "Welcome to " PACKAGE " (" VERSION ")! ");
+ BarUiMsg (&app.settings, MSG_NONE,
+ "Welcome to " PACKAGE " (" VERSION ")! ");
if (app.settings.keys[BAR_KS_HELP] == BAR_KS_DISABLED) {
- BarUiMsg (MSG_NONE, "\n");
+ BarUiMsg (&app.settings, MSG_NONE, "\n");
} else {
- BarUiMsg (MSG_NONE, "Press %c for a list of commands.\n",
+ BarUiMsg (&app.settings, MSG_NONE,
+ "Press %c for a list of commands.\n",
app.settings.keys[BAR_KS_HELP]);
}
@@ -375,7 +378,8 @@ int main (int argc, char **argv) {
app.input.fds[1] = open (ctlPath, O_RDWR);
if (app.input.fds[1] != -1) {
FD_SET(app.input.fds[1], &app.input.set);
- BarUiMsg (MSG_INFO, "Control fifo at %s opened\n", ctlPath);
+ BarUiMsg (&app.settings, MSG_INFO, "Control fifo at %s opened\n",
+ ctlPath);
}
app.input.maxfd = app.input.fds[0] > app.input.fds[1] ? app.input.fds[0] :
app.input.fds[1];
View
@@ -33,6 +33,7 @@ THE SOFTWARE.
#include "player.h"
#include "config.h"
#include "ui.h"
+#include "ui_types.h"
#define bigToHostEndian32(x) ntohl(x)
@@ -88,7 +89,7 @@ static inline int BarPlayerBufferFill (struct audioPlayer *player, char *data,
size_t dataSize) {
/* fill buffer */
if (player->bufferFilled + dataSize > sizeof (player->buffer)) {
- BarUiMsg (MSG_ERR, "Buffer overflow!\n");
+ BarUiMsg (player->settings, MSG_ERR, "Buffer overflow!\n");
return 0;
}
memcpy (player->buffer+player->bufferFilled, data, dataSize);
@@ -140,7 +141,7 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream)
player->buffer + player->bufferRead,
player->sampleSize[player->sampleSizeCurr]);
if (frameInfo.error != 0) {
- BarUiMsg (MSG_ERR, "Decoding error: %s\n",
+ BarUiMsg (player->settings, MSG_ERR, "Decoding error: %s\n",
NeAACDecGetErrorMessage (frameInfo.error));
break;
}
@@ -189,8 +190,8 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream)
&player->channels);
player->bufferRead += 5;
if (err != 0) {
- BarUiMsg (MSG_ERR, "Error while "
- "initializing audio decoder"
+ BarUiMsg (player->settings, MSG_ERR,
+ "Error while initializing audio decoder "
"(%i)\n", err);
return WAITRESS_CB_RET_ERR;
}
@@ -204,7 +205,8 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream)
&format, NULL)) == NULL) {
/* we're not interested in the errno */
player->aoError = 1;
- BarUiMsg (MSG_ERR, "Cannot open audio device\n");
+ BarUiMsg (player->settings, MSG_ERR,
+ "Cannot open audio device\n");
return WAITRESS_CB_RET_ERR;
}
player->mode = PLAYER_AUDIO_INITIALIZED;
@@ -329,7 +331,8 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, void *stream)
if (mad_frame_decode (&player->mp3Frame, &player->mp3Stream) != 0) {
if (player->mp3Stream.error != MAD_ERROR_BUFLEN) {
- BarUiMsg (MSG_ERR, "mp3 decoding error: %s\n",
+ BarUiMsg (player->settings, MSG_ERR,
+ "mp3 decoding error: %s\n",
mad_stream_errorstr (&player->mp3Stream));
return WAITRESS_CB_RET_ERR;
} else {
@@ -362,7 +365,8 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, void *stream)
if ((player->audioOutDevice = ao_open_live (audioOutDriver,
&format, NULL)) == NULL) {
player->aoError = 1;
- BarUiMsg (MSG_ERR, "Cannot open audio device\n");
+ BarUiMsg (player->settings, MSG_ERR,
+ "Cannot open audio device\n");
return WAITRESS_CB_RET_ERR;
}
@@ -444,7 +448,7 @@ void *BarPlayerThread (void *data) {
#endif /* ENABLE_MAD */
default:
- BarUiMsg (MSG_ERR, "Unsupported audio format!\n");
+ BarUiMsg (player->settings, MSG_ERR, "Unsupported audio format!\n");
return PLAYER_RET_OK;
break;
}
View
@@ -42,6 +42,8 @@ THE SOFTWARE.
#include <piano.h>
#include <waitress.h>
+#include "settings.h"
+
#define BAR_PLAYER_MS_TO_S_FACTOR 1000
struct audioPlayer {
@@ -99,6 +101,8 @@ struct audioPlayer {
char doQuit;
pthread_mutex_t pauseMutex;
+
+ const BarSettings_t *settings;
};
enum {PLAYER_RET_OK = 0, PLAYER_RET_ERR = 1};
View
@@ -85,6 +85,12 @@ void BarSettingsDestroy (BarSettings_t *settings) {
free (settings->eventCmd);
free (settings->loveIcon);
free (settings->banIcon);
+ free (settings->npSongFormat);
+ free (settings->npStationFormat);
+ for (size_t i = 0; i < MSG_COUNT; i++) {
+ free (settings->msgFormat[i].prefix);
+ free (settings->msgFormat[i].postfix);
+ }
memset (settings, 0, sizeof (*settings));
}
@@ -95,6 +101,7 @@ void BarSettingsDestroy (BarSettings_t *settings) {
void BarSettingsRead (BarSettings_t *settings) {
char configfile[PATH_MAX], key[256], val[256];
FILE *configfd;
+ static const char *formatMsgPrefix = "format_msg_";
assert (sizeof (settings->keys) / sizeof (*settings->keys) ==
sizeof (dispatchActions) / sizeof (*dispatchActions));
@@ -110,8 +117,26 @@ void BarSettingsRead (BarSettings_t *settings) {
settings->history = 5;
settings->volume = 0;
settings->sortOrder = BAR_SORT_NAME_AZ;
- settings->loveIcon = strdup ("<3");
- settings->banIcon = strdup ("</3");
+ settings->loveIcon = strdup (" <3");
+ settings->banIcon = strdup (" </3");
+ settings->npSongFormat = strdup ("\"%t\" by \"%a\" on \"%l\"%r%@%s");
+ settings->npStationFormat = strdup ("Station \"%n\" (%i)");
+
+ settings->msgFormat[MSG_NONE].prefix = NULL;
+ settings->msgFormat[MSG_NONE].postfix = NULL;
+ settings->msgFormat[MSG_INFO].prefix = strdup ("(i) ");
+ settings->msgFormat[MSG_INFO].postfix = NULL;
+ settings->msgFormat[MSG_PLAYING].prefix = strdup ("|> ");
+ settings->msgFormat[MSG_PLAYING].postfix = NULL;
+ settings->msgFormat[MSG_TIME].prefix = strdup ("# ");
+ settings->msgFormat[MSG_TIME].postfix = NULL;
+ settings->msgFormat[MSG_ERR].prefix = strdup ("/!\\ ");
+ settings->msgFormat[MSG_ERR].postfix = NULL;
+ settings->msgFormat[MSG_QUESTION].prefix = strdup ("[?] ");
+ settings->msgFormat[MSG_QUESTION].postfix = NULL;
+ settings->msgFormat[MSG_LIST].prefix = strdup ("\t");
+ settings->msgFormat[MSG_LIST].postfix = NULL;
+
for (size_t i = 0; i < BAR_KS_COUNT; i++) {
settings->keys[i] = dispatchActions[i].defaultKey;
}
@@ -188,6 +213,42 @@ void BarSettingsRead (BarSettings_t *settings) {
settings->banIcon = strdup (val);
} else if (streq ("volume", key)) {
settings->volume = atoi (val);
+ } else if (streq ("format_nowplaying_song", key)) {
+ free (settings->npSongFormat);
+ settings->npSongFormat = strdup (val);
+ } else if (streq ("format_nowplaying_station", key)) {
+ free (settings->npStationFormat);
+ settings->npStationFormat = strdup (val);
+ } else if (strncmp (formatMsgPrefix, key,
+ strlen (formatMsgPrefix)) == 0) {
+ static const char *mapping[] = {"none", "info", "nowplaying",
+ "time", "err", "question", "list"};
+ const char *typeStart = key + strlen (formatMsgPrefix);
+ for (size_t i = 0; i < sizeof (mapping) / sizeof (*mapping); i++) {
+ if (streq (typeStart, mapping[i])) {
+ const char *formatPos = strstr (val, "%s");
+
+ /* keep default if there is no format character */
+ if (formatPos != NULL) {
+ BarMsgFormatStr_t *format = &settings->msgFormat[i];
+
+ free (format->prefix);
+ free (format->postfix);
+
+ const size_t prefixLen = formatPos - val;
+ format->prefix = calloc (prefixLen + 1,
+ sizeof (*format->prefix));
+ memcpy (format->prefix, val, prefixLen);
+
+ const size_t postfixLen = strlen (val) -
+ (formatPos-val) - 2;
+ format->postfix = calloc (postfixLen + 1,
+ sizeof (*format->postfix));
+ memcpy (format->postfix, formatPos+2, postfixLen);
+ }
+ break;
+ }
+ }
}
}
View
@@ -70,6 +70,13 @@ typedef enum {
BAR_SORT_COUNT = 6,
} BarStationSorting_t;
+#include "ui_types.h"
+
+typedef struct {
+ char *prefix;
+ char *postfix;
+} BarMsgFormatStr_t;
+
typedef struct {
unsigned int history;
int volume;
@@ -84,6 +91,9 @@ typedef struct {
char *eventCmd;
char *loveIcon;
char *banIcon;
+ char *npSongFormat;
+ char *npStationFormat;
+ BarMsgFormatStr_t msgFormat[MSG_COUNT];
} BarSettings_t;
void BarSettingsInit (BarSettings_t *);
Oops, something went wrong.

0 comments on commit 41f1a72

Please sign in to comment.