J'essaie de détecter un signal lorsqu'un casque est connecté ou déconnecté du système. Quelle est la meilleure façon de procéder?
S'il y a une carte spéciale avec pilotes, ce sera ma façon préférée.
/syslà, et peut-être des notifications via dbus.
J'essaie de détecter un signal lorsqu'un casque est connecté ou déconnecté du système. Quelle est la meilleure façon de procéder?
S'il y a une carte spéciale avec pilotes, ce sera ma façon préférée.
/syslà, et peut-être des notifications via dbus.
Réponses:
Dans mon linux (Debian GNU / Linux 3.12.0 x86_64), cela est connu par le système acpi, donc appeler acpi_listenmontre:
jack/microphone MICROPHONE plug
jack/headphone HEADPHONE plug
jack/microphone MICROPHONE unplug
jack/headphone HEADPHONE unplug
cela semble dépendre d'une option sélectionnée dans la configuration du noyau CONFIG_SND_HDA_INPUT_JACK
Si tel est votre cas, vous pouvez remplir /etc/acpi/events/des scripts pour déclencher ce que vous voulez.
Consultez la page de manuel acpid http://linux.die.net/man/8/acpid
Ces informations sont disponibles dans /proc/asound/card0/codec#0et dépendent du matériel. Pour mon ordinateur, c'est dans la section qui capture ces informations:
Casque connecté:
[...]
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:
[...]
Casque déconnecté (voir 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
[...]
Vous pouvez utiliser inotify pour vérifier si le fichier a été modifié et récupérer les informations.
Voir également https://askubuntu.com/questions/133809/mute-sound-on-headphone-unplug .
man inotifydit que les pseudo-fs comme / proc ne sont pas contrôlables avec inotify. Y at-il un travail autour?
acpi_listen(voir la réponse du théiste).
audio1au lieu de audio0pour que cela fonctionne, même si je n'ai qu'un simple ordinateur portable avec une seule carte son. donc c'est /proc/asound/card1/codec#0pour moi, ça pourrait être encore un autre numéro pour toi.
Trouvez le taggeur udev de la prise sur le système, connectez un client (dbus-monitor) qui surveille le bus à l'aide de DBus pour les messages sur la connexion et la déconnexion de la prise.
Si votre noyau a des entrées jack configurées
root@brix:~# grep CONFIG_SND_HDA_INPUT_JACK /boot/config-$(uname -r)
En utilisant, Evtestvous pouvez répertorier tous vos événements d'entrée.
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 ------------
Un outil appelé hda-verb peut activer / désactiver la prise casque à l'aide de broches.
Par exemple,
Pour activer la prise casque, utilisez:
./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0x40
Pour désactiver la prise casque, utilisez:
./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0
Puisque vous voulez simplement vérifier son état, vous pouvez peut-être utiliser un mécanisme d'interrogation dans votre programme java qui peut vérifier l'état des broches ci-dessus à l'aide de hda. Pour cela, votre programme java doit pouvoir appeler hda-verb. Alternativement, vous pouvez vérifier la source du verbe hda tel qu'il est disponible et voir comment ils l'ont fait.
Mon ordinateur portable dispose d'un système audio Conexant Intel HDA, mais les contrôles de volume de la prise micro et casque ne s'affichent jamais à l'intérieur des nombreux mélangeurs disponibles, même en même temps. Les haut-parleurs micro et casque ne peuvent être allumés ou éteints. Voici un petit script pour faire exactement cela:
#!/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