Updated the hctl interface (the more independent way).

This commit is contained in:
Jaroslav Kysela 2001-02-09 14:50:05 +00:00
parent 3e561540d5
commit 38033b49dd
10 changed files with 250 additions and 177 deletions

View file

@ -196,33 +196,39 @@ void snd_ctl_elem_list_free_space(snd_ctl_elem_list_t *obj);
typedef struct _snd_hctl_elem snd_hctl_elem_t; typedef struct _snd_hctl_elem snd_hctl_elem_t;
typedef struct _snd_hctl snd_hctl_t;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef int (*snd_hctl_compare_t)(const snd_hctl_elem_t *e1, typedef int (*snd_hctl_compare_t)(const snd_hctl_elem_t *e1,
const snd_hctl_elem_t *e2); const snd_hctl_elem_t *e2);
typedef int (*snd_hctl_callback_t)(snd_ctl_t *ctl, typedef int (*snd_hctl_callback_t)(snd_hctl_t *hctl,
snd_ctl_event_type_t event, snd_ctl_event_type_t event,
snd_hctl_elem_t *elem); snd_hctl_elem_t *elem);
typedef int (*snd_hctl_elem_callback_t)(snd_hctl_elem_t *elem, typedef int (*snd_hctl_elem_callback_t)(snd_hctl_elem_t *elem,
snd_ctl_event_type_t event); snd_ctl_event_type_t event);
int snd_hctl_build(snd_ctl_t *ctl); int snd_hctl_open(snd_hctl_t **hctl, char *name);
snd_hctl_elem_t *snd_hctl_first_elem(snd_ctl_t *ctl); int snd_hctl_close(snd_hctl_t *hctl);
snd_hctl_elem_t *snd_hctl_last_elem(snd_ctl_t *ctl); int snd_hctl_nonblock(snd_hctl_t *hctl, int nonblock);
int snd_hctl_async(snd_hctl_t *hctl, int sig, pid_t pid);
int snd_hctl_poll_descriptor(snd_hctl_t *hctl);
snd_hctl_elem_t *snd_hctl_first_elem(snd_hctl_t *hctl);
snd_hctl_elem_t *snd_hctl_last_elem(snd_hctl_t *hctl);
snd_hctl_elem_t *snd_hctl_elem_next(snd_hctl_elem_t *elem); snd_hctl_elem_t *snd_hctl_elem_next(snd_hctl_elem_t *elem);
snd_hctl_elem_t *snd_hctl_elem_prev(snd_hctl_elem_t *elem); snd_hctl_elem_t *snd_hctl_elem_prev(snd_hctl_elem_t *elem);
int snd_hctl_elem_info(snd_hctl_elem_t *elem, snd_ctl_elem_info_t * info); int snd_hctl_elem_info(snd_hctl_elem_t *elem, snd_ctl_elem_info_t * info);
int snd_hctl_elem_read(snd_hctl_elem_t *elem, snd_ctl_elem_t * value); int snd_hctl_elem_read(snd_hctl_elem_t *elem, snd_ctl_elem_t * value);
int snd_hctl_elem_write(snd_hctl_elem_t *elem, snd_ctl_elem_t * value); int snd_hctl_elem_write(snd_hctl_elem_t *elem, snd_ctl_elem_t * value);
unsigned int snd_hctl_get_count(snd_ctl_t *ctl); unsigned int snd_hctl_get_count(snd_hctl_t *ctl);
snd_hctl_elem_t *snd_hctl_find_elem(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id); snd_hctl_elem_t *snd_hctl_find_elem(snd_hctl_t *ctl, const snd_ctl_elem_id_t *id);
void snd_hctl_set_callback(snd_ctl_t *ctl, snd_hctl_callback_t callback); void snd_hctl_set_callback(snd_hctl_t *ctl, snd_hctl_callback_t callback);
void snd_hctl_set_callback_private(snd_ctl_t *ctl, void *private); void snd_hctl_set_callback_private(snd_hctl_t *ctl, void *private);
void *snd_hctl_get_callback_private(snd_ctl_t *ctl); void *snd_hctl_get_callback_private(snd_hctl_t *ctl);
int snd_hctl_event(snd_ctl_t *ctl, snd_ctl_event_t *event); int snd_hctl_event(snd_hctl_t *ctl, snd_ctl_event_t *event);
int snd_hctl_events(snd_ctl_t *ctl); int snd_hctl_events(snd_hctl_t *ctl);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -39,7 +39,6 @@ int snd_ctl_close(snd_ctl_t *ctl)
int res; int res;
assert(ctl); assert(ctl);
res = ctl->ops->close(ctl); res = ctl->ops->close(ctl);
snd_hctl_free(ctl);
free(ctl); free(ctl);
return res; return res;
} }

View file

@ -289,7 +289,6 @@ int snd_ctl_hw_open(snd_ctl_t **handle, const char *name, int card)
ctl->type = SND_CTL_TYPE_HW; ctl->type = SND_CTL_TYPE_HW;
ctl->ops = &snd_ctl_hw_ops; ctl->ops = &snd_ctl_hw_ops;
ctl->private = hw; ctl->private = hw;
INIT_LIST_HEAD(&ctl->hlist);
*handle = ctl; *handle = ctl;
return 0; return 0;
} }

View file

@ -50,13 +50,6 @@ struct _snd_ctl {
snd_ctl_ops_t *ops; snd_ctl_ops_t *ops;
void *private; void *private;
int nonblock; int nonblock;
struct list_head hlist; /* list of all controls */
unsigned int halloc;
unsigned int hcount;
snd_hctl_elem_t **helems;
snd_hctl_compare_t hcompare;
snd_hctl_callback_t callback;
void *callback_private;
}; };
struct _snd_hctl_elem { struct _snd_hctl_elem {
@ -66,13 +59,20 @@ struct _snd_hctl_elem {
snd_hctl_elem_callback_t callback; snd_hctl_elem_callback_t callback;
void *callback_private; void *callback_private;
/* links */ /* links */
snd_ctl_t *ctl; /* associated handle */ snd_hctl_t *hctl; /* associated handle */
};
struct _snd_hctl {
snd_ctl_t *ctl;
struct list_head hlist; /* list of all controls */
unsigned int halloc;
unsigned int hcount;
snd_hctl_elem_t **helems;
snd_hctl_compare_t hcompare;
snd_hctl_callback_t callback;
void *callback_private;
}; };
int snd_ctl_hw_open(snd_ctl_t **handle, const char *name, int card); int snd_ctl_hw_open(snd_ctl_t **handle, const char *name, int card);
int snd_ctl_shm_open(snd_ctl_t **handlep, const char *name, const char *socket, const char *sname); int snd_ctl_shm_open(snd_ctl_t **handlep, const char *name, const char *socket, const char *sname);
int snd_hctl_free(snd_ctl_t *ctl);
int snd_hctl_compare_default(const snd_hctl_elem_t *c1,
const snd_hctl_elem_t *c2);

View file

@ -475,7 +475,6 @@ int snd_ctl_shm_open(snd_ctl_t **handlep, const char *name, const char *socket,
ctl->type = SND_CTL_TYPE_SHM; ctl->type = SND_CTL_TYPE_SHM;
ctl->ops = &snd_ctl_shm_ops; ctl->ops = &snd_ctl_shm_ops;
ctl->private = shm; ctl->private = shm;
INIT_LIST_HEAD(&ctl->hlist);
*handlep = ctl; *handlep = ctl;
return 0; return 0;

View file

@ -29,18 +29,77 @@
#define __USE_GNU #define __USE_GNU
#include "control_local.h" #include "control_local.h"
static int _snd_hctl_find_elem(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id, int *dir) static int snd_hctl_build(snd_hctl_t *hctl);
static int snd_hctl_free(snd_hctl_t *hctl);
static int snd_hctl_compare_default(const snd_hctl_elem_t *c1,
const snd_hctl_elem_t *c2);
int snd_hctl_open(snd_hctl_t **hctlp, char *name)
{
snd_hctl_t *hctl;
snd_ctl_t *ctl;
int err;
assert(hctlp);
*hctlp = NULL;
if ((err = snd_ctl_open(&ctl, name)) < 0)
return err;
if ((hctl = (snd_hctl_t *)calloc(1, sizeof(snd_hctl_t))) == NULL) {
snd_ctl_close(ctl);
return -ENOMEM;
}
INIT_LIST_HEAD(&hctl->hlist);
hctl->ctl = ctl;
if ((err = snd_hctl_build(hctl)) < 0) {
snd_hctl_close(hctl);
return err;
}
*hctlp = hctl;
return 0;
}
int snd_hctl_close(snd_hctl_t *hctl)
{
int err;
assert(hctl);
assert(hctl->ctl);
err = snd_ctl_close(hctl->ctl);
snd_hctl_free(hctl);
free(hctl);
return err;
}
int snd_hctl_nonblock(snd_hctl_t *hctl, int nonblock)
{
assert(hctl && hctl->ctl);
return snd_ctl_nonblock(hctl->ctl, nonblock);
}
int snd_hctl_async(snd_hctl_t *hctl, int sig, pid_t pid)
{
assert(hctl && hctl->ctl);
return snd_ctl_async(hctl->ctl, sig, pid);
}
int snd_hctl_poll_descriptor(snd_hctl_t *hctl)
{
assert(hctl && hctl->ctl);
return snd_ctl_poll_descriptor(hctl->ctl);
}
static int _snd_hctl_find_elem(snd_hctl_t *hctl, const snd_ctl_elem_id_t *id, int *dir)
{ {
unsigned int l, u; unsigned int l, u;
int c = 0; int c = 0;
int idx = -1; int idx = -1;
assert(ctl && id); assert(hctl && id);
assert(ctl->hcompare); assert(hctl->hcompare);
l = 0; l = 0;
u = ctl->hcount; u = hctl->hcount;
while (l < u) { while (l < u) {
idx = (l + u) / 2; idx = (l + u) / 2;
c = ctl->hcompare((snd_hctl_elem_t *) id, ctl->helems[idx]); c = hctl->hcompare((snd_hctl_elem_t *) id, hctl->helems[idx]);
if (c < 0) if (c < 0)
u = idx; u = idx;
else if (c > 0) else if (c > 0)
@ -52,89 +111,88 @@ static int _snd_hctl_find_elem(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id, int
return idx; return idx;
} }
static int snd_hctl_elem_add(snd_ctl_t *ctl, snd_hctl_elem_t *elem) static int snd_hctl_elem_add(snd_hctl_t *hctl, snd_hctl_elem_t *elem)
{ {
int dir; int dir;
int idx; int idx;
if (ctl->hcount == ctl->halloc) { if (hctl->hcount == hctl->halloc) {
snd_hctl_elem_t **h; snd_hctl_elem_t **h;
ctl->halloc += 32; hctl->halloc += 32;
h = realloc(ctl->helems, sizeof(*h) * ctl->halloc); h = realloc(hctl->helems, sizeof(*h) * hctl->halloc);
if (!h) if (!h)
return -ENOMEM; return -ENOMEM;
ctl->helems = h; hctl->helems = h;
} }
if (ctl->hcount == 0) { if (hctl->hcount == 0) {
list_add_tail(&elem->list, &ctl->hlist); list_add_tail(&elem->list, &hctl->hlist);
ctl->helems[0] = elem; hctl->helems[0] = elem;
} else { } else {
idx = _snd_hctl_find_elem(ctl, &elem->id, &dir); idx = _snd_hctl_find_elem(hctl, &elem->id, &dir);
assert(dir != 0); assert(dir != 0);
if (dir > 0) { if (dir > 0) {
list_add(&elem->list, &ctl->helems[idx]->list); list_add(&elem->list, &hctl->helems[idx]->list);
} else { } else {
list_add_tail(&elem->list, &ctl->helems[idx]->list); list_add_tail(&elem->list, &hctl->helems[idx]->list);
idx++; idx++;
} }
memmove(ctl->helems + idx + 1, memmove(hctl->helems + idx + 1,
ctl->helems + idx, hctl->helems + idx,
ctl->hcount - idx); hctl->hcount - idx);
} }
ctl->hcount++; hctl->hcount++;
if (ctl->callback) { if (hctl->callback) {
int res = ctl->callback(ctl, SND_CTL_EVENT_ADD, elem); int res = hctl->callback(hctl, SND_CTL_EVENT_ADD, elem);
if (res < 0) if (res < 0)
return res; return res;
} }
return 0; return 0;
} }
static void snd_hctl_elem_remove(snd_hctl_t *hctl, unsigned int idx)
static void snd_hctl_elem_remove(snd_ctl_t *ctl, unsigned int idx)
{ {
snd_hctl_elem_t *elem = ctl->helems[idx]; snd_hctl_elem_t *elem = hctl->helems[idx];
unsigned int m; unsigned int m;
if (elem->callback) if (elem->callback)
elem->callback(elem, SND_CTL_EVENT_REMOVE); elem->callback(elem, SND_CTL_EVENT_REMOVE);
list_del(&elem->list); list_del(&elem->list);
free(elem); free(elem);
ctl->hcount--; hctl->hcount--;
m = ctl->hcount - idx; m = hctl->hcount - idx;
if (m > 0) if (m > 0)
memmove(ctl->helems + idx, ctl->helems + idx + 1, m); memmove(hctl->helems + idx, hctl->helems + idx + 1, m);
} }
int snd_hctl_free(snd_ctl_t *ctl) static int snd_hctl_free(snd_hctl_t *hctl)
{ {
while (ctl->hcount > 0) while (hctl->hcount > 0)
snd_hctl_elem_remove(ctl, ctl->hcount - 1); snd_hctl_elem_remove(hctl, hctl->hcount - 1);
free(ctl->helems); free(hctl->helems);
ctl->helems = 0; hctl->helems = 0;
ctl->halloc = 0; hctl->halloc = 0;
INIT_LIST_HEAD(&ctl->hlist); INIT_LIST_HEAD(&hctl->hlist);
return 0; return 0;
} }
static void snd_hctl_sort(snd_ctl_t *ctl) static void snd_hctl_sort(snd_hctl_t *hctl)
{ {
unsigned int k; unsigned int k;
int compar(const void *a, const void *b) { int compar(const void *a, const void *b) {
return ctl->hcompare(*(const snd_hctl_elem_t **) a, return hctl->hcompare(*(const snd_hctl_elem_t **) a,
*(const snd_hctl_elem_t **) b); *(const snd_hctl_elem_t **) b);
} }
assert(ctl); assert(hctl);
assert(ctl->hcompare); assert(hctl->hcompare);
INIT_LIST_HEAD(&ctl->hlist); INIT_LIST_HEAD(&hctl->hlist);
qsort(ctl->helems, ctl->hcount, sizeof(*ctl->helems), compar); qsort(hctl->helems, hctl->hcount, sizeof(*hctl->helems), compar);
for (k = 0; k < ctl->hcount; k++) for (k = 0; k < hctl->hcount; k++)
list_add_tail(&ctl->helems[k]->list, &ctl->hlist); list_add_tail(&hctl->helems[k]->list, &hctl->hlist);
} }
void snd_hctl_set_compare(snd_ctl_t *ctl, snd_hctl_compare_t hsort) void snd_hctl_set_compare(snd_hctl_t *hctl, snd_hctl_compare_t hsort)
{ {
assert(ctl); assert(hctl);
ctl->hcompare = hsort; hctl->hcompare = hsort == NULL ? snd_hctl_compare_default : hsort;
snd_hctl_sort(ctl); snd_hctl_sort(hctl);
} }
#define NOT_FOUND 1000000000 #define NOT_FOUND 1000000000
@ -225,7 +283,7 @@ int snd_hctl_compare_fast(const snd_hctl_elem_t *c1,
return c1->id.numid - c2->id.numid; return c1->id.numid - c2->id.numid;
} }
int snd_hctl_compare_default(const snd_hctl_elem_t *c1, static int snd_hctl_compare_default(const snd_hctl_elem_t *c1,
const snd_hctl_elem_t *c2) const snd_hctl_elem_t *c2)
{ {
int res, p1, p2; int res, p1, p2;
@ -246,26 +304,26 @@ int snd_hctl_compare_default(const snd_hctl_elem_t *c1,
return d; return d;
} }
snd_hctl_elem_t *snd_hctl_first_elem(snd_ctl_t *ctl) snd_hctl_elem_t *snd_hctl_first_elem(snd_hctl_t *hctl)
{ {
assert(ctl); assert(hctl);
if (list_empty(&ctl->hlist)) if (list_empty(&hctl->hlist))
return NULL; return NULL;
return list_entry(ctl->hlist.next, snd_hctl_elem_t, list); return list_entry(hctl->hlist.next, snd_hctl_elem_t, list);
} }
snd_hctl_elem_t *snd_hctl_last_elem(snd_ctl_t *ctl) snd_hctl_elem_t *snd_hctl_last_elem(snd_hctl_t *hctl)
{ {
assert(ctl); assert(hctl);
if (list_empty(&ctl->hlist)) if (list_empty(&hctl->hlist))
return NULL; return NULL;
return list_entry(ctl->hlist.prev, snd_hctl_elem_t, list); return list_entry(hctl->hlist.prev, snd_hctl_elem_t, list);
} }
snd_hctl_elem_t *snd_hctl_elem_next(snd_hctl_elem_t *elem) snd_hctl_elem_t *snd_hctl_elem_next(snd_hctl_elem_t *elem)
{ {
assert(elem); assert(elem);
if (elem->list.next == &elem->ctl->hlist) if (elem->list.next == &elem->hctl->hlist)
return NULL; return NULL;
return list_entry(elem->list.next, snd_hctl_elem_t, list); return list_entry(elem->list.next, snd_hctl_elem_t, list);
} }
@ -273,44 +331,45 @@ snd_hctl_elem_t *snd_hctl_elem_next(snd_hctl_elem_t *elem)
snd_hctl_elem_t *snd_hctl_elem_prev(snd_hctl_elem_t *elem) snd_hctl_elem_t *snd_hctl_elem_prev(snd_hctl_elem_t *elem)
{ {
assert(elem); assert(elem);
if (elem->list.prev == &elem->ctl->hlist) if (elem->list.prev == &elem->hctl->hlist)
return NULL; return NULL;
return list_entry(elem->list.prev, snd_hctl_elem_t, list); return list_entry(elem->list.prev, snd_hctl_elem_t, list);
} }
snd_hctl_elem_t *snd_hctl_find_elem(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id) snd_hctl_elem_t *snd_hctl_find_elem(snd_hctl_t *hctl, const snd_ctl_elem_id_t *id)
{ {
int dir; int dir;
int res = _snd_hctl_find_elem(ctl, id, &dir); int res = _snd_hctl_find_elem(hctl, id, &dir);
if (res < 0 || dir != 0) if (res < 0 || dir != 0)
return NULL; return NULL;
return ctl->helems[res]; return hctl->helems[res];
} }
int snd_hctl_build(snd_ctl_t *ctl) static int snd_hctl_build(snd_hctl_t *hctl)
{ {
snd_ctl_elem_list_t list; snd_ctl_elem_list_t list;
int err = 0; int err = 0;
unsigned int idx; unsigned int idx;
assert(ctl); assert(hctl);
assert(ctl->hcount == 0); assert(hctl->ctl);
assert(list_empty(&ctl->hlist)); assert(hctl->hcount == 0);
assert(list_empty(&hctl->hlist));
memset(&list, 0, sizeof(list)); memset(&list, 0, sizeof(list));
if ((err = snd_ctl_elem_list(ctl, &list)) < 0) if ((err = snd_ctl_elem_list(hctl->ctl, &list)) < 0)
goto _end; goto _end;
while (list.count != list.used) { while (list.count != list.used) {
err = snd_ctl_elem_list_alloc_space(&list, list.count); err = snd_ctl_elem_list_alloc_space(&list, list.count);
if (err < 0) if (err < 0)
goto _end; goto _end;
if ((err = snd_ctl_elem_list(ctl, &list)) < 0) if ((err = snd_ctl_elem_list(hctl->ctl, &list)) < 0)
goto _end; goto _end;
} }
if (ctl->halloc < list.count) { if (hctl->halloc < list.count) {
ctl->halloc = list.count; hctl->halloc = list.count;
free(ctl->helems); free(hctl->helems);
ctl->helems = malloc(ctl->halloc * sizeof(*ctl->helems)); hctl->helems = malloc(hctl->halloc * sizeof(*hctl->helems));
if (!ctl->helems) { if (!hctl->helems) {
err = -ENOMEM; err = -ENOMEM;
goto _end; goto _end;
} }
@ -319,23 +378,23 @@ int snd_hctl_build(snd_ctl_t *ctl)
snd_hctl_elem_t *elem; snd_hctl_elem_t *elem;
elem = calloc(1, sizeof(snd_hctl_elem_t)); elem = calloc(1, sizeof(snd_hctl_elem_t));
if (elem == NULL) { if (elem == NULL) {
snd_hctl_free(ctl); snd_hctl_free(hctl);
err = -ENOMEM; err = -ENOMEM;
goto _end; goto _end;
} }
elem->id = list.pids[idx]; elem->id = list.pids[idx];
elem->ctl = ctl; elem->hctl = hctl;
ctl->helems[idx] = elem; hctl->helems[idx] = elem;
list_add_tail(&elem->list, &ctl->hlist); list_add_tail(&elem->list, &hctl->hlist);
ctl->hcount++; hctl->hcount++;
} }
if (!ctl->hcompare) if (!hctl->hcompare)
ctl->hcompare = snd_hctl_compare_default; hctl->hcompare = snd_hctl_compare_default;
snd_hctl_sort(ctl); snd_hctl_sort(hctl);
if (ctl->callback) { if (hctl->callback) {
for (idx = 0; idx < ctl->hcount; idx++) { for (idx = 0; idx < hctl->hcount; idx++) {
int res = ctl->callback(ctl, SND_CTL_EVENT_ADD, int res = hctl->callback(hctl, SND_CTL_EVENT_ADD,
ctl->helems[idx]); hctl->helems[idx]);
if (res < 0) if (res < 0)
return res; return res;
} }
@ -346,49 +405,50 @@ int snd_hctl_build(snd_ctl_t *ctl)
return err; return err;
} }
void snd_hctl_set_callback(snd_ctl_t *ctl, snd_hctl_callback_t callback) void snd_hctl_set_callback(snd_hctl_t *hctl, snd_hctl_callback_t callback)
{ {
assert(ctl); assert(hctl);
ctl->callback = callback; hctl->callback = callback;
} }
void snd_hctl_set_callback_private(snd_ctl_t *ctl, void *callback_private) void snd_hctl_set_callback_private(snd_hctl_t *hctl, void *callback_private)
{ {
assert(ctl); assert(hctl);
ctl->callback_private = callback_private; hctl->callback_private = callback_private;
} }
void *snd_hctl_get_callback_private(snd_ctl_t *ctl) void *snd_hctl_get_callback_private(snd_hctl_t *hctl)
{ {
assert(ctl); assert(hctl);
return ctl->callback_private; return hctl->callback_private;
} }
unsigned int snd_hctl_get_count(snd_ctl_t *ctl) unsigned int snd_hctl_get_count(snd_hctl_t *hctl)
{ {
return ctl->hcount; return hctl->hcount;
} }
int snd_hctl_event(snd_ctl_t *ctl, snd_ctl_event_t *event) int snd_hctl_event(snd_hctl_t *hctl, snd_ctl_event_t *event)
{ {
snd_hctl_elem_t *elem; snd_hctl_elem_t *elem;
int res; int res;
assert(ctl); assert(hctl);
assert(hctl->ctl);
switch (event->type) { switch (event->type) {
case SND_CTL_EVENT_REMOVE: case SND_CTL_EVENT_REMOVE:
{ {
int dir; int dir;
res = _snd_hctl_find_elem(ctl, &event->data.id, &dir); res = _snd_hctl_find_elem(hctl, &event->data.id, &dir);
assert(res >= 0 && dir == 0); assert(res >= 0 && dir == 0);
if (res < 0 || dir != 0) if (res < 0 || dir != 0)
return -ENOENT; return -ENOENT;
snd_hctl_elem_remove(ctl, res); snd_hctl_elem_remove(hctl, res);
break; break;
} }
case SND_CTL_EVENT_VALUE: case SND_CTL_EVENT_VALUE:
case SND_CTL_EVENT_CHANGE: case SND_CTL_EVENT_CHANGE:
elem = snd_hctl_find_elem(ctl, &event->data.id); elem = snd_hctl_find_elem(hctl, &event->data.id);
assert(elem); assert(elem);
if (!elem) if (!elem)
return -ENOENT; return -ENOENT;
@ -403,16 +463,16 @@ int snd_hctl_event(snd_ctl_t *ctl, snd_ctl_event_t *event)
if (elem == NULL) if (elem == NULL)
return -ENOMEM; return -ENOMEM;
elem->id = event->data.id; elem->id = event->data.id;
elem->ctl = ctl; elem->hctl = hctl;
res = snd_hctl_elem_add(ctl, elem); res = snd_hctl_elem_add(hctl, elem);
if (res < 0) if (res < 0)
return res; return res;
break; break;
case SND_CTL_EVENT_REBUILD: case SND_CTL_EVENT_REBUILD:
snd_hctl_free(ctl); snd_hctl_free(hctl);
res = snd_hctl_build(ctl); res = snd_hctl_build(hctl);
if (ctl->callback) { if (hctl->callback) {
res = ctl->callback(ctl, event->type, NULL); res = hctl->callback(hctl, event->type, NULL);
if (res < 0) if (res < 0)
return res; return res;
} }
@ -424,14 +484,17 @@ int snd_hctl_event(snd_ctl_t *ctl, snd_ctl_event_t *event)
return 0; return 0;
} }
int snd_hctl_events(snd_ctl_t *ctl) int snd_hctl_events(snd_hctl_t *hctl)
{ {
snd_ctl_event_t event; snd_ctl_event_t event;
int res; int res;
while ((res = snd_ctl_read(ctl, &event)) != 0) {
assert(hctl);
assert(hctl->ctl);
while ((res = snd_ctl_read(hctl->ctl, &event)) != 0) {
if (res < 0) if (res < 0)
return res; return res;
res = snd_hctl_event(ctl, &event); res = snd_hctl_event(hctl, &event);
if (res < 0) if (res < 0)
return res; return res;
} }
@ -440,19 +503,28 @@ int snd_hctl_events(snd_ctl_t *ctl)
int snd_hctl_elem_info(snd_hctl_elem_t *elem, snd_ctl_elem_info_t *info) int snd_hctl_elem_info(snd_hctl_elem_t *elem, snd_ctl_elem_info_t *info)
{ {
assert(elem);
assert(elem->hctl);
assert(info);
info->id = elem->id; info->id = elem->id;
return snd_ctl_elem_info(elem->ctl, info); return snd_ctl_elem_info(elem->hctl->ctl, info);
} }
int snd_hctl_elem_read(snd_hctl_elem_t *elem, snd_ctl_elem_t * value) int snd_hctl_elem_read(snd_hctl_elem_t *elem, snd_ctl_elem_t * value)
{ {
assert(elem);
assert(elem->hctl);
assert(value);
value->id = elem->id; value->id = elem->id;
return snd_ctl_elem_read(elem->ctl, value); return snd_ctl_elem_read(elem->hctl->ctl, value);
} }
int snd_hctl_elem_write(snd_hctl_elem_t *elem, snd_ctl_elem_t * value) int snd_hctl_elem_write(snd_hctl_elem_t *elem, snd_ctl_elem_t * value)
{ {
assert(elem);
assert(elem->hctl);
assert(value);
value->id = elem->id; value->id = elem->id;
return snd_ctl_elem_write(elem->ctl, value); return snd_ctl_elem_write(elem->hctl->ctl, value);
} }

View file

@ -31,17 +31,17 @@
int snd_mixer_open(snd_mixer_t **mixerp, char *name) int snd_mixer_open(snd_mixer_t **mixerp, char *name)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
snd_ctl_t *ctl; snd_hctl_t *hctl;
int err; int err;
assert(mixerp); assert(mixerp);
if ((err = snd_ctl_open(&ctl, name)) < 0) if ((err = snd_hctl_open(&hctl, name)) < 0)
return err; return err;
mixer = calloc(1, sizeof(snd_mixer_t)); mixer = calloc(1, sizeof(snd_mixer_t));
if (mixer == NULL) { if (mixer == NULL) {
snd_ctl_close(ctl); snd_hctl_close(hctl);
return -ENOMEM; return -ENOMEM;
} }
mixer->ctl = ctl; mixer->hctl = hctl;
INIT_LIST_HEAD(&mixer->elems); INIT_LIST_HEAD(&mixer->elems);
*mixerp = mixer; *mixerp = mixer;
return 0; return 0;
@ -82,14 +82,14 @@ int snd_mixer_close(snd_mixer_t *mixer)
{ {
assert(mixer); assert(mixer);
snd_mixer_free(mixer); snd_mixer_free(mixer);
return snd_ctl_close(mixer->ctl); return snd_hctl_close(mixer->hctl);
} }
int snd_mixer_poll_descriptor(snd_mixer_t *mixer) int snd_mixer_poll_descriptor(snd_mixer_t *mixer)
{ {
if (mixer == NULL || mixer->ctl == NULL) if (mixer == NULL || mixer->hctl == NULL)
return -EIO; return -EIO;
return snd_ctl_poll_descriptor(mixer->ctl); return snd_hctl_poll_descriptor(mixer->hctl);
} }
snd_mixer_elem_t *snd_mixer_first_elem(snd_mixer_t *mixer) snd_mixer_elem_t *snd_mixer_first_elem(snd_mixer_t *mixer)
@ -126,6 +126,6 @@ snd_mixer_elem_t *snd_mixer_elem_prev(snd_mixer_elem_t *elem)
int snd_mixer_events(snd_mixer_t *mixer) int snd_mixer_events(snd_mixer_t *mixer)
{ {
return snd_hctl_events(mixer->ctl); return snd_hctl_events(mixer->hctl);
} }

View file

@ -46,7 +46,7 @@ struct _snd_mixer_elem {
}; };
struct _snd_mixer { struct _snd_mixer {
snd_ctl_t *ctl; snd_hctl_t *hctl;
struct list_head elems; /* list of all elemss */ struct list_head elems; /* list of all elemss */
unsigned int count; unsigned int count;
snd_mixer_callback_t callback; snd_mixer_callback_t callback;

View file

@ -112,39 +112,39 @@ static snd_hctl_elem_t *test_mixer_id(snd_mixer_t *mixer, const char *name, int
id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
strcpy(id.name, name); strcpy(id.name, name);
id.index = index; id.index = index;
helem = snd_hctl_find_elem(mixer->ctl, &id); helem = snd_hctl_find_elem(mixer->hctl, &id);
// fprintf(stderr, "Looking for control: '%s', %i (0x%lx)\n", name, index, (long)helem); // fprintf(stderr, "Looking for control: '%s', %i (0x%lx)\n", name, index, (long)helem);
return helem; return helem;
} }
static int get_mixer_info(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_info_t *info) static int get_mixer_info(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_info_t *info)
{ {
memset(info, 0, sizeof(*info)); snd_hctl_elem_t *helem = test_mixer_id(mixer, name, index);
info->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; if (helem == NULL)
strcpy(info->id.name, name); return -EINVAL;
info->id.index = index; return snd_hctl_elem_info(helem, info);
return snd_ctl_elem_info(mixer->ctl, info);
} }
static int get_mixer_read(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_t *control) static int get_mixer_read(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_t *control)
{ {
memset(control, 0, sizeof(*control)); snd_hctl_elem_t *helem = test_mixer_id(mixer, name, index);
control->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; if (helem == NULL)
strcpy(control->id.name, name); return -EINVAL;
control->id.index = index; return snd_hctl_elem_read(helem, control);
return snd_ctl_elem_read(mixer->ctl, control);
} }
static int put_mixer_write(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_t *control) static int put_mixer_write(snd_mixer_t *mixer, const char *name, int index, snd_ctl_elem_t *control)
{ {
snd_hctl_elem_t *helem = test_mixer_id(mixer, name, index);
if (helem == NULL)
return -EINVAL;
control->id.numid = 0; control->id.numid = 0;
control->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; control->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
strcpy(control->id.name, name); strcpy(control->id.name, name);
control->id.device = control->id.subdevice = 0; control->id.device = control->id.subdevice = 0;
control->id.index = index; control->id.index = index;
control->indirect = 0; control->indirect = 0;
memset(&control->reserved, 0, sizeof(control->reserved)); return snd_hctl_elem_write(helem, control);
return snd_ctl_elem_write(mixer->ctl, control);
} }
static int hctl_elem_event(snd_hctl_elem_t *helem, static int hctl_elem_event(snd_hctl_elem_t *helem,
@ -694,7 +694,7 @@ static int build_elem(snd_mixer_t *mixer, const char *sname)
hctl_elem_add(simple, helem); hctl_elem_add(simple, helem);
} else for (capture_item = 1; capture_item < csource_info.value.enumerated.items; capture_item++) { } else for (capture_item = 1; capture_item < csource_info.value.enumerated.items; capture_item++) {
csource_info.value.enumerated.item = capture_item; csource_info.value.enumerated.item = capture_item;
if ((err = snd_ctl_elem_info(mixer->ctl, &csource_info)) < 0) if ((err = snd_hctl_elem_info(helem, &csource_info)) < 0)
return err; return err;
if (!strcmp(csource_info.value.enumerated.name, str)) { if (!strcmp(csource_info.value.enumerated.name, str)) {
if (voices < csource_info.count) if (voices < csource_info.count)
@ -783,11 +783,11 @@ static int build_elem(snd_mixer_t *mixer, const char *sname)
return 0; return 0;
} }
int mixer_simple_ctl_callback(snd_ctl_t *ctl, int mixer_simple_ctl_callback(snd_hctl_t *hctl,
snd_ctl_event_type_t event, snd_ctl_event_type_t event,
snd_hctl_elem_t *elem ATTRIBUTE_UNUSED) snd_hctl_elem_t *elem ATTRIBUTE_UNUSED)
{ {
snd_mixer_t *mixer = snd_hctl_get_callback_private(ctl); snd_mixer_t *mixer = snd_hctl_get_callback_private(hctl);
int err; int err;
switch (event) { switch (event) {
case SND_CTL_EVENT_REBUILD: case SND_CTL_EVENT_REBUILD:
@ -835,19 +835,17 @@ int snd_mixer_simple_build(snd_mixer_t *mixer)
"Capture Boost", "Capture Boost",
NULL NULL
}; };
snd_ctl_t *ctl = mixer->ctl; snd_hctl_t *hctl = mixer->hctl;
char **elem = elems; char **elem = elems;
int err; int err;
if ((err = snd_hctl_build(ctl)) < 0)
return err;
while (*elem) { while (*elem) {
if ((err = build_elem(mixer, *elem)) < 0) if ((err = build_elem(mixer, *elem)) < 0)
return err; return err;
elem++; elem++;
} }
snd_hctl_set_callback(ctl, mixer_simple_ctl_callback); snd_hctl_set_callback(hctl, mixer_simple_ctl_callback);
snd_hctl_set_callback_private(ctl, mixer); snd_hctl_set_callback_private(hctl, mixer);
return 0; return 0;
} }