XShm(3)

πŸ’– XShm β€” the X Shared Memory extension functions πŸ’–

okay bestie, sit down, hydrate, because we're about to talk about XShm, which lives in the X FUNCTIONS corner of your manual and is genuinely one of the most underrated glow-ups in X11. it's the shared memory extension. it lets your client and the X server share a memory segment so you're not yeeting pixels across a socket like it's 1993. iconic behaviour, honestly.


✨ NAME ✨

here's the whole squad, no cap:

  • XShmQueryExtension
  • XShmQueryVersion
  • XShmPixmapFormat
  • XShmAttach
  • XShmDetach
  • XShmCreateImage
  • XShmPutImage
  • XShmGetImage
  • XShmCreatePixmap
  • XShmGetEventBase

and what do they all do, collectively? they are the X Shared Memory extension functions. that's the vibe. 🫢


🌸 SYNTAX 🌸

first, the includes. you need all of these, don't skip any, they're the fit check:

#include <X11/Xlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>

now the function signatures, exactly as they are, i am not touching a single character of these:

Bool XShmQueryExtension(
     Display *display);

Bool XShmQueryVersion(
     Display *display;
     int *major, *minor;
     Bool *pixmaps);

int XShmPixmapFormat(
     Display *display);

Bool XShmAttach(
     Display *display;
     XShmSegmentInfo *shminfo);

Bool XShmDetach(
     Display *display;
     XShmSegmentInfo *shminfo);

XImage *XShmCreateImage (
     Display *display;
     Visual *visual;
     unsigned int depth;
     int format;
     char *data;
     XShmSegmentInfo *shminfo;
     unsigned int width, height);

Bool XShmPutImage(
     Display *display;
     Drawable d;
     GC gc;
     XImage *image;
     int src_x, src_y, dest_x, dest_y;
     unsigned int width, height;
     bool send_event);

Bool XShmGetImage (
     Display *display;
     Drawable d;
     XImage *image;
     int x, y;
     unsigned long plane_mask);

Pixmap XShmCreatePixmap(
        Display *display;
        Drawable d;
        char *data;
     XShmSegmentInfo *shminfo;
     unsigned int width, height, depth);

Status XShmGetEventBase(
     Display *display);
ok yes i know there are semicolons where you'd expect commas in the parameter lists, and bool shows up lowercase in XShmPutImage while everything else says Bool. that's just how the original is. i'm not gaslighting you, i'm transcribing. πŸ’…

🧚 STRUCTURES 🧚

events

this is the completion event. when a XShmPutImage finishes, this is what comes back to you:

typedef struct {
    int type;               /* of event */
    unsigned long serial;   /* # of last request processed by server*/
    Bool send_event;        /* true if this came from a SendEvent request*/
    Display *display;       /* Display the event was read from */
    Drawable drawable;      /* drawable of request */
    int major_code;         /* ShmReqCode */
    int minor_code;         /* X_ShmPutImage */
    ShmSeg shmseg;          /* the ShmSeg used in the request*/
    unsigned long offset;   /* the offset into ShmSeg used in the request*/
} XShmCompletionEvent;

XShmSegmentInfo

and this is a structure of type XShmSegmentInfo β€” the little ID card for your shared memory segment:

typedef struct {
    ShmSeg shmseg;      /* resource id */
    int shmid;          /* kernel id */
    char *shmaddr;      /* address in client */
    Bool readOnly;      /* how the server should attach it */
} XShmSegmentInfo;

πŸ‘‘ DESCRIPTION πŸ‘‘

grab a snack, this is the main character section.

XShmQueryExtension β€” checks to see if the shared memory extensions are available for the specified display. that's it. that's the whole job. it's a vibe check for the extension.

XShmQueryVersion β€” returns the version numbers of the extension implementation. and if the pixmaps argument comes back true? shared memory pixmaps are supported. slay.

XShmAttach β€” tells the server to attach to your shared memory segment. if all goes well, you will get a non-zero status back, and your XImage is ready for use. πŸ”₯

XShmDetach β€” tells the server to detach from your shared memory segment. the breakup arc.

XShmPutImage β€” combines an image in memory with a shape of the specified drawable.

⚠️ now listen carefully bestie because this part matters: if XYBitmap format is used, the depth must be one, or a `BadMatch` error results. the foreground pixel in the GC defines the source for the one bits in the image, and the background pixel defines the source for the zero bits. for XYPixmap and ZPixmap, the depth must match the depth of the drawable, or a `BadMatch` error results. no cap.

XShmGetImage β€” reads image data into a shared memory XImage, where display is the display of interest, drawable is the source drawable, image is the destination XImage, x and y are offsets within the drawable, and plane_mask defines which planes are to be read. it's giving "controlled theft of pixels, but consensual."

XShmCreateImage β€” allocates the memory needed for an XImage structure for the specified display, but does not allocate space for the image itself. so like, it books the venue but doesn't buy the snacks. πŸ™ˆ

XShmPixmapFormat β€” gets the format for the server. if your application can deal with the server pixmap data format, a shared memory segment and shminfo structure are created.

XShmCreatePixmap β€” points to a pixmap which you can manipulate in all of the usual ways, with the added bonus of being able to edit its contents directly through the shared memory segment. main character pixmap energy, honestly.

XShmGetEventBase β€” gets the completion event value. short, sweet, gets the job done. ✨


🌈 SEE ALSO 🌈

  • MIT-SHM - The MIT Shared Memory Extension

and that's the whole page, bestie. go forth and share your memory responsibly. πŸ’œ

this has been libXext 1.3.6, part of X Version 11, and the page is XShm(3) β€” mwah, take care of her. πŸŽ€πŸ’–πŸ¦‹

πŸ“¦ where this page comes from