Add: add capture multi screen default support

This commit is contained in:
vcaesar 2023-01-19 10:06:56 -08:00
parent 736fa1cc26
commit f830c8ed13
3 changed files with 23 additions and 14 deletions

View File

@ -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)
}

View File

@ -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;
}

View File

@ -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);