/* $Id: e2_fs_FAM_inotify.h 2064 2010-03-12 13:15:36Z tpgww $

Copyright (C) 2005-2009 tooar <tooar@emelfm2.net>
Portions copyright (C) 2005 John McCutchan

This file is part of emelFM2.
emelFM2 is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

emelFM2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with emelFM2; see the file GPL. If not, see http://www.gnu.org/licenses.
*/

#ifndef __E2_FS_FAMINOTIFY_H__
#define __E2_FS_FAMINOTIFY_H__

#include <sys/inotify.h>
#ifdef _SYS_INOTIFY_H

/* when monitoring a dir we ignore these inotify events:
IN_CLOSE_WRITE this should be covered by IN_MODIFY
IN_CLOSE_NOWRITE
IN_OPEN we're monitoring dirs and config file and don't care about opening
and we always get these, so don't need to ask
IN_UNMOUNT
IN_Q_OVERFLOW
IN_IGNORED (item no longer watched, likely due to its deletion (or replacement))
*/
#define INDIR_FLAGS \
IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_MOVED_FROM | IN_MOVED_TO | \
IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF
/* flags used when limited refreshing is in effect */
#define INDIR_SHORT_FLAGS \
IN_MODIFY | IN_ATTRIB | IN_MOVED_FROM | IN_MOVED_TO | \
IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF
/* flags used to supplement INDIR_SHORT_FLAGS to get back to full operation */
#define INDIR_XTRA_FLAGS IN_ACCESS

/*for the config file we're interested only in events that would trigger a
reload of that file, ie modification or creation. Not deletion, as a replacement
may not yet be available.
If something renames the file (whether or not it then creates a replacement version),
the renamed file will continue to be monotored unless IN_MOVED* are checked.
However, in normal course of events, the file is deleted and then replaced, and
deletion causes a IN_IGNORED report to be generated, and monitoring ceases to be
effective - so no IN_CREATE report is generated, normally.
*/
#define INCFG_FLAGS \
IN_MODIFY | IN_MOVE_SELF


#include <stdint.h>
#include <unistd.h>

#undef __NR_inotify_init
#undef __NR_inotify_add_watch
#undef __NR_inotify_rm_watch

#if defined (__alpha__)
# define __NR_inotify_init      444
# define __NR_inotify_add_watch 445
# define __NR_inotify_rm_watch  446

#elif defined (__arm__)
# define __NR_inotify_init      (__NR_SYSCALL_BASE+316)
# define __NR_inotify_add_watch (__NR_SYSCALL_BASE+317)
# define __NR_inotify_rm_watch  (__NR_SYSCALL_BASE+318)

#elif defined (__frv__)
# define __NR_inotify_init      291
# define __NR_inotify_add_watch 292
# define __NR_inotify_rm_watch  293

#elif defined(__i386__)
# define __NR_inotify_init      291
# define __NR_inotify_add_watch 292
# define __NR_inotify_rm_watch  293

#elif defined (__ia64__)
# define __NR_inotify_init      1277
# define __NR_inotify_add_watch 1278
# define __NR_inotify_rm_watch  1279

#elif defined (__mips__)
# if _MIPS_SIM == _MIPS_SIM_ABI32
#  define __NR_inotify_init      (__NR_Linux + 284)
#  define __NR_inotify_add_watch (__NR_Linux + 285)
#  define __NR_inotify_rm_watch  (__NR_Linux + 286)
# endif
# if _MIPS_SIM == _MIPS_SIM_ABI64
#  define __NR_inotify_init      (__NR_Linux + 243)
#  define __NR_inotify_add_watch (__NR_Linux + 243)
#  define __NR_inotify_rm_watch  (__NR_Linux + 243)
# endif
# if _MIPS_SIM == _MIPS_SIM_NABI32
#  define __NR_inotify_init      (__NR_Linux + 247)
#  define __NR_inotify_add_watch (__NR_Linux + 248)
#  define __NR_inotify_rm_watch  (__NR_Linux + 249)
# endif

#elif defined(__parisc__)
# define __NR_inotify_init      (__NR_Linux + 269)
# define __NR_inotify_add_watch (__NR_Linux + 270)
# define __NR_inotify_rm_watch  (__NR_Linux + 271)

#elif defined(__powerpc__) || defined(__powerpc64__)
# define __NR_inotify_init      275
# define __NR_inotify_add_watch 276
# define __NR_inotify_rm_watch  277

#elif defined (__s390__)
# define __NR_inotify_init      284
# define __NR_inotify_add_watch 285
# define __NR_inotify_rm_watch  286

#elif defined (__sh__)
# define __NR_inotify_init      290
# define __NR_inotify_add_watch 291
# define __NR_inotify_rm_watch  292

#elif defined (__sh64__)
# define __NR_inotify_init      318
# define __NR_inotify_add_watch 319
# define __NR_inotify_rm_watch  320

#elif defined (__sparc__) || defined (__sparc64__)
# define __NR_inotify_init      151
# define __NR_inotify_add_watch 152
# define __NR_inotify_rm_watch  156

#elif defined(__x86_64__)
# define __NR_inotify_init      253
# define __NR_inotify_add_watch 254
# define __NR_inotify_rm_watch  255

#elif defined(__cris__)
# define __NR_inotify_init      291
# define __NR_inotify_add_watch 292
# define __NR_inotify_rm_watch  293

#endif

#ifdef __NR_inotify_init
#include <sys/syscall.h>
#define inotify_init(a) syscall(__NR_inotify_init)
#define inotify_add_watch(a,b,c) syscall(__NR_inotify_add_watch,a,b,c)
#define inotify_rm_watch(a,b) syscall(__NR_inotify_rm_watch,a,b)
#else
#warning "Unsupported architecture"
#define inotify_init(a) -1
#define inotify_add_watch(a,b,c) -1
#define inotify_rm_watch(a,b) -1
#endif  //supported arch

#else
#warning "No inotify header"
#define inotify_init(a) -1
#define inotify_add_watch(a,b,c) -1
#define inotify_rm_watch(a,b) -1
#endif //def _SYS_INOTIFY_H

#endif //ndef __E2_FS_FAMINOTIFY_H__
