source: launchers/macosx/obj-cpp/PidWatcher.h @ f9fbc76

Last change on this file since f9fbc76 was f9fbc76, checked in by meeh <meeh@…>, 2 years ago

Code changes:

  • Port check to see if i2p is already running.
  • All buttons in the menu should work now.
  • Rewrote some C++ to Objective-C, which fits better with the integration.
  • Misc cleanup.
  • Made the launch of router repeatable.
  • Property mode set to 100644
File size: 1.8 KB
Line 
1#pragma once
2
3#include <CoreFoundation/CoreFoundation.h>
4#include <unistd.h>
5#include <sys/event.h>
6
7#include "neither/either.hpp"
8#include "AppDelegate.h"
9
10using callbackType = void (CFFileDescriptorRef, CFOptionFlags, void *);
11using HandleFunction = std::function<void(int)>;
12
13static void noteProcDeath(CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) {
14    struct kevent kev;
15    int fd = CFFileDescriptorGetNativeDescriptor(fdref);
16    kevent(fd, NULL, 0, &kev, 1, NULL);
17    // take action on death of process here
18    NSLog(@"process with pid '%u' died\n", (unsigned int)kev.ident);
19    sendUserNotification(APP_IDSTR, @"The I2P router has stopped.");
20    CFFileDescriptorInvalidate(fdref);
21    CFRelease(fdref); // the CFFileDescriptorRef is no longer of any use in this example
22}
23// one argument, an integer pid to watch, required
24int watchPid(int pid, callbackType callback = noteProcDeath) {
25    int fd = kqueue();
26    struct kevent kev;
27    EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ENABLE, NOTE_EXIT, 0, NULL);
28    kevent(fd, &kev, 1, NULL, 0, NULL);
29    CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd, true, callback, NULL);
30    CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack);
31    CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0);
32    CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode);
33    CFRelease(source);
34    /*
35    seconds
36    The length of time to run the run loop. If 0, only one pass is made through the run loop before returning;
37    if multiple sources or timers are ready to fire immediately, only one (possibly two if one is a version
38    0 source) will be fired, regardless of the value of returnAfterSourceHandled.
39    */
40    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
41    return 0;
42}
Note: See TracBrowser for help on using the repository browser.