Skip to content

Commit

Permalink
Merge pull request #21 from Airblader/feature-db-api
Browse files Browse the repository at this point in the history
Reworked the entire API.
  • Loading branch information
Airblader committed Mar 18, 2016
2 parents 706ddea + 857d184 commit b1ae5c7
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ xcbinclude_HEADERS = include/xcb_xrm.h

AM_CFLAGS = $(CWARNFLAGS)

libxcb_xrm_la_SOURCES = src/xrm.c src/entry.c src/util.c src/match.c
libxcb_xrm_la_SOURCES = src/database.c src/resource.c src/entry.c src/match.c src/util.c
libxcb_xrm_la_CPPFLAGS = -I$(srcdir)/include/ $(XCB_CFLAGS)
libxcb_xrm_la_LIBADD = $(XCB_LIBS) -lm
libxcb_xrm_la_LDFLAGS = -version-info 0:0:0 -no-undefined -export-symbols-regex '^xcb_xrm_'
Expand Down
39 changes: 39 additions & 0 deletions include/database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* vim:ts=4:sw=4:expandtab
*
* Copyright © 2016 Ingo Bürk
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors or their
* institutions shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization from the authors.
*
*/
#ifndef __DATABASE_H__
#define __DATABASE_H__

#include "externals.h"

#include "xcb_xrm.h"
#include "entry.h"

TAILQ_HEAD(xcb_xrm_database_t, xcb_xrm_entry_t);

#endif /* __DATABASE_H__ */
45 changes: 45 additions & 0 deletions include/externals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* vim:ts=4:sw=4:expandtab
*
* Copyright © 2016 Ingo Bürk
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors or their
* institutions shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization from the authors.
*
*/
#ifndef __EXTERNALS_H__
#define __EXTERNALS_H__

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <sys/queue.h>

#include <xcb/xcb.h>

#endif /* __EXTERNALS_H__ */
5 changes: 3 additions & 2 deletions include/match.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#ifndef __MATCH_H__
#define __MATCH_H__

#include "xrm.h"
#include "database.h"
#include "resource.h"
#include "entry.h"

/** Information about a matched component. */
Expand Down Expand Up @@ -61,7 +62,7 @@ typedef struct xcb_xrm_match_t {
* Finds the matching entry in the database given a full name / class query string.
*
*/
int xcb_xrm_match(xcb_xrm_context_t *ctx, xcb_xrm_entry_t *query_name, xcb_xrm_entry_t *query_class,
int xcb_xrm_match(xcb_xrm_database_t *database, xcb_xrm_entry_t *query_name, xcb_xrm_entry_t *query_class,
xcb_xrm_resource_t *resource);

#endif /* __MATCH_H__ */
20 changes: 4 additions & 16 deletions include/xrm.h → include/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,17 @@
* authorization from the authors.
*
*/
#ifndef __XRM_H__
#define __XRM_H__
#ifndef __RESOURCE_H__
#define __RESOURCE_H__

#include <sys/queue.h>
#include <assert.h>
#include <stdbool.h>
#include "externals.h"

#include "xcb_xrm.h"
#include "util.h"
#include "entry.h"

struct xcb_xrm_context_t {
xcb_connection_t *conn;
xcb_screen_t *screen;

/* The unprocessed resource manager string. */
char *resources;

TAILQ_HEAD(database_head, xcb_xrm_entry_t) entries;
};

struct xcb_xrm_resource_t {
char *value;
};

#endif /* __XRM_H__ */
#endif /* __RESOURCE_H__ */
2 changes: 1 addition & 1 deletion include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#ifndef __UTIL_H__
#define __UTIL_H__

#include <xcb/xcb.h>
#include "externals.h"

#define FREE(p) \
do { \
Expand Down
84 changes: 37 additions & 47 deletions include/xcb_xrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#ifndef __XCB_XRM_H__
#define __XCB_XRM_H__

#include <xcb/xcb.h>
#include "externals.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup xcb_xrm_context_t XCB XRM Functions
* @defgroup xcb_xrm_database_t XCB XRM Functions
*
* These functions are the xcb equivalent of the Xrm* function family in Xlib.
* They allow the parsing and matching of X resources as well as some utility
Expand All @@ -54,39 +54,35 @@ extern "C" {
*
* xcb_screen_t *screen = xcb_aux_get_screen(conn, screennr);
*
* xcb_xrm_context_t *ctx;
* if (xcb_xrm_context_new(conn, screen, &ctx) < 0)
* err(EXIT_FAILURE, "Could not initialize xcb-xrm.");
*
* if (xcb_xrm_database_from_resource_manager(ctx) < 0)
* err(EXIT_FAILURE, "Could not load the X resource database.");
* xcb_xrm_database_t *database = xcb_xrm_database_from_resource_manager(conn, screen);
*
* xcb_xrm_resource_t *resource;
* if (xcb_xrm_resource_get(ctx, "Xft.dpi", "Xft.dpi", &resource) < 0) {
* if (xcb_xrm_resource_get(database, "Xft.dpi", "Xft.dpi", &resource) < 0) {
* // Resource not found in database
* value = NULL;
* } else {
* value = xcb_xrm_resource_value(resource);
* xcb_xrm_resource_free(resource);
* value = strdup(xcb_xrm_resource_value(resource));
* }
*
* xcb_xrm_context_free(ctx);
* xcb_xrm_resource_free(resource);
* xcb_xrm_database_free(database);
* xcb_disconnect(conn);
* @endcode
*
* @{
*/

/**
* @struct xcb_xrm_context_t
* Describes a context for using this library.
*
* A context can be created using @ref xcb_xrm_context_new (). Afterwards, the
* resource database must be loaded, e.g., with @ref
* xcb_xrm_database_from_resource_manager (). After fetching resources, the
* context must be destroyed by calling @ref xcb_xrm_context_free ().
* @struct xcb_xrm_database_t
* Reference to a database.
*
* The database can be loaded in different ways, e.g., from the
* RESOURCE_MANAGER property by using @ref
* xcb_xrm_database_from_resource_manager (). All queries for a resource go
* against a specific database. A database must always be free'd by using @ref
* xcb_xrm_database_free ().
*/
typedef struct xcb_xrm_context_t xcb_xrm_context_t;
typedef struct xcb_xrm_database_t xcb_xrm_database_t;

/**
* @struct xcb_xrm_resource_t
Expand All @@ -101,48 +97,42 @@ typedef struct xcb_xrm_context_t xcb_xrm_context_t;
typedef struct xcb_xrm_resource_t xcb_xrm_resource_t;

/**
* Create a new @ref xcb_xrm_context_t.
* Loads the RESOURCE_MANAGER property and creates a database with its
* contents. If the database could not be created, thie function will return
* NULL.
*
* @param conn A working XCB connection. The connection must be kept open until
* after the context has been destroyed again.
* @param screen The xcb_screen_t to use.
* @param ctx A pointer to a xcb_xrm_context_t* which will be modified to
* refer to the newly created context.
* @return 0 on success, a negative error code otherwise.
* @param conn A working XCB connection.
* @param screen The xcb_screen_t* screen to use.
* @returns The database described by the RESOURCE_MANAGER property.
*
* @ingroup xcb_xrm_context_t
* @ingroup xcb_xrm_database_t
*/
int xcb_xrm_context_new(xcb_connection_t *conn, xcb_screen_t *screen, xcb_xrm_context_t **ctx);
xcb_xrm_database_t *xcb_xrm_database_from_resource_manager(xcb_connection_t *conn, xcb_screen_t *screen);

/**
* Destroys the @ref xcb_xrm_context_t.
* Creates a database from the given string.
* If the database could not be created, this function will return NULL.
*
* @param ctx The context to destroy.
*/
void xcb_xrm_context_free(xcb_xrm_context_t *ctx);

/**
* Loads the RESOURCE_MANAGER property and uses it as the database for this
* context.
* @param str The resource string.
* @returns The database described by the resource string.
*
* @param ctx The context to use.
* @return 0 on success, a negative error code otherwise.
* @ingroup xcb_xrm_database_t
*/
int xcb_xrm_database_from_resource_manager(xcb_xrm_context_t *ctx);
xcb_xrm_database_t *xcb_xrm_database_from_string(const char *str);

/**
* Uses the given string as the database for this context.
* Destroys the given database.
*
* @param ctx The context to use.
* @param str The resource string.
* @return 0 on success, a negative error code otherwise.
* @param database The database to destroy.
*
* @ingroup xcb_xrm_database_t
*/
int xcb_xrm_database_from_string(xcb_xrm_context_t *ctx, const char *str);
void xcb_xrm_database_free(xcb_xrm_database_t *database);

/**
* Fetches a resource from the database.
*
* @param ctx The context to use.
* @param database The database to use.
* @param res_name The fully qualified resource name.
* @param res_class The fully qualified resource class. Note that this argument
* may be left empty / NULL, but if given, it must contain the same number of
Expand All @@ -152,7 +142,7 @@ int xcb_xrm_database_from_string(xcb_xrm_context_t *ctx, const char *str);
* caller.
* @return 0 on success, a negative error code otherwise.
*/
int xcb_xrm_resource_get(xcb_xrm_context_t *ctx, const char *res_name, const char *res_class,
int xcb_xrm_resource_get(xcb_xrm_database_t *database, const char *res_name, const char *res_class,
xcb_xrm_resource_t **resource);

/**
Expand Down
Loading

0 comments on commit b1ae5c7

Please sign in to comment.