1999-03-08 16:51:36 +00:00
|
|
|
#include <stdio.h>
|
1999-03-11 13:07:12 +00:00
|
|
|
#include <stdlib.h>
|
1999-03-08 16:51:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include "../include/asoundlib.h"
|
|
|
|
|
|
1999-03-08 20:06:32 +00:00
|
|
|
void show_status(void *handle)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
snd_timer_status_t status;
|
|
|
|
|
|
|
|
|
|
if ((err = snd_timer_status(handle, &status)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer status %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("STATUS:\n");
|
|
|
|
|
printf(" resolution = %lu\n", status.resolution);
|
|
|
|
|
printf(" lost = %lu\n", status.lost);
|
|
|
|
|
printf(" overrun = %lu\n", status.overrun);
|
|
|
|
|
printf(" queue = %i\n", status.queue);
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-11 13:07:12 +00:00
|
|
|
void read_loop(void *handle, int master_ticks, int timeout)
|
1999-03-08 16:51:36 +00:00
|
|
|
{
|
1999-03-11 13:07:12 +00:00
|
|
|
int err, max;
|
|
|
|
|
fd_set in;
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
snd_timer_read_t tr;
|
|
|
|
|
|
|
|
|
|
while (master_ticks-- > 0) {
|
|
|
|
|
FD_ZERO(&in);
|
2000-09-24 09:57:26 +00:00
|
|
|
max = snd_timer_poll_descriptor(handle);
|
1999-03-11 13:07:12 +00:00
|
|
|
FD_SET(max, &in);
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
if ((err = select(max + 1, &in, NULL, NULL, &tv)) < 0) {
|
|
|
|
|
fprintf(stderr, "select error %i (%s)\n", err, strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
if (err == 0) {
|
|
|
|
|
fprintf(stderr, "timer time out!!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
|
|
|
|
|
printf("TIMER: resolution = %lu, ticks = %lu\n",
|
|
|
|
|
tr.resolution, tr.ticks);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-03 21:41:29 +00:00
|
|
|
int main(int argc, char *argv[])
|
1999-03-11 13:07:12 +00:00
|
|
|
{
|
|
|
|
|
int idx, err;
|
2001-03-29 11:18:16 +00:00
|
|
|
int class = SND_TIMER_CLASS_GLOBAL;
|
|
|
|
|
int sclass = SND_TIMER_CLASS_NONE;
|
2000-11-30 19:17:55 +00:00
|
|
|
int card = 0;
|
|
|
|
|
int device = SND_TIMER_GLOBAL_SYSTEM;
|
|
|
|
|
int subdevice = 0;
|
|
|
|
|
int list = 0;
|
1999-06-03 21:41:29 +00:00
|
|
|
snd_timer_t *handle;
|
1999-03-08 20:06:32 +00:00
|
|
|
snd_timer_select_t sel;
|
|
|
|
|
snd_timer_info_t info;
|
|
|
|
|
snd_timer_params_t params;
|
1999-03-08 16:51:36 +00:00
|
|
|
|
1999-03-11 13:07:12 +00:00
|
|
|
idx = 1;
|
|
|
|
|
while (idx < argc) {
|
2001-03-29 11:18:16 +00:00
|
|
|
if (!strncmp(argv[idx], "class=", 5)) {
|
|
|
|
|
class = atoi(argv[idx]+5);
|
|
|
|
|
} else if (!strncmp(argv[idx], "sclass=", 6)) {
|
|
|
|
|
sclass = atoi(argv[idx]+6);
|
2000-11-30 19:17:55 +00:00
|
|
|
} else if (!strncmp(argv[idx], "card=", 5)) {
|
|
|
|
|
card = atoi(argv[idx]+5);
|
|
|
|
|
} else if (!strncmp(argv[idx], "device=", 7)) {
|
|
|
|
|
device = atoi(argv[idx]+7);
|
|
|
|
|
} else if (!strncmp(argv[idx], "subdevice=", 10)) {
|
|
|
|
|
subdevice = atoi(argv[idx]+10);
|
|
|
|
|
} else if (!strcmp(argv[idx], "list")) {
|
|
|
|
|
list = 1;
|
1999-03-11 13:07:12 +00:00
|
|
|
}
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
2001-03-29 11:18:16 +00:00
|
|
|
if (class == SND_TIMER_CLASS_SLAVE && sclass == SND_TIMER_SCLASS_NONE) {
|
|
|
|
|
fprintf(stderr, "slave class is not set\n");
|
1999-03-11 13:07:12 +00:00
|
|
|
exit(0);
|
|
|
|
|
}
|
1999-03-08 16:51:36 +00:00
|
|
|
if ((err = snd_timer_open(&handle))<0) {
|
|
|
|
|
fprintf(stderr, "timer open %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2000-11-30 19:17:55 +00:00
|
|
|
if (list) {
|
|
|
|
|
bzero(&sel.id, sizeof(sel.id));
|
2001-03-29 11:18:16 +00:00
|
|
|
sel.id.class = -1;
|
2000-11-30 19:17:55 +00:00
|
|
|
while (1) {
|
|
|
|
|
if ((err = snd_timer_next_device(handle, &sel.id)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer next device error: %s\n", snd_strerror(err));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-03-29 11:18:16 +00:00
|
|
|
if (sel.id.class < 0)
|
2000-11-30 19:17:55 +00:00
|
|
|
break;
|
2001-03-29 11:18:16 +00:00
|
|
|
printf("Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
|
|
|
|
|
sel.id.class, sel.id.sclass, sel.id.card, sel.id.device, sel.id.subdevice);
|
2000-11-30 19:17:55 +00:00
|
|
|
}
|
1999-03-08 16:51:36 +00:00
|
|
|
}
|
2001-03-29 11:18:16 +00:00
|
|
|
printf("Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n", class, sclass, card, device, subdevice);
|
1999-03-08 20:06:32 +00:00
|
|
|
bzero(&sel, sizeof(sel));
|
2001-03-29 11:18:16 +00:00
|
|
|
sel.id.class = class;
|
|
|
|
|
sel.id.sclass = sclass;
|
2000-11-30 19:17:55 +00:00
|
|
|
sel.id.card = card;
|
|
|
|
|
sel.id.device = device;
|
|
|
|
|
sel.id.subdevice = subdevice;
|
1999-03-08 20:06:32 +00:00
|
|
|
if ((err = snd_timer_select(handle, &sel)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer select %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2001-03-29 11:18:16 +00:00
|
|
|
if (class != SND_TIMER_CLASS_SLAVE) {
|
1999-03-11 13:07:12 +00:00
|
|
|
if ((err = snd_timer_info(handle, &info)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer info %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2000-11-30 19:17:55 +00:00
|
|
|
printf("Timer info:\n");
|
1999-03-11 13:07:12 +00:00
|
|
|
printf(" flags = 0x%x\n", info.flags);
|
|
|
|
|
printf(" id = '%s'\n", info.id);
|
|
|
|
|
printf(" name = '%s'\n", info.name);
|
|
|
|
|
printf(" max ticks = %lu\n", info.ticks);
|
|
|
|
|
printf(" average resolution = %lu\n", info.resolution);
|
|
|
|
|
bzero(¶ms, sizeof(params));
|
|
|
|
|
params.flags = SND_TIMER_PSFLG_AUTO;
|
|
|
|
|
params.ticks = (1000000000 / info.resolution) / 50; /* 50Hz */
|
|
|
|
|
if (params.ticks < 1)
|
|
|
|
|
params.ticks = 1;
|
|
|
|
|
printf("Using %lu tick(s)\n", params.ticks);
|
|
|
|
|
if ((err = snd_timer_params(handle, ¶ms)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer params %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
1999-03-08 20:06:32 +00:00
|
|
|
}
|
|
|
|
|
show_status(handle);
|
|
|
|
|
if ((err = snd_timer_start(handle)) < 0) {
|
|
|
|
|
fprintf(stderr, "timer start %i (%s)\n", err, snd_strerror(err));
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2001-03-29 11:18:16 +00:00
|
|
|
read_loop(handle, 25, class == SND_TIMER_CLASS_SLAVE ? 10000 : 1);
|
1999-03-08 20:06:32 +00:00
|
|
|
show_status(handle);
|
1999-03-08 16:51:36 +00:00
|
|
|
snd_timer_close(handle);
|
1999-06-03 21:41:29 +00:00
|
|
|
return 0;
|
1999-03-08 16:51:36 +00:00
|
|
|
}
|