2012-07-05 256 views
4

我正在學習使用xlib,我無法獲得XChangeProperty()爲我工作。XChangeProperty()總是失敗

我有一個簡單的程序,顯示一個窗口成功。但撥打XChangeProperty()始終會失敗,錯誤代碼爲error 1 (BadRequest)

有人能告訴我我做錯了什麼嗎?

這是我的代碼來更改屬性。

static void 
change_prop(Display *display, Window window) 
{ 
    unsigned char some_text[40] = "hello world!"; 
    int retval; 
    Atom my_atom; 

    my_atom = XInternAtom(display, "PERSONAL_PROPERTY", False); 
    if (my_atom == None) 
    { 
      printf("### failed to create atom with name PERSONAL_PROPERTY\n"); 
      return; 
    } 

    retval = XChangeProperty(display, /* connection to x server */ 
          window, /* window whose property we want to change */ 
          my_atom, /* property name */ 
          XA_STRING, /* type of property */ 
          8,   /* format of prop; can be 8, 16, 32 */ 
          PropModeReplace, 
          some_text, /* actual data */ 
          10   /* number of elements */ 
          ); 

    printf("###### XChangeProperty() reted %d\n", retval); 
} 

回答

4

大多數Xlib函數始終返回1,你應該use error handlers檢查錯誤。請參閱XChangeProperty implementation - 註釋return 1最後。

你的代碼工作得很好:

#include <stdio.h> 
#include <stdlib.h> 
#include <X11/Xlib.h> 
#include <X11/Xutil.h> 
#include <X11/Xos.h> 
#include <X11/Xatom.h> 
#include <X11/keysym.h> 


static void 
change_prop(Display *display, Window window) 
{ 
    unsigned char some_text[40] = "hello world!"; 
    int retval; 
    Atom my_atom; 

    my_atom = XInternAtom(display, "PERSONAL_PROPERTY", False); 
    if (my_atom == None) 
    { 
      printf("### failed to create atom with name PERSONAL_PROPERTY\n"); 
      return; 
    } 

    retval = XChangeProperty(display, /* connection to x server */ 
          window, /* window whose property we want to change */ 
          my_atom, /* property name */ 
          XA_STRING, /* type of property */ 
          8,   /* format of prop; can be 8, 16, 32 */ 
          PropModeReplace, 
          some_text, /* actual data */ 
          10   /* number of elements */ 
          ); 

    printf("###### XChangeProperty() reted %d\n", retval); 
} 

int main() 
{ 

    Display *dis; 
    Window win; 

    dis = XOpenDisplay(NULL); 
    win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, \ 
    0, BlackPixel (dis, 0), BlackPixel(dis, 0)); 
    XMapWindow(dis, win); 
    printf("window %i\n", (int)win); 
    change_prop(dis, win); 

    XFlush(dis); 
    sleep(50); 
    return(0); 
} 

結果:

09:48 tmp $ g++ prop.cpp /usr/X11/lib/libX11.dylib 
09:48 tmp $ ./a.out 
window 6291457 
###### XChangeProperty() reted 1 

xprop結果:

09:48 tmp $ xprop -id 6291457 
WM_STATE(WM_STATE): 
     window state: Normal 
     icon window: 0x0 
_NET_WM_STATE(ATOM) = 
_NET_WM_ALLOWED_ACTIONS(ATOM) = _NET_WM_ACTION_MOVE, _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_MINIMIZE, _NET_WM_ACTION_MAXIMIZE_HORZ, _NET_WM_ACTION_MAXIMIZE_VERT, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE 
PERSONAL_PROPERTY(STRING) = "hello worl"