public IOKitMotionSensorCoords getCoords() { sbyte[] bc = IOKit.IOConnectCallStructMethod(connect); return(new IOKitMotionSensorCoords { x = bc[0], y = bc[1], z = bc[2] }); }
public IOKitMotionSensor() { var matchingDictionary = IOKit.IOServiceMatching("SMCMotionSensor"); using (var iterator = IOKit.IOServiceGetMatchingServices(IOKit.IOMasterPort(), matchingDictionary)) { using (var service = IOKit.IOIteratorNext(iterator)) { connect = IOKit.IOServiceOpen(service); } } }
internal static sbyte[] IOConnectCallStructMethod(IOKitObject connect) { uint osize = 40, isize = 40; sbyte[] @out = new sbyte[osize]; if (IOConnectCallStructMethodIn == null) { IOConnectCallStructMethodIn = new sbyte[isize]; for (int i = 0; i < isize; i++) { IOConnectCallStructMethodIn [i] = 1; } } IOKit.CheckResult(IOKit.IOConnectCallStructMethod( connect, 5, IOConnectCallStructMethodIn, osize, @out, ref isize )); return(@out); }
static void Main() { var sensor = new IOKitMotionSensor(); Int16 prevz = 0, currz; bool inited = false, sleeping = false; while (true) { var coords = sensor.getCoords(); currz = coords.z; if (inited) { if (Math.Abs(currz - prevz) > Threshold) { if (prevz > currz && !sleeping && Math.Abs(coords.x) >= Angle) { IOKit.SleepAwake(IOKit.ASCommands.Sleep); sleeping = true; } else if (sleeping) { if (Math.Abs(coords.x) < Angle && Math.Abs(coords.y) < Angle) { IOKit.SleepAwake(IOKit.ASCommands.Awake); sleeping = false; } } } } else { inited = true; } prevz = currz; Thread.Sleep(100); } }
public void Dispose() { IOKit.IOObjectRelease(value); }
internal static void SleepAwake(ASCommands cmd) { using (var r = IOKit.IORegistryEntryFromPath("IOService:/IOResources/IODisplayWrangler")) { IOKit.IORegistryEntrySetCFProperty(r, "IORequestIdle", cmd == ASCommands.Sleep); } }