update window.h code style

This commit is contained in:
vcaesar 2018-07-09 19:55:37 +08:00
parent 482d465472
commit e7f841be68

View File

@ -33,7 +33,7 @@ bool setHandle(uintptr handle);
bool IsValid(); bool IsValid();
bool IsAxEnabled(bool options); bool IsAxEnabled(bool options);
MData GetActive(void); MData GetActive(void);
void aWindow(); void initWindow();
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
@ -89,7 +89,7 @@ void aWindow();
// Reference to resulting value // Reference to resulting value
AXUIElementRef result = NULL; AXUIElementRef result = NULL;
if (windows != NULL){ if (windows != NULL) {
int count = CFArrayGetCount(windows); int count = CFArrayGetCount(windows);
// Loop all windows in the process // Loop all windows in the process
for (CFIndex i = 0; i < count; ++i){ for (CFIndex i = 0; i < count; ++i){
@ -102,10 +102,10 @@ void aWindow();
_AXUIElementGetWindow(element, &temp); _AXUIElementGetWindow(element, &temp);
// Check results // Check results
if (temp == win){ if (temp == win) {
// Retain element // Retain element
CFRetain(element); CFRetain(element);
result = element; result = element;
break; break;
} }
} }
@ -126,7 +126,7 @@ void aWindow();
XErrorHandler mOld; XErrorHandler mOld;
void XDismissErrors (void){ void XDismissErrors (void) {
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Save old handler and dismiss errors // Save old handler and dismiss errors
mOld = XSetErrorHandler(XHandleError); mOld = XSetErrorHandler(XHandleError);
@ -196,14 +196,14 @@ void aWindow();
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Check the atom // Check the atom
if (atom != None){ if (atom != None) {
// Retrieve and validate the specified property // Retrieve and validate the specified property
if (!XGetWindowProperty(rDisplay, win.XWin, atom, 0, if (!XGetWindowProperty(rDisplay, win.XWin, atom, 0,
BUFSIZ, False, AnyPropertyType, &type, &format, BUFSIZ, False, AnyPropertyType, &type, &format,
&nItems, &bAfter, &result) && result && nItems){ &nItems, &bAfter, &result) && result && nItems) {
// Copy items result // Copy items result
if (items != NULL){ if (items != NULL) {
*items = (uint32) nItems; *items = (uint32) nItems;
} }
@ -233,12 +233,12 @@ void aWindow();
static void SetDesktopForWindow(MData win){ static void SetDesktopForWindow(MData win){
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Validate every atom that we want to use // Validate every atom that we want to use
if (WM_DESKTOP != None && WM_CURDESK != None){ if (WM_DESKTOP != None && WM_CURDESK != None) {
// Get desktop property // Get desktop property
long* desktop = (long*)GetWindowProperty(win, WM_DESKTOP,NULL); long* desktop = (long*)GetWindowProperty(win, WM_DESKTOP,NULL);
// Check result value // Check result value
if (desktop != NULL){ if (desktop != NULL) {
// Retrieve the screen number // Retrieve the screen number
XWindowAttributes attr = { 0 }; XWindowAttributes attr = { 0 };
XGetWindowAttributes(rDisplay, win.XWin, &attr); XGetWindowAttributes(rDisplay, win.XWin, &attr);
@ -271,9 +271,9 @@ void aWindow();
//int findwindow() //int findwindow()
uintptr ahandle = 0; uintptr initHandle = 0;
void aWindow(uintptr handle){ void initWindow(uintptr handle){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
mData.CgID = 0; mData.CgID = 0;
@ -282,7 +282,7 @@ void aWindow(uintptr handle){
#elif defined(USE_X11) #elif defined(USE_X11)
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// If atoms loaded // If atoms loaded
if (WM_PID == None){ if (WM_PID == None) {
// Load all necessary atom properties // Load all necessary atom properties
if (rDisplay != NULL) {LoadAtoms();} if (rDisplay != NULL) {LoadAtoms();}
} }
@ -290,18 +290,17 @@ void aWindow(uintptr handle){
mData.XWin = 0; mData.XWin = 0;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
mData.HWnd = 0; mData.HWnd = 0;
#endif #endif
setHandle(handle); setHandle(handle);
} }
bool IsValid(){ bool IsValid(){
aWindow(ahandle); initWindow(initHandle);
if(!IsAxEnabled(true)){ if(!IsAxEnabled(true)){
printf("%s\n", "Window:Accessibility API is disabled!\nFailed to enable access for assistive devices."); printf("%s\n", "Window:Accessibility API is disabled!\n
Failed to enable access for assistive devices.");
} }
MData actdata = GetActive(); MData actdata = GetActive();
@ -345,7 +344,7 @@ bool IsValid(){
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
mData.HWnd = actdata.HWnd; mData.HWnd = actdata.HWnd;
if (mData.HWnd == 0){ if (mData.HWnd == 0) {
return false; return false;
} }
@ -365,7 +364,7 @@ bool IsAxEnabled(bool options){
"Services.framework/ApplicationServices", RTLD_LAZY); "Services.framework/ApplicationServices", RTLD_LAZY);
// Validate the handle // Validate the handle
if (handle != NULL){ if (handle != NULL) {
*(void**) (&gAXIsProcessTrustedWithOptions) = *(void**) (&gAXIsProcessTrustedWithOptions) =
dlsym (handle, "AXIsProcessTrustedWithOptions"); dlsym (handle, "AXIsProcessTrustedWithOptions");
@ -375,7 +374,7 @@ bool IsAxEnabled(bool options){
}); });
// Check for new OSX 10.9 function // Check for new OSX 10.9 function
if (gAXIsProcessTrustedWithOptions){ if (gAXIsProcessTrustedWithOptions) {
// Check whether to show prompt // Check whether to show prompt
CFBooleanRef displayPrompt = options ? CFBooleanRef displayPrompt = options ?
kCFBooleanTrue : kCFBooleanFalse; kCFBooleanTrue : kCFBooleanFalse;
@ -391,7 +390,7 @@ bool IsAxEnabled(bool options){
// Free memory // Free memory
CFRelease(o); CFRelease(o);
return result; return result;
}else{ } else {
// Ignore deprecated warnings // Ignore deprecated warnings
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
@ -402,13 +401,9 @@ bool IsAxEnabled(bool options){
} }
#elif defined(USE_X11) #elif defined(USE_X11)
return true; return true;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return true; return true;
#endif #endif
} }
@ -461,11 +456,11 @@ bool setHandle(uintptr handle){
mData.HWnd = (HWND)handle; mData.HWnd = (HWND)handle;
if (handle == 0){ if (handle == 0) {
return true; return true;
} }
if (IsValid()){ if (IsValid()) {
return true; return true;
} }
@ -476,14 +471,14 @@ bool setHandle(uintptr handle){
} }
// uint32 uintptr // uint32 uintptr
uintptr getHandle(){ uintptr getHandle() {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
return (uintptr)mData.CgID; return (uintptr)mData.CgID;
#elif defined(USE_X11) #elif defined(USE_X11)
return (uintptr)mData.XWin; return (uintptr)mData.XWin;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return (uintptr)mData.HWnd; return (uintptr)mData.HWnd;
#endif #endif
} }
bool IsTopMost(void){ bool IsTopMost(void){
@ -517,7 +512,7 @@ bool IsMinimized(void){
// Determine whether the window is minimized // Determine whether the window is minimized
if (AXUIElementCopyAttributeValue(mData.AxID, if (AXUIElementCopyAttributeValue(mData.AxID,
kAXMinimizedAttribute, (CFTypeRef*) &data) kAXMinimizedAttribute, (CFTypeRef*) &data)
== kAXErrorSuccess && data != NULL){ == kAXErrorSuccess && data != NULL) {
// Convert resulting data into a bool // Convert resulting data into a bool
bool result = CFBooleanGetValue(data); bool result = CFBooleanGetValue(data);
CFRelease(data); CFRelease(data);
@ -570,7 +565,7 @@ void SetActive(const MData win){
// Attempt to raise the specified window object // Attempt to raise the specified window object
if (AXUIElementPerformAction(win.AxID, kAXRaiseAction) if (AXUIElementPerformAction(win.AxID, kAXRaiseAction)
!= kAXErrorSuccess){ != kAXErrorSuccess) {
pid_t pid = 0; pid_t pid = 0;
// Attempt to retrieve the PID of the window // Attempt to retrieve the PID of the window
if (AXUIElementGetPid(win.AxID, &pid) if (AXUIElementGetPid(win.AxID, &pid)
@ -586,7 +581,7 @@ void SetActive(const MData win){
ProcessSerialNumber psn; ProcessSerialNumber psn;
// Attempt to retrieve the process psn // Attempt to retrieve the process psn
if (GetProcessForPID(pid, &psn) == 0){ if (GetProcessForPID(pid, &psn) == 0) {
// Gracefully activate process // Gracefully activate process
SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);
} }
@ -603,7 +598,7 @@ void SetActive(const MData win){
SetDesktopForWindow(win); SetDesktopForWindow(win);
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Check the atom value // Check the atom value
if (WM_ACTIVE != None){ if (WM_ACTIVE != None) {
// Retrieve the screen number // Retrieve the screen number
XWindowAttributes attr = { 0 }; XWindowAttributes attr = { 0 };
XGetWindowAttributes(rDisplay, win.XWin, &attr); XGetWindowAttributes(rDisplay, win.XWin, &attr);
@ -623,7 +618,7 @@ void SetActive(const MData win){
XSendEvent(rDisplay, XRootWindow(rDisplay, s), False, XSendEvent(rDisplay, XRootWindow(rDisplay, s), False,
SubstructureNotifyMask | SubstructureRedirectMask, SubstructureNotifyMask | SubstructureRedirectMask,
(XEvent*) &e); (XEvent*) &e);
}else{ } else {
// Attempt to raise the specified window // Attempt to raise the specified window
XRaiseWindow(rDisplay, win.XWin); XRaiseWindow(rDisplay, win.XWin);
@ -634,7 +629,7 @@ void SetActive(const MData win){
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
if (IsMinimized()){ if (IsMinimized()) {
ShowWindow(win.HWnd, SW_RESTORE); ShowWindow(win.HWnd, SW_RESTORE);
} }
@ -654,7 +649,7 @@ MData GetActive(void){
ProcessSerialNumber psn; pid_t pid; ProcessSerialNumber psn; pid_t pid;
// Attempt to retrieve the front process // Attempt to retrieve the front process
if (GetFrontProcess(&psn) != 0 || if (GetFrontProcess(&psn) != 0 ||
GetProcessPID(&psn, &pid) != 0){ GetProcessPID(&psn, &pid) != 0) {
return result; return result;
} }
@ -668,12 +663,12 @@ MData GetActive(void){
// Retrieve the currently focused window // Retrieve the currently focused window
if (AXUIElementCopyAttributeValue(focused, if (AXUIElementCopyAttributeValue(focused,
kAXFocusedWindowAttribute, (CFTypeRef*) kAXFocusedWindowAttribute, (CFTypeRef*)
&element) == kAXErrorSuccess && element){ &element) == kAXErrorSuccess && element) {
CGWindowID win = 0; CGWindowID win = 0;
// Use undocumented API to get WID // Use undocumented API to get WID
if (_AXUIElementGetWindow(element, &win) if (_AXUIElementGetWindow(element, &win)
== kAXErrorSuccess && win){ == kAXErrorSuccess && win) {
// Manually set internals // Manually set internals
result.CgID = win; result.CgID = win;
result.AxID = element; result.AxID = element;
@ -693,7 +688,7 @@ MData GetActive(void){
MData result; MData result;
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Check X-Window display // Check X-Window display
if (WM_ACTIVE == None || rDisplay == NULL){ if (WM_ACTIVE == None || rDisplay == NULL) {
return result; return result;
} }
@ -705,12 +700,12 @@ MData GetActive(void){
void* active = GetWindowProperty(result,WM_ACTIVE,NULL); void* active = GetWindowProperty(result,WM_ACTIVE,NULL);
// Check result value // Check result value
if (active != NULL){ if (active != NULL) {
// Extract window from the result // Extract window from the result
long window = *((long*)active); long window = *((long*)active);
XFree(active); XFree(active);
if (window != 0){ if (window != 0) {
// Set and return the foreground window // Set and return the foreground window
result.XWin = (Window)window; result.XWin = (Window)window;
return result; return result;
@ -732,10 +727,10 @@ MData GetActive(void){
MData result; MData result;
uint8 times = 0; uint8 times = 0;
while (++times < 20){ while (++times < 20) {
HWND handle; HWND handle;
handle = GetForegroundWindow(); handle = GetForegroundWindow();
if (handle != NULL){ if (handle != NULL) {
// mData.HWnd = (uintptr) handle; // mData.HWnd = (uintptr) handle;
result.HWnd = (HWND)handle; result.HWnd = (HWND)handle;
return result; return result;
@ -781,7 +776,7 @@ void CloseWin(void){
// Retrieve the close button of this window // Retrieve the close button of this window
if (AXUIElementCopyAttributeValue(mData.AxID, if (AXUIElementCopyAttributeValue(mData.AxID,
kAXCloseButtonAttribute, (CFTypeRef*) &b) kAXCloseButtonAttribute, (CFTypeRef*) &b)
== kAXErrorSuccess && b != NULL){ == kAXErrorSuccess && b != NULL) {
// Simulate button press on the close button // Simulate button press on the close button
AXUIElementPerformAction(b, kAXPressAction); AXUIElementPerformAction(b, kAXPressAction);
CFRelease(b); CFRelease(b);
@ -815,16 +810,14 @@ char *GetTitle(){
// Determine the current title of the window // Determine the current title of the window
if (AXUIElementCopyAttributeValue(mData.AxID, if (AXUIElementCopyAttributeValue(mData.AxID,
kAXTitleAttribute, (CFTypeRef*) &data) kAXTitleAttribute, (CFTypeRef*) &data)
== kAXErrorSuccess && data != NULL){ == kAXErrorSuccess && data != NULL) {
char conv[512]; char conv[512];
// Convert result to a C-String // Convert result to a C-String
CFStringGetCString(data, conv, CFStringGetCString(data, conv,
512, kCFStringEncodingUTF8); 512, kCFStringEncodingUTF8);
CFRelease(data); CFRelease(data);
char* s = (char*)calloc(100, sizeof(char*)); char* s = (char*)calloc(100, sizeof(char*));
if (s){ if (s) { strcpy(s,conv); }
strcpy(s,conv);
}
// return (char *)&conv; // return (char *)&conv;
return s; return s;
} }
@ -841,7 +834,7 @@ char *GetTitle(){
result = GetWindowProperty(mData, WM_NAME,NULL); result = GetWindowProperty(mData, WM_NAME,NULL);
// Check result value // Check result value
if (result != NULL){ if (result != NULL) {
// Convert result to a string // Convert result to a string
char *name = (char*)result; char *name = (char*)result;
XFree(result); XFree(result);
@ -853,7 +846,7 @@ char *GetTitle(){
result = GetWindowProperty(mData, XA_WM_NAME,NULL); result = GetWindowProperty(mData, XA_WM_NAME,NULL);
// Check result value // Check result value
if (result != NULL){ if (result != NULL) {
// Convert result to a string // Convert result to a string
char *name = (char*)result; char *name = (char*)result;
XFree(result); XFree(result);