From 5d8fb6520f646107c94b2b96fe1beb087b1b9bd0 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 28 Aug 2013 13:14:08 -0400 Subject: [PATCH] Code factorisation (Add function inject_user_event() to utilities) --- src/clock.cpp | 18 +++--------------- src/mediamonitor.cpp | 17 +++++------------ src/monitor.cpp | 18 ++++++------------ src/utilities.cpp | 15 +++++++++++++++ src/utilities.h | 5 +++++ 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/clock.cpp b/src/clock.cpp index e496b3d..92ad25d 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -2,6 +2,7 @@ #include "debug.h" #include "inputmanager.h" +#include "utilities.h" #include #include @@ -45,20 +46,6 @@ static std::shared_ptr globalTimerInstance() } } -static void notify() -{ - SDL_UserEvent e = { - .type = SDL_USEREVENT, - .code = REPAINT_MENU, - .data1 = NULL, - .data2 = NULL, - }; - - /* Inject an user event, that will be handled as a "repaint" - * event by the InputManager */ - SDL_PushEvent((SDL_Event *) &e); -} - extern "C" Uint32 callbackFunc(Uint32 /*timeout*/, void */*d*/) { std::shared_ptr timer = globalTimer.lock(); @@ -126,7 +113,8 @@ unsigned int Clock::Timer::update() unsigned int Clock::Timer::callback() { unsigned int ms = update(); - notify(); + inject_user_event(); + // TODO: SDL timer forgets adjusted interval if a timer was inserted or // removed during the callback. So we should either fix that bug // in SDL or ensure we don't insert/remove timers at runtime. diff --git a/src/mediamonitor.cpp b/src/mediamonitor.cpp index 164cd0e..05848e1 100644 --- a/src/mediamonitor.cpp +++ b/src/mediamonitor.cpp @@ -6,6 +6,7 @@ #include "debug.h" #include "inputmanager.h" #include "mediamonitor.h" +#include "utilities.h" MediaMonitor::MediaMonitor(std::string dir) : Monitor(dir, IN_MOVE | IN_DELETE | IN_CREATE | IN_ONLYDIR) @@ -20,22 +21,14 @@ bool MediaMonitor::event_accepted( void MediaMonitor::inject_event(bool is_add, const char *path) { - SDL_UserEvent e = { - .type = SDL_USEREVENT, - .code = is_add ? OPEN_PACKAGES_FROM_DIR : REMOVE_LINKS, - .data1 = strdup(path), - .data2 = NULL, - }; - /* Sleep for a bit, to ensure that the media will be mounted * on the mountpoint before we start looking for OPKs */ sleep(1); - DEBUG("MediaMonitor: Injecting event code %i\n", e.code); - - /* Inject an user event, that will be handled as a "repaint" - * event by the InputManager */ - SDL_PushEvent((SDL_Event *) &e); + if (is_add) + inject_user_event(OPEN_PACKAGES_FROM_DIR, strdup(path)); + else + inject_user_event(REMOVE_LINKS, strdup(path)); } #endif /* ENABLE_INOTIFY */ diff --git a/src/monitor.cpp b/src/monitor.cpp index 47bc33c..29f5ed3 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -10,19 +10,14 @@ #include "inputmanager.h" #include "monitor.h" +#include "utilities.h" void Monitor::inject_event(bool is_add, const char *path) { - SDL_UserEvent e = { - .type = SDL_USEREVENT, - .code = is_add ? OPEN_PACKAGE : REMOVE_LINKS, - .data1 = strdup(path), - .data2 = NULL, - }; - - /* Inject an user event, that will be handled as a "repaint" - * event by the InputManager */ - SDL_PushEvent((SDL_Event *) &e); + if (is_add) + inject_user_event(OPEN_PACKAGE, strdup(path)); + else + inject_user_event(REMOVE_LINKS, strdup(path)); } bool Monitor::event_accepted(struct inotify_event &event) @@ -70,8 +65,7 @@ int Monitor::run(void) if (!event_accepted(event)) continue; - inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE | - IN_CREATE), buf); + inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE | IN_CREATE), buf); } return 0; diff --git a/src/utilities.cpp b/src/utilities.cpp index 7f5d243..8f1ae57 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -162,3 +162,18 @@ int intTransition(int from, int to, long tickStart, long duration, long tickNow) return constrain(((tickNow-tickStart) * (to-from)) / duration, from, to); // elapsed increments } + +void inject_user_event(enum EventCode code, void *data1, void *data2) +{ + SDL_UserEvent e = { + .type = SDL_USEREVENT, + .code = code, + .data1 = data1, + .data2 = data2, + }; + + /* Inject an user event, that will be handled as a "repaint" + * event by the InputManager */ + SDL_PushEvent((SDL_Event *) &e); + DEBUG("Injecting event code %i\n", e.code); +} diff --git a/src/utilities.h b/src/utilities.h index 7df8aff..573080a 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -24,6 +24,8 @@ #include #include +#include "inputmanager.h" + class case_less { public: bool operator()(const std::string &left, const std::string &right) const; @@ -47,4 +49,7 @@ bool split(std::vector &vec, const std::string &str, int intTransition(int from, int to, long int tickStart, long duration=500, long tickNow=-1); +void inject_user_event(enum EventCode code = REPAINT_MENU, + void *data1 = NULL, void *data2 = NULL); + #endif // UTILITIES_H