1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 09:09:43 +02: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 "inputmanager.h"
#include "utilities.h"
#include <SDL.h>
#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*/)
{
std::shared_ptr<Clock::Timer> 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.

View File

@ -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 */

View File

@ -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;

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);
// 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 <vector>
#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<std::string> &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