private static IOConnect IORegisterForSystemPower(IOServiceInterestCallback systemPowerDelegate, NSObject refcon) { var ioNotificationPort = System.IntPtr.Zero; var notifierHandle = System.IntPtr.Zero; var callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(systemPowerDelegate); var nativeIOConnect = NativeMethods.IORegisterForSystemPower(refcon.Handle, out ioNotificationPort, callback, out notifierHandle); var ioConnect = IOKitObject.CreateIOKitObject <IOConnect>(nativeIOConnect); ioConnect.NotificationPort = new IONotificationPort(ioNotificationPort); ioConnect.Notifier = IOKitObject.CreateIOKitObject <IOKitObject>(notifierHandle, n => IODeregisterForSystemPower(n)); return(ioConnect); }
public IOObject AddInterestNotification(IONotificationPort notifyPort, InterestType interestType, InterestCallback callback) { ThrowIfDisposed(); var interestTypeString = interestType.GetKey(); IOServiceInterestCallback nativeCallback = (refCon, service, messageType, messageArgument) => { if (service != Handle) { return; } callback.Invoke(this, messageType, messageArgument); }; callbackStore.Add(nativeCallback); IntPtr notification; var result = IOServiceAddInterestNotification(notifyPort.Handle, Handle, interestTypeString, nativeCallback, IntPtr.Zero, out notification); ThrowIfError(result); return(new IOObject(notification, true)); }
/// <inheritdoc/> internal override void StartInThread() { NotificationPort = new IONotificationPort(); Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeDefault); var systemPowerDelegate = new IOServiceInterestCallback(SystemPowerInterestCallback); IOConnectionPort = IOConnect.CreateSystemPowerMonitorConnection(systemPowerDelegate, this); Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, IOConnectionPort.NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeCommon); var servicesDictionary = IOMachPort.GetRS232SerialMatchDictionary(); #if __UNIFIED__ servicesDictionary.DangerousRetain(); // retain an extra time because we're using it twice #else servicesDictionary.Retain(); // retain an extra time because we're using it twice #endif // __UNIFIED__ var publishDelegate = new IONotificationPortCallback(FirstMatchNotification); var callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(publishDelegate); IntPtr iterator; var result = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOFirstMatchNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator); System.Diagnostics.Debug.Assert(result == NativeMethods.Success, "IOService.IOKitNotificationPort: Failed to add notification."); if (result == NativeMethods.Success) { PublishNotificationIterator = new IOIterator(iterator); var terminateDelegate = new IONotificationPortCallback(TerminateNotification); callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(terminateDelegate); result = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOTerminatedNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator); TerminateNotificationIterator = new IOIterator(iterator); // The iterators returned when adding the matching notifications must be iterated to // completion in order to arm the notifications. Otherwise, they will never fire. PublishNotificationIterator.EnumerateSerialPorts(null); TerminateNotificationIterator.EnumerateSerialPorts(null); } }
/// <summary> /// Creates the system power monitor connection. /// </summary> /// <param name="systemPowerDelegate">System power delegate.</param> /// <param name="callbackData">Callback data.</param> /// <returns>The system power monitor connection.</returns> /// <remarks>The caller must add the <see cref="IOConnect.NotificationPort"/> to a CFRunLoop in order for /// <paramref name="systemPowerDelegate"/> to be called. When the consumer of the returned IO connection /// is finished, first, remove the NotificationPort from the CFRunLoop, then call the Dispose() method.</remarks> public static IOConnect CreateSystemPowerMonitorConnection(IOServiceInterestCallback systemPowerDelegate, NSObject callbackData) { return(IORegisterForSystemPower(systemPowerDelegate, callbackData)); }
extern static kern_return_t IOServiceAddInterestNotification(IONotificationPortRef notifyPort, io_service_t service, io_name_t interestType, IOServiceInterestCallback callback, IntPtr refCon, out io_object_t notification);
public static extern IOKitError IOServiceAddInterestNotification(IntPtr notifyPort, IntPtr service, string interestType, IOServiceInterestCallback callback, DeviceWatcherCallbackReference refCon, ref IntPtr notification);