1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-12-29 13:28:37 +02:00

Prepare the Monitor class for proper inheritance

This commit is contained in:
Paul Cercueil 2013-07-18 21:32:23 -04:00
parent cc869c07a8
commit f308ed983b
2 changed files with 59 additions and 30 deletions

View File

@ -10,28 +10,47 @@
#include "monitor.h" #include "monitor.h"
static void * inotify_thd(void *p) void Monitor::inject_event(bool is_add, const char *path)
{
SDL_UserEvent e = {
.type = SDL_USEREVENT,
.code = (int) is_add,
.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);
}
bool Monitor::event_accepted(struct inotify_event &event)
{
/* Don't bother other files than OPKs */
size_t len = strlen(event.name);
return len >= 5 && !strncmp(event.name + len - 4, ".opk", 4);
}
int Monitor::run(void)
{ {
const char *path = (const char *) p;
int wd, fd; int wd, fd;
DEBUG("Starting inotify thread for path %s...\n", path); DEBUG("Starting inotify thread for path %s...\n", path.c_str());
fd = inotify_init(); fd = inotify_init();
if (fd == -1) { if (fd < 0) {
ERROR("Unable to start inotify\n"); ERROR("Unable to start inotify\n");
return NULL; return fd;
} }
wd = inotify_add_watch(fd, path, IN_MOVED_FROM | IN_MOVED_TO | wd = inotify_add_watch(fd, path.c_str(), mask);
IN_CLOSE_WRITE | IN_DELETE | IN_CREATE); if (wd < 0) {
if (wd == -1) {
ERROR("Unable to add inotify watch\n"); ERROR("Unable to add inotify watch\n");
close(fd); close(fd);
return NULL; return wd;
} }
DEBUG("Starting watching directory %s\n", path); DEBUG("Starting watching directory %s\n", path.c_str());
for (;;) { for (;;) {
size_t len = sizeof(struct inotify_event) + NAME_MAX + 1; size_t len = sizeof(struct inotify_event) + NAME_MAX + 1;
@ -39,30 +58,29 @@ static void * inotify_thd(void *p)
char buf[256]; char buf[256];
read(fd, &event, len); read(fd, &event, len);
sprintf(buf, "%s/%s", path, event.name); sprintf(buf, "%s/%s", path.c_str(), event.name);
/* Don't bother other files than OPKs */ if (!event_accepted(event))
len = strlen(event.name);
if (len < 5 || strncmp(event.name + len - 4, ".opk", 4))
continue; continue;
SDL_UserEvent e = { inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE |
.type = SDL_USEREVENT, IN_CREATE), buf);
.code = (int) (event.mask &
(IN_MOVED_TO | IN_CLOSE_WRITE | IN_CREATE)),
.data1 = strdup(buf),
.data2 = NULL,
};
/* Inject an user event, that will be handled as a "repaint"
* event by the InputManager */
SDL_PushEvent((SDL_Event *) &e);
} }
return 0;
} }
Monitor::Monitor(std::string path) : path(path) static void * inotify_thd(void *p)
{ {
pthread_create(&thd, NULL, inotify_thd, (void *) path.c_str()); Monitor *monitor = (Monitor *) p;
monitor->run();
return NULL;
}
Monitor::Monitor(std::string path, unsigned int flags) : path(path)
{
mask = flags;
pthread_create(&thd, NULL, inotify_thd, (void *) this);
} }
Monitor::~Monitor(void) Monitor::~Monitor(void)

View File

@ -1,17 +1,28 @@
#ifndef __MONITOR_H__ #ifndef __MONITOR_H__
#define __MONITOR_H__ #define __MONITOR_H__
#ifdef ENABLE_INOTIFY
#include <string>
#include <pthread.h> #include <pthread.h>
#include <string>
#include <sys/inotify.h>
class Monitor { class Monitor {
public: public:
Monitor(std::string path); Monitor(std::string path, unsigned int flags = IN_MOVE |
~Monitor(); IN_CLOSE_WRITE | IN_DELETE | IN_CREATE);
virtual ~Monitor();
int run(void);
private: private:
std::string path; std::string path;
pthread_t thd; pthread_t thd;
protected:
unsigned int mask;
virtual bool event_accepted(struct inotify_event &event);
virtual void inject_event(bool is_add, const char *path);
}; };
#endif
#endif /* __MONITOR_H__ */ #endif /* __MONITOR_H__ */