82 VUserLog(
"Error: VuoEventLoop_processEvent must be called from the main thread.");
87 NSAutoreleasePool *pool = [NSAutoreleasePool new];
97 NSEvent *
event = [*nsAppGlobal nextEventMatchingMask:NSEventMaskAny
98 untilDate:(mode == VuoEventLoop_WaitIndefinitely ? [NSDate distantFuture] : [NSDate distantPast])
99 inMode:NSDefaultRunLoopMode
101 [*nsAppGlobal sendEvent:event];
102 [*nsAppGlobal updateWindows];
108 if (mode == VuoEventLoop_WaitIndefinitely)
111 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0,
false);
128 dispatch_async(dispatch_get_main_queue(), ^{
129 NSEvent *killswitch = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
130 location:NSMakePoint(0,0)
138 [*nsAppGlobal postEvent:killswitch atStart:NO];
144 CFRunLoopStop(CFRunLoopGetMain());
229 vuoStopCompositionType
vuoStopComposition = (vuoStopCompositionType)dlsym(RTLD_SELF,
"vuoStopComposition");
231 vuoStopComposition = (vuoStopCompositionType)dlsym(RTLD_DEFAULT,
"vuoStopComposition");
234 VUserLog(
"Warning: Couldn't find vuoStopComposition symbol; not installing clean-shutdown signal handlers.");
239 signal(SIGINT, SIG_IGN);
240 signal(SIGQUIT, SIG_IGN);
241 signal(SIGTERM, SIG_IGN);
245 void (^stop)(void) = ^{
248 dispatch_source_t sigintSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, dispatch_get_main_queue());
249 dispatch_source_t sigquitSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGQUIT, 0, dispatch_get_main_queue());
250 dispatch_source_t sigtermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
251 dispatch_source_set_event_handler(sigintSource, stop);
252 dispatch_source_set_event_handler(sigquitSource, stop);
253 dispatch_source_set_event_handler(sigtermSource, stop);
254 dispatch_resume(sigintSource);
255 dispatch_resume(sigquitSource);
256 dispatch_resume(sigtermSource);
264 if (messageType != kIOPMMessageSystemPowerEventOccurred)
268 IOPMCopyCPUPowerStatus(&d);
269 CFNumberRef cpuSpeedLimitCF = CFDictionaryGetValue(d, CFSTR(kIOPMCPUPowerLimitProcessorSpeedKey));
270 int cpuSpeedLimit = -1;
271 CFNumberGetValue(cpuSpeedLimitCF, kCFNumberIntType, &cpuSpeedLimit);
274 if (cpuSpeedLimit >= 0)
275 VUserLog(
"The system changed the CPU speed limit to %d%%.", cpuSpeedLimit);
284 void (^logThermalState)(
int thermalState) = ^(
int thermalState){
285 if (thermalState == 0)
287 else if (thermalState == 1)
288 VUserLog(
"thermalState = fair (\"fans audible\")");
289 else if (thermalState == 2)
290 VUserLog(
"thermalState = serious (\"fans at maximum speed\")");
291 else if (thermalState == 3)
292 VUserLog(
"thermalState = critical (\"system needs to cool down\")");
294 if ([NSProcessInfo.processInfo respondsToSelector:
@selector(thermalState)])
296 logThermalState(((
int (*)(
id,
SEL))objc_msgSend)(NSProcessInfo.processInfo,
@selector(thermalState)));
297 [NSNotificationCenter.defaultCenter addObserverForName:@"NSProcessInfoThermalStateDidChangeNotification" object:nil queue:nil usingBlock:^(NSNotification *note){
298 logThermalState(((int (*)(id, SEL))objc_msgSend)(note.object, @selector(thermalState)));
304 io_service_t rootDomain = IORegistryEntryFromPath(kIOMasterPortDefault, kIOPowerPlane
":/IOPowerConnection/IOPMrootDomain");
305 IONotificationPortRef notePort = IONotificationPortCreate(MACH_PORT_NULL);
306 if (rootDomain && notePort)
308 io_object_t notification_object = MACH_PORT_NULL;
309 IOReturn ret = IOServiceAddInterestNotification(notePort, rootDomain, kIOGeneralInterest,
VuoShowSystemPowerEvent, NULL, ¬ification_object);
310 if (ret == kIOReturnSuccess)
312 CFRunLoopSourceRef runLoopSrc = IONotificationPortGetRunLoopSource(notePort);
314 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSrc, kCFRunLoopDefaultMode);
325 dispatch_source_t memoryPressureWatcher = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_NORMAL|DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
326 dispatch_source_set_event_handler(memoryPressureWatcher, ^{
327 int pressure = dispatch_source_get_data(memoryPressureWatcher);
328 if (pressure == DISPATCH_MEMORYPRESSURE_NORMAL)
329 VUserLog(
"memoryPressure = normal");
330 else if (pressure == DISPATCH_MEMORYPRESSURE_WARN)
331 VUserLog(
"memoryPressure = warning");
332 else if (pressure == DISPATCH_MEMORYPRESSURE_CRITICAL)
333 VUserLog(
"memoryPressure = critical");
335 dispatch_resume(memoryPressureWatcher);
343 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceWillSleepNotification object:nil queue:nil usingBlock:^(NSNotification *note){
344 VUserLog("The system is going to sleep.");
345 VuoEventLoop_systemAsleep = true;
347 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceDidWakeNotification object:nil queue:nil usingBlock:^(NSNotification *note){
348 VUserLog("The system is waking up.");
349 VuoEventLoop_systemAsleep = false;
351 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceWillPowerOffNotification object:nil queue:nil usingBlock:^(NSNotification *note){
352 VUserLog("The system is powering off");
354 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceScreensDidSleepNotification object:nil queue:nil usingBlock:^(NSNotification *note){
355 VUserLog("The screens are going to sleep.");
357 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceScreensDidWakeNotification object:nil queue:nil usingBlock:^(NSNotification *note){
358 VUserLog("The screens are waking up.");
360 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceSessionDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note){
361 VUserLog("The system is switching back to this user account.");
363 [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceSessionDidResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note){
364 VUserLog("The system is switching away to another user account.");
366 [NSNotificationCenter.defaultCenter addObserverForName:NSSystemClockDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note){
367 VUserLog("The system's clock changed to %s", NSDate.date.description.UTF8String);