# Pixmap and Cursor Functions

## Creating and Freeing Pixmaps

Pixmaps can only be used on the screen on which they were created. Pixmaps are off-screen resources that are used for various operations, such as defining cursors as tiling patterns or as the source for certain raster operations. Most graphics requests can operate either on a window or on a pixmap. A bitmap is a single bit-plane pixmap.

To create a pixmap of a given size, use `XCreatePixmap`.

      Pixmap XCreatePixmap
      Display *display
      Drawable d
      unsignedintwidth, height
      unsignedint depth

display

  Specifies the connection to the X server.

d

  Specifies which screen the pixmap is created on.

width

height

  Specify the width and height(Wh.

depth

  Specifies the depth of the pixmap.

The `XCreatePixmap` function creates a pixmap of the width, height, and depth you specified and returns a pixmap ID that identifies it. It is valid to pass an InputOnly window to the drawable argument. The width and height arguments must be nonzero, or a BadValue error results. The depth argument must be one of the depths supported by the screen of the specified drawable, or a BadValue error results.

The server uses the specified drawable to determine on which screen to create the pixmap. The pixmap can be used only on this screen and only with other drawables of the same depth (see `XCopyPlane` for an exception to this rule). The initial contents of the pixmap are undefined.

`XCreatePixmap` can generate BadAlloc, BadDrawable, and BadValue errors.

To free all storage associated with a specified pixmap, use `XFreePixmap`.

      XFreePixmap
      Display *display
      Pixmap pixmap

display

  Specifies the connection to the X server.

pixmap

  Specifies the pixmap.

The `XFreePixmap` function first deletes the association between the pixmap ID and the pixmap. Then, the X server frees the pixmap storage when there are no references to it. The pixmap should never be referenced again.

`XFreePixmap` can generate a BadPixmap error.

## Creating, Recoloring, and Freeing Cursors

Each window can have a different cursor defined for it. Whenever the pointer is in a visible window, it is set to the cursor defined for that window. If no cursor was defined for that window, the cursor is the one defined for the parent window.

From X's perspective, a cursor consists of a cursor source, mask, colors, and a hotspot. The mask pixmap determines the shape of the cursor and must be a depth of one. The source pixmap must have a depth of one, and the colors determine the colors of the source. The hotspot defines the point on the cursor that is reported when a pointer event occurs. There may be limitations imposed by the hardware on cursors as to size and whether a mask is implemented.
`XQueryBestCursor` can be used to find out what sizes are possible. There is a standard font for creating cursors, but Xlib provides functions that you can use to create cursors from an arbitrary font or from bitmaps.

To create a cursor from the standard cursor font, use `XCreateFontCursor`.

#include <X11/cursorfont.h>

      Cursor XCreateFontCursor
      Display *display
      unsignedint shape

display

  Specifies the connection to the X server.

shape

  Specifies the shape of the cursor.

X provides a set of standard cursor shapes in a special font named cursor. Applications are encouraged to use this interface for their cursors because the font can be customized for the individual display type. The shape argument specifies which glyph of the standard fonts to use.

The hotspot comes from the information stored in the cursor font. The initial colors of a cursor are a black foreground and a white background (see `XRecolorCursor`). For further information about cursor shapes, see appendix B.

`XCreateFontCursor` can generate BadAlloc and BadValue errors.

To create a cursor from font glyphs, use `XCreateGlyphCursor`.

      Cursor XCreateGlyphCursor
      Display *display
      Fontsource_font, mask_font
      unsignedintsource_char, mask_char
      XColor *foreground_color
      XColor *background_color

display

  Specifies the connection to the X server.

source_font

  Specifies the font for the source glyph.

mask_font

  Specifies the font for the mask glyph or None.

source_char

  Specifies the character glyph for the source.

mask_char

  Specifies the glyph character for the mask.

foreground_color

  Specifies the RGB values for the foreground of the source.

background_color

  Specifies the RGB values for the background of the source.

The `XCreateGlyphCursor` function is similar to `XCreatePixmapCursor` except that the source and mask bitmaps are obtained from the specified font glyphs. The source_char must be a defined glyph in source_font, or a BadValue error results. If mask_font is given, mask_char must be a defined glyph in mask_font, or a BadValue error results. The mask_font and character are optional. The origins of the source_char and mask_char (if defined) glyphs are positioned coincidently and define the hotspot. The source_char and mask_char need not have the same bounding box metrics, and there is no restriction on the placement of the hotspot relative to the bounding boxes. If no mask_char is given, all pixels of the source are displayed. You can free the fonts immediately by calling `XFreeFont` if no further explicit references to them are to be made.

For 2-byte matrix fonts, the 16-bit value should be formed with the byte1 member in the most significant byte and the byte2 member in the least significant byte.

`XCreateGlyphCursor` can generate BadAlloc, BadFont, and BadValue errors.

To create a cursor from two bitmaps, use `XCreatePixmapCursor`.

      Cursor XCreatePixmapCursor
      Display *display
      Pixmap source
      Pixmap mask
      XColor *foreground_color
      XColor *background_color
      unsignedintx, y

display

  Specifies the connection to the X server.

source

  Specifies the shape of the source cursor.

mask

  Specifies the cursor's source bits to be displayed or None.

foreground_color

  Specifies the RGB values for the foreground of the source.

background_color

  Specifies the RGB values for the background of the source.

x

y

  Specify the x and y coordinates(Xy.

The `XCreatePixmapCursor` function creates a cursor and returns the cursor ID associated with it. The foreground and background RGB values must be specified using foreground_color and background_color, even if the X server only has a StaticGray or GrayScale screen. The foreground color is used for the pixels set to 1 in the source, and the background color is used for the pixels set to 0. Both source and mask, if specified, must have depth one (or a BadMatch error results) but can have any root. The mask argument defines the shape of the cursor. The pixels set to 1 in the mask define which source pixels are displayed, and the pixels set to 0 define which pixels are ignored. If no mask is given, all pixels of the source are displayed. The mask, if present, must be the same size as the pixmap defined by the source argument, or a BadMatch error results. The hotspot must be a point within the source, or a BadMatch error results.

The components of the cursor can be transformed arbitrarily to meet display limitations. The pixmaps can be freed immediately if no further explicit references to them are to be made. Subsequent drawing in the source or mask pixmap has an undefined effect on the cursor. The X server might or might not make a copy of the pixmap.

`XCreatePixmapCursor` can generate BadAlloc and BadPixmap errors.

To determine useful cursor sizes, use `XQueryBestCursor`.

      Status XQueryBestCursor
      Display *display
      Drawable d
      unsignedintwidth, height
      unsignedint*width_return, *height_return

display

  Specifies the connection to the X server.

d

  Specifies the drawable(Dr.

width

height

  Specify the width and height(Wh.

width_return

height_return

  Return the best width and height that is closest to the specified width and height.

Some displays allow larger cursors than other displays. The `XQueryBestCursor` function provides a way to find out what size cursors are actually possible on the display.
It returns the largest size that can be displayed. Applications should be prepared to use smaller cursors on displays that cannot support large ones.

`XQueryBestCursor` can generate a BadDrawable error.

To change the color of a given cursor, use `XRecolorCursor`.

      XRecolorCursor
      Display *display
      Cursor cursor
      XColor*foreground_color, *background_color

display

  Specifies the connection to the X server.

cursor

  Specifies the cursor.

foreground_color

  Specifies the RGB values for the foreground of the source.

background_color

  Specifies the RGB values for the background of the source.

The `XRecolorCursor` function changes the color of the specified cursor, and if the cursor is being displayed on a screen, the change is visible immediately. The pixel members of the XColor structures are ignored; only the RGB values are used.

`XRecolorCursor` can generate a BadCursor error.

To free (destroy) a given cursor, use `XFreeCursor`.

      XFreeCursor
      Display *display
      Cursor cursor

display

  Specifies the connection to the X server.

cursor

  Specifies the cursor.

The `XFreeCursor` function deletes the association between the cursor resource ID and the specified cursor. The cursor storage is freed when no other resource references it. The specified cursor ID should not be referred to again.

`XFreeCursor` can generate a BadCursor error.