OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FileMonitor.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2008-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 // An inotify wrapper
18 
19 #ifndef STD_FILE_MONITOR_H_INC
20 #define STD_FILE_MONITOR_H_INC
21 
23 #include "platform/StdScheduler.h"
24 
25 #ifdef __APPLE__
26 #import <CoreFoundation/CoreFoundation.h>
27 #import <CoreServices/CoreServices.h>
29 #endif
30 
32 #ifdef __APPLE__
33 , public ObjectiveCAssociated
34 #endif
35 {
36 
37 public:
38 
39  typedef void (*ChangeNotify)(const char *, const char *);
40 
41  C4FileMonitor(ChangeNotify pCallback);
42  ~C4FileMonitor() override;
43 
44  void StartMonitoring();
45  void AddDirectory(const char *szDir);
46 
47  // StdSchedulerProc:
48  bool Execute(int iTimeout = -1, pollfd * = nullptr) override;
49 
50  // Signal for calling Execute()
51 #ifdef STDSCHEDULER_USE_EVENTS
52  HANDLE GetEvent() override;
53 #else
54  void GetFDs(std::vector<struct pollfd> & FDs) override;
55 #endif
56 
57  // C4InteractiveThread::Callback:
58  void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) override;
59 
60 private:
61 
62  bool fStarted;
63  ChangeNotify pCallback;
64 
65 #ifdef HAVE_SYS_INOTIFY_H
66  int fd;
67  std::map<int, const char *> watch_descriptors;
68 #elif defined(_WIN32)
69 
70  HANDLE hEvent;
71 
72  struct TreeWatch
73  {
74  HANDLE hDir;
75  StdCopyStrBuf DirName;
76  OVERLAPPED ov;
77  char Buffer[1024];
78  TreeWatch *Next;
79  };
80  TreeWatch *pWatches;
81 
82  void HandleNotify(const char *szDir, const struct _FILE_NOTIFY_INFORMATION *pNotify);
83 #elif defined(__APPLE__)
84  FSEventStreamRef eventStream;
85  FSEventStreamContext context;
86  void StartStream();
87  void StopStream();
88 #endif
89 };
90 
91 #endif // STD_FILE_MONITOR_H_INC
void AddDirectory(const char *szDir)
~C4FileMonitor() override
void StartMonitoring()
void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) override
C4InteractiveEventType
C4FileMonitor(ChangeNotify pCallback)
void(* ChangeNotify)(const char *, const char *)
Definition: C4FileMonitor.h:39
bool Execute(int iTimeout=-1, pollfd *=nullptr) override
void GetFDs(std::vector< struct pollfd > &FDs) override