From f830c8ed13ade08e86f01f013cc63ab7cd7325a2 Mon Sep 17 00:00:00 2001
From: vcaesar <vzvway@gmail.com>
Date: Thu, 19 Jan 2023 10:06:56 -0800
Subject: [PATCH] Add: add capture multi screen default support

---
 robotgo.go            |  9 ++++++++-
 screen/goScreen.h     |  6 +++---
 screen/screengrab_c.h | 22 ++++++++++++----------
 3 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/robotgo.go b/robotgo.go
index 0632996..4acce50 100644
--- a/robotgo.go
+++ b/robotgo.go
@@ -72,6 +72,9 @@ var (
 
 	// DisplayID set the screen display id
 	DisplayID = -1
+
+	// NotPid used the hwnd not pid in windows
+	NotPid bool
 )
 
 type (
@@ -324,7 +327,11 @@ func CaptureScreen(args ...int) CBitmap {
 		h = C.int32_t(rect.H)
 	}
 
-	bit := C.capture_screen(x, y, w, h, C.int32_t(displayId))
+	isPid := 0
+	if NotPid || len(args) > 5 {
+		isPid = 1
+	}
+	bit := C.capture_screen(x, y, w, h, C.int32_t(displayId), C.int8_t(isPid))
 	return CBitmap(bit)
 }
 
diff --git a/screen/goScreen.h b/screen/goScreen.h
index c093d06..24321bf 100644
--- a/screen/goScreen.h
+++ b/screen/goScreen.h
@@ -51,7 +51,7 @@ MMRGBHex get_px_color(int32_t x, int32_t y, int32_t display_id) {
 		return color;
 	}
 
-	bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1), display_id);
+	bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1), display_id, 0);
 	color = MMRGBHexAtPoint(bitmap, 0, 0);
 	destroyMMBitmap(bitmap);
 
@@ -114,8 +114,8 @@ void bitmap_dealloc(MMBitmapRef bitmap) {
 }
 
 // capture_screen capture screen
-MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h, int32_t display_id) {
-	MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h), display_id);
+MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h, int32_t display_id, int8_t isPid) {
+	MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h), display_id, isPid);
 	return bitmap;
 }
 
diff --git a/screen/screengrab_c.h b/screen/screengrab_c.h
index c390fe5..49852d0 100644
--- a/screen/screengrab_c.h
+++ b/screen/screengrab_c.h
@@ -13,7 +13,7 @@
 	#include <string.h>
 #endif
 
-MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id) {
+MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id, int8_t isPid) {
 #if defined(IS_MACOSX)
 	MMBitmapRef bitmap = NULL;
 	uint8_t *buffer = NULL;
@@ -24,8 +24,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
 		displayID = CGMainDisplayID();
 	}
 
-	CGImageRef image = CGDisplayCreateImageForRect(displayID,
-		CGRectMake(rect.origin.x, rect.origin.y, rect.size.w, rect.size.h));
+	MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
+	CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
 	if (!image) { return NULL; }
 
 	CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
@@ -52,14 +52,14 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
 		display = XGetMainDisplay();
 	}
 
+	MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
 	XImage *image = XGetImage(display, XDefaultRootWindow(display), 
-							(int)rect.origin.x, (int)rect.origin.y,
-	                        (unsigned int)rect.size.w, (unsigned int)rect.size.h, AllPlanes, ZPixmap);
+							(int)o.x, (int)o.y, (unsigned int)s.w, (unsigned int)s.h, AllPlanes, ZPixmap);
 	XCloseDisplay(display);
 	if (image == NULL) { return NULL; }
 
 	bitmap = createMMBitmap_c((uint8_t *)image->data, 
-				rect.size.w, rect.size.h, (size_t)image->bytes_per_line, 
+				s.w, s.h, (size_t)image->bytes_per_line, 
 				(uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
 	image->data = NULL; /* Steal ownership of bitmap data so we don't have to copy it. */
 	XDestroyImage(image);
@@ -89,15 +89,17 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
 	bi.bmiHeader.biClrImportant = 0;
 
 	HWND hwnd;
-	if (display_id == -1) {
-		screen = GetDC(NULL); /* Get entire screen */
+	if (display_id == -1 || isPid == 0) {
+	// 	screen = GetDC(NULL); /* Get entire screen */
+		hwnd = GetDesktopWindow();
 	} else {
 		hwnd = (HWND) (uintptr) display_id;
-		screen = GetDC(hwnd);
 	}
+	screen = GetDC(hwnd);
+	
 	if (screen == NULL) { return NULL; }
 
-	// Todo:
+	// Todo: Use DXGI
 	screenMem = CreateCompatibleDC(screen);
 	/* Get screen data in display device context. */
    	dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);