Use FSEvents in wxFileSystemWatcher on OS X
The FSEvents API allows for creating watches in entire trees of directories in an efficient manner. Closes #16969.
This commit is contained in:
committed by
Dimitri Schoolwerth
parent
f0e098fa06
commit
aa5dbad410
@@ -50,6 +50,16 @@
|
||||
#define wxOSX_USE_QUICKTIME 0
|
||||
#define wxOSX_USE_AUDIOTOOLBOX 1
|
||||
|
||||
/*
|
||||
Use the more efficient FSEvents API instead of kqueue
|
||||
events for file system watcher, but only on OS X >= 10.7 since
|
||||
that version introduced a flag that allows watching files as
|
||||
well as sub directories.
|
||||
*/
|
||||
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
|
||||
#define wxHAVE_FSEVENTS_FILE_NOTIFICATIONS 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* turning off capabilities that don't work under cocoa yet
|
||||
*/
|
||||
|
88
include/wx/osx/fswatcher_fsevents.h
Normal file
88
include/wx/osx/fswatcher_fsevents.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/osx/fswatcher_fsevents.h
|
||||
// Purpose: File System watcher that uses the FSEvents API
|
||||
// of OS X to efficiently watch trees
|
||||
// Author: Roberto Perpuly
|
||||
// Created: 2015-04-24
|
||||
// Copyright: (c) 2015 Roberto Perpuly <robertop2004@gmail.com>
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _WX_FSWATCHER_FSEVENTS_H_
|
||||
#define _WX_FSWATCHER_FSEVENTS_H_
|
||||
|
||||
#include "wx/defs.h"
|
||||
|
||||
#if wxUSE_FSWATCHER
|
||||
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include "wx/unix/fswatcher_kqueue.h"
|
||||
|
||||
WX_DECLARE_STRING_HASH_MAP(FSEventStreamRef, FSEventStreamRefMap);
|
||||
|
||||
/*
|
||||
The FSEvents watcher uses the newer FSEvents service
|
||||
that is available in OS X, the service allows for
|
||||
efficient watching of entire directory hierarchies.
|
||||
Note that adding a single file watch (or directory
|
||||
watch) still use kqueue events.
|
||||
|
||||
We take care to only use this on OS X >= 10.7, as that
|
||||
version introduced the ability to get file-level notifications.
|
||||
|
||||
See the following docs that outline the FSEvents API
|
||||
|
||||
https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/FSEvents_ProgGuide/UsingtheFSEventsFramework/UsingtheFSEventsFramework.html
|
||||
|
||||
https://developer.apple.com/library/mac/documentation/Darwin/Reference/FSEvents_Ref/index.html
|
||||
*/
|
||||
class WXDLLIMPEXP_BASE wxFsEventsFileSystemWatcher :
|
||||
public wxKqueueFileSystemWatcher
|
||||
{
|
||||
public:
|
||||
wxFsEventsFileSystemWatcher();
|
||||
|
||||
wxFsEventsFileSystemWatcher(const wxFileName& path,
|
||||
int events = wxFSW_EVENT_ALL);
|
||||
|
||||
~wxFsEventsFileSystemWatcher();
|
||||
|
||||
// reimplement adding a tree so that it does not use
|
||||
// kqueue at all
|
||||
bool AddTree(const wxFileName& path, int events = wxFSW_EVENT_ALL,
|
||||
const wxString& filespec = wxEmptyString) wxOVERRIDE;
|
||||
|
||||
// reimplement removing a tree so that we
|
||||
// cleanup the opened fs streams
|
||||
bool RemoveTree(const wxFileName& path) wxOVERRIDE;
|
||||
|
||||
// reimplement remove all so that we cleanup
|
||||
// watches from kqeueue and from FSEvents
|
||||
bool RemoveAll() wxOVERRIDE;
|
||||
|
||||
// post an file change event to the owner
|
||||
void PostChange(const wxFileName& oldFileName,
|
||||
const wxFileName& newFileName, int event);
|
||||
|
||||
// post a warning event to the owner
|
||||
void PostWarning(wxFSWWarningType warning, const wxString& msg);
|
||||
|
||||
// post an error event to the owner
|
||||
void PostError(const wxString& msg);
|
||||
|
||||
// reimplement count to include the FS stream watches
|
||||
int GetWatchedPathsCount() const;
|
||||
|
||||
// reimplement to include paths from FS stream watches
|
||||
int GetWatchedPaths(wxArrayString* paths) const;
|
||||
|
||||
private:
|
||||
|
||||
// map of path => FSEventStreamRef
|
||||
FSEventStreamRefMap m_streams;
|
||||
|
||||
};
|
||||
|
||||
#endif /* wxUSE_FSWATCHER */
|
||||
|
||||
#endif /* _WX_FSWATCHER_FSEVENTS_H_ */
|
Reference in New Issue
Block a user