Skip to content

Commit

Permalink
Merge branch '4.0-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueM committed Mar 24, 2018
2 parents 89e4aa2 + ed093d2 commit 10ba6f7
Show file tree
Hide file tree
Showing 47 changed files with 1,511 additions and 925 deletions.
12 changes: 6 additions & 6 deletions ActionExecutor.h
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
Expand Down Expand Up @@ -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
68 changes: 38 additions & 30 deletions ActionExecutor.m
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
Expand Down Expand Up @@ -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"
Expand All @@ -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];
}

Expand Down
43 changes: 23 additions & 20 deletions Actions/ActionProtocol.h
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
Expand Down Expand Up @@ -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
10 changes: 5 additions & 5 deletions Actions/ClickAction.h
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
Expand Down Expand Up @@ -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
13 changes: 8 additions & 5 deletions Actions/ClickAction.m
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
Expand Down Expand Up @@ -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"
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions Actions/ColorPickerAction.h
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
Expand Down Expand Up @@ -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
19 changes: 9 additions & 10 deletions Actions/ColorPickerAction.m
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
Expand Down Expand Up @@ -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];

Expand All @@ -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;
Expand All @@ -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
8 changes: 4 additions & 4 deletions Actions/DoubleclickAction.h
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
Expand Down Expand Up @@ -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
Loading

0 comments on commit 10ba6f7

Please sign in to comment.