Skip to content

Commit 897af2a

Browse files
authored
Merge pull request #442 from go-vgo/bitmap-pr
Add more multi screen support, update go mod, remove some code
2 parents 4021a8b + acfb61e commit 897af2a

File tree

15 files changed

+290
-572
lines changed

15 files changed

+290
-572
lines changed

base/os.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,18 @@
4646
#define ADD_PADDING(width) (BYTE_ALIGN + (((width) - 1) & ~(BYTE_ALIGN - 1)))
4747
#endif
4848

49+
#if defined(IS_WINDOWS)
50+
#if defined (_WIN64)
51+
#define RobotGo_64
52+
#else
53+
#define RobotGo_32
54+
#endif
55+
#else
56+
#if defined (__x86_64__)
57+
#define RobotGo_64
58+
#else
59+
#define RobotGo_32
60+
#endif
61+
#endif
62+
4963
#endif /* OS_H */

base/win32.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if defined(IS_WINDOWS)
2+
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
3+
uint32_t *count = (uint32_t*)dwData;
4+
(*count)++;
5+
return TRUE;
6+
}
7+
8+
typedef struct{
9+
HWND hWnd;
10+
DWORD dwPid;
11+
}WNDINFO;
12+
13+
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam){
14+
WNDINFO* pInfo = (WNDINFO*)lParam;
15+
DWORD dwProcessId = 0;
16+
GetWindowThreadProcessId(hWnd, &dwProcessId);
17+
18+
if (dwProcessId == pInfo->dwPid) {
19+
pInfo->hWnd = hWnd;
20+
return FALSE;
21+
}
22+
return TRUE;
23+
}
24+
25+
HWND GetHwndByPId(DWORD dwProcessId) {
26+
WNDINFO info = {0};
27+
info.hWnd = NULL;
28+
info.dwPid = dwProcessId;
29+
EnumWindows(EnumWindowsProc, (LPARAM)&info);
30+
// printf("%d\n", info.hWnd);
31+
return info.hWnd;
32+
}
33+
34+
#endif

examples/screen/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
)
1919

2020
func bitmap() {
21-
abitMap := robotgo.CaptureScreen()
22-
fmt.Println("abitMap...", abitMap)
23-
gbit := robotgo.ToBitmap(abitMap)
21+
bit := robotgo.CaptureScreen()
22+
fmt.Println("abitMap...", bit)
23+
gbit := robotgo.ToBitmap(bit)
2424
fmt.Println("bitmap...", gbit.Width)
2525

2626
gbitMap := robotgo.CaptureGo()

go.mod

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
// github.com/robotn/gohook v0.31.3
99
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934
1010
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770
11-
github.com/vcaesar/gops v0.21.3
11+
github.com/vcaesar/gops v0.22.0
1212
github.com/vcaesar/imgo v0.30.0
1313
github.com/vcaesar/keycode v0.10.0
1414
github.com/vcaesar/tt v0.20.0
@@ -17,15 +17,16 @@ require (
1717
)
1818

1919
require (
20-
github.com/StackExchange/wmi v1.2.1 // indirect
2120
github.com/davecgh/go-spew v1.1.1 // indirect
2221
github.com/go-ole/go-ole v1.2.6 // indirect
22+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
2323
github.com/otiai10/mint v1.3.0 // indirect
24-
github.com/pmezard/go-difflib v1.0.0 // indirect
25-
github.com/shirou/gopsutil v3.21.10+incompatible // indirect
24+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
25+
github.com/shirou/gopsutil/v3 v3.21.12 // indirect
2626
github.com/tklauser/go-sysconf v0.3.9 // indirect
2727
github.com/tklauser/numcpus v0.3.0 // indirect
28-
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab // indirect
28+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
29+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
2930
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
3031
)
3132

go.sum

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ=
22
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966/go.mod h1:Mid70uvE93zn9wgF92A/r5ixgnvX8Lh68fxp9KQBaI0=
3-
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
4-
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
53
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
64
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
75
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8-
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
96
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
107
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
8+
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
9+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
10+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
11+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1112
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
1213
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
1314
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
@@ -17,27 +18,31 @@ github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc=
1718
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
1819
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1920
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
22+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
2023
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934 h1:2lhSR8N3T6I30q096DT7/5AKEIcf1vvnnWAmS0wfnNY=
2124
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934/go.mod h1:SxQhJskUJ4rleVU44YvnrdvxQr0tKy5SRSigBrCgyyQ=
2225
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770 h1:2uX8QRLkkxn2EpAQ6I3KhA79BkdRZfvugJUzJadiJwk=
2326
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770/go.mod h1:svkDXUDQjUiWzLrA0OZgHc4lbOts3C+uRfP6/yjwYnU=
24-
github.com/shirou/gopsutil v3.21.10+incompatible h1:AL2kpVykjkqeN+MFe1WcwSBVUjGjvdU8/ubvCuXAjrU=
25-
github.com/shirou/gopsutil v3.21.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
27+
github.com/shirou/gopsutil/v3 v3.21.12 h1:VoGxEW2hpmz0Vt3wUvHIl9fquzYLNpVpgNNB7pGJimA=
28+
github.com/shirou/gopsutil/v3 v3.21.12/go.mod h1:BToYZVTlSVlfazpDDYFnsVZLaoRG+g8ufT6fPQLdJzA=
2629
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
27-
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
28-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
30+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
31+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2932
github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=
3033
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
3134
github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2biQ=
3235
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
33-
github.com/vcaesar/gops v0.21.3 h1:VR7amkxVv9CQfsotkXrmMyT19dVuNTa1PM/oopJeIc0=
34-
github.com/vcaesar/gops v0.21.3/go.mod h1:3e2EnlZTI9/44bqzRwkeZ3s0ZQwK2Cn4QPLx8Ii8Agk=
36+
github.com/vcaesar/gops v0.22.0 h1:aWHWxY3fvUuaJGph+LegYUSEU/874WJT7MInd8OUmP0=
37+
github.com/vcaesar/gops v0.22.0/go.mod h1:GFNGo9xpCfjcfd/Hovi9RSrpd4FdaQt8V92TzpU22w4=
3538
github.com/vcaesar/imgo v0.30.0 h1:ODQVX0EFJEh+WkKahCBtE0SqcDCIjl/kjiOplR0Ouh8=
3639
github.com/vcaesar/imgo v0.30.0/go.mod h1:8TGnt5hjaMgwDByvMFIzUDSh5uSea4n1tAbSvnhvA6U=
3740
github.com/vcaesar/keycode v0.10.0 h1:Qx5QE8ZXHyRyjoA2QOxBp25OKMKB+zxMVqm0FWGV0d4=
3841
github.com/vcaesar/keycode v0.10.0/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ=
3942
github.com/vcaesar/tt v0.20.0 h1:9t2Ycb9RNHcP0WgQgIaRKJBB+FrRdejuaL6uWIHuoBA=
4043
github.com/vcaesar/tt v0.20.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
44+
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
45+
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
4146
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4247
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
4348
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
@@ -51,13 +56,17 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
5156
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5257
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5358
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
59+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5460
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55-
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab h1:rfJ1bsoJQQIAoAxTxB7bme+vHrNkRw8CqfsYh9w54cw=
56-
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
61+
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
62+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
63+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5764
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5865
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5966
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
6067
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
68+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
69+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6170
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6271
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6372
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

screen/goScreen.h

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010

1111
#include "../base/types.h"
1212
#include "../base/rgb.h"
13+
#include "../base/win32.h"
1314
#include "screengrab_c.h"
1415
#include "screen_c.h"
1516
#include <stdio.h>
1617
// #include "../MMBitmap_c.h"
1718

18-
void padHex(MMRGBHex color, char* hex){
19+
void padHex(MMRGBHex color, char* hex) {
1920
// Length needs to be 7 because snprintf includes a terminating null.
2021
// Use %06x to pad hex value with leading 0s.
2122
snprintf(hex, 7, "%06x", color);
2223
}
2324

24-
char* pad_hex(MMRGBHex color){
25+
char* pad_hex(MMRGBHex color) {
2526
char hex[7];
2627
padHex(color, hex);
2728
// destroyMMBitmap(bitmap);
@@ -34,22 +35,22 @@ char* pad_hex(MMRGBHex color){
3435

3536
static uint8_t rgb[3];
3637

37-
uint8_t* color_hex_to_rgb(uint32_t h){
38+
uint8_t* color_hex_to_rgb(uint32_t h) {
3839
rgb[0] = RED_FROM_HEX(h);
3940
rgb[1] = GREEN_FROM_HEX(h);
4041
rgb[2] = BLUE_FROM_HEX(h);
4142
return rgb;
4243
}
4344

44-
uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b){
45+
uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b) {
4546
return RGB_TO_HEX(r, g, b);
4647
}
4748

4849
MMRGBHex get_px_color(int32_t x, int32_t y, int32_t display_id) {
4950
MMBitmapRef bitmap;
5051
MMRGBHex color;
5152

52-
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))){
53+
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))) {
5354
return color;
5455
}
5556

@@ -69,13 +70,13 @@ char* get_pixel_color(int32_t x, int32_t y, int32_t display_id) {
6970
return s;
7071
}
7172

72-
MMSizeInt32 get_screen_size(){
73+
MMSizeInt32 get_screen_size() {
7374
// Get display size.
7475
MMSizeInt32 displaySize = getMainDisplaySize();
7576
return displaySize;
7677
}
7778

78-
char* set_XDisplay_name(char* name){
79+
char* set_XDisplay_name(char* name) {
7980
#if defined(USE_X11)
8081
setXDisplay(name);
8182
return "success";
@@ -84,7 +85,7 @@ char* set_XDisplay_name(char* name){
8485
#endif
8586
}
8687

87-
char* get_XDisplay_name(){
88+
char* get_XDisplay_name() {
8889
#if defined(USE_X11)
8990
const char* display = getXDisplay();
9091
char* sd = (char*)calloc(100, sizeof(char*));
@@ -96,34 +97,26 @@ char* get_XDisplay_name(){
9697
#endif
9798
}
9899

99-
#if defined(IS_WINDOWS)
100-
bool CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
101-
uint32_t *count = (uint32_t*)dwData;
102-
(*count)++;
103-
return true;
104-
}
105-
#endif
106-
107100
uint32_t get_num_displays() {
108-
#if defined(IS_MACOSX)
109-
uint32_t count = 0;
110-
if (CGGetActiveDisplayList(0, nil, &count) == kCGErrorSuccess) {
111-
return count;
112-
}
113-
return 0;
114-
#elif defined(USE_X11)
115-
return 0;
116-
#elif defined(IS_WINDOWS)
117-
uint32_t count = 0;
118-
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&count)) {
119-
return count;
120-
}
121-
return 0;
122-
#endif
101+
#if defined(IS_MACOSX)
102+
uint32_t count = 0;
103+
if (CGGetActiveDisplayList(0, nil, &count) == kCGErrorSuccess) {
104+
return count;
105+
}
106+
return 0;
107+
#elif defined(USE_X11)
108+
return 0;
109+
#elif defined(IS_WINDOWS)
110+
uint32_t count = 0;
111+
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&count)) {
112+
return count;
113+
}
114+
return 0;
115+
#endif
123116
}
124117

125118

126-
void bitmap_dealloc(MMBitmapRef bitmap){
119+
void bitmap_dealloc(MMBitmapRef bitmap) {
127120
if (bitmap != NULL) {
128121
destroyMMBitmap(bitmap);
129122
bitmap = NULL;

screen/screengrab_c.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
#include <string.h>
1818
#endif
1919

20-
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id){
20+
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id) {
2121
#if defined(IS_MACOSX)
22-
2322
MMBitmapRef bitmap = NULL;
2423
uint8_t *buffer = NULL;
2524
size_t bufferSize = 0;
2625

2726
CGDirectDisplayID displayID = (CGDirectDisplayID) display_id;
28-
2927
if (displayID == -1) {
3028
displayID = CGMainDisplayID();
3129
}
@@ -43,7 +41,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id){
4341
CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer);
4442

4543
bitmap = createMMBitmap_c(buffer,
46-
CGImageGetWidth(image),CGImageGetHeight(image),
44+
CGImageGetWidth(image), CGImageGetHeight(image),
4745
CGImageGetBytesPerRow(image), CGImageGetBitsPerPixel(image),
4846
CGImageGetBitsPerPixel(image) / 8);
4947

@@ -53,20 +51,24 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id){
5351
return bitmap;
5452
#elif defined(USE_X11)
5553
MMBitmapRef bitmap;
54+
Display *display;
55+
if (display_id == -1) {
56+
display = XOpenDisplay(NULL);
57+
} else {
58+
display = XGetMainDisplay();
59+
}
5660

57-
Display *display = XOpenDisplay(NULL);
5861
XImage *image = XGetImage(display, XDefaultRootWindow(display),
5962
(int)rect.origin.x, (int)rect.origin.y,
60-
(unsigned int)rect.size.w,(unsigned int)rect.size.h,
63+
(unsigned int)rect.size.w, (unsigned int)rect.size.h,
6164
AllPlanes, ZPixmap);
6265
XCloseDisplay(display);
6366
if (image == NULL) { return NULL; }
6467

6568
bitmap = createMMBitmap_c((uint8_t *)image->data,
6669
rect.size.w, rect.size.h, (size_t)image->bytes_per_line,
6770
(uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
68-
image->data = NULL; /* Steal ownership of bitmap data so we don't have to
69-
* copy it. */
71+
image->data = NULL; /* Steal ownership of bitmap data so we don't have to copy it. */
7072
XDestroyImage(image);
7173

7274
return bitmap;
@@ -90,7 +92,11 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id){
9092
bi.bmiHeader.biClrUsed = 0;
9193
bi.bmiHeader.biClrImportant = 0;
9294

93-
screen = GetDC(NULL); /* Get entire screen */
95+
if (display_id == -1) {
96+
screen = GetDC(NULL); /* Get entire screen */
97+
} else {
98+
screen = GetDC((HWND) display_id);
99+
}
94100
if (screen == NULL) { return NULL; }
95101

96102
/* Get screen data in display device context. */

0 commit comments

Comments
 (0)