polybar/include/utils/inotify.hpp

49 lines
832 B
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include <poll.h>
2016-11-25 07:42:31 +00:00
#include <sys/inotify.h>
2016-06-15 03:32:35 +00:00
#include <cstdio>
#include "common.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
struct inotify_event {
string filename;
bool is_dir;
int wd = 0;
int cookie = 0;
int mask = 0;
};
2016-11-02 19:22:45 +00:00
namespace inotify_util {
using event_t = inotify_event;
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
class inotify_watch {
public:
2016-12-15 02:30:41 +00:00
explicit inotify_watch(string path);
2016-11-02 19:22:45 +00:00
~inotify_watch() noexcept;
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
void attach(int mask = IN_MODIFY);
2016-12-01 07:41:49 +00:00
void remove(bool force = false);
2016-11-02 19:22:45 +00:00
bool poll(int wait_ms = 1000);
unique_ptr<event_t> get_event();
bool await_match();
2016-11-02 19:22:45 +00:00
const string path() const;
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
protected:
string m_path;
int m_fd = -1;
int m_wd = -1;
int m_mask = 0;
2016-11-02 19:22:45 +00:00
};
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
using watch_t = unique_ptr<inotify_watch>;
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
watch_t make_watch(string path);
bool match(const event_t* evt, int mask);
2016-06-15 03:32:35 +00:00
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END