Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added -s Speed option #171

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Actions/MouseBaseAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef enum {
withOptions:(struct ExecutionOptions)options;

- (void)postHumanizedMouseEventsWithEasingFactor:(unsigned)easing
speedFactor:(unsigned)speed
toX:(float)endX
toY:(float)endY;

Expand Down
12 changes: 10 additions & 2 deletions Actions/MouseBaseAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ - (void)performActionWithData:(NSString *)data
if (options.easing) {
// Eased move
[self postHumanizedMouseEventsWithEasingFactor:options.easing
speedFactor:options.speed
toX:(float)p.x
toY:(float)p.y];
} else {
Expand All @@ -148,6 +149,7 @@ - (uint32_t)getMoveEventConstant {
}

- (void)postHumanizedMouseEventsWithEasingFactor:(unsigned)easing
speedFactor:(unsigned)speed
toX:(float)endX
toY:(float)endY {

Expand All @@ -158,15 +160,21 @@ - (void)postHumanizedMouseEventsWithEasingFactor:(unsigned)easing
float startX = currentLocation.x;
float startY = currentLocation.y;
float distance = [self distanceBetweenPoint:NSPointFromCGPoint(currentLocation) andPoint:NSMakePoint(endX, endY)];
bool speederIsEnabled = speed >= 10;

unsigned steps = ((int)(distance * easing / 100)) + 1;

if (speederIsEnabled) {
steps = steps * speed;
};

float xDiff = (endX - startX);
float yDiff = (endY - startY);
float stepSize = (float)1.0 / (float)steps;

unsigned i;
for (i = 0; i < steps; i ++) {
float factor = [self cubicEaseInOut:(stepSize * i)];
float factor = speederIsEnabled ? stepSize * i : [self cubicEaseInOut:(stepSize * i)];
CGEventRef eventRef = CGEventCreateMouseEvent(NULL, eventConstant, CGPointMake(startX + (factor * xDiff), startY + (factor * yDiff)), 0);
CGEventPost(kCGHIDEventTap, eventRef);
CFRelease(eventRef);
Expand Down
1 change: 1 addition & 0 deletions ExecutionOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
struct ExecutionOptions {
unsigned mode;
unsigned easing;
unsigned speed;
unsigned waitTime;
BOOL isFirstAction;
BOOL isLastAction;
Expand Down
5 changes: 4 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage
To get a quick first impression, this is what you will get when you invoke `cliclick -h`:

USAGE
cliclick [-r] [-m <mode>] [-d <target>] [-e <num>] [-f <file>] [-w <num>] command1 [command2]
cliclick [-r] [-m <mode>] [-d <target>] [-e <num>] [-s <num>] [-f <file>] [-w <num>] command1 [command2]

OPTIONS
-r Restore initial mouse location when finished
Expand All @@ -35,6 +35,9 @@ To get a quick first impression, this is what you will get when you invoke `clic
on the distance between the start and the end position, i.e.
the time needed for moving will be higher if the distance
is larger.
-s <speed> Allows slowing the mouse movement to a constant rate.
Minimum accepted value is 10. Higher the value,
slower the mouse movement. Do not use together with -e option.
-f <file> Instead of passing commands as arguments, you may instead
specify a file from which cliclick will read the commands
(or stdin, when - is given as filename).
Expand Down
12 changes: 9 additions & 3 deletions cliclick.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ int main (int argc, const char * argv[]) {

struct ExecutionOptions executionOptions;
executionOptions.easing = 0;
executionOptions.speed = 10;
executionOptions.waitTime = 0;
executionOptions.mode = MODE_REGULAR;
NSArray *modeOptionArg;
Expand All @@ -64,7 +65,7 @@ int main (int argc, const char * argv[]) {
fprintf(stderr, " System Preferences → Security & Privacy → Accessibility\n");
}

while ((optchar = getopt(argc, (char * const *)argv, "horVne:f:d:m:w:")) != -1) {
while ((optchar = getopt(argc, (char * const *)argv, "horVne:f:d:m:w:s:")) != -1) {
switch(optchar) {
case 'h':
help();
Expand Down Expand Up @@ -112,6 +113,10 @@ int main (int argc, const char * argv[]) {
case 'w':
executionOptions.waitTime = atoi(optarg) > 0 ? atoi(optarg) : 0;
break;
case 's':
executionOptions.easing = 1;
executionOptions.speed = atoi(optarg) > 1 ? atoi(optarg) : 1;
break;
default:
[pool release];
return EXIT_FAILURE;
Expand Down Expand Up @@ -217,12 +222,12 @@ int main (int argc, const char * argv[]) {
return [commands autorelease];
}

void error() {
void error(void) {
fprintf(stderr, "You did not pass any commands as argument to cliclick.\n");
fprintf(stderr, "Call cliclick with option -h to see usage instructions.\n");
}

void help() {
void help(void) {

NSArray *actionClasses = [ActionExecutor actionClasses];
NSUInteger i, count = [actionClasses count];
Expand Down Expand Up @@ -250,6 +255,7 @@ void help() {
" on the distance between the start and the end position, i.e.\n"
" the time needed for moving will be higher if the distance\n"
" is larger.\n"
" -s <speed> You can change speed of movement by changing this value\n"
" -f <file> Instead of passing commands as arguments, you may instead\n"
" specify a file from which cliclick will read the commands\n"
" (or stdin, when - is given as filename).\n"
Expand Down