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:
parent
cc869c07a8
commit
f308ed983b
@ -10,47 +10,12 @@
|
|||||||
|
|
||||||
#include "monitor.h"
|
#include "monitor.h"
|
||||||
|
|
||||||
static void * inotify_thd(void *p)
|
void Monitor::inject_event(bool is_add, const char *path)
|
||||||
{
|
{
|
||||||
const char *path = (const char *) p;
|
|
||||||
int wd, fd;
|
|
||||||
|
|
||||||
DEBUG("Starting inotify thread for path %s...\n", path);
|
|
||||||
|
|
||||||
fd = inotify_init();
|
|
||||||
if (fd == -1) {
|
|
||||||
ERROR("Unable to start inotify\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
wd = inotify_add_watch(fd, path, IN_MOVED_FROM | IN_MOVED_TO |
|
|
||||||
IN_CLOSE_WRITE | IN_DELETE | IN_CREATE);
|
|
||||||
if (wd == -1) {
|
|
||||||
ERROR("Unable to add inotify watch\n");
|
|
||||||
close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG("Starting watching directory %s\n", path);
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
size_t len = sizeof(struct inotify_event) + NAME_MAX + 1;
|
|
||||||
struct inotify_event event;
|
|
||||||
char buf[256];
|
|
||||||
|
|
||||||
read(fd, &event, len);
|
|
||||||
sprintf(buf, "%s/%s", path, event.name);
|
|
||||||
|
|
||||||
/* Don't bother other files than OPKs */
|
|
||||||
len = strlen(event.name);
|
|
||||||
if (len < 5 || strncmp(event.name + len - 4, ".opk", 4))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
SDL_UserEvent e = {
|
SDL_UserEvent e = {
|
||||||
.type = SDL_USEREVENT,
|
.type = SDL_USEREVENT,
|
||||||
.code = (int) (event.mask &
|
.code = (int) is_add,
|
||||||
(IN_MOVED_TO | IN_CLOSE_WRITE | IN_CREATE)),
|
.data1 = strdup(path),
|
||||||
.data1 = strdup(buf),
|
|
||||||
.data2 = NULL,
|
.data2 = NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -58,11 +23,64 @@ static void * inotify_thd(void *p)
|
|||||||
* event by the InputManager */
|
* event by the InputManager */
|
||||||
SDL_PushEvent((SDL_Event *) &e);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
Monitor::Monitor(std::string path) : path(path)
|
int Monitor::run(void)
|
||||||
{
|
{
|
||||||
pthread_create(&thd, NULL, inotify_thd, (void *) path.c_str());
|
int wd, fd;
|
||||||
|
|
||||||
|
DEBUG("Starting inotify thread for path %s...\n", path.c_str());
|
||||||
|
|
||||||
|
fd = inotify_init();
|
||||||
|
if (fd < 0) {
|
||||||
|
ERROR("Unable to start inotify\n");
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
wd = inotify_add_watch(fd, path.c_str(), mask);
|
||||||
|
if (wd < 0) {
|
||||||
|
ERROR("Unable to add inotify watch\n");
|
||||||
|
close(fd);
|
||||||
|
return wd;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG("Starting watching directory %s\n", path.c_str());
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
size_t len = sizeof(struct inotify_event) + NAME_MAX + 1;
|
||||||
|
struct inotify_event event;
|
||||||
|
char buf[256];
|
||||||
|
|
||||||
|
read(fd, &event, len);
|
||||||
|
sprintf(buf, "%s/%s", path.c_str(), event.name);
|
||||||
|
|
||||||
|
if (!event_accepted(event))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
inject_event(event.mask & (IN_MOVED_TO | IN_CLOSE_WRITE |
|
||||||
|
IN_CREATE), buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void * inotify_thd(void *p)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
@ -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__ */
|
||||||
|
Loading…
Reference in New Issue
Block a user