连接耳机或从系统断开连接时,我正在尝试检测信号。做这个的最好方式是什么?
如果有带有驱动程序的特殊板,那将是我的首选方法。
/sys
那时提供信息,也许还会通过dbus 提供通知。
连接耳机或从系统断开连接时,我正在尝试检测信号。做这个的最好方式是什么?
如果有带有驱动程序的特殊板,那将是我的首选方法。
/sys
那时提供信息,也许还会通过dbus 提供通知。
Answers:
在我的Linux(Debian GNU / Linux 3.12.0 x86_64)中,acpi系统知道这一点,因此调用acpi_listen
显示:
jack/microphone MICROPHONE plug
jack/headphone HEADPHONE plug
jack/microphone MICROPHONE unplug
jack/headphone HEADPHONE unplug
这似乎取决于内核配置中的选定选项 CONFIG_SND_HDA_INPUT_JACK
如果是这种情况,则可以填充 /etc/acpi/events/
脚本以触发所需的任何内容。
检查acpid手册页http://linux.die.net/man/8/acpid
此信息在其中可用,/proc/asound/card0/codec#0
并取决于硬件。对于我的计算机,它位于捕获此信息的部分中:
耳机已连接:
[...]
Node 0x0d [Pin Complex] wcaps 0x400181: Stereo
Control: name="Speaker Phantom Jack", index=0, device=0
Pincap 0x00000014: OUT Detect
Pin Default 0x90170110: [Fixed] Speaker at Int N/A
Conn = Analog, Color = Unknown
DefAssociation = 0x1, Sequence = 0x0
Misc = NO_PRESENCE
Pin-ctls: 0x00:
[...]
耳机已断开连接(请参阅Pin-ctls
):
[...]
Node 0x0d [Pin Complex] wcaps 0x400181: Stereo
Control: name="Speaker Phantom Jack", index=0, device=0
Pincap 0x00000014: OUT Detect
Pin Default 0x90170110: [Fixed] Speaker at Int N/A
Conn = Analog, Color = Unknown
DefAssociation = 0x1, Sequence = 0x0
Misc = NO_PRESENCE
Pin-ctls: 0x40: OUT
[...]
您可以使用inotify来检查文件是否被修改并grep信息。
另请参阅https://askubuntu.com/questions/133809/mute-sound-on-headphone-unplug。
man inotify
说像/ proc这样的伪fs不能用inotify监视。有解决方法吗?
acpi_listen
(请参阅有神论者的答案)。
audio1
来代替audio0
它。所以/proc/asound/card1/codec#0
对我来说,可能对您来说是另一个号码。
在系统上找到插孔的udev标记器,连接一个客户端(dbus-monitor),该客户端使用DBus监视总线,以获取有关插孔连接和断开连接的消息。
如果您的内核已配置插孔输入
root@brix:~# grep CONFIG_SND_HDA_INPUT_JACK /boot/config-$(uname -r)
使用Evtest
您可以列出所有输入事件。
root@brix:/etc/acpi# evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: Power Button
/dev/input/event1: Power Button
/dev/input/event2: Logitech Logitech BT Mini-Receiver
/dev/input/event3: CM Storm QuickFire Rapid keyboard
/dev/input/event4: CM Storm QuickFire Rapid keyboard
/dev/input/event5: PixArt Microsoft USB Optical Mouse
/dev/input/event6: Logitech Logitech BT Mini-Receiver
/dev/input/event7: Video Bus
/dev/input/event8: HDA Intel HDMI HDMI/DP,pcm=3
/dev/input/event9: HDA Intel HDMI HDMI/DP,pcm=7
/dev/input/event10: HDA Intel HDMI HDMI/DP,pcm=8
/dev/input/event11: HDA Intel PCH Front Mic
/dev/input/event12: HDA Intel PCH Rear Mic
/dev/input/event13: HDA Intel PCH Line
/dev/input/event14: HDA Intel PCH Line Out
/dev/input/event15: HDA Intel PCH Front Headphone
Select the device event number [0-15]: 14
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "HDA Intel PCH Line Out"
Supported events:
Event type 0 (EV_SYN)
Event type 5 (EV_SW)
Event code 6 (SW_LINEOUT_INSERT)
Properties:
Testing ... (interrupt to exit)
Event: time 1465927534.591787, type 5 (EV_SW), code 6 (SW_LINEOUT_INSERT), value 0
Event: time 1465927534.591787, -------------- EV_SYN ------------
Event: time 1465927536.618428, type 5 (EV_SW), code 6 (SW_LINEOUT_INSERT), value 1
Event: time 1465927536.618428, -------------- EV_SYN ------------
称为hda-verb的工具可以使用引脚启用/禁用耳机插孔。
例如,
要启用耳机插孔,请使用:
./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0x40
要禁用耳机插孔,请使用:
./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0
由于您只想检查其状态,因此也许可以在Java程序中使用某种轮询机制,该机制可以使用hda检查上述引脚的状态。为此,您的Java程序应该能够调用hda-verb。另外,您可以检查hda-verb的来源(如果可用),并查看他们的操作方式。
我的笔记本电脑具有Conexant Intel HDA音频系统,但是麦克风和耳机插孔的音量控制从未出现在许多可用的混音器中,甚至包括alsamixer。麦克风和耳机扬声器只能打开或关闭。这是一个很小的脚本,可以做到这一点:
#!/bin/bash
#
# Switching on or off your headphone speaker and mic jacks
# and at the same time switching off or on your laptop front speakers.
# requires hda-verb-0.3-6-mdv2011.0.x86_64
#
# Before putting it in place make sure to test your PIN_WIDGET_CONTROL's
# with su -c 'python2 hda-analyzer.py' available here :
# http://www.alsa-project.org/hda-analyzer.py
#
PIN_CONFIGS=/sys/class/sound/hwC0D0/init_pin_configs
if [ ! -f $PIN_CONFIGS ]; then
echo "Your kernel is missing CONFIG_SND_HDA_HWDEP=y"
exit 0
fi
if [ ! -f /usr/sbin/hda-verb ]; then
echo "This script requires hda-verb-0.3-6-mdv2011.0.x86_64"
exit 0
fi
PINS_PRESENT=`cat $PIN_CONFIGS | awk '{print $1}'`
if [ `basename $0` = "speakers-off.sh" ]; then
# Headset (Mic (Node 0x1b) + Headphone Drive (Node 0x19)) : ON
# Laptop Speaker (Node 0x1f) : OFF
[ `echo "$PINS_PRESENT" | grep 0x19` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x19 SET_PIN_WIDGET_CONTROL 0x40
[ `echo "$PINS_PRESENT" | grep 0x1f` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x1f SET_PIN_WIDGET_CONTROL 0
[ `echo "$PINS_PRESENT" | grep 0x1b` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x1b SET_PIN_WIDGET_CONTROL 0x64
fi
if [ `basename $0` = "speakers-on.sh" ]; then
# Headset (Mic (Node 0x1b) + Headphone Drive (Node 0x19)) : OFF
# Laptop Speaker (Node 0x1f) : ON
[ `echo "$PINS_PRESENT" | grep 0x19` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x19 SET_PIN_WIDGET_CONTROL 0
[ `echo "$PINS_PRESENT" | grep 0x1f` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x1f SET_PIN_WIDGET_CONTROL 0x40
[ `echo "$PINS_PRESENT" | grep 0x1b` ] &&
/usr/sbin/hda-verb /dev/snd/hwC0D0 0x1b SET_PIN_WIDGET_CONTROL 0x24
fi
exit 0