XChangeProperty()总是失败

时间:2012-07-05 00:22:24

标签: xlib

我正在学习使用xlib而我无法让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);
}

1 个答案:

答案 0 :(得分: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"