Merge pull request #156 from go-vgo/dev

refactoring get_title() allow by pid
This commit is contained in:
vz 2018-08-23 10:43:33 -04:00 committed by GitHub
commit c8e0da71ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 28 deletions

View File

@ -1385,10 +1385,23 @@ func GetBHandle() int {
} }
// GetTitle get the window title // GetTitle get the window title
func GetTitle() string { func GetTitle(args ...int32) string {
title := C.get_title() if len(args) <= 0 {
title := C.get_main_title()
gtittle := C.GoString(title)
return gtittle
}
var hwnd, isHwnd int32
if len(args) > 0 {
hwnd = args[0]
}
if len(args) > 1 {
isHwnd = args[1]
}
title := C.get_title(C.uintptr(hwnd), C.uintptr(isHwnd))
gtittle := C.GoString(title) gtittle := C.GoString(title)
// fmt.Println("title...", gtittle)
return gtittle return gtittle
} }

View File

@ -10,7 +10,6 @@
#include "alert_c.h" #include "alert_c.h"
#include "window.h" #include "window.h"
#include "win32.h"
#include "win_sys.h" #include "win_sys.h"
int show_alert(const char *title, const char *msg, int show_alert(const char *title, const char *msg,
@ -87,6 +86,7 @@ bool set_handle(uintptr handle){
uintptr get_handle(){ uintptr get_handle(){
MData mData = GetActive(); MData mData = GetActive();
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
return (uintptr)mData.CgID; return (uintptr)mData.CgID;
#elif defined(USE_X11) #elif defined(USE_X11)
@ -106,21 +106,7 @@ void set_active(const MData win){
} }
void active_PID(uintptr pid, uintptr isHwnd){ void active_PID(uintptr pid, uintptr isHwnd){
MData win; MData win = set_hand_pid(pid, isHwnd);
#if defined(IS_MACOSX)
// Handle to a AXUIElementRef
win.AxID = AXUIElementCreateApplication(pid);
#elif defined(USE_X11)
win.XWin = (Window)pid; // Handle to an X11 window
#elif defined(IS_WINDOWS)
// win.HWnd = (HWND)pid; // Handle to a window HWND
if (isHwnd == 0) {
win.HWnd = GetHwndByPId(pid);
} else {
win.HWnd = (HWND)pid;
}
#endif
SetActive(win); SetActive(win);
} }
@ -129,8 +115,8 @@ MData get_active(){
return mdata; return mdata;
} }
char* get_title(){ char* get_title(uintptr pid, uintptr isHwnd){
char* title = GetTitle(); char* title = get_title_by_pid(pid, isHwnd);
// printf("title::::%s\n", title ); // printf("title::::%s\n", title );
return title; return title;
} }

View File

@ -12,12 +12,14 @@
// #include <stdlib.h> // #include <stdlib.h>
#include "process.h" #include "process.h"
#include "pub.h" #include "pub.h"
#include "win32.h"
bool setHandle(uintptr handle); bool setHandle(uintptr handle);
bool IsValid(); bool IsValid();
bool IsAxEnabled(bool options); bool IsAxEnabled(bool options);
MData GetActive(void); MData GetActive(void);
void initWindow(); void initWindow();
char* get_title_by_hand(MData m_data);
//int findwindow() //int findwindow()
@ -46,6 +48,26 @@ void initWindow(uintptr handle){
setHandle(handle); setHandle(handle);
} }
MData set_hand_pid(uintptr pid, uintptr isHwnd){
MData win;
#if defined(IS_MACOSX)
// Handle to a AXUIElementRef
win.AxID = AXUIElementCreateApplication(pid);
#elif defined(USE_X11)
win.XWin = (Window)pid; // Handle to an X11 window
#elif defined(IS_WINDOWS)
// win.HWnd = (HWND)pid; // Handle to a window HWND
if (isHwnd == 0) {
win.HWnd = GetHwndByPId(pid);
} else {
win.HWnd = (HWND)pid;
}
#endif
return win;
}
bool IsValid(){ bool IsValid(){
initWindow(initHandle); initWindow(initHandle);
if (!IsAxEnabled(true)) { if (!IsAxEnabled(true)) {
@ -545,11 +567,22 @@ void CloseWin(void){
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
PostMessage(mData.HWnd, WM_CLOSE, 0, 0); PostMessage(mData.HWnd, WM_CLOSE, 0, 0);
#endif #endif
} }
char *GetTitle(){ char* get_main_title(){
// Check if the window is valid
if (!IsValid()) {return "IsValid failed.";}
return get_title_by_hand(mData);
}
char* get_title_by_pid(uintptr pid, uintptr isHwnd){
MData win = set_hand_pid(pid, isHwnd);
return get_title_by_hand(win);
}
char* get_title_by_hand(MData m_data){
// Check if the window is valid // Check if the window is valid
if (!IsValid()) {return "IsValid failed.";} if (!IsValid()) {return "IsValid failed.";}
@ -558,7 +591,7 @@ char *GetTitle(){
CFStringRef data = NULL; CFStringRef data = NULL;
// Determine the current title of the window // Determine the current title of the window
if (AXUIElementCopyAttributeValue(mData.AxID, if (AXUIElementCopyAttributeValue(m_data.AxID,
kAXTitleAttribute, (CFTypeRef*) &data) kAXTitleAttribute, (CFTypeRef*) &data)
== kAXErrorSuccess && data != NULL) { == kAXErrorSuccess && data != NULL) {
char conv[512]; char conv[512];
@ -581,7 +614,7 @@ char *GetTitle(){
XDismissErrors(); XDismissErrors();
// Get window title (UTF-8) // Get window title (UTF-8)
result = GetWindowProperty(mData, WM_NAME,NULL); result = GetWindowProperty(m_data, WM_NAME,NULL);
// Check result value // Check result value
if (result != NULL) { if (result != NULL) {
@ -593,7 +626,7 @@ char *GetTitle(){
} }
// Get window title (ASCII) // Get window title (ASCII)
result = GetWindowProperty(mData, XA_WM_NAME,NULL); result = GetWindowProperty(m_data, XA_WM_NAME,NULL);
// Check result value // Check result value
if (result != NULL) { if (result != NULL) {
@ -608,8 +641,8 @@ char *GetTitle(){
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return GetWindowText( return GetWindowText(
mData.HWnd, mData.Title, 512) > 0 ? m_data.HWnd, m_data.Title, 512) > 0 ?
mData.Title : ""; m_data.Title : "";
// return GetWindowText // return GetWindowText
// (mData.HWnd, name, 512) > 0 ? // (mData.HWnd, name, 512) > 0 ?
// _UTF8Encode(name) : "null"; // _UTF8Encode(name) : "null";