2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2004 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 13:06:25 +00:00
|
|
|
#import "platform/OSXScreenSaver.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#import "base/IEventQueue.h"
|
|
|
|
|
#import "base/Log.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#import "deskflow/IPrimaryScreen.h"
|
2014-02-28 13:06:25 +00:00
|
|
|
#import "platform/OSXScreenSaverUtil.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#import <string.h>
|
2014-02-25 15:03:43 +00:00
|
|
|
#import <sys/sysctl.h>
|
|
|
|
|
|
2014-02-25 19:10:54 +00:00
|
|
|
// TODO: upgrade deprecated function usage in these functions.
|
2024-07-02 19:07:06 +00:00
|
|
|
void getProcessSerialNumber(const char *name, ProcessSerialNumber &psn);
|
|
|
|
|
bool isScreenSaverEngine(const ProcessSerialNumber &psn);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// OSXScreenSaver
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
OSXScreenSaver::OSXScreenSaver(IEventQueue *events, void *eventTarget)
|
2024-07-16 13:36:44 +00:00
|
|
|
: m_eventTarget(eventTarget),
|
|
|
|
|
m_enabled(true),
|
2024-10-17 18:04:35 +00:00
|
|
|
m_events(events)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_autoReleasePool = screenSaverUtilCreatePool();
|
|
|
|
|
m_screenSaverController = screenSaverUtilCreateController();
|
|
|
|
|
|
|
|
|
|
// install launch/termination event handlers
|
|
|
|
|
EventTypeSpec launchEventTypes[2];
|
|
|
|
|
launchEventTypes[0].eventClass = kEventClassApplication;
|
|
|
|
|
launchEventTypes[0].eventKind = kEventAppLaunched;
|
|
|
|
|
launchEventTypes[1].eventClass = kEventClassApplication;
|
|
|
|
|
launchEventTypes[1].eventKind = kEventAppTerminated;
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EventHandlerUPP launchTerminationEventHandler = NewEventHandlerUPP(launchTerminationCallback);
|
2024-07-16 13:36:44 +00:00
|
|
|
InstallApplicationEventHandler(
|
2024-10-17 18:04:35 +00:00
|
|
|
launchTerminationEventHandler, 2, launchEventTypes, this, &m_launchTerminationEventHandlerRef
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
DisposeEventHandlerUPP(launchTerminationEventHandler);
|
|
|
|
|
|
|
|
|
|
m_screenSaverPSN.highLongOfPSN = 0;
|
|
|
|
|
m_screenSaverPSN.lowLongOfPSN = 0;
|
|
|
|
|
|
|
|
|
|
if (isActive()) {
|
|
|
|
|
getProcessSerialNumber("ScreenSaverEngine", m_screenSaverPSN);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
OSXScreenSaver::~OSXScreenSaver()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
RemoveEventHandler(m_launchTerminationEventHandlerRef);
|
|
|
|
|
// screenSaverUtilReleaseController(m_screenSaverController);
|
|
|
|
|
screenSaverUtilReleasePool(m_autoReleasePool);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::enable()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_enabled = true;
|
|
|
|
|
screenSaverUtilEnable(m_screenSaverController);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::disable()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_enabled = false;
|
|
|
|
|
screenSaverUtilDisable(m_screenSaverController);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::activate()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
screenSaverUtilActivate(m_screenSaverController);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::deactivate()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
screenSaverUtilDeactivate(m_screenSaverController, m_enabled);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool OSXScreenSaver::isActive() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return (screenSaverUtilIsActive(m_screenSaverController) != 0);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::processLaunched(ProcessSerialNumber psn)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (isScreenSaverEngine(psn)) {
|
|
|
|
|
m_screenSaverPSN = psn;
|
|
|
|
|
LOG((CLOG_DEBUG1 "screen saver engine launched, enabled=%d", m_enabled));
|
|
|
|
|
if (m_enabled) {
|
2024-10-17 18:04:35 +00:00
|
|
|
m_events->addEvent(Event(m_events->forIPrimaryScreen().screensaverActivated(), m_eventTarget));
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXScreenSaver::processTerminated(ProcessSerialNumber psn)
|
|
|
|
|
{
|
|
|
|
|
if (m_screenSaverPSN.highLongOfPSN == psn.highLongOfPSN && m_screenSaverPSN.lowLongOfPSN == psn.lowLongOfPSN) {
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_DEBUG1 "screen saver engine terminated, enabled=%d", m_enabled));
|
|
|
|
|
if (m_enabled) {
|
2024-10-17 18:04:35 +00:00
|
|
|
m_events->addEvent(Event(m_events->forIPrimaryScreen().screensaverDeactivated(), m_eventTarget));
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
m_screenSaverPSN.highLongOfPSN = 0;
|
|
|
|
|
m_screenSaverPSN.lowLongOfPSN = 0;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
pascal OSStatus
|
|
|
|
|
OSXScreenSaver::launchTerminationCallback(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
OSStatus result;
|
|
|
|
|
ProcessSerialNumber psn;
|
|
|
|
|
EventParamType actualType;
|
|
|
|
|
ByteCount actualSize;
|
|
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
result = GetEventParameter(
|
2024-10-17 18:04:35 +00:00
|
|
|
theEvent, kEventParamProcessID, typeProcessSerialNumber, &actualType, sizeof(psn), &actualSize, &psn
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
if ((result == noErr) && (actualSize > 0) && (actualType == typeProcessSerialNumber)) {
|
2024-07-02 19:07:06 +00:00
|
|
|
OSXScreenSaver *screenSaver = (OSXScreenSaver *)userData;
|
|
|
|
|
UInt32 eventKind = GetEventKind(theEvent);
|
|
|
|
|
if (eventKind == kEventAppLaunched) {
|
|
|
|
|
screenSaver->processLaunched(psn);
|
|
|
|
|
} else if (eventKind == kEventAppTerminated) {
|
|
|
|
|
screenSaver->processTerminated(psn);
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
return (CallNextEventHandler(nextHandler, theEvent));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2014-02-25 15:03:43 +00:00
|
|
|
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void getProcessSerialNumber(const char *name, ProcessSerialNumber &psn)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ProcessInfoRec procInfo;
|
|
|
|
|
Str31 procName; // pascal string. first byte holds length.
|
|
|
|
|
memset(&procInfo, 0, sizeof(procInfo));
|
|
|
|
|
procInfo.processName = procName;
|
|
|
|
|
procInfo.processInfoLength = sizeof(ProcessInfoRec);
|
|
|
|
|
|
|
|
|
|
ProcessSerialNumber checkPsn;
|
|
|
|
|
OSErr err = GetNextProcess(&checkPsn);
|
|
|
|
|
while (err == 0) {
|
|
|
|
|
memset(procName, 0, sizeof(procName));
|
|
|
|
|
err = GetProcessInformation(&checkPsn, &procInfo);
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(name, (const char *)&procName[1]) == 0) {
|
|
|
|
|
psn = checkPsn;
|
|
|
|
|
break;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
err = GetNextProcess(&checkPsn);
|
|
|
|
|
}
|
2014-02-25 15:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool isScreenSaverEngine(const ProcessSerialNumber &psn)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
CFStringRef processName;
|
|
|
|
|
OSStatus err = CopyProcessName(&psn, &processName);
|
|
|
|
|
bool result = (err == 0 && CFEqual(CFSTR("ScreenSaverEngine"), processName));
|
|
|
|
|
CFRelease(processName);
|
2022-05-16 16:00:16 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return result;
|
2014-02-25 15:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-26 15:53:28 +00:00
|
|
|
#pragma GCC diagnostic error "-Wdeprecated-declarations"
|