2009年2月2日月曜日

(Code: c) atc-ha7usb

USBヘッドホンの付属ボタン操作用に書いたCのコードです。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <stdlib.h>

#define CMD_MUTE "amixer set PCM mute >/dev/null 2>&1"
#define CMD_UNMUTE "amixer set PCM unmute >/dev/null 2>&1"
#define CMD_VOLUMEDOWN "amixer set PCM 10%- >/dev/null 2>&1"
#define CMD_VOLUMEUP "amixer set PCM 10%+ >/dev/null 2>&1"


int main()
{
int fd;
int flg_mute = 0;

fd = open("/dev/input/atc-ha7usb", O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}

while (1) {
struct input_event event;

if (read(fd, &event, sizeof(event)) != sizeof(event)) {
close(fd);
exit(EXIT_FAILURE);
}
if (event.type == EV_KEY && event.value) {
switch(event.code) {
case KEY_MUTE:
if (!flg_mute) {
system(CMD_MUTE);
flg_mute = 1;
} else {
system(CMD_UNMUTE);
flg_mute = 0;
}
break;
case KEY_VOLUMEDOWN:
system(CMD_VOLUMEDOWN);
break;
case KEY_VOLUMEUP:
system(CMD_VOLUMEUP);
break;
default:
break;
}
}
}

close(fd);
return 0;
}

このコードはこちらの記事のように使っています。

USBヘッドホンのinputデバイス利用
http://p2pob.blogspot.com/2009/02/usbinput.html



環境

OS: Linux
debian-lenny(i386)
gcc-4.3.2