XCreateWindow(3)

πŸ’… XCreateWindow(3) πŸ’…

ok so this is a page from XLIB FUNCTIONS and bestie, it's literally the origin story of every window you've ever seen on an X display. buckle up πŸŽ€


✨ NAME ✨

XCreateWindow, XCreateSimpleWindow, XSetWindowAttributes β€” the trio that creates windows and hands you the window attributes structure. main character energy fr fr πŸ‘‘


🌸 SYNTAX 🌸

Window XCreateWindow(Display *display, Window parent, int x, int y,
       unsigned int width, unsigned int height, unsigned int
       border_width, int depth, unsigned int class, Visual *visual,
       unsigned long valuemask, XSetWindowAttributes *attributes);

Window XCreateSimpleWindow(Display *display, Window parent, int x, int
       y, unsigned int width, unsigned int height, unsigned int
       border_width, unsigned long border, unsigned long background);

like, XCreateWindow is the full-control queen and XCreateSimpleWindow is her low-effort little sister who still slays πŸ’…


πŸ¦‹ ARGUMENTS πŸ¦‹

  • attributes β€” this is the structure the values get pulled from (the ones your value mask says are set, anyway). the value mask needs the right bits turned on so it knows which attributes actually got set in the struct. no cap, the mask is the whole vibe check here.
  • background β€” the background pixel value of the window. that's it, that's the tweet.
  • border β€” the border pixel value of the window.
  • border_width β€” how wide the created window's border is, in pixels. it's giving "frame thickness" ✨
  • class β€” the created window's class. you can pass InputOutput, InputOnly, or CopyFromParent. and a class of CopyFromParent means, yup, the class is taken from the parent. inheritance icon πŸ’–
  • depth β€” the window's depth. a depth of CopyFromParent means the depth comes from the parent. same energy, same nepo-baby behavior 🫢
  • display β€” the connection to the X server. your link to the mothership.
  • parent β€” the parent window. every window needs a mother, periodt.
  • valuemask β€” which window attributes are defined in the attributes argument. this mask is the bitwise inclusive OR of the valid attribute mask bits. and listen: if valuemask is zero, the attributes are ignored and not referenced. like, she's not even looking at them πŸ™ˆ
  • visual β€” the visual type. a visual of CopyFromParent means the visual type is taken from the parent. again with the inheritance, obsessed 😭
  • width, height β€” the width and height, which are the created window's inside dimensions and do not include the created window's borders. so don't come crying when the border adds extra pixels, that's on you bestie.
  • x, y β€” the x and y coordinates, which are the top-left outside corner of the window's borders, and they're relative to the inside of the parent window's borders. read that twice, it matters πŸ’€

πŸ”₯ DESCRIPTION πŸ”₯

So the XCreateWindow function creates an unmapped subwindow for a specified parent window, returns the window ID of the created window, and causes the X server to generate a CreateNotify event. and the created window? placed on top in the stacking order with respect to siblings. she's literally above the drama πŸ‘‘

The coordinate system has the X axis horizontal and the Y axis vertical, with the origin [0, 0] at the upper-left corner. coordinates are integral, in terms of pixels, and coincide with pixel centers. every window and pixmap has its own coordinate system. for a window, the origin is inside the border at the inside, upper-left corner. cute little self-contained universe 🧚

Now the rules, and ngl these are the important ones:

  • The border_width for an InputOnly window must be zero, or a BadMatch error results.
  • For class InputOutput, the visual type and depth must be a combination supported for the screen, or a BadMatch error results.
  • The depth need not be the same as the parent, but the parent must not be a window of class InputOnly, or a BadMatch error results.
  • For an InputOnly window, the depth must be zero, and the visual must be one supported by the screen. if either condition is not met, a BadMatch error results.
  • The parent window, however, may have any depth and class. slay, flexible queen 🌈
  • If you specify any invalid window attribute for a window, a BadMatch error results.

The created window is not yet displayed (mapped) on the user's display. to display the window, call XMapWindow. the new window initially uses the same cursor as its parent. a new cursor can be defined for the new window by calling XDefineCursor. and the window will not be visible on the screen unless it and all of its ancestors are mapped and it is not obscured by any of its ancestors. so like, if grandma's not showing up, nobody's showing up πŸ’€

XCreateWindow can generate BadAlloc BadColor, BadCursor, BadMatch, BadPixmap, BadValue, and BadWindow errors.


Now the XCreateSimpleWindow function creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window, and causes the X server to generate a CreateNotify event. created window goes on top in the stacking order with respect to siblings. any part of the window that extends outside its parent window is clipped βœ‚οΈ

The border_width for an InputOnly window must be zero, or a BadMatch error results. XCreateSimpleWindow inherits its depth, class, and visual from its parent. all other window attributes, except background and border, have their default values. lowkey the default settings enjoyer πŸ“

XCreateSimpleWindow can generate BadAlloc, BadMatch, BadValue, and BadWindow errors.


πŸŽ€ STRUCTURES πŸŽ€

The XSetWindowAttributes structure contains:

/* Window attribute value mask bits */
#define   CWBackPixmap                (1L<<0)
#define   CWBackPixel                 (1L<<1)
#define   CWBorderPixmap              (1L<<2)
#define   CWBorderPixel               (1L<<3)
#define   CWBitGravity                (1L<<4)
#define   CWWinGravity                (1L<<5)
#define   CWBackingStore              (1L<<6)
#define   CWBackingPlanes             (1L<<7)
#define   CWBackingPixel              (1L<<8)
#define   CWOverrideRedirect          (1L<<9)
#define   CWSaveUnder                 (1L<<10)
#define   CWEventMask                 (1L<<11)
#define   CWDontPropagate             (1L<<12)
#define   CWColormap                  (1L<<13)
#define   CWCursor                    (1L<<14)
/* Values */

typedef struct {
        Pixmap background_pixmap;       /* background, None, or ParentRelative */
        unsigned long background_pixel; /* background pixel */
        Pixmap border_pixmap;           /* border of the window or CopyFromParent */
        unsigned long border_pixel;     /* border pixel value */
        int bit_gravity;        /* one of bit gravity values */
        int win_gravity;        /* one of the window gravity values */
        int backing_store;      /* NotUseful, WhenMapped, Always */
        unsigned long backing_planes;   /* planes to be preserved if possible */
        unsigned long backing_pixel;    /* value to use in restoring planes */
        Bool save_under;        /* should bits under be saved? (popups) */
        long event_mask;        /* set of events that should be saved */
        long do_not_propagate_mask;     /* set of events that should not propagate */
        Bool override_redirect; /* boolean value for override_redirect */
        Colormap colormap;      /* color map to be associated with window */
        Cursor cursor;          /* cursor to be displayed (or None) */
} XSetWindowAttributes;

for a detailed explanation of the members of this structure, see Xlib - C Language X Interface. it's giving required reading πŸ“š


πŸ’€ DIAGNOSTICS πŸ’€

  • BadAlloc β€” the server failed to allocate the requested resource or server memory. she tried her best 😭
  • BadColor β€” a value for a Colormap argument does not name a defined Colormap.
  • BadCursor β€” a value for a Cursor argument does not name a defined Cursor.
  • BadMatch β€” the values do not exist for an InputOnly window.
  • BadMatch β€” some argument or pair of arguments has the correct type and range but fails to match in some other way required by the request. (yes bestie, BadMatch shows up twice, and both times it's a different flavor of "nope" πŸ™ˆ)
  • BadPixmap β€” a value for a Pixmap argument does not name a defined Pixmap.
  • BadValue β€” some numeric value falls outside the range of values accepted by the request. unless a specific range is specified for an argument, the full range defined by the argument's type is accepted. any argument defined as a set of alternatives can generate this error.
  • BadWindow β€” a value for a Window argument does not name a defined Window. she doesn't know her πŸ’€

🌈 SEE ALSO 🌈

XChangeWindowAttributes(3), XConfigureWindow(3), XDefineCursor(3), XDestroyWindow(3), XMapWindow(3), XRaiseWindow(3), XUnmapWindow(3), Xlib - C Language X Interface


and that's the whole page bestie 🫢 this one's from X Version 11, libX11 1.8.14, page XCreateWindow(3) β€” go forth and create windows, you absolute icon πŸ’œβœ¨

πŸ“¦ where this page comes from