Created
October 26, 2020 14:48
-
-
Save khng300/b9b3ccc86c24691273dc36c85066e2dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/nv.h> | |
#include <sys/sndstat.h> | |
int | |
main() | |
{ | |
int fd; | |
struct sndstat_nvlbuf_arg arg; | |
const nvlist_t * const *di; | |
size_t i, nitems; | |
nvlist_t *nvl; | |
/* Open sndstat node in read-only first */ | |
fd = open("/dev/sndstat", O_RDONLY); | |
/* Get the size of snapshot, when nbytes = 0 */ | |
arg.nbytes = 0; | |
arg.buf = NULL; | |
ioctl(fd, SNDSTAT_GET_DEVS, &arg); | |
/* Get snapshot data */ | |
arg.buf = malloc(arg.nbytes); | |
ioctl(fd, SNDSTAT_GET_DEVS, &arg); | |
/* Deserialize the nvlist stream */ | |
nvl = nvlist_unpack(arg.buf, arg.nbytes, 0); | |
free(arg.buf); | |
/* Get DSPs array */ | |
di = nvlist_get_nvlist_array(nvl, SNDSTAT_LABEL_DSPS, &nitems); | |
for (i = 0; i < nitems; i++) { | |
const char *nameunit, *devnode, *desc; | |
/* | |
* Examine each device nvlist item | |
*/ | |
nameunit = nvlist_get_string(di[i], SNDSTAT_LABEL_NAMEUNIT); | |
devnode = nvlist_get_string(di[i], SNDSTAT_LABEL_DEVNODE); | |
desc = nvlist_get_string(di[i], SNDSTAT_LABEL_DESC); | |
printf("Name unit: `%s`, Device node: `%s`, Description: `%s`\n", | |
nameunit, devnode, desc); | |
} | |
nvlist_destroy(nvl); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment