Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
6 changes: 6 additions & 0 deletions examples/window/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func get() {
// get current Window Active
mdata := robotgo.GetActive()

// get current Window Active , return CMData
cmdata := robotgo.GetActiveCMData()

// get current Window Handle
hwnd := robotgo.GetHandle()
fmt.Println("hwnd---", hwnd)
Expand All @@ -44,6 +47,9 @@ func get() {

// set Window Active
robotgo.SetActive(mdata)

// set Window Active by CMData
robotgo.SetActiveByCMData(cmdata)
}

func findIds() {
Expand Down
17 changes: 17 additions & 0 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ package robotgo
#cgo darwin CFLAGS: -x objective-c -Wno-deprecated-declarations
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit
#cgo darwin LDFLAGS: -framework Carbon -framework CoreFoundation
//
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
#cgo darwin LDFLAGS: -framework ScreenCaptureKit
#endif

#cgo linux CFLAGS: -I/usr/src
#cgo linux LDFLAGS: -L/usr/src -lm -lX11 -lXtst
Expand Down Expand Up @@ -86,6 +90,7 @@ type (
CHex C.MMRGBHex
// CBitmap define CBitmap as C.MMBitmapRef type
CBitmap C.MMBitmapRef
CMData C.MData
)

// Bitmap define the go Bitmap struct
Expand Down Expand Up @@ -870,13 +875,25 @@ func SetActive(win C.MData) {
C.set_active(win)
}

// SetActiveByCMData set the window active by CMData
func SetActiveByCMData(win CMData) {
C.set_active((C.MData)(win))
}

// GetActive get the active window
func GetActive() C.MData {
mdata := C.get_active()
// fmt.Println("active----", mdata)
return mdata
}

// GetActiveCMData get the active window, return CMData
func GetActiveCMData() CMData {
mdata := C.get_active()
// fmt.Println("active----", mdata)
return (CMData)(mdata)
}

// MinWindow set the window min
func MinWindow(pid int, args ...interface{}) {
var (
Expand Down
55 changes: 53 additions & 2 deletions screen/screengrab_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <ApplicationServices/ApplicationServices.h>
#include <ScreenCaptureKit/ScreenCaptureKit.h>
#elif defined(USE_X11)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Expand All @@ -13,6 +14,49 @@
#include <string.h>
#endif

#if defined(IS_MACOSX) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
static CGImageRef capture15(CGDirectDisplayID id, CGRect diIntersectDisplayLocal, CGColorSpaceRef colorSpace) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block CGImageRef image1 = nil;
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent* content, NSError* error) {
@autoreleasepool {
if (error) {
dispatch_semaphore_signal(semaphore);
return;
}
SCDisplay* target = nil;
for (SCDisplay *display in content.displays) {
if (display.displayID == id) {
target = display;
break;
}
}
if (!target) {
dispatch_semaphore_signal(semaphore);
return;
}

SCContentFilter* filter = [[SCContentFilter alloc] initWithDisplay:target excludingWindows:@[]];
SCStreamConfiguration* config = [[SCStreamConfiguration alloc] init];
config.sourceRect = diIntersectDisplayLocal;
config.width = diIntersectDisplayLocal.size.width;
config.height = diIntersectDisplayLocal.size.height;
[SCScreenshotManager captureImageWithFilter:filter
configuration:config
completionHandler:^(CGImageRef img, NSError* error) {
if (!error) {
image1 = CGImageCreateCopyWithColorSpace(img, colorSpace);
}
dispatch_semaphore_signal(semaphore);
}];
}
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
return image1;
}
#endif

MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id, int8_t isPid) {
#if defined(IS_MACOSX)
MMBitmapRef bitmap = NULL;
Expand All @@ -25,9 +69,16 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id,
}

MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
CGColorSpaceRef color = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGImageRef image = capture15(displayID, CGRectMake(o.x, o.y, s.w, s.h), color);
CGColorSpaceRelease(color);
#else
// This API is deprecated in macos 15, use ScreenCaptureKit's captureScreenshot
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
#endif
if (!image) { return NULL; }

CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
if (!imageData) { return NULL; }

Expand Down