5

我正在学习使用 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);
}
4

1 回答 1

4

大多数 xlib 函数总是返回 1,您应该使用错误处理程序来检查错误。请参阅XChangeProperty 实现-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"
于 2012-07-06T01:00:54.490 回答