1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/private/fswatcher.h
3 // Purpose: File system watcher impl classes
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef WX_MSW_PRIVATE_FSWATCHER_H_
12 #define WX_MSW_PRIVATE_FSWATCHER_H_
14 #include "wx/filename.h"
15 #include "wx/vector.h"
16 #include "wx/msw/private.h"
18 // ============================================================================
19 // wxFSWatcherEntry implementation & helper declarations
20 // ============================================================================
22 class wxFSWatcherImplMSW
;
24 class wxFSWatchEntryMSW
: public wxFSWatchInfo
29 BUFFER_SIZE
= 4096 // TODO parametrize
32 wxFSWatchEntryMSW(const wxFSWatchInfo
& winfo
) :
35 // get handle for this path
36 m_handle
= OpenDir(m_path
);
37 m_overlapped
= (OVERLAPPED
*)calloc(1, sizeof(OVERLAPPED
));
38 wxZeroMemory(m_buffer
);
41 virtual ~wxFSWatchEntryMSW()
43 wxLogTrace(wxTRACE_FSWATCHER
, "Deleting entry '%s'", m_path
);
45 if (m_handle
!= INVALID_HANDLE_VALUE
)
47 if (!CloseHandle(m_handle
))
49 wxLogSysError(_("Unable to close the handle for '%s'"),
58 return m_handle
!= INVALID_HANDLE_VALUE
;
61 HANDLE
GetHandle() const
71 OVERLAPPED
* GetOverlapped() const
77 // opens dir with all flags, attributes etc. necessary to be later
78 // asynchronous watched with ReadDirectoryChangesW
79 static HANDLE
OpenDir(const wxString
& path
)
81 HANDLE handle
= CreateFile(path
.t_str(),
88 FILE_FLAG_BACKUP_SEMANTICS
|
91 if (handle
== INVALID_HANDLE_VALUE
)
93 wxLogSysError(_("Failed to open directory \"%s\" for monitoring."),
100 HANDLE m_handle
; // handle to opened directory
101 char m_buffer
[BUFFER_SIZE
]; // buffer for fs events
102 OVERLAPPED
* m_overlapped
;
104 wxDECLARE_NO_COPY_CLASS(wxFSWatchEntryMSW
);
108 // ============================================================================
109 // wxFSWatcherImplMSW helper classes implementations
110 // ============================================================================
116 m_iocp(INVALID_HANDLE_VALUE
)
123 if (m_iocp
!= INVALID_HANDLE_VALUE
)
125 if (!CloseHandle(m_iocp
))
127 wxLogSysError(_("Unable to close I/O completion port handle"));
133 // associates a wxFSWatchEntryMSW with completion port
134 bool Add(wxSharedPtr
<wxFSWatchEntryMSW
> watch
)
136 wxCHECK_MSG( m_iocp
!= INVALID_HANDLE_VALUE
, false, "IOCP not init" );
137 wxCHECK_MSG( watch
->IsOk(), false, "Invalid watch" );
139 // associate with IOCP
140 HANDLE ret
= CreateIoCompletionPort(watch
->GetHandle(), m_iocp
,
141 (ULONG_PTR
)watch
.get(), 0);
144 wxLogSysError(_("Unable to associate handle with "
145 "I/O completion port"));
148 else if (ret
!= m_iocp
)
150 wxFAIL_MSG(_("Unexpectedly new I/O completion port was created"));
155 wxFSWatchEntries::value_type
val(watch
->GetPath(), watch
);
156 return m_watches
.insert(val
).second
;
159 // post completion packet
160 bool PostEmptyStatus()
162 wxCHECK_MSG( m_iocp
!= INVALID_HANDLE_VALUE
, false, "IOCP not init" );
164 int ret
= PostQueuedCompletionStatus(m_iocp
, 0, 0, NULL
);
167 wxLogSysError(_("Unable to post completion status"));
173 // Wait for completion status to arrive.
174 // This function can block forever in it's wait for completion status.
175 // Use PostEmptyStatus() to wake it up (and end the worker thread)
176 bool GetStatus(unsigned long* count
, wxFSWatchEntryMSW
** watch
,
177 OVERLAPPED
** overlapped
)
179 wxCHECK_MSG( m_iocp
!= INVALID_HANDLE_VALUE
, false, "IOCP not init" );
180 wxCHECK_MSG( count
!= NULL
, false, "Null out parameter 'count'");
181 wxCHECK_MSG( watch
!= NULL
, false, "Null out parameter 'watch'");
182 wxCHECK_MSG( overlapped
!= NULL
, false,
183 "Null out parameter 'overlapped'");
185 int ret
= GetQueuedCompletionStatus(m_iocp
, count
, (ULONG_PTR
*)watch
,
186 overlapped
, INFINITE
);
189 wxLogSysError(_("Unable to dequeue completion packet"));
197 m_iocp
= CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL
, 0, 0);
200 wxLogSysError(_("Unable to create I/O completion port"));
202 return m_iocp
!= NULL
;
206 wxFSWatchEntries m_watches
;
210 class wxIOCPThread
: public wxThread
213 wxIOCPThread(wxFSWatcherImplMSW
* service
, wxIOCPService
* iocp
);
215 // finishes this thread
219 // structure to hold information needed to process one native event
220 // this is just a dummy holder, so it doesn't take ownership of it's data
221 struct wxEventProcessingData
223 wxEventProcessingData(const FILE_NOTIFY_INFORMATION
* ne
,
224 const wxFSWatchEntryMSW
* watch
) :
225 nativeEvent(ne
), watch(watch
)
228 const FILE_NOTIFY_INFORMATION
* nativeEvent
;
229 const wxFSWatchEntryMSW
* watch
;
232 virtual ExitCode
Entry();
234 // wait for events to occur, read them and send to interested parties
235 // returns false it empty status was read, which means we whould exit
239 void ProcessNativeEvents(wxVector
<wxEventProcessingData
>& events
);
241 void SendEvent(wxFileSystemWatcherEvent
& evt
);
243 static int Native2WatcherFlags(int flags
);
245 static wxString
FileNotifyInformationToString(
246 const FILE_NOTIFY_INFORMATION
& e
);
248 static wxFileName
GetEventPath(const wxFSWatchEntryMSW
& watch
,
249 const FILE_NOTIFY_INFORMATION
& e
);
251 wxFSWatcherImplMSW
* m_service
;
252 wxIOCPService
* m_iocp
;
255 #endif /* WX_MSW_PRIVATE_FSWATCHER_H_ */