diff --git a/macos/ScreenHandler.mm b/macos/ScreenHandler.mm index b7075e6..6b0d30e 100644 --- a/macos/ScreenHandler.mm +++ b/macos/ScreenHandler.mm @@ -5,6 +5,7 @@ #import "../common/commands.h" #import "Permissions.h" #import +#import #import #import #import @@ -104,6 +105,51 @@ bool ScreenHandler::init() m_currFrame.resize(m_bmpHeader.biSizeImage, 0); m_diffBuffer.resize(1 + 1 + 8 + 1 + m_bmpHeader.biSizeImage * 2); + // Wake display if needed (do this early, before sending TOKEN_BITMAPINFO) + bool wasAsleep = CGDisplayIsAsleep(m_displayID); + bool isLocked = false; + CFDictionaryRef sessionInfo = CGSessionCopyCurrentDictionary(); + if (sessionInfo) { + CFBooleanRef screenLocked = (CFBooleanRef)CFDictionaryGetValue( + sessionInfo, CFSTR("CGSSessionScreenIsLocked")); + if (screenLocked && CFBooleanGetValue(screenLocked)) { + isLocked = true; + } + CFRelease(sessionInfo); + } + + if (wasAsleep || isLocked) { + NSLog(@"Waking display in init (asleep=%d, locked=%d)...", wasAsleep, isLocked); + + // Create NoDisplaySleep assertion - this wakes the display + if (m_displayAssertionID == 0) { + IOReturn result = IOPMAssertionCreateWithName( + kIOPMAssertionTypeNoDisplaySleep, + kIOPMAssertionLevelOn, + CFSTR("SimpleRemoter - remote desktop session active"), + &m_displayAssertionID + ); + if (result == kIOReturnSuccess) { + NSLog(@"Display assertion created (ID: %u)", m_displayAssertionID); + } + } + + // Declare user activity to ensure wake + IOPMAssertionID wakeAssertionID = 0; + IOPMAssertionDeclareUserActivity( + CFSTR("SimpleRemoter - waking display"), + kIOPMUserActiveLocal, + &wakeAssertionID + ); + if (wakeAssertionID) { + IOPMAssertionRelease(wakeAssertionID); + } + + // Brief wait for loginwindow to render + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + NSLog(@"Display wake complete"); + } + NSLog(@"ScreenHandler initialized: %dx%d", m_width, m_height); return true; } @@ -116,7 +162,7 @@ void ScreenHandler::start(IOCPClient* client, uint64_t clientID) m_clientID = clientID; m_running = true; - // Prevent display sleep during remote desktop session + // Display wake was already done in init(), just ensure assertion exists if (m_displayAssertionID == 0) { IOReturn result = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep, @@ -125,9 +171,7 @@ void ScreenHandler::start(IOCPClient* client, uint64_t clientID) &m_displayAssertionID ); if (result == kIOReturnSuccess) { - NSLog(@"Display sleep disabled for remote desktop (ID: %u)", m_displayAssertionID); - } else { - NSLog(@"Warning: Failed to prevent display sleep (error: 0x%x)", result); + NSLog(@"Display sleep disabled (ID: %u)", m_displayAssertionID); } }