1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 20:34:11 +03:00

Code factorisation (Add function inject_user_event() to utilities)

This commit is contained in:
Paul Cercueil 2013-08-28 13:14:08 -04:00
parent 3ff6dc93f2
commit 5d8fb6520f
5 changed files with 34 additions and 39 deletions

View File

@ -2,6 +2,7 @@
#include "debug.h" #include "debug.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "utilities.h"
#include <SDL.h> #include <SDL.h>
#include <atomic> #include <atomic>
@ -45,20 +46,6 @@ static std::shared_ptr<Clock::Timer> 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*/) extern "C" Uint32 callbackFunc(Uint32 /*timeout*/, void */*d*/)
{ {
std::shared_ptr<Clock::Timer> timer = globalTimer.lock(); std::shared_ptr<Clock::Timer> timer = globalTimer.lock();
@ -126,7 +113,8 @@ unsigned int Clock::Timer::update()
unsigned int Clock::Timer::callback() unsigned int Clock::Timer::callback()
{ {
unsigned int ms = update(); unsigned int ms = update();
notify(); inject_user_event();
// TODO: SDL timer forgets adjusted interval if a timer was inserted or // TODO: SDL timer forgets adjusted interval if a timer was inserted or
// removed during the callback. So we should either fix that bug // removed during the callback. So we should either fix that bug
// in SDL or ensure we don't insert/remove timers at runtime. // in SDL or ensure we don't insert/remove timers at runtime.

View File

@ -6,6 +6,7 @@
#include "debug.h" #include "debug.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "mediamonitor.h" #include "mediamonitor.h"
#include "utilities.h"
MediaMonitor::MediaMonitor(std::string dir) : MediaMonitor::MediaMonitor(std::string dir) :
Monitor(dir, IN_MOVE | IN_DELETE | IN_CREATE | IN_ONLYDIR) 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) 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 /* Sleep for a bit, to ensure that the media will be mounted
* on the mountpoint before we start looking for OPKs */ * on the mountpoint before we start looking for OPKs */
sleep(1); sleep(1);
DEBUG("MediaMonitor: Injecting event code %i\n", e.code); if (is_add)
inject_user_event(OPEN_PACKAGES_FROM_DIR, strdup(path));
/* Inject an user event, that will be handled as a "repaint" else
* event by the InputManager */ inject_user_event(REMOVE_LINKS, strdup(path));
SDL_PushEvent((SDL_Event *) &e);
} }
#endif /* ENABLE_INOTIFY */ #endif /* ENABLE_INOTIFY */

View File

@ -10,19 +10,14 @@
#include "inputmanager.h" #include "inputmanager.h"
#include "monitor.h" #include "monitor.h"
#include "utilities.h"
void Monitor::inject_event(bool is_add, const char *path) void Monitor::inject_event(bool is_add, const char *path)
{ {
SDL_UserEvent e = { if (is_add)
.type = SDL_USEREVENT, inject_user_event(OPEN_PACKAGE, strdup(path));
.code = is_add ? OPEN_PACKAGE : REMOVE_LINKS, else
.data1 = strdup(path), inject_user_event(REMOVE_LINKS, strdup(path));
.data2 = NULL,
};
/* Inject an user event, that will be handled as a "repaint"
* event by the InputManager */
SDL_PushEvent((SDL_Event *) &e);
} }
bool Monitor::event_accepted(struct inotify_event &event) bool Monitor::event_accepted(struct inotify_event &event)
@ -70,8 +65,7 @@ int Monitor::run(void)
if (!event_accepted(event)) if (!event_accepted(event))
continue; continue;
inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE | inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE | IN_CREATE), buf);
IN_CREATE), buf);
} }
return 0; return 0;

View File

@ -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); return constrain(((tickNow-tickStart) * (to-from)) / duration, from, to);
// elapsed increments // 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);
}

View File

@ -24,6 +24,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "inputmanager.h"
class case_less { class case_less {
public: public:
bool operator()(const std::string &left, const std::string &right) const; bool operator()(const std::string &left, const std::string &right) const;
@ -47,4 +49,7 @@ bool split(std::vector<std::string> &vec, const std::string &str,
int intTransition(int from, int to, long int tickStart, long duration=500, int intTransition(int from, int to, long int tickStart, long duration=500,
long tickNow=-1); long tickNow=-1);
void inject_user_event(enum EventCode code = REPAINT_MENU,
void *data1 = NULL, void *data2 = NULL);
#endif // UTILITIES_H #endif // UTILITIES_H