linuxglibgsettings

Generate Gsettings schema files in linux


I am using Gsettings schema and have com.test.gschema.xml file. Now some keys inside the schema are enum and thus I require com.test.enums.xml file.

Now I am using CMakeLists and thus can't use gsettings_ENUM_NAMESPACE and gsettings_ENUM_FILES. On searching I found out that we can make use of glib-mkenums utility but I tried generating the *.enums.xml file using it by providing it the .c and .h files having the enums declaration and definition but all it does is generate an empty file with no enums.


Solution

  • The glib-mkenums utility parses C files for enumeration definitions, and can be used to generate other files—typically, C sources for GType enumeration definitions to be used with GObject. The same utility can also be used for generating other file types, like the XML for GSettings schemas.

    You can look in the gsettings.m4 macro file shipped by GLib to get the glib-mkenums incantation that the macros generate for you when using Autotools:

    glib-mkenums \
      --comments '<!-- @comment@ -->' \
      --fhead "<schemalist>" \
      --vhead "  <@type@ id=\'$NAMESPACE.@EnumName@\'>" \
      --vprod "    <value nick=\'@valuenick@\' value=\'@valuenum@\'/>" \
      --vtail "  </@type@>" \
      --ftail "</schemalist>" \
      --output $OUTPUT_FILE \
      $INPUT_FILES
    

    Where $NAMESPACE is the namespace of your library—and would be the gsettings_ENUM_NAMESPACE value; $INPUT_FILES contains the list of files that define enumeration types to be used as settings values; and $OUTPUT_FILE is the XML file you're generating.

    I recommend reading the glib-mkenums manual page, which lists all the expansions and options.