2012-01-11 2 views
3

내 Linux 시스템에서 활성 윈도우의 제목을 가져 오는 C 코드를 작성하려하지만 Function XFetchName은 항상 0을 반환하고 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'에서'_' '? –

+1

Xterm (기본적으로'xterm'이라는 이름이 설정되어 있음)에서 실행할 때 코드는 현재 OSX Lion을 사용하고있는 컴퓨터에서 완벽하게 작동합니다.또한'XStoreName()'을 추가하여 먼저 다른 이름으로 설정하고 예상대로 새로운 이름을 검색하는 코드로도 작업했습니다. 이것은 물론'_main()'을'main()'으로 이름을 바꾼 후입니다 - 프로그램을 어떻게 운영하고 있습니까? –

+0

'_'은 stdlib을 사용하지 않으므로 "-Wl, -e__main"명령 줄을 사용하여 진입 점 이름을 정의하는 것입니다. 아무 것도하지 않아도됩니다. 여전히 우분투 터미널에서 실행되면 0을 반환하지만 xterm에서 작동합니다. 이유가 있습니까? – killercode

답변

0

XFetchName 함수는 지정된 윈도우의 이름을 반환합니다. 성공하면 0이 아닌 상태를 반환합니다. 그렇지 않으면 창에 대한 이름이 설정되지 않고 0이 반환됩니다.

창의 이름을 설정해야합니다.

가 나는 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()이 실제 문제가 될 수있는 더 큰 프레임 워크에 연결되어 있기 때문에 의심 스럽습니다. –

+0

@BrianRoach 좋은 지적입니다. 우리는 OP에서 더 자세한 내용을 듣고 싶습니다. –

+0

'_'은 stdlib를 사용하지 않기 때문에 진입 점 이름 "-Wl, -e__main"을 정의하는 것입니다. – killercode

1

XGetWMName 기능을 사용해 볼 수 있습니다. XGetWMNameXFetchName의 디스크는 모두 WM_NAME 속성을 반환한다고 말하지만 서로 다릅니다. 어떤 때는 같은 이름을 반환합니다. 때로는 XGetWMName 만 이름을 반환합니다.

xwininfo -root -tree을 사용하여 모든 창 '이름을 가져와 XFetchNameXGetWMName의 결과와 비교할 수도 있습니다.

이 코드는 모든 창을 나열하고 XFetchNameXGetWMName의 창 ID와 결과를 인쇄 할 수 있습니다. 창 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