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

use ScreenCaptureKit and add the "sl" command for scrolling #184

Open
wants to merge 1 commit 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
16 changes: 11 additions & 5 deletions Actions/ColorPickerAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import "ColorPickerAction.h"
#import "MouseBaseAction.h"
#import <ScreenCaptureKit/ScreenCaptureKit.h>

@implementation ColorPickerAction

Expand Down Expand Up @@ -82,12 +83,17 @@ - (void)performActionWithData:(NSString *)data
p.x = [MouseBaseAction getCoordinate:[coords objectAtIndex:0] forAxis:XAXIS];
p.y = [MouseBaseAction getCoordinate:[coords objectAtIndex:1] forAxis:YAXIS];

CGRect imageRect = CGRectMake(p.x, p.y, 1, 1);
CGImageRef imageRef = CGWindowListCreateImage(imageRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
// Use ScreenCaptureKit to capture the screen
SCStreamConfiguration *config = [[SCStreamConfiguration alloc] init];
config.width = 1;
config.height = 1;
SCStream *stream = [[SCStream alloc] initWithDisplay:[SCDisplay mainDisplay] configuration:config];
[stream startCapture];

// Capture the color at the specified point
NSBitmapImageRep *bitmap = [stream bitmapImageRepForRect:CGRectMake(p.x, p.y, 1, 1)];
NSColor *color = [bitmap colorAtX:0 y:0];
[bitmap release];
[stream stopCapture];

[options.commandOutputHandler write:[NSString stringWithFormat:@"%d %d %d\n", (int)(color.redComponent*255), (int)(color.greenComponent*255), (int)(color.blueComponent*255)]];
}
Expand Down
43 changes: 43 additions & 0 deletions Actions/ScrollBaseAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2007-2024, Carsten Blüm <[email protected]>
* 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"

@interface ScrollBaseAction : NSObject {

}

-(CGScrollEventUnit)scrollUnit;

-(NSString *)scrollUnitName;

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

@end
134 changes: 134 additions & 0 deletions Actions/ScrollBaseAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Copyright (c) 2007-2024, Carsten Blüm <[email protected]>
* 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 "ScrollBaseAction.h"

@implementation ScrollBaseAction

-(NSString *)scrollUnitName {
[NSException raise:@"InvalidCommandException"
format:@"To be implemented by subclasses"];
return @"";
}

-(CGScrollEventUnit)scrollUnit {
[NSException raise:@"InvalidCommandException"
format:@"To be implemented by subclasses"];
return (CGScrollEventUnit)NULL;
}

#pragma mark - ActionProtocol

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

NSString *shortcut = [[self class] commandShortcut];

if ([data isEqualToString:@""]) {
[NSException raise:@"InvalidCommandException"
format:@"Missing argument to command \"%@\": Expected one to three values (separated by a comma). Example: \"%@:5\" or \"%@:0,5,0\"",
shortcut, shortcut, shortcut];
} else {
NSArray *scrollStrVals = [data componentsSeparatedByString:@","];
int scrollVals[3];

CGEventRef scrollEvent;

switch([scrollStrVals count]) {
case 1:
scrollVals[0] = [[scrollStrVals objectAtIndex:0] intValue] * -1;
scrollVals[1] = 0;
scrollVals[2] = 0;
if (MODE_VERBOSE == mode || MODE_TEST == mode) {
printf("Scroll up/down %d %s\n", scrollVals[0], [[self scrollUnitName] UTF8String]);
}
break;
case 2:
scrollVals[0] = [[scrollStrVals objectAtIndex:0] intValue] * -1;
scrollVals[1] = [[scrollStrVals objectAtIndex:1] intValue] * -1;
scrollVals[2] = 0;
if (MODE_VERBOSE == mode || MODE_TEST == mode) {
printf("Scroll up/down %d, left/right %d %s\n", scrollVals[0], scrollVals[1], [[self scrollUnitName] UTF8String]);
}
break;
case 3:
scrollVals[0] = [[scrollStrVals objectAtIndex:0] intValue] * -1;
scrollVals[1] = [[scrollStrVals objectAtIndex:1] intValue] * -1;
scrollVals[2] = [[scrollStrVals objectAtIndex:2] intValue] * -1;
if (MODE_VERBOSE == mode || MODE_TEST == mode) {
printf("Scroll up/down %d, left/right %d, in/out %d %s\n", scrollVals[0], scrollVals[1], scrollVals[2], [[self scrollUnitName] UTF8String]);
}
break;
default:
[NSException raise:@"InvalidCommandException"
format:@"Invalid argument \"%@\" to command \"%@\": Expected one to three values (separated by a comma). Example: \"%@:5\" or \"%@:0,5,0\"",
data, shortcut, shortcut, shortcut];
break;
}

if (MODE_TEST != mode) {
/*
* Rather than do a massive scroll all at once, split it into chunks of 10 units or fewer.
* This is according to Apple docs:
*
* Scrolling movement is generally represented by small signed integer values, typically in a
* range from -10 to +10. Large values may have unexpected results, depending on the application
* that processes the event.
*/
int scrollDir[3];
for (int i=0; i<3; i++) {
if (scrollVals[i] > 0) {
scrollDir[i] = 1;
} else {
scrollDir[i] = -1;
scrollVals[i] *= -1;
}
}
while (scrollVals[0] > 0 ||
scrollVals[1] > 0 ||
scrollVals[2] > 0) {
int scrollPart[3];
for (int i=0; i<3; i++) {
scrollPart[i] = scrollVals[i];
if (scrollPart[i] > 10) {
scrollPart[i] = 10;
scrollVals[i] -= 10;
} else {
scrollVals[i] = 0;
}
scrollPart[i] *= scrollDir[i];
}
scrollEvent = CGEventCreateScrollWheelEvent(NULL, [self scrollUnit], 3, scrollPart[0], scrollPart[1], scrollPart[2]);
CGEventPost(kCGHIDEventTap, scrollEvent);
CFRelease(scrollEvent);
}
}
}
}

@end
43 changes: 43 additions & 0 deletions Actions/ScrollLineAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2007-2024, Carsten Blüm <[email protected]>
* 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 "ScrollBaseAction.h"

@interface ScrollLineAction : ScrollBaseAction <ActionProtocol> {

}

+(NSString *)commandShortcut;

+(NSString *)commandDescription;

-(CGScrollEventUnit)scrollUnit;

@end
64 changes: 64 additions & 0 deletions Actions/ScrollLineAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2007-2024, Carsten Blüm <[email protected]>
* 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 "ScrollLineAction.h"

@implementation ScrollLineAction

#pragma mark - ActionProtocol

+(NSString *)commandShortcut {
return @"sl";
}

+(NSString *)commandDescription {
return @" sl:str Will send a mouse scroll event in the specified number of lines.\n"
" You can specify up to three values, corresponding to three scroll wheels,\n"
" usually interpreted as vertical, horizontal, and depth.\n"
" Positive is down/right; negative is up/left.\n"
" Example: \"sl:10,2,0\" will scroll down and to the right";
}

- (void)performActionWithData:(NSString *)data
withOptions:(struct ExecutionOptions)options {
// Implement the method to handle the scroll action
// This can be similar to the implementation in ScrollBaseAction
[super performActionWithData:data inMode:options.mode];
}

#pragma mark - ScrollBaseAction

-(CGScrollEventUnit)scrollUnit {
return kCGScrollEventUnitLine;
}

-(NSString *)scrollUnitName {
return @"lines";
}

@end
43 changes: 43 additions & 0 deletions Actions/ScrollPixelAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2007-2024, Carsten Blüm <[email protected]>
* 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 "ScrollBaseAction.h"

@interface ScrollPixelAction : ScrollBaseAction <ActionProtocol> {

}

+(NSString *)commandShortcut;

+(NSString *)commandDescription;

-(CGScrollEventUnit)scrollUnit;

@end
Loading