mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13:55 +00:00
Modified RobotGo to avoid github.com/kbinani/screenshot with deprecated CGDisplayCreateImageForRect macOS15
This commit is contained in:
parent
a299ce776d
commit
0ff21b8cab
14
go.mod
14
go.mod
@ -2,8 +2,6 @@ module github.com/yarda7/robotgo
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/yarda7/robotgo v0.110.4
|
||||
|
||||
require (
|
||||
// github.com/robotn/gohook v0.31.3
|
||||
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934
|
||||
@ -15,12 +13,20 @@ require (
|
||||
github.com/vcaesar/tt v0.20.1
|
||||
)
|
||||
|
||||
require github.com/otiai10/gosseract v2.2.1+incompatible
|
||||
require (
|
||||
github.com/go-vgo/robotgo v0.110.2
|
||||
github.com/otiai10/gosseract v2.2.1+incompatible
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dblohm7/wingoes v0.0.0-20231019175336-f6e33aa7cc34 // indirect
|
||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/jezek/xgb v1.1.0 // indirect
|
||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
|
||||
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
|
||||
github.com/otiai10/mint v1.6.3 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
|
||||
github.com/shirou/gopsutil/v4 v4.24.5 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
@ -28,8 +34,8 @@ require (
|
||||
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
golang.org/x/image v0.18.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
)
|
||||
|
||||
// replace golang.org/x/sys => github.com/golang/sys v0.0.0-20190109145017-48ac38b7c8cb
|
||||
|
||||
|
14
go.sum
14
go.sum
@ -5,19 +5,30 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dblohm7/wingoes v0.0.0-20231019175336-f6e33aa7cc34 h1:FBMro26TLQwBk+n4fbTSmSf3QUKb09pvW4fz49lxpl0=
|
||||
github.com/dblohm7/wingoes v0.0.0-20231019175336-f6e33aa7cc34/go.mod h1:6NCrWM5jRefaG7iN0iMShPalLsljHWBh9v1zxM2f8Xs=
|
||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 h1:VLEKvjGJYAMCXw0/32r9io61tEXnMWDRxMk+peyRVFc=
|
||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/go-vgo/robotgo v0.110.2 h1:7uCrK5DSztrtwM1T6LAp6b6gVhl+XUD8Tpwzoy1SclU=
|
||||
github.com/go-vgo/robotgo v0.110.2/go.mod h1:ERKRRTIWCsypcrxWrUt/0ZXl5nVsUQkutYARvW22q98=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 h1:YOp8St+CM/AQ9Vp4XYm4272E77MptJDHkwypQHIRl9Q=
|
||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237/go.mod h1:e7qQlOY68wOz4b82D7n+DdaptZAi+SHW0+yKiWZzEYE=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik=
|
||||
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE=
|
||||
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
|
||||
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
|
||||
github.com/otiai10/gosseract v2.2.1+incompatible h1:Ry5ltVdpdp4LAa2bMjsSJH34XHVOV7XMi41HtzL8X2I=
|
||||
github.com/otiai10/gosseract v2.2.1+incompatible/go.mod h1:XrzWItCzCpFRZ35n3YtVTgq5bLAhFIkascoRo8G32QE=
|
||||
github.com/otiai10/gosseract/v2 v2.4.1/go.mod h1:1gNWP4Hgr2o7yqWfs6r5bZxAatjOIdqWxJLWsTsembk=
|
||||
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
|
||||
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@ -58,8 +69,6 @@ github.com/vcaesar/keycode v0.10.1 h1:0DesGmMAPWpYTCYddOFiCMKCDKgNnwiQa2QXindVUH
|
||||
github.com/vcaesar/keycode v0.10.1/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ=
|
||||
github.com/vcaesar/tt v0.20.1 h1:D/jUeeVCNbq3ad8M7hhtB3J9x5RZ6I1n1eZ0BJp7M+4=
|
||||
github.com/vcaesar/tt v0.20.1/go.mod h1:cH2+AwGAJm19Wa6xvEa+0r+sXDJBT0QgNQey6mwqLeU=
|
||||
github.com/yarda7/robotgo v0.110.3 h1:u1LaxnTx4BTw5STpU3vf05np0dTWPhK7C38jv30Y+IA=
|
||||
github.com/yarda7/robotgo v0.110.3/go.mod h1:B6zSZD7WXZXbo7TIwkGYLDwHyQrrsZr3J0kHmtjwZyw=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
@ -102,6 +111,7 @@ golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
@ -1,121 +0,0 @@
|
||||
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#include "../base/types.h"
|
||||
#include "../base/pubs.h"
|
||||
#include "../base/rgb.h"
|
||||
#include "screengrab_c.h"
|
||||
#include "screen_c.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void padHex(MMRGBHex color, char* hex) {
|
||||
// Length needs to be 7 because snprintf includes a terminating null.
|
||||
snprintf(hex, 7, "%06x", color);
|
||||
}
|
||||
|
||||
char* pad_hex(MMRGBHex color) {
|
||||
char hex[7];
|
||||
padHex(color, hex);
|
||||
// destroyMMBitmap(bitmap);
|
||||
|
||||
char* str = (char*)calloc(100, sizeof(char*));
|
||||
if (str) { strcpy(str, hex); }
|
||||
return str;
|
||||
}
|
||||
|
||||
static uint8_t rgb[3];
|
||||
|
||||
uint8_t* color_hex_to_rgb(uint32_t h) {
|
||||
rgb[0] = RED_FROM_HEX(h);
|
||||
rgb[1] = GREEN_FROM_HEX(h);
|
||||
rgb[2] = BLUE_FROM_HEX(h);
|
||||
return rgb;
|
||||
}
|
||||
|
||||
uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b) {
|
||||
return RGB_TO_HEX(r, g, b);
|
||||
}
|
||||
|
||||
MMRGBHex get_px_color(int32_t x, int32_t y, int32_t display_id) {
|
||||
MMBitmapRef bitmap;
|
||||
MMRGBHex color;
|
||||
|
||||
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))) {
|
||||
return color;
|
||||
}
|
||||
|
||||
bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1), display_id, 0);
|
||||
color = MMRGBHexAtPoint(bitmap, 0, 0);
|
||||
destroyMMBitmap(bitmap);
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
char* set_XDisplay_name(char* name) {
|
||||
#if defined(USE_X11)
|
||||
setXDisplay(name);
|
||||
return "";
|
||||
#else
|
||||
return "SetXDisplayName is only supported on Linux";
|
||||
#endif
|
||||
}
|
||||
|
||||
char* get_XDisplay_name() {
|
||||
#if defined(USE_X11)
|
||||
const char* display = getXDisplay();
|
||||
|
||||
char* sd = (char*)calloc(100, sizeof(char*));
|
||||
if (sd) { strcpy(sd, display); }
|
||||
return sd;
|
||||
#else
|
||||
return "GetXDisplayName is only supported on Linux";
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t get_num_displays() {
|
||||
#if defined(IS_MACOSX)
|
||||
uint32_t count = 0;
|
||||
if (CGGetActiveDisplayList(0, nil, &count) == kCGErrorSuccess) {
|
||||
return count;
|
||||
}
|
||||
return 0;
|
||||
#elif defined(USE_X11)
|
||||
return 0;
|
||||
#elif defined(IS_WINDOWS)
|
||||
uint32_t count = 0;
|
||||
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&count)) {
|
||||
return count;
|
||||
}
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
uintptr get_hwnd_by_pid(uintptr pid) {
|
||||
#if defined(IS_WINDOWS)
|
||||
HWND hwnd = GetHwndByPid(pid);
|
||||
return (uintptr)hwnd;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void bitmap_dealloc(MMBitmapRef bitmap) {
|
||||
if (bitmap != NULL) {
|
||||
destroyMMBitmap(bitmap);
|
||||
bitmap = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// capture_screen capture screen
|
||||
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;
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package screen
|
@ -1,73 +0,0 @@
|
||||
//#include "../base/os.h"
|
||||
|
||||
#if defined(IS_MACOSX)
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#elif defined(USE_X11)
|
||||
#include <X11/Xlib.h>
|
||||
// #include "../base/xdisplay_c.h"
|
||||
#endif
|
||||
|
||||
MMSizeInt32 getMainDisplaySize(void) {
|
||||
#if defined(IS_MACOSX)
|
||||
CGDirectDisplayID displayID = CGMainDisplayID();
|
||||
CGRect displayRect = CGDisplayBounds(displayID);
|
||||
|
||||
CGSize size = displayRect.size;
|
||||
return MMSizeInt32Make((int32_t)size.width, (int32_t)size.height);
|
||||
#elif defined(USE_X11)
|
||||
Display *display = XGetMainDisplay();
|
||||
const int screen = DefaultScreen(display);
|
||||
|
||||
return MMSizeInt32Make(
|
||||
(int32_t)DisplayWidth(display, screen),
|
||||
(int32_t)DisplayHeight(display, screen));
|
||||
#elif defined(IS_WINDOWS)
|
||||
return MMSizeInt32Make(
|
||||
(int32_t)GetSystemMetrics(SM_CXSCREEN),
|
||||
(int32_t)GetSystemMetrics(SM_CYSCREEN));
|
||||
#endif
|
||||
}
|
||||
|
||||
MMRectInt32 getScreenRect(int32_t display_id) {
|
||||
#if defined(IS_MACOSX)
|
||||
CGDirectDisplayID displayID = (CGDirectDisplayID) display_id;
|
||||
if (display_id == -1) {
|
||||
displayID = CGMainDisplayID();
|
||||
}
|
||||
CGRect displayRect = CGDisplayBounds(displayID);
|
||||
|
||||
CGPoint point = displayRect.origin;
|
||||
CGSize size = displayRect.size;
|
||||
return MMRectInt32Make(
|
||||
(int32_t)point.x, (int32_t)point.y,
|
||||
(int32_t)size.width, (int32_t)size.height);
|
||||
#elif defined(USE_X11)
|
||||
Display *display = XGetMainDisplay();
|
||||
const int screen = DefaultScreen(display);
|
||||
|
||||
return MMRectInt32Make(
|
||||
(int32_t)0, (int32_t)0,
|
||||
(int32_t)DisplayWidth(display, screen),
|
||||
(int32_t)DisplayHeight(display, screen));
|
||||
#elif defined(IS_WINDOWS)
|
||||
if (GetSystemMetrics(SM_CMONITORS) == 1
|
||||
|| display_id == -1 || display_id == 0) {
|
||||
return MMRectInt32Make(
|
||||
(int32_t)0,
|
||||
(int32_t)0,
|
||||
(int32_t)GetSystemMetrics(SM_CXSCREEN),
|
||||
(int32_t)GetSystemMetrics(SM_CYSCREEN));
|
||||
} else {
|
||||
return MMRectInt32Make(
|
||||
(int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
|
||||
(int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
|
||||
(int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
|
||||
(int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool pointVisibleOnMainDisplay(MMPointInt32 point){
|
||||
MMSizeInt32 displaySize = getMainDisplaySize();
|
||||
return point.x < displaySize.w && point.y < displaySize.h;
|
||||
}
|
@ -1,135 +0,0 @@
|
||||
#include "../base/bitmap_free_c.h"
|
||||
#include <stdlib.h> /* malloc() */
|
||||
|
||||
#if defined(IS_MACOSX)
|
||||
#include <OpenGL/OpenGL.h>
|
||||
#include <OpenGL/gl.h>
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#elif defined(USE_X11)
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include "../base/xdisplay_c.h"
|
||||
#elif defined(IS_WINDOWS)
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id, int8_t isPid) {
|
||||
#if defined(IS_MACOSX)
|
||||
MMBitmapRef bitmap = NULL;
|
||||
uint8_t *buffer = NULL;
|
||||
size_t bufferSize = 0;
|
||||
|
||||
CGDirectDisplayID displayID = (CGDirectDisplayID) display_id;
|
||||
if (displayID == -1 || displayID == 0) {
|
||||
displayID = CGMainDisplayID();
|
||||
}
|
||||
|
||||
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));
|
||||
if (!imageData) { return NULL; }
|
||||
|
||||
bufferSize = CFDataGetLength(imageData);
|
||||
buffer = malloc(bufferSize);
|
||||
CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer);
|
||||
|
||||
bitmap = createMMBitmap_c(buffer,
|
||||
CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBytesPerRow(image),
|
||||
CGImageGetBitsPerPixel(image), CGImageGetBitsPerPixel(image) / 8);
|
||||
|
||||
CFRelease(imageData);
|
||||
CGImageRelease(image);
|
||||
|
||||
return bitmap;
|
||||
#elif defined(USE_X11)
|
||||
MMBitmapRef bitmap;
|
||||
Display *display;
|
||||
if (display_id == -1) {
|
||||
display = XOpenDisplay(NULL);
|
||||
} else {
|
||||
display = XGetMainDisplay();
|
||||
}
|
||||
|
||||
MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
|
||||
XImage *image = XGetImage(display, XDefaultRootWindow(display),
|
||||
(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,
|
||||
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);
|
||||
|
||||
return bitmap;
|
||||
#elif defined(IS_WINDOWS)
|
||||
MMBitmapRef bitmap;
|
||||
void *data;
|
||||
HDC screen = NULL, screenMem = NULL;
|
||||
HBITMAP dib;
|
||||
BITMAPINFO bi;
|
||||
|
||||
int32_t x = rect.origin.x, y = rect.origin.y;
|
||||
int32_t w = rect.size.w, h = rect.size.h;
|
||||
|
||||
/* Initialize bitmap info. */
|
||||
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
||||
bi.bmiHeader.biWidth = (long) w;
|
||||
bi.bmiHeader.biHeight = -(long) h; /* Non-cartesian, please */
|
||||
bi.bmiHeader.biPlanes = 1;
|
||||
bi.bmiHeader.biBitCount = 32;
|
||||
bi.bmiHeader.biCompression = BI_RGB;
|
||||
bi.bmiHeader.biSizeImage = (DWORD)(4 * w * h);
|
||||
bi.bmiHeader.biXPelsPerMeter = 0;
|
||||
bi.bmiHeader.biYPelsPerMeter = 0;
|
||||
bi.bmiHeader.biClrUsed = 0;
|
||||
bi.bmiHeader.biClrImportant = 0;
|
||||
|
||||
HWND hwnd;
|
||||
if (display_id == -1 || isPid == 0) {
|
||||
// screen = GetDC(NULL); /* Get entire screen */
|
||||
hwnd = GetDesktopWindow();
|
||||
} else {
|
||||
hwnd = (HWND) (uintptr) display_id;
|
||||
}
|
||||
screen = GetDC(hwnd);
|
||||
|
||||
if (screen == NULL) { return NULL; }
|
||||
|
||||
// Todo: Use DXGI
|
||||
screenMem = CreateCompatibleDC(screen);
|
||||
/* Get screen data in display device context. */
|
||||
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);
|
||||
|
||||
/* Copy the data into a bitmap struct. */
|
||||
BOOL b = (screenMem == NULL) ||
|
||||
SelectObject(screenMem, dib) == NULL ||
|
||||
!BitBlt(screenMem, (int)0, (int)0, (int)w, (int)h, screen, x, y, SRCCOPY);
|
||||
if (b) {
|
||||
/* Error copying data. */
|
||||
ReleaseDC(hwnd, screen);
|
||||
DeleteObject(dib);
|
||||
if (screenMem != NULL) { DeleteDC(screenMem); }
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bitmap = createMMBitmap_c(NULL, w, h, 4 * w, (uint8_t)bi.bmiHeader.biBitCount, 4);
|
||||
|
||||
/* Copy the data to our pixel buffer. */
|
||||
if (bitmap != NULL) {
|
||||
bitmap->imageBuffer = malloc(bitmap->bytewidth * bitmap->height);
|
||||
memcpy(bitmap->imageBuffer, data, bitmap->bytewidth * bitmap->height);
|
||||
}
|
||||
|
||||
ReleaseDC(hwnd, screen);
|
||||
DeleteObject(dib);
|
||||
DeleteDC(screenMem);
|
||||
|
||||
return bitmap;
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue
Block a user