xlib - XChangeProperty() 总是失败

标签 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);
}

最佳答案

大多数 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"

关于xlib - XChangeProperty() 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336596/

相关文章:

c++ - Xlib的Colormap在哪里定义的?

c - 从屏幕获取像素颜色

linux - 如何解释 XImage 的字段 'data'

python - 带有框的 pygtk 窗口忽略所有 X(鼠标)事件(通过它们)

c++ - Linux C++ XImage RGB <-> BGR?

c++ - 是否可以在 Linux 中以编程方式单击另一个应用程序的按钮?

python - 使用 PySide 和 PyKDE4 移动窗口

xlib - xinit 窗口始终位于顶部 _NET_WM_STATE _NET_WM_STATE_ABOVE

x11 - X 服务器上的致命 IO 错误 0(成功)

c++ - XQueryPointer 掩码返回值中的位代表什么键?