Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : /* 24 : * portable directory change notification API. 25 : */ 26 : 27 : #ifndef INCLUDED_DIR_WATCH 28 : #define INCLUDED_DIR_WATCH 29 : 30 : #include "lib/os_path.h" 31 : #include "lib/status.h" 32 : 33 : #include <vector> 34 : #include <memory> 35 : 36 : struct DirWatch; 37 : typedef std::shared_ptr<DirWatch> PDirWatch; 38 : 39 : /** 40 : * start watching a single directory for changes. 41 : * 42 : * @param path (must end in slash) 43 : * @param dirWatch opaque smart pointer to the watch state; used to 44 : * manage its lifetime (this is deemed more convenient than a 45 : * separate dir_watch_Remove interface). 46 : * 47 : * clients typically want to watch entire directory subtrees (e.g. a mod), 48 : * which is supported by Windows but not FAM. to reduce overhead, the 49 : * Windows backend always watches subtrees, but portable clients should 50 : * still add a watch for each subdirectory (the shared watch state is 51 : * reference-counted). 52 : * rationale: since the VFS has per-directory data structures, it is 53 : * convenient to store PDirWatch there instead of creating a second 54 : * tree structure here. 55 : **/ 56 : Status dir_watch_Add(const OsPath& path, PDirWatch& dirWatch); 57 : 58 36 : class DirWatchNotification 59 : { 60 : public: 61 : enum EType 62 : { 63 : Created, 64 : Deleted, 65 : Changed 66 : }; 67 : 68 9 : DirWatchNotification(const OsPath& pathname, EType type) 69 9 : : pathname(pathname), type(type) 70 : { 71 : } 72 : 73 18 : const OsPath& Pathname() const 74 : { 75 18 : return pathname; 76 : } 77 : 78 : EType Type() const 79 : { 80 : return type; 81 : } 82 : 83 : private: 84 : OsPath pathname; 85 : EType type; 86 : }; 87 : 88 : typedef std::vector<DirWatchNotification> DirWatchNotifications; 89 : 90 : /** 91 : * return all pending directory watch notifications. 92 : * 93 : * @param notifications receives any pending notifications in unspecified order. 94 : * @return Status (INFO::OK doesn't imply notifications were returned) 95 : * 96 : * note: the run time of this function is independent of the number of 97 : * directory watches and number of files. 98 : * 99 : * rationale for a polling interface: users (e.g. the main game loop) 100 : * typically want to receive change notifications at a single point, 101 : * rather than deal with the complexity of asynchronous notifications. 102 : **/ 103 : Status dir_watch_Poll(DirWatchNotifications& notifications); 104 : 105 : #endif // #ifndef INCLUDED_DIR_WATCH