Projekt

Obecné

Profil

Stáhnout (2.45 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
/*
2
** © 2014 by Philipp Dunkel <pip@pipobscure.com>
3
** Licensed under MIT License.
4
*/
5
6
// constants from https://developer.apple.com/library/mac/documentation/Darwin/Reference/FSEvents_Ref/index.html#//apple_ref/doc/constant_group/FSEventStreamCreateFlags
7
#ifndef kFSEventStreamCreateFlagNone
8
#define kFSEventStreamCreateFlagNone 0x00000000
9
#endif
10
11
#ifndef kFSEventStreamCreateFlagUseCFTypes
12
#define kFSEventStreamCreateFlagUseCFTypes 0x00000001
13
#endif
14
15
#ifndef kFSEventStreamCreateFlagNoDefer
16
#define kFSEventStreamCreateFlagNoDefer 0x00000002
17
#endif
18
19
#ifndef kFSEventStreamCreateFlagWatchRoot
20
#define kFSEventStreamCreateFlagWatchRoot 0x00000004
21
#endif
22
23
#ifndef kFSEventStreamCreateFlagIgnoreSelf
24
#define kFSEventStreamCreateFlagIgnoreSelf 0x00000008
25
#endif
26
27
#ifndef kFSEventStreamCreateFlagFileEvents
28
#define kFSEventStreamCreateFlagFileEvents 0x00000010
29
#endif
30
31
void FSEvents::threadStart() {
32
  if (threadloop) return;
33
  if (uv_thread_create(&thread, &FSEvents::threadRun, this)) abort();
34
}
35
36
void HandleStreamEvents(ConstFSEventStreamRef stream, void *ctx, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
37
  FSEvents * fse = (FSEvents *)ctx;
38
  size_t idx;
39
  uv_mutex_lock(&fse->mutex);
40
  for (idx=0; idx < numEvents; idx++) {
41
    fse_event *event = new fse_event(
42
        (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)eventPaths, idx),
43
        eventFlags[idx],
44
        eventIds[idx]
45
      );
46
    fse->events.push_back(event);
47
  }
48
  fse->asyncTrigger();
49
  uv_mutex_unlock(&fse->mutex);
50
}
51
52
void FSEvents::threadRun(void *ctx) {
53
  FSEvents *fse = (FSEvents*)ctx;
54
  FSEventStreamContext context = { 0, ctx, NULL, NULL, NULL };
55
  fse->threadloop = CFRunLoopGetCurrent();
56
  FSEventStreamRef stream = FSEventStreamCreate(NULL, &HandleStreamEvents, &context, fse->paths, kFSEventStreamEventIdSinceNow, (CFAbsoluteTime) 0.1, kFSEventStreamCreateFlagNone | kFSEventStreamCreateFlagWatchRoot | kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagUseCFTypes);
57
  FSEventStreamScheduleWithRunLoop(stream, fse->threadloop, kCFRunLoopDefaultMode);
58
  FSEventStreamStart(stream);
59
  CFRunLoopRun();
60
  FSEventStreamStop(stream);
61
  FSEventStreamUnscheduleFromRunLoop(stream, fse->threadloop, kCFRunLoopDefaultMode);
62
  FSEventStreamInvalidate(stream);
63
  FSEventStreamRelease(stream);
64
  fse->threadloop = NULL;
65
}
66
67
void FSEvents::threadStop() {
68
  if (!threadloop) return;
69
  CFRunLoopStop(threadloop);
70
  if (uv_thread_join(&thread)) abort();
71
}