diff options
author | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2010-01-18 15:41:07 +0100 |
---|---|---|
committer | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2010-01-18 15:41:07 +0100 |
commit | 7df92f7654662d95959c4f1e43610be2f2d67fd1 (patch) | |
tree | 7cd0ad65f45ed914d52700b82406884deac665e7 | |
parent | 83e056f390c4804f9d283c7083a76dfa957ee054 (diff) | |
download | pianobar-7df92f7654662d95959c4f1e43610be2f2d67fd1.tar.gz pianobar-7df92f7654662d95959c4f1e43610be2f2d67fd1.tar.bz2 pianobar-7df92f7654662d95959c4f1e43610be2f2d67fd1.zip |
Shortcut refactoring
-rw-r--r-- | src/main.c | 22 | ||||
-rw-r--r-- | src/settings.c | 125 | ||||
-rw-r--r-- | src/settings.h | 56 | ||||
-rw-r--r-- | src/ui_act.c | 33 |
4 files changed, 95 insertions, 141 deletions
@@ -52,6 +52,7 @@ THE SOFTWARE. #include "terminal.h" #include "config.h" #include "ui.h" +#include "ui_act.h" #include "ui_readline.h" int main (int argc, char **argv) { @@ -74,7 +75,6 @@ int main (int argc, char **argv) { struct pollfd polls[2]; nfds_t pollsLen = 0; char buf = '\0'; - BarKeyShortcut_t *curShortcut = NULL; /* terminal attributes _before_ we started messing around with ~ECHO */ struct termios termOrig; @@ -301,15 +301,25 @@ int main (int argc, char **argv) { curFd = ctlFd; } buf = fgetc (curFd); - curShortcut = settings.keys; - while (curShortcut != NULL) { - if (curShortcut->key == buf) { - curShortcut->cmd (&ph, &player, &settings, &playlist, + size_t i; + for (i = 0; i < BAR_KS_COUNT; i++) { + if (settings.keys[i] == buf) { + BarKeyShortcutFunc_t idToF[] = {BarUiActHelp, + BarUiActLoveSong, BarUiActBanSong, + BarUiActAddMusic, BarUiActCreateStation, + BarUiActDeleteStation, BarUiActExplain, + BarUiActStationFromGenre, BarUiActHistory, + BarUiActSongInfo, BarUiActAddSharedStation, + BarUiActMoveSong, BarUiActSkipSong, BarUiActPause, + BarUiActQuit, BarUiActRenameStation, + BarUiActSelectStation, BarUiActTempBanSong, + BarUiActPrintUpcoming, BarUiActSelectQuickMix, + BarUiActDebug}; + idToF[i] (&ph, &player, &settings, &playlist, &curStation, &songHistory, &doQuit, curFd); break; } - curShortcut = curShortcut->next; } } diff --git a/src/settings.c b/src/settings.c index d2853e8..31690e6 100644 --- a/src/settings.c +++ b/src/settings.c @@ -31,7 +31,6 @@ THE SOFTWARE. #include "settings.h" #include "config.h" -#include "ui_act.h" /* tries to guess your config dir; somehow conforming to * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html @@ -72,19 +71,6 @@ void BarSettingsInit (BarSettings_t *settings) { * @oaram pointer to struct */ void BarSettingsDestroy (BarSettings_t *settings) { - BarKeyShortcut_t *curShortcut = settings->keys, *lastShortcut; - - while (curShortcut != NULL) { - lastShortcut = curShortcut; - curShortcut = curShortcut->next; - if (lastShortcut->description != NULL) { - free (lastShortcut->description); - } - if (lastShortcut->configKey != NULL) { - free (lastShortcut->configKey); - } - free (lastShortcut); - } free (settings->controlProxy); free (settings->username); free (settings->password); @@ -95,35 +81,6 @@ void BarSettingsDestroy (BarSettings_t *settings) { memset (settings, 0, sizeof (*settings)); } -/* copy key shortcut into settings structure - * @param shortcut to be copied - * @param destination settings structure - */ -static void BarSettingsAppendKey (BarKeyShortcut_t *shortcut, - BarSettings_t *settings) { - BarKeyShortcut_t *tmp = calloc (1, sizeof (*tmp)); - - /* copy shortcut */ - memcpy (tmp, shortcut, sizeof (*tmp)); - if (shortcut->description != NULL) { - tmp->description = strdup (shortcut->description); - } - if (shortcut->configKey != NULL) { - tmp->configKey = strdup (shortcut->configKey); - } - - /* insert into linked list */ - if (settings->keys == NULL) { - settings->keys = tmp; - } else { - BarKeyShortcut_t *curShortcut = settings->keys; - while (curShortcut->next != NULL) { - curShortcut = curShortcut->next; - } - curShortcut->next = tmp; - } -} - /* read app settings from file; format is: key = value\n * @param where to save these settings * @return nothing yet @@ -133,46 +90,17 @@ void BarSettingsRead (BarSettings_t *settings) { char configfile[1024], key[256], val[256]; size_t i; FILE *configfd; - BarKeyShortcut_t *curShortcut; - BarKeyShortcut_t defaultKeys[] = { - {'?', BarUiActHelp, NULL, "act_help", NULL}, - {'+', BarUiActLoveSong, "love current song", "act_songlove", - NULL}, - {'-', BarUiActBanSong, "ban current song", "act_songban", NULL}, - {'a', BarUiActAddMusic, "add music to current station", - "act_stationaddmusic", NULL}, - {'c', BarUiActCreateStation, "create new station", - "act_stationcreate", NULL}, - {'d', BarUiActDeleteStation, "delete current station", - "act_stationdelete", NULL}, - {'e', BarUiActExplain, "explain why this song is played", - "act_songexplain", NULL}, - {'g', BarUiActStationFromGenre, "add genre station", - "act_stationaddbygenre", NULL}, - {'h', BarUiActHistory, "song history", - "act_history", NULL}, - {'i', BarUiActSongInfo, - "print information about current song/station", - "act_songinfo", NULL}, - {'j', BarUiActAddSharedStation, "add shared station", - "act_addshared", NULL}, - {'m', BarUiActMoveSong, "move song to different station", - "act_songmove", NULL}, - {'n', BarUiActSkipSong, "next song", "act_songnext", NULL}, - {'p', BarUiActPause, "pause/continue", "act_songpause", NULL}, - {'q', BarUiActQuit, "quit", "act_quit", NULL}, - {'r', BarUiActRenameStation, "rename current station", - "act_stationrename", NULL}, - {'s', BarUiActSelectStation, "change station", - "act_stationchange", NULL}, - {'t', BarUiActTempBanSong, "tired (ban song for 1 month)", - "act_songtired", NULL}, - {'u', BarUiActPrintUpcoming, "upcoming songs", "act_upcoming", - NULL}, - {'x', BarUiActSelectQuickMix, "select quickmix stations", - "act_stationselectquickmix", NULL}, - {'$', BarUiActDebug, NULL, - "act_debug", NULL}, + /* _must_ have same order as in BarKeyShortcutId_t */ + const char defaultKeys[] = {'?', '+', '-', 'a', 'c', 'd', 'e', 'g', + 'h', 'i', 'j', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'x', '$'}; + const char *shortcutFileKeys[] = { + "act_help", "act_songlove", "act_songban", "act_stationaddmusic", + "act_stationcreate", "act_stationdelete", "act_songexplain", + "act_stationaddbygenre", "act_history", "act_songinfo", + "act_addshared", "act_songmove", "act_songnext", "act_songpause", + "act_quit", "act_stationrename", "act_stationchange", + "act_songtired", "act_upcoming", "act_stationselectquickmix", + "act_debug" }; /* apply defaults */ @@ -184,14 +112,10 @@ void BarSettingsRead (BarSettings_t *settings) { #endif #endif settings->history = 5; + memcpy (settings->keys, defaultKeys, sizeof (defaultKeys)); BarGetXdgConfigDir (PACKAGE "/config", configfile, sizeof (configfile)); if ((configfd = fopen (configfile, "r")) == NULL) { - /* use default keyboard shortcuts */ - for (i = 0; i < sizeof (defaultKeys) / sizeof (*defaultKeys); - i++) { - BarSettingsAppendKey (&defaultKeys[i], settings); - } return; } @@ -218,11 +142,9 @@ void BarSettingsRead (BarSettings_t *settings) { settings->lastfmScrobblePercent = atoi (val); } else if (memcmp ("act_", key, 4) == 0) { /* keyboard shortcuts */ - for (i = 0; i < sizeof (defaultKeys) / sizeof (*defaultKeys); - i++) { - if (strcmp (defaultKeys[i].configKey, key) == 0) { - defaultKeys[i].key = val[0]; - BarSettingsAppendKey (&defaultKeys[i], settings); + for (i = 0; i < BAR_KS_COUNT; i++) { + if (strcmp (shortcutFileKeys[i], key) == 0) { + settings->keys[i] = val[0]; break; } } @@ -255,22 +177,5 @@ void BarSettingsRead (BarSettings_t *settings) { settings->enableScrobbling = 1; } - /* append missing keyboard shortcuts to ensure the functionality is - * available */ - for (i = 0; i < sizeof (defaultKeys) / sizeof (*defaultKeys); i++) { - char shortcutAvailable = 0; - curShortcut = settings->keys; - while (curShortcut != NULL) { - if (curShortcut->cmd == defaultKeys[i].cmd) { - shortcutAvailable = 1; - break; - } - curShortcut = curShortcut->next; - } - if (!shortcutAvailable) { - BarSettingsAppendKey (&defaultKeys[i], settings); - } - } - fclose (configfd); } diff --git a/src/settings.h b/src/settings.h index 3d98ec6..614ebf3 100644 --- a/src/settings.h +++ b/src/settings.h @@ -29,11 +29,39 @@ THE SOFTWARE. #include "player.h" #define BAR_KS_ARGS PianoHandle_t *ph, struct audioPlayer *player, \ - struct BarSettings *settings, PianoSong_t **curSong, \ + BarSettings_t *settings, PianoSong_t **curSong, \ PianoStation_t **curStation, PianoSong_t **songHistory, char *doQuit, \ FILE *curFd -struct BarSettings { +/* keep in mind that you have to update several arrays in main.c/ui_act.c too, + * if you're adding new shortcuts */ +typedef enum { + BAR_KS_HELP = 0, + BAR_KS_LOVE = 1, + BAR_KS_BAN = 2, + BAR_KS_ADDMUSIC = 3, + BAR_KS_CREATESTATION = 4, + BAR_KS_DELETESTATION = 5, + BAR_KS_EXPLAIN = 6, + BAR_KS_GENRESTATION = 7, + BAR_KS_HISTORY = 8, + BAR_KS_INFO = 9, + BAR_KS_ADDSHARED = 10, + BAR_KS_MOVESONG = 11, + BAR_KS_SKIP = 12, + BAR_KS_PLAYPAUSE = 13, + BAR_KS_QUIT = 14, + BAR_KS_RENAMESTATION = 15, + BAR_KS_SELECTSTATION = 16, + BAR_KS_TIRED = 17, + BAR_KS_UPCOMING = 18, + BAR_KS_SELECTQUICKMIX = 19, + BAR_KS_DEBUG = 20, + /* insert new shortcuts _before_ this element and increase its value */ + BAR_KS_COUNT = 21, +} BarKeyShortcutId_t; + +typedef struct { char *username; char *password; char *controlProxy; /* non-american listeners need this */ @@ -41,28 +69,18 @@ struct BarSettings { char *lastfmPassword; unsigned char lastfmScrobblePercent; char enableScrobbling; - struct BarKeyShortcut { - char key; - void (*cmd) (BAR_KS_ARGS); - char *description; - char *configKey; - struct BarKeyShortcut *next; - } *keys; + char keys[BAR_KS_COUNT]; PianoAudioFormat_t audioFormat; char *autostartStation; char *eventCmd; unsigned int history; -}; - -typedef struct BarSettings BarSettings_t; -typedef struct BarKeyShortcut BarKeyShortcut_t; - -void BarSettingsInit (BarSettings_t *settings); -void BarSettingsDestroy (BarSettings_t *settings); +} BarSettings_t; -void BarSettingsRead (BarSettings_t *settings); +typedef void (*BarKeyShortcutFunc_t) (BAR_KS_ARGS); -void BarGetXdgConfigDir (const char *filename, char *retDir, - size_t retDirN); +void BarSettingsInit (BarSettings_t *); +void BarSettingsDestroy (BarSettings_t *); +void BarSettingsRead (BarSettings_t *); +void BarGetXdgConfigDir (const char *, char *, size_t); #endif /* _SETTINGS_H */ diff --git a/src/ui_act.c b/src/ui_act.c index 5510b2f..518dc80 100644 --- a/src/ui_act.c +++ b/src/ui_act.c @@ -67,15 +67,36 @@ static int BarTransformIfShared (PianoHandle_t *ph, PianoStation_t *station) { /* print current shortcut configuration */ void BarUiActHelp (BAR_KS_ARGS) { - BarKeyShortcut_t *curShortcut = settings->keys; + const char *idToDesc[] = { + NULL, + "love current song", + "ban current song", + "add music to current station", + "create new station", + "delete current station", + "explain why this song is played", + "add genre station", + "song history", + "print information about current song/station", + "add shared station", + "move song to different station", + "next song", + "pause/continue", + "quit", + "rename current station", + "change station", + "tired (ban song for 1 month)", + "upcoming songs", + "select quickmix stations", + NULL, + }; + size_t i; BarUiMsg (MSG_NONE, "\r"); - while (curShortcut != NULL) { - if (curShortcut->description != NULL) { - BarUiMsg (MSG_LIST, "%c %s\n", curShortcut->key, - curShortcut->description); + for (i = 0; i < BAR_KS_COUNT; i++) { + if (idToDesc[i] != NULL) { + BarUiMsg (MSG_LIST, "%c %s\n", settings->keys[i], idToDesc[i]); } - curShortcut = curShortcut->next; } } |