2012-01-11 130 views
3

即時嘗試編寫一個C代碼來獲得我的Linux系統中的活動窗口的標題,但函數XFetchName總是返回零,我也試過XGetWMName,結果相同... 但使用xprop,我可以看到「WM_NAME」屬性中有一個字符串XFetchName總是返回0

任何人都可以告訴我我的代碼有什麼問題嗎?

#include <X11/Xlib.h> 
#include <stdio.h> 
#include <stdarg.h> 


int main(int argc, char* argv[]) 
{ 
     Display *display; 
     Window focus; 
     char *window_name; 
     int revert; 

     display = XOpenDisplay(NULL); 
     XGetInputFocus(display, &focus, &revert); 
     int ret = XFetchName(display, focus, &window_name); 
     printf("ret = %d\n", ret); 
     if (window_name) printf("Title = %s\n", window_name); 
     return 0; 
} 

謝謝。

+0

爲什麼'_'在'_main '? – 2012-01-11 19:55:29

+1

從Xterm運行(默認情況下名稱爲「xterm」設置),您的代碼在我正在使用的計算機(OSX Lion)上運行得非常好。另外,我添加了一個對'XStoreName()'的調用,將其設置爲其他第一個,並且按照預期工作,並且您的代碼檢索新名稱就好了。這當然是在將_main()重命名爲main()之後的 - 你如何運行你的程序? – 2012-01-11 20:07:37

+0

'_'只是用命令行「-Wl,-e__main」來定義入口點名稱,因爲我沒有使用stdlib,我不認爲這必須做任何事情。並仍然從Ubuntu的終端執行時返回0,但從xterm工作,是否有這個原因? – killercode 2012-01-12 01:44:52

回答

0

XFetchName函數返回指定窗口的名稱。如果成功,則返回非零狀態;否則,沒有爲該窗口設置名稱,並且它返回零。

您需要爲您的窗口設置一個名稱。

我開始一個xterm會話和執行你的代碼,並得到了以下的輸出:

[email protected]:~/work/test$ ./a.out 
ret = 1 
Title = [email protected]: ~/work/test 
[email protected]:~/work/test$ 

OTOH,我試圖編譯程序,並得到了以下錯誤:

(.text+0x18): undefined reference to `main' 

你需要改變

int _main(int argc, char* argv[]) 

int main(int argc, char* argv[]) 
+0

我懷疑'_main()'是因爲它是一個大型框架的鉤子,可能是實際的問題。 – 2012-01-11 20:13:34

+0

@BrianRoach這是一個很好的觀點。我們需要聽取來自OP的更多細節。 – 2012-01-11 20:15:48

+0

'_'只是定義一個入口點名稱「-Wl,-e__main」,因爲我沒有使用stdlib – killercode 2012-01-12 01:41:09

1

您可以嘗試使用XGetWMName函數。儘管XGetWMNameXFetchName的描述都表示他們會返回WM_NAME屬性,但它們似乎彼此不同。有時候,他們會返回相同的名字。有時候,只有XGetWMName返回名稱。

您也可以使用xwininfo -root -tree來獲取所有窗口的名稱,並與XFetchNameXGetWMName的結果進行比較。

此代碼可以列出所有窗口並打印窗口ID和XFetchNameXGetWMName的結果。您可以使用窗口ID在xwininfo -root -tree的輸出中查找。

#include <stdio.h> 
#include <X11/Xlib.h> 
#include <X11/Xutil.h> 

void enum_windows(Display* display, Window window, int depth) { 
    int i; 

    XTextProperty text; 
    XGetWMName(display, window, &text); 
    char* name; 
    XFetchName(display, window, &name); 
    for (i = 0; i < depth; i++) 
    printf("\t"); 
    printf("id=0x%x, XFetchName=\"%s\", XGetWMName=\"%s\"\n", window, name != NULL ? name : "(no name)", text.value); 

    Window root, parent; 
    Window* children; 
    int n; 
    XQueryTree(display, window, &root, &parent, &children, &n); 
    if (children != NULL) { 
    for (i = 0; i < n; i++) { 
     enum_windows(display, children[i], depth + 1); 
    } 
    XFree(children); 
    } 
} 

int main() { 
    Display* display = XOpenDisplay(NULL); 
    Window root = XDefaultRootWindow(display); 
    enum_windows(display, root, 0); 
} 

這裏的一個片輸出的表示兩個函數的結果可以是不同的。

id=0x2c7, XFetchName="(no name)", XGetWMName="(null)" 
    id=0x400001, XFetchName="(no name)", XGetWMName="(null)" 
    id=0x800036, XFetchName="(no name)", XGetWMName="(null)" 
     id=0x1400001, XFetchName="(no name)", XGetWMName="c - XFetchName always returns 0 - Stack Overflow - Chromium" 
    id=0x1000001, XFetchName="terminator", XGetWMName="terminator" 
     id=0x1000002, XFetchName="(no name)", XGetWMName="(null)" 
    id=0x1200001, XFetchName="chromium", XGetWMName="chromium" 
     id=0x1200002, XFetchName="(no name)", XGetWMName="(null)" 

這裏的一個片的xwininfo -root -tree輸出表示這些窗口的名稱。 xwininfo:窗口ID:0x2c7(根窗口)(沒有名字)

Root window id: 0x2c7 (the root window) (has no name) 
    Parent window id: 0x0 (none) 
    29 children: 
    0x1200001 "chromium": ("chromium" "Chromium") 10x10+10+10 +10+10 
     1 child: 
     0x1200002 (has no name):() 1x1+-1+-1 +9+9 
    0x1000001 "terminator": ("terminator" "Terminator") 10x10+10+10 +10+10 
     1 child: 
     0x1000002 (has no name):() 1x1+-1+-1 +9+9 
    0x800036 (has no name):() 1364x741+0+25 +0+25 
     1 child: 
     0x1400001 "c - XFetchName always returns 0 - Stack Overflow - Chromium": ("Chromium" "Chromium") 1364x741+0+0 +1+26 
    0x400001 (has no name):() 10x10+-20+-20 +-20+-20 
0
/* 
    * The following functions are internationalized substitutions 
    * for XFetchName and XGetIconName using XGetWMName and 
    * XGetWMIconName. 
    * 
    * Please note that the third arguments have to be freed using free(), 
    * not XFree(). 
    */ 
Status 
I18N_FetchName(dpy, w, winname) 
    Display *dpy; 
    Window w; 
    char ** winname; 
{ 
    int status; 
    XTextProperty text_prop; 
    char **list; 
    int num; 

    status = XGetWMName(dpy, w, &text_prop); 
    if (!status || !text_prop.value || !text_prop.nitems) return 0; 
    status = XmbTextPropertyToTextList(dpy, &text_prop, &list, &num); 
    if (status < Success || !num || !*list) return 0; 
    XFree(text_prop.value); 
    *winname = (char *)strdup(*list); 
    XFreeStringList(list); 
    return 1; 
} 

// XFetchName使用XGetWMName

見:enter link description here