| 1 | /* |
| 2 | * vim:ts=4:sw=4:expandtab |
| 3 | * |
| 4 | * Copyright © 2016 Ingo Bürk |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Except as contained in this notice, the names of the authors or their |
| 24 | * institutions shall not be used in advertising or otherwise to promote the |
| 25 | * sale, use or other dealings in this Software without prior written |
| 26 | * authorization from the authors. |
| 27 | * |
| 28 | */ |
| 29 | #ifndef __XCB_XRM_H__ |
| 30 | #define __XCB_XRM_H__ |
| 31 | |
| 32 | #include <stdbool.h> |
| 33 | #include <xcb/xcb.h> |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | /** |
| 40 | * @defgroup xcb_xrm_database_t XCB XRM Functions |
| 41 | * |
| 42 | * These functions are the xcb equivalent of the Xrm* function family in Xlib. |
| 43 | * They allow the parsing and matching of X resources as well as some utility |
| 44 | * functions. |
| 45 | * |
| 46 | * Here is an example of how this library can be used to retrieve a |
| 47 | * user-configured resource: |
| 48 | * @code |
| 49 | * xcb_connection_t *conn = xcb_connect(NULL, &screennr); |
| 50 | * if (conn == NULL || xcb_connection_has_error(conn)) |
| 51 | * err(EXIT_FAILURE, "Could not connect to the X server."); |
| 52 | * |
| 53 | * xcb_xrm_database_t *database = xcb_xrm_database_from_default(conn); |
| 54 | * if (database == NULL) |
| 55 | * err(EXIT_FAILURE, "Could not open database"); |
| 56 | * |
| 57 | * char *value; |
| 58 | * if (xcb_xrm_resource_get_string(database, "Xft.dpi", NULL, &value) >= 0) { |
| 59 | * fprintf(stdout, "Xft.dpi: %s\n", value); |
| 60 | * free(value); |
| 61 | * } |
| 62 | * |
| 63 | * xcb_xrm_database_free(database); |
| 64 | * xcb_disconnect(conn); |
| 65 | * @endcode |
| 66 | * |
| 67 | * @{ |
| 68 | */ |
| 69 | |
| 70 | /** |
| 71 | * @struct xcb_xrm_database_t |
| 72 | * Reference to a database. |
| 73 | * |
| 74 | * The database can be loaded in different ways, e.g., from the |
| 75 | * RESOURCE_MANAGER property by using @ref |
| 76 | * xcb_xrm_database_from_resource_manager (). All queries for a resource go |
| 77 | * against a specific database. A database must always be free'd by using @ref |
| 78 | * xcb_xrm_database_free (). |
| 79 | * |
| 80 | * Note that a database is not thread-safe, i.e., multiple threads should not |
| 81 | * operate on the same database instance. This is especially true for write |
| 82 | * operations on the database. However, you can use this library in a |
| 83 | * multi-threaded application as long as the database is thread-local. |
| 84 | */ |
| 85 | typedef struct xcb_xrm_database_t xcb_xrm_database_t; |
| 86 | |
| 87 | /** |
| 88 | * Creates a database similarly to XGetDefault(). For typical applications, |
| 89 | * this is the recommended way to construct the resource database. |
| 90 | * |
| 91 | * The database is created as follows: |
| 92 | * - If the RESOURCE_MANAGER property exists on the root window of |
| 93 | * screen 0, the database is constructed from it using @ref |
| 94 | * xcb_xrm_database_from_resource_manager(). |
| 95 | * - Otherwise, if $HOME/.Xresources exists, the database is constructed from |
| 96 | * it using @ref xcb_xrm_database_from_file(). |
| 97 | * - Otherwise, if $HOME/.Xdefaults exists, the database is constructed from |
| 98 | * it using @ref xcb_xrm_database_from_file(). |
| 99 | * - If the environment variable XENVIRONMENT is set, the file specified by |
| 100 | * it is loaded using @ref xcb_xrm_database_from_file and then combined with |
| 101 | * the database using @ref xcb_xrm_database_combine() with override set to |
| 102 | * true. |
| 103 | * If XENVIRONMENT is not specified, the same is done with |
| 104 | * $HOME/.Xdefaults-$HOSTNAME, wherein $HOSTNAME is determined by |
| 105 | * gethostname(2). |
| 106 | * |
| 107 | * This represents the way XGetDefault() creates the database for the most |
| 108 | * part, but is not exactly the same. In particular, XGetDefault() does not |
| 109 | * consider $HOME/.Xresources. |
| 110 | * |
| 111 | * @param conn XCB connection. |
| 112 | * @returns The constructed database. Can return NULL, e.g., if the screen |
| 113 | * cannot be determined. |
| 114 | */ |
| 115 | xcb_xrm_database_t *xcb_xrm_database_from_default(xcb_connection_t *conn); |
| 116 | |
| 117 | /** |
| 118 | * Loads the RESOURCE_MANAGER property and creates a database with its |
| 119 | * contents. If the database could not be created, this function will return |
| 120 | * NULL. |
| 121 | * |
| 122 | * @param conn A working XCB connection. |
| 123 | * @param screen The xcb_screen_t* screen to use. |
| 124 | * @returns The database described by the RESOURCE_MANAGER property. |
| 125 | * |
| 126 | * @ingroup xcb_xrm_database_t |
| 127 | */ |
| 128 | xcb_xrm_database_t *xcb_xrm_database_from_resource_manager(xcb_connection_t *conn, xcb_screen_t *screen); |
| 129 | |
| 130 | /** |
| 131 | * Creates a database from the given string. |
| 132 | * If the database could not be created, this function will return NULL. |
| 133 | * |
| 134 | * @param str The resource string. |
| 135 | * @returns The database described by the resource string. |
| 136 | * |
| 137 | * @ingroup xcb_xrm_database_t |
| 138 | */ |
| 139 | xcb_xrm_database_t *xcb_xrm_database_from_string(const char *str); |
| 140 | |
| 141 | /** |
| 142 | * Creates a database from a given file. |
| 143 | * If the file cannot be found or opened, NULL is returned. |
| 144 | * |
| 145 | * @param filename Valid filename. |
| 146 | * @returns The database described by the file's contents. |
| 147 | */ |
| 148 | xcb_xrm_database_t *xcb_xrm_database_from_file(const char *filename); |
| 149 | |
| 150 | /** |
| 151 | * Returns a string representation of a database. |
| 152 | * The string is owned by the caller and must be free'd. |
| 153 | * |
| 154 | * @param database The database to return in string format. |
| 155 | * @returns A string representation of the specified database. |
| 156 | */ |
| 157 | char *xcb_xrm_database_to_string(xcb_xrm_database_t *database); |
| 158 | |
| 159 | /** |
| 160 | * Combines two databases. |
| 161 | * The entries from the source database are stored in the target database. If |
| 162 | * the same specifier already exists in the target database, the value will be |
| 163 | * overridden if override is set; otherwise, the value is discarded. |
| 164 | * If NULL is passed for target_db, a new and empty database will be created |
| 165 | * and returned in the pointer. |
| 166 | * |
| 167 | * @param source_db Source database. |
| 168 | * @param target_db Target database. |
| 169 | * @param override If true, entries from the source database override entries |
| 170 | * in the target database using the same resource specifier. |
| 171 | */ |
| 172 | void xcb_xrm_database_combine(xcb_xrm_database_t *source_db, xcb_xrm_database_t **target_db, bool override); |
| 173 | |
| 174 | /** |
| 175 | * Inserts a new resource into the database. |
| 176 | * If the resource already exists, the current value will be replaced. |
| 177 | * If NULL is passed for database, a new and empty database will be created and |
| 178 | * returned in the pointer. |
| 179 | * |
| 180 | * Note that this is not the equivalent of @ref |
| 181 | * xcb_xrm_database_put_resource_line when concatenating the resource name and |
| 182 | * value with a colon. For example, if the value starts with a leading space, |
| 183 | * this must (and will) be replaced with the special '\ ' sequence. |
| 184 | * |
| 185 | * @param database The database to modify. |
| 186 | * @param resource The fully qualified or partial resource specifier. |
| 187 | * @param value The value of the resource. |
| 188 | */ |
| 189 | void xcb_xrm_database_put_resource(xcb_xrm_database_t **database, const char *resource, const char *value); |
| 190 | |
| 191 | /** |
| 192 | * Inserts a new resource into the database. |
| 193 | * If the resource already exists, the current value will be replaced. |
| 194 | * If NULL is passed for database, a new and empty database will be created and |
| 195 | * returned in the pointer. |
| 196 | * |
| 197 | * @param database The database to modify. |
| 198 | * @param line The complete resource specification to insert. |
| 199 | */ |
| 200 | void xcb_xrm_database_put_resource_line(xcb_xrm_database_t **database, const char *line); |
| 201 | |
| 202 | /** |
| 203 | * Destroys the given database. |
| 204 | * |
| 205 | * @param database The database to destroy. |
| 206 | * |
| 207 | * @ingroup xcb_xrm_database_t |
| 208 | */ |
| 209 | void xcb_xrm_database_free(xcb_xrm_database_t *database); |
| 210 | |
| 211 | /** |
| 212 | * Find the string value of a resource. |
| 213 | * |
| 214 | * Note that the string is owned by the caller and must be free'd. |
| 215 | * |
| 216 | * @param database The database to query. |
| 217 | * @param res_name The fully qualified resource name string. |
| 218 | * @param res_class The fully qualified resource class string. This argument |
| 219 | * may be left empty / NULL, but if given, it must contain the same number of |
| 220 | * components as res_name. |
| 221 | * @param out Out parameter to which the value will be written. |
| 222 | * @returns 0 if the resource was found, a negative error code otherwise. |
| 223 | */ |
| 224 | int xcb_xrm_resource_get_string(xcb_xrm_database_t *database, |
| 225 | const char *res_name, const char *res_class, char **out); |
| 226 | |
| 227 | /** |
| 228 | * Find the long value of a resource. |
| 229 | * |
| 230 | * @param database The database to query. |
| 231 | * @param res_name The fully qualified resource name string. |
| 232 | * @param res_class The fully qualified resource class string. This argument |
| 233 | * may be left empty / NULL, but if given, it must contain the same number of |
| 234 | * components as res_name. |
| 235 | * @param out Out parameter to which the converted value will be written. |
| 236 | * @returns 0 if the resource was found and converted, -1 if the resource was |
| 237 | * found but could not be converted and -2 if the resource was not found. |
| 238 | */ |
| 239 | int xcb_xrm_resource_get_long(xcb_xrm_database_t *database, |
| 240 | const char *res_name, const char *res_class, long *out); |
| 241 | |
| 242 | /** |
| 243 | * Find the bool value of a resource. |
| 244 | * |
| 245 | * The conversion to a bool is done by applying the following steps in order: |
| 246 | * - If the value can be converted to a long, return the truthiness of the |
| 247 | * converted number. |
| 248 | * - If the value is one of "true", "on" or "yes" (case-insensitive), return |
| 249 | * true. |
| 250 | * - If the value is one of "false", "off" or "no" (case-insensitive), return |
| 251 | * false. |
| 252 | * |
| 253 | * @param database The database to query. |
| 254 | * @param res_name The fully qualified resource name string. |
| 255 | * @param res_class The fully qualified resource class string. This argument |
| 256 | * may be left empty / NULL, but if given, it must contain the same number of |
| 257 | * components as res_name. |
| 258 | * @param out Out parameter to which the converted value will be written. |
| 259 | * @returns 0 if the resource was found and converted, -1 if the resource was |
| 260 | * found but could not be converted and -2 if the resource was not found. |
| 261 | */ |
| 262 | int xcb_xrm_resource_get_bool(xcb_xrm_database_t *database, |
| 263 | const char *res_name, const char *res_class, bool *out); |
| 264 | |
| 265 | /** |
| 266 | * @} |
| 267 | */ |
| 268 | |
| 269 | #ifdef __cplusplus |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | #endif /* __XCB_XRM_H__ */ |