Search code examples
xlib

XChangeProperty() always fails


I'm learning to use xlib and I'm unable to get XChangeProperty() to work for me.

I have a simple program that displays a window successfully. But calls to XChangeProperty() always fail with error code error 1 (BadRequest).

Can someone tell me what I'm doing wrong?

Here is my code to change a property.

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

Solution

  • Most xlib functions always return 1 and you should use error handlers to check for errors. See XChangeProperty implementation - note return 1 at the end.

    Your code works just fine:

    #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);
    }
    

    result:

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

    xprop result:

    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"