-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
1,511 additions
and
925 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -28,17 +28,17 @@ | |
|
||
#import <Foundation/Foundation.h> | ||
#import "ActionProtocol.h" | ||
#import "ExecutionOptions.h" | ||
|
||
@interface ActionExecutor : NSObject { | ||
|
||
} | ||
|
||
+(void)executeActions:(NSArray *)actions | ||
inMode:(unsigned)mode | ||
waitingMilliseconds:(int)milliseconds; | ||
+ (void)executeActions:(NSArray *)actions | ||
withOptions:(struct ExecutionOptions)options; | ||
|
||
+(NSDictionary *)shortcuts; | ||
+ (NSDictionary *)shortcuts; | ||
|
||
+(NSArray *)actionClasses; | ||
+ (NSArray *)actionClasses; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -28,34 +28,34 @@ | |
|
||
#import "ActionExecutor.h" | ||
#include "ActionClassesMacro.h" | ||
#include "ExecutionOptions.h" | ||
|
||
@implementation ActionExecutor | ||
|
||
+(void)executeActions:(NSArray *)actions | ||
inMode:(unsigned)mode | ||
waitingMilliseconds:(int)milliseconds { | ||
+ (void)executeActions:(NSArray *)actions | ||
withOptions:(struct ExecutionOptions)options { | ||
|
||
NSDictionary *shortcuts = [self shortcuts]; | ||
|
||
struct timespec waitingtime; | ||
waitingtime.tv_sec = 0; | ||
if (milliseconds < 30) { | ||
milliseconds = 30; | ||
|
||
if (options.waitTime < 100) { | ||
options.waitTime = 100; | ||
} | ||
|
||
if (milliseconds > 999) { | ||
waitingtime.tv_sec = (int)floor(milliseconds / 1000); | ||
waitingtime.tv_nsec = (milliseconds - waitingtime.tv_sec * 1000) * 1000000; | ||
if (options.waitTime > 999) { | ||
waitingtime.tv_sec = (int)floor(options.waitTime / 1000); | ||
waitingtime.tv_nsec = (options.waitTime - waitingtime.tv_sec * 1000) * 1000000; | ||
} else { | ||
waitingtime.tv_sec = 0; | ||
waitingtime.tv_nsec = milliseconds * 1000000; | ||
waitingtime.tv_nsec = options.waitTime * 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]]; | ||
Class actionClass = [shortcuts objectForKey:[action objectAtIndex:0]]; | ||
if (nil == actionClass) { | ||
if ([[action objectAtIndex:0] isEqualToString:[actions objectAtIndex:i]]) { | ||
[NSException raise:@"InvalidCommandException" | ||
|
@@ -65,47 +65,55 @@ +(void)executeActions:(NSArray *)actions | |
format:@"Unrecognized action shortcut “%@” in “%@”", [action objectAtIndex:0], [actions objectAtIndex:i]]; | ||
} | ||
} | ||
id actionClassInstance = [[NSClassFromString(actionClass) alloc] init]; | ||
|
||
id actionClassInstance = [[actionClass alloc] init]; | ||
|
||
if (![actionClassInstance conformsToProtocol:@protocol(ActionProtocol)]) { | ||
[NSException raise:@"InvalidCommandException" | ||
format:@"%@ does not conform to ActionProtocol", actionClass]; | ||
} | ||
|
||
|
||
options.isFirstAction = i == 0; | ||
options.isLastAction = i == count - 1; | ||
|
||
if ([action count] > 1) { | ||
[actionClassInstance performActionWithData:[[action subarrayWithRange:NSMakeRange(1, [action count] - 1)] componentsJoinedByString:@":"] inMode:mode]; | ||
[actionClassInstance performActionWithData:[[action subarrayWithRange:NSMakeRange(1, [action count] - 1)] componentsJoinedByString:@":"] | ||
withOptions:options]; | ||
} else { | ||
[actionClassInstance performActionWithData:@"" inMode:mode]; | ||
[actionClassInstance performActionWithData:@"" | ||
withOptions:options]; | ||
} | ||
|
||
[actionClassInstance release]; | ||
|
||
nanosleep(&waitingtime, NULL); | ||
if (!options.isLastAction) { | ||
nanosleep(&waitingtime, NULL); | ||
} | ||
} | ||
} | ||
|
||
+(NSArray *)actionClasses { | ||
+ (NSArray *)actionClasses { | ||
NSArray *actionClasses = [NSArray arrayWithObjects:ACTION_CLASSES]; | ||
return actionClasses; | ||
} | ||
|
||
+(NSDictionary *)shortcuts { | ||
+ (NSDictionary *)shortcuts { | ||
|
||
NSArray *actionClasses = [[self class] actionClasses]; | ||
NSMutableDictionary *shortcuts = [NSMutableDictionary dictionaryWithCapacity:[actionClasses count]]; | ||
NSUInteger i, ii; | ||
NSUInteger i, ii; | ||
|
||
for (i = 0, ii = [actionClasses count]; i < ii; i++) { | ||
NSString *classname = [actionClasses objectAtIndex:i]; | ||
NSString *shortcut = [NSClassFromString(classname) commandShortcut]; | ||
Class actionClass = NSClassFromString(classname); | ||
NSString *shortcut = [actionClass commandShortcut]; | ||
if (nil != [shortcuts objectForKey:shortcut]) { | ||
[NSException raise:@"ShortcutConflictException" | ||
format:@"Shortcut “%@” is used by more than one action class", shortcut]; | ||
} | ||
[shortcuts setObject:classname forKey:shortcut]; | ||
} | ||
[shortcuts setObject:actionClass forKey:shortcut]; | ||
} | ||
|
||
return [[shortcuts retain] autorelease]; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -27,35 +27,38 @@ | |
*/ | ||
|
||
#import <Cocoa/Cocoa.h> | ||
#import "ExecutionOptions.h" | ||
|
||
@protocol ActionProtocol | ||
|
||
/** | ||
Returns the command's shortcut. | ||
The command shortcut is the string which has to be used as command-line argument (typically followed by “:” plus some arguments) to invoke the command. | ||
@note The command shortcut is unique for each command. | ||
@return Command shortcut | ||
* Returns the command's shortcut | ||
* | ||
* The command shortcut is the string which has to be used as command-line argument (typically followed by “:” plus some arguments) to invoke the command. | ||
* | ||
* @note The command shortcut has to be unique for each command. | ||
* @return Command shortcut | ||
*/ | ||
+(NSString *)commandShortcut; | ||
+ (NSString *)commandShortcut; | ||
|
||
/** | ||
Returns the command description | ||
The command description is to be included in the help output and is formatted (i.e.: indented). It should include a description as well as at least one usage example. | ||
@return Command shortcut | ||
* Returns the command description | ||
* | ||
* The command description is to be included in the help output and is formatted (i.e.: indented). It should include a description as well as at least one usage example. | ||
* | ||
* @return Command description | ||
*/ | ||
+(NSString *)commandDescription; | ||
+ (NSString *)commandDescription; | ||
|
||
/** | ||
Performs the action | ||
Depending on the `mode` argument, this can be the action, printing a description of the action to STDOUT or both. | ||
@param data Part of the argument remaining after stripping the leading command identifier | ||
@param mode One of: MODE_VERBOSE, MODE_TEST, MODE_REGULAR | ||
* Performs the action | ||
* | ||
* Depending on the `mode` argument, this can be the action, printing a description of the action to STDOUT or both. | ||
* | ||
* @param data Part of the argument remaining after stripping the leading command identifier | ||
* @param options | ||
*/ | ||
-(void)performActionWithData:(NSString *)data | ||
inMode:(unsigned)mode; | ||
- (void)performActionWithData:(NSString *)data | ||
withOptions:(struct ExecutionOptions)options; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -34,12 +34,12 @@ | |
|
||
} | ||
|
||
+(NSString *)commandShortcut; | ||
+ (NSString *)commandShortcut; | ||
|
||
+(NSString *)commandDescription; | ||
+ (NSString *)commandDescription; | ||
|
||
-(NSString *)actionDescriptionString:(NSString *)locationDescription; | ||
- (NSString *)actionDescriptionString:(NSString *)locationDescription; | ||
|
||
-(void)performActionAtPoint:(CGPoint) p; | ||
- (void)performActionAtPoint:(CGPoint) p; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -27,16 +27,17 @@ | |
*/ | ||
|
||
#import "ClickAction.h" | ||
#include <unistd.h> | ||
|
||
@implementation ClickAction | ||
|
||
#pragma mark - ActionProtocol | ||
|
||
+(NSString *)commandShortcut { | ||
+ (NSString *)commandShortcut { | ||
return @"c"; | ||
} | ||
|
||
+(NSString *)commandDescription { | ||
+ (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" | ||
|
@@ -46,16 +47,18 @@ +(NSString *)commandDescription { | |
|
||
#pragma mark - MouseBaseAction | ||
|
||
-(NSString *)actionDescriptionString:(NSString *)locationDescription { | ||
- (NSString *)actionDescriptionString:(NSString *)locationDescription { | ||
return [NSString stringWithFormat:@"Click at %@", locationDescription]; | ||
} | ||
|
||
-(void)performActionAtPoint:(CGPoint) p { | ||
- (void)performActionAtPoint:(CGPoint) p { | ||
// Left button down | ||
CGEventRef leftDown = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(p.x, p.y), kCGMouseButtonLeft); | ||
CGEventPost(kCGHIDEventTap, leftDown); | ||
CFRelease(leftDown); | ||
|
||
usleep(15000); // Improve reliability | ||
|
||
// Left button up | ||
CGEventRef leftUp = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(p.x, p.y), kCGMouseButtonLeft); | ||
CGEventPost(kCGHIDEventTap, leftUp); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -33,11 +33,11 @@ | |
|
||
} | ||
|
||
+(NSString *)commandShortcut; | ||
+ (NSString *)commandShortcut; | ||
|
||
+(NSString *)commandDescription; | ||
+ (NSString *)commandDescription; | ||
|
||
-(void)performActionWithData:(NSString *)data | ||
inMode:(unsigned)mode; | ||
- (void)performActionWithData:(NSString *)data | ||
withOptions:(struct ExecutionOptions)options; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -33,19 +33,19 @@ @implementation ColorPickerAction | |
|
||
#pragma mark - ActionProtocol | ||
|
||
+(NSString *)commandShortcut { | ||
+ (NSString *)commandShortcut { | ||
return @"cp"; | ||
} | ||
|
||
+(NSString *)commandDescription { | ||
+ (NSString *)commandDescription { | ||
return @" cp:str Will PRINT THE COLOR value at the given screen location.\n" | ||
" The color value is printed as three decimal 8-bit values,\n" | ||
" representing, in order, red, green, and blue.\n" | ||
" Example: “cp:123,456” might print “127 63 0”"; | ||
} | ||
|
||
-(void)performActionWithData:(NSString *)data | ||
inMode:(unsigned)mode { | ||
- (void)performActionWithData:(NSString *)data | ||
withOptions:(struct ExecutionOptions)options { | ||
|
||
NSString *shortcut = [[self class] commandShortcut]; | ||
|
||
|
@@ -71,11 +71,11 @@ -(void)performActionWithData:(NSString *)data | |
} | ||
} | ||
|
||
if (MODE_TEST == mode) { | ||
if (MODE_TEST == options.mode) { | ||
if ([data isEqualToString:@"."]) { | ||
printf("Print color at current mouse position\n"); | ||
[options.verbosityOutputHandler write:@"Print color at current mouse position"]; | ||
} else { | ||
printf("Print color at location %i,%i\n", [[coords objectAtIndex:0] intValue], [[coords objectAtIndex:1] intValue]); | ||
[options.verbosityOutputHandler write:[NSString stringWithFormat:@"Print color at location %i,%i\n", [[coords objectAtIndex:0] intValue], [[coords objectAtIndex:1] intValue]]]; | ||
} | ||
} else { | ||
CGPoint p; | ||
|
@@ -89,10 +89,9 @@ -(void)performActionWithData:(NSString *)data | |
NSColor *color = [bitmap colorAtX:0 y:0]; | ||
[bitmap release]; | ||
|
||
printf("%d %d %d\n", (int)(color.redComponent*255), (int)(color.greenComponent*255), (int)(color.blueComponent*255)); | ||
[options.commandOutputHandler write:[NSString stringWithFormat:@"%d %d %d\n", (int)(color.redComponent*255), (int)(color.greenComponent*255), (int)(color.blueComponent*255)]]; | ||
} | ||
} | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (c) 2007-2015, Carsten Blüm <[email protected]> | ||
* Copyright (c) 2007-2018, Carsten Blüm <[email protected]> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -34,10 +34,10 @@ | |
|
||
} | ||
|
||
+(NSString *)commandShortcut; | ||
+ (NSString *)commandShortcut; | ||
|
||
-(NSString *)actionDescriptionString:(NSString *)locationDescription; | ||
- (NSString *)actionDescriptionString:(NSString *)locationDescription; | ||
|
||
-(void)performActionAtPoint:(CGPoint) p; | ||
- (void)performActionAtPoint:(CGPoint) p; | ||
|
||
@end |
Oops, something went wrong.