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 (
version string = "v0.47.0.487, Mount Cook!"
version string = "v0.47.0.490, Mount Cook!"
)
type (

View File

@ -65,20 +65,21 @@ void aWindow();
CFNumberRef data =(CFNumberRef)
CFDictionaryGetValue(desc, kCGWindowOwnerPID);
if (data != NULL)
if (data != NULL){
CFNumberGetValue(data, kCFNumberIntType, &pid);
}
// Return result
CFRelease(info);
}
// Check if PID was retrieved
if (pid <= 0) return NULL;
if (pid <= 0) {return NULL;}
// Create an accessibility object using retrieved PID
AXUIElementRef application = AXUIElementCreateApplication(pid);
if (application == 0) return NULL;
if (application == 0) {return NULL;}
CFArrayRef windows = NULL;
// Get all windows associated with the app
@ -138,8 +139,7 @@ void aWindow();
// Definitions
struct Hints
{
struct Hints{
unsigned long Flags;
unsigned long Funcs;
unsigned long Decorations;
@ -165,8 +165,7 @@ void aWindow();
////////////////////////////////////////////////////////////////////////////////
static void LoadAtoms (void)
{
static void LoadAtoms (void){
Display *rDisplay = XOpenDisplay(NULL);
WM_STATE = XInternAtom(rDisplay, "_NET_WM_STATE", True);
WM_ABOVE = XInternAtom(rDisplay, "_NET_WM_STATE_ABOVE", True);
@ -188,8 +187,7 @@ void aWindow();
// Functions
static void* GetWindowProperty (MData win, Atom atom, uint32* items)
{
static void* GetWindowProperty (MData win, Atom atom, uint32* items){
// Property variables
Atom type; int format;
unsigned long nItems;
@ -198,26 +196,29 @@ void aWindow();
Display *rDisplay = XOpenDisplay(NULL);
// Check the atom
if (atom != None)
{
if (atom != None){
// Retrieve and validate the specified property
if (!XGetWindowProperty (rDisplay, win.XWin, atom, 0,
BUFSIZ, False, AnyPropertyType, &type, &format,
&nItems, &bAfter, &result) && result && nItems)
{
&nItems, &bAfter, &result) && result && nItems){
// Copy items result
if (items != NULL)
if (items != NULL){
*items = (uint32) nItems;
}
return result;
}
}
// Reset the items result if valid
if (items != NULL) *items = 0;
if (items != NULL) {*items = 0;}
// Free the result if it got allocated
if (result != NULL) XFree (result);
if (result != NULL) {
XFree (result);
}
return NULL;
}
@ -234,8 +235,7 @@ void aWindow();
// Validate every atom that we want to use
if (WM_DESKTOP != None && WM_CURDESK != None){
// Get desktop property
long* desktop = (long*)
GetWindowProperty (win, WM_DESKTOP,NULL);
long* desktop = (long*)GetWindowProperty (win, WM_DESKTOP,NULL);
// Check result value
if (desktop != NULL){
@ -284,7 +284,7 @@ void aWindow(uintptr handle){
// If atoms loaded
if (WM_PID == None){
// Load all necessary atom properties
if (rDisplay != NULL) LoadAtoms();
if (rDisplay != NULL) {LoadAtoms();}
}
mData.XWin = 0;
@ -300,7 +300,9 @@ void aWindow(uintptr handle){
bool IsValid (){
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();
#if defined(IS_MACOSX)
@ -323,27 +325,29 @@ bool IsValid (){
#elif defined(USE_X11)
mData.XWin = actdata.XWin;
if (mData.XWin == 0) return false;
if (mData.XWin == 0) {return false;}
Display *rDisplay = XOpenDisplay(NULL);
// Check for a valid X-Window display
if (rDisplay == NULL) return false;
if (rDisplay == NULL) {return false;}
// Ignore X errors
XDismissErrors();
// Get the window PID property
void* result = GetWindowProperty(mData, WM_PID,NULL);
if (result == NULL) return false;
if (result == NULL) {return false;}
// Free result and return true
XFree (result); return true;
XFree (result);
return true;
#elif defined(IS_WINDOWS)
mData.HWnd = actdata.HWnd;
if (mData.HWnd == 0)
if (mData.HWnd == 0){
return false;
}
return IsWindow(mData.HWnd) != 0;
@ -357,8 +361,7 @@ bool IsAxEnabled (bool options){
static dispatch_once_t once; dispatch_once (&once,
^{
// Open the framework
void* handle = dlopen
("/System/Library/Frameworks/Application"
void* handle = dlopen("/System/Library/Frameworks/Application"
"Services.framework/ApplicationServices", RTLD_LAZY);
// Validate the handle
@ -414,7 +417,9 @@ bool IsAxEnabled (bool options){
bool setHandle (uintptr handle){
#if defined(IS_MACOSX)
// Release the AX element
if (mData.AxID != NULL)CFRelease (mData.AxID);
if (mData.AxID != NULL){
CFRelease(mData.AxID);
}
// Reset both values
mData.CgID = 0;
@ -441,11 +446,13 @@ bool setHandle (uintptr handle){
mData.XWin = (Window)handle;
if (handle == 0)
if (handle == 0){
return true;
}
if (IsValid())
if (IsValid()){
return true;
}
mData.XWin = 0;
return false;
@ -453,11 +460,13 @@ bool setHandle (uintptr handle){
mData.HWnd = (HWND)handle;
if (handle == 0)
if (handle == 0){
return true;
}
if (IsValid())
if (IsValid()){
return true;
}
mData.HWnd = 0;
return false;
@ -481,7 +490,7 @@ uintptr getHandle(){
bool IsTopMost (void){
// Check the window validity
if (!IsValid()) return false;
if (!IsValid()) {return false;}
#if defined(IS_MACOSX)
return false; // WARNING: Unavailable
@ -502,7 +511,7 @@ bool IsTopMost (void){
bool IsMinimized(void){
// Check the window validity
if (!IsValid()) return false;
if (!IsValid()) {return false;}
#if defined(IS_MACOSX)
CFBooleanRef data = NULL;
@ -510,8 +519,7 @@ bool IsMinimized(void){
// Determine whether the window is minimized
if (AXUIElementCopyAttributeValue (mData.AxID,
kAXMinimizedAttribute, (CFTypeRef*) &data)
== kAXErrorSuccess && data != NULL)
{
== kAXErrorSuccess && data != NULL){
// Convert resulting data into a bool
bool result = CFBooleanGetValue(data);
CFRelease (data); return result;
@ -537,7 +545,7 @@ bool IsMinimized(void){
bool IsMaximized(void){
// Check the window validity
if (!IsValid()) return false;
if (!IsValid()) {return false;}
#if defined(IS_MACOSX)
return false; // WARNING: Unavailable
@ -558,17 +566,16 @@ bool IsMaximized(void){
void SetActive(const MData win){
// Check if the window is valid
if (!IsValid()) return;
if (!IsValid()) {return;}
#if defined(IS_MACOSX)
// Attempt to raise the specified window object
if (AXUIElementPerformAction (win.AxID,
kAXRaiseAction) != kAXErrorSuccess)
{
if (AXUIElementPerformAction(win.AxID, kAXRaiseAction)
!= kAXErrorSuccess){
pid_t pid = 0;
// Attempt to retrieve the PID of the window
if (AXUIElementGetPid (win.AxID, &pid)
!= kAXErrorSuccess || !pid) return;
!= kAXErrorSuccess || !pid) {return;}
// Ignore deprecated warnings
#pragma clang diagnostic push
@ -582,8 +589,7 @@ void SetActive(const MData win){
// Attempt to retrieve the process psn
if (GetProcessForPID (pid, &psn) == 0){
// Gracefully activate process
SetFrontProcessWithOptions (&psn,
kSetFrontProcessFrontWindowOnly);
SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);
}
#pragma clang diagnostic pop
@ -630,8 +636,9 @@ void SetActive(const MData win){
#elif defined(IS_WINDOWS)
if (IsMinimized())
if (IsMinimized()){
ShowWindow(win.HWnd, SW_RESTORE);
}
SetForegroundWindow(win.HWnd);
@ -649,33 +656,34 @@ MData GetActive (void){
ProcessSerialNumber psn; pid_t pid;
// Attempt to retrieve the front process
if (GetFrontProcess (&psn ) != 0 ||
GetProcessPID (&psn, &pid) != 0)
GetProcessPID (&psn, &pid) != 0){
return result;
}
#pragma clang diagnostic pop
// Create accessibility object using focused PID
AXUIElementRef focused = AXUIElementCreateApplication(pid);
if (focused == NULL) return result; // Verify
if (focused == NULL) {return result; }// Verify
AXUIElementRef element;
// Retrieve the currently focused window
if (AXUIElementCopyAttributeValue (focused,
kAXFocusedWindowAttribute, (CFTypeRef*)
&element) == kAXErrorSuccess && element)
{
&element) == kAXErrorSuccess && element){
CGWindowID win = 0;
// Use undocumented API to get WID
if (_AXUIElementGetWindow (element,
&win) == kAXErrorSuccess && win)
{
&win) == kAXErrorSuccess && win){
// Manually set internals
result.CgID = win;
result.AxID = element;
}
// Something went wrong
else CFRelease (element);
else {
CFRelease(element);
}
}
CFRelease(focused);
@ -688,8 +696,9 @@ MData GetActive (void){
Display *rDisplay = XOpenDisplay(NULL);
// Check X-Window display
if (WM_ACTIVE == None ||
rDisplay == NULL)
rDisplay == NULL){
return result;
}
// Ignore X errors
XDismissErrors();
@ -699,12 +708,12 @@ MData GetActive (void){
void* active = GetWindowProperty(result,WM_ACTIVE,NULL);
// Check result value
if (active != NULL)
{
if (active != NULL){
// Extract window from the result
long window = *((long*) active);
XFree (active); if (window != 0)
{
XFree (active);
if (window != 0){
// Set and return the foreground window
result.XWin = (Window) window;
return result;
@ -727,8 +736,7 @@ MData GetActive (void){
MData result;
uint8 times = 0;
while (++times < 20)
{
while (++times < 20){
HWND handle;
handle = GetForegroundWindow();
if (handle != NULL){
@ -747,7 +755,7 @@ MData GetActive (void){
void SetTopMost (bool state){
// Check window validity
if (!IsValid()) return;
if (!IsValid()) {return;}
#if defined(IS_MACOSX)
// WARNING: Unavailable
@ -770,15 +778,14 @@ void SetTopMost (bool state){
// CloseWindow
void CloseWin(void){
// Check window validity
if (!IsValid()) return;
if (!IsValid()) {return;}
#if defined(IS_MACOSX)
AXUIElementRef b = NULL;
// Retrieve the close button of this window
if (AXUIElementCopyAttributeValue(mData.AxID,
kAXCloseButtonAttribute, (CFTypeRef*) &b)
== kAXErrorSuccess && b != NULL)
{
== kAXErrorSuccess && b != NULL){
// Simulate button press on the close button
AXUIElementPerformAction(b, kAXPressAction);
CFRelease(b);
@ -803,7 +810,7 @@ void CloseWin(void){
char *GetTitle(){
// Check if the window is valid
if (!IsValid()) return "IsValid failed.";
if (!IsValid()) {return "IsValid failed.";}
#if defined(IS_MACOSX)
@ -812,15 +819,16 @@ char *GetTitle(){
// Determine the current title of the window
if (AXUIElementCopyAttributeValue (mData.AxID,
kAXTitleAttribute, (CFTypeRef*) &data)
== kAXErrorSuccess && data != NULL)
{
== kAXErrorSuccess && data != NULL){
char conv[512];
// Convert result to a C-String
CFStringGetCString(data, conv,
512, kCFStringEncodingUTF8);
CFRelease(data);
char* s = (char*)calloc(100, sizeof(char*));
if(s)strcpy(s,conv);
if (s){
strcpy(s,conv);
}
// return (char *)&conv;
return s;
}
@ -842,7 +850,7 @@ char *GetTitle(){
char *name = (char*) result;
XFree(result);
if (name != NULL) return name;
if (name != NULL) {return name;}
}
// Get window title (ASCII)
@ -852,7 +860,8 @@ char *GetTitle(){
if (result != NULL){
// Convert result to a string
char *name = (char*) result;
XFree (result); return name;
XFree(result);
return name;
}
return "";
@ -870,14 +879,16 @@ char *GetTitle(){
int32 WGetPID(void){
// Check window validity
if (!IsValid()) return 0;
if (!IsValid()) {return 0;}
#if defined(IS_MACOSX)
pid_t pid = 0;
// Attempt to retrieve the window pid
if (AXUIElementGetPid(mData.AxID, &pid)
== kAXErrorSuccess) return pid;
== kAXErrorSuccess) {
return pid;
}
return 0;
@ -887,13 +898,13 @@ int32 WGetPID(void){
XDismissErrors();
// Get the window PID
long* result = (long*)
GetWindowProperty(mData, WM_PID,NULL);
long* result = (long*)GetWindowProperty(mData, WM_PID,NULL);
// Check result and convert it
if (result == NULL) return 0;
if (result == NULL) {return 0;}
int32 pid = (int32) *result;
XFree (result); return pid;
XFree (result);
return pid;
#elif defined(IS_WINDOWS)