From 8a91b0e5724323e95616e1acca5d12e921d718ae Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 12 Aug 2018 08:51:36 -0400 Subject: [PATCH] move some pub method and rename some c_func --- robotgo.go | 4 +- window/goWindow.h | 4 +- window/pub.h | 245 ++++++++++++++++++++++++++++++++++++++++++++ window/window.h | 254 ++-------------------------------------------- 4 files changed, 259 insertions(+), 248 deletions(-) create mode 100644 window/pub.h diff --git a/robotgo.go b/robotgo.go index f184f07..240d33d 100644 --- a/robotgo.go +++ b/robotgo.go @@ -194,12 +194,12 @@ func GetPixelColor(x, y int) string { // ScaleX get primary display horizontal DPI scale factor func ScaleX() int { - return int(C.scalex()) + return int(C.scaleX()) } // ScaleY get primary display vertical DPI scale factor func ScaleY() int { - return int(C.scaley()) + return int(C.scaleY()) } // GetScreenSize get the screen size diff --git a/window/goWindow.h b/window/goWindow.h index cd49821..5c0d135 100644 --- a/window/goWindow.h +++ b/window/goWindow.h @@ -19,7 +19,7 @@ int show_alert(const char *title, const char *msg, return alert; } -intptr scalex(){ +intptr scaleX(){ #if defined(IS_MACOSX) return 0; #elif defined(USE_X11) @@ -34,7 +34,7 @@ intptr scalex(){ #endif } -intptr scaley(){ +intptr scaleY(){ #if defined(IS_MACOSX) return 0; #elif defined(USE_X11) diff --git a/window/pub.h b/window/pub.h new file mode 100644 index 0000000..5b5aefb --- /dev/null +++ b/window/pub.h @@ -0,0 +1,245 @@ +// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// https://github.com/go-vgo/robotgo/blob/master/LICENSE +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// #include "../base/os.h" + +#if defined(IS_MACOSX) + + static Boolean(*gAXIsProcessTrustedWithOptions) (CFDictionaryRef); + + static CFStringRef* gkAXTrustedCheckOptionPrompt; + + AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out); + + static AXUIElementRef GetUIElement(CGWindowID win){ + intptr pid = 0; + // double_t pid = 0; + + // Create array storing window + CGWindowID window[1] = { win }; + CFArrayRef wlist = CFArrayCreate(NULL, + (const void**)window, 1, NULL); + + // Get window info + CFArrayRef info = CGWindowListCreateDescriptionFromArray(wlist); + CFRelease(wlist); + + // Check whether the resulting array is populated + if (info != NULL && CFArrayGetCount(info) > 0){ + // Retrieve description from info array + CFDictionaryRef desc = (CFDictionaryRef)CFArrayGetValueAtIndex(info, 0); + + // Get window PID + CFNumberRef data =(CFNumberRef) + CFDictionaryGetValue(desc, kCGWindowOwnerPID); + + if (data != NULL){ + CFNumberGetValue(data, kCFNumberIntType, &pid); + } + + // Return result + CFRelease(info); + } + + // Check if PID was retrieved + if (pid <= 0) {return NULL;} + + // Create an accessibility object using retrieved PID + AXUIElementRef application = AXUIElementCreateApplication(pid); + + if (application == 0) {return NULL;} + + CFArrayRef windows = NULL; + // Get all windows associated with the app + AXUIElementCopyAttributeValues(application, + kAXWindowsAttribute, 0, 1024, &windows); + + // Reference to resulting value + AXUIElementRef result = NULL; + + if (windows != NULL) { + int count = CFArrayGetCount(windows); + // Loop all windows in the process + for (CFIndex i = 0; i < count; ++i){ + // Get the element at the index + AXUIElementRef element = (AXUIElementRef) + CFArrayGetValueAtIndex(windows, i); + + CGWindowID temp = 0; + // Use undocumented API to get WindowID + _AXUIElementGetWindow(element, &temp); + + // Check results + if (temp == win) { + // Retain element + CFRetain(element); + result = element; + break; + } + } + + CFRelease(windows); + } + + CFRelease(application); + return result; + } +#elif defined(USE_X11) + + // Error Handling + + typedef int (*XErrorHandler) (Display*, XErrorEvent*); + + static int XHandleError(Display* dp, XErrorEvent* e) { return 0; } + + XErrorHandler mOld; + + void XDismissErrors (void) { + Display *rDisplay = XOpenDisplay(NULL); + // Save old handler and dismiss errors + mOld = XSetErrorHandler(XHandleError); + // Flush output buffer + XSync(rDisplay, False); + + // Reinstate old handler + XSetErrorHandler(mOld); + } + + // Definitions + + struct Hints{ + unsigned long Flags; + unsigned long Funcs; + unsigned long Decorations; + signed long Mode; + unsigned long Stat; + }; + + static Atom WM_STATE = None; + static Atom WM_ABOVE = None; + static Atom WM_HIDDEN = None; + static Atom WM_HMAX = None; + static Atom WM_VMAX = None; + + static Atom WM_DESKTOP = None; + static Atom WM_CURDESK = None; + + static Atom WM_NAME = None; + static Atom WM_UTF8 = None; + static Atom WM_PID = None; + static Atom WM_ACTIVE = None; + static Atom WM_HINTS = None; + static Atom WM_EXTENTS = None; + + //////////////////////////////////////////////////////////////////////////////// + + 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); + WM_HIDDEN = XInternAtom(rDisplay, "_NET_WM_STATE_HIDDEN", True); + WM_HMAX = XInternAtom(rDisplay, "_NET_WM_STATE_MAXIMIZED_HORZ", True); + WM_VMAX = XInternAtom(rDisplay, "_NET_WM_STATE_MAXIMIZED_VERT", True); + + WM_DESKTOP = XInternAtom(rDisplay, "_NET_WM_DESKTOP", True); + WM_CURDESK = XInternAtom(rDisplay, "_NET_CURRENT_DESKTOP", True); + + WM_NAME = XInternAtom(rDisplay, "_NET_WM_NAME", True); + WM_UTF8 = XInternAtom(rDisplay, "UTF8_STRING", True); + WM_PID = XInternAtom(rDisplay, "_NET_WM_PID", True); + WM_ACTIVE = XInternAtom(rDisplay, "_NET_ACTIVE_WINDOW", True); + WM_HINTS = XInternAtom(rDisplay, "_MOTIF_WM_HINTS", True); + WM_EXTENTS = XInternAtom(rDisplay, "_NET_FRAME_EXTENTS", True); + } + + + + // Functions + static void* GetWindowProperty(MData win, Atom atom, uint32* items){ + // Property variables + Atom type; int format; + unsigned long nItems; + unsigned long bAfter; + unsigned char* result = NULL; + + Display *rDisplay = XOpenDisplay(NULL); + // Check the atom + 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) { + + // Copy items result + if (items != NULL) { + *items = (uint32) nItems; + } + + return result; + } + } + + // Reset the items result if valid + if (items != NULL) {*items = 0;} + + // Free the result if it got allocated + if (result != NULL) { + XFree (result); + } + + return NULL; + } + + ////// + + #define STATE_TOPMOST 0 + #define STATE_MINIMIZE 1 + #define STATE_MAXIMIZE 2 + + + ////// + static void SetDesktopForWindow(MData win){ + Display *rDisplay = XOpenDisplay(NULL); + // 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); + + // Check result value + if (desktop != NULL) { + // Retrieve the screen number + XWindowAttributes attr = { 0 }; + XGetWindowAttributes(rDisplay, win.XWin, &attr); + int s = XScreenNumberOfScreen(attr.screen); + Window root = XRootWindow(rDisplay, s); + + // Prepare an event + XClientMessageEvent e = { 0 }; + e.window = root; e.format = 32; + e.message_type = WM_CURDESK; + e.display = rDisplay; + e.type = ClientMessage; + e.data.l[0] = *desktop; + e.data.l[1] = CurrentTime; + + // Send the message + XSendEvent(rDisplay, + root, False, SubstructureNotifyMask | + SubstructureRedirectMask, (XEvent*) &e); + + XFree(desktop); + } + } + } + + +#elif defined(IS_WINDOWS) + // +#endif \ No newline at end of file diff --git a/window/window.h b/window/window.h index d1c4a94..f17a06c 100644 --- a/window/window.h +++ b/window/window.h @@ -11,264 +11,30 @@ // #include "../base/os.h" // #include #include "process.h" +#include "pub.h" struct _MData{ -#if defined(IS_MACOSX) - CGWindowID CgID; // Handle to a CGWindowID - AXUIElementRef AxID; // Handle to a AXUIElementRef -#elif defined(USE_X11) - Window XWin; // Handle to an X11 window -#elif defined(IS_WINDOWS) - HWND HWnd; // Handle to a window HWND - TCHAR Title[512]; -#endif + #if defined(IS_MACOSX) + CGWindowID CgID; // Handle to a CGWindowID + AXUIElementRef AxID; // Handle to a AXUIElementRef + #elif defined(USE_X11) + Window XWin; // Handle to an X11 window + #elif defined(IS_WINDOWS) + HWND HWnd; // Handle to a window HWND + TCHAR Title[512]; + #endif }; typedef struct _MData MData; MData mData; - bool setHandle(uintptr handle); bool IsValid(); bool IsAxEnabled(bool options); MData GetActive(void); void initWindow(); -#if defined(IS_MACOSX) - - static Boolean(*gAXIsProcessTrustedWithOptions) (CFDictionaryRef); - - static CFStringRef* gkAXTrustedCheckOptionPrompt; - - AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out); - - static AXUIElementRef GetUIElement(CGWindowID win){ - intptr pid = 0; - // double_t pid = 0; - - // Create array storing window - CGWindowID window[1] = { win }; - CFArrayRef wlist = CFArrayCreate(NULL, - (const void**)window, 1, NULL); - - // Get window info - CFArrayRef info = CGWindowListCreateDescriptionFromArray(wlist); - CFRelease(wlist); - - // Check whether the resulting array is populated - if (info != NULL && CFArrayGetCount(info) > 0){ - // Retrieve description from info array - CFDictionaryRef desc = (CFDictionaryRef)CFArrayGetValueAtIndex(info, 0); - - // Get window PID - CFNumberRef data =(CFNumberRef) - CFDictionaryGetValue(desc, kCGWindowOwnerPID); - - if (data != NULL){ - CFNumberGetValue(data, kCFNumberIntType, &pid); - } - - // Return result - CFRelease(info); - } - - // Check if PID was retrieved - if (pid <= 0) {return NULL;} - - // Create an accessibility object using retrieved PID - AXUIElementRef application = AXUIElementCreateApplication(pid); - - if (application == 0) {return NULL;} - - CFArrayRef windows = NULL; - // Get all windows associated with the app - AXUIElementCopyAttributeValues(application, - kAXWindowsAttribute, 0, 1024, &windows); - - // Reference to resulting value - AXUIElementRef result = NULL; - - if (windows != NULL) { - int count = CFArrayGetCount(windows); - // Loop all windows in the process - for (CFIndex i = 0; i < count; ++i){ - // Get the element at the index - AXUIElementRef element = (AXUIElementRef) - CFArrayGetValueAtIndex(windows, i); - - CGWindowID temp = 0; - // Use undocumented API to get WindowID - _AXUIElementGetWindow(element, &temp); - - // Check results - if (temp == win) { - // Retain element - CFRetain(element); - result = element; - break; - } - } - - CFRelease(windows); - } - - CFRelease(application); - return result; - } -#elif defined(USE_X11) - - // Error Handling - - typedef int (*XErrorHandler) (Display*, XErrorEvent*); - - static int XHandleError(Display* dp, XErrorEvent* e) { return 0; } - - XErrorHandler mOld; - - void XDismissErrors (void) { - Display *rDisplay = XOpenDisplay(NULL); - // Save old handler and dismiss errors - mOld = XSetErrorHandler(XHandleError); - // Flush output buffer - XSync(rDisplay, False); - - // Reinstate old handler - XSetErrorHandler(mOld); - } - - // Definitions - - struct Hints{ - unsigned long Flags; - unsigned long Funcs; - unsigned long Decorations; - signed long Mode; - unsigned long Stat; - }; - - static Atom WM_STATE = None; - static Atom WM_ABOVE = None; - static Atom WM_HIDDEN = None; - static Atom WM_HMAX = None; - static Atom WM_VMAX = None; - - static Atom WM_DESKTOP = None; - static Atom WM_CURDESK = None; - - static Atom WM_NAME = None; - static Atom WM_UTF8 = None; - static Atom WM_PID = None; - static Atom WM_ACTIVE = None; - static Atom WM_HINTS = None; - static Atom WM_EXTENTS = None; - - //////////////////////////////////////////////////////////////////////////////// - - 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); - WM_HIDDEN = XInternAtom(rDisplay, "_NET_WM_STATE_HIDDEN", True); - WM_HMAX = XInternAtom(rDisplay, "_NET_WM_STATE_MAXIMIZED_HORZ", True); - WM_VMAX = XInternAtom(rDisplay, "_NET_WM_STATE_MAXIMIZED_VERT", True); - - WM_DESKTOP = XInternAtom(rDisplay, "_NET_WM_DESKTOP", True); - WM_CURDESK = XInternAtom(rDisplay, "_NET_CURRENT_DESKTOP", True); - - WM_NAME = XInternAtom(rDisplay, "_NET_WM_NAME", True); - WM_UTF8 = XInternAtom(rDisplay, "UTF8_STRING", True); - WM_PID = XInternAtom(rDisplay, "_NET_WM_PID", True); - WM_ACTIVE = XInternAtom(rDisplay, "_NET_ACTIVE_WINDOW", True); - WM_HINTS = XInternAtom(rDisplay, "_MOTIF_WM_HINTS", True); - WM_EXTENTS = XInternAtom(rDisplay, "_NET_FRAME_EXTENTS", True); - } - - - - // Functions - static void* GetWindowProperty(MData win, Atom atom, uint32* items){ - // Property variables - Atom type; int format; - unsigned long nItems; - unsigned long bAfter; - unsigned char* result = NULL; - - Display *rDisplay = XOpenDisplay(NULL); - // Check the atom - 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) { - - // Copy items result - if (items != NULL) { - *items = (uint32) nItems; - } - - return result; - } - } - - // Reset the items result if valid - if (items != NULL) {*items = 0;} - - // Free the result if it got allocated - if (result != NULL) { - XFree (result); - } - - return NULL; - } - - ////// - - #define STATE_TOPMOST 0 - #define STATE_MINIMIZE 1 - #define STATE_MAXIMIZE 2 - - - ////// - static void SetDesktopForWindow(MData win){ - Display *rDisplay = XOpenDisplay(NULL); - // 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); - - // Check result value - if (desktop != NULL) { - // Retrieve the screen number - XWindowAttributes attr = { 0 }; - XGetWindowAttributes(rDisplay, win.XWin, &attr); - int s = XScreenNumberOfScreen(attr.screen); - Window root = XRootWindow(rDisplay, s); - - // Prepare an event - XClientMessageEvent e = { 0 }; - e.window = root; e.format = 32; - e.message_type = WM_CURDESK; - e.display = rDisplay; - e.type = ClientMessage; - e.data.l[0] = *desktop; - e.data.l[1] = CurrentTime; - - // Send the message - XSendEvent(rDisplay, - root, False, SubstructureNotifyMask | - SubstructureRedirectMask, (XEvent*) &e); - - XFree(desktop); - } - } - } - - -#elif defined(IS_WINDOWS) - // -#endif - //int findwindow() uintptr initHandle = 0;