Re: Preventing screen-saver from triggering ?
Re: Preventing screen-saver from triggering ?
- Subject: Re: Preventing screen-saver from triggering ?
- From: Rolf <email@hidden>
- Date: Tue, 18 Nov 2003 22:49:53 +0100
18.11.2003 21:45:56, skrev Shaun Wexler <email@hidden>:
>
On Nov 18, 2003, at 7:06 AM, Rolf wrote:
>
>
> Hi everyone!
>
>
>
> Anyone know how to programatically prevent the screen-saver from
>
> triggering ? I have tried "fooling" the system by making minute mouse
>
> pointer movements every 15 seconds (using CGWarpMouseCursorPosition),
>
> but that didn't work ..
>
>
How about something like this?
>
>
#import <Cocoa/Cocoa.h>
>
#import <IOKit/hidsystem/IOHIDLib.h>
>
>
@implementation NSApplication (SKWInsomnia)
>
>
- (void)allowSleep:(BOOL)sleepAllowed
>
{
>
static NSTimer *timer = nil;
>
>
if (sleepAllowed && timer) {
>
[timer invalidate]; // released by the NSRunLoop
>
timer = nil;
>
}
>
>
if (!sleepAllowed && !timer) {
>
timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self
>
selector:@selector(tickleHIDEventTimer:) userInfo:nil repeats:YES];
>
}
>
}
>
>
- (void)tickleHIDEventTimer:(NSTimer *)timer
>
{
>
IOGPoint locPoint;
>
kern_return_t kr;
>
mach_port_t masterPort, service, iter;
>
NXEvent nullEvent = {NX_NULLEVENT, {0, 0}, 0, -1, 0};
>
static io_connect_t serviceConnection = MACH_PORT_NULL;
>
>
if (serviceConnection == MACH_PORT_NULL)
>
{
>
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
>
if (kr == KERN_SUCCESS && masterPort)
>
{
>
kr = IOServiceGetMatchingServices(masterPort,
>
IOServiceMatching(kIOHIDSystemClass), &iter);
>
if (kr == KERN_SUCCESS)
>
{
>
service = IOIteratorNext(iter);
>
kr = IOServiceOpen(service, mach_task_self(),
>
kIOHIDParamConnectType, &serviceConnection);
>
IOObjectRelease(service);
>
IOObjectRelease(iter);
>
}
>
}
>
if (masterPort) {
>
IOObjectRelease(masterPort);
>
masterPort = MACH_PORT_NULL;
>
}
>
if (kr != KERN_SUCCESS) {
>
serviceConnection = MACH_PORT_NULL;
>
return;
>
}
>
}
>
>
if (serviceConnection != MACH_PORT_NULL) {
>
kr = IOHIDPostEvent(serviceConnection, NX_NULLEVENT, locPoint,
>
&nullEvent.data, FALSE, 0, FALSE);
>
}
>
}
>
>
@end
>
>
Typed in Mail.app; usual disclaimer applies...
>
--
>
Shaun Wexler
>
MacFOH
>
http://www.macfoh.com
>
>
>
Very interesting method! I am actually also searching for a way to generate mouse events programatically. For some unknown reason I never got CGPostMouseEvent to work properly. From your code it seems as if IOHIDPostEvent can be used to simulate mouse events
instead. Do you have any experience with that ? What happens if the user presses left mouse button down, and an app simulates left mouse button up immediately afterwards ? Are there recommended ways to deal with such synchronization issues ?
Thanks!
/Rolf
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.