1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/* $Id: hw_alsa_usb.c,v 5.5 2010/04/11 18:50:38 lirc Exp $ */
/****************************************************************************
** hw_alsa_usb.c ***********************************************************
****************************************************************************
*
* routines for Sound Blaster USB audio devices accessed via ALSA hwdep
*
* Copyright (c) 2005 Clemens Ladisch <clemens@ladisch.de>
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <alsa/asoundlib.h>
#include "lircd.h"
#include "hardware.h"
static int init(void);
static int deinit(void);
static int decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int *repeat_flagp,
lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp);
static char *rec(struct ir_remote *remotes);
static ir_code code, last_code;
static snd_hwdep_t *hwdep;
static struct timeval last_time;
static int repeat_flag;
struct hardware hw_alsa_usb = {
"", /* default device */
-1, /* fd */
LIRC_CAN_REC_LIRCCODE, /* features */
0, /* send_mode */
LIRC_MODE_LIRCCODE, /* rec_mode */
8, /* code_length */
init, /* init_func */
deinit, /* deinit_func */
NULL, /* send_func */
rec, /* rec_func */
decode, /* decode_func */
NULL, /* ioctl_func */
NULL, /* readdata */
"alsa_usb"
};
static const char *search_device(void)
{
int card, err;
snd_hwdep_info_t *info;
snd_hwdep_info_alloca(&info);
card = -1;
while (snd_card_next(&card) >= 0 && card >= 0) {
char ctl_name[20];
snd_ctl_t *ctl;
int device;
sprintf(ctl_name, "hw:CARD=%d", card);
err = snd_ctl_open(&ctl, ctl_name, SND_CTL_NONBLOCK);
if (err < 0)
continue;
device = -1;
while (snd_ctl_hwdep_next_device(ctl, &device) >= 0 && device >= 0) {
snd_hwdep_info_set_device(info, device);
err = snd_ctl_hwdep_info(ctl, info);
if (err >= 0 && snd_hwdep_info_get_iface(info) == SND_HWDEP_IFACE_SB_RC) {
static char name[36];
sprintf(name, "hw:CARD=%d,DEV=%d", card, device);
snd_ctl_close(ctl);
return name;
}
}
snd_ctl_close(ctl);
}
return NULL;
}
static int init(void)
{
const char *device;
snd_hwdep_info_t *info;
struct pollfd pollfd;
int err;
device = hw.device;
if (!device || !*device) {
device = search_device();
if (!device) {
logprintf(LOG_ERR, "device not found");
return 0;
}
}
err = snd_hwdep_open(&hwdep, device, SND_HWDEP_OPEN_READ);
if (err < 0) {
logprintf(LOG_ERR, "cannot open %s: %s", device, snd_strerror(err));
return 0;
}
snd_hwdep_info_alloca(&info);
err = snd_hwdep_info(hwdep, info);
if (err < 0) {
snd_hwdep_close(hwdep);
logprintf(LOG_ERR, "cannot get hwdep info: %s", snd_strerror(err));
return 0;
}
if (snd_hwdep_info_get_iface(info) != SND_HWDEP_IFACE_SB_RC) {
snd_hwdep_close(hwdep);
logprintf(LOG_ERR, "%s is not a Sound Blaster remote control device", device);
return 0;
}
err = snd_hwdep_poll_descriptors(hwdep, &pollfd, 1);
if (err < 0) {
snd_hwdep_close(hwdep);
logprintf(LOG_ERR, "cannot get file descriptor: %s", snd_strerror(err));
return 0;
}
if (err != 1) {
snd_hwdep_close(hwdep);
logprintf(LOG_ERR, "invalid number of file descriptors (%d): %s", err, snd_strerror(err));
return 0;
}
hw.fd = pollfd.fd;
return 1;
}
static int deinit(void)
{
snd_hwdep_close(hwdep);
hw.fd = -1;
return 1;
}
static char *rec(struct ir_remote *remotes)
{
unsigned char rc_code;
ssize_t size;
struct timeval current;
size = snd_hwdep_read(hwdep, &rc_code, 1);
if (size < 1)
return NULL;
gettimeofday(¤t, NULL);
last_code = code;
code = (ir_code) rc_code;
/* delay for repeating buttons is up to 320 ms */
repeat_flag = code == last_code && current.tv_sec - last_time.tv_sec <= 2
&& time_elapsed(&last_time, ¤t) <= 350000;
last_time = current;
LOGPRINTF(1, "code: %llx", (__u64) code);
LOGPRINTF(1, "repeat_flag: %d", repeat_flag);
return decode_all(remotes);
}
static int decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int *repeat_flagp,
lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp)
{
if (!map_code(remote, prep, codep, postp, 0, 0, 8, code, 0, 0)) {
return (0);
}
*repeat_flagp = repeat_flag;
*min_remaining_gapp = 0;
*max_remaining_gapp = 0;
return 1;
}
|