Merge pull request #691 from go-vgo/bitmap-pr

Update: move scale to screen and rename pub mdata
This commit is contained in:
Evans 2024-10-06 17:54:40 -07:00 committed by GitHub
commit 1923d7bb48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 106 additions and 104 deletions

View File

@ -4,9 +4,70 @@
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
#elif defined(USE_X11) #elif defined(USE_X11)
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xresource.h>
// #include "../base/xdisplay_c.h" // #include "../base/xdisplay_c.h"
#endif #endif
intptr scaleX();
double sys_scale(int32_t display_id) {
#if defined(IS_MACOSX)
CGDirectDisplayID displayID = (CGDirectDisplayID) display_id;
if (displayID == -1) {
displayID = CGMainDisplayID();
}
CGDisplayModeRef modeRef = CGDisplayCopyDisplayMode(displayID);
double pixelWidth = CGDisplayModeGetPixelWidth(modeRef);
double targetWidth = CGDisplayModeGetWidth(modeRef);
return pixelWidth / targetWidth;
#elif defined(USE_X11)
Display *dpy = XOpenDisplay(NULL);
int scr = 0; /* Screen number */
double xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) /
((double) DisplayWidthMM(dpy, scr)));
char *rms = XResourceManagerString(dpy);
if (rms) {
XrmDatabase db = XrmGetStringDatabase(rms);
if (db) {
XrmValue value;
char *type = NULL;
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value)) {
if (value.addr) {
xres = atof(value.addr);
}
}
XrmDestroyDatabase(db);
}
}
XCloseDisplay (dpy);
return xres / 96.0;
#elif defined(IS_WINDOWS)
double s = scaleX() / 96.0;
return s;
#endif
}
intptr scaleX(){
#if defined(IS_MACOSX)
return 0;
#elif defined(USE_X11)
return 0;
#elif defined(IS_WINDOWS)
// Get desktop dc
HDC desktopDc = GetDC(NULL);
// Get native resolution
intptr horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
return horizontalDPI;
#endif
}
MMSizeInt32 getMainDisplaySize(void) { MMSizeInt32 getMainDisplaySize(void) {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CGDirectDisplayID displayID = CGMainDisplayID(); CGDirectDisplayID displayID = CGMainDisplayID();

View File

@ -55,11 +55,11 @@ uintptr get_handle(){
uintptr b_get_handle() { uintptr b_get_handle() {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
return (uintptr)mData.CgID; return (uintptr)pub_mData.CgID;
#elif defined(USE_X11) #elif defined(USE_X11)
return (uintptr)mData.XWin; return (uintptr)pub_mData.XWin;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return (uintptr)mData.HWnd; return (uintptr)pub_mData.HWnd;
#endif #endif
} }

View File

@ -28,7 +28,7 @@ struct _MData{
}; };
typedef struct _MData MData; typedef struct _MData MData;
MData mData; MData pub_mData;
struct _Bounds { struct _Bounds {
int32_t X; // Top left X coordinate int32_t X; // Top left X coordinate

View File

@ -8,70 +8,11 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#if defined(USE_X11) // #if defined(USE_X11)
#include <X11/Xresource.h> // #include <X11/Xresource.h>
#endif // #endif
Bounds get_client(uintptr pid, int8_t isPid); Bounds get_client(uintptr pid, int8_t isPid);
intptr scaleX();
double sys_scale(int32_t display_id) {
#if defined(IS_MACOSX)
CGDirectDisplayID displayID = (CGDirectDisplayID) display_id;
if (displayID == -1) {
displayID = CGMainDisplayID();
}
CGDisplayModeRef modeRef = CGDisplayCopyDisplayMode(displayID);
double pixelWidth = CGDisplayModeGetPixelWidth(modeRef);
double targetWidth = CGDisplayModeGetWidth(modeRef);
return pixelWidth / targetWidth;
#elif defined(USE_X11)
Display *dpy = XOpenDisplay(NULL);
int scr = 0; /* Screen number */
double xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) /
((double) DisplayWidthMM(dpy, scr)));
char *rms = XResourceManagerString(dpy);
if (rms) {
XrmDatabase db = XrmGetStringDatabase(rms);
if (db) {
XrmValue value;
char *type = NULL;
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value)) {
if (value.addr) {
xres = atof(value.addr);
}
}
XrmDestroyDatabase(db);
}
}
XCloseDisplay (dpy);
return xres / 96.0;
#elif defined(IS_WINDOWS)
double s = scaleX() / 96.0;
return s;
#endif
}
intptr scaleX(){
#if defined(IS_MACOSX)
return 0;
#elif defined(USE_X11)
return 0;
#elif defined(IS_WINDOWS)
// Get desktop dc
HDC desktopDc = GetDC(NULL);
// Get native resolution
intptr horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
return horizontalDPI;
#endif
}
Bounds get_bounds(uintptr pid, int8_t isPid){ Bounds get_bounds(uintptr pid, int8_t isPid){
// Check if the window is valid // Check if the window is valid

View File

@ -24,8 +24,8 @@ uintptr initHandle = 0;
void initWindow(uintptr handle){ void initWindow(uintptr handle){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
mData.CgID = 0; pub_mData.CgID = 0;
mData.AxID = 0; pub_mData.AxID = 0;
#elif defined(USE_X11) #elif defined(USE_X11)
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// If atoms loaded // If atoms loaded
@ -34,10 +34,10 @@ void initWindow(uintptr handle){
if (rDisplay != NULL) {LoadAtoms();} if (rDisplay != NULL) {LoadAtoms();}
} }
mData.XWin = 0; pub_mData.XWin = 0;
XCloseDisplay(rDisplay); XCloseDisplay(rDisplay);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
mData.HWnd = 0; pub_mData.HWnd = 0;
#endif #endif
setHandle(handle); setHandle(handle);
} }
@ -67,33 +67,33 @@ MData set_handle_pid(uintptr pid, int8_t isPid){
void set_handle_pid_mData(uintptr pid, int8_t isPid){ void set_handle_pid_mData(uintptr pid, int8_t isPid){
MData win = set_handle_pid(pid, isPid); MData win = set_handle_pid(pid, isPid);
mData = win; pub_mData = win;
} }
bool is_valid() { bool is_valid() {
initWindow(initHandle); initWindow(initHandle);
if (!IsAxEnabled(true)) { if (!IsAxEnabled(true)) {
printf("%s\n", "Window: Accessibility API is disabled!\n" printf("%s\n", "Window: Accessibility API is disabled! "
"Failed to enable access for assistive devices."); "Failed to enable access for assistive devices. \n");
} }
MData actdata = get_active(); MData actdata = get_active();
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
mData.CgID = actdata.CgID; pub_mData.CgID = actdata.CgID;
mData.AxID = actdata.AxID; pub_mData.AxID = actdata.AxID;
if (mData.CgID == 0 || mData.AxID == 0) { return false; } if (pub_mData.CgID == 0 || pub_mData.AxID == 0) { return false; }
CFTypeRef r = NULL; CFTypeRef r = NULL;
// Attempt to get the window role // Attempt to get the window role
if (AXUIElementCopyAttributeValue(mData.AxID, kAXRoleAttribute, &r) == kAXErrorSuccess && r){ if (AXUIElementCopyAttributeValue(pub_mData.AxID, kAXRoleAttribute, &r) == kAXErrorSuccess && r){
CFRelease(r); CFRelease(r);
return true; return true;
} }
return false; return false;
#elif defined(USE_X11) #elif defined(USE_X11)
mData.XWin = actdata.XWin; pub_mData.XWin = actdata.XWin;
if (mData.XWin == 0) { return false; } if (pub_mData.XWin == 0) { return false; }
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Check for a valid X-Window display // Check for a valid X-Window display
@ -103,7 +103,7 @@ bool is_valid() {
XDismissErrors(); XDismissErrors();
// Get the window PID property // Get the window PID property
void* result = GetWindowProperty(mData, WM_PID,NULL); void* result = GetWindowProperty(pub_mData, WM_PID,NULL);
if (result == NULL) { if (result == NULL) {
XCloseDisplay(rDisplay); XCloseDisplay(rDisplay);
return false; return false;
@ -115,12 +115,12 @@ bool is_valid() {
return true; return true;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
mData.HWnd = actdata.HWnd; pub_mData.HWnd = actdata.HWnd;
if (mData.HWnd == 0) { if (pub_mData.HWnd == 0) {
return false; return false;
} }
return IsWindow(mData.HWnd) != 0; return IsWindow(pub_mData.HWnd) != 0;
#endif #endif
} }
@ -175,13 +175,13 @@ bool IsAxEnabled(bool options){
bool setHandle(uintptr handle){ bool setHandle(uintptr handle){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
// Release the AX element // Release the AX element
if (mData.AxID != NULL) { if (pub_mData.AxID != NULL) {
CFRelease(mData.AxID); CFRelease(pub_mData.AxID);
} }
// Reset both values // Reset both values
mData.CgID = 0; pub_mData.CgID = 0;
mData.AxID = 0; pub_mData.AxID = 0;
if (handle == 0) { if (handle == 0) {
// return 0; // return 0;
@ -192,8 +192,8 @@ bool setHandle(uintptr handle){
CGWindowID cgID = (CGWindowID)handle; CGWindowID cgID = (CGWindowID)handle;
AXUIElementRef axID = GetUIElement(cgID); AXUIElementRef axID = GetUIElement(cgID);
if (axID != NULL){ if (axID != NULL){
mData.CgID = cgID; pub_mData.CgID = cgID;
mData.AxID = axID; pub_mData.AxID = axID;
// return 0; // return 0;
return true; return true;
} }
@ -201,7 +201,7 @@ bool setHandle(uintptr handle){
// return 1; // return 1;
return false; return false;
#elif defined(USE_X11) #elif defined(USE_X11)
mData.XWin = (Window)handle; pub_mData.XWin = (Window)handle;
if (handle == 0) { if (handle == 0) {
return true; return true;
} }
@ -210,10 +210,10 @@ bool setHandle(uintptr handle){
return true; return true;
} }
mData.XWin = 0; pub_mData.XWin = 0;
return false; return false;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
mData.HWnd = (HWND)handle; pub_mData.HWnd = (HWND)handle;
if (handle == 0) { if (handle == 0) {
return true; return true;
} }
@ -222,7 +222,7 @@ bool setHandle(uintptr handle){
return true; return true;
} }
mData.HWnd = 0; pub_mData.HWnd = 0;
return false; return false;
#endif #endif
} }
@ -237,7 +237,7 @@ bool IsTopMost(void){
// XDismissErrors (); // XDismissErrors ();
// return GetState (mData.XWin, STATE_TOPMOST); // return GetState (mData.XWin, STATE_TOPMOST);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return (GetWindowLongPtr(mData.HWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0; return (GetWindowLongPtr(pub_mData.HWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
#endif #endif
} }
@ -247,7 +247,7 @@ bool IsMinimized(void){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CFBooleanRef data = NULL; CFBooleanRef data = NULL;
// Determine whether the window is minimized // Determine whether the window is minimized
if (AXUIElementCopyAttributeValue(mData.AxID, kAXMinimizedAttribute, if (AXUIElementCopyAttributeValue(pub_mData.AxID, kAXMinimizedAttribute,
(CFTypeRef*) &data) == kAXErrorSuccess && data != NULL) { (CFTypeRef*) &data) == kAXErrorSuccess && data != NULL) {
// Convert resulting data into a bool // Convert resulting data into a bool
bool result = CFBooleanGetValue(data); bool result = CFBooleanGetValue(data);
@ -261,7 +261,7 @@ bool IsMinimized(void){
// XDismissErrors(); // XDismissErrors();
// return GetState(mData.XWin, STATE_MINIMIZE); // return GetState(mData.XWin, STATE_MINIMIZE);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return (GetWindowLongPtr(mData.HWnd, GWL_STYLE) & WS_MINIMIZE) != 0; return (GetWindowLongPtr(pub_mData.HWnd, GWL_STYLE) & WS_MINIMIZE) != 0;
#endif #endif
} }
@ -276,7 +276,7 @@ bool IsMaximized(void){
// XDismissErrors(); // XDismissErrors();
// return GetState(mData.XWin, STATE_MAXIMIZE); // return GetState(mData.XWin, STATE_MAXIMIZE);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return (GetWindowLongPtr(mData.HWnd, GWL_STYLE) & WS_MAXIMIZE) != 0; return (GetWindowLongPtr(pub_mData.HWnd, GWL_STYLE) & WS_MAXIMIZE) != 0;
#endif #endif
} }
@ -453,9 +453,9 @@ void SetTopMost(bool state){
#elif defined(USE_X11) #elif defined(USE_X11)
// Ignore X errors // Ignore X errors
// XDismissErrors(); // XDismissErrors();
// SetState(mData.XWin, STATE_TOPMOST, state); // SetState(pub_mData.XWin, STATE_TOPMOST, state);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
SetWindowPos(mData.HWnd, state ? HWND_TOPMOST : HWND_NOTOPMOST, SetWindowPos(pub_mData.HWnd, state ? HWND_TOPMOST : HWND_NOTOPMOST,
0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
#endif #endif
} }
@ -464,7 +464,7 @@ void close_main_window () {
// Check if the window is valid // Check if the window is valid
if (!is_valid()) { return; } if (!is_valid()) { return; }
close_window_by_Id(mData); close_window_by_Id(pub_mData);
} }
void close_window_by_PId(uintptr pid, int8_t isPid){ void close_window_by_PId(uintptr pid, int8_t isPid){
@ -502,7 +502,7 @@ char* get_main_title(){
// Check if the window is valid // Check if the window is valid
if (!is_valid()) { return "is_valid failed."; } if (!is_valid()) { return "is_valid failed."; }
return get_title_by_hand(mData); return get_title_by_hand(pub_mData);
} }
char* get_title_by_pid(uintptr pid, int8_t isPid){ char* get_title_by_pid(uintptr pid, int8_t isPid){
@ -591,7 +591,7 @@ int32_t get_PID(void) {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
pid_t pid = 0; pid_t pid = 0;
// Attempt to retrieve the window pid // Attempt to retrieve the window pid
if (AXUIElementGetPid(mData.AxID, &pid)== kAXErrorSuccess) { if (AXUIElementGetPid(pub_mData.AxID, &pid)== kAXErrorSuccess) {
return pid; return pid;
} }
return 0; return 0;
@ -600,7 +600,7 @@ int32_t get_PID(void) {
XDismissErrors(); XDismissErrors();
// Get the window PID // Get the window PID
long* result = (long*)GetWindowProperty(mData, WM_PID,NULL); long* result = (long*)GetWindowProperty(pub_mData, WM_PID,NULL);
// Check result and convert it // Check result and convert it
if (result == NULL) { return 0; } if (result == NULL) { return 0; }
@ -609,7 +609,7 @@ int32_t get_PID(void) {
return pid; return pid;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
DWORD id = 0; DWORD id = 0;
GetWindowThreadProcessId(mData.HWnd, &id); GetWindowThreadProcessId(pub_mData.HWnd, &id);
return id; return id;
#endif #endif
} }