change some code style

This commit is contained in:
vcaesar 2018-01-21 21:56:33 +08:00
parent 0425190a57
commit 065d1acc2b
2 changed files with 144 additions and 133 deletions

View File

@ -63,7 +63,7 @@ import (
) )
const ( const (
version string = "v0.47.0.487, Mount Cook!" version string = "v0.47.0.490, Mount Cook!"
) )
type ( type (

View File

@ -65,20 +65,21 @@ void aWindow();
CFNumberRef data =(CFNumberRef) CFNumberRef data =(CFNumberRef)
CFDictionaryGetValue(desc, kCGWindowOwnerPID); CFDictionaryGetValue(desc, kCGWindowOwnerPID);
if (data != NULL) if (data != NULL){
CFNumberGetValue(data, kCFNumberIntType, &pid); CFNumberGetValue(data, kCFNumberIntType, &pid);
}
// Return result // Return result
CFRelease(info); CFRelease(info);
} }
// Check if PID was retrieved // Check if PID was retrieved
if (pid <= 0) return NULL; if (pid <= 0) {return NULL;}
// Create an accessibility object using retrieved PID // Create an accessibility object using retrieved PID
AXUIElementRef application = AXUIElementCreateApplication(pid); AXUIElementRef application = AXUIElementCreateApplication(pid);
if (application == 0) return NULL; if (application == 0) {return NULL;}
CFArrayRef windows = NULL; CFArrayRef windows = NULL;
// Get all windows associated with the app // Get all windows associated with the app
@ -138,8 +139,7 @@ void aWindow();
// Definitions // Definitions
struct Hints struct Hints{
{
unsigned long Flags; unsigned long Flags;
unsigned long Funcs; unsigned long Funcs;
unsigned long Decorations; unsigned long Decorations;
@ -165,8 +165,7 @@ void aWindow();
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void LoadAtoms (void) static void LoadAtoms (void){
{
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
WM_STATE = XInternAtom(rDisplay, "_NET_WM_STATE", True); WM_STATE = XInternAtom(rDisplay, "_NET_WM_STATE", True);
WM_ABOVE = XInternAtom(rDisplay, "_NET_WM_STATE_ABOVE", True); WM_ABOVE = XInternAtom(rDisplay, "_NET_WM_STATE_ABOVE", True);
@ -188,8 +187,7 @@ void aWindow();
// Functions // Functions
static void* GetWindowProperty (MData win, Atom atom, uint32* items) static void* GetWindowProperty (MData win, Atom atom, uint32* items){
{
// Property variables // Property variables
Atom type; int format; Atom type; int format;
unsigned long nItems; unsigned long nItems;
@ -198,26 +196,29 @@ 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;
}
return result; return result;
} }
} }
// Reset the items result if valid // Reset the items result if valid
if (items != NULL) *items = 0; if (items != NULL) {*items = 0;}
// Free the result if it got allocated // Free the result if it got allocated
if (result != NULL) XFree (result); if (result != NULL) {
XFree (result);
}
return NULL; return NULL;
} }
@ -234,8 +235,7 @@ void aWindow();
// 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*) long* desktop = (long*)GetWindowProperty (win, WM_DESKTOP,NULL);
GetWindowProperty (win, WM_DESKTOP,NULL);
// Check result value // Check result value
if (desktop != NULL){ if (desktop != NULL){
@ -284,7 +284,7 @@ void aWindow(uintptr handle){
// 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();}
} }
mData.XWin = 0; mData.XWin = 0;
@ -300,7 +300,9 @@ void aWindow(uintptr handle){
bool IsValid (){ bool IsValid (){
aWindow(ahandle); aWindow(ahandle);
if(!IsAxEnabled(true))printf("%s\n", "Window:Accessibility API is disabled!\nFailed to enable access for assistive devices."); if(!IsAxEnabled(true)){
printf("%s\n", "Window:Accessibility API is disabled!\nFailed to enable access for assistive devices.");
}
MData actdata = GetActive(); MData actdata = GetActive();
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
@ -323,27 +325,29 @@ bool IsValid (){
#elif defined(USE_X11) #elif defined(USE_X11)
mData.XWin = actdata.XWin; mData.XWin = actdata.XWin;
if (mData.XWin == 0) return false; if (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
if (rDisplay == NULL) return false; if (rDisplay == NULL) {return false;}
// Ignore X errors // Ignore X errors
XDismissErrors(); XDismissErrors();
// Get the window PID property // Get the window PID property
void* result = GetWindowProperty(mData, WM_PID,NULL); void* result = GetWindowProperty(mData, WM_PID,NULL);
if (result == NULL) return false; if (result == NULL) {return false;}
// Free result and return true // Free result and return true
XFree (result); return true; XFree (result);
return true;
#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;
}
return IsWindow(mData.HWnd) != 0; return IsWindow(mData.HWnd) != 0;
@ -357,8 +361,7 @@ bool IsAxEnabled (bool options){
static dispatch_once_t once; dispatch_once (&once, static dispatch_once_t once; dispatch_once (&once,
^{ ^{
// Open the framework // Open the framework
void* handle = dlopen void* handle = dlopen("/System/Library/Frameworks/Application"
("/System/Library/Frameworks/Application"
"Services.framework/ApplicationServices", RTLD_LAZY); "Services.framework/ApplicationServices", RTLD_LAZY);
// Validate the handle // Validate the handle
@ -414,7 +417,9 @@ 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)CFRelease (mData.AxID); if (mData.AxID != NULL){
CFRelease(mData.AxID);
}
// Reset both values // Reset both values
mData.CgID = 0; mData.CgID = 0;
@ -441,11 +446,13 @@ bool setHandle (uintptr handle){
mData.XWin = (Window)handle; mData.XWin = (Window)handle;
if (handle == 0) if (handle == 0){
return true; return true;
}
if (IsValid()) if (IsValid()){
return true; return true;
}
mData.XWin = 0; mData.XWin = 0;
return false; return false;
@ -453,11 +460,13 @@ 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;
}
mData.HWnd = 0; mData.HWnd = 0;
return false; return false;
@ -481,7 +490,7 @@ uintptr getHandle(){
bool IsTopMost (void){ bool IsTopMost (void){
// Check the window validity // Check the window validity
if (!IsValid()) return false; if (!IsValid()) {return false;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
return false; // WARNING: Unavailable return false; // WARNING: Unavailable
@ -502,7 +511,7 @@ bool IsTopMost (void){
bool IsMinimized(void){ bool IsMinimized(void){
// Check the window validity // Check the window validity
if (!IsValid()) return false; if (!IsValid()) {return false;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CFBooleanRef data = NULL; CFBooleanRef data = NULL;
@ -510,8 +519,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); return result; CFRelease (data); return result;
@ -537,7 +545,7 @@ bool IsMinimized(void){
bool IsMaximized(void){ bool IsMaximized(void){
// Check the window validity // Check the window validity
if (!IsValid()) return false; if (!IsValid()) {return false;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
return false; // WARNING: Unavailable return false; // WARNING: Unavailable
@ -558,17 +566,16 @@ bool IsMaximized(void){
void SetActive(const MData win){ void SetActive(const MData win){
// Check if the window is valid // Check if the window is valid
if (!IsValid()) return; if (!IsValid()) {return;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
// Attempt to raise the specified window object // Attempt to raise the specified window object
if (AXUIElementPerformAction (win.AxID, if (AXUIElementPerformAction(win.AxID, kAXRaiseAction)
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)
!= kAXErrorSuccess || !pid) return; != kAXErrorSuccess || !pid) {return;}
// Ignore deprecated warnings // Ignore deprecated warnings
#pragma clang diagnostic push #pragma clang diagnostic push
@ -582,8 +589,7 @@ void SetActive(const MData win){
// 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, SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);
kSetFrontProcessFrontWindowOnly);
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop
@ -630,8 +636,9 @@ 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);
}
SetForegroundWindow(win.HWnd); SetForegroundWindow(win.HWnd);
@ -649,33 +656,34 @@ 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;
}
#pragma clang diagnostic pop #pragma clang diagnostic pop
// Create accessibility object using focused PID // Create accessibility object using focused PID
AXUIElementRef focused = AXUIElementCreateApplication(pid); AXUIElementRef focused = AXUIElementCreateApplication(pid);
if (focused == NULL) return result; // Verify if (focused == NULL) {return result; }// Verify
AXUIElementRef element; AXUIElementRef element;
// 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, if (_AXUIElementGetWindow (element,
&win) == kAXErrorSuccess && win) &win) == kAXErrorSuccess && win){
{
// Manually set internals // Manually set internals
result.CgID = win; result.CgID = win;
result.AxID = element; result.AxID = element;
} }
// Something went wrong // Something went wrong
else CFRelease (element); else {
CFRelease(element);
}
} }
CFRelease(focused); CFRelease(focused);
@ -688,8 +696,9 @@ MData GetActive (void){
Display *rDisplay = XOpenDisplay(NULL); Display *rDisplay = XOpenDisplay(NULL);
// Check X-Window display // Check X-Window display
if (WM_ACTIVE == None || if (WM_ACTIVE == None ||
rDisplay == NULL) rDisplay == NULL){
return result; return result;
}
// Ignore X errors // Ignore X errors
XDismissErrors(); XDismissErrors();
@ -699,12 +708,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); if (window != 0) XFree (active);
{
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;
@ -727,8 +736,7 @@ 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){
@ -747,7 +755,7 @@ MData GetActive (void){
void SetTopMost (bool state){ void SetTopMost (bool state){
// Check window validity // Check window validity
if (!IsValid()) return; if (!IsValid()) {return;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
// WARNING: Unavailable // WARNING: Unavailable
@ -770,15 +778,14 @@ void SetTopMost (bool state){
// CloseWindow // CloseWindow
void CloseWin(void){ void CloseWin(void){
// Check window validity // Check window validity
if (!IsValid()) return; if (!IsValid()) {return;}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
AXUIElementRef b = NULL; AXUIElementRef b = NULL;
// 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);
@ -803,7 +810,7 @@ void CloseWin(void){
char *GetTitle(){ char *GetTitle(){
// Check if the window is valid // Check if the window is valid
if (!IsValid()) return "IsValid failed."; if (!IsValid()) {return "IsValid failed.";}
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
@ -812,15 +819,16 @@ 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)strcpy(s,conv); if (s){
strcpy(s,conv);
}
// return (char *)&conv; // return (char *)&conv;
return s; return s;
} }
@ -842,7 +850,7 @@ char *GetTitle(){
char *name = (char*) result; char *name = (char*) result;
XFree(result); XFree(result);
if (name != NULL) return name; if (name != NULL) {return name;}
} }
// Get window title (ASCII) // Get window title (ASCII)
@ -852,7 +860,8 @@ char *GetTitle(){
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); return name; XFree(result);
return name;
} }
return ""; return "";
@ -870,14 +879,16 @@ char *GetTitle(){
int32 WGetPID(void){ int32 WGetPID(void){
// Check window validity // Check window validity
if (!IsValid()) return 0; if (!IsValid()) {return 0;}
#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) if (AXUIElementGetPid(mData.AxID, &pid)
== kAXErrorSuccess) return pid; == kAXErrorSuccess) {
return pid;
}
return 0; return 0;
@ -887,13 +898,13 @@ int32 WGetPID(void){
XDismissErrors(); XDismissErrors();
// Get the window PID // Get the window PID
long* result = (long*) long* result = (long*)GetWindowProperty(mData, WM_PID,NULL);
GetWindowProperty(mData, WM_PID,NULL);
// Check result and convert it // Check result and convert it
if (result == NULL) return 0; if (result == NULL) {return 0;}
int32 pid = (int32) *result; int32 pid = (int32) *result;
XFree (result); return pid; XFree (result);
return pid;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)