1 xtk_icon_theme
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_icon_theme.h

The user's icon theme: which one it is, and the file of an icon in it.

Which theme. One choice decides it, in this order:

  1. The program's own choice, xtk_icon_theme_set_program_choice(), when it made one (a program keeps it in its own settings).
  2. Else the choice all xtk programs share, in ~/.config/xtk/icons.conf (a line theme = ...; xtk_icon_theme_set_shared_choice() writes it).
  3. Else "desktop".

A choice is one of:

  • "desktop": the theme the desktop uses. It is read from XSETTINGS (Net/IconThemeName, which GNOME, Xfce, MATE, LXDE and xsettingsd set; only after xtk_icon_theme_set_display()), else from ~/.config/gtk-4.0/settings.ini, ~/.config/gtk-3.0/settings.ini or ~/.gtkrc-2.0 (gtk-icon-theme-name), else from ~/.config/kdeglobals ([Icons] Theme=), else it is "hicolor".
  • "builtin": no theme; xtk_icon.h draws its own icons, as before.
  • The name of an installed theme (its folder's name, such as "Papirus").

Finding an icon. xtk_icon_theme_lookup() looks for a name in the theme, then in the themes it inherits from (its Inherits= line, at any depth), then in "hicolor", then outside any theme (an icon file directly in an icons folder, then in /usr/share/pixmaps). A theme is a folder with an index.theme in $XDG_DATA_HOME/icons (~/.local/share/icons), ~/.icons, or icons in each folder of $XDG_DATA_DIRS (/usr/local/share and /usr/share). Within a theme, an icon of the size asked for wins; else the one closest in size. A file may be PNG, SVG (or SVGZ) or XPM. A name that is not found in a theme is tried there again without its last part ("go-previous-symbolic", then "go-previous") before the theme it inherits, as the Icon Naming Specification says; a MIME type's icon name ("text-x-csrc") is not cut. Answers are kept, so the same lookup costs nothing the second time; they are forgotten when an icon folder changes (xtk_icon_theme_check()). An index.theme's lines may be of any length, its Type of any case.

A file's icon. xtk_icon_theme_file_icons() gives the names to look for a file's icon: its type's own icon ("text-x-csrc"), then its type's generic one ("text-x-generic"). The type comes from the file's name, by the patterns shared-mime-info installs (/usr/share/mime/globs2).

Following changes. A program that calls xtk_icon_theme_set_display() and gives its X events to xtk_icon_theme_handle_event() learns when the desktop's theme changes; xtk_icon_theme_check() notices a changed icons.conf or GTK settings file, and (every five seconds, as the spec asks) an icon folder whose time changed: a theme or an icon installed. Each returns 1 when the icons may be others now: then the program redraws.

Limitations.

  • It does not read icon-theme.cache files: it looks at the files themselves, which is slower the first time an icon is asked for.
  • The scale of an icon (@2x folders) is always 1; a large screen asks for a larger size instead (xtk_icon_scaled_size()).
  • It keeps its state in static variables: one thread at a time.
#include "xtk_icon_theme.h"

Constants: XTK_ICON_THEME_NAME_MAX

Functions: xtk_icon_theme_set_display, xtk_icon_theme_handle_event, xtk_icon_theme_check, xtk_icon_theme_set_program_choice, xtk_icon_theme_get_program_choice, xtk_icon_theme_get_shared_choice, xtk_icon_theme_set_shared_choice, xtk_icon_theme_current, xtk_icon_theme_list, xtk_icon_theme_lookup, xtk_icon_theme_file_icons, xtk_icon_theme_lookup_list, xtk_icon_theme_names_for_key, xtk_icon_theme_lookup_key, xtk_icon_theme_lookup_key_symbolic

Constants

XTK_ICON_THEME_NAME_MAX

#define XTK_ICON_THEME_NAME_MAX 128

The longest theme name kept, with its NUL.

Functions

xtk_icon_theme_set_display

void xtk_icon_theme_set_display(Display *display, int screen);

Read the desktop's theme from XSETTINGS from now on, and follow it.

Optional: without it, "desktop" reads the GTK and KDE settings files only.

Parameter Description
display The program's display
screen Its screen

xtk_icon_theme_handle_event

int xtk_icon_theme_handle_event(const XEvent *event);

Give an X event to the theme, to follow the desktop's (XSETTINGS) changes.

Parameter Description
event Any event

Returns: 1 when the theme in use changed (the program redraws its icons), else 0

xtk_icon_theme_check

int xtk_icon_theme_check(void);

Notice a changed icons.conf, (for "desktop" without XSETTINGS) a changed GTK or KDE settings file, or a changed icon folder (a theme or an icon installed or removed; looked at every five seconds).

It looks at the files' times at most once a second, so it can be called from every turn of a main loop.

Returns: 1 when the theme in use changed or the icon folders did (icons are looked for again), else 0

xtk_icon_theme_set_program_choice

void xtk_icon_theme_set_program_choice(const char *choice);

Set the program's own choice, which wins over the shared one.

Parameter Description
choice "desktop", "builtin" or a theme's name; NULL or "" to follow the shared choice

xtk_icon_theme_get_program_choice

const char *xtk_icon_theme_get_program_choice(void);

The program's own choice, "" when it follows the shared one.

xtk_icon_theme_get_shared_choice

const char *xtk_icon_theme_get_shared_choice(void);

The choice all xtk programs share (~/.config/xtk/icons.conf): "desktop" when the file says none.

xtk_icon_theme_set_shared_choice

int xtk_icon_theme_set_shared_choice(const char *choice);

Set the choice all xtk programs share, written to ~/.config/xtk/icons.conf.

Other programs see it at their next xtk_icon_theme_check().

Parameter Description
choice "desktop", "builtin" or a theme's name

Returns: 1 when it was written

xtk_icon_theme_current

const char *xtk_icon_theme_current(void);

The theme in use, after the choices: its folder's name, or "" when the icons are xtk's own (builtin).

xtk_icon_theme_list

int xtk_icon_theme_list(
    char(*)names[XTK_ICON_THEME_NAME_MAX],
    char(*)labels[XTK_ICON_THEME_NAME_MAX],
    int capacity);

The installed themes that hold icons (not cursor themes), by name, for a choice in a settings dialog.

Parameter Description
names Receives each theme's folder name
labels Receives each theme's name for people (its Name=), or NULL
capacity How many fit

Returns: How many were written, sorted by label

xtk_icon_theme_lookup

int xtk_icon_theme_lookup(const char *name, int size, char *path, size_t path_size);

The file of an icon in the theme in use.

Parameter Description
name The icon's standard name, such as "document-save" or "text-x-generic"
size The size wanted, in pixels
path Receives the file's path
path_size Its size

Returns: 1 when there is one; 0 when the theme has none (or the choice is "builtin")

xtk_icon_theme_file_icons

int xtk_icon_theme_file_icons(const char *file_name, int is_folder, char(*)names[96]);

The icon names for a file, best first: its type's own icon, then its type's generic one.

Parameter Description
file_name The file's name (a path is fine: only its last part counts)
is_folder 1 for a folder ("folder")
names Receives up to four names

Returns: How many names were written (at least one: "application-x-generic" when nothing else fits)

xtk_icon_theme_lookup_list

int xtk_icon_theme_lookup_list(
    char(*)names[96],
    int count,
    int size,
    char *path,
    size_t path_size);

The best icon of several names, best first (a file's icons, xtk_icon_theme_file_icons()): every name in the theme in use before the theme it inherits (the spec's FindBestIcon), so they keep the theme's look. No name is cut short.

Parameter Description
names The names
count How many
size The size wanted, in pixels
path Receives the file's path
path_size Its size

Returns: 1 when there is one; 0 when not, or the choice is "builtin"

xtk_icon_theme_names_for_key

const char *const *xtk_icon_theme_names_for_key(const char *key);

The theme names for one of xtk's own icon keys (xtk_icon.h), best first: the Icon Naming Specification's name for its meaning, then the name the common themes use where the spec has none.

Parameter Description
key An icon key, such as "device-floppy"

Returns: A list of names ended by NULL, or NULL when the key has no standard counterpart (it stays xtk's own icon)

xtk_icon_theme_lookup_key

int xtk_icon_theme_lookup_key(const char *key, int size, char *path, size_t path_size);

The theme's icon for one of xtk's icon keys (xtk_icon.h), chosen so that it means what the key means.

Themes link one name to another icon (Papirus links "geany-close-all" to "window-close", Close's icon), and a theme without "document-save-all" would give Save's icon for Save All if the name were cut short. So each of the key's names (xtk_icon_theme_names_for_key()) is looked for exactly, theme by theme: the theme in use, the ones it inherits, then hicolor; every name in a theme before the next theme, so the theme's own look wins over an earlier name that only an inherited theme has. An icon that is a link to a name of a key it must not look like (Close All: not Close), or the same file as that key's icon, is passed over.

Parameter Description
key An icon key, such as "palette"
size The size wanted, in pixels
path Receives the file's path
path_size Its size

Returns: 1 when the theme has one; 0 when not (xtk draws its own), or the choice is "builtin"

xtk_icon_theme_lookup_key_symbolic

int xtk_icon_theme_lookup_key_symbolic(
    const char *key,
    int size,
    char *path,
    size_t path_size);

The theme's symbolic icon for one of xtk's icon keys: each of the key's names with "-symbolic" after it, looked for as xtk_icon_theme_lookup_key() does, through the theme in use, the ones it inherits and hicolor, then Adwaita's (GTK's fallback for symbolic icons). xtk_icon_draw_symbolic() draws it in one color.

Returns: 1 when found; 0 when not, or the choice is "builtin"