#ifndef FILEMONITOR_FREEBSD_INCLUDED
#define FILEMONITOR_FREEBSD_INCLUDED

/* ### LINUX-BLOCK-BEGIN (FileMonitor_FreeBSD.h kqueue-only) #############
 * This class is a kqueue(2) client: <sys/event.h>, struct kevent, EV_SET().
 * None of that exists on Linux (the equivalent would be inotify), so the
 * whole body is confined to __FreeBSD__ and src/Makefile drops the .cpp from
 * the Linux CPPFILE list.
 *
 * Nothing is lost.  The feature is already dead on every platform: the master
 * switch `//#define __FILEMONITOR__' (main.cpp:71) is commented out, both
 * uses - the Update() call at main.cpp:323 and the #include at main.cpp:74 -
 * sit behind `#if defined(__FreeBSD__) && defined(__FILEMONITOR__)', and
 * every AddWatch() call site (main.cpp:589, 590, 606) is commented out, so
 * m_FileLists is always empty and Update() early-returns.
 *
 * It was not ported because it is also incorrect as written: AddWatch()
 * push_back()s an uninitialised `struct kevent' onto m_TriggeredEventLists,
 * Update() passes a NULL timeout to kevent() - which blocks indefinitely, and
 * it is called from heartbeat(), so enabling it would freeze the game loop -
 * and it tests NOTE_* constants against kevent::flags when they belong to
 * kevent::fflags.  Porting the bugs to inotify would be worse than leaving it.
 *
 * IFileMonitor.h is deliberately NOT touched: it has no BSD content and is
 * pulled in transitively by desc_manager.h (and XTrapManager.h). */
#if !defined(__linux__)
/* ### LINUX-BLOCK-END (FileMonitor_FreeBSD.h kqueue-only) ############### */

#include "IFileMonitor.h"
#include <unistd.h>
#include <sys/event.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/time.h>

struct FileIOContext_FreeBSD
{
	int		fhMonitor;
	int		idxToEventList;	// evtTrigger & evtMonitor index should be same	
	PFN_FileChangeListener pListenFunc;
};

class FileMonitorFreeBSD : public IFileMonitor
{
private:
	FileMonitorFreeBSD(); //hidden

public:
	virtual ~FileMonitorFreeBSD();

	void AddWatch	(const std::string& strFileName, PFN_FileChangeListener pListenerFunc); 
	void Update		(DWORD dwPulses);

	static FileMonitorFreeBSD& Instance() 
	{
		static FileMonitorFreeBSD theMonitor;
		return theMonitor;
	}

private:
	typedef std::unordered_map<std::string, FileIOContext_FreeBSD> TMonitorFileHashMap;
	typedef std::vector<struct kevent>	TEventList;
	
	TMonitorFileHashMap	m_FileLists;
	TEventList			m_MonitoredEventLists;
	TEventList			m_TriggeredEventLists;

	int					m_KernelEventQueue;
};

/* ### LINUX-BLOCK-BEGIN (FileMonitor_FreeBSD.h kqueue-only) ############# */
#endif // !__linux__
/* ### LINUX-BLOCK-END (FileMonitor_FreeBSD.h kqueue-only) ############### */

#endif //FILEMONITOR_FREEBSD_INCLUDED
