PageMeta: add Command attribute
authorMax Kellermann <max@musicpd.org>
Mon, 10 Sep 2018 12:05:43 +0000 (14:05 +0200)
committerMax Kellermann <max@musicpd.org>
Mon, 10 Sep 2018 12:11:10 +0000 (14:11 +0200)
Look up the command in the page table which saves some code in
ScreenManager::OnCommand().

14 files changed:
src/ChatPage.cxx
src/FileBrowserPage.cxx
src/HelpPage.cxx
src/LyricsPage.cxx
src/OutputsPage.cxx
src/PageMeta.hxx
src/QueuePage.cxx
src/SearchPage.cxx
src/SongPage.cxx
src/screen.cxx
src/screen_artist.cxx
src/screen_keydef.cxx
src/screen_list.cxx
src/screen_list.hxx

index 60f8009..888a294 100644 (file)
@@ -189,5 +189,6 @@ ChatPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_chat = {
        "chat",
+       Command::SCREEN_CHAT,
        screen_chat_init,
 };
index 92abed4..5d39488 100644 (file)
@@ -393,6 +393,7 @@ FileBrowserPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_browse = {
        "browse",
+       Command::SCREEN_FILE,
        screen_file_init,
 };
 
index f670b07..2f55ef1 100644 (file)
@@ -309,5 +309,6 @@ HelpPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_help = {
        "help",
+       Command::SCREEN_HELP,
        help_init,
 };
index 2640b04..870e44b 100644 (file)
@@ -485,6 +485,7 @@ LyricsPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_lyrics = {
        "lyrics",
+       Command::SCREEN_LYRICS,
        lyrics_screen_init,
 };
 
index a6e6b1a..9283c1e 100644 (file)
@@ -203,5 +203,6 @@ OutputsPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_outputs = {
        "outputs",
+       Command::SCREEN_OUTPUTS,
        outputs_init,
 };
index e320ce9..72665ae 100644 (file)
 #include "ncmpc_curses.h"
 #include "Size.hxx"
 
+enum class Command : unsigned;
 class Page;
 class ScreenManager;
 
 struct PageMeta {
        const char *name;
+
+       /**
+        * The command which switches to this page.
+        */
+       Command command;
+
        Page *(*init)(ScreenManager &screen, WINDOW *w, Size size);
 };
 
index 8aa61db..2edba5b 100644 (file)
@@ -671,5 +671,6 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_queue = {
        "playlist",
+       Command::SCREEN_PLAY,
        screen_queue_init,
 };
index a18d64f..310a75f 100644 (file)
@@ -472,5 +472,6 @@ SearchPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_search = {
        "search",
+       Command::SCREEN_SEARCH,
        screen_search_init,
 };
index cbb4150..8feed4a 100644 (file)
@@ -551,6 +551,7 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_song = {
        "song",
+       Command::SCREEN_SONG,
        screen_song_init,
 };
 
index d9f4f1b..cb7b567 100644 (file)
 #include "mpdclient.hxx"
 #include "options.hxx"
 #include "player_command.hxx"
-#include "HelpPage.hxx"
-#include "QueuePage.hxx"
-#include "FileBrowserPage.hxx"
-#include "screen_artist.hxx"
-#include "SearchPage.hxx"
 #include "SongPage.hxx"
-#include "screen_keydef.hxx"
 #include "LyricsPage.hxx"
-#include "OutputsPage.hxx"
-#include "ChatPage.hxx"
 
 #include <mpd/client.h>
 
@@ -254,6 +246,12 @@ ScreenManager::OnCommand(struct mpdclient &c, Command cmd)
        if (handle_player_command(c, cmd))
                return;
 
+       const auto *new_page = PageByCommand(cmd);
+       if (new_page != nullptr) {
+               Switch(*new_page, c);
+               return;
+       }
+
        switch(cmd) {
        case Command::TOGGLE_FIND_WRAP:
                options.find_wrap = !options.find_wrap;
@@ -276,52 +274,6 @@ ScreenManager::OnCommand(struct mpdclient &c, Command cmd)
        case Command::SCREEN_NEXT:
                NextMode(c, 1);
                break;
-       case Command::SCREEN_PLAY:
-               Switch(screen_queue, c);
-               break;
-       case Command::SCREEN_FILE:
-               Switch(screen_browse, c);
-               break;
-#ifdef ENABLE_HELP_SCREEN
-       case Command::SCREEN_HELP:
-               Switch(screen_help, c);
-               break;
-#endif
-#ifdef ENABLE_SEARCH_SCREEN
-       case Command::SCREEN_SEARCH:
-               Switch(screen_search, c);
-               break;
-#endif
-#ifdef ENABLE_ARTIST_SCREEN
-       case Command::SCREEN_ARTIST:
-               Switch(screen_artist, c);
-               break;
-#endif
-#ifdef ENABLE_SONG_SCREEN
-       case Command::SCREEN_SONG:
-               Switch(screen_song, c);
-               break;
-#endif
-#ifdef ENABLE_KEYDEF_SCREEN
-       case Command::SCREEN_KEYDEF:
-               Switch(screen_keydef, c);
-               break;
-#endif
-#ifdef ENABLE_LYRICS_SCREEN
-       case Command::SCREEN_LYRICS:
-               Switch(screen_lyrics, c);
-               break;
-#endif
-#ifdef ENABLE_OUTPUTS_SCREEN
-       case Command::SCREEN_OUTPUTS:
-               Switch(screen_outputs, c);
-               break;
-#endif
-#ifdef ENABLE_CHAT_SCREEN
-       case Command::SCREEN_CHAT:
-               Switch(screen_chat, c);
-               break;
-#endif
        case Command::SCREEN_SWAP:
                Swap(c, nullptr);
                break;
index 8f0640f..4c2bbb9 100644 (file)
@@ -295,5 +295,6 @@ ArtistBrowserPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_artist = {
        "artist",
+       Command::SCREEN_ARTIST,
        screen_artist_init,
 };
index d5c9afd..dda6faf 100644 (file)
@@ -602,5 +602,6 @@ KeyDefPage::OnCommand(struct mpdclient &c, Command cmd)
 
 const PageMeta screen_keydef = {
        "keydef",
+       Command::SCREEN_KEYDEF,
        keydef_init,
 };
index 899f358..1ed2217 100644 (file)
@@ -71,3 +71,13 @@ screen_lookup_name(const char *name)
 
        return nullptr;
 }
+
+const PageMeta *
+PageByCommand(Command cmd)
+{
+       for (const auto *i : screens)
+               if (i->command == cmd)
+                       return i;
+
+       return nullptr;
+}
index e5f0961..c98f4a6 100644 (file)
 
 #include "Compiler.h"
 
+enum class Command : unsigned;
 struct PageMeta;
 
 gcc_pure
 const PageMeta *
 screen_lookup_name(const char *name);
 
+gcc_const
+const PageMeta *
+PageByCommand(Command cmd);
+
 #endif