Skip to content

Commit

Permalink
Merge pull request #5 from jusa/dev
Browse files Browse the repository at this point in the history
Update libngf client library.
  • Loading branch information
jusa committed Dec 1, 2014
2 parents 9054c2c + 9c83783 commit fb581e8
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 122 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -5,7 +5,7 @@ AC_CONFIG_MACRO_DIR([m4])
NGF_LIBRARY_NAME=libngf

NGF_MAJOR_VERSION=0
NGF_MINOR_VERSION=1
NGF_MINOR_VERSION=24
NGF_MICRO_VERSION=0

NGF_API_VERSION=1.0
Expand Down
28 changes: 17 additions & 11 deletions examples/ngf-client.c
Expand Up @@ -19,6 +19,8 @@ typedef struct _TestClient

DBusConnection *connection;
NgfClient *client;
gboolean quit_after_event;
int return_value;
} TestClient;

static char*
Expand Down Expand Up @@ -291,30 +293,26 @@ static void
client_callback_cb (NgfClient *client, uint32_t event_id, NgfEventState state, void *userdata)
{
(void) client;
(void) userdata;
TestClient *c = (TestClient*) userdata;

switch (state) {
case NGF_EVENT_FAILED:
g_print ("\nFailed (event_id=%d)\n", event_id);
if (c->quit_after_event)
g_main_loop_quit (c->loop);
c->return_value = 1;
break;
case NGF_EVENT_COMPLETED:
g_print ("\nCompleted (event_id=%d)\n", event_id);
if (c->quit_after_event)
g_main_loop_quit (c->loop);
break;
case NGF_EVENT_PLAYING:
g_print ("\nPlaying (event_id=%d)\n", event_id);
break;
case NGF_EVENT_PAUSED:
g_print ("\nPaused (event_id=%d)\n", event_id);
break;
case NGF_EVENT_BUSY:
g_print ("\nBusy (event_id=%d)\n", event_id);
break;
case NGF_EVENT_LONG:
g_print ("\nLong (event_id=%d)\n", event_id);
break;
case NGF_EVENT_SHORT:
g_print ("\nShort (event_id=%d)\n", event_id);
break;

default:
g_print ("\nUnknown (event_id=%d)\n", event_id);
Expand Down Expand Up @@ -386,15 +384,23 @@ int
main (int argc, char *argv[])
{
TestClient *c = NULL;
int ret = 0;

c = g_new0 (TestClient, 1);
if (!application_create (c))
return 1;

if (argc > 1) {
int i;
for (i = 1; i < argc; i++)
parse_input (c, argv[i], strlen(argv[i]));
c->quit_after_event = TRUE;
}
application_run (c);
application_destroy (c);

ret = c->return_value;
g_free (c);

return 0;
return ret;
}
8 changes: 0 additions & 8 deletions libngf/client.h
Expand Up @@ -51,14 +51,6 @@ typedef enum _NgfEventState
/** Event is in paused state when pause is called. */
NGF_EVENT_PAUSED = 3,

/** Event is busy, because there is a more higher priority event playing. */
NGF_EVENT_BUSY = (1 << 2),

/** Event will be played using a long tone */
NGF_EVENT_LONG = (1 << 3),

/** Event will be played using a short tone */
NGF_EVENT_SHORT = (1 << 4)
} NgfEventState;

/** Internal client structure. */
Expand Down
127 changes: 98 additions & 29 deletions libngf/proplist.c
Expand Up @@ -35,6 +35,11 @@
#define MAX_KEY_LENGTH 32
#define MAX_VALUE_LENGTH 512

#define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
#define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
#define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
#define INT32_TO_PTR(i) ((void*) ((intptr_t) (i)))

typedef struct _PropEntry PropEntry;

struct _PropEntry
Expand Down Expand Up @@ -65,6 +70,44 @@ ngf_proplist_new ()
return proplist;
}

static void copy_cb (const char *key, const void *value, NgfProplistType type, void *userdata)
{
NgfProplist *list = (NgfProplist*) userdata;

switch (type) {
case NGF_PROPLIST_VALUE_TYPE_STRING:
ngf_proplist_sets (list, key, (const char *) value);
break;

case NGF_PROPLIST_VALUE_TYPE_INTEGER:
ngf_proplist_set_as_integer (list, key, *(const int32_t *) value);
break;

case NGF_PROPLIST_VALUE_TYPE_UNSIGNED:
ngf_proplist_set_as_unsigned (list, key, *(const uint32_t *) value);
break;

case NGF_PROPLIST_VALUE_TYPE_BOOLEAN:
ngf_proplist_set_as_boolean (list, key, *(const int32_t *) value);
break;

default:
break;
}
}

NgfProplist*
ngf_proplist_copy (NgfProplist *orig)
{
NgfProplist *list = NULL;
if (!(list = ngf_proplist_new ()))
return NULL;

ngf_proplist_foreach_extended (orig, copy_cb, list);

return list;
}

static void
_free_item (PropEntry *entry, void *userdata)
{
Expand All @@ -75,7 +118,7 @@ _free_item (PropEntry *entry, void *userdata)

if (entry->key)
free (entry->key);
if (entry->value)
if (entry->type == NGF_PROPLIST_VALUE_TYPE_STRING && entry->value)
free (entry->value);
free (entry);
}
Expand Down Expand Up @@ -146,7 +189,6 @@ ngf_proplist_set_as_integer (NgfProplist *proplist,
int32_t value)
{
PropEntry *item = NULL;
int32_t *data;

if (proplist == NULL || key == NULL)
goto error;
Expand All @@ -157,10 +199,8 @@ ngf_proplist_set_as_integer (NgfProplist *proplist,

if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(int32_t))) == NULL)
goto error;
*data = value;
item->value = data;

item->value = INT32_TO_PTR (value);
item->type = NGF_PROPLIST_VALUE_TYPE_INTEGER;
item->next = NULL;

Expand All @@ -170,8 +210,6 @@ ngf_proplist_set_as_integer (NgfProplist *proplist,
error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
Expand All @@ -183,7 +221,6 @@ ngf_proplist_set_as_unsigned (NgfProplist *proplist,
uint32_t value)
{
PropEntry *item = NULL;
uint32_t *data;

if (proplist == NULL || key == NULL)
goto error;
Expand All @@ -194,10 +231,7 @@ ngf_proplist_set_as_unsigned (NgfProplist *proplist,

if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(uint32_t))) == NULL)
goto error;
*data = value;
item->value = data;
item->value = UINT32_TO_PTR (value);
item->type = NGF_PROPLIST_VALUE_TYPE_UNSIGNED;
item->next = NULL;

Expand All @@ -207,8 +241,6 @@ ngf_proplist_set_as_unsigned (NgfProplist *proplist,
error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
Expand All @@ -227,7 +259,7 @@ ngf_proplist_get_as_integer (NgfProplist *proplist,
for (iter = proplist->entries; iter; iter = iter->next) {
if (strncmp (iter->key, key, (size_t) MAX_KEY_LENGTH) == 0
&& iter->type == NGF_PROPLIST_VALUE_TYPE_INTEGER) {
*integer_value = *(const int32_t*) iter->value;
*integer_value = PTR_TO_INT32 (iter->value);
return 1;
}
}
Expand All @@ -248,7 +280,7 @@ ngf_proplist_get_as_unsigned (NgfProplist *proplist,
for (iter = proplist->entries; iter; iter = iter->next) {
if (strncmp (iter->key, key, (size_t) MAX_KEY_LENGTH) == 0
&& iter->type == NGF_PROPLIST_VALUE_TYPE_UNSIGNED) {
*unsigned_value = *(const uint32_t*) iter->value;
*unsigned_value = PTR_TO_UINT32 (iter->value);
return 1;
}
}
Expand All @@ -262,7 +294,6 @@ ngf_proplist_set_as_boolean (NgfProplist *proplist,
int value)
{
PropEntry *item = NULL;
int *data;

if (proplist == NULL || key == NULL)
goto error;
Expand All @@ -273,10 +304,7 @@ ngf_proplist_set_as_boolean (NgfProplist *proplist,

if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(int))) == NULL)
goto error;
*data = value > 0 ? 1 : 0;
item->value = data;
item->value = INT32_TO_PTR (value > 0 ? 1 : 0);
item->type = NGF_PROPLIST_VALUE_TYPE_BOOLEAN;
item->next = NULL;

Expand All @@ -286,8 +314,6 @@ ngf_proplist_set_as_boolean (NgfProplist *proplist,
error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
Expand All @@ -306,7 +332,7 @@ ngf_proplist_get_as_boolean (NgfProplist *proplist,
for (iter = proplist->entries; iter; iter = iter->next) {
if (strncmp (iter->key, key, (size_t) MAX_KEY_LENGTH) == 0
&& iter->type == NGF_PROPLIST_VALUE_TYPE_BOOLEAN) {
*boolean_value = *(const int*) iter->value;
*boolean_value = PTR_TO_INT32 (iter->value);
return 1;
}
}
Expand Down Expand Up @@ -388,12 +414,34 @@ ngf_proplist_foreach (NgfProplist *proplist,
void *userdata)
{
PropEntry *iter = NULL;
uint32_t uval;
int32_t ival;

if (proplist == NULL || callback == NULL)
return;

for (iter = proplist->entries; iter; iter = iter->next)
callback (iter->key, iter->value, userdata);
for (iter = proplist->entries; iter; iter = iter->next) {
switch (iter->type) {
case NGF_PROPLIST_VALUE_TYPE_STRING:
callback (iter->key, iter->value, userdata);
break;

case NGF_PROPLIST_VALUE_TYPE_INTEGER:
case NGF_PROPLIST_VALUE_TYPE_BOOLEAN:
ival = PTR_TO_INT32 (iter->value);
callback (iter->key, &ival, userdata);
break;

case NGF_PROPLIST_VALUE_TYPE_UNSIGNED:
uval = PTR_TO_UINT32 (iter->value);
callback (iter->key, &uval, userdata);
break;

default:
break;
}

}
}

void
Expand All @@ -402,12 +450,33 @@ ngf_proplist_foreach_extended (NgfProplist *proplist,
void *userdata)
{
PropEntry *iter = NULL;
uint32_t uval;
int32_t ival;

if (proplist == NULL || callback == NULL)
return;

for (iter = proplist->entries; iter; iter = iter->next)
callback (iter->key, iter->value, iter->type, userdata);
for (iter = proplist->entries; iter; iter = iter->next) {
switch (iter->type) {
case NGF_PROPLIST_VALUE_TYPE_STRING:
callback (iter->key, iter->value, iter->type, userdata);
break;

case NGF_PROPLIST_VALUE_TYPE_INTEGER:
case NGF_PROPLIST_VALUE_TYPE_BOOLEAN:
ival = PTR_TO_INT32 (iter->value);
callback (iter->key, &ival, iter->type, userdata);
break;

case NGF_PROPLIST_VALUE_TYPE_UNSIGNED:
uval = PTR_TO_UINT32 (iter->value);
callback (iter->key, &uval, iter->type, userdata);
break;

default:
break;
}
}
}

const char**
Expand Down
7 changes: 7 additions & 0 deletions libngf/proplist.h
Expand Up @@ -51,6 +51,13 @@ typedef void (*NgfProplistExtendedCallback) (const char *key, const void *val

NgfProplist* ngf_proplist_new ();

/**
* Create an identical copy of other proplist.
* @param orig Original NgfProplist a copy is made from.
* @return Copy of orig NgfProplist or NULL if no memory.
*/
NgfProplist* ngf_proplist_copy (NgfProplist *orig);

/**
* Free property list.
* @param proplist NgfProplist, if NULL nothing done.
Expand Down
4 changes: 2 additions & 2 deletions libngf0.pc.in
@@ -1,12 +1,12 @@
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/libngf-1.0
includedir=${prefix}/include/libngf-@NGF_API_VERSION@
toolsdir=${exec_prefix}/bin

Name: libngf
Description: non-graphical feedback client library
Version: 0.1
Version: @VERSION@
Libs: -L${libdir} -lngf0 -lm
Cflags: -I${includedir}

Expand Down

0 comments on commit fb581e8

Please sign in to comment.