Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueM committed Jan 31, 2013
0 parents commit b6b124d
Show file tree
Hide file tree
Showing 33 changed files with 5,335 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ActionExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <Foundation/Foundation.h>
#import "ActionProtocol.h"

@interface ActionExecutor : NSObject {

}

+(void)executeActions:(NSArray *)actions
inMode:(unsigned)mode
waitingMilliseconds:(int)milliseconds;

+(NSDictionary *)shortcuts;

+(NSArray *)actionClasses;

@end
113 changes: 113 additions & 0 deletions ActionExecutor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ActionExecutor.h"

@implementation ActionExecutor

+(void)executeActions:(NSArray *)actions
inMode:(unsigned)mode
waitingMilliseconds:(int)milliseconds {

NSDictionary *shortcuts = [self shortcuts];

struct timespec waitingtime;
waitingtime.tv_sec = 0;

if (milliseconds < 20) {
milliseconds = 20;
}

if (milliseconds > 999) {
waitingtime.tv_sec = (int)floor(milliseconds / 1000);
waitingtime.tv_nsec = (milliseconds - waitingtime.tv_sec * 1000) * 1000000;
} else {
waitingtime.tv_sec = 0;
waitingtime.tv_nsec = milliseconds * 1000000;
}

NSUInteger i, count = [actions count];
for (i = 0; i < count; i++) {
NSArray *action = [[actions objectAtIndex:i] componentsSeparatedByString:@":"];
NSString *actionClass = [shortcuts objectForKey:[action objectAtIndex:0]];
if (nil == actionClass) {
if ([[action objectAtIndex:0] isEqualToString:[actions objectAtIndex:i]]) {
[NSException raise:@"InvalidCommandException"
format:@"Unrecognized action shortcut “%@", [action objectAtIndex:0]];
} else {
[NSException raise:@"InvalidCommandException"
format:@"Unrecognized action shortcut “%@” in “%@", [action objectAtIndex:0], [actions objectAtIndex:i]];
}
}

id actionClassInstance = [[NSClassFromString(actionClass) alloc] init];

if (![actionClassInstance conformsToProtocol:@protocol(ActionProtocol)]) {
[NSException raise:@"InvalidCommandException"
format:@"%@ does not conform to ActionProtocol", actionClass];
}

if ([action count] > 1) {
[actionClassInstance performActionWithData:[action objectAtIndex:1] inMode:mode];
} else {
[actionClassInstance performActionWithData:@"" inMode:mode];
}

[actionClassInstance release];

nanosleep(&waitingtime, NULL);
}
}

+(NSArray *)actionClasses {
NSArray *actionClasses = [NSArray arrayWithObjects:@"ClickAction", @"MoveAction", @"DoubleclickAction", @"TripleclickAction",
@"KeyDownAction", @"KeyUpAction", @"PrintAction", @"WaitAction",
@"KeyPressAction", nil];
return actionClasses;
}

+(NSDictionary *)shortcuts {

NSArray *actionClasses = [[self class] actionClasses];
NSMutableDictionary *shortcuts = [NSMutableDictionary dictionaryWithCapacity:[actionClasses count]];
NSUInteger i, ii;

for (i = 0, ii = [actionClasses count]; i < ii; i++) {
NSString *classname = [actionClasses objectAtIndex:i];
NSString *shortcut = [NSClassFromString(classname) commandListShortcut];
if (nil != [shortcuts objectForKey:shortcut]) {
[NSException raise:@"ShortcutConflictException"
format:@"Shortcut “%@” is used by more than one action class", shortcut];
}
[shortcuts setObject:classname forKey:shortcut];
}

return [[shortcuts retain] autorelease];
}

@end
40 changes: 40 additions & 0 deletions Actions/ActionProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <Cocoa/Cocoa.h>

@protocol ActionProtocol

+(NSString *)commandListShortcut;

+(NSString *)commandDescription;

-(void)performActionWithData:(NSString *)data
inMode:(unsigned)mode;

@end
45 changes: 45 additions & 0 deletions Actions/ClickAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <Cocoa/Cocoa.h>
#import "ActionProtocol.h"
#import "MouseBaseAction.h"

@interface ClickAction : MouseBaseAction <ActionProtocol> {

}

+(NSString *)commandListShortcut;

+(NSString *)commandDescription;

-(NSString *)actionDescriptionString:(NSString *)locationDescription;

-(void)performActionAtPoint:(CGPoint) p;

@end
59 changes: 59 additions & 0 deletions Actions/ClickAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ClickAction.h"

@implementation ClickAction

+(NSString *)commandListShortcut {
return @"c";
}

+(NSString *)commandDescription {
return @" c:x,y Will CLICK at the point with the given coordinates.\n"
" Example: “c:12,34” will click at the point with x coordinate\n"
" 12 and y coordinate 34. Instead of x and y values, you may\n"
" also use “.”, which means: the current position. Using “.” is\n"
" equivalent to using relative zero values “c:+0,+0”.";
}

-(NSString *)actionDescriptionString:(NSString *)locationDescription {
return [NSString stringWithFormat:@"Click at %@", locationDescription];
}

-(void)performActionAtPoint:(CGPoint) p {
// Left button down
CGEventRef leftDown = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(p.x, p.y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, leftDown);

// Left button up
CGEventRef leftUp = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(p.x, p.y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, leftUp);
}

@end
43 changes: 43 additions & 0 deletions Actions/DoubleclickAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2007-2013, Carsten Blüm <carsten@bluem.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of Carsten Blüm nor the names of his contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <Cocoa/Cocoa.h>
#import "ActionProtocol.h"
#import "MouseBaseAction.h"

@interface DoubleclickAction : MouseBaseAction <ActionProtocol> {

}

+(NSString *)commandListShortcut;

-(NSString *)actionDescriptionString:(NSString *)locationDescription;

-(void)performActionAtPoint:(CGPoint) p;

@end
Loading

0 comments on commit b6b124d

Please sign in to comment.