From b0fa6db97d51f0b1af5d30799fd7f88bb53d368d Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Sun, 21 Jul 2013 23:52:35 -0400 Subject: [PATCH] Use an enum instead of hardcoded values for the user-injected codes --- src/inputmanager.cpp | 29 ++++++++++++++++++++--------- src/inputmanager.h | 7 +++++++ src/mediamonitor.cpp | 3 ++- src/monitor.cpp | 3 ++- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 2c0471b..aac15d2 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -178,18 +178,29 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) { break; #endif case SDL_USEREVENT: - if (!event.user.code) - menu->removePackageLink((const char *) event.user.data1); - else if (event.user.code == 1) - menu->openPackage((const char *) event.user.data1); - else if (event.user.code == 2) - menu->openPackagesFromDir( - ((string) (const char *) event.user.data1 - + "/apps").c_str()); - free(event.user.data1); + switch ((enum EventCode) event.user.code) { + case REMOVE_LINKS: + menu->removePackageLink((const char *) event.user.data1); + break; + case OPEN_PACKAGE: + menu->openPackage((const char *) event.user.data1); + break; + case OPEN_PACKAGES_FROM_DIR: + menu->openPackagesFromDir( + ((string) (const char *) event.user.data1 + + "/apps").c_str()); + break; + case REPAINT_MENU: + default: + break; + } + + if (event.user.data1) + free(event.user.data1); bevent->state = PRESSED; bevent->button = REPAINT; return true; + default: return false; } diff --git a/src/inputmanager.h b/src/inputmanager.h index 8323b62..84ac588 100644 --- a/src/inputmanager.h +++ b/src/inputmanager.h @@ -26,6 +26,13 @@ class Menu; +enum EventCode { + REMOVE_LINKS, + OPEN_PACKAGE, + OPEN_PACKAGES_FROM_DIR, + REPAINT_MENU, +}; + class InputManager { public: enum Button { diff --git a/src/mediamonitor.cpp b/src/mediamonitor.cpp index 90bc1a1..164cd0e 100644 --- a/src/mediamonitor.cpp +++ b/src/mediamonitor.cpp @@ -4,6 +4,7 @@ #include #include "debug.h" +#include "inputmanager.h" #include "mediamonitor.h" MediaMonitor::MediaMonitor(std::string dir) : @@ -21,7 +22,7 @@ void MediaMonitor::inject_event(bool is_add, const char *path) { SDL_UserEvent e = { .type = SDL_USEREVENT, - .code = is_add ? 2 : 0, + .code = is_add ? OPEN_PACKAGES_FROM_DIR : REMOVE_LINKS, .data1 = strdup(path), .data2 = NULL, }; diff --git a/src/monitor.cpp b/src/monitor.cpp index 754dba6..47bc33c 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -8,13 +8,14 @@ #include #include +#include "inputmanager.h" #include "monitor.h" void Monitor::inject_event(bool is_add, const char *path) { SDL_UserEvent e = { .type = SDL_USEREVENT, - .code = (int) is_add, + .code = is_add ? OPEN_PACKAGE : REMOVE_LINKS, .data1 = strdup(path), .data2 = NULL, };